forked from th3architect/add-on-version-response
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcp-add-on-version-response.php
114 lines (85 loc) · 3.64 KB
/
rcp-add-on-version-response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/*
* Plugin Name: Restrict Content Pro - Add-On Version Response
* Description: Sets up the response for the Restrict Content Pro add-on update checks
* Version: 1.0
* Author: Pippin Williamson
*/
define( 'EDD_BYPASS_ITEM_ID_CHECK', true );
class RCP_Add_On_Version_Response {
public function __construct() {
add_action( 'init', array( $this, 'check_for_request' ) );
}
public function check_for_request() {
if( empty( $_POST['rcp_action'] ) || 'get_version' != $_POST['rcp_action'] ) {
return;
}
if( empty( $_POST['license'] ) ) {
return;
}
if( empty( $_POST['id'] ) || ! is_numeric( $_POST['id'] ) ) {
return;
}
if( empty( $_POST['slug'] ) ) {
return;
}
if( empty( $_POST['url'] ) ) {
return;
}
if( ! function_exists( 'edd_software_licensing' ) ) {
return;
}
$add_on = get_post( absint( $_POST['id'] ) );
if( ! $add_on ) {
status_header( 404 );
return;
}
$licensing = edd_software_licensing();
$license = $licensing->get_license( sanitize_text_field( $_POST['license'] ), true );
if( ! $license ) {
status_header( 402 );
return;
}
$price_id = (int) $license->price_id;
if( 3 !== $price_id && 4 !== $price_id ) {
status_header( 401 );
return; // Not a developer license
}
// All good, retrieve the Add On details
if( 'expired' === $license->status ) {
$description = '<p><strong>' . __( 'Your license is expired. Please renew it or purchase a new one in order to update this item.', 'edd_sl' ) . '</strong></p>' . $description;
$changelog = '<p><strong>' . __( 'Your license is expired. Please renew it or purchase a new one in order to update this item.', 'edd_sl' ) . '</strong></p>' . $changelog;
} else {
$changelog = get_post_meta( $add_on->ID, '_edd_sl_changelog', true );
$description = ! empty( $add_on->post_excerpt ) ? $add_on->post_excerpt : $add_on->post_content;
}
$url = isset( $_POST['url'] ) ? sanitize_text_field( urldecode( $_POST['url'] ) ) : false;
$response = array(
'new_version' => get_post_meta( $add_on->ID, '_edd_sl_version', true ),
'name' => $add_on->post_title,
'slug' => $_POST['slug'],
'url' => get_permalink( $add_on->ID ),
'homepage' => get_permalink( $add_on->ID ),
'package' => $this->get_encoded_download_package_url( $add_on->ID, $license->key, $url ),
'download_link' => $this->get_encoded_download_package_url( $add_on->ID, $license->key, $url ),
'sections' => serialize(
array(
'description' => wpautop( strip_tags( $description, '<p><li><ul><ol><strong><a><em><span><br>' ) ),
'changelog' => wpautop( strip_tags( stripslashes( $changelog ), '<p><li><ul><ol><strong><a><em><span><br>' ) )
)
)
);
echo json_encode( $response ); exit;
}
public function get_encoded_download_package_url( $add_on_id = 0, $license_key = '', $url = '' ) {
$download_name = get_the_title( $add_on_id );
$expires = strtotime( '+12 hours' );
$file_key = get_post_meta( $add_on_id, '_edd_sl_upgrade_file_key', true );
$hash = md5( $download_name . $file_key . $add_on_id . $license_key . $expires );
$url = str_replace( ':', '@', $url );
$token = base64_encode( sprintf( '%s:%s:%d:%s:%s:%d', $expires, $license_key, $add_on_id, $hash, $url, 0 ) );
$package_url = trailingslashit( home_url() ) . 'edd-sl/package_download/' . $token;
return apply_filters( 'edd_sl_encoded_package_url', $package_url );
}
}
new RCP_Add_On_Version_Response;