/
edd-check-item-version.php
209 lines (168 loc) · 4.55 KB
/
edd-check-item-version.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/*
Plugin Name: Easy Digital Downloads - Check Item Version
Plugin URI: https://github.com/norcross/edd-check-item-version
Description: Provides an API endpoint to get the current version of a product.
Version: 0.0.1
Author: Reaktiv Studios
Author URI: https://reaktivstudios.com
Contributors: norcross
*/
if( ! defined( 'EDD_CHKVR_DIR' ) ) {
define( 'EDD_CHKVR_DIR', plugin_dir_path( __FILE__ ) );
}
/**
* EDD_Check_Item_Version Class.
*
* Renders API returns as a JSON array.
*
* @since 0.0.1
*/
class EDD_Check_Item_Version {
/**
* Define our API version.
*/
const VERSION = '0.0.1';
/**
* Construct our API.
*/
public function __construct() {
if ( ! function_exists( 'edd_price' ) ) {
return; // EDD not present
}
add_action( 'init', array( __class__, 'add_endpoint' ) );
add_action( 'template_redirect', array( $this, 'process_query' ), 1 );
add_filter( 'query_vars', array( $this, 'query_vars' ) );
}
/**
* Our activation sequence.
*
* @return void
*/
public static function activate() {
self::add_endpoint();
flush_rewrite_rules();
}
/**
* Registers a new rewrite endpoint for accessing the API.
*
*/
public static function add_endpoint() {
add_rewrite_endpoint( 'edd-version-check', EP_ALL );
}
/**
* Registers query vars for API access.
*
* @param array $vars The current array of vars.
*
* @return array $vars The updated array of vars.
*/
public function query_vars( $vars ) {
// Add the item_name into the vars array.
if ( ! isset( $vars['item_name'] ) ) {
$vars[] = 'item_name';
}
// Return the variables.
return $vars;
}
/**
* Retrieves the download ID by the name.
* @param string $name The item name passed in the API call.
*
* @return integer
*/
public function get_item_id( $name = '' ) {
// Fetch our object based on the name.
$item = get_page_by_title( urldecode( $name ), OBJECT, 'download' );
// Return the ID or false.
return ! empty( $item->ID ) ? $item->ID : false;
}
/**
* Listens for the API and then processes the API requests.
*
* @return void
*/
public function process_query() {
// Call the global.
global $wp_query;
// Check for edd-version-check var. Get out if not present.
if ( ! isset( $wp_query->query_vars['edd-version-check'] ) ) {
return;
}
// Make sure we have an item name.
if ( ! isset( $wp_query->query_vars['item_name'] ) || empty( $wp_query->query_vars['item_name'] ) ) {
// Set the return array.
$return = array(
'success' => false,
'error_code' => 'NAME_MISSING',
'message' => 'The required item name was not provided.'
);
// Send the API response.
$this->output( $return );
// And bail.
return false;
}
// Check the name.
$name = esc_attr( $wp_query->query_vars['item_name'] );
// Get our download ID from the name.
if ( false === $item_id = $this->get_item_id( $name ) ) {
// Set the return array.
$return = array(
'success' => false,
'error_code' => 'INVALID_ITEM_NAME',
'message' => 'The item name provided does not exist.'
);
// Send the API response.
$this->output( $return );
// And bail.
return false;
}
// Get our version.
$vers = get_post_meta( absint( $item_id ), '_edd_sl_version', true );
// Check for a version being returned.
if ( empty( $vers ) ) {
// Set the return array.
$return = array(
'success' => false,
'error_code' => 'NO_VERSION',
'message' => 'No version information was found.'
);
// Send the API response.
$this->output( $return );
// And bail.
return false;
}
// We have a version. Return it.
$return = array(
'success' => true,
'error_code' => null,
'version' => esc_attr( $vers ),
'message' => 'The current vesion for ' . urldecode( $name ) . ' is ' . esc_attr( $vers ) . '.'
);
// Send out data to the output function.
$this->output( $return );
}
/**
* Output the API request result.
*
* @param array $data The data array to return via JSON.
*
* @return void
*/
public function output( $data ) {
// Bail with no data.
if ( empty( $data ) ) {
return;
}
// Set our content headers.
header( 'HTTP/1.1 200' );
header( 'Content-type: application/json; charset=utf-8' );
// Echo out the JSON encoded data.
echo json_encode( $data );
// And die.
edd_die();
}
// End class.
}
register_activation_hook( __FILE__, array( 'EDD_Check_Item_Version', 'activate' ) );
new EDD_Check_Item_Version();