TGM_Plugin_Activation

Automatic plugin installation and activation library.

Defined (1)

The class is defined in the following location(s).

/class-tgm-plugin-activation.php  
  1. class TGM_Plugin_Activation { 
  2. /** 
  3. * TGMPA version number. 
  4. * @since 2.5.0 
  5. * @const string Version number. 
  6. */ 
  7. const TGMPA_VERSION = '2.5.2'; 
  8.  
  9. /** 
  10. * Regular expression to test if a URL is a WP plugin repo URL. 
  11. * @const string Regex. 
  12. * @since 2.5.0 
  13. */ 
  14. const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|'; 
  15.  
  16. /** 
  17. * Arbitrary regular expression to test if a string starts with a URL. 
  18. * @const string Regex. 
  19. * @since 2.5.0 
  20. */ 
  21. const IS_URL_REGEX = '|^http[s]?://|'; 
  22.  
  23. /** 
  24. * Holds a copy of itself, so it can be referenced by the class name. 
  25. * @since 1.0.0 
  26. * @var TGM_Plugin_Activation 
  27. */ 
  28. public static $instance; 
  29.  
  30. /** 
  31. * Holds arrays of plugin details. 
  32. * @since 1.0.0 
  33. * @since 2.5.0 the array has the plugin slug as an associative key. 
  34. * @var array 
  35. */ 
  36. public $plugins = array(); 
  37.  
  38. /** 
  39. * Holds arrays of plugin names to use to sort the plugins array. 
  40. * @since 2.5.0 
  41. * @var array 
  42. */ 
  43. protected $sort_order = array(); 
  44.  
  45. /** 
  46. * Whether any plugins have the 'force_activation' setting set to true. 
  47. * @since 2.5.0 
  48. * @var bool 
  49. */ 
  50. protected $has_forced_activation = false; 
  51.  
  52. /** 
  53. * Whether any plugins have the 'force_deactivation' setting set to true. 
  54. * @since 2.5.0 
  55. * @var bool 
  56. */ 
  57. protected $has_forced_deactivation = false; 
  58.  
  59. /** 
  60. * Name of the unique ID to hash notices. 
  61. * @since 2.4.0 
  62. * @var string 
  63. */ 
  64. public $id = 'megaresponsive-lite'; 
  65.  
  66. /** 
  67. * Name of the query-string argument for the admin page. 
  68. * @since 1.0.0 
  69. * @var string 
  70. */ 
  71. protected $menu = 'tgmpa-install-plugins'; 
  72.  
  73. /** 
  74. * Parent menu file slug. 
  75. * @since 2.5.0 
  76. * @var string 
  77. */ 
  78. public $parent_slug = 'themes.php'; 
  79.  
  80. /** 
  81. * Capability needed to view the plugin installation menu item. 
  82. * @since 2.5.0 
  83. * @var string 
  84. */ 
  85. public $capability = 'edit_theme_options'; 
  86.  
  87. /** 
  88. * Default absolute path to folder containing bundled plugin zip files. 
  89. * @since 2.0.0 
  90. * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string. 
  91. */ 
  92. public $default_path = ''; 
  93.  
  94. /** 
  95. * Flag to show admin notices or not. 
  96. * @since 2.1.0 
  97. * @var boolean 
  98. */ 
  99. public $has_notices = true; 
  100.  
  101. /** 
  102. * Flag to determine if the user can dismiss the notice nag. 
  103. * @since 2.4.0 
  104. * @var boolean 
  105. */ 
  106. public $dismissable = true; 
  107.  
  108. /** 
  109. * Message to be output above nag notice if dismissable is false. 
  110. * @since 2.4.0 
  111. * @var string 
  112. */ 
  113. public $dismiss_msg = ''; 
  114.  
  115. /** 
  116. * Flag to set automatic activation of plugins. Off by default. 
  117. * @since 2.2.0 
  118. * @var boolean 
  119. */ 
  120. public $is_automatic = false; 
  121.  
  122. /** 
  123. * Optional message to display before the plugins table. 
  124. * @since 2.2.0 
  125. * @var string Message filtered by wp_kses_post(). Default is empty string. 
  126. */ 
  127. public $message = ''; 
  128.  
  129. /** 
  130. * Holds configurable array of strings. 
  131. * Default values are added in the constructor. 
  132. * @since 2.0.0 
  133. * @var array 
  134. */ 
  135. public $strings = array(); 
  136.  
  137. /** 
  138. * Holds the version of WordPress. 
  139. * @since 2.4.0 
  140. * @var int 
  141. */ 
  142. public $wp_version; 
  143.  
  144. /** 
  145. * Holds the hook name for the admin page. 
  146. * @since 2.5.0 
  147. * @var string 
  148. */ 
  149. public $page_hook; 
  150.  
  151. /** 
  152. * Adds a reference of this object to $instance, populates default strings,  
  153. * does the tgmpa_init action hook, and hooks in the interactions to init. 
  154. * @internal This method should be `protected`, but as too many TGMPA implementations 
  155. * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues. 
  156. * Reverted back to public for the time being. 
  157. * @since 1.0.0 
  158. * @see TGM_Plugin_Activation::init() 
  159. */ 
  160. public function __construct() { 
  161. // Set the current WordPress version. 
  162. $this->wp_version = $GLOBALS['wp_version']; 
  163.  
  164. // Announce that the class is ready, and pass the object (for advanced use). 
  165. do_action_ref_array( 'tgmpa_init', array( $this ) ); 
  166.  
  167. // When the rest of WP has loaded, kick-start the rest of the class. 
  168. add_action( 'init', array( $this, 'init' ) ); 
  169.  
  170. /** 
  171. * Magic method to (not) set protected properties from outside of this class. 
  172. * @internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6 where the `menu` property 
  173. * is being assigned rather than tested in a conditional, effectively rendering it useless. 
  174. * This 'hack' prevents this from happening. 
  175. * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593 
  176. * @param string $name Name of an inaccessible property. 
  177. * @param mixed $value Value to assign to the property. 
  178. * @return void Silently fail to set the property when this is tried from outside of this class context. 
  179. * (Inside this class context, the __set() method if not used as there is direct access.) 
  180. */ 
  181. public function __set( $name, $value ) { 
  182. return; 
  183.  
  184. /** 
  185. * Magic method to get the value of a protected property outside of this class context. 
  186. * @param string $name Name of an inaccessible property. 
  187. * @return mixed The property value. 
  188. */ 
  189. public function __get( $name ) { 
  190. return $this->{$name}; 
  191.  
  192. /** 
  193. * Initialise the interactions between this class and WordPress. 
  194. * Hooks in three new methods for the class: admin_menu, notices and styles. 
  195. * @since 2.0.0 
  196. * @see TGM_Plugin_Activation::admin_menu() 
  197. * @see TGM_Plugin_Activation::notices() 
  198. * @see TGM_Plugin_Activation::styles() 
  199. */ 
  200. public function init() { 
  201. /** 
  202. * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter 
  203. * you can overrule that behaviour. 
  204. * @since 2.5.0 
  205. * @param bool $load Whether or not TGMPA should load. 
  206. * Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`. 
  207. */ 
  208. if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) { 
  209. return; 
  210.  
  211. // Load class strings. 
  212. $this->strings = array( 
  213. 'page_title' => __( 'Install Required Plugins', 'megaresponsive-lite' ),  
  214. 'menu_title' => __( 'Install Plugins', 'megaresponsive-lite' ),  
  215. 'installing' => __( 'Installing Plugin: %s', 'megaresponsive-lite' ),  
  216. 'oops' => __( 'Something went wrong with the plugin API.', 'megaresponsive-lite' ),  
  217. 'notice_can_install_required' => _n_noop( 
  218. 'This theme requires the following plugin: %1$s.',  
  219. 'This theme requires the following plugins: %1$s.',  
  220. 'megaresponsive-lite' 
  221. ),  
  222. 'notice_can_install_recommended' => _n_noop( 
  223. 'This theme recommends the following plugin: %1$s.',  
  224. 'This theme recommends the following plugins: %1$s.',  
  225. 'megaresponsive-lite' 
  226. ),  
  227. 'notice_cannot_install' => _n_noop( 
  228. 'Sorry, but you do not have the correct permissions to install the %1$s plugin.',  
  229. 'Sorry, but you do not have the correct permissions to install the %1$s plugins.',  
  230. 'megaresponsive-lite' 
  231. ),  
  232. 'notice_ask_to_update' => _n_noop( 
  233. 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',  
  234. 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',  
  235. 'megaresponsive-lite' 
  236. ),  
  237. 'notice_ask_to_update_maybe' => _n_noop( 
  238. 'There is an update available for: %1$s.',  
  239. 'There are updates available for the following plugins: %1$s.',  
  240. 'megaresponsive-lite' 
  241. ),  
  242. 'notice_cannot_update' => _n_noop( 
  243. 'Sorry, but you do not have the correct permissions to update the %1$s plugin.',  
  244. 'Sorry, but you do not have the correct permissions to update the %1$s plugins.',  
  245. 'megaresponsive-lite' 
  246. ),  
  247. 'notice_can_activate_required' => _n_noop( 
  248. 'The following required plugin is currently inactive: %1$s.',  
  249. 'The following required plugins are currently inactive: %1$s.',  
  250. 'megaresponsive-lite' 
  251. ),  
  252. 'notice_can_activate_recommended' => _n_noop( 
  253. 'The following recommended plugin is currently inactive: %1$s.',  
  254. 'The following recommended plugins are currently inactive: %1$s.',  
  255. 'megaresponsive-lite' 
  256. ),  
  257. 'notice_cannot_activate' => _n_noop( 
  258. 'Sorry, but you do not have the correct permissions to activate the %1$s plugin.',  
  259. 'Sorry, but you do not have the correct permissions to activate the %1$s plugins.',  
  260. 'megaresponsive-lite' 
  261. ),  
  262. 'install_link' => _n_noop( 
  263. 'Begin installing plugin',  
  264. 'Begin installing plugins',  
  265. 'megaresponsive-lite' 
  266. ),  
  267. 'update_link' => _n_noop( 
  268. 'Begin updating plugin',  
  269. 'Begin updating plugins',  
  270. 'megaresponsive-lite' 
  271. ),  
  272. 'activate_link' => _n_noop( 
  273. 'Begin activating plugin',  
  274. 'Begin activating plugins',  
  275. 'megaresponsive-lite' 
  276. ),  
  277. 'return' => __( 'Return to Required Plugins Installer', 'megaresponsive-lite' ),  
  278. 'dashboard' => __( 'Return to the dashboard', 'megaresponsive-lite' ),  
  279. 'plugin_activated' => __( 'Plugin activated successfully.', 'megaresponsive-lite' ),  
  280. 'activated_successfully' => __( 'The following plugin was activated successfully:', 'megaresponsive-lite' ),  
  281. 'plugin_already_active' => __( 'No action taken. Plugin %1$s was already active.', 'megaresponsive-lite' ),  
  282. 'plugin_needs_higher_version' => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'megaresponsive-lite' ),  
  283. 'complete' => __( 'All plugins installed and activated successfully. %1$s', 'megaresponsive-lite' ),  
  284. 'dismiss' => __( 'Dismiss this notice', 'megaresponsive-lite' ),  
  285. 'contact_admin' => __( 'Please contact the administrator of this site for help.', 'megaresponsive-lite' ),  
  286. ); 
  287.  
  288. do_action( 'tgmpa_register' ); 
  289.  
  290. /** After this point, the plugins should be registered and the configuration set. */ 
  291.  
  292. // Proceed only if we have plugins to handle. 
  293. if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { 
  294. return; 
  295.  
  296. // Set up the menu and notices if we still have outstanding actions. 
  297. if ( true !== $this->is_tgmpa_complete() ) { 
  298. // Sort the plugins. 
  299. array_multisort( $this->sort_order, SORT_ASC, $this->plugins ); 
  300.  
  301. add_action( 'admin_menu', array( $this, 'admin_menu' ) ); 
  302. add_action( 'admin_head', array( $this, 'dismiss' ) ); 
  303.  
  304. // Prevent the normal links from showing underneath a single install/update page. 
  305. add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) ); 
  306. add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) ); 
  307.  
  308. if ( $this->has_notices ) { 
  309. add_action( 'admin_notices', array( $this, 'notices' ) ); 
  310. add_action( 'admin_init', array( $this, 'admin_init' ), 1 ); 
  311. add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) ); 
  312.  
  313. add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 ); 
  314.  
  315. // Make sure things get reset on switch theme. 
  316. add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) ); 
  317.  
  318. if ( $this->has_notices ) { 
  319. add_action( 'switch_theme', array( $this, 'update_dismiss' ) ); 
  320.  
  321. // Setup the force activation hook. 
  322. if ( true === $this->has_forced_activation ) { 
  323. add_action( 'admin_init', array( $this, 'force_activation' ) ); 
  324.  
  325. // Setup the force deactivation hook. 
  326. if ( true === $this->has_forced_deactivation ) { 
  327. add_action( 'switch_theme', array( $this, 'force_deactivation' ) ); 
  328.  
  329. /** 
  330. * Prevent activation of plugins which don't meet the minimum version requirement from the 
  331. * WP native plugins page. 
  332. * @since 2.5.0 
  333. */ 
  334. public function add_plugin_action_link_filters() { 
  335. foreach ( $this->plugins as $slug => $plugin ) { 
  336. if ( false === $this->can_plugin_activate( $slug ) ) { 
  337. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 ); 
  338.  
  339. if ( true === $plugin['force_activation'] ) { 
  340. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 ); 
  341.  
  342. if ( false !== $this->does_plugin_require_update( $slug ) ) { 
  343. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 ); 
  344.  
  345. /** 
  346. * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the 
  347. * minimum version requirements. 
  348. * @since 2.5.0 
  349. * @param array $actions Action links. 
  350. * @return array 
  351. */ 
  352. public function filter_plugin_action_links_activate( $actions ) { 
  353. unset( $actions['activate'] ); 
  354.  
  355. return $actions; 
  356.  
  357. /** 
  358. * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate. 
  359. * @since 2.5.0 
  360. * @param array $actions Action links. 
  361. * @return array 
  362. */ 
  363. public function filter_plugin_action_links_deactivate( $actions ) { 
  364. unset( $actions['deactivate'] ); 
  365.  
  366. return $actions; 
  367.  
  368. /** 
  369. * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the 
  370. * minimum version requirements. 
  371. * @since 2.5.0 
  372. * @param array $actions Action links. 
  373. * @return array 
  374. */ 
  375. public function filter_plugin_action_links_update( $actions ) { 
  376. $actions['update'] = sprintf( 
  377. '<a href="%1$s" title="%2$s" class="edit">%3$s</a>',  
  378. esc_url( $this->get_tgmpa_status_url( 'update' ) ),  
  379. esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'megaresponsive-lite' ),  
  380. esc_html__( 'Update Required', 'megaresponsive-lite' ) 
  381. ); 
  382.  
  383. return $actions; 
  384.  
  385. /** 
  386. * Handles calls to show plugin information via links in the notices. 
  387. * We get the links in the admin notices to point to the TGMPA page, rather 
  388. * than the typical plugin-install.php file, so we can prepare everything 
  389. * beforehand. 
  390. * WP does not make it easy to show the plugin information in the thickbox - 
  391. * here we have to require a file that includes a function that does the 
  392. * main work of displaying it, enqueue some styles, set up some globals and 
  393. * finally call that function before exiting. 
  394. * Down right easy once you know how... 
  395. * Returns early if not the TGMPA page. 
  396. * @since 2.1.0 
  397. * @global string $tab Used as iframe div class names, helps with styling 
  398. * @global string $body_id Used as the iframe body ID, helps with styling 
  399. * @return null Returns early if not the TGMPA page. 
  400. */ 
  401. public function admin_init() { 
  402. if ( ! $this->is_tgmpa_page() ) { 
  403. return; 
  404.  
  405. if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) { 
  406. // Needed for install_plugin_information(). 
  407. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; 
  408.  
  409. wp_enqueue_style( 'plugin-install' ); 
  410.  
  411. global $tab, $body_id; 
  412. $body_id = 'plugin-information'; 
  413. // @codingStandardsIgnoreStart 
  414. $tab = 'plugin-information'; 
  415. // @codingStandardsIgnoreEnd 
  416.  
  417. install_plugin_information(); 
  418.  
  419. exit; 
  420.  
  421. /** 
  422. * Enqueue thickbox scripts/styles for plugin info. 
  423. * Thickbox is not automatically included on all admin pages, so we must 
  424. * manually enqueue it for those pages. 
  425. * Thickbox is only loaded if the user has not dismissed the admin 
  426. * notice or if there are any plugins left to install and activate. 
  427. * @since 2.1.0 
  428. */ 
  429. public function thickbox() { 
  430. if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) { 
  431. add_thickbox(); 
  432.  
  433. /** 
  434. * Adds submenu page if there are plugin actions to take. 
  435. * This method adds the submenu page letting users know that a required 
  436. * plugin needs to be installed. 
  437. * This page disappears once the plugin has been installed and activated. 
  438. * @since 1.0.0 
  439. * @see TGM_Plugin_Activation::init() 
  440. * @see TGM_Plugin_Activation::install_plugins_page() 
  441. * @return null Return early if user lacks capability to install a plugin. 
  442. */ 
  443. public function admin_menu() { 
  444. // Make sure privileges are correct to see the page. 
  445. if ( ! current_user_can( 'install_plugins' ) ) { 
  446. return; 
  447.  
  448. $args = apply_filters( 
  449. 'tgmpa_admin_menu_args',  
  450. array( 
  451. 'parent_slug' => $this->parent_slug, // Parent Menu slug. 
  452. 'page_title' => $this->strings['page_title'], // Page title. 
  453. 'menu_title' => $this->strings['menu_title'], // Menu title. 
  454. 'capability' => $this->capability, // Capability. 
  455. 'menu_slug' => $this->menu, // Menu slug. 
  456. 'function' => array( $this, 'install_plugins_page' ), // Callback. 
  457. ); 
  458.  
  459. $this->add_admin_menu( $args ); 
  460.  
  461. /** 
  462. * Add the menu item. 
  463. * @since 2.5.0 
  464. * @param array $args Menu item configuration. 
  465. */ 
  466. protected function add_admin_menu( array $args ) { 
  467. if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) { 
  468. _deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'megaresponsive-lite' ) ); 
  469.  
  470. if ( 'themes.php' === $this->parent_slug ) { 
  471. $this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); 
  472. } else { 
  473. $this->page_hook = call_user_func( 'add_theme_page', $args['parent_slug'], $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); 
  474.  
  475. /** 
  476. * Echoes plugin installation form. 
  477. * This method is the callback for the admin_menu method function. 
  478. * This displays the admin page and form area where the user can select to install and activate the plugin. 
  479. * Aborts early if we're processing a plugin installation action. 
  480. * @since 1.0.0 
  481. * @return null Aborts early if we're processing a plugin installation action. 
  482. */ 
  483. public function install_plugins_page() { 
  484. // Store new instance of plugin table in object. 
  485. $plugin_table = new TGMPA_List_Table; 
  486.  
  487. // Return early if processing a plugin installation action. 
  488. if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) { 
  489. return; 
  490.  
  491. // Force refresh of available plugin information so we'll know about manual updates/deletes. 
  492. wp_clean_plugins_cache( false ); 
  493.  
  494. ?> 
  495. <div class="tgmpa wrap"> 
  496. <h2><?php echo esc_html( get_admin_page_title() ); ?></h2> 
  497. <?php $plugin_table->prepare_items(); ?> 
  498.  
  499. <?php 
  500. if ( ! empty( $this->message ) && is_string( $this->message ) ) { 
  501. echo wp_kses_post( $this->message ); 
  502. ?> 
  503. <?php $plugin_table->views(); ?> 
  504.  
  505. <form id="tgmpa-plugins" action="" method="post"> 
  506. <input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" /> 
  507. <input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" /> 
  508. <?php $plugin_table->display(); ?> 
  509. </form> 
  510. </div> 
  511. <?php 
  512.  
  513. /** 
  514. * Installs, updates or activates a plugin depending on the action link clicked by the user. 
  515. * Checks the $_GET variable to see which actions have been 
  516. * passed and responds with the appropriate method. 
  517. * Uses WP_Filesystem to process and handle the plugin installation 
  518. * method. 
  519. * @since 1.0.0 
  520. * @uses WP_Filesystem 
  521. * @uses WP_Error 
  522. * @uses WP_Upgrader 
  523. * @uses Plugin_Upgrader 
  524. * @uses Plugin_Installer_Skin 
  525. * @uses Plugin_Upgrader_Skin 
  526. * @return boolean True on success, false on failure. 
  527. */ 
  528. protected function do_plugin_install() { 
  529. if ( empty( $_GET['plugin'] ) ) { 
  530. return false; 
  531.  
  532. // All plugin information will be stored in an array for processing. 
  533. $slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) ); 
  534.  
  535. if ( ! isset( $this->plugins[ $slug ] ) ) { 
  536. return false; 
  537.  
  538. // Was an install or upgrade action link clicked? 
  539. if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) { 
  540.  
  541. $install_type = 'install'; 
  542. if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) { 
  543. $install_type = 'update'; 
  544.  
  545. check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' ); 
  546.  
  547. // Pass necessary information via URL if WP_Filesystem is needed. 
  548. $url = wp_nonce_url( 
  549. add_query_arg( 
  550. array( 
  551. 'plugin' => urlencode( $slug ),  
  552. 'tgmpa-' . $install_type => $install_type . '-plugin',  
  553. ),  
  554. $this->get_tgmpa_url() 
  555. ),  
  556. 'tgmpa-' . $install_type,  
  557. 'tgmpa-nonce' 
  558. ); 
  559.  
  560. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary. 
  561.  
  562. if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) { 
  563. return true; 
  564.  
  565. if ( ! WP_Filesystem( $creds ) ) { 
  566. request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem. 
  567. return true; 
  568.  
  569. /** If we arrive here, we have the filesystem. */ 
  570.  
  571. // Prep variables for Plugin_Installer_Skin class. 
  572. $extra = array(); 
  573. $extra['slug'] = $slug; // Needed for potentially renaming of directory name. 
  574. $source = $this->get_download_url( $slug ); 
  575. $api = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null; 
  576. $api = ( false !== $api ) ? $api : null; 
  577.  
  578. $url = add_query_arg( 
  579. array( 
  580. 'action' => $install_type . '-plugin',  
  581. 'plugin' => urlencode( $slug ),  
  582. ),  
  583. 'update.php' 
  584. ); 
  585.  
  586. if ( ! class_exists( 'Plugin_Upgrader', false ) ) { 
  587. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
  588.  
  589. $skin_args = array( 
  590. 'type' => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',  
  591. 'title' => sprintf( $this->strings['installing'], $this->plugins[ $slug ]['name'] ),  
  592. 'url' => esc_url_raw( $url ),  
  593. 'nonce' => $install_type . '-plugin_' . $slug,  
  594. 'plugin' => '',  
  595. 'api' => $api,  
  596. 'extra' => $extra,  
  597. ); 
  598.  
  599. if ( 'update' === $install_type ) { 
  600. $skin_args['plugin'] = $this->plugins[ $slug ]['file_path']; 
  601. $skin = new Plugin_Upgrader_Skin( $skin_args ); 
  602. } else { 
  603. $skin = new Plugin_Installer_Skin( $skin_args ); 
  604.  
  605. // Create a new instance of Plugin_Upgrader. 
  606. $upgrader = new Plugin_Upgrader( $skin ); 
  607.  
  608. // Perform the action and install the plugin from the $source urldecode(). 
  609. add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 ); 
  610.  
  611. if ( 'update' === $install_type ) { 
  612. // Inject our info into the update transient. 
  613. $to_inject = array( $slug => $this->plugins[ $slug ] ); 
  614. $to_inject[ $slug ]['source'] = $source; 
  615. $this->inject_update_info( $to_inject ); 
  616.  
  617. $upgrader->upgrade( $this->plugins[ $slug ]['file_path'] ); 
  618. } else { 
  619. $upgrader->install( $source ); 
  620.  
  621. remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 ); 
  622.  
  623. // Make sure we have the correct file path now the plugin is installed/updated. 
  624. $this->populate_file_path( $slug ); 
  625.  
  626. // Only activate plugins if the config option is set to true and the plugin isn't 
  627. // already active (upgrade). 
  628. if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) { 
  629. $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method. 
  630. if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { 
  631. return true; // Finish execution of the function early as we encountered an error. 
  632.  
  633. $this->show_tgmpa_version(); 
  634.  
  635. // Display message based on if all plugins are now active or not. 
  636. if ( $this->is_tgmpa_complete() ) { 
  637. echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'megaresponsive-lite' ) . '</a>' ), '</p>'; 
  638. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; 
  639. } else { 
  640. echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; 
  641.  
  642. return true; 
  643. } elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) { 
  644. // Activate action link was clicked. 
  645. check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' ); 
  646.  
  647. if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) { 
  648. return true; // Finish execution of the function early as we encountered an error. 
  649.  
  650. return false; 
  651.  
  652. /** 
  653. * Inject information into the 'update_plugins' site transient as WP checks that before running an update. 
  654. * @since 2.5.0 
  655. * @param array $plugins The plugin information for the plugins which are to be updated. 
  656. */ 
  657. public function inject_update_info( $plugins ) { 
  658. $repo_updates = get_site_transient( 'update_plugins' ); 
  659.  
  660. if ( ! is_object( $repo_updates ) ) { 
  661. $repo_updates = new stdClass; 
  662.  
  663. foreach ( $plugins as $slug => $plugin ) { 
  664. $file_path = $plugin['file_path']; 
  665.  
  666. if ( empty( $repo_updates->response[ $file_path ] ) ) { 
  667. $repo_updates->response[ $file_path ] = new stdClass; 
  668.  
  669. // We only really need to set package, but let's do all we can in case WP changes something. 
  670. $repo_updates->response[ $file_path ]->slug = $slug; 
  671. $repo_updates->response[ $file_path ]->plugin = $file_path; 
  672. $repo_updates->response[ $file_path ]->new_version = $plugin['version']; 
  673. $repo_updates->response[ $file_path ]->package = $plugin['source']; 
  674. if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) { 
  675. $repo_updates->response[ $file_path ]->url = $plugin['external_url']; 
  676.  
  677. set_site_transient( 'update_plugins', $repo_updates ); 
  678.  
  679. /** 
  680. * Adjust the plugin directory name if necessary. 
  681. * The final destination directory of a plugin is based on the subdirectory name found in the 
  682. * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this 
  683. * subdirectory name is not the same as the expected slug and the plugin will not be recognized 
  684. * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to 
  685. * the expected plugin slug. 
  686. * @since 2.5.0 
  687. * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/. 
  688. * @param string $remote_source Path to upgrade/zip-file-name.tmp. 
  689. * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin. 
  690. * @return string $source 
  691. */ 
  692. public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { 
  693. if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) { 
  694. return $source; 
  695.  
  696. // Check for single file plugins. 
  697. $source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) ); 
  698. if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) { 
  699. return $source; 
  700.  
  701. // Multi-file plugin, let's see if the directory is correctly named. 
  702. $desired_slug = ''; 
  703.  
  704. // Figure out what the slug is supposed to be. 
  705. if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) { 
  706. $desired_slug = $upgrader->skin->options['extra']['slug']; 
  707. } else { 
  708. // Bulk installer contains less info, so fall back on the info registered here. 
  709. foreach ( $this->plugins as $slug => $plugin ) { 
  710. if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) { 
  711. $desired_slug = $slug; 
  712. break; 
  713. unset( $slug, $plugin ); 
  714.  
  715. if ( ! empty( $desired_slug ) ) { 
  716. $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) ); 
  717.  
  718. if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) { 
  719. $from = untrailingslashit( $source ); 
  720. $to = trailingslashit( $remote_source ) . $desired_slug; 
  721.  
  722. if ( true === $GLOBALS['wp_filesystem']->move( $from, $to ) ) { 
  723. return trailingslashit( $to ); 
  724. } else { 
  725. return new WP_Error( 'rename_failed', esc_html__( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'megaresponsive-lite' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'megaresponsive-lite' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) ); 
  726. } elseif ( empty( $subdir_name ) ) { 
  727. return new WP_Error( 'packaged_wrong', esc_html__( 'The remote plugin package consists of more than one file, but the files are not packaged in a folder.', 'megaresponsive-lite' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'megaresponsive-lite' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) ); 
  728.  
  729. return $source; 
  730.  
  731. /** 
  732. * Activate a single plugin and send feedback about the result to the screen. 
  733. * @since 2.5.0 
  734. * @param string $file_path Path within wp-plugins/ to main plugin file. 
  735. * @param string $slug Plugin slug. 
  736. * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false. 
  737. * This determines the styling of the output messages. 
  738. * @return bool False if an error was encountered, true otherwise. 
  739. */ 
  740. protected function activate_single_plugin( $file_path, $slug, $automatic = false ) { 
  741. if ( $this->can_plugin_activate( $slug ) ) { 
  742. $activate = activate_plugin( $file_path ); 
  743.  
  744. if ( is_wp_error( $activate ) ) { 
  745. echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',  
  746. '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; 
  747.  
  748. return false; // End it here if there is an error with activation. 
  749. } else { 
  750. if ( ! $automatic ) { 
  751. // Make sure message doesn't display again if bulk activation is performed 
  752. // immediately after a single activation. 
  753. if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK. 
  754. echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>'; 
  755. } else { 
  756. // Simpler message layout for use on the plugin install page. 
  757. echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>'; 
  758. } elseif ( $this->is_plugin_active( $slug ) ) { 
  759. // No simpler message format provided as this message should never be encountered 
  760. // on the plugin install page. 
  761. echo '<div id="message" class="error"><p>',  
  762. sprintf( 
  763. esc_html( $this->strings['plugin_already_active'] ),  
  764. '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>' 
  765. ),  
  766. '</p></div>'; 
  767. } elseif ( $this->does_plugin_require_update( $slug ) ) { 
  768. if ( ! $automatic ) { 
  769. // Make sure message doesn't display again if bulk activation is performed 
  770. // immediately after a single activation. 
  771. if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK. 
  772. echo '<div id="message" class="error"><p>',  
  773. sprintf( 
  774. esc_html( $this->strings['plugin_needs_higher_version'] ),  
  775. '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>' 
  776. ),  
  777. '</p></div>'; 
  778. } else { 
  779. // Simpler message layout for use on the plugin install page. 
  780. echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>'; 
  781.  
  782. return true; 
  783.  
  784. /** 
  785. * Echoes required plugin notice. 
  786. * Outputs a message telling users that a specific plugin is required for 
  787. * their theme. If appropriate, it includes a link to the form page where 
  788. * users can install and activate the plugin. 
  789. * Returns early if we're on the Install page. 
  790. * @since 1.0.0 
  791. * @global object $current_screen 
  792. * @return null Returns early if we're on the Install page. 
  793. */ 
  794. public function notices() { 
  795. // Remove nag on the install page / Return early if the nag message has been dismissed. 
  796. if ( $this->is_tgmpa_page() || get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) { 
  797. return; 
  798.  
  799. // Store for the plugin slugs by message type. 
  800. $message = array(); 
  801.  
  802. // Initialize counters used to determine plurality of action link texts. 
  803. $install_link_count = 0; 
  804. $update_link_count = 0; 
  805. $activate_link_count = 0; 
  806.  
  807. foreach ( $this->plugins as $slug => $plugin ) { 
  808. if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) { 
  809. continue; 
  810.  
  811. if ( ! $this->is_plugin_installed( $slug ) ) { 
  812. if ( current_user_can( 'install_plugins' ) ) { 
  813. $install_link_count++; 
  814.  
  815. if ( true === $plugin['required'] ) { 
  816. $message['notice_can_install_required'][] = $slug; 
  817. } else { 
  818. $message['notice_can_install_recommended'][] = $slug; 
  819. } else { 
  820. // Need higher privileges to install the plugin. 
  821. $message['notice_cannot_install'][] = $slug; 
  822. } else { 
  823. if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) { 
  824. if ( current_user_can( 'activate_plugins' ) ) { 
  825. $activate_link_count++; 
  826.  
  827. if ( true === $plugin['required'] ) { 
  828. $message['notice_can_activate_required'][] = $slug; 
  829. } else { 
  830. $message['notice_can_activate_recommended'][] = $slug; 
  831. } else { 
  832. // Need higher privileges to activate the plugin. 
  833. $message['notice_cannot_activate'][] = $slug; 
  834.  
  835. if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) { 
  836.  
  837. if ( current_user_can( 'install_plugins' ) ) { 
  838. $update_link_count++; 
  839.  
  840. if ( $this->does_plugin_require_update( $slug ) ) { 
  841. $message['notice_ask_to_update'][] = $slug; 
  842. } elseif ( false !== $this->does_plugin_have_update( $slug ) ) { 
  843. $message['notice_ask_to_update_maybe'][] = $slug; 
  844. } else { 
  845. // Need higher privileges to update the plugin. 
  846. $message['notice_cannot_update'][] = $slug; 
  847. unset( $slug, $plugin ); 
  848.  
  849. // If we have notices to display, we move forward. 
  850. if ( ! empty( $message ) ) { 
  851. krsort( $message ); // Sort messages. 
  852. $rendered = ''; 
  853.  
  854. // As add_settings_error() wraps the final message in a <p> and as the final message can't be 
  855. // filtered, using <p>'s in our html would render invalid html output. 
  856. $line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n"; 
  857.  
  858. // If dismissable is false and a message is set, output it now. 
  859. if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) { 
  860. $rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) ); 
  861.  
  862. // Render the individual message lines for the notice. 
  863. foreach ( $message as $type => $plugin_group ) { 
  864. $linked_plugins = array(); 
  865.  
  866. // Get the external info link for a plugin if one is available. 
  867. foreach ( $plugin_group as $plugin_slug ) { 
  868. $linked_plugins[] = $this->get_info_link( $plugin_slug ); 
  869. unset( $plugin_slug ); 
  870.  
  871. $count = count( $plugin_group ); 
  872. $linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins ); 
  873. $last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability. 
  874. $imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'megaresponsive-lite' ) . ' ' . $last_plugin ); 
  875.  
  876. $rendered .= sprintf( 
  877. $line_template,  
  878. sprintf( 
  879. translate_nooped_plural( $this->strings[ $type ], $count, 'megaresponsive-lite' ),  
  880. $imploded,  
  881. $count 
  882. ); 
  883.  
  884. if ( 0 === strpos( $type, 'notice_cannot' ) ) { 
  885. $rendered .= $this->strings['contact_admin']; 
  886. unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded ); 
  887.  
  888. // Setup action links. 
  889. $action_links = array( 
  890. 'install' => '',  
  891. 'update' => '',  
  892. 'activate' => '',  
  893. 'dismiss' => $this->dismissable ? '<a href="' . esc_url( add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ) ) . '" class="dismiss-notice" target="_parent">' . esc_html( $this->strings['dismiss'] ) . '</a>' : '',  
  894. ); 
  895.  
  896. $link_template = '<a href="%2$s">%1$s</a>'; 
  897.  
  898. if ( current_user_can( 'install_plugins' ) ) { 
  899. if ( $install_link_count > 0 ) { 
  900. $action_links['install'] = sprintf( 
  901. $link_template,  
  902. translate_nooped_plural( $this->strings['install_link'], $install_link_count, 'megaresponsive-lite' ),  
  903. esc_url( $this->get_tgmpa_status_url( 'install' ) ) 
  904. ); 
  905. if ( $update_link_count > 0 ) { 
  906. $action_links['update'] = sprintf( 
  907. $link_template,  
  908. translate_nooped_plural( $this->strings['update_link'], $update_link_count, 'megaresponsive-lite' ),  
  909. esc_url( $this->get_tgmpa_status_url( 'update' ) ) 
  910. ); 
  911.  
  912. if ( current_user_can( 'activate_plugins' ) && $activate_link_count > 0 ) { 
  913. $action_links['activate'] = sprintf( 
  914. $link_template,  
  915. translate_nooped_plural( $this->strings['activate_link'], $activate_link_count, 'megaresponsive-lite' ),  
  916. esc_url( $this->get_tgmpa_status_url( 'activate' ) ) 
  917. ); 
  918.  
  919. $action_links = apply_filters( 'tgmpa_notice_action_links', $action_links ); 
  920.  
  921. $action_links = array_filter( (array) $action_links ); // Remove any empty array items. 
  922.  
  923. if ( ! empty( $action_links ) && is_array( $action_links ) ) { 
  924. $action_links = sprintf( $line_template, implode( ' | ', $action_links ) ); 
  925. $rendered .= apply_filters( 'tgmpa_notice_rendered_action_links', $action_links ); 
  926.  
  927. // Register the nag messages and prepare them to be processed. 
  928. if ( ! empty( $this->strings['nag_type'] ) ) { 
  929. add_settings_error( 'megaresponsive-lite', 'megaresponsive-lite', $rendered, sanitize_html_class( strtolower( $this->strings['nag_type'] ) ) ); 
  930. } else { 
  931. $nag_class = version_compare( $this->wp_version, '3.8', '<' ) ? 'updated' : 'update-nag'; 
  932. add_settings_error( 'megaresponsive-lite', 'megaresponsive-lite', $rendered, $nag_class ); 
  933.  
  934. // Admin options pages already output settings_errors, so this is to avoid duplication. 
  935. if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) { 
  936. $this->display_settings_errors(); 
  937.  
  938. /** 
  939. * Display settings errors and remove those which have been displayed to avoid duplicate messages showing 
  940. * @since 2.5.0 
  941. */ 
  942. protected function display_settings_errors() { 
  943. global $wp_settings_errors; 
  944.  
  945. settings_errors( 'megaresponsive-lite' ); 
  946.  
  947. foreach ( (array) $wp_settings_errors as $key => $details ) { 
  948. if ( 'megaresponsive-lite' === $details['setting'] ) { 
  949. unset( $wp_settings_errors[ $key ] ); 
  950. break; 
  951.  
  952. /** 
  953. * Add dismissable admin notices. 
  954. * Appends a link to the admin nag messages. If clicked, the admin notice disappears and no longer is visible to users. 
  955. * @since 2.1.0 
  956. */ 
  957. public function dismiss() { 
  958. if ( isset( $_GET['tgmpa-dismiss'] ) ) { 
  959. update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 ); 
  960.  
  961. /** 
  962. * Add individual plugin to our collection of plugins. 
  963. * If the required keys are not set or the plugin has already 
  964. * been registered, the plugin is not added. 
  965. * @since 2.0.0 
  966. * @param array|null $plugin Array of plugin arguments or null if invalid argument. 
  967. * @return null Return early if incorrect argument. 
  968. */ 
  969. public function register( $plugin ) { 
  970. if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) { 
  971. return; 
  972.  
  973. if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) { 
  974. return; 
  975.  
  976. $defaults = array( 
  977. 'name' => '', // String 
  978. 'slug' => '', // String 
  979. 'source' => 'repo', // String 
  980. 'required' => false, // Boolean 
  981. 'version' => '', // String 
  982. 'force_activation' => false, // Boolean 
  983. 'force_deactivation' => false, // Boolean 
  984. 'external_url' => '', // String 
  985. 'is_callable' => '', // String|Array. 
  986. ); 
  987.  
  988. // Prepare the received data. 
  989. $plugin = wp_parse_args( $plugin, $defaults ); 
  990.  
  991. // Standardize the received slug. 
  992. $plugin['slug'] = $this->sanitize_key( $plugin['slug'] ); 
  993.  
  994. // Forgive users for using string versions of booleans or floats for version number. 
  995. $plugin['version'] = (string) $plugin['version']; 
  996. $plugin['source'] = empty( $plugin['source'] ) ? 'repo' : $plugin['source']; 
  997. $plugin['required'] = TGMPA_Utils::validate_bool( $plugin['required'] ); 
  998. $plugin['force_activation'] = TGMPA_Utils::validate_bool( $plugin['force_activation'] ); 
  999. $plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] ); 
  1000.  
  1001. // Enrich the received data. 
  1002. $plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] ); 
  1003. $plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] ); 
  1004.  
  1005. // Set the class properties. 
  1006. $this->plugins[ $plugin['slug'] ] = $plugin; 
  1007. $this->sort_order[ $plugin['slug'] ] = $plugin['name']; 
  1008.  
  1009. // Should we add the force activation hook ? 
  1010. if ( true === $plugin['force_activation'] ) { 
  1011. $this->has_forced_activation = true; 
  1012.  
  1013. // Should we add the force deactivation hook ? 
  1014. if ( true === $plugin['force_deactivation'] ) { 
  1015. $this->has_forced_deactivation = true; 
  1016.  
  1017. /** 
  1018. * Determine what type of source the plugin comes from. 
  1019. * @since 2.5.0 
  1020. * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path 
  1021. * (= bundled) or an external URL. 
  1022. * @return string 'repo', 'external', or 'bundled' 
  1023. */ 
  1024. protected function get_plugin_source_type( $source ) { 
  1025. if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) { 
  1026. return 'repo'; 
  1027. } elseif ( preg_match( self::IS_URL_REGEX, $source ) ) { 
  1028. return 'external'; 
  1029. } else { 
  1030. return 'bundled'; 
  1031.  
  1032. /** 
  1033. * Sanitizes a string key. 
  1034. * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are* 
  1035. * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase 
  1036. * characters in the plugin directory path/slug. Silly them. 
  1037. * @see https://developer.wordpress.org/reference/hooks/sanitize_key/ 
  1038. * @since 2.5.0 
  1039. * @param string $key String key. 
  1040. * @return string Sanitized key 
  1041. */ 
  1042. public function sanitize_key( $key ) { 
  1043. $raw_key = $key; 
  1044. $key = preg_replace( '`[^A-Za-z0-9_-]`', '', $key ); 
  1045.  
  1046. /** 
  1047. * Filter a sanitized key string. 
  1048. * @since 3.0.0 
  1049. * @param string $key Sanitized key. 
  1050. * @param string $raw_key The key prior to sanitization. 
  1051. */ 
  1052. return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key ); 
  1053.  
  1054. /** 
  1055. * Amend default configuration settings. 
  1056. * @since 2.0.0 
  1057. * @param array $config Array of config options to pass as class properties. 
  1058. */ 
  1059. public function config( $config ) { 
  1060. $keys = array( 
  1061. 'id',  
  1062. 'default_path',  
  1063. 'has_notices',  
  1064. 'dismissable',  
  1065. 'dismiss_msg',  
  1066. 'menu',  
  1067. 'parent_slug',  
  1068. 'capability',  
  1069. 'is_automatic',  
  1070. 'message',  
  1071. 'strings',  
  1072. ); 
  1073.  
  1074. foreach ( $keys as $key ) { 
  1075. if ( isset( $config[ $key ] ) ) { 
  1076. if ( is_array( $config[ $key ] ) ) { 
  1077. $this->$key = array_merge( $this->$key, $config[ $key ] ); 
  1078. } else { 
  1079. $this->$key = $config[ $key ]; 
  1080.  
  1081. /** 
  1082. * Amend action link after plugin installation. 
  1083. * @since 2.0.0 
  1084. * @param array $install_actions Existing array of actions. 
  1085. * @return array Amended array of actions. 
  1086. */ 
  1087. public function actions( $install_actions ) { 
  1088. // Remove action links on the TGMPA install page. 
  1089. if ( $this->is_tgmpa_page() ) { 
  1090. return false; 
  1091.  
  1092. return $install_actions; 
  1093.  
  1094. /** 
  1095. * Flushes the plugins cache on theme switch to prevent stale entries 
  1096. * from remaining in the plugin table. 
  1097. * @since 2.4.0 
  1098. * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache. 
  1099. * Parameter added in v2.5.0. 
  1100. */ 
  1101. public function flush_plugins_cache( $clear_update_cache = true ) { 
  1102. wp_clean_plugins_cache( $clear_update_cache ); 
  1103.  
  1104. /** 
  1105. * Set file_path key for each installed plugin. 
  1106. * @since 2.1.0 
  1107. * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin. 
  1108. * Parameter added in v2.5.0. 
  1109. */ 
  1110. public function populate_file_path( $plugin_slug = '' ) { 
  1111. if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) { 
  1112. $this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug ); 
  1113. } else { 
  1114. // Add file_path key for all plugins. 
  1115. foreach ( $this->plugins as $slug => $values ) { 
  1116. $this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug ); 
  1117.  
  1118. /** 
  1119. * Helper function to extract the file path of the plugin file from the 
  1120. * plugin slug, if the plugin is installed. 
  1121. * @since 2.0.0 
  1122. * @param string $slug Plugin slug (typically folder name) as provided by the developer. 
  1123. * @return string Either file path for plugin if installed, or just the plugin slug. 
  1124. */ 
  1125. protected function _get_plugin_basename_from_slug( $slug ) { 
  1126. $keys = array_keys( $this->get_plugins() ); 
  1127.  
  1128. foreach ( $keys as $key ) { 
  1129. if ( preg_match( '|^' . $slug . '/|', $key ) ) { 
  1130. return $key; 
  1131.  
  1132. return $slug; 
  1133.  
  1134. /** 
  1135. * Retrieve plugin data, given the plugin name. 
  1136. * Loops through the registered plugins looking for $name. If it finds it,  
  1137. * it returns the $data from that plugin. Otherwise, returns false. 
  1138. * @since 2.1.0 
  1139. * @param string $name Name of the plugin, as it was registered. 
  1140. * @param string $data Optional. Array key of plugin data to return. Default is slug. 
  1141. * @return string|boolean Plugin slug if found, false otherwise. 
  1142. */ 
  1143. public function _get_plugin_data_from_name( $name, $data = 'slug' ) { 
  1144. foreach ( $this->plugins as $values ) { 
  1145. if ( $name === $values['name'] && isset( $values[ $data ] ) ) { 
  1146. return $values[ $data ]; 
  1147.  
  1148. return false; 
  1149.  
  1150. /** 
  1151. * Retrieve the download URL for a package. 
  1152. * @since 2.5.0 
  1153. * @param string $slug Plugin slug. 
  1154. * @return string Plugin download URL or path to local file or empty string if undetermined. 
  1155. */ 
  1156. public function get_download_url( $slug ) { 
  1157. $dl_source = ''; 
  1158.  
  1159. switch ( $this->plugins[ $slug ]['source_type'] ) { 
  1160. case 'repo': 
  1161. return $this->get_wp_repo_download_url( $slug ); 
  1162. case 'external': 
  1163. return $this->plugins[ $slug ]['source']; 
  1164. case 'bundled': 
  1165. return $this->default_path . $this->plugins[ $slug ]['source']; 
  1166.  
  1167. return $dl_source; // Should never happen. 
  1168.  
  1169. /** 
  1170. * Retrieve the download URL for a WP repo package. 
  1171. * @since 2.5.0 
  1172. * @param string $slug Plugin slug. 
  1173. * @return string Plugin download URL. 
  1174. */ 
  1175. protected function get_wp_repo_download_url( $slug ) { 
  1176. $source = ''; 
  1177. $api = $this->get_plugins_api( $slug ); 
  1178.  
  1179. if ( false !== $api && isset( $api->download_link ) ) { 
  1180. $source = $api->download_link; 
  1181.  
  1182. return $source; 
  1183.  
  1184. /** 
  1185. * Try to grab information from WordPress API. 
  1186. * @since 2.5.0 
  1187. * @param string $slug Plugin slug. 
  1188. * @return object Plugins_api response object on success, WP_Error on failure. 
  1189. */ 
  1190. protected function get_plugins_api( $slug ) { 
  1191. static $api = array(); // Cache received responses. 
  1192.  
  1193. if ( ! isset( $api[ $slug ] ) ) { 
  1194. if ( ! function_exists( 'plugins_api' ) ) { 
  1195. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; 
  1196.  
  1197. $response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); 
  1198.  
  1199. $api[ $slug ] = false; 
  1200.  
  1201. if ( is_wp_error( $response ) ) { 
  1202. wp_die( esc_html( $this->strings['oops'] ) ); 
  1203. } else { 
  1204. $api[ $slug ] = $response; 
  1205.  
  1206. return $api[ $slug ]; 
  1207.  
  1208. /** 
  1209. * Retrieve a link to a plugin information page. 
  1210. * @since 2.5.0 
  1211. * @param string $slug Plugin slug. 
  1212. * @return string Fully formed html link to a plugin information page if available 
  1213. * or the plugin name if not. 
  1214. */ 
  1215. public function get_info_link( $slug ) { 
  1216. if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) { 
  1217. $link = sprintf( 
  1218. '<a href="%1$s" target="_blank">%2$s</a>',  
  1219. esc_url( $this->plugins[ $slug ]['external_url'] ),  
  1220. esc_html( $this->plugins[ $slug ]['name'] ) 
  1221. ); 
  1222. } elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) { 
  1223. $url = add_query_arg( 
  1224. array( 
  1225. 'tab' => 'plugin-information',  
  1226. 'plugin' => urlencode( $slug ),  
  1227. 'TB_iframe' => 'true',  
  1228. 'width' => '640',  
  1229. 'height' => '500',  
  1230. ),  
  1231. self_admin_url( 'plugin-install.php' ) 
  1232. ); 
  1233.  
  1234. $link = sprintf( 
  1235. '<a href="%1$s" class="thickbox">%2$s</a>',  
  1236. esc_url( $url ),  
  1237. esc_html( $this->plugins[ $slug ]['name'] ) 
  1238. ); 
  1239. } else { 
  1240. $link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink. 
  1241.  
  1242. return $link; 
  1243.  
  1244. /** 
  1245. * Determine if we're on the TGMPA Install page. 
  1246. * @since 2.1.0 
  1247. * @return boolean True when on the TGMPA page, false otherwise. 
  1248. */ 
  1249. protected function is_tgmpa_page() { 
  1250. return isset( $_GET['page'] ) && $this->menu === $_GET['page']; 
  1251.  
  1252. /** 
  1253. * Retrieve the URL to the TGMPA Install page. 
  1254. * I.e. depending on the config settings passed something along the lines of: 
  1255. * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins 
  1256. * @since 2.5.0 
  1257. * @return string Properly encoded URL (not escaped). 
  1258. */ 
  1259. public function get_tgmpa_url() { 
  1260. static $url; 
  1261.  
  1262. if ( ! isset( $url ) ) { 
  1263. $parent = $this->parent_slug; 
  1264. if ( false === strpos( $parent, '.php' ) ) { 
  1265. $parent = 'admin.php'; 
  1266. $url = add_query_arg( 
  1267. array( 
  1268. 'page' => urlencode( $this->menu ),  
  1269. ),  
  1270. self_admin_url( $parent ) 
  1271. ); 
  1272.  
  1273. return $url; 
  1274.  
  1275. /** 
  1276. * Retrieve the URL to the TGMPA Install page for a specific plugin status (view). 
  1277. * I.e. depending on the config settings passed something along the lines of: 
  1278. * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install 
  1279. * @since 2.5.0 
  1280. * @param string $status Plugin status - either 'install', 'update' or 'activate'. 
  1281. * @return string Properly encoded URL (not escaped). 
  1282. */ 
  1283. public function get_tgmpa_status_url( $status ) { 
  1284. return add_query_arg( 
  1285. array( 
  1286. 'plugin_status' => urlencode( $status ),  
  1287. ),  
  1288. $this->get_tgmpa_url() 
  1289. ); 
  1290.  
  1291. /** 
  1292. * Determine whether there are open actions for plugins registered with TGMPA. 
  1293. * @since 2.5.0 
  1294. * @return bool True if complete, i.e. no outstanding actions. False otherwise. 
  1295. */ 
  1296. public function is_tgmpa_complete() { 
  1297. $complete = true; 
  1298. foreach ( $this->plugins as $slug => $plugin ) { 
  1299. if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) { 
  1300. $complete = false; 
  1301. break; 
  1302.  
  1303. return $complete; 
  1304.  
  1305. /** 
  1306. * Check if a plugin is installed. Does not take must-use plugins into account. 
  1307. * @since 2.5.0 
  1308. * @param string $slug Plugin slug. 
  1309. * @return bool True if installed, false otherwise. 
  1310. */ 
  1311. public function is_plugin_installed( $slug ) { 
  1312. $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached). 
  1313.  
  1314. return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) ); 
  1315.  
  1316. /** 
  1317. * Check if a plugin is active. 
  1318. * @since 2.5.0 
  1319. * @param string $slug Plugin slug. 
  1320. * @return bool True if active, false otherwise. 
  1321. */ 
  1322. public function is_plugin_active( $slug ) { 
  1323. return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) ); 
  1324.  
  1325. /** 
  1326. * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required 
  1327. * available, check whether the current install meets them. 
  1328. * @since 2.5.0 
  1329. * @param string $slug Plugin slug. 
  1330. * @return bool True if OK to update, false otherwise. 
  1331. */ 
  1332. public function can_plugin_update( $slug ) { 
  1333. // We currently can't get reliable info on non-WP-repo plugins - issue #380. 
  1334. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { 
  1335. return true; 
  1336.  
  1337. $api = $this->get_plugins_api( $slug ); 
  1338.  
  1339. if ( false !== $api && isset( $api->requires ) ) { 
  1340. return version_compare( $GLOBALS['wp_version'], $api->requires, '>=' ); 
  1341.  
  1342. // No usable info received from the plugins API, presume we can update. 
  1343. return true; 
  1344.  
  1345. /** 
  1346. * Check if a plugin can be activated, i.e. is not currently active and meets the minimum 
  1347. * plugin version requirements set in TGMPA (if any). 
  1348. * @since 2.5.0 
  1349. * @param string $slug Plugin slug. 
  1350. * @return bool True if OK to activate, false otherwise. 
  1351. */ 
  1352. public function can_plugin_activate( $slug ) { 
  1353. return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) ); 
  1354.  
  1355. /** 
  1356. * Retrieve the version number of an installed plugin. 
  1357. * @since 2.5.0 
  1358. * @param string $slug Plugin slug. 
  1359. * @return string Version number as string or an empty string if the plugin is not installed 
  1360. * or version unknown (plugins which don't comply with the plugin header standard). 
  1361. */ 
  1362. public function get_installed_version( $slug ) { 
  1363. $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached). 
  1364.  
  1365. if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) { 
  1366. return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version']; 
  1367.  
  1368. return ''; 
  1369.  
  1370. /** 
  1371. * Check whether a plugin complies with the minimum version requirements. 
  1372. * @since 2.5.0 
  1373. * @param string $slug Plugin slug. 
  1374. * @return bool True when a plugin needs to be updated, otherwise false. 
  1375. */ 
  1376. public function does_plugin_require_update( $slug ) { 
  1377. $installed_version = $this->get_installed_version( $slug ); 
  1378. $minimum_version = $this->plugins[ $slug ]['version']; 
  1379.  
  1380. return version_compare( $minimum_version, $installed_version, '>' ); 
  1381.  
  1382. /** 
  1383. * Check whether there is an update available for a plugin. 
  1384. * @since 2.5.0 
  1385. * @param string $slug Plugin slug. 
  1386. * @return false|string Version number string of the available update or false if no update available. 
  1387. */ 
  1388. public function does_plugin_have_update( $slug ) { 
  1389. // Presume bundled and external plugins will point to a package which meets the minimum required version. 
  1390. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { 
  1391. if ( $this->does_plugin_require_update( $slug ) ) { 
  1392. return $this->plugins[ $slug ]['version']; 
  1393.  
  1394. return false; 
  1395.  
  1396. $repo_updates = get_site_transient( 'update_plugins' ); 
  1397.  
  1398. if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) { 
  1399. return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version; 
  1400.  
  1401. return false; 
  1402.  
  1403. /** 
  1404. * Retrieve potential upgrade notice for a plugin. 
  1405. * @since 2.5.0 
  1406. * @param string $slug Plugin slug. 
  1407. * @return string The upgrade notice or an empty string if no message was available or provided. 
  1408. */ 
  1409. public function get_upgrade_notice( $slug ) { 
  1410. // We currently can't get reliable info on non-WP-repo plugins - issue #380. 
  1411. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { 
  1412. return ''; 
  1413.  
  1414. $repo_updates = get_site_transient( 'update_plugins' ); 
  1415.  
  1416. if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) { 
  1417. return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice; 
  1418.  
  1419. return ''; 
  1420.  
  1421. /** 
  1422. * Wrapper around the core WP get_plugins function, making sure it's actually available. 
  1423. * @since 2.5.0 
  1424. * @param string $plugin_folder Optional. Relative path to single plugin folder. 
  1425. * @return array Array of installed plugins with plugin information. 
  1426. */ 
  1427. public function get_plugins( $plugin_folder = '' ) { 
  1428. if ( ! function_exists( 'get_plugins' ) ) { 
  1429. require_once ABSPATH . 'wp-admin/includes/plugin.php'; 
  1430.  
  1431. return get_plugins( $plugin_folder ); 
  1432.  
  1433. /** 
  1434. * Delete dismissable nag option when theme is switched. 
  1435. * This ensures that the user(s) is/are again reminded via nag of required 
  1436. * and/or recommended plugins if they re-activate the theme. 
  1437. * @since 2.1.1 
  1438. */ 
  1439. public function update_dismiss() { 
  1440. delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true ); 
  1441.  
  1442. /** 
  1443. * Forces plugin activation if the parameter 'force_activation' is 
  1444. * set to true. 
  1445. * This allows theme authors to specify certain plugins that must be 
  1446. * active at all times while using the current theme. 
  1447. * Please take special care when using this parameter as it has the 
  1448. * potential to be harmful if not used correctly. Setting this parameter 
  1449. * to true will not allow the specified plugin to be deactivated unless 
  1450. * the user switches themes. 
  1451. * @since 2.2.0 
  1452. */ 
  1453. public function force_activation() { 
  1454. foreach ( $this->plugins as $slug => $plugin ) { 
  1455. if ( true === $plugin['force_activation'] ) { 
  1456. if ( ! $this->is_plugin_installed( $slug ) ) { 
  1457. // Oops, plugin isn't there so iterate to next condition. 
  1458. continue; 
  1459. } elseif ( $this->can_plugin_activate( $slug ) ) { 
  1460. // There we go, activate the plugin. 
  1461. activate_plugin( $plugin['file_path'] ); 
  1462.  
  1463. /** 
  1464. * Forces plugin deactivation if the parameter 'force_deactivation' 
  1465. * is set to true. 
  1466. * This allows theme authors to specify certain plugins that must be 
  1467. * deactivated upon switching from the current theme to another. 
  1468. * Please take special care when using this parameter as it has the 
  1469. * potential to be harmful if not used correctly. 
  1470. * @since 2.2.0 
  1471. */ 
  1472. public function force_deactivation() { 
  1473. foreach ( $this->plugins as $slug => $plugin ) { 
  1474. // Only proceed forward if the parameter is set to true and plugin is active. 
  1475. if ( true === $plugin['force_deactivation'] && $this->is_plugin_active( $slug ) ) { 
  1476. deactivate_plugins( $plugin['file_path'] ); 
  1477.  
  1478. /** 
  1479. * Echo the current TGMPA version number to the page. 
  1480. */ 
  1481. public function show_tgmpa_version() { 
  1482. echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',  
  1483. esc_html( sprintf( _x( 'TGMPA v%s', '%s = version number', 'megaresponsive-lite' ), self::TGMPA_VERSION ) ),  
  1484. '</small></strong></p>'; 
  1485.  
  1486. /** 
  1487. * Returns the singleton instance of the class. 
  1488. * @since 2.4.0 
  1489. * @return object The TGM_Plugin_Activation object. 
  1490. */ 
  1491. public static function get_instance() { 
  1492. if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { 
  1493. self::$instance = new self(); 
  1494.  
  1495. return self::$instance;