1: <?php
2: /**
3: * EPL Updater
4: *
5: * @package EPL
6: * @subpackage Classes/Updater
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 1.0
10: * @version 1.6.6
11: */
12:
13: // uncomment this line for testing.
14: // set_site_transient( 'update_plugins', null ); | Testing.
15: // phpcs:disable WordPress.Security.NonceVerification
16:
17: // Exit if accessed directly.
18: if ( ! defined( 'ABSPATH' ) ) {
19: exit;
20: }
21:
22: /**
23: * Allows plugins to use their own update API.
24: *
25: * @author Pippin Williamson
26: * @version 1.6.6
27: * @since 1.0
28: */
29: class EPL_SL_Plugin_Updater {
30: /**
31: * File
32: *
33: * @var string $api_url.
34: */
35: private $api_url = '';
36:
37: /**
38: * API data
39: *
40: * @var string $api_data.
41: */
42: private $api_data = array();
43:
44: /**
45: * Name
46: *
47: * @var string name.
48: */
49: private $name = '';
50:
51: /**
52: * Slug
53: *
54: * @var string slug.
55: */
56: private $slug = '';
57:
58: /**
59: * Version
60: *
61: * @var string $version.
62: */
63: private $version = '';
64:
65:
66: /**
67: * WP override
68: *
69: * @var bool $wp_override.
70: */
71: private $wp_override = false;
72:
73: /**
74: * Cache key
75: *
76: * @var string cache_key.
77: */
78: private $cache_key = '';
79:
80: /**
81: * Beta
82: *
83: * @var bool cache_key.
84: */
85: private $beta = false;
86:
87: /**
88: * Class constructor.
89: *
90: * @uses plugin_basename()
91: * @uses hook()
92: *
93: * @param string $_api_url The URL pointing to the custom API endpoint.
94: * @param string $_plugin_file Path to the plugin file.
95: * @param array $_api_data Optional data to send with API calls.
96: */
97: public function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
98:
99: global $epl_plugin_data;
100:
101: $this->api_url = trailingslashit( $_api_url );
102: $this->api_data = $_api_data;
103: $this->name = plugin_basename( $_plugin_file );
104: $this->slug = basename( $_plugin_file, '.php' );
105: $this->version = $_api_data['version'];
106: $this->wp_override = isset( $_api_data['wp_override'] ) ? (bool) $_api_data['wp_override'] : false;
107: $this->beta = ! empty( $this->api_data['beta'] ) ? true : false;
108: $this->cache_key = md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ); //phpcs:ignore
109:
110: $epl_plugin_data[ $this->slug ] = $this->api_data;
111:
112: // Set up hooks.
113: $this->init();
114: }
115:
116: /**
117: * Set up WordPress filters to hook into WP's update process.
118: *
119: * @uses add_filter()
120: *
121: * @return void
122: */
123: public function init() {
124:
125: add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
126: add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
127: remove_action( 'after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10 );
128: add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 10, 2 );
129: add_action( 'admin_init', array( $this, 'show_changelog' ) );
130: }
131:
132: /**
133: * Check for Updates at the defined API endpoint and modify the update array.
134: *
135: * This function dives into the update API just when WordPress creates its update array,
136: * then adds a custom API call and injects the custom plugin data retrieved from the API.
137: * It is reassembled from parts of the native WordPress plugin update code.
138: * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
139: *
140: * @uses api_request()
141: *
142: * @param array $_transient_data Update array build by WordPress.
143: * @return array Modified update array with custom plugin data.
144: */
145: public function check_update( $_transient_data ) {
146:
147: global $pagenow;
148:
149: if ( ! is_object( $_transient_data ) ) {
150: $_transient_data = new stdClass();
151: }
152:
153: if ( 'plugins.php' === $pagenow && is_multisite() ) {
154: return $_transient_data;
155: }
156:
157: if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) {
158: return $_transient_data;
159: }
160:
161: $version_info = $this->get_cached_version_info();
162:
163: if ( false === $version_info ) {
164: $version_info = $this->api_request(
165: 'plugin_latest_version',
166: array(
167: 'slug' => $this->slug,
168: 'beta' => $this->beta,
169: )
170: );
171:
172: $this->set_version_info_cache( $version_info );
173: }
174:
175: if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
176:
177: if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
178:
179: $_transient_data->response[ $this->name ] = $version_info;
180:
181: }
182: $_transient_data->last_checked = time();
183: $_transient_data->checked[ $this->name ] = $this->version;
184:
185: }
186:
187: return $_transient_data;
188: }
189:
190: /**
191: * Show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
192: *
193: * @param string $file PLugin file.
194: * @param array $plugin Plugin name.
195: */
196: public function show_update_notification( $file, $plugin ) {
197:
198: if ( is_network_admin() ) {
199: return;
200: }
201:
202: if ( ! current_user_can( 'update_plugins' ) ) {
203: return;
204: }
205:
206: if ( ! is_multisite() ) {
207: return;
208: }
209:
210: if ( $this->name !== $file ) {
211: return;
212: }
213:
214: // Remove our filter on the site transient.
215: remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
216:
217: $update_cache = get_site_transient( 'update_plugins' );
218:
219: $update_cache = is_object( $update_cache ) ? $update_cache : new stdClass();
220:
221: if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
222:
223: $version_info = $this->get_cached_version_info();
224:
225: if ( false === $version_info ) {
226:
227: $version_info = $this->api_request(
228: 'plugin_latest_version',
229: array(
230: 'slug' => $this->slug,
231: 'beta' => $this->beta,
232: )
233: );
234:
235: $this->set_version_info_cache( $version_info );
236: }
237:
238: if ( ! is_object( $version_info ) ) {
239: return;
240: }
241:
242: if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
243:
244: $update_cache->response[ $this->name ] = $version_info;
245: }
246:
247: $update_cache->last_checked = time();
248: $update_cache->checked[ $this->name ] = $this->version;
249:
250: set_site_transient( 'update_plugins', $update_cache );
251:
252: } else {
253:
254: $version_info = $update_cache->response[ $this->name ];
255:
256: }
257:
258: // Restore our filter.
259: add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
260:
261: if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) {
262:
263: // build a plugin list row, with update notification.
264: $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
265: echo '<tr class="plugin-update-tr" id="' . esc_attr( $this->slug ) . '-update" data-slug="' . esc_attr( $this->slug ) . '" data-plugin="' . esc_attr( $this->slug ) . '/' . esc_url( $file ) . '">';
266: echo '<td colspan="3" class="plugin-update colspanchange">';
267: echo '<div class="update-message notice inline notice-warning notice-alt">';
268:
269: $changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' );
270:
271: if ( empty( $version_info->download_link ) ) {
272:
273: printf( // translators: plugin name, anchor change log, new version details.
274: wp_kses_post( __( 'There is a new version of %1$s available. %2$s View version %3$s details %4$s.', 'easy-property-listings' ) ),
275: esc_html( $version_info->name ),
276: '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
277: esc_html( $version_info->new_version ),
278: '</a>'
279: );
280: } else {
281:
282: printf( // translators: plugin name, anchor change log, new version details, download / update links.
283: wp_kses_post( __( 'There is a new version of %1$s available. %2$s View version %3$s details %4$s or %5$s update now %6$s.', 'easy-property-listings' ) ),
284: esc_html( $version_info->name ),
285: '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
286: esc_html( $version_info->new_version ),
287: '</a>',
288: '<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) . '">',
289: '</a>'
290: );
291: }
292:
293: do_action( "in_plugin_update_message-{$file}", $plugin, $version_info ); //phpcs:ignore
294:
295: echo '</div></td></tr>';
296: }
297: }
298:
299:
300: /**
301: * Updates information on the "View version x.x details" page with custom data.
302: *
303: * @uses api_request()
304: *
305: * @param mixed $_data Data.
306: * @param string $_action Action name.
307: * @param object $_args Arguments.
308: * @return object $_data
309: */
310: public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
311:
312: if ( 'plugin_information' !== $_action ) {
313:
314: return $_data;
315:
316: }
317:
318: if ( ! isset( $_args->slug ) || ( $_args->slug !== $this->slug ) ) {
319:
320: return $_data;
321:
322: }
323:
324: $to_send = array(
325: 'slug' => $this->slug,
326: 'is_ssl' => is_ssl(),
327: 'fields' => array(
328: 'banners' => array(),
329: 'reviews' => false,
330: ),
331: );
332:
333: $cache_key = 'epl_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ); //phpcs:ignore
334:
335: // Get the transient where we store the api request for this plugin for 24 hours.
336: $epl_api_request_transient = $this->get_cached_version_info( $cache_key );
337:
338: // If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now.
339: if ( empty( $epl_api_request_transient ) ) {
340:
341: $api_response = $this->api_request( 'plugin_information', $to_send );
342: // Expires in 3 hours.
343: $this->set_version_info_cache( $api_response, $cache_key );
344: if ( false !== $api_response ) {
345: $_data = $api_response;
346: }
347: } else {
348:
349: $_data = $epl_api_request_transient;
350: }
351:
352: // Convert sections into an associative array, since we're getting an object, but Core expects an array.
353: if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
354:
355: $new_sections = array();
356: foreach ( $_data->sections as $key => $value ) {
357: $new_sections[ $key ] = $value;
358: }
359: $_data->sections = $new_sections;
360: }
361:
362: // Convert banners into an associative array, since we're getting an object, but Core expects an array.
363: if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
364:
365: $new_banners = array();
366: foreach ( $_data->banners as $key => $value ) {
367: $new_banners[ $key ] = $value;
368: }
369: $_data->banners = $new_banners;
370: }
371:
372: return $_data;
373: }
374:
375:
376: /**
377: * Disable SSL verification in order to prevent download update failures.
378: *
379: * @param array $args Arguments.
380: * @param string $url URL.
381: *
382: * @return array $array
383: */
384: public function http_request_args( $args, $url ) {
385: // If it is an https request and we are performing a package download, disable ssl verification.
386: if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
387: $args['sslverify'] = false;
388: }
389: return $args;
390: }
391:
392: /**
393: * Calls the API and, if successfull, returns the object delivered by the API.
394: *
395: * @uses get_bloginfo()
396: * @uses wp_remote_post()
397: * @uses is_wp_error()
398: *
399: * @param string $_action The requested action.
400: * @param array $_data Parameters for the API action.
401: * @return false|object
402: */
403: private function api_request( $_action, $_data ) {
404:
405: global $wp_version;
406:
407: $data = array_merge( $this->api_data, $_data );
408:
409: if ( $data['slug'] !== $this->slug ) {
410: return;
411: }
412:
413: if ( trailingslashit( home_url() ) === $this->api_url ) {
414: return false; // Don't allow a plugin to ping itself.
415: }
416:
417: $api_params = array(
418: 'edd_action' => 'get_version',
419: 'license' => ! empty( $data['license'] ) ? $data['license'] : '',
420: 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
421: 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
422: 'version' => isset( $data['version'] ) ? $data['version'] : false,
423: 'slug' => $data['slug'],
424: 'author' => $data['author'],
425: 'url' => home_url(),
426: 'beta' => ! empty( $data['beta'] ),
427: );
428:
429: $verify_ssl = $this->verify_ssl();
430:
431: $request = wp_remote_post(
432: $this->api_url,
433: array(
434: 'timeout' => 15,
435: 'sslverify' => $verify_ssl,
436: 'body' => $api_params,
437: )
438: );
439:
440: if ( ! is_wp_error( $request ) ) {
441: $request = json_decode( wp_remote_retrieve_body( $request ) );
442: }
443:
444: if ( $request && isset( $request->sections ) ) {
445: $request->sections = maybe_unserialize( $request->sections );
446: } else {
447: $request = false;
448: }
449:
450: if ( $request && isset( $request->banners ) ) {
451:
452: $request->banners = maybe_unserialize( $request->banners );
453: }
454:
455: if ( ! empty( $request->sections ) ) {
456: foreach ( $request->sections as $key => $section ) {
457: $request->$key = (array) $section;
458: }
459: }
460:
461: return $request;
462: }
463:
464: /**
465: * Display change log
466: */
467: public function show_changelog() {
468:
469: global $epl_plugin_data;
470:
471: if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' !== $_REQUEST['edd_sl_action'] ) {
472: return;
473: }
474:
475: if ( empty( $_REQUEST['plugin'] ) ) {
476: return;
477: }
478:
479: if ( empty( $_REQUEST['slug'] ) ) {
480: return;
481: }
482:
483: if ( ! current_user_can( 'update_plugins' ) ) {
484: wp_die( esc_html__( 'You do not have permission to install plugin updates', 'easy-property-listings' ), esc_html__( 'Error', 'easy-property-listings' ), array( 'response' => 403 ) );
485: }
486:
487: $data = $epl_plugin_data[ sanitize_text_field( wp_unslash( $_REQUEST['slug'] ) ) ];
488: $beta = ! empty( $data['beta'] ) ? true : false;
489: $cache_key = md5( 'epl_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_' . $beta . '_version_info' );
490: $version_info = $this->get_cached_version_info( $cache_key );
491:
492: if ( false === $version_info ) {
493:
494: $api_params = array(
495: 'edd_action' => 'get_version',
496: 'item_name' => isset( $data['item_name'] ) ? sanitize_text_field( $data['item_name'] ) : false,
497: 'item_id' => isset( $data['item_id'] ) ? sanitize_text_field( $data['item_id'] ) : false,
498: 'slug' => sanitize_text_field( wp_unslash( $_REQUEST['slug'] ) ),
499: 'author' => sanitize_text_field( $data['author'] ),
500: 'url' => home_url(),
501: 'beta' => ! empty( $data['beta'] ),
502: );
503:
504: $verify_ssl = $this->verify_ssl();
505:
506: $request = wp_remote_post(
507: $this->api_url,
508: array(
509: 'timeout' => 15,
510: 'sslverify' => $verify_ssl,
511: 'body' => $api_params,
512: )
513: );
514:
515: if ( ! is_wp_error( $request ) ) {
516: $version_info = json_decode( wp_remote_retrieve_body( $request ) );
517: }
518:
519: if ( ! empty( $version_info ) && isset( $version_info->sections ) ) {
520: $version_info->sections = maybe_unserialize( $version_info->sections );
521: } else {
522: $version_info = false;
523: }
524:
525: if ( ! empty( $version_info ) ) {
526: foreach ( $version_info->sections as $key => $section ) {
527: $version_info->$key = (array) $section;
528: }
529: }
530:
531: $this->set_version_info_cache( $version_info, $cache_key );
532:
533: }
534:
535: if ( ! empty( $version_info ) && isset( $version_info->sections['changelog'] ) ) {
536: echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $version_info->sections['changelog'] ) . '</div>';
537: }
538:
539: exit;
540: }
541:
542: /**
543: * Get cached version info
544: *
545: * @param string $cache_key The cache key.
546: *
547: * @return bool
548: */
549: public function get_cached_version_info( $cache_key = '' ) {
550:
551: if ( empty( $cache_key ) ) {
552: $cache_key = $this->cache_key;
553: }
554: $cache = get_option( $cache_key );
555: if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
556: return false; // Cache is expired.
557: }
558: return json_decode( $cache['value'] );
559: }
560:
561: /**
562: * Set version info cache
563: *
564: * @param string $value The value.
565: * @param string $cache_key The cache key.
566: */
567: public function set_version_info_cache( $value = '', $cache_key = '' ) {
568:
569: if ( empty( $cache_key ) ) {
570: $cache_key = $this->cache_key;
571: }
572: $data = array(
573: 'timeout' => strtotime( '+3 hours', time() ),
574: 'value' => wp_json_encode( $value ),
575: );
576: update_option( $cache_key, $data, 'no' );
577: }
578:
579: /**
580: * Returns if the SSL of the store should be verified.
581: *
582: * @since 1.6.13
583: * @return bool
584: */
585: private function verify_ssl() {
586: return (bool) apply_filters( 'epl_sl_api_request_verify_ssl', true, $this );
587: }
588: }
589: