PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 3.1.4
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v3.1.4
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / freemius / includes / class-fs-plugin-updater.php

class-fs-plugin-updater.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites 3.1.4, at freemius/includes/class-fs-plugin-updater.php

1,590 lines 63.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Freemius
4 * @copyright Copyright (c) 2015, Freemius, Inc.
5 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6 * @since 1.0.4
7 *
8 * @link https://github.com/easydigitaldownloads/EDD-License-handler/blob/master/EDD_SL_Plugin_Updater.php
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 class FS_Plugin_Updater {
16
17 /**
18 * @var Freemius
19 * @since 1.0.4
20 */
21 private $_fs;
22 /**
23 * @var FS_Logger
24 * @since 1.0.4
25 */
26 private $_logger;
27 /**
28 * @var object
29 * @since 1.1.8.1
30 */
31 private $_update_details;
32 /**
33 * @var array
34 * @since 2.1.2
35 */
36 private $_translation_updates;
37
38 private static $_upgrade_basename = null;
39
40 #--------------------------------------------------------------------------------
41 #region Singleton
42 #--------------------------------------------------------------------------------
43
44 /**
45 * @var FS_Plugin_Updater[]
46 * @since 2.0.0
47 */
48 private static $_INSTANCES = array();
49
50 /**
51 * @param Freemius $freemius
52 *
53 * @return FS_Plugin_Updater
54 */
55 static function instance( Freemius $freemius ) {
56 $key = $freemius->get_id();
57
58 if ( ! isset( self::$_INSTANCES[ $key ] ) ) {
59 self::$_INSTANCES[ $key ] = new self( $freemius );
60 }
61
62 return self::$_INSTANCES[ $key ];
63 }
64
65 #endregion
66
67 private function __construct( Freemius $freemius ) {
68 $this->_fs = $freemius;
69
70 $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $freemius->get_slug() . '_updater', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
71
72 $this->filters();
73 }
74
75 /**
76 * Initiate required filters.
77 *
78 * @author Vova Feldman (@svovaf)
79 * @since 1.0.4
80 */
81 private function filters() {
82 // Override request for plugin information
83 add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 );
84
85 $this->add_transient_filters();
86
87 /**
88 * If user has the premium plugin's code but do NOT have an active license,
89 * encourage him to upgrade by showing that there's a new release, but instead
90 * of showing an update link, show upgrade link to the pricing page.
91 *
92 * @since 1.1.6
93 *
94 */
95 // WP 2.9+
96 add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array(
97 &$this,
98 'catch_plugin_update_row'
99 ), 9 );
100 add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array(
101 &$this,
102 'edit_and_echo_plugin_update_row'
103 ), 11, 2 );
104
105 if ( ! $this->_fs->has_any_active_valid_license() ) {
106 add_action( 'admin_head', array( &$this, 'catch_plugin_information_dialog_contents' ) );
107 }
108
109 if ( ! WP_FS__IS_PRODUCTION_MODE ) {
110 add_filter( 'http_request_host_is_external', array(
111 $this,
112 'http_request_host_is_external_filter'
113 ), 10, 3 );
114 }
115
116 if ( $this->_fs->is_premium() ) {
117 if ( ! $this->is_correct_folder_name() ) {
118 add_filter( 'upgrader_post_install', array( &$this, '_maybe_update_folder_name' ), 10, 3 );
119 }
120
121 add_filter( 'upgrader_pre_install', array( 'FS_Plugin_Updater', '_store_basename_for_source_adjustment' ), 1, 2 );
122 add_filter( 'upgrader_source_selection', array( 'FS_Plugin_Updater', '_maybe_adjust_source_dir' ), 1, 3 );
123
124 if ( ! $this->_fs->has_any_active_valid_license() ) {
125 add_filter( 'wp_prepare_themes_for_js', array( &$this, 'change_theme_update_info_html' ), 10, 1 );
126 }
127 }
128 }
129
130 /**
131 * @author Leo Fajardo (@leorw)
132 * @since 2.1.4
133 */
134 function catch_plugin_information_dialog_contents() {
135 if (
136 'plugin-information' !== fs_request_get( 'tab', false ) ||
137 $this->_fs->get_slug() !== fs_request_get_raw( 'plugin', false )
138 ) {
139 return;
140 }
141
142 add_action( 'admin_footer', array( &$this, 'edit_and_echo_plugin_information_dialog_contents' ), 0, 1 );
143
144 ob_start();
145 }
146
147 /**
148 * @author Leo Fajardo (@leorw)
149 * @since 2.1.4
150 *
151 * @param string $hook_suffix
152 */
153 function edit_and_echo_plugin_information_dialog_contents( $hook_suffix ) {
154 if (
155 'plugin-information' !== fs_request_get( 'tab', false ) ||
156 $this->_fs->get_slug() !== fs_request_get_raw( 'plugin', false )
157 ) {
158 return;
159 }
160
161 $license = $this->_fs->_get_license();
162
163 $subscription = ( is_object( $license ) && ! $license->is_lifetime() ) ?
164 $this->_fs->_get_subscription( $license->id ) :
165 null;
166
167 $contents = ob_get_clean();
168
169 $install_or_update_button_id_attribute_pos = strpos( $contents, 'id="plugin_install_from_iframe"' );
170
171 if ( false === $install_or_update_button_id_attribute_pos ) {
172 $install_or_update_button_id_attribute_pos = strpos( $contents, 'id="plugin_update_from_iframe"' );
173 }
174
175 if ( false !== $install_or_update_button_id_attribute_pos ) {
176 $install_or_update_button_start_pos = strrpos(
177 substr( $contents, 0, $install_or_update_button_id_attribute_pos ),
178 '<a'
179 );
180
181 $install_or_update_button_end_pos = ( strpos( $contents, '</a>', $install_or_update_button_id_attribute_pos ) + strlen( '</a>' ) );
182
183 /**
184 * The part of the contents without the update button.
185 *
186 * @author Leo Fajardo (@leorw)
187 * @since 2.2.5
188 */
189 $modified_contents = substr( $contents, 0, $install_or_update_button_start_pos );
190
191 $install_or_update_button = substr( $contents, $install_or_update_button_start_pos, ( $install_or_update_button_end_pos - $install_or_update_button_start_pos ) );
192
193 /**
194 * Replace the plugin information dialog's "Install Update Now" button's text and URL. If there's a license,
195 * the text will be "Renew license" and will link to the checkout page with the license's billing cycle
196 * and quota. If there's no license, the text will be "Buy license" and will link to the pricing page.
197 */
198 $install_or_update_button = preg_replace(
199 '/(\<a.+)(id="plugin_(install|update)_from_iframe")(.+href=")([^\s]+)(".*\>)(.+)(\<\/a>)/is',
200 is_object( $license ) ?
201 sprintf(
202 '$1$4%s$6%s$8',
203 $this->_fs->checkout_url(
204 is_object( $subscription ) ?
205 ( 1 == $subscription->billing_cycle ? WP_FS__PERIOD_MONTHLY : WP_FS__PERIOD_ANNUALLY ) :
206 WP_FS__PERIOD_LIFETIME,
207 false,
208 array( 'licenses' => $license->quota )
209 ),
210 fs_text_inline( 'Renew license', 'renew-license', $this->_fs->get_slug() )
211 ) :
212 sprintf(
213 '$1$4%s$6%s$8',
214 $this->_fs->pricing_url(),
215 fs_text_inline( 'Buy license', 'buy-license', $this->_fs->get_slug() )
216 ),
217 $install_or_update_button
218 );
219
220 /**
221 * Append the modified button.
222 *
223 * @author Leo Fajardo (@leorw)
224 * @since 2.2.5
225 */
226 $modified_contents .= $install_or_update_button;
227
228 /**
229 * Append the remaining part of the contents after the update button.
230 *
231 * @author Leo Fajardo (@leorw)
232 * @since 2.2.5
233 */
234 $modified_contents .= substr( $contents, $install_or_update_button_end_pos );
235
236 $contents = $modified_contents;
237 }
238
239 echo $contents;
240 }
241
242 /**
243 * @author Vova Feldman (@svovaf)
244 * @since 2.0.0
245 */
246 private function add_transient_filters() {
247 if (
248 $this->_fs->is_premium() &&
249 $this->_fs->is_registered() &&
250 ! FS_Permission_Manager::instance( $this->_fs )->is_essentials_tracking_allowed()
251 ) {
252 $this->_logger->log( 'Opted out sites cannot receive automatic software updates.' );
253
254 return;
255 }
256
257 add_filter( 'pre_set_site_transient_update_plugins', array(
258 &$this,
259 'pre_set_site_transient_update_plugins_filter'
260 ) );
261
262 add_filter( 'pre_set_site_transient_update_themes', array(
263 &$this,
264 'pre_set_site_transient_update_plugins_filter'
265 ) );
266 }
267
268 /**
269 * @author Vova Feldman (@svovaf)
270 * @since 2.0.0
271 */
272 private function remove_transient_filters() {
273 remove_filter( 'pre_set_site_transient_update_plugins', array(
274 &$this,
275 'pre_set_site_transient_update_plugins_filter'
276 ) );
277
278 remove_filter( 'pre_set_site_transient_update_themes', array(
279 &$this,
280 'pre_set_site_transient_update_plugins_filter'
281 ) );
282 }
283
284 /**
285 * Capture plugin update row by turning output buffering.
286 *
287 * @author Vova Feldman (@svovaf)
288 * @since 1.1.6
289 */
290 function catch_plugin_update_row() {
291 ob_start();
292 }
293
294 /**
295 * Overrides default update message format with "renew your license" message.
296 *
297 * @author Vova Feldman (@svovaf)
298 * @since 1.1.6
299 *
300 * @param string $file
301 * @param array $plugin_data
302 */
303 function edit_and_echo_plugin_update_row( $file, $plugin_data ) {
304 $plugin_update_row = ob_get_clean();
305
306 $current = get_site_transient( 'update_plugins' );
307 if ( ! isset( $current->response[ $file ] ) ) {
308 echo $plugin_update_row;
309
310 return;
311 }
312
313 $r = $current->response[ $file ];
314
315 $has_beta_update = $this->_fs->has_beta_update();
316
317 if ( $this->_fs->has_any_active_valid_license() ) {
318 if ( $has_beta_update ) {
319 /**
320 * Turn the "new version" text into "new Beta version".
321 *
322 * Sample input:
323 * There is a new version of Awesome Plugin available. <a href="...>View version x.y.z details</a> or <a href="...>update now</a>.
324 * Output:
325 * There is a new Beta version of Awesome Plugin available. <a href="...>View version x.y.z details</a> or <a href="...>update now</a>.
326 *
327 * @author Leo Fajardo (@leorw)
328 * @since 2.3.0
329 */
330 $plugin_update_row = preg_replace(
331 '/(\<div.+>)(.+)(\<a.+href="([^\s]+)"([^\<]+)\>.+\<a.+)(\<\/div\>)/is',
332 (
333 '$1' .
334 sprintf(
335 fs_text_inline( 'There is a %s of %s available.', 'new-version-available', $this->_fs->get_slug() ),
336 $has_beta_update ?
337 fs_text_inline( 'new Beta version', 'new-beta-version', $this->_fs->get_slug() ) :
338 fs_text_inline( 'new version', 'new-version', $this->_fs->get_slug() ),
339 $this->_fs->get_plugin_title()
340 ) .
341 ' ' .
342 '$3' .
343 '$6'
344 ),
345 $plugin_update_row
346 );
347 }
348 } else {
349 /**
350 * Turn the "new version" text into a link that opens the plugin information dialog when clicked and
351 * make the "View version x details" text link to the checkout page instead of opening the plugin
352 * information dialog when clicked.
353 *
354 * Sample input:
355 * There is a new version of Awesome Plugin available. <a href="...>View version x.y.z details</a> or <a href="...>update now</a>.
356 * Output:
357 * There is a <a href="...>new version</a> of Awesome Plugin available. <a href="...>Buy a license now</a> to access version x.y.z security & feature updates, and support.
358 * OR
359 * There is a <a href="...>new Beta version</a> of Awesome Plugin available. <a href="...>Buy a license now</a> to access version x.y.z security & feature updates, and support.
360 *
361 * @author Leo Fajardo (@leorw)
362 */
363 $plugin_update_row = preg_replace(
364 '/(\<div.+>)(.+)(\<a.+href="([^\s]+)"([^\<]+)\>.+\<a.+)(\<\/div\>)/is',
365 (
366 '$1' .
367 sprintf(
368 fs_text_inline( 'There is a %s of %s available.', 'new-version-available', $this->_fs->get_slug() ),
369 sprintf(
370 '<a href="$4"%s>%s</a>',
371 '$5',
372 $has_beta_update ?
373 fs_text_inline( 'new Beta version', 'new-beta-version', $this->_fs->get_slug() ) :
374 fs_text_inline( 'new version', 'new-version', $this->_fs->get_slug() )
375 ),
376 $this->_fs->get_plugin_title()
377 ) .
378 ' ' .
379 $this->_fs->version_upgrade_checkout_link( $r->new_version ) .
380 '$6'
381 ),
382 $plugin_update_row
383 );
384 }
385
386 if (
387 $this->_fs->is_plugin() &&
388 isset( $r->upgrade_notice ) &&
389 strlen( trim( $r->upgrade_notice ) ) > 0
390 ) {
391 $slug = $this->_fs->get_slug();
392
393 $upgrade_notice_html = sprintf(
394 '<p class="notice fs-upgrade-notice fs-slug-%1$s fs-type-%2$s" data-slug="%1$s" data-type="%2$s"><strong>%3$s</strong> %4$s</p>',
395 $slug,
396 $this->_fs->get_module_type(),
397 fs_text_inline( 'Important Upgrade Notice:', 'upgrade_notice', $slug ),
398 esc_html( $r->upgrade_notice )
399 );
400
401 $plugin_update_row = str_replace( '</div>', '</div>' . $upgrade_notice_html, $plugin_update_row );
402 }
403
404 echo $plugin_update_row;
405 }
406
407 /**
408 * @author Leo Fajardo (@leorw)
409 * @since 2.0.2
410 *
411 * @param array $prepared_themes
412 *
413 * @return array
414 */
415 function change_theme_update_info_html( $prepared_themes ) {
416 $theme_basename = $this->_fs->get_plugin_basename();
417
418 if ( ! isset( $prepared_themes[ $theme_basename ] ) ) {
419 return $prepared_themes;
420 }
421
422 $themes_update = get_site_transient( 'update_themes' );
423 if ( ! isset( $themes_update->response[ $theme_basename ] ) ||
424 empty( $themes_update->response[ $theme_basename ]['package'] )
425 ) {
426 return $prepared_themes;
427 }
428
429 $prepared_themes[ $theme_basename ]['update'] = preg_replace(
430 '/(\<p.+>)(.+)(\<a.+\<a.+)\.(.+\<\/p\>)/is',
431 '$1 $2 ' . $this->_fs->version_upgrade_checkout_link( $themes_update->response[ $theme_basename ]['new_version'] ) .
432 '$4',
433 $prepared_themes[ $theme_basename ]['update']
434 );
435
436 // Set to false to prevent the "Update now" link for the context theme from being shown on the "Themes" page.
437 $prepared_themes[ $theme_basename ]['hasPackage'] = false;
438
439 return $prepared_themes;
440 }
441
442 /**
443 * Since WP version 3.6, a new security feature was added that denies access to repository with a local ip.
444 * During development mode we want to be able updating plugin versions via our localhost repository. This
445 * filter white-list all domains including "api.freemius".
446 *
447 * @link http://www.emanueletessore.com/wordpress-download-failed-valid-url-provided/
448 *
449 * @author Vova Feldman (@svovaf)
450 * @since 1.0.4
451 *
452 * @param bool $allow
453 * @param string $host
454 * @param string $url
455 *
456 * @return bool
457 */
458 function http_request_host_is_external_filter( $allow, $host, $url ) {
459 return ( false !== strpos( $host, 'freemius' ) ) ? true : $allow;
460 }
461
462 /**
463 * Check for Updates at the defined API endpoint and modify the update array.
464 *
465 * This function dives into the update api just when WordPress creates its update array,
466 * then adds a custom API call and injects the custom plugin data retrieved from the API.
467 * It is reassembled from parts of the native WordPress plugin update code.
468 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
469 *
470 * @author Vova Feldman (@svovaf)
471 * @since 1.0.4
472 *
473 * @uses FS_Api
474 *
475 * @param object $transient_data Update array build by WordPress.
476 *
477 * @return object Modified update array with custom plugin data.
478 */
479 function pre_set_site_transient_update_plugins_filter( $transient_data ) {
480 $this->_logger->entrance();
481
482 /**
483 * "plugins" or "themes".
484 *
485 * @author Leo Fajardo (@leorw)
486 * @since 1.2.2
487 */
488 $module_type = $this->_fs->get_module_type() . 's';
489
490 /**
491 * Ensure that we don't mix plugins update info with themes update info.
492 *
493 * @author Leo Fajardo (@leorw)
494 * @since 1.2.2
495 */
496 if ( "pre_set_site_transient_update_{$module_type}" !== current_filter() ) {
497 return $transient_data;
498 }
499
500 if ( empty( $transient_data ) ||
501 defined( 'WP_FS__UNINSTALL_MODE' )
502 ) {
503 return $transient_data;
504 }
505
506 global $wp_current_filter;
507
508 $current_plugin_version = $this->_fs->get_plugin_version();
509
510 if ( ! empty( $wp_current_filter ) && 'upgrader_process_complete' === $wp_current_filter[0] ) {
511 if (
512 is_null( $this->_update_details ) ||
513 ( is_object( $this->_update_details ) && $this->_update_details->new_version !== $current_plugin_version )
514 ) {
515 /**
516 * After an update, clear the stored update details and reparse the plugin's main file in order to get
517 * the updated version's information and prevent the previous update information from showing up on the
518 * updates page.
519 *
520 * @author Leo Fajardo (@leorw)
521 * @since 2.3.1
522 */
523 $this->_update_details = null;
524 $current_plugin_version = $this->_fs->get_plugin_version( true );
525 }
526 }
527
528 if ( ! isset( $this->_update_details ) ) {
529 // Get plugin's newest update.
530 $new_version = $this->_fs->get_update(
531 false,
532 fs_request_get_bool( 'force-check' ),
533 WP_FS__TIME_24_HOURS_IN_SEC / 24,
534 $current_plugin_version
535 );
536
537 $this->_update_details = false;
538
539 if ( is_object( $new_version ) && $this->is_new_version_premium( $new_version ) ) {
540 $this->_logger->log( 'Found newer plugin version ' . $new_version->version );
541
542 /**
543 * Cache plugin details locally since set_site_transient( 'update_plugins' )
544 * called multiple times and the non wp.org plugins are filtered after the
545 * call to .org.
546 *
547 * @since 1.1.8.1
548 */
549 $this->_update_details = $this->get_update_details( $new_version );
550 }
551 }
552
553 // Alias.
554 $basename = $this->_fs->premium_plugin_basename();
555
556 if ( is_object( $this->_update_details ) ) {
557 if ( isset( $transient_data->no_update ) ) {
558 unset( $transient_data->no_update[ $basename ] );
559 }
560
561 if ( ! isset( $transient_data->response ) ) {
562 $transient_data->response = array();
563 }
564
565 // Add plugin to transient data.
566 $transient_data->response[ $basename ] = $this->_fs->is_plugin() ?
567 $this->_update_details :
568 (array) $this->_update_details;
569 } else {
570 if ( isset( $transient_data->response ) ) {
571 /**
572 * Ensure that there's no update data for the plugin to prevent upgrading the premium version to the latest free version.
573 *
574 * @author Leo Fajardo (@leorw)
575 * @since 2.3.0
576 */
577 unset( $transient_data->response[ $basename ] );
578 }
579
580 if ( ! isset( $transient_data->no_update ) ) {
581 $transient_data->no_update = array();
582 }
583
584 /**
585 * Add product to no_update transient data to properly integrate with WP 5.5 auto-updates UI.
586 *
587 * @since 2.4.1
588 * @link https://make.wordpress.org/core/2020/07/30/recommended-usage-of-the-updates-api-to-support-the-auto-updates-ui-for-plugins-and-themes-in-wordpress-5-5/
589 */
590 $transient_data->no_update[ $basename ] = $this->_fs->is_plugin() ?
591 (object) array(
592 'id' => $basename,
593 'slug' => $this->_fs->get_slug(),
594 'plugin' => $basename,
595 'new_version' => $this->_fs->get_plugin_version(),
596 'url' => '',
597 'package' => '',
598 'icons' => array(),
599 'banners' => array(),
600 'banners_rtl' => array(),
601 'tested' => '',
602 'requires_php' => '',
603 'compatibility' => new stdClass(),
604 ) :
605 array(
606 'theme' => $basename,
607 'new_version' => $this->_fs->get_plugin_version(),
608 'url' => '',
609 'package' => '',
610 'requires' => '',
611 'requires_php' => '',
612 );
613 }
614
615 $slug = $this->_fs->get_slug();
616
617 if ( $this->_fs->is_org_repo_compliant() && $this->_fs->is_freemium() ) {
618 if ( ! isset( $this->_translation_updates ) ) {
619 $this->_translation_updates = array();
620
621 $translation_updates = $this->fetch_wp_org_module_translation_updates( $module_type, $slug );
622 if ( ! empty( $translation_updates ) ) {
623 $this->_translation_updates = $translation_updates;
624 }
625 }
626
627 if ( ! empty( $this->_translation_updates ) ) {
628 $all_translation_updates = ( isset( $transient_data->translations ) && is_array( $transient_data->translations ) ) ?
629 $transient_data->translations :
630 array();
631
632 $current_plugin_translation_updates_map = array();
633 foreach ( $all_translation_updates as $key => $translation_update ) {
634 if ( $module_type === ( $translation_update['type'] . 's' ) && $slug === $translation_update['slug'] ) {
635 $current_plugin_translation_updates_map[ $translation_update['language'] ] = $translation_update;
636 unset( $all_translation_updates[ $key ] );
637 }
638 }
639
640 foreach ( $this->_translation_updates as $translation_update ) {
641 $lang = $translation_update['language'];
642 if ( ! isset( $current_plugin_translation_updates_map[ $lang ] ) ||
643 version_compare( $translation_update['version'], $current_plugin_translation_updates_map[ $lang ]['version'], '>' )
644 ) {
645 $current_plugin_translation_updates_map[ $lang ] = $translation_update;
646 }
647 }
648
649 $transient_data->translations = array_merge( $all_translation_updates, array_values( $current_plugin_translation_updates_map ) );
650 }
651 }
652
653 return $transient_data;
654 }
655
656 /**
657 * Get module's required data for the updates' mechanism.
658 *
659 * @author Vova Feldman (@svovaf)
660 * @since 2.0.0
661 *
662 * @param \FS_Plugin_Tag $new_version
663 *
664 * @return object
665 */
666 function get_update_details( FS_Plugin_Tag $new_version ) {
667 $update = new stdClass();
668 $update->slug = $this->_fs->get_slug();
669 $update->new_version = $new_version->version;
670 $update->url = WP_FS__ADDRESS;
671 $update->package = $new_version->url;
672 $update->tested = self::get_tested_wp_version( $new_version->tested_up_to_version );
673 $update->requires = $new_version->requires_platform_version;
674 $update->requires_php = $new_version->requires_programming_language_version;
675
676 $icon = $this->_fs->get_local_icon_url();
677
678 if ( ! empty( $icon ) ) {
679 $update->icons = array(
680 // '1x' => $icon,
681 // '2x' => $icon,
682 'default' => $icon,
683 );
684 }
685
686 if ( $this->_fs->is_premium() ) {
687 $latest_tag = $this->_fs->_fetch_latest_version( $this->_fs->get_id(), false );
688
689 if (
690 isset( $latest_tag->readme ) &&
691 isset( $latest_tag->readme->upgrade_notice ) &&
692 ! empty( $latest_tag->readme->upgrade_notice )
693 ) {
694 $update->upgrade_notice = $latest_tag->readme->upgrade_notice;
695 }
696 }
697
698 $update->{$this->_fs->get_module_type()} = $this->_fs->get_plugin_basename();
699
700 return $update;
701 }
702
703 /**
704 * @author Leo Fajardo (@leorw)
705 * @since 2.3.0
706 *
707 * @param FS_Plugin_Tag $new_version
708 *
709 * @return bool
710 */
711 private function is_new_version_premium( FS_Plugin_Tag $new_version ) {
712 $params = fs_parse_url_params( $new_version->url );
713
714 return ( isset( $params['is_premium'] ) && 'true' == $params['is_premium'] );
715 }
716
717 /**
718 * Update the updates transient with the module's update information.
719 *
720 * This method is required for multisite environment.
721 * If a module is site activated (not network) and not on the main site,
722 * the module will NOT be executed on the network level, therefore, the
723 * custom updates logic will not be executed as well, so unless we force
724 * the injection of the update into the updates transient, premium updates
725 * will not work.
726 *
727 * @author Vova Feldman (@svovaf)
728 * @since 2.0.0
729 *
730 * @param \FS_Plugin_Tag $new_version
731 */
732 function set_update_data( FS_Plugin_Tag $new_version ) {
733 $this->_logger->entrance();
734
735 if ( ! $this->is_new_version_premium( $new_version ) ) {
736 return;
737 }
738
739 $transient_key = "update_{$this->_fs->get_module_type()}s";
740
741 $transient_data = get_site_transient( $transient_key );
742
743 $transient_data = is_object( $transient_data ) ?
744 $transient_data :
745 new stdClass();
746
747 // Alias.
748 $basename = $this->_fs->get_plugin_basename();
749 $is_plugin = $this->_fs->is_plugin();
750
751 if ( ! isset( $transient_data->response ) ||
752 ! is_array( $transient_data->response )
753 ) {
754 $transient_data->response = array();
755 } else if ( ! empty( $transient_data->response[ $basename ] ) ) {
756 $version = $is_plugin ?
757 ( ! empty( $transient_data->response[ $basename ]->new_version ) ?
758 $transient_data->response[ $basename ]->new_version :
759 null
760 ) : ( ! empty( $transient_data->response[ $basename ]['new_version'] ) ?
761 $transient_data->response[ $basename ]['new_version'] :
762 null
763 );
764
765 if ( $version == $new_version->version ) {
766 // The update data is already set.
767 return;
768 }
769 }
770
771 // Remove the added filters.
772 $this->remove_transient_filters();
773
774 $this->_update_details = $this->get_update_details( $new_version );
775
776 // Set update data in transient.
777 $transient_data->response[ $basename ] = $is_plugin ?
778 $this->_update_details :
779 (array) $this->_update_details;
780
781 if ( ! isset( $transient_data->checked ) ||
782 ! is_array( $transient_data->checked )
783 ) {
784 $transient_data->checked = array();
785 }
786
787 // Flag the module as if it was already checked.
788 $transient_data->checked[ $basename ] = $this->_fs->get_plugin_version();
789 $transient_data->last_checked = time();
790
791 set_site_transient( $transient_key, $transient_data );
792
793 $this->add_transient_filters();
794 }
795
796 /**
797 * @author Leo Fajardo (@leorw)
798 * @since 2.0.2
799 */
800 function delete_update_data() {
801 $this->_logger->entrance();
802
803 $transient_key = "update_{$this->_fs->get_module_type()}s";
804
805 $transient_data = get_site_transient( $transient_key );
806
807 // Alias
808 $basename = $this->_fs->get_plugin_basename();
809
810 if ( ! is_object( $transient_data ) ||
811 ! isset( $transient_data->response ) ||
812 ! is_array( $transient_data->response ) ||
813 empty( $transient_data->response[ $basename ] )
814 ) {
815 return;
816 }
817
818 unset( $transient_data->response[ $basename ] );
819
820 // Remove the added filters.
821 $this->remove_transient_filters();
822
823 set_site_transient( $transient_key, $transient_data );
824
825 $this->add_transient_filters();
826 }
827
828 /**
829 * Try to fetch plugin's info from .org repository.
830 *
831 * @author Vova Feldman (@svovaf)
832 * @since 1.0.5
833 *
834 * @param string $action
835 * @param object $args
836 *
837 * @return bool|mixed
838 */
839 static function _fetch_plugin_info_from_repository( $action, $args ) {
840 $url = $http_url = 'http://api.wordpress.org/plugins/info/1.2/';
841 $url = add_query_arg(
842 array(
843 'action' => $action,
844 'request' => $args,
845 ),
846 $url
847 );
848
849 if ( wp_http_supports( array( 'ssl' ) ) ) {
850 $url = set_url_scheme( $url, 'https' );
851 }
852
853 // The new endpoint version serves only GET requests.
854 $request = wp_remote_get( $url, array( 'timeout' => 15 ) );
855
856 if ( is_wp_error( $request ) ) {
857 return false;
858 }
859
860 $res = json_decode( wp_remote_retrieve_body( $request ), true );
861
862 if ( is_array( $res ) ) {
863 // Object casting is required in order to match the info/1.0 format. We are not decoding directly into an object as we need some fields to remain an array (e.g., $res->sections).
864 $res = (object) $res;
865 }
866
867 if ( ! is_object( $res ) || isset( $res->error ) ) {
868 return false;
869 }
870
871 return $res;
872 }
873
874 /**
875 * Fetches module translation updates from wordpress.org.
876 *
877 * @author Leo Fajardo (@leorw)
878 * @since 2.1.2
879 *
880 * @param string $module_type
881 * @param string $slug
882 *
883 * @return array|null
884 */
885 private function fetch_wp_org_module_translation_updates( $module_type, $slug ) {
886 $plugin_data = $this->_fs->get_plugin_data();
887
888 $locales = array_values( get_available_languages() );
889 $locales = apply_filters( "{$module_type}_update_check_locales", $locales );
890 $locales = array_unique( $locales );
891
892 $plugin_basename = $this->_fs->get_plugin_basename();
893 if ( 'themes' === $module_type ) {
894 $plugin_basename = $slug;
895 }
896
897 global $wp_version;
898
899 $request_args = array(
900 'timeout' => 15,
901 'body' => array(
902 "{$module_type}" => json_encode(
903 array(
904 "{$module_type}" => array(
905 $plugin_basename => array(
906 'Name' => trim( str_replace( $this->_fs->get_plugin()->premium_suffix, '', $plugin_data['Name'] ) ),
907 'Author' => $plugin_data['Author'],
908 )
909 )
910 )
911 ),
912 'translations' => json_encode( $this->get_installed_translations( $module_type, $slug ) ),
913 'locale' => json_encode( $locales )
914 ),
915 'user-agent' => ( 'WordPress/' . $wp_version . '; ' . home_url( '/' ) )
916 );
917
918 $url = "http://api.wordpress.org/{$module_type}/update-check/1.1/";
919 if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
920 $url = set_url_scheme( $url, 'https' );
921 }
922
923 $raw_response = Freemius::safe_remote_post(
924 $url,
925 $request_args,
926 WP_FS__TIME_24_HOURS_IN_SEC,
927 WP_FS__TIME_12_HOURS_IN_SEC,
928 false
929 );
930
931 if ( is_wp_error( $raw_response ) ) {
932 return null;
933 }
934
935 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
936
937 if ( ! is_array( $response ) ) {
938 return null;
939 }
940
941 if ( ! isset( $response['translations'] ) || empty( $response['translations'] ) ) {
942 return null;
943 }
944
945 return $response['translations'];
946 }
947
948 /**
949 * @author Leo Fajardo (@leorw)
950 * @since 2.1.2
951 *
952 * @param string $module_type
953 * @param string $slug
954 *
955 * @return array
956 */
957 private function get_installed_translations( $module_type, $slug ) {
958 if ( function_exists( 'wp_get_installed_translations' ) ) {
959 return wp_get_installed_translations( $module_type );
960 }
961
962 $dir = "/{$module_type}";
963
964 if ( ! is_dir( WP_LANG_DIR . $dir ) )
965 return array();
966
967 $files = scandir( WP_LANG_DIR . $dir );
968 if ( ! $files )
969 return array();
970
971 $language_data = array();
972
973 foreach ( $files as $file ) {
974 if ( 0 !== strpos( $file, $slug ) ) {
975 continue;
976 }
977
978 if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "{$dir}/{$file}" ) ) {
979 continue;
980 }
981
982 if ( substr( $file, -3 ) !== '.po' ) {
983 continue;
984 }
985
986 if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) {
987 continue;
988 }
989
990 if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files ) ) {
991 continue;
992 }
993
994 list( , $textdomain, $language ) = $match;
995
996 if ( '' === $textdomain ) {
997 $textdomain = 'default';
998 }
999
1000 $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "{$dir}/{$file}" );
1001 }
1002
1003 return $language_data;
1004 }
1005
1006 /**
1007 * Updates information on the "View version x.x details" page with custom data.
1008 *
1009 * @author Vova Feldman (@svovaf)
1010 * @since 1.0.4
1011 *
1012 * @uses FS_Api
1013 *
1014 * @param object $data
1015 * @param string $action
1016 * @param mixed $args
1017 *
1018 * @return object
1019 */
1020 function plugins_api_filter( $data, $action = '', $args = null ) {
1021 $this->_logger->entrance();
1022
1023 if ( ( 'plugin_information' !== $action ) ||
1024 ! isset( $args->slug )
1025 ) {
1026 return $data;
1027 }
1028
1029 $addon = false;
1030 $is_addon = false;
1031 $addon_version = false;
1032
1033 if ( $this->_fs->get_slug() !== $args->slug ) {
1034 $addon = $this->_fs->get_addon_by_slug( $args->slug );
1035
1036 if ( ! is_object( $addon ) ) {
1037 return $data;
1038 }
1039
1040 if ( $this->_fs->is_addon_activated( $addon->id ) ) {
1041 $addon_version = $this->_fs->get_addon_instance( $addon->id )->get_plugin_version();
1042 } else if ( $this->_fs->is_addon_installed( $addon->id ) ) {
1043 $addon_plugin_data = get_plugin_data(
1044 ( WP_PLUGIN_DIR . '/' . $this->_fs->get_addon_basename( $addon->id ) ),
1045 false,
1046 false
1047 );
1048
1049 if ( ! empty( $addon_plugin_data ) ) {
1050 $addon_version = $addon_plugin_data['Version'];
1051 }
1052 }
1053
1054 $is_addon = true;
1055 }
1056
1057 $plugin_in_repo = false;
1058 if ( ! $is_addon ) {
1059 // Try to fetch info from .org repository.
1060 $data = self::_fetch_plugin_info_from_repository( $action, $args );
1061
1062 $plugin_in_repo = ( false !== $data );
1063 }
1064
1065 if ( ! $plugin_in_repo ) {
1066 $data = $args;
1067
1068 // Fetch as much as possible info from local files.
1069 $plugin_local_data = $this->_fs->get_plugin_data();
1070 $data->name = $plugin_local_data['Name'];
1071 $data->author = $plugin_local_data['Author'];
1072 $data->sections = array(
1073 'description' => 'Upgrade ' . $plugin_local_data['Name'] . ' to latest.',
1074 );
1075
1076 // @todo Store extra plugin info on Freemius or parse readme.txt markup.
1077 /*$info = $this->_fs->get_api_site_scope()->call('/information.json');
1078
1079 if ( !isset($info->error) ) {
1080 $data = $info;
1081 }*/
1082 }
1083
1084 $plugin_version = $is_addon ?
1085 $addon_version :
1086 $this->_fs->get_plugin_version();
1087
1088 // Get plugin's newest update.
1089 $new_version = $this->get_latest_download_details( $is_addon ? $addon->id : false, $plugin_version );
1090
1091 if ( ! is_object( $new_version ) || empty( $new_version->version ) ) {
1092 $data->version = $plugin_version;
1093 } else {
1094 if ( $is_addon ) {
1095 $data->name = $addon->title . ' ' . $this->_fs->get_text_inline( 'Add-On', 'addon' );
1096 $data->slug = $addon->slug;
1097 $data->url = WP_FS__ADDRESS;
1098 $data->package = $new_version->url;
1099 }
1100
1101 if ( ! $plugin_in_repo ) {
1102 $data->last_updated = ! is_null( $new_version->updated ) ? $new_version->updated : $new_version->created;
1103 $data->requires = $new_version->requires_platform_version;
1104 $data->requires_php = $new_version->requires_programming_language_version;
1105 $data->tested = $new_version->tested_up_to_version;
1106 }
1107
1108 $data->version = $new_version->version;
1109 $data->download_link = $new_version->url;
1110
1111 if ( isset( $new_version->readme ) && is_object( $new_version->readme ) ) {
1112 $new_version_readme_data = $new_version->readme;
1113 if ( isset( $new_version_readme_data->sections ) ) {
1114 $new_version_readme_data->sections = (array) $new_version_readme_data->sections;
1115 } else {
1116 $new_version_readme_data->sections = array();
1117 }
1118
1119 if ( isset( $data->sections ) ) {
1120 if ( isset( $data->sections['screenshots'] ) ) {
1121 $new_version_readme_data->sections['screenshots'] = $data->sections['screenshots'];
1122 }
1123
1124 if ( isset( $data->sections['reviews'] ) ) {
1125 $new_version_readme_data->sections['reviews'] = $data->sections['reviews'];
1126 }
1127 }
1128
1129 if ( isset( $new_version_readme_data->banners ) ) {
1130 $new_version_readme_data->banners = (array) $new_version_readme_data->banners;
1131 } else if ( isset( $data->banners ) ) {
1132 $new_version_readme_data->banners = $data->banners;
1133 }
1134
1135 $wp_org_sections = array(
1136 'author',
1137 'author_profile',
1138 'rating',
1139 'ratings',
1140 'num_ratings',
1141 'support_threads',
1142 'support_threads_resolved',
1143 'active_installs',
1144 'added',
1145 'homepage'
1146 );
1147
1148 foreach ( $wp_org_sections as $wp_org_section ) {
1149 if ( isset( $data->{$wp_org_section} ) ) {
1150 $new_version_readme_data->{$wp_org_section} = $data->{$wp_org_section};
1151 }
1152 }
1153
1154 $data = $new_version_readme_data;
1155 }
1156 }
1157
1158 if ( ! empty( $data->tested ) ) {
1159 $data->tested = self::get_tested_wp_version( $data->tested );
1160 }
1161
1162 return $data;
1163 }
1164
1165 /**
1166 * @since 2.5.3 If the current WordPress version is a patch of the tested version (e.g., 6.1.2 is a patch of 6.1), then override the tested version with the patch so developers won't need to release a new version just to bump the latest supported WP version.
1167 *
1168 * @param string|null $tested_up_to
1169 *
1170 * @return string|null
1171 */
1172 private static function get_tested_wp_version( $tested_up_to ) {
1173 $current_wp_version = get_bloginfo( 'version' );
1174
1175 return ( ! empty($tested_up_to) && fs_starts_with( $current_wp_version, $tested_up_to . '.' ) ) ?
1176 $current_wp_version :
1177 $tested_up_to;
1178 }
1179
1180 /**
1181 * @author Vova Feldman (@svovaf)
1182 * @since 1.2.1.7
1183 *
1184 * @param number|bool $addon_id
1185 * @param bool|string $newer_than Since 2.2.1
1186 * @param bool|string $fetch_readme Since 2.2.1
1187 *
1188 * @return object
1189 */
1190 private function get_latest_download_details( $addon_id = false, $newer_than = false, $fetch_readme = true ) {
1191 return $this->_fs->_fetch_latest_version( $addon_id, true, WP_FS__TIME_24_HOURS_IN_SEC, $newer_than, $fetch_readme );
1192 }
1193
1194 /**
1195 * Checks if a given basename has a matching folder name
1196 * with the current context plugin.
1197 *
1198 * @author Vova Feldman (@svovaf)
1199 * @since 1.2.1.6
1200 *
1201 * @return bool
1202 */
1203 private function is_correct_folder_name() {
1204 return ( $this->_fs->get_target_folder_name() == trim( dirname( $this->_fs->get_plugin_basename() ), '/\\' ) );
1205 }
1206
1207 /**
1208 * This is a special after upgrade handler for migrating modules
1209 * that didn't use the '-premium' suffix folder structure before
1210 * the migration.
1211 *
1212 * @author Vova Feldman (@svovaf)
1213 * @since 1.2.1.6
1214 *
1215 * @param bool $response Install response.
1216 * @param array $hook_extra Extra arguments passed to hooked filters.
1217 * @param array $result Installation result data.
1218 *
1219 * @return bool
1220 */
1221 function _maybe_update_folder_name( $response, $hook_extra, $result ) {
1222 $basename = $this->_fs->get_plugin_basename();
1223
1224 if ( true !== $response ||
1225 empty( $hook_extra ) ||
1226 empty( $hook_extra['plugin'] ) ||
1227 $basename !== $hook_extra['plugin']
1228 ) {
1229 return $response;
1230 }
1231
1232 $active_plugins_basenames = get_option( 'active_plugins' );
1233
1234 foreach ( $active_plugins_basenames as $key => $active_plugin_basename ) {
1235 if ( $basename === $active_plugin_basename ) {
1236 // Get filename including extension.
1237 $filename = basename( $basename );
1238
1239 $new_basename = plugin_basename(
1240 trailingslashit( $this->_fs->is_premium() ? $this->_fs->get_premium_slug() : $this->_fs->get_slug() ) .
1241 $filename
1242 );
1243
1244 // Verify that the expected correct path exists.
1245 if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $new_basename ) ) ) {
1246 // Override active plugin name.
1247 $active_plugins_basenames[ $key ] = $new_basename;
1248 update_option( 'active_plugins', $active_plugins_basenames );
1249 }
1250
1251 break;
1252 }
1253 }
1254
1255 return $response;
1256 }
1257
1258 #----------------------------------------------------------------------------------
1259 #region Auto Activation
1260 #----------------------------------------------------------------------------------
1261
1262 /**
1263 * Installs and active a plugin when explicitly requested that from a 3rd party service.
1264 *
1265 * This logic was inspired by the TGMPA GPL licensed library by Thomas Griffin.
1266 *
1267 * @link http://tgmpluginactivation.com/
1268 *
1269 * @author Vova Feldman
1270 * @since 1.2.1.7
1271 *
1272 * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
1273 *
1274 * @uses WP_Filesystem
1275 * @uses WP_Error
1276 * @uses WP_Upgrader
1277 * @uses Plugin_Upgrader
1278 * @uses Plugin_Installer_Skin
1279 * @uses Plugin_Upgrader_Skin
1280 *
1281 * @param number|bool $plugin_id
1282 *
1283 * @return array
1284 */
1285 function install_and_activate_plugin( $plugin_id = false ) {
1286 if ( ! empty( $plugin_id ) && ! FS_Plugin::is_valid_id( $plugin_id ) ) {
1287 // Invalid plugin ID.
1288 return array(
1289 'message' => $this->_fs->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
1290 'code' => 'invalid_module_id',
1291 );
1292 }
1293
1294 $is_addon = false;
1295 if ( FS_Plugin::is_valid_id( $plugin_id ) &&
1296 $plugin_id != $this->_fs->get_id()
1297 ) {
1298 $addon = $this->_fs->get_addon( $plugin_id );
1299
1300 if ( ! is_object( $addon ) ) {
1301 // Invalid add-on ID.
1302 return array(
1303 'message' => $this->_fs->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
1304 'code' => 'invalid_module_id',
1305 );
1306 }
1307
1308 $slug = $addon->slug;
1309 $premium_slug = $addon->premium_slug;
1310 $title = $addon->title . ' ' . $this->_fs->get_text_inline( 'Add-On', 'addon' );
1311
1312 $is_addon = true;
1313 } else {
1314 $slug = $this->_fs->get_slug();
1315 $premium_slug = $this->_fs->get_premium_slug();
1316 $title = $this->_fs->get_plugin_title() .
1317 ( $this->_fs->is_addon() ? ' ' . $this->_fs->get_text_inline( 'Add-On', 'addon' ) : '' );
1318 }
1319
1320 if ( $this->is_premium_plugin_active( $plugin_id ) ) {
1321 // Premium version already activated.
1322 return array(
1323 'message' => $is_addon ?
1324 $this->_fs->get_text_inline( 'Premium add-on version already installed.', 'auto-install-error-premium-addon-activated' ) :
1325 $this->_fs->get_text_inline( 'Premium version already active.', 'auto-install-error-premium-activated' ),
1326 'code' => 'premium_installed',
1327 );
1328 }
1329
1330 $latest_version = $this->get_latest_download_details( $plugin_id, false, false );
1331 $target_folder = $premium_slug;
1332
1333 // Prep variables for Plugin_Installer_Skin class.
1334 $extra = array();
1335 $extra['slug'] = $target_folder;
1336 $source = $latest_version->url;
1337 $api = null;
1338
1339 $install_url = add_query_arg(
1340 array(
1341 'action' => 'install-plugin',
1342 'plugin' => urlencode( $slug ),
1343 ),
1344 'update.php'
1345 );
1346
1347 if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
1348 // Include required resources for the installation.
1349 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
1350 }
1351
1352 $skin_args = array(
1353 'type' => 'web',
1354 'title' => sprintf( $this->_fs->get_text_inline( 'Installing plugin: %s', 'installing-plugin-x' ), $title ),
1355 'url' => esc_url_raw( $install_url ),
1356 'nonce' => 'install-plugin_' . $slug,
1357 'plugin' => '',
1358 'api' => $api,
1359 'extra' => $extra,
1360 );
1361
1362 // $skin = new Automatic_Upgrader_Skin( $skin_args );
1363 // $skin = new Plugin_Installer_Skin( $skin_args );
1364 $skin = new WP_Ajax_Upgrader_Skin( $skin_args );
1365
1366 // Create a new instance of Plugin_Upgrader.
1367 $upgrader = new Plugin_Upgrader( $skin );
1368
1369 // Perform the action and install the plugin from the $source urldecode().
1370 add_filter( 'upgrader_source_selection', array( 'FS_Plugin_Updater', '_maybe_adjust_source_dir' ), 1, 3 );
1371
1372 $install_result = $upgrader->install( $source );
1373
1374 remove_filter( 'upgrader_source_selection', array( 'FS_Plugin_Updater', '_maybe_adjust_source_dir' ), 1 );
1375
1376 if ( is_wp_error( $install_result ) ) {
1377 return array(
1378 'message' => $install_result->get_error_message(),
1379 'code' => $install_result->get_error_code(),
1380 );
1381 } elseif ( is_wp_error( $skin->result ) ) {
1382 return array(
1383 'message' => $skin->result->get_error_message(),
1384 'code' => $skin->result->get_error_code(),
1385 );
1386 } elseif ( $skin->get_errors()->get_error_code() ) {
1387 return array(
1388 'message' => $skin->get_error_messages(),
1389 'code' => 'unknown',
1390 );
1391 } elseif ( is_null( $install_result ) ) {
1392 global $wp_filesystem;
1393
1394 $error_code = 'unable_to_connect_to_filesystem';
1395 $error_message = $this->_fs->get_text_inline( 'Unable to connect to the filesystem. Please confirm your credentials.' );
1396
1397 // Pass through the error from WP_Filesystem if one was raised.
1398 if ( $wp_filesystem instanceof WP_Filesystem_Base &&
1399 is_wp_error( $wp_filesystem->errors ) &&
1400 $wp_filesystem->errors->get_error_code()
1401 ) {
1402 $error_message = $wp_filesystem->errors->get_error_message();
1403 }
1404
1405 return array(
1406 'message' => $error_message,
1407 'code' => $error_code,
1408 );
1409 }
1410
1411 // Grab the full path to the main plugin's file.
1412 $plugin_activate = $upgrader->plugin_info();
1413
1414 // Try to activate the plugin.
1415 $activation_result = $this->try_activate_plugin( $plugin_activate );
1416
1417 if ( is_wp_error( $activation_result ) ) {
1418 return array(
1419 'message' => $activation_result->get_error_message(),
1420 'code' => $activation_result->get_error_code(),
1421 );
1422 }
1423
1424 return $skin->get_upgrade_messages();
1425 }
1426
1427 /**
1428 * Tries to activate a plugin. If fails, returns the error.
1429 *
1430 * @author Vova Feldman
1431 * @since 1.2.1.7
1432 *
1433 * @param string $file_path Path within wp-plugins/ to main plugin file.
1434 * This determines the styling of the output messages.
1435 *
1436 * @return bool|WP_Error
1437 */
1438 protected function try_activate_plugin( $file_path ) {
1439 $activate = activate_plugin( $file_path, '', $this->_fs->is_network_active() );
1440
1441 return is_wp_error( $activate ) ?
1442 $activate :
1443 true;
1444 }
1445
1446 /**
1447 * Check if a premium module version is already active.
1448 *
1449 * @author Vova Feldman
1450 * @since 1.2.1.7
1451 *
1452 * @param number|bool $plugin_id
1453 *
1454 * @return bool
1455 */
1456 private function is_premium_plugin_active( $plugin_id = false ) {
1457 if ( $plugin_id != $this->_fs->get_id() ) {
1458 return $this->_fs->is_addon_activated( $plugin_id, true );
1459 }
1460
1461 return is_plugin_active( $this->_fs->premium_plugin_basename() );
1462 }
1463
1464 /**
1465 * Store the basename since it's not always available in the `_maybe_adjust_source_dir` method below.
1466 *
1467 * @author Leo Fajardo (@leorw)
1468 * @since 2.2.1
1469 *
1470 * @param bool|WP_Error $response Response.
1471 * @param array $hook_extra Extra arguments passed to hooked filters.
1472 *
1473 * @return bool|WP_Error
1474 */
1475 static function _store_basename_for_source_adjustment( $response, $hook_extra ) {
1476 if ( isset( $hook_extra['plugin'] ) ) {
1477 self::$_upgrade_basename = $hook_extra['plugin'];
1478 } else if ( isset( $hook_extra['theme'] ) ) {
1479 self::$_upgrade_basename = $hook_extra['theme'];
1480 } else {
1481 self::$_upgrade_basename = null;
1482 }
1483
1484 return $response;
1485 }
1486
1487 /**
1488 * Adjust the plugin directory name if necessary.
1489 * Assumes plugin has a folder (not a single file plugin).
1490 *
1491 * The final destination directory of a plugin is based on the subdirectory name found in the
1492 * (un)zipped source. In some cases this subdirectory name is not the same as the expected
1493 * slug and the plugin will not be recognized as installed. This is fixed by adjusting
1494 * the temporary unzipped source subdirectory name to the expected plugin slug.
1495 *
1496 * @author Vova Feldman
1497 * @since 1.2.1.7
1498 * @since 2.2.1 The method was converted to static since when the admin update bulk products via the Updates section, the logic applies the `upgrader_source_selection` filter for every product that is being updated.
1499 *
1500 * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/.
1501 * @param string $remote_source Path to upgrade/zip-file-name.tmp.
1502 * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin.
1503 *
1504 * @return string|WP_Error
1505 */
1506 static function _maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
1507 if ( ! is_object( $GLOBALS['wp_filesystem'] ) ) {
1508 return $source;
1509 }
1510
1511 $basename = self::$_upgrade_basename;
1512 $is_theme = false;
1513
1514 // Figure out what the slug is supposed to be.
1515 if ( isset( $upgrader->skin->options['extra'] ) ) {
1516 // Set by the auto-install logic.
1517 $desired_slug = $upgrader->skin->options['extra']['slug'];
1518 } else if ( ! empty( $basename ) ) {
1519 /**
1520 * If it doesn't end with ".php", it's a theme.
1521 *
1522 * @author Leo Fajardo (@leorw)
1523 * @since 2.2.1
1524 */
1525 $is_theme = ( ! fs_ends_with( $basename, '.php' ) );
1526
1527 $desired_slug = ( ! $is_theme ) ?
1528 dirname( $basename ) :
1529 // Theme slug
1530 $basename;
1531 } else {
1532 // Can't figure out the desired slug, stop the execution.
1533 return $source;
1534 }
1535
1536 if ( is_multisite() ) {
1537 /**
1538 * If we are running in a multisite environment and the product is not network activated,
1539 * the instance will not exist anyway. Therefore, try to update the source if necessary
1540 * regardless if the Freemius instance of the product exists or not.
1541 *
1542 * @author Vova Feldman
1543 */
1544 } else if ( ! empty( $basename ) ) {
1545 $fs = Freemius::get_instance_by_file(
1546 $basename,
1547 $is_theme ?
1548 WP_FS__MODULE_TYPE_THEME :
1549 WP_FS__MODULE_TYPE_PLUGIN
1550 );
1551
1552 if ( ! is_object( $fs ) ) {
1553 /**
1554 * If the Freemius instance does not exist on a non-multisite network environment, it means that:
1555 * 1. The product is not powered by Freemius; OR
1556 * 2. The product is not activated, therefore, we don't mind if after the update the folder name will change.
1557 *
1558 * @author Leo Fajardo (@leorw)
1559 * @since 2.2.1
1560 */
1561 return $source;
1562 }
1563 }
1564
1565 $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
1566
1567 if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
1568 $from_path = untrailingslashit( $source );
1569 $to_path = trailingslashit( $remote_source ) . $desired_slug;
1570
1571 if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
1572 return trailingslashit( $to_path );
1573 }
1574
1575 return new WP_Error(
1576 'rename_failed',
1577 fs_text_inline( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'module-package-rename-failure' ),
1578 array(
1579 'found' => $subdir_name,
1580 'expected' => $desired_slug
1581 )
1582 );
1583 }
1584
1585 return $source;
1586 }
1587
1588 #endregion
1589 }
1590