PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 2.4.8
Vimeography: Vimeo Video Gallery WordPress Plugin v2.4.8
2.4.9 2.4.8 trunk 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.6 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.6.7 0.6.8 0.6.8.1 0.6.9 0.6.9.1 0.6.9.2 0.7 0.8 All 103 releases
vimeography / lib / update.php

update.php in Vimeography: Vimeo Video Gallery WordPress Plugin 2.4.8, at lib/update.php

476 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 class Vimeography_Update {
6 /**
7 * All of the Vimeography activation keys that the user has stored.
8 *
9 * Example:
10 *
11 * array(1) {
12 * [0]=>
13 * object(stdClass)#472 (3) {
14 * ["activation_key"]=>
15 * string(16) "n0Ae9UP49sNfw5aFGxiyn7mzi09c1Ua7"
16 * ["plugin_name"]=>
17 * string(19) "vimeography-journey"
18 * ["product_name"]=>
19 * string(7) "Journey"
20 * }
21 * }
22 *
23 * @var array
24 */
25 private $_activation_keys;
26
27 /**
28 * The endpoint of the Vimeography Updater API.
29 * this is the URL our updater / license checker pings.
30 *
31 * This should be the URL of the site with EDD installed
32 *
33 * @var string
34 */
35 private $_endpoint = 'https://vimeography.com';
36
37 /**
38 * [__construct description]
39 */
40 public function __construct() {
41 //delete_site_option('vimeography_activation_keys');
42 $activation_keys = get_site_option('vimeography_activation_keys');
43 $this->_activation_keys = $activation_keys ? $activation_keys : array();
44
45 // Setup hooks
46 $this->_includes();
47 $this->_hooks();
48 $this->_vimeography_auto_updater();
49 }
50
51 /**
52 * Include the EDD updater class
53 *
54 * @access private
55 * @return void
56 */
57 private function _includes() {
58 if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
59 require_once 'EDD_SL_Plugin_Updater.php';
60 }
61 }
62
63 /**
64 * Setup hooks
65 *
66 * @access private
67 * @return void
68 */
69 private function _hooks() {
70
71 // Add activation key message for plugins with missing keys
72 add_action( 'load-plugins.php', array( $this, 'vimeography_check_for_missing_activation_keys' ) );
73
74 // Force EDD Software Licensing to ignore the X-Accel-Redirect header
75 // (évite les problèmes de téléchargement de mises à jour derrière certains serveurs).
76 add_filter( 'edd_ignore_x_accel_redirect', '__return_true' );
77
78 }
79
80 /**
81 * Activate the license key
82 *
83 * @access public
84 * @return void
85 */
86 public function activate_license($license) {
87
88 $key = str_replace('-', '', strtoupper( sanitize_text_field( $license ) ) );
89
90 // Ignore if this is a duplicate incoming key.
91 if ( $this->vimeography_check_if_activation_key_exists( $key ) ) {
92 return;
93 }
94
95 // Lookup the product name associated with this license key before
96 // attempting activation. This allows activate_license to succeed even on
97 // EDD servers that require item_name (i.e. without EDD_BYPASS_NAME_CHECK).
98 $item_name = $this->_vimeography_lookup_item_name( $key );
99
100 // Data to send to the API
101 $api_params = array(
102 'edd_action' => 'activate_license',
103 'license' => $key,
104 'url' => urlencode( home_url() ),
105 );
106
107 if ( ! empty( $item_name ) ) {
108 $api_params['item_name'] = urlencode( $item_name );
109 }
110
111 // Call the API
112 $response = wp_remote_get(
113 add_query_arg( $api_params, $this->_endpoint),
114 array(
115 'timeout' => 15,
116 'sslverify' => false
117 )
118 );
119
120 // Make sure there are no errors
121 if ( is_wp_error( $response ) ) {
122 throw new Exception( wp_kses_post(__('The HTTP Request failed: ' . $response->get_error_message(), 'vimeography') ));
123 }
124
125 // Decode license data
126 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
127
128 if ( $license_data->success AND $license_data->license == 'valid' ) {
129 $this->_vimeography_add_activation_key( $key, $license_data );
130 return TRUE;
131 } else {
132 // Add failed message
133 switch ($license_data->error) {
134 case 'missing': case 'revoked':
135 throw new Exception( wp_kses_post(__('That license key could not be found in our system.', 'vimeography') ));
136 case 'no_activations_left':
137 throw new Exception( wp_kses_post(__('You have reached the max number of sites that this license can be used on.', 'vimeography') ));
138 case 'expired':
139 throw new Exception( wp_kses_post(__('The license key you entered has expired. Please visit http://vimeography.com to renew it.', 'vimeography') ));
140 case 'key_mismatch':
141 throw new Exception( wp_kses_post(__('The license key you entered does not match the one we have on file.', 'vimeography') ));
142 case 'license_not_activable':
143 throw new Exception( wp_kses_post(__('Looks like you are trying to activate your bundle license. Please activate each of the products in your bundle separately by using their respective individual licenses.', 'vimeography') ));
144 default:
145 throw new Exception( wp_kses_post(__('Unknown error: ' . $license_data->error, 'vimeography') ));
146 }
147 }
148 }
149
150 /**
151 * Ask the Vimeography EDD endpoint which product is associated with a given
152 * license key.
153 *
154 * Implementation note: the `check_license` endpoint does not return the
155 * product name on Vimeography's server (item_name is empty). However, calling
156 * `activate_license` *without* the `url` parameter triggers a `missing_url`
157 * error response that includes both `item_name` and `vimeography_product_name`
158 * in its payload — and does NOT consume an activation, since the request
159 * aborts on the server before the site count is incremented.
160 *
161 * @access protected
162 * @param string $key Normalized license key.
163 * @return string Product name, or empty string if it could not be resolved.
164 */
165 protected function _vimeography_lookup_item_name( $key ) {
166 $response = wp_remote_get(
167 add_query_arg( array(
168 'edd_action' => 'activate_license',
169 'license' => $key,
170 ), $this->_endpoint ),
171 array(
172 'timeout' => 15,
173 'sslverify' => false,
174 )
175 );
176
177 if ( is_wp_error( $response ) ) {
178 return '';
179 }
180
181 $data = json_decode( wp_remote_retrieve_body( $response ) );
182
183 if ( ! is_object( $data ) ) {
184 return '';
185 }
186
187 if ( ! empty( $data->item_name ) ) {
188 return $data->item_name;
189 }
190
191 if ( ! empty( $data->vimeography_product_name ) ) {
192 return $data->vimeography_product_name;
193 }
194
195 return '';
196 }
197
198 /**
199 * Deactivate the license key
200 *
201 * @access public
202 * @return void
203 */
204 public function deactivate_license( $license ) {
205 $key = str_replace('-', '', strtoupper( sanitize_text_field( $license ) ) );
206
207 // Data to send to the API
208 $api_params = array(
209 'edd_action' => 'deactivate_license',
210 'license' => $key,
211 //'item_name' => urlencode( $this->item_name ), // the name of our product in EDD
212 'url' => urlencode( home_url() )
213 );
214
215 // Call the API
216 $response = wp_remote_get(
217 add_query_arg( $api_params, $this->_endpoint ),
218 array(
219 'timeout' => 15,
220 'sslverify' => false
221 )
222 );
223
224 // Make sure there are no errors
225 if ( is_wp_error( $response ) ) {
226 return;
227 }
228
229 // Decode the license data
230 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
231
232 // Remove the key even if deactivation fails
233 $this->_vimeography_remove_activation_key( $key );
234
235 if ( ! $license_data->success ) {
236 throw new Exception( wp_kses_post(__('That license key has been removed from your site, but could not be deactivated in our system.', 'vimeography')) );
237 }
238 }
239
240 /**
241 * [check_license description]
242 * @return [type] [description]
243 */
244 public function check_license( $license ) {
245
246 // Data to send to the API
247 $api_params = array(
248 'edd_action' => 'check_license',
249 'license' => $license->activation_key,
250 //'item_name' => urlencode( $this->item_name ), // the name of our product in EDD
251 'url' => urlencode( home_url() )
252 );
253
254 // Call the API
255 $response = wp_remote_get(
256 add_query_arg( $api_params, $this->_endpoint ),
257 array(
258 'timeout' => 15,
259 'sslverify' => false
260 )
261 );
262
263 // Make sure there are no errors
264 if ( is_wp_error( $response ) ) {
265 return FALSE;
266 }
267
268 // Decode the license data
269 return json_decode( wp_remote_retrieve_body( $response ) );
270 }
271
272 /**
273 * Add a plugins page message to any Vimeography add-ons that are installed,
274 * but that do not have an activation key associated with the installation.
275 *
276 * @return void
277 */
278 public function vimeography_check_for_missing_activation_keys() {
279
280 $addons = Vimeography::get_instance()->addons->installed_addons;
281
282 if ( ! empty( $addons ) ) {
283 // If the activation key is not found for the installed plugin,
284 // add the plugin message hook
285 $addons_with_missing_keys = array_filter($addons, array($this, 'vimeography_is_addon_missing_activation_key') );
286
287 if ( ! empty( $addons_with_missing_keys ) ) {
288 foreach ( $addons_with_missing_keys as $plugin ) {
289 $hook = 'after_plugin_row_' . $plugin['basename'];
290 add_action( $hook, array($this, 'vimeography_addon_update_message'), 10, 2 );
291 }
292 }
293 }
294 }
295
296 /**
297 * Loop through the activations keys to check if one exists for the
298 * given Vimeography addon plugin headers.
299 *
300 * @var $plugin Meta headers for a Vimeography addon plugin.
301 * @return bool TRUE if missing, FALSE if found
302 */
303 public function vimeography_is_addon_missing_activation_key($plugin) {
304 $plugins_with_keys = array();
305
306 if ( ! empty($this->_activation_keys) ) {
307 foreach ($this->_activation_keys as $key) {
308 $plugins_with_keys[] = $key->plugin_name;
309 }
310 }
311
312 return !in_array( $plugin['slug'], $plugins_with_keys );
313 }
314
315 /**
316 * Loop through the activations keys to check if one exists for the
317 * given Vimeography activation key.
318 *
319 * @var $key string Activation key.
320 * @return bool TRUE if exists, FALSE if not found
321 */
322 public function vimeography_check_if_activation_key_exists($key) {
323 $result = FALSE;
324
325 if ( ! empty($this->_activation_keys) ) {
326 foreach ( $this->_activation_keys as $entry ) {
327 if ( $entry->activation_key == $key ) {
328 $result = TRUE;
329 }
330 }
331 }
332
333 return $result;
334 }
335
336 /**
337 * Add the activation key to the database.
338 *
339 * @var $key string Activation key.
340 * @var $license_data array
341 * @return bool TRUE if successful, FALSE if failed
342 */
343 protected function _vimeography_add_activation_key( $key, $license_data ) {
344 $entry = new stdClass();
345 $entry->activation_key = $key;
346 $entry->plugin_name = $license_data->vimeography_plugin_slug;
347 $entry->product_name = $license_data->vimeography_product_name;
348 $entry->expires = $license_data->expires;
349 $entry->status = $license_data->license;
350 $entry->limit = $license_data->license_limit;
351 $entry->activations_left = $license_data->activations_left;
352
353 $this->_activation_keys[] = $entry;
354 return update_site_option('vimeography_activation_keys', array_values( $this->_activation_keys ) );
355 }
356
357 /**
358 * Remove the activation key to the database.
359 *
360 * @var $key string Activation key.
361 * @return bool TRUE if successful, FALSE if failed
362 */
363 protected function _vimeography_remove_activation_key( $key ) {
364 if ( ! empty( $this->_activation_keys ) ) {
365 foreach ( $this->_activation_keys as $i => $entry ) {
366 if ( $entry->activation_key === $key ) {
367 unset( $this->_activation_keys[$i] );
368 }
369 }
370
371 return update_site_option( 'vimeography_activation_keys', array_values( $this->_activation_keys ) );
372 }
373
374 return FALSE;
375 }
376
377 /**
378 * Add a reminder to add the activation key to receives updates
379 * for the installed Vimeography theme.
380 *
381 * @param string $plugin_basename Folder and filename, eg:
382 * @return [type] [description]
383 */
384 public function vimeography_addon_update_message($plugin_basename, $plugin_data) {
385 $ineligible = array(
386 'Vimeography Theme: Bugsauce',
387 'Vimeography Theme: Ballistic',
388 'Vimeography Theme: Single'
389 );
390
391 if ( in_array( $plugin_data['Name'], $ineligible ) ) {
392 return;
393 }
394
395 echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message notice inline notice-warning notice-alt">';
396 echo '<span style="display: block; padding: 5px;">';
397 printf(
398 wp_kses_post(
399 __(
400 'Hey! Don\'t forget to <a title="Activate my Vimeography Addon" href="%1$sadmin.php?page=vimeography-manage-activations">enter your activation key</a> to receive the latest updates for the %2$s plugin.',
401 'vimeography'
402 )
403 ),
404 esc_url(get_admin_url()), // Premier argument %1$s
405 esc_html($plugin_data['Name']) // Deuxième argument %2$s
406 );
407 echo '</span>';
408 echo '</div></td></tr>';
409 }
410
411 /**
412 * Auto updater
413 *
414 * @access protected
415 * @return void
416 */
417 protected function _vimeography_auto_updater() {
418 // We only need to check for updates if an activation key exists
419 // in the options table.
420 if ( ! empty( $this->_activation_keys ) ) {
421 foreach ( $this->_activation_keys as $plugin ) {
422
423 $plugin_path = self::_vimeography_get_plugin_path( $plugin->plugin_name );
424
425 if ( $plugin_path ) {
426 // Get the plugin headers
427 $headers = get_file_data( $plugin_path, array('version' => 'Version') );
428
429 // setup the updater
430 new EDD_SL_Plugin_Updater(
431 $this->_endpoint,
432 $plugin_path,
433 array(
434 'version' => $headers['version'],
435 'license' => trim($plugin->activation_key),
436 'item_name' => $plugin->product_name,
437 'author' => 'Dave Kiss',
438 )
439 );
440 }
441 }
442 }
443 }
444
445 /**
446 * Get the absolute path to the provided plugin name.
447 *
448 * @access protected
449 * @return string
450 */
451 protected function _vimeography_get_plugin_path( $plugin_name ) {
452 //return str_replace('vimeography/', trailingslashit($plugin_name), VIMEOGRAPHY_PATH);
453 $basename = '/' . trailingslashit( $plugin_name ) . $plugin_name . '.php';
454
455 if ( ! is_file( $dir = WPMU_PLUGIN_DIR . $basename ) ) {
456 if ( ! is_file( $dir = WP_PLUGIN_DIR . $basename ) ) {
457 return FALSE;
458 }
459 }
460
461 return $dir;
462 }
463
464 /**
465 * Sets the activation keys in the class
466 *
467 * @param array $keys [description]
468 * @return [type] [description]
469 */
470 public function vimeography_set_activation_keys($keys = array()) {
471 $this->_activation_keys = $keys;
472 return $this;
473 }
474
475 }
476