TGM_Plugin_Activation

Automatic plugin installation and activation library.

Defined (1)

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

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