/includes/class-tgm-plugin-activation.php

  1. <?php 
  2. /** 
  3. * Plugin installation and activation for WordPress themes. 
  4. * 
  5. * Please note that this is a drop-in library for a theme or plugin. 
  6. * The authors of this library (Thomas, Gary and Juliette) are NOT responsible 
  7. * for the support of your plugin or theme. Please contact the plugin 
  8. * or theme author for support. 
  9. * 
  10. * @package TGM-Plugin-Activation 
  11. * @version 2.6.1 
  12. * @link http://tgmpluginactivation.com/ 
  13. * @author Thomas Griffin, Gary Jones, Juliette Reinders Folmer 
  14. * @copyright Copyright (c) 2011, Thomas Griffin 
  15. * @license GPL-2.0+ 
  16. */ 
  17.  
  18. /** 
  19. Copyright 2011 Thomas Griffin (thomasgriffinmedia.com) 
  20.   
  21. This program is free software; you can redistribute it and/or modify 
  22. it under the terms of the GNU General Public License, version 2, as 
  23. published by the Free Software Foundation. 
  24.   
  25. This program is distributed in the hope that it will be useful,  
  26. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  27. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  28. GNU General Public License for more details. 
  29.   
  30. You should have received a copy of the GNU General Public License 
  31. along with this program; if not, write to the Free Software 
  32. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
  33. */ 
  34.  
  35. if ( ! class_exists( 'TGM_Plugin_Activation' ) ) { 
  36.  
  37. /** 
  38. * Automatic plugin installation and activation library. 
  39. * 
  40. * Creates a way to automatically install and activate plugins from within themes. 
  41. * The plugins can be either bundled, downloaded from the WordPress 
  42. * Plugin Repository or downloaded from another external source. 
  43. * 
  44. * @since 1.0.0 
  45. * 
  46. * @package TGM-Plugin-Activation 
  47. * @author Thomas Griffin 
  48. * @author Gary Jones 
  49. */ 
  50. class TGM_Plugin_Activation { 
  51. /** 
  52. * TGMPA version number. 
  53. * 
  54. * @since 2.5.0 
  55. * 
  56. * @const string Version number. 
  57. */ 
  58. const TGMPA_VERSION = '2.6.1'; 
  59.  
  60. /** 
  61. * Regular expression to test if a URL is a WP plugin repo URL. 
  62. * 
  63. * @const string Regex. 
  64. * 
  65. * @since 2.5.0 
  66. */ 
  67. const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|'; 
  68.  
  69. /** 
  70. * Arbitrary regular expression to test if a string starts with a URL. 
  71. * 
  72. * @const string Regex. 
  73. * 
  74. * @since 2.5.0 
  75. */ 
  76. const IS_URL_REGEX = '|^http[s]?://|'; 
  77.  
  78. /** 
  79. * Holds a copy of itself, so it can be referenced by the class name. 
  80. * 
  81. * @since 1.0.0 
  82. * 
  83. * @var TGM_Plugin_Activation 
  84. */ 
  85. public static $instance; 
  86.  
  87. /** 
  88. * Holds arrays of plugin details. 
  89. * 
  90. * @since 1.0.0 
  91. * @since 2.5.0 the array has the plugin slug as an associative key. 
  92. * 
  93. * @var array 
  94. */ 
  95. public $plugins = array(); 
  96.  
  97. /** 
  98. * Holds arrays of plugin names to use to sort the plugins array. 
  99. * 
  100. * @since 2.5.0 
  101. * 
  102. * @var array 
  103. */ 
  104. protected $sort_order = array(); 
  105.  
  106. /** 
  107. * Whether any plugins have the 'force_activation' setting set to true. 
  108. * 
  109. * @since 2.5.0 
  110. * 
  111. * @var bool 
  112. */ 
  113. protected $has_forced_activation = false; 
  114.  
  115. /** 
  116. * Whether any plugins have the 'force_deactivation' setting set to true. 
  117. * 
  118. * @since 2.5.0 
  119. * 
  120. * @var bool 
  121. */ 
  122. protected $has_forced_deactivation = false; 
  123.  
  124. /** 
  125. * Name of the unique ID to hash notices. 
  126. * 
  127. * @since 2.4.0 
  128. * 
  129. * @var string 
  130. */ 
  131. public $id = 'tgmpa'; 
  132.  
  133. /** 
  134. * Name of the query-string argument for the admin page. 
  135. * 
  136. * @since 1.0.0 
  137. * 
  138. * @var string 
  139. */ 
  140. protected $menu = 'tgmpa-install-plugins'; 
  141.  
  142. /** 
  143. * Parent menu file slug. 
  144. * 
  145. * @since 2.5.0 
  146. * 
  147. * @var string 
  148. */ 
  149. public $parent_slug = 'themes.php'; 
  150.  
  151. /** 
  152. * Capability needed to view the plugin installation menu item. 
  153. * 
  154. * @since 2.5.0 
  155. * 
  156. * @var string 
  157. */ 
  158. public $capability = 'edit_theme_options'; 
  159.  
  160. /** 
  161. * Default absolute path to folder containing bundled plugin zip files. 
  162. * 
  163. * @since 2.0.0 
  164. * 
  165. * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string. 
  166. */ 
  167. public $default_path = ''; 
  168.  
  169. /** 
  170. * Flag to show admin notices or not. 
  171. * 
  172. * @since 2.1.0 
  173. * 
  174. * @var boolean 
  175. */ 
  176. public $has_notices = true; 
  177.  
  178. /** 
  179. * Flag to determine if the user can dismiss the notice nag. 
  180. * 
  181. * @since 2.4.0 
  182. * 
  183. * @var boolean 
  184. */ 
  185. public $dismissable = true; 
  186.  
  187. /** 
  188. * Message to be output above nag notice if dismissable is false. 
  189. * 
  190. * @since 2.4.0 
  191. * 
  192. * @var string 
  193. */ 
  194. public $dismiss_msg = ''; 
  195.  
  196. /** 
  197. * Flag to set automatic activation of plugins. Off by default. 
  198. * 
  199. * @since 2.2.0 
  200. * 
  201. * @var boolean 
  202. */ 
  203. public $is_automatic = false; 
  204.  
  205. /** 
  206. * Optional message to display before the plugins table. 
  207. * 
  208. * @since 2.2.0 
  209. * 
  210. * @var string Message filtered by wp_kses_post(). Default is empty string. 
  211. */ 
  212. public $message = ''; 
  213.  
  214. /** 
  215. * Holds configurable array of strings. 
  216. * 
  217. * Default values are added in the constructor. 
  218. * 
  219. * @since 2.0.0 
  220. * 
  221. * @var array 
  222. */ 
  223. public $strings = array(); 
  224.  
  225. /** 
  226. * Holds the version of WordPress. 
  227. * 
  228. * @since 2.4.0 
  229. * 
  230. * @var int 
  231. */ 
  232. public $wp_version; 
  233.  
  234. /** 
  235. * Holds the hook name for the admin page. 
  236. * 
  237. * @since 2.5.0 
  238. * 
  239. * @var string 
  240. */ 
  241. public $page_hook; 
  242.  
  243. /** 
  244. * Adds a reference of this object to $instance, populates default strings,  
  245. * does the tgmpa_init action hook, and hooks in the interactions to init. 
  246. * 
  247. * {@internal This method should be `protected`, but as too many TGMPA implementations 
  248. * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues. 
  249. * Reverted back to public for the time being.}} 
  250. * 
  251. * @since 1.0.0 
  252. * 
  253. * @see TGM_Plugin_Activation::init() 
  254. */ 
  255. public function __construct() { 
  256. // Set the current WordPress version. 
  257. $this->wp_version = $GLOBALS['wp_version']; 
  258.  
  259. // Announce that the class is ready, and pass the object (for advanced use). 
  260. do_action_ref_array( 'tgmpa_init', array( $this ) ); 
  261.  
  262. /** 
  263. * Load our text domain and allow for overloading the fall-back file. 
  264. * 
  265. * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA 
  266. * generator on the website.}} 
  267. */ 
  268. add_action( 'init', array( $this, 'load_textdomain' ), 5 ); 
  269. add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 ); 
  270.  
  271. // When the rest of WP has loaded, kick-start the rest of the class. 
  272. add_action( 'init', array( $this, 'init' ) ); 
  273.  
  274. /** 
  275. * Magic method to (not) set protected properties from outside of this class. 
  276. * 
  277. * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6 where the `menu` property 
  278. * is being assigned rather than tested in a conditional, effectively rendering it useless. 
  279. * This 'hack' prevents this from happening.}} 
  280. * 
  281. * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593 
  282. * 
  283. * @since 2.5.2 
  284. * 
  285. * @param string $name Name of an inaccessible property. 
  286. * @param mixed $value Value to assign to the property. 
  287. * @return void Silently fail to set the property when this is tried from outside of this class context. 
  288. * (Inside this class context, the __set() method if not used as there is direct access.) 
  289. */ 
  290. public function __set( $name, $value ) { 
  291. return; 
  292.  
  293. /** 
  294. * Magic method to get the value of a protected property outside of this class context. 
  295. * 
  296. * @since 2.5.2 
  297. * 
  298. * @param string $name Name of an inaccessible property. 
  299. * @return mixed The property value. 
  300. */ 
  301. public function __get( $name ) { 
  302. return $this->{$name}; 
  303.  
  304. /** 
  305. * Initialise the interactions between this class and WordPress. 
  306. * 
  307. * Hooks in three new methods for the class: admin_menu, notices and styles. 
  308. * 
  309. * @since 2.0.0 
  310. * 
  311. * @see TGM_Plugin_Activation::admin_menu() 
  312. * @see TGM_Plugin_Activation::notices() 
  313. * @see TGM_Plugin_Activation::styles() 
  314. */ 
  315. public function init() { 
  316. /** 
  317. * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter 
  318. * you can overrule that behaviour. 
  319. * 
  320. * @since 2.5.0 
  321. * 
  322. * @param bool $load Whether or not TGMPA should load. 
  323. * Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`. 
  324. */ 
  325. if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) { 
  326. return; 
  327.  
  328. // Load class strings. 
  329. $this->strings = array( 
  330. 'page_title' => __( 'Install Required Plugins', 'tgmpa' ),  
  331. 'menu_title' => __( 'Install Plugins', 'tgmpa' ),  
  332. /** translators: %s: plugin name. */ 
  333. 'installing' => __( 'Installing Plugin: %s', 'tgmpa' ),  
  334. /** translators: %s: plugin name. */ 
  335. 'updating' => __( 'Updating Plugin: %s', 'tgmpa' ),  
  336. 'oops' => __( 'Something went wrong with the plugin API.', 'tgmpa' ),  
  337. 'notice_can_install_required' => _n_noop( 
  338. /** translators: 1: plugin name(s). */ 
  339. 'This theme requires the following plugin: %1$s.',  
  340. 'This theme requires the following plugins: %1$s.',  
  341. 'tgmpa' 
  342. ),  
  343. 'notice_can_install_recommended' => _n_noop( 
  344. /** translators: 1: plugin name(s). */ 
  345. 'This theme recommends the following plugin: %1$s.',  
  346. 'This theme recommends the following plugins: %1$s.',  
  347. 'tgmpa' 
  348. ),  
  349. 'notice_ask_to_update' => _n_noop( 
  350. /** translators: 1: plugin name(s). */ 
  351. 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',  
  352. 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',  
  353. 'tgmpa' 
  354. ),  
  355. 'notice_ask_to_update_maybe' => _n_noop( 
  356. /** translators: 1: plugin name(s). */ 
  357. 'There is an update available for: %1$s.',  
  358. 'There are updates available for the following plugins: %1$s.',  
  359. 'tgmpa' 
  360. ),  
  361. 'notice_can_activate_required' => _n_noop( 
  362. /** translators: 1: plugin name(s). */ 
  363. 'The following required plugin is currently inactive: %1$s.',  
  364. 'The following required plugins are currently inactive: %1$s.',  
  365. 'tgmpa' 
  366. ),  
  367. 'notice_can_activate_recommended' => _n_noop( 
  368. /** translators: 1: plugin name(s). */ 
  369. 'The following recommended plugin is currently inactive: %1$s.',  
  370. 'The following recommended plugins are currently inactive: %1$s.',  
  371. 'tgmpa' 
  372. ),  
  373. 'install_link' => _n_noop( 
  374. 'Begin installing plugin',  
  375. 'Begin installing plugins',  
  376. 'tgmpa' 
  377. ),  
  378. 'update_link' => _n_noop( 
  379. 'Begin updating plugin',  
  380. 'Begin updating plugins',  
  381. 'tgmpa' 
  382. ),  
  383. 'activate_link' => _n_noop( 
  384. 'Begin activating plugin',  
  385. 'Begin activating plugins',  
  386. 'tgmpa' 
  387. ),  
  388. 'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ),  
  389. 'dashboard' => __( 'Return to the Dashboard', 'tgmpa' ),  
  390. 'plugin_activated' => __( 'Plugin activated successfully.', 'tgmpa' ),  
  391. 'activated_successfully' => __( 'The following plugin was activated successfully:', 'tgmpa' ),  
  392. /** translators: 1: plugin name. */ 
  393. 'plugin_already_active' => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ),  
  394. /** translators: 1: plugin name. */ 
  395. 'plugin_needs_higher_version' => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ),  
  396. /** translators: 1: dashboard link. */ 
  397. 'complete' => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ),  
  398. 'dismiss' => __( 'Dismiss this notice', 'tgmpa' ),  
  399. 'notice_cannot_install_activate' => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ),  
  400. 'contact_admin' => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),  
  401. ); 
  402.  
  403. do_action( 'tgmpa_register' ); 
  404.  
  405. /** After this point, the plugins should be registered and the configuration set. */ 
  406.  
  407. // Proceed only if we have plugins to handle. 
  408. if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { 
  409. return; 
  410.  
  411. // Set up the menu and notices if we still have outstanding actions. 
  412. if ( true !== $this->is_tgmpa_complete() ) { 
  413. // Sort the plugins. 
  414. array_multisort( $this->sort_order, SORT_ASC, $this->plugins ); 
  415.  
  416. add_action( 'admin_menu', array( $this, 'admin_menu' ) ); 
  417. add_action( 'admin_head', array( $this, 'dismiss' ) ); 
  418.  
  419. // Prevent the normal links from showing underneath a single install/update page. 
  420. add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) ); 
  421. add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) ); 
  422.  
  423. if ( $this->has_notices ) { 
  424. add_action( 'admin_notices', array( $this, 'notices' ) ); 
  425. add_action( 'admin_init', array( $this, 'admin_init' ), 1 ); 
  426. add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) ); 
  427.  
  428. // If needed, filter plugin action links. 
  429. add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 ); 
  430.  
  431. // Make sure things get reset on switch theme. 
  432. add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) ); 
  433.  
  434. if ( $this->has_notices ) { 
  435. add_action( 'switch_theme', array( $this, 'update_dismiss' ) ); 
  436.  
  437. // Setup the force activation hook. 
  438. if ( true === $this->has_forced_activation ) { 
  439. add_action( 'admin_init', array( $this, 'force_activation' ) ); 
  440.  
  441. // Setup the force deactivation hook. 
  442. if ( true === $this->has_forced_deactivation ) { 
  443. add_action( 'switch_theme', array( $this, 'force_deactivation' ) ); 
  444.  
  445. /** 
  446. * Load translations. 
  447. * 
  448. * @since 2.6.0 
  449. * 
  450. * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to 
  451. * get round the different ways of handling the path and deprecated notices being thrown 
  452. * and such. For plugins, the actual file name will be corrected by a filter.}} 
  453. * 
  454. * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA 
  455. * generator on the website.}} 
  456. */ 
  457. public function load_textdomain() { 
  458. if ( is_textdomain_loaded( 'tgmpa' ) ) { 
  459. return; 
  460.  
  461. if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) { 
  462. // Plugin, we'll need to adjust the file name. 
  463. add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 ); 
  464. load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' ); 
  465. remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 ); 
  466. } else { 
  467. load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' ); 
  468.  
  469. /** 
  470. * Correct the .mo file name for (must-use) plugins. 
  471. * 
  472. * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`. 
  473. * 
  474. * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA 
  475. * generator on the website.}} 
  476. * 
  477. * @since 2.6.0 
  478. * 
  479. * @param string $mofile Full path to the target mofile. 
  480. * @param string $domain The domain for which a language file is being loaded. 
  481. * @return string $mofile 
  482. */ 
  483. public function correct_plugin_mofile( $mofile, $domain ) { 
  484. // Exit early if not our domain (just in case). 
  485. if ( 'tgmpa' !== $domain ) { 
  486. return $mofile; 
  487. return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile ); 
  488.  
  489. /** 
  490. * Potentially overload the fall-back translation file for the current language. 
  491. * 
  492. * WP, by default since WP 3.7, will load a local translation first and if none 
  493. * can be found, will try and find a translation in the /wp-content/languages/ directory. 
  494. * As this library is theme/plugin agnostic, translation files for TGMPA can exist both 
  495. * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory. 
  496. * 
  497. * This method makes sure both directories are checked. 
  498. * 
  499. * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA 
  500. * generator on the website.}} 
  501. * 
  502. * @since 2.6.0 
  503. * 
  504. * @param string $mofile Full path to the target mofile. 
  505. * @param string $domain The domain for which a language file is being loaded. 
  506. * @return string $mofile 
  507. */ 
  508. public function overload_textdomain_mofile( $mofile, $domain ) { 
  509. // Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable. 
  510. if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) { 
  511. return $mofile; 
  512.  
  513. // Current fallback file is not valid, let's try the alternative option. 
  514. if ( false !== strpos( $mofile, '/themes/' ) ) { 
  515. return str_replace( '/themes/', '/plugins/', $mofile ); 
  516. } elseif ( false !== strpos( $mofile, '/plugins/' ) ) { 
  517. return str_replace( '/plugins/', '/themes/', $mofile ); 
  518. } else { 
  519. return $mofile; 
  520.  
  521. /** 
  522. * Hook in plugin action link filters for the WP native plugins page. 
  523. * 
  524. * - Prevent activation of plugins which don't meet the minimum version requirements. 
  525. * - Prevent deactivation of force-activated plugins. 
  526. * - Add update notice if update available. 
  527. * 
  528. * @since 2.5.0 
  529. */ 
  530. public function add_plugin_action_link_filters() { 
  531. foreach ( $this->plugins as $slug => $plugin ) { 
  532. if ( false === $this->can_plugin_activate( $slug ) ) { 
  533. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 ); 
  534.  
  535. if ( true === $plugin['force_activation'] ) { 
  536. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 ); 
  537.  
  538. if ( false !== $this->does_plugin_require_update( $slug ) ) { 
  539. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 ); 
  540.  
  541. /** 
  542. * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the 
  543. * minimum version requirements. 
  544. * 
  545. * @since 2.5.0 
  546. * 
  547. * @param array $actions Action links. 
  548. * @return array 
  549. */ 
  550. public function filter_plugin_action_links_activate( $actions ) { 
  551. unset( $actions['activate'] ); 
  552.  
  553. return $actions; 
  554.  
  555. /** 
  556. * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate. 
  557. * 
  558. * @since 2.5.0 
  559. * 
  560. * @param array $actions Action links. 
  561. * @return array 
  562. */ 
  563. public function filter_plugin_action_links_deactivate( $actions ) { 
  564. unset( $actions['deactivate'] ); 
  565.  
  566. return $actions; 
  567.  
  568. /** 
  569. * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the 
  570. * minimum version requirements. 
  571. * 
  572. * @since 2.5.0 
  573. * 
  574. * @param array $actions Action links. 
  575. * @return array 
  576. */ 
  577. public function filter_plugin_action_links_update( $actions ) { 
  578. $actions['update'] = sprintf( 
  579. '<a href="%1$s" title="%2$s" class="edit">%3$s</a>',  
  580. esc_url( $this->get_tgmpa_status_url( 'update' ) ),  
  581. esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ),  
  582. esc_html__( 'Update Required', 'tgmpa' ) 
  583. ); 
  584.  
  585. return $actions; 
  586.  
  587. /** 
  588. * Handles calls to show plugin information via links in the notices. 
  589. * 
  590. * We get the links in the admin notices to point to the TGMPA page, rather 
  591. * than the typical plugin-install.php file, so we can prepare everything 
  592. * beforehand. 
  593. * 
  594. * WP does not make it easy to show the plugin information in the thickbox - 
  595. * here we have to require a file that includes a function that does the 
  596. * main work of displaying it, enqueue some styles, set up some globals and 
  597. * finally call that function before exiting. 
  598. * 
  599. * Down right easy once you know how... 
  600. * 
  601. * Returns early if not the TGMPA page. 
  602. * 
  603. * @since 2.1.0 
  604. * 
  605. * @global string $tab Used as iframe div class names, helps with styling 
  606. * @global string $body_id Used as the iframe body ID, helps with styling 
  607. * 
  608. * @return null Returns early if not the TGMPA page. 
  609. */ 
  610. public function admin_init() { 
  611. if ( ! $this->is_tgmpa_page() ) { 
  612. return; 
  613.  
  614. if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) { 
  615. // Needed for install_plugin_information(). 
  616. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; 
  617.  
  618. wp_enqueue_style( 'plugin-install' ); 
  619.  
  620. global $tab, $body_id; 
  621. $body_id = 'plugin-information'; 
  622. // @codingStandardsIgnoreStart 
  623. $tab = 'plugin-information'; 
  624. // @codingStandardsIgnoreEnd 
  625.  
  626. install_plugin_information(); 
  627.  
  628. exit; 
  629.  
  630. /** 
  631. * Enqueue thickbox scripts/styles for plugin info. 
  632. * 
  633. * Thickbox is not automatically included on all admin pages, so we must 
  634. * manually enqueue it for those pages. 
  635. * 
  636. * Thickbox is only loaded if the user has not dismissed the admin 
  637. * notice or if there are any plugins left to install and activate. 
  638. * 
  639. * @since 2.1.0 
  640. */ 
  641. public function thickbox() { 
  642. if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) { 
  643. add_thickbox(); 
  644.  
  645. /** 
  646. * Adds submenu page if there are plugin actions to take. 
  647. * 
  648. * This method adds the submenu page letting users know that a required 
  649. * plugin needs to be installed. 
  650. * 
  651. * This page disappears once the plugin has been installed and activated. 
  652. * 
  653. * @since 1.0.0 
  654. * 
  655. * @see TGM_Plugin_Activation::init() 
  656. * @see TGM_Plugin_Activation::install_plugins_page() 
  657. * 
  658. * @return null Return early if user lacks capability to install a plugin. 
  659. */ 
  660. public function admin_menu() { 
  661. // Make sure privileges are correct to see the page. 
  662. if ( ! current_user_can( 'install_plugins' ) ) { 
  663. return; 
  664.  
  665. $args = apply_filters( 
  666. 'tgmpa_admin_menu_args',  
  667. array( 
  668. 'parent_slug' => $this->parent_slug, // Parent Menu slug. 
  669. 'page_title' => $this->strings['page_title'], // Page title. 
  670. 'menu_title' => $this->strings['menu_title'], // Menu title. 
  671. 'capability' => $this->capability, // Capability. 
  672. 'menu_slug' => $this->menu, // Menu slug. 
  673. 'function' => array( $this, 'install_plugins_page' ), // Callback. 
  674. ); 
  675.  
  676. $this->add_admin_menu( $args ); 
  677.  
  678. /** 
  679. * Add the menu item. 
  680. * 
  681. * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA 
  682. * generator on the website.}} 
  683. * 
  684. * @since 2.5.0 
  685. * 
  686. * @param array $args Menu item configuration. 
  687. */ 
  688. protected function add_admin_menu( array $args ) { 
  689. if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) { 
  690. _deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) ); 
  691.  
  692. if ( 'themes.php' === $this->parent_slug ) { 
  693. $this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); 
  694. } else { 
  695. $this->page_hook = call_user_func( 'add_submenu_page', $args['parent_slug'], $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); 
  696.  
  697. /** 
  698. * Echoes plugin installation form. 
  699. * 
  700. * This method is the callback for the admin_menu method function. 
  701. * This displays the admin page and form area where the user can select to install and activate the plugin. 
  702. * Aborts early if we're processing a plugin installation action. 
  703. * 
  704. * @since 1.0.0 
  705. * 
  706. * @return null Aborts early if we're processing a plugin installation action. 
  707. */ 
  708. public function install_plugins_page() { 
  709. // Store new instance of plugin table in object. 
  710. $plugin_table = new TGMPA_List_Table; 
  711.  
  712. // Return early if processing a plugin installation action. 
  713. if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) { 
  714. return; 
  715.  
  716. // Force refresh of available plugin information so we'll know about manual updates/deletes. 
  717. wp_clean_plugins_cache( false ); 
  718.  
  719. ?> 
  720. <div class="tgmpa wrap"> 
  721. <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> 
  722. <?php $plugin_table->prepare_items(); ?> 
  723.  
  724. <?php 
  725. if ( ! empty( $this->message ) && is_string( $this->message ) ) { 
  726. echo wp_kses_post( $this->message ); 
  727. ?> 
  728. <?php $plugin_table->views(); ?> 
  729.  
  730. <form id="tgmpa-plugins" action="" method="post"> 
  731. <input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" /> 
  732. <input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" /> 
  733. <?php $plugin_table->display(); ?> 
  734. </form> 
  735. </div> 
  736. <?php 
  737.  
  738. /** 
  739. * Installs, updates or activates a plugin depending on the action link clicked by the user. 
  740. * 
  741. * Checks the $_GET variable to see which actions have been 
  742. * passed and responds with the appropriate method. 
  743. * 
  744. * Uses WP_Filesystem to process and handle the plugin installation 
  745. * method. 
  746. * 
  747. * @since 1.0.0 
  748. * 
  749. * @uses WP_Filesystem 
  750. * @uses WP_Error 
  751. * @uses WP_Upgrader 
  752. * @uses Plugin_Upgrader 
  753. * @uses Plugin_Installer_Skin 
  754. * @uses Plugin_Upgrader_Skin 
  755. * 
  756. * @return boolean True on success, false on failure. 
  757. */ 
  758. protected function do_plugin_install() { 
  759. if ( empty( $_GET['plugin'] ) ) { 
  760. return false; 
  761.  
  762. // All plugin information will be stored in an array for processing. 
  763. $slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) ); 
  764.  
  765. if ( ! isset( $this->plugins[ $slug ] ) ) { 
  766. return false; 
  767.  
  768. // Was an install or upgrade action link clicked? 
  769. if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) { 
  770.  
  771. $install_type = 'install'; 
  772. if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) { 
  773. $install_type = 'update'; 
  774.  
  775. check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' ); 
  776.  
  777. // Pass necessary information via URL if WP_Filesystem is needed. 
  778. $url = wp_nonce_url( 
  779. add_query_arg( 
  780. array( 
  781. 'plugin' => urlencode( $slug ),  
  782. 'tgmpa-' . $install_type => $install_type . '-plugin',  
  783. ),  
  784. $this->get_tgmpa_url() 
  785. ),  
  786. 'tgmpa-' . $install_type,  
  787. 'tgmpa-nonce' 
  788. ); 
  789.  
  790. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary. 
  791.  
  792. if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) { 
  793. return true; 
  794.  
  795. if ( ! WP_Filesystem( $creds ) ) { 
  796. request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem. 
  797. return true; 
  798.  
  799. /** If we arrive here, we have the filesystem. */ 
  800.  
  801. // Prep variables for Plugin_Installer_Skin class. 
  802. $extra = array(); 
  803. $extra['slug'] = $slug; // Needed for potentially renaming of directory name. 
  804. $source = $this->get_download_url( $slug ); 
  805. $api = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null; 
  806. $api = ( false !== $api ) ? $api : null; 
  807.  
  808. $url = add_query_arg( 
  809. array( 
  810. 'action' => $install_type . '-plugin',  
  811. 'plugin' => urlencode( $slug ),  
  812. ),  
  813. 'update.php' 
  814. ); 
  815.  
  816. if ( ! class_exists( 'Plugin_Upgrader', false ) ) { 
  817. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
  818.  
  819. $title = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing']; 
  820. $skin_args = array( 
  821. 'type' => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',  
  822. 'title' => sprintf( $title, $this->plugins[ $slug ]['name'] ),  
  823. 'url' => esc_url_raw( $url ),  
  824. 'nonce' => $install_type . '-plugin_' . $slug,  
  825. 'plugin' => '',  
  826. 'api' => $api,  
  827. 'extra' => $extra,  
  828. ); 
  829. unset( $title ); 
  830.  
  831. if ( 'update' === $install_type ) { 
  832. $skin_args['plugin'] = $this->plugins[ $slug ]['file_path']; 
  833. $skin = new Plugin_Upgrader_Skin( $skin_args ); 
  834. } else { 
  835. $skin = new Plugin_Installer_Skin( $skin_args ); 
  836.  
  837. // Create a new instance of Plugin_Upgrader. 
  838. $upgrader = new Plugin_Upgrader( $skin ); 
  839.  
  840. // Perform the action and install the plugin from the $source urldecode(). 
  841. add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 ); 
  842.  
  843. if ( 'update' === $install_type ) { 
  844. // Inject our info into the update transient. 
  845. $to_inject = array( $slug => $this->plugins[ $slug ] ); 
  846. $to_inject[ $slug ]['source'] = $source; 
  847. $this->inject_update_info( $to_inject ); 
  848.  
  849. $upgrader->upgrade( $this->plugins[ $slug ]['file_path'] ); 
  850. } else { 
  851. $upgrader->install( $source ); 
  852.  
  853. remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 ); 
  854.  
  855. // Make sure we have the correct file path now the plugin is installed/updated. 
  856. $this->populate_file_path( $slug ); 
  857.  
  858. // Only activate plugins if the config option is set to true and the plugin isn't 
  859. // already active (upgrade). 
  860. if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) { 
  861. $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method. 
  862. if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { 
  863. return true; // Finish execution of the function early as we encountered an error. 
  864.  
  865. $this->show_tgmpa_version(); 
  866.  
  867. // Display message based on if all plugins are now active or not. 
  868. if ( $this->is_tgmpa_complete() ) { 
  869. echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ), '</p>'; 
  870. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; 
  871. } else { 
  872. echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; 
  873.  
  874. return true; 
  875. } elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) { 
  876. // Activate action link was clicked. 
  877. check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' ); 
  878.  
  879. if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) { 
  880. return true; // Finish execution of the function early as we encountered an error. 
  881.  
  882. return false; 
  883.  
  884. /** 
  885. * Inject information into the 'update_plugins' site transient as WP checks that before running an update. 
  886. * 
  887. * @since 2.5.0 
  888. * 
  889. * @param array $plugins The plugin information for the plugins which are to be updated. 
  890. */ 
  891. public function inject_update_info( $plugins ) { 
  892. $repo_updates = get_site_transient( 'update_plugins' ); 
  893.  
  894. if ( ! is_object( $repo_updates ) ) { 
  895. $repo_updates = new stdClass; 
  896.  
  897. foreach ( $plugins as $slug => $plugin ) { 
  898. $file_path = $plugin['file_path']; 
  899.  
  900. if ( empty( $repo_updates->response[ $file_path ] ) ) { 
  901. $repo_updates->response[ $file_path ] = new stdClass; 
  902.  
  903. // We only really need to set package, but let's do all we can in case WP changes something. 
  904. $repo_updates->response[ $file_path ]->slug = $slug; 
  905. $repo_updates->response[ $file_path ]->plugin = $file_path; 
  906. $repo_updates->response[ $file_path ]->new_version = $plugin['version']; 
  907. $repo_updates->response[ $file_path ]->package = $plugin['source']; 
  908. if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) { 
  909. $repo_updates->response[ $file_path ]->url = $plugin['external_url']; 
  910.  
  911. set_site_transient( 'update_plugins', $repo_updates ); 
  912.  
  913. /** 
  914. * Adjust the plugin directory name if necessary. 
  915. * 
  916. * The final destination directory of a plugin is based on the subdirectory name found in the 
  917. * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this 
  918. * subdirectory name is not the same as the expected slug and the plugin will not be recognized 
  919. * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to 
  920. * the expected plugin slug. 
  921. * 
  922. * @since 2.5.0 
  923. * 
  924. * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/. 
  925. * @param string $remote_source Path to upgrade/zip-file-name.tmp. 
  926. * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin. 
  927. * @return string $source 
  928. */ 
  929. public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { 
  930. if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) { 
  931. return $source; 
  932.  
  933. // Check for single file plugins. 
  934. $source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) ); 
  935. if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) { 
  936. return $source; 
  937.  
  938. // Multi-file plugin, let's see if the directory is correctly named. 
  939. $desired_slug = ''; 
  940.  
  941. // Figure out what the slug is supposed to be. 
  942. if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) { 
  943. $desired_slug = $upgrader->skin->options['extra']['slug']; 
  944. } else { 
  945. // Bulk installer contains less info, so fall back on the info registered here. 
  946. foreach ( $this->plugins as $slug => $plugin ) { 
  947. if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) { 
  948. $desired_slug = $slug; 
  949. break; 
  950. unset( $slug, $plugin ); 
  951.  
  952. if ( ! empty( $desired_slug ) ) { 
  953. $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) ); 
  954.  
  955. if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) { 
  956. $from_path = untrailingslashit( $source ); 
  957. $to_path = trailingslashit( $remote_source ) . $desired_slug; 
  958.  
  959. if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) { 
  960. return trailingslashit( $to_path ); 
  961. } else { 
  962. 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.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) ); 
  963. } elseif ( empty( $subdir_name ) ) { 
  964. 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.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) ); 
  965.  
  966. return $source; 
  967.  
  968. /** 
  969. * Activate a single plugin and send feedback about the result to the screen. 
  970. * 
  971. * @since 2.5.0 
  972. * 
  973. * @param string $file_path Path within wp-plugins/ to main plugin file. 
  974. * @param string $slug Plugin slug. 
  975. * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false. 
  976. * This determines the styling of the output messages. 
  977. * @return bool False if an error was encountered, true otherwise. 
  978. */ 
  979. protected function activate_single_plugin( $file_path, $slug, $automatic = false ) { 
  980. if ( $this->can_plugin_activate( $slug ) ) { 
  981. $activate = activate_plugin( $file_path ); 
  982.  
  983. if ( is_wp_error( $activate ) ) { 
  984. echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',  
  985. '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; 
  986.  
  987. return false; // End it here if there is an error with activation. 
  988. } else { 
  989. if ( ! $automatic ) { 
  990. // Make sure message doesn't display again if bulk activation is performed 
  991. // immediately after a single activation. 
  992. if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK. 
  993. echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>'; 
  994. } else { 
  995. // Simpler message layout for use on the plugin install page. 
  996. echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>'; 
  997. } elseif ( $this->is_plugin_active( $slug ) ) { 
  998. // No simpler message format provided as this message should never be encountered 
  999. // on the plugin install page. 
  1000. echo '<div id="message" class="error"><p>',  
  1001. sprintf( 
  1002. esc_html( $this->strings['plugin_already_active'] ),  
  1003. '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>' 
  1004. ),  
  1005. '</p></div>'; 
  1006. } elseif ( $this->does_plugin_require_update( $slug ) ) { 
  1007. if ( ! $automatic ) { 
  1008. // Make sure message doesn't display again if bulk activation is performed 
  1009. // immediately after a single activation. 
  1010. if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK. 
  1011. echo '<div id="message" class="error"><p>',  
  1012. sprintf( 
  1013. esc_html( $this->strings['plugin_needs_higher_version'] ),  
  1014. '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>' 
  1015. ),  
  1016. '</p></div>'; 
  1017. } else { 
  1018. // Simpler message layout for use on the plugin install page. 
  1019. echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>'; 
  1020.  
  1021. return true; 
  1022.  
  1023. /** 
  1024. * Echoes required plugin notice. 
  1025. * 
  1026. * Outputs a message telling users that a specific plugin is required for 
  1027. * their theme. If appropriate, it includes a link to the form page where 
  1028. * users can install and activate the plugin. 
  1029. * 
  1030. * Returns early if we're on the Install page. 
  1031. * 
  1032. * @since 1.0.0 
  1033. * 
  1034. * @global object $current_screen 
  1035. * 
  1036. * @return null Returns early if we're on the Install page. 
  1037. */ 
  1038. public function notices() { 
  1039. // Remove nag on the install page / Return early if the nag message has been dismissed or user < author. 
  1040. if ( ( $this->is_tgmpa_page() || $this->is_core_update_page() ) || get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) || ! current_user_can( apply_filters( 'tgmpa_show_admin_notice_capability', 'publish_posts' ) ) ) { 
  1041. return; 
  1042.  
  1043. // Store for the plugin slugs by message type. 
  1044. $message = array(); 
  1045.  
  1046. // Initialize counters used to determine plurality of action link texts. 
  1047. $install_link_count = 0; 
  1048. $update_link_count = 0; 
  1049. $activate_link_count = 0; 
  1050. $total_required_action_count = 0; 
  1051.  
  1052. foreach ( $this->plugins as $slug => $plugin ) { 
  1053. if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) { 
  1054. continue; 
  1055.  
  1056. if ( ! $this->is_plugin_installed( $slug ) ) { 
  1057. if ( current_user_can( 'install_plugins' ) ) { 
  1058. $install_link_count++; 
  1059.  
  1060. if ( true === $plugin['required'] ) { 
  1061. $message['notice_can_install_required'][] = $slug; 
  1062. } else { 
  1063. $message['notice_can_install_recommended'][] = $slug; 
  1064. if ( true === $plugin['required'] ) { 
  1065. $total_required_action_count++; 
  1066. } else { 
  1067. if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) { 
  1068. if ( current_user_can( 'activate_plugins' ) ) { 
  1069. $activate_link_count++; 
  1070.  
  1071. if ( true === $plugin['required'] ) { 
  1072. $message['notice_can_activate_required'][] = $slug; 
  1073. } else { 
  1074. $message['notice_can_activate_recommended'][] = $slug; 
  1075. if ( true === $plugin['required'] ) { 
  1076. $total_required_action_count++; 
  1077.  
  1078. if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) { 
  1079.  
  1080. if ( current_user_can( 'update_plugins' ) ) { 
  1081. $update_link_count++; 
  1082.  
  1083. if ( $this->does_plugin_require_update( $slug ) ) { 
  1084. $message['notice_ask_to_update'][] = $slug; 
  1085. } elseif ( false !== $this->does_plugin_have_update( $slug ) ) { 
  1086. $message['notice_ask_to_update_maybe'][] = $slug; 
  1087. if ( true === $plugin['required'] ) { 
  1088. $total_required_action_count++; 
  1089. unset( $slug, $plugin ); 
  1090.  
  1091. // If we have notices to display, we move forward. 
  1092. if ( ! empty( $message ) || $total_required_action_count > 0 ) { 
  1093. krsort( $message ); // Sort messages. 
  1094. $rendered = ''; 
  1095.  
  1096. // As add_settings_error() wraps the final message in a <p> and as the final message can't be 
  1097. // filtered, using <p>'s in our html would render invalid html output. 
  1098. $line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n"; 
  1099.  
  1100. if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) { 
  1101. $rendered = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] ); 
  1102. $rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template ); 
  1103. } else { 
  1104.  
  1105. // If dismissable is false and a message is set, output it now. 
  1106. if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) { 
  1107. $rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) ); 
  1108.  
  1109. // Render the individual message lines for the notice. 
  1110. foreach ( $message as $type => $plugin_group ) { 
  1111. $linked_plugins = array(); 
  1112.  
  1113. // Get the external info link for a plugin if one is available. 
  1114. foreach ( $plugin_group as $plugin_slug ) { 
  1115. $linked_plugins[] = $this->get_info_link( $plugin_slug ); 
  1116. unset( $plugin_slug ); 
  1117.  
  1118. $count = count( $plugin_group ); 
  1119. $linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins ); 
  1120. $last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability. 
  1121. $imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin ); 
  1122.  
  1123. $rendered .= sprintf( 
  1124. $line_template,  
  1125. sprintf( 
  1126. translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),  
  1127. $imploded,  
  1128. $count 
  1129. ); 
  1130.  
  1131. unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded ); 
  1132.  
  1133. $rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template ); 
  1134.  
  1135. // Register the nag messages and prepare them to be processed. 
  1136. add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() ); 
  1137.  
  1138. // Admin options pages already output settings_errors, so this is to avoid duplication. 
  1139. if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) { 
  1140. $this->display_settings_errors(); 
  1141.  
  1142. /** 
  1143. * Generate the user action links for the admin notice. 
  1144. * 
  1145. * @since 2.6.0 
  1146. * 
  1147. * @param int $install_count Number of plugins to install. 
  1148. * @param int $update_count Number of plugins to update. 
  1149. * @param int $activate_count Number of plugins to activate. 
  1150. * @param int $line_template Template for the HTML tag to output a line. 
  1151. * @return string Action links. 
  1152. */ 
  1153. protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) { 
  1154. // Setup action links. 
  1155. $action_links = array( 
  1156. 'install' => '',  
  1157. 'update' => '',  
  1158. 'activate' => '',  
  1159. 'dismiss' => $this->dismissable ? '<a href="' . esc_url( wp_nonce_url( add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ), 'tgmpa-dismiss-' . get_current_user_id() ) ) . '" class="dismiss-notice" target="_parent">' . esc_html( $this->strings['dismiss'] ) . '</a>' : '',  
  1160. ); 
  1161.  
  1162. $link_template = '<a href="%2$s">%1$s</a>'; 
  1163.  
  1164. if ( current_user_can( 'install_plugins' ) ) { 
  1165. if ( $install_count > 0 ) { 
  1166. $action_links['install'] = sprintf( 
  1167. $link_template,  
  1168. translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),  
  1169. esc_url( $this->get_tgmpa_status_url( 'install' ) ) 
  1170. ); 
  1171. if ( $update_count > 0 ) { 
  1172. $action_links['update'] = sprintf( 
  1173. $link_template,  
  1174. translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),  
  1175. esc_url( $this->get_tgmpa_status_url( 'update' ) ) 
  1176. ); 
  1177.  
  1178. if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) { 
  1179. $action_links['activate'] = sprintf( 
  1180. $link_template,  
  1181. translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),  
  1182. esc_url( $this->get_tgmpa_status_url( 'activate' ) ) 
  1183. ); 
  1184.  
  1185. $action_links = apply_filters( 'tgmpa_notice_action_links', $action_links ); 
  1186.  
  1187. $action_links = array_filter( (array) $action_links ); // Remove any empty array items. 
  1188.  
  1189. if ( ! empty( $action_links ) ) { 
  1190. $action_links = sprintf( $line_template, implode( ' | ', $action_links ) ); 
  1191. return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links ); 
  1192. } else { 
  1193. return ''; 
  1194.  
  1195. /** 
  1196. * Get admin notice class. 
  1197. * 
  1198. * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7 
  1199. * (lowest supported version by TGMPA). 
  1200. * 
  1201. * @since 2.6.0 
  1202. * 
  1203. * @return string 
  1204. */ 
  1205. protected function get_admin_notice_class() { 
  1206. if ( ! empty( $this->strings['nag_type'] ) ) { 
  1207. return sanitize_html_class( strtolower( $this->strings['nag_type'] ) ); 
  1208. } else { 
  1209. if ( version_compare( $this->wp_version, '4.2', '>=' ) ) { 
  1210. return 'notice-warning'; 
  1211. } elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) { 
  1212. return 'notice'; 
  1213. } else { 
  1214. return 'updated'; 
  1215.  
  1216. /** 
  1217. * Display settings errors and remove those which have been displayed to avoid duplicate messages showing 
  1218. * 
  1219. * @since 2.5.0 
  1220. */ 
  1221. protected function display_settings_errors() { 
  1222. global $wp_settings_errors; 
  1223.  
  1224. settings_errors( 'tgmpa' ); 
  1225.  
  1226. foreach ( (array) $wp_settings_errors as $key => $details ) { 
  1227. if ( 'tgmpa' === $details['setting'] ) { 
  1228. unset( $wp_settings_errors[ $key ] ); 
  1229. break; 
  1230.  
  1231. /** 
  1232. * Register dismissal of admin notices. 
  1233. * 
  1234. * Acts on the dismiss link in the admin nag messages. 
  1235. * If clicked, the admin notice disappears and will no longer be visible to this user. 
  1236. * 
  1237. * @since 2.1.0 
  1238. */ 
  1239. public function dismiss() { 
  1240. if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) { 
  1241. update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 ); 
  1242.  
  1243. /** 
  1244. * Add individual plugin to our collection of plugins. 
  1245. * 
  1246. * If the required keys are not set or the plugin has already 
  1247. * been registered, the plugin is not added. 
  1248. * 
  1249. * @since 2.0.0 
  1250. * 
  1251. * @param array|null $plugin Array of plugin arguments or null if invalid argument. 
  1252. * @return null Return early if incorrect argument. 
  1253. */ 
  1254. public function register( $plugin ) { 
  1255. if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) { 
  1256. return; 
  1257.  
  1258. if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) { 
  1259. return; 
  1260.  
  1261. $defaults = array( 
  1262. 'name' => '', // String 
  1263. 'slug' => '', // String 
  1264. 'source' => 'repo', // String 
  1265. 'required' => false, // Boolean 
  1266. 'version' => '', // String 
  1267. 'force_activation' => false, // Boolean 
  1268. 'force_deactivation' => false, // Boolean 
  1269. 'external_url' => '', // String 
  1270. 'is_callable' => '', // String|Array. 
  1271. ); 
  1272.  
  1273. // Prepare the received data. 
  1274. $plugin = wp_parse_args( $plugin, $defaults ); 
  1275.  
  1276. // Standardize the received slug. 
  1277. $plugin['slug'] = $this->sanitize_key( $plugin['slug'] ); 
  1278.  
  1279. // Forgive users for using string versions of booleans or floats for version number. 
  1280. $plugin['version'] = (string) $plugin['version']; 
  1281. $plugin['source'] = empty( $plugin['source'] ) ? 'repo' : $plugin['source']; 
  1282. $plugin['required'] = TGMPA_Utils::validate_bool( $plugin['required'] ); 
  1283. $plugin['force_activation'] = TGMPA_Utils::validate_bool( $plugin['force_activation'] ); 
  1284. $plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] ); 
  1285.  
  1286. // Enrich the received data. 
  1287. $plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] ); 
  1288. $plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] ); 
  1289.  
  1290. // Set the class properties. 
  1291. $this->plugins[ $plugin['slug'] ] = $plugin; 
  1292. $this->sort_order[ $plugin['slug'] ] = $plugin['name']; 
  1293.  
  1294. // Should we add the force activation hook ? 
  1295. if ( true === $plugin['force_activation'] ) { 
  1296. $this->has_forced_activation = true; 
  1297.  
  1298. // Should we add the force deactivation hook ? 
  1299. if ( true === $plugin['force_deactivation'] ) { 
  1300. $this->has_forced_deactivation = true; 
  1301.  
  1302. /** 
  1303. * Determine what type of source the plugin comes from. 
  1304. * 
  1305. * @since 2.5.0 
  1306. * 
  1307. * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path 
  1308. * (= bundled) or an external URL. 
  1309. * @return string 'repo', 'external', or 'bundled' 
  1310. */ 
  1311. protected function get_plugin_source_type( $source ) { 
  1312. if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) { 
  1313. return 'repo'; 
  1314. } elseif ( preg_match( self::IS_URL_REGEX, $source ) ) { 
  1315. return 'external'; 
  1316. } else { 
  1317. return 'bundled'; 
  1318.  
  1319. /** 
  1320. * Sanitizes a string key. 
  1321. * 
  1322. * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are* 
  1323. * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase 
  1324. * characters in the plugin directory path/slug. Silly them. 
  1325. * 
  1326. * @see https://developer.wordpress.org/reference/hooks/sanitize_key/ 
  1327. * 
  1328. * @since 2.5.0 
  1329. * 
  1330. * @param string $key String key. 
  1331. * @return string Sanitized key 
  1332. */ 
  1333. public function sanitize_key( $key ) { 
  1334. $raw_key = $key; 
  1335. $key = preg_replace( '`[^A-Za-z0-9_-]`', '', $key ); 
  1336.  
  1337. /** 
  1338. * Filter a sanitized key string. 
  1339. * 
  1340. * @since 2.5.0 
  1341. * 
  1342. * @param string $key Sanitized key. 
  1343. * @param string $raw_key The key prior to sanitization. 
  1344. */ 
  1345. return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key ); 
  1346.  
  1347. /** 
  1348. * Amend default configuration settings. 
  1349. * 
  1350. * @since 2.0.0 
  1351. * 
  1352. * @param array $config Array of config options to pass as class properties. 
  1353. */ 
  1354. public function config( $config ) { 
  1355. $keys = array( 
  1356. 'id',  
  1357. 'default_path',  
  1358. 'has_notices',  
  1359. 'dismissable',  
  1360. 'dismiss_msg',  
  1361. 'menu',  
  1362. 'parent_slug',  
  1363. 'capability',  
  1364. 'is_automatic',  
  1365. 'message',  
  1366. 'strings',  
  1367. ); 
  1368.  
  1369. foreach ( $keys as $key ) { 
  1370. if ( isset( $config[ $key ] ) ) { 
  1371. if ( is_array( $config[ $key ] ) ) { 
  1372. $this->$key = array_merge( $this->$key, $config[ $key ] ); 
  1373. } else { 
  1374. $this->$key = $config[ $key ]; 
  1375.  
  1376. /** 
  1377. * Amend action link after plugin installation. 
  1378. * 
  1379. * @since 2.0.0 
  1380. * 
  1381. * @param array $install_actions Existing array of actions. 
  1382. * @return false|array Amended array of actions. 
  1383. */ 
  1384. public function actions( $install_actions ) { 
  1385. // Remove action links on the TGMPA install page. 
  1386. if ( $this->is_tgmpa_page() ) { 
  1387. return false; 
  1388.  
  1389. return $install_actions; 
  1390.  
  1391. /** 
  1392. * Flushes the plugins cache on theme switch to prevent stale entries 
  1393. * from remaining in the plugin table. 
  1394. * 
  1395. * @since 2.4.0 
  1396. * 
  1397. * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache. 
  1398. * Parameter added in v2.5.0. 
  1399. */ 
  1400. public function flush_plugins_cache( $clear_update_cache = true ) { 
  1401. wp_clean_plugins_cache( $clear_update_cache ); 
  1402.  
  1403. /** 
  1404. * Set file_path key for each installed plugin. 
  1405. * 
  1406. * @since 2.1.0 
  1407. * 
  1408. * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin. 
  1409. * Parameter added in v2.5.0. 
  1410. */ 
  1411. public function populate_file_path( $plugin_slug = '' ) { 
  1412. if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) { 
  1413. $this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug ); 
  1414. } else { 
  1415. // Add file_path key for all plugins. 
  1416. foreach ( $this->plugins as $slug => $values ) { 
  1417. $this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug ); 
  1418.  
  1419. /** 
  1420. * Helper function to extract the file path of the plugin file from the 
  1421. * plugin slug, if the plugin is installed. 
  1422. * 
  1423. * @since 2.0.0 
  1424. * 
  1425. * @param string $slug Plugin slug (typically folder name) as provided by the developer. 
  1426. * @return string Either file path for plugin if installed, or just the plugin slug. 
  1427. */ 
  1428. protected function _get_plugin_basename_from_slug( $slug ) { 
  1429. $keys = array_keys( $this->get_plugins() ); 
  1430.  
  1431. foreach ( $keys as $key ) { 
  1432. if ( preg_match( '|^' . $slug . '/|', $key ) ) { 
  1433. return $key; 
  1434.  
  1435. return $slug; 
  1436.  
  1437. /** 
  1438. * Retrieve plugin data, given the plugin name. 
  1439. * 
  1440. * Loops through the registered plugins looking for $name. If it finds it,  
  1441. * it returns the $data from that plugin. Otherwise, returns false. 
  1442. * 
  1443. * @since 2.1.0 
  1444. * 
  1445. * @param string $name Name of the plugin, as it was registered. 
  1446. * @param string $data Optional. Array key of plugin data to return. Default is slug. 
  1447. * @return string|boolean Plugin slug if found, false otherwise. 
  1448. */ 
  1449. public function _get_plugin_data_from_name( $name, $data = 'slug' ) { 
  1450. foreach ( $this->plugins as $values ) { 
  1451. if ( $name === $values['name'] && isset( $values[ $data ] ) ) { 
  1452. return $values[ $data ]; 
  1453.  
  1454. return false; 
  1455.  
  1456. /** 
  1457. * Retrieve the download URL for a package. 
  1458. * 
  1459. * @since 2.5.0 
  1460. * 
  1461. * @param string $slug Plugin slug. 
  1462. * @return string Plugin download URL or path to local file or empty string if undetermined. 
  1463. */ 
  1464. public function get_download_url( $slug ) { 
  1465. $dl_source = ''; 
  1466.  
  1467. switch ( $this->plugins[ $slug ]['source_type'] ) { 
  1468. case 'repo': 
  1469. return $this->get_wp_repo_download_url( $slug ); 
  1470. case 'external': 
  1471. return $this->plugins[ $slug ]['source']; 
  1472. case 'bundled': 
  1473. return $this->default_path . $this->plugins[ $slug ]['source']; 
  1474.  
  1475. return $dl_source; // Should never happen. 
  1476.  
  1477. /** 
  1478. * Retrieve the download URL for a WP repo package. 
  1479. * 
  1480. * @since 2.5.0 
  1481. * 
  1482. * @param string $slug Plugin slug. 
  1483. * @return string Plugin download URL. 
  1484. */ 
  1485. protected function get_wp_repo_download_url( $slug ) { 
  1486. $source = ''; 
  1487. $api = $this->get_plugins_api( $slug ); 
  1488.  
  1489. if ( false !== $api && isset( $api->download_link ) ) { 
  1490. $source = $api->download_link; 
  1491.  
  1492. return $source; 
  1493.  
  1494. /** 
  1495. * Try to grab information from WordPress API. 
  1496. * 
  1497. * @since 2.5.0 
  1498. * 
  1499. * @param string $slug Plugin slug. 
  1500. * @return object Plugins_api response object on success, WP_Error on failure. 
  1501. */ 
  1502. protected function get_plugins_api( $slug ) { 
  1503. static $api = array(); // Cache received responses. 
  1504.  
  1505. if ( ! isset( $api[ $slug ] ) ) { 
  1506. if ( ! function_exists( 'plugins_api' ) ) { 
  1507. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; 
  1508.  
  1509. $response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); 
  1510.  
  1511. $api[ $slug ] = false; 
  1512.  
  1513. if ( is_wp_error( $response ) ) { 
  1514. wp_die( esc_html( $this->strings['oops'] ) ); 
  1515. } else { 
  1516. $api[ $slug ] = $response; 
  1517.  
  1518. return $api[ $slug ]; 
  1519.  
  1520. /** 
  1521. * Retrieve a link to a plugin information page. 
  1522. * 
  1523. * @since 2.5.0 
  1524. * 
  1525. * @param string $slug Plugin slug. 
  1526. * @return string Fully formed html link to a plugin information page if available 
  1527. * or the plugin name if not. 
  1528. */ 
  1529. public function get_info_link( $slug ) { 
  1530. if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) { 
  1531. $link = sprintf( 
  1532. '<a href="%1$s" target="_blank">%2$s</a>',  
  1533. esc_url( $this->plugins[ $slug ]['external_url'] ),  
  1534. esc_html( $this->plugins[ $slug ]['name'] ) 
  1535. ); 
  1536. } elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) { 
  1537. $url = add_query_arg( 
  1538. array( 
  1539. 'tab' => 'plugin-information',  
  1540. 'plugin' => urlencode( $slug ),  
  1541. 'TB_iframe' => 'true',  
  1542. 'width' => '640',  
  1543. 'height' => '500',  
  1544. ),  
  1545. self_admin_url( 'plugin-install.php' ) 
  1546. ); 
  1547.  
  1548. $link = sprintf( 
  1549. '<a href="%1$s" class="thickbox">%2$s</a>',  
  1550. esc_url( $url ),  
  1551. esc_html( $this->plugins[ $slug ]['name'] ) 
  1552. ); 
  1553. } else { 
  1554. $link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink. 
  1555.  
  1556. return $link; 
  1557.  
  1558. /** 
  1559. * Determine if we're on the TGMPA Install page. 
  1560. * 
  1561. * @since 2.1.0 
  1562. * 
  1563. * @return boolean True when on the TGMPA page, false otherwise. 
  1564. */ 
  1565. protected function is_tgmpa_page() { 
  1566. return isset( $_GET['page'] ) && $this->menu === $_GET['page']; 
  1567.  
  1568. /** 
  1569. * Determine if we're on a WP Core installation/upgrade page. 
  1570. * 
  1571. * @since 2.6.0 
  1572. * 
  1573. * @return boolean True when on a WP Core installation/upgrade page, false otherwise. 
  1574. */ 
  1575. protected function is_core_update_page() { 
  1576. // Current screen is not always available, most notably on the customizer screen. 
  1577. if ( ! function_exists( 'get_current_screen' ) ) { 
  1578. return false; 
  1579.  
  1580. $screen = get_current_screen(); 
  1581.  
  1582. if ( 'update-core' === $screen->base ) { 
  1583. // Core update screen. 
  1584. return true; 
  1585. } elseif ( 'plugins' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok. 
  1586. // Plugins bulk update screen. 
  1587. return true; 
  1588. } elseif ( 'update' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok. 
  1589. // Individual updates (ajax call). 
  1590. return true; 
  1591.  
  1592. return false; 
  1593.  
  1594. /** 
  1595. * Retrieve the URL to the TGMPA Install page. 
  1596. * 
  1597. * I.e. depending on the config settings passed something along the lines of: 
  1598. * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins 
  1599. * 
  1600. * @since 2.5.0 
  1601. * 
  1602. * @return string Properly encoded URL (not escaped). 
  1603. */ 
  1604. public function get_tgmpa_url() { 
  1605. static $url; 
  1606.  
  1607. if ( ! isset( $url ) ) { 
  1608. $parent = $this->parent_slug; 
  1609. if ( false === strpos( $parent, '.php' ) ) { 
  1610. $parent = 'admin.php'; 
  1611. $url = add_query_arg( 
  1612. array( 
  1613. 'page' => urlencode( $this->menu ),  
  1614. ),  
  1615. self_admin_url( $parent ) 
  1616. ); 
  1617.  
  1618. return $url; 
  1619.  
  1620. /** 
  1621. * Retrieve the URL to the TGMPA Install page for a specific plugin status (view). 
  1622. * 
  1623. * I.e. depending on the config settings passed something along the lines of: 
  1624. * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install 
  1625. * 
  1626. * @since 2.5.0 
  1627. * 
  1628. * @param string $status Plugin status - either 'install', 'update' or 'activate'. 
  1629. * @return string Properly encoded URL (not escaped). 
  1630. */ 
  1631. public function get_tgmpa_status_url( $status ) { 
  1632. return add_query_arg( 
  1633. array( 
  1634. 'plugin_status' => urlencode( $status ),  
  1635. ),  
  1636. $this->get_tgmpa_url() 
  1637. ); 
  1638.  
  1639. /** 
  1640. * Determine whether there are open actions for plugins registered with TGMPA. 
  1641. * 
  1642. * @since 2.5.0 
  1643. * 
  1644. * @return bool True if complete, i.e. no outstanding actions. False otherwise. 
  1645. */ 
  1646. public function is_tgmpa_complete() { 
  1647. $complete = true; 
  1648. foreach ( $this->plugins as $slug => $plugin ) { 
  1649. if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) { 
  1650. $complete = false; 
  1651. break; 
  1652.  
  1653. return $complete; 
  1654.  
  1655. /** 
  1656. * Check if a plugin is installed. Does not take must-use plugins into account. 
  1657. * 
  1658. * @since 2.5.0 
  1659. * 
  1660. * @param string $slug Plugin slug. 
  1661. * @return bool True if installed, false otherwise. 
  1662. */ 
  1663. public function is_plugin_installed( $slug ) { 
  1664. $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached). 
  1665.  
  1666. return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) ); 
  1667.  
  1668. /** 
  1669. * Check if a plugin is active. 
  1670. * 
  1671. * @since 2.5.0 
  1672. * 
  1673. * @param string $slug Plugin slug. 
  1674. * @return bool True if active, false otherwise. 
  1675. */ 
  1676. public function is_plugin_active( $slug ) { 
  1677. return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) ); 
  1678.  
  1679. /** 
  1680. * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required 
  1681. * available, check whether the current install meets them. 
  1682. * 
  1683. * @since 2.5.0 
  1684. * 
  1685. * @param string $slug Plugin slug. 
  1686. * @return bool True if OK to update, false otherwise. 
  1687. */ 
  1688. public function can_plugin_update( $slug ) { 
  1689. // We currently can't get reliable info on non-WP-repo plugins - issue #380. 
  1690. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { 
  1691. return true; 
  1692.  
  1693. $api = $this->get_plugins_api( $slug ); 
  1694.  
  1695. if ( false !== $api && isset( $api->requires ) ) { 
  1696. return version_compare( $this->wp_version, $api->requires, '>=' ); 
  1697.  
  1698. // No usable info received from the plugins API, presume we can update. 
  1699. return true; 
  1700.  
  1701. /** 
  1702. * Check to see if the plugin is 'updatetable', i.e. installed, with an update available 
  1703. * and no WP version requirements blocking it. 
  1704. * 
  1705. * @since 2.6.0 
  1706. * 
  1707. * @param string $slug Plugin slug. 
  1708. * @return bool True if OK to proceed with update, false otherwise. 
  1709. */ 
  1710. public function is_plugin_updatetable( $slug ) { 
  1711. if ( ! $this->is_plugin_installed( $slug ) ) { 
  1712. return false; 
  1713. } else { 
  1714. return ( false !== $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) ); 
  1715.  
  1716. /** 
  1717. * Check if a plugin can be activated, i.e. is not currently active and meets the minimum 
  1718. * plugin version requirements set in TGMPA (if any). 
  1719. * 
  1720. * @since 2.5.0 
  1721. * 
  1722. * @param string $slug Plugin slug. 
  1723. * @return bool True if OK to activate, false otherwise. 
  1724. */ 
  1725. public function can_plugin_activate( $slug ) { 
  1726. return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) ); 
  1727.  
  1728. /** 
  1729. * Retrieve the version number of an installed plugin. 
  1730. * 
  1731. * @since 2.5.0 
  1732. * 
  1733. * @param string $slug Plugin slug. 
  1734. * @return string Version number as string or an empty string if the plugin is not installed 
  1735. * or version unknown (plugins which don't comply with the plugin header standard). 
  1736. */ 
  1737. public function get_installed_version( $slug ) { 
  1738. $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached). 
  1739.  
  1740. if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) { 
  1741. return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version']; 
  1742.  
  1743. return ''; 
  1744.  
  1745. /** 
  1746. * Check whether a plugin complies with the minimum version requirements. 
  1747. * 
  1748. * @since 2.5.0 
  1749. * 
  1750. * @param string $slug Plugin slug. 
  1751. * @return bool True when a plugin needs to be updated, otherwise false. 
  1752. */ 
  1753. public function does_plugin_require_update( $slug ) { 
  1754. $installed_version = $this->get_installed_version( $slug ); 
  1755. $minimum_version = $this->plugins[ $slug ]['version']; 
  1756.  
  1757. return version_compare( $minimum_version, $installed_version, '>' ); 
  1758.  
  1759. /** 
  1760. * Check whether there is an update available for a plugin. 
  1761. * 
  1762. * @since 2.5.0 
  1763. * 
  1764. * @param string $slug Plugin slug. 
  1765. * @return false|string Version number string of the available update or false if no update available. 
  1766. */ 
  1767. public function does_plugin_have_update( $slug ) { 
  1768. // Presume bundled and external plugins will point to a package which meets the minimum required version. 
  1769. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { 
  1770. if ( $this->does_plugin_require_update( $slug ) ) { 
  1771. return $this->plugins[ $slug ]['version']; 
  1772.  
  1773. return false; 
  1774.  
  1775. $repo_updates = get_site_transient( 'update_plugins' ); 
  1776.  
  1777. if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) { 
  1778. return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version; 
  1779.  
  1780. return false; 
  1781.  
  1782. /** 
  1783. * Retrieve potential upgrade notice for a plugin. 
  1784. * 
  1785. * @since 2.5.0 
  1786. * 
  1787. * @param string $slug Plugin slug. 
  1788. * @return string The upgrade notice or an empty string if no message was available or provided. 
  1789. */ 
  1790. public function get_upgrade_notice( $slug ) { 
  1791. // We currently can't get reliable info on non-WP-repo plugins - issue #380. 
  1792. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { 
  1793. return ''; 
  1794.  
  1795. $repo_updates = get_site_transient( 'update_plugins' ); 
  1796.  
  1797. if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) { 
  1798. return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice; 
  1799.  
  1800. return ''; 
  1801.  
  1802. /** 
  1803. * Wrapper around the core WP get_plugins function, making sure it's actually available. 
  1804. * 
  1805. * @since 2.5.0 
  1806. * 
  1807. * @param string $plugin_folder Optional. Relative path to single plugin folder. 
  1808. * @return array Array of installed plugins with plugin information. 
  1809. */ 
  1810. public function get_plugins( $plugin_folder = '' ) { 
  1811. if ( ! function_exists( 'get_plugins' ) ) { 
  1812. require_once ABSPATH . 'wp-admin/includes/plugin.php'; 
  1813.  
  1814. return get_plugins( $plugin_folder ); 
  1815.  
  1816. /** 
  1817. * Delete dismissable nag option when theme is switched. 
  1818. * 
  1819. * This ensures that the user(s) is/are again reminded via nag of required 
  1820. * and/or recommended plugins if they re-activate the theme. 
  1821. * 
  1822. * @since 2.1.1 
  1823. */ 
  1824. public function update_dismiss() { 
  1825. delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true ); 
  1826.  
  1827. /** 
  1828. * Forces plugin activation if the parameter 'force_activation' is 
  1829. * set to true. 
  1830. * 
  1831. * This allows theme authors to specify certain plugins that must be 
  1832. * active at all times while using the current theme. 
  1833. * 
  1834. * Please take special care when using this parameter as it has the 
  1835. * potential to be harmful if not used correctly. Setting this parameter 
  1836. * to true will not allow the specified plugin to be deactivated unless 
  1837. * the user switches themes. 
  1838. * 
  1839. * @since 2.2.0 
  1840. */ 
  1841. public function force_activation() { 
  1842. foreach ( $this->plugins as $slug => $plugin ) { 
  1843. if ( true === $plugin['force_activation'] ) { 
  1844. if ( ! $this->is_plugin_installed( $slug ) ) { 
  1845. // Oops, plugin isn't there so iterate to next condition. 
  1846. continue; 
  1847. } elseif ( $this->can_plugin_activate( $slug ) ) { 
  1848. // There we go, activate the plugin. 
  1849. activate_plugin( $plugin['file_path'] ); 
  1850.  
  1851. /** 
  1852. * Forces plugin deactivation if the parameter 'force_deactivation' 
  1853. * is set to true and adds the plugin to the 'recently active' plugins list. 
  1854. * 
  1855. * This allows theme authors to specify certain plugins that must be 
  1856. * deactivated upon switching from the current theme to another. 
  1857. * 
  1858. * Please take special care when using this parameter as it has the 
  1859. * potential to be harmful if not used correctly. 
  1860. * 
  1861. * @since 2.2.0 
  1862. */ 
  1863. public function force_deactivation() { 
  1864. $deactivated = array(); 
  1865.  
  1866. foreach ( $this->plugins as $slug => $plugin ) { 
  1867. /** 
  1868. * Only proceed forward if the parameter is set to true and plugin is active 
  1869. * as a 'normal' (not must-use) plugin. 
  1870. */ 
  1871. if ( true === $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) ) { 
  1872. deactivate_plugins( $plugin['file_path'] ); 
  1873. $deactivated[ $plugin['file_path'] ] = time(); 
  1874.  
  1875. if ( ! empty( $deactivated ) ) { 
  1876. update_option( 'recently_activated', $deactivated + (array) get_option( 'recently_activated' ) ); 
  1877.  
  1878. /** 
  1879. * Echo the current TGMPA version number to the page. 
  1880. * 
  1881. * @since 2.5.0 
  1882. */ 
  1883. public function show_tgmpa_version() { 
  1884. echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',  
  1885. esc_html( 
  1886. sprintf( 
  1887. /** translators: %s: version number */ 
  1888. __( 'TGMPA v%s', 'tgmpa' ),  
  1889. self::TGMPA_VERSION 
  1890. ),  
  1891. '</small></strong></p>'; 
  1892.  
  1893. /** 
  1894. * Returns the singleton instance of the class. 
  1895. * 
  1896. * @since 2.4.0 
  1897. * 
  1898. * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object. 
  1899. */ 
  1900. public static function get_instance() { 
  1901. if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { 
  1902. self::$instance = new self(); 
  1903.  
  1904. return self::$instance; 
  1905.  
  1906. if ( ! function_exists( 'load_tgm_plugin_activation' ) ) { 
  1907. /** 
  1908. * Ensure only one instance of the class is ever invoked. 
  1909. * 
  1910. * @since 2.5.0 
  1911. */ 
  1912. function load_tgm_plugin_activation() { 
  1913. $GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance(); 
  1914.  
  1915. if ( did_action( 'plugins_loaded' ) ) { 
  1916. load_tgm_plugin_activation(); 
  1917. } else { 
  1918. add_action( 'plugins_loaded', 'load_tgm_plugin_activation' ); 
  1919.  
  1920. if ( ! function_exists( 'tgmpa' ) ) { 
  1921. /** 
  1922. * Helper function to register a collection of required plugins. 
  1923. * 
  1924. * @since 2.0.0 
  1925. * @api 
  1926. * 
  1927. * @param array $plugins An array of plugin arrays. 
  1928. * @param array $config Optional. An array of configuration values. 
  1929. */ 
  1930. function tgmpa( $plugins, $config = array() ) { 
  1931. $instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); 
  1932.  
  1933. foreach ( $plugins as $plugin ) { 
  1934. call_user_func( array( $instance, 'register' ), $plugin ); 
  1935.  
  1936. if ( ! empty( $config ) && is_array( $config ) ) { 
  1937. // Send out notices for deprecated arguments passed. 
  1938. if ( isset( $config['notices'] ) ) { 
  1939. _deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' ); 
  1940. if ( ! isset( $config['has_notices'] ) ) { 
  1941. $config['has_notices'] = $config['notices']; 
  1942.  
  1943. if ( isset( $config['parent_menu_slug'] ) ) { 
  1944. _deprecated_argument( __FUNCTION__, '2.4.0', 'The `parent_menu_slug` config parameter was removed in TGMPA 2.4.0. In TGMPA 2.5.0 an alternative was (re-)introduced. Please adjust your configuration. For more information visit the website: http://tgmpluginactivation.com/configuration/#h-configuration-options.' ); 
  1945. if ( isset( $config['parent_url_slug'] ) ) { 
  1946. _deprecated_argument( __FUNCTION__, '2.4.0', 'The `parent_url_slug` config parameter was removed in TGMPA 2.4.0. In TGMPA 2.5.0 an alternative was (re-)introduced. Please adjust your configuration. For more information visit the website: http://tgmpluginactivation.com/configuration/#h-configuration-options.' ); 
  1947.  
  1948. call_user_func( array( $instance, 'config' ), $config ); 
  1949.  
  1950. /** 
  1951. * WP_List_Table isn't always available. If it isn't available,  
  1952. * we load it here. 
  1953. * 
  1954. * @since 2.2.0 
  1955. */ 
  1956. if ( ! class_exists( 'WP_List_Table' ) ) { 
  1957. require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; 
  1958.  
  1959. if ( ! class_exists( 'TGMPA_List_Table' ) ) { 
  1960.  
  1961. /** 
  1962. * List table class for handling plugins. 
  1963. * 
  1964. * Extends the WP_List_Table class to provide a future-compatible 
  1965. * way of listing out all required/recommended plugins. 
  1966. * 
  1967. * Gives users an interface similar to the Plugin Administration 
  1968. * area with similar (albeit stripped down) capabilities. 
  1969. * 
  1970. * This class also allows for the bulk install of plugins. 
  1971. * 
  1972. * @since 2.2.0 
  1973. * 
  1974. * @package TGM-Plugin-Activation 
  1975. * @author Thomas Griffin 
  1976. * @author Gary Jones 
  1977. */ 
  1978. class TGMPA_List_Table extends WP_List_Table { 
  1979. /** 
  1980. * TGMPA instance. 
  1981. * 
  1982. * @since 2.5.0 
  1983. * 
  1984. * @var object 
  1985. */ 
  1986. protected $tgmpa; 
  1987.  
  1988. /** 
  1989. * The currently chosen view. 
  1990. * 
  1991. * @since 2.5.0 
  1992. * 
  1993. * @var string One of: 'all', 'install', 'update', 'activate' 
  1994. */ 
  1995. public $view_context = 'all'; 
  1996.  
  1997. /** 
  1998. * The plugin counts for the various views. 
  1999. * 
  2000. * @since 2.5.0 
  2001. * 
  2002. * @var array 
  2003. */ 
  2004. protected $view_totals = array( 
  2005. 'all' => 0,  
  2006. 'install' => 0,  
  2007. 'update' => 0,  
  2008. 'activate' => 0,  
  2009. ); 
  2010.  
  2011. /** 
  2012. * References parent constructor and sets defaults for class. 
  2013. * 
  2014. * @since 2.2.0 
  2015. */ 
  2016. public function __construct() { 
  2017. $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); 
  2018.  
  2019. parent::__construct( 
  2020. array( 
  2021. 'singular' => 'plugin',  
  2022. 'plural' => 'plugins',  
  2023. 'ajax' => false,  
  2024. ); 
  2025.  
  2026. if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) { 
  2027. $this->view_context = sanitize_key( $_REQUEST['plugin_status'] ); 
  2028.  
  2029. add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) ); 
  2030.  
  2031. /** 
  2032. * Get a list of CSS classes for the <table> tag. 
  2033. * 
  2034. * Overruled to prevent the 'plural' argument from being added. 
  2035. * 
  2036. * @since 2.5.0 
  2037. * 
  2038. * @return array CSS classnames. 
  2039. */ 
  2040. public function get_table_classes() { 
  2041. return array( 'widefat', 'fixed' ); 
  2042.  
  2043. /** 
  2044. * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table. 
  2045. * 
  2046. * @since 2.2.0 
  2047. * 
  2048. * @return array $table_data Information for use in table. 
  2049. */ 
  2050. protected function _gather_plugin_data() { 
  2051. // Load thickbox for plugin links. 
  2052. $this->tgmpa->admin_init(); 
  2053. $this->tgmpa->thickbox(); 
  2054.  
  2055. // Categorize the plugins which have open actions. 
  2056. $plugins = $this->categorize_plugins_to_views(); 
  2057.  
  2058. // Set the counts for the view links. 
  2059. $this->set_view_totals( $plugins ); 
  2060.  
  2061. // Prep variables for use and grab list of all installed plugins. 
  2062. $table_data = array(); 
  2063. $i = 0; 
  2064.  
  2065. // Redirect to the 'all' view if no plugins were found for the selected view context. 
  2066. if ( empty( $plugins[ $this->view_context ] ) ) { 
  2067. $this->view_context = 'all'; 
  2068.  
  2069. foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) { 
  2070. $table_data[ $i ]['sanitized_plugin'] = $plugin['name']; 
  2071. $table_data[ $i ]['slug'] = $slug; 
  2072. $table_data[ $i ]['plugin'] = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>'; 
  2073. $table_data[ $i ]['source'] = $this->get_plugin_source_type_text( $plugin['source_type'] ); 
  2074. $table_data[ $i ]['type'] = $this->get_plugin_advise_type_text( $plugin['required'] ); 
  2075. $table_data[ $i ]['status'] = $this->get_plugin_status_text( $slug ); 
  2076. $table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug ); 
  2077. $table_data[ $i ]['minimum_version'] = $plugin['version']; 
  2078. $table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug ); 
  2079.  
  2080. // Prep the upgrade notice info. 
  2081. $upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug ); 
  2082. if ( ! empty( $upgrade_notice ) ) { 
  2083. $table_data[ $i ]['upgrade_notice'] = $upgrade_notice; 
  2084.  
  2085. add_action( "tgmpa_after_plugin_row_{$slug}", array( $this, 'wp_plugin_update_row' ), 10, 2 ); 
  2086.  
  2087. $table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin ); 
  2088.  
  2089. $i++; 
  2090.  
  2091. return $table_data; 
  2092.  
  2093. /** 
  2094. * Categorize the plugins which have open actions into views for the TGMPA page. 
  2095. * 
  2096. * @since 2.5.0 
  2097. */ 
  2098. protected function categorize_plugins_to_views() { 
  2099. $plugins = array( 
  2100. 'all' => array(), // Meaning: all plugins which still have open actions. 
  2101. 'install' => array(),  
  2102. 'update' => array(),  
  2103. 'activate' => array(),  
  2104. ); 
  2105.  
  2106. foreach ( $this->tgmpa->plugins as $slug => $plugin ) { 
  2107. if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) { 
  2108. // No need to display plugins if they are installed, up-to-date and active. 
  2109. continue; 
  2110. } else { 
  2111. $plugins['all'][ $slug ] = $plugin; 
  2112.  
  2113. if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) { 
  2114. $plugins['install'][ $slug ] = $plugin; 
  2115. } else { 
  2116. if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) { 
  2117. $plugins['update'][ $slug ] = $plugin; 
  2118.  
  2119. if ( $this->tgmpa->can_plugin_activate( $slug ) ) { 
  2120. $plugins['activate'][ $slug ] = $plugin; 
  2121.  
  2122. return $plugins; 
  2123.  
  2124. /** 
  2125. * Set the counts for the view links. 
  2126. * 
  2127. * @since 2.5.0 
  2128. * 
  2129. * @param array $plugins Plugins order by view. 
  2130. */ 
  2131. protected function set_view_totals( $plugins ) { 
  2132. foreach ( $plugins as $type => $list ) { 
  2133. $this->view_totals[ $type ] = count( $list ); 
  2134.  
  2135. /** 
  2136. * Get the plugin required/recommended text string. 
  2137. * 
  2138. * @since 2.5.0 
  2139. * 
  2140. * @param string $required Plugin required setting. 
  2141. * @return string 
  2142. */ 
  2143. protected function get_plugin_advise_type_text( $required ) { 
  2144. if ( true === $required ) { 
  2145. return __( 'Required', 'tgmpa' ); 
  2146.  
  2147. return __( 'Recommended', 'tgmpa' ); 
  2148.  
  2149. /** 
  2150. * Get the plugin source type text string. 
  2151. * 
  2152. * @since 2.5.0 
  2153. * 
  2154. * @param string $type Plugin type. 
  2155. * @return string 
  2156. */ 
  2157. protected function get_plugin_source_type_text( $type ) { 
  2158. $string = ''; 
  2159.  
  2160. switch ( $type ) { 
  2161. case 'repo': 
  2162. $string = __( 'WordPress Repository', 'tgmpa' ); 
  2163. break; 
  2164. case 'external': 
  2165. $string = __( 'External Source', 'tgmpa' ); 
  2166. break; 
  2167. case 'bundled': 
  2168. $string = __( 'Pre-Packaged', 'tgmpa' ); 
  2169. break; 
  2170.  
  2171. return $string; 
  2172.  
  2173. /** 
  2174. * Determine the plugin status message. 
  2175. * 
  2176. * @since 2.5.0 
  2177. * 
  2178. * @param string $slug Plugin slug. 
  2179. * @return string 
  2180. */ 
  2181. protected function get_plugin_status_text( $slug ) { 
  2182. if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) { 
  2183. return __( 'Not Installed', 'tgmpa' ); 
  2184.  
  2185. if ( ! $this->tgmpa->is_plugin_active( $slug ) ) { 
  2186. $install_status = __( 'Installed But Not Activated', 'tgmpa' ); 
  2187. } else { 
  2188. $install_status = __( 'Active', 'tgmpa' ); 
  2189.  
  2190. $update_status = ''; 
  2191.  
  2192. if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) { 
  2193. $update_status = __( 'Required Update not Available', 'tgmpa' ); 
  2194.  
  2195. } elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) { 
  2196. $update_status = __( 'Requires Update', 'tgmpa' ); 
  2197.  
  2198. } elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) { 
  2199. $update_status = __( 'Update recommended', 'tgmpa' ); 
  2200.  
  2201. if ( '' === $update_status ) { 
  2202. return $install_status; 
  2203.  
  2204. return sprintf( 
  2205. /** translators: 1: install status, 2: update status */ 
  2206. _x( '%1$s, %2$s', 'Install/Update Status', 'tgmpa' ),  
  2207. $install_status,  
  2208. $update_status 
  2209. ); 
  2210.  
  2211. /** 
  2212. * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type. 
  2213. * 
  2214. * @since 2.5.0 
  2215. * 
  2216. * @param array $items Prepared table items. 
  2217. * @return array Sorted table items. 
  2218. */ 
  2219. public function sort_table_items( $items ) { 
  2220. $type = array(); 
  2221. $name = array(); 
  2222.  
  2223. foreach ( $items as $i => $plugin ) { 
  2224. $type[ $i ] = $plugin['type']; // Required / recommended. 
  2225. $name[ $i ] = $plugin['sanitized_plugin']; 
  2226.  
  2227. array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items ); 
  2228.  
  2229. return $items; 
  2230.  
  2231. /** 
  2232. * Get an associative array ( id => link ) of the views available on this table. 
  2233. * 
  2234. * @since 2.5.0 
  2235. * 
  2236. * @return array 
  2237. */ 
  2238. public function get_views() { 
  2239. $status_links = array(); 
  2240.  
  2241. foreach ( $this->view_totals as $type => $count ) { 
  2242. if ( $count < 1 ) { 
  2243. continue; 
  2244.  
  2245. switch ( $type ) { 
  2246. case 'all': 
  2247. /** translators: 1: number of plugins. */ 
  2248. $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' ); 
  2249. break; 
  2250. case 'install': 
  2251. /** translators: 1: number of plugins. */ 
  2252. $text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' ); 
  2253. break; 
  2254. case 'update': 
  2255. /** translators: 1: number of plugins. */ 
  2256. $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' ); 
  2257. break; 
  2258. case 'activate': 
  2259. /** translators: 1: number of plugins. */ 
  2260. $text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' ); 
  2261. break; 
  2262. default: 
  2263. $text = ''; 
  2264. break; 
  2265.  
  2266. if ( ! empty( $text ) ) { 
  2267.  
  2268. $status_links[ $type ] = sprintf( 
  2269. '<a href="%s"%s>%s</a>',  
  2270. esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),  
  2271. ( $type === $this->view_context ) ? ' class="current"' : '',  
  2272. sprintf( $text, number_format_i18n( $count ) ) 
  2273. ); 
  2274.  
  2275. return $status_links; 
  2276.  
  2277. /** 
  2278. * Create default columns to display important plugin information 
  2279. * like type, action and status. 
  2280. * 
  2281. * @since 2.2.0 
  2282. * 
  2283. * @param array $item Array of item data. 
  2284. * @param string $column_name The name of the column. 
  2285. * @return string 
  2286. */ 
  2287. public function column_default( $item, $column_name ) { 
  2288. return $item[ $column_name ]; 
  2289.  
  2290. /** 
  2291. * Required for bulk installing. 
  2292. * 
  2293. * Adds a checkbox for each plugin. 
  2294. * 
  2295. * @since 2.2.0 
  2296. * 
  2297. * @param array $item Array of item data. 
  2298. * @return string The input checkbox with all necessary info. 
  2299. */ 
  2300. public function column_cb( $item ) { 
  2301. return sprintf( 
  2302. '<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />',  
  2303. esc_attr( $this->_args['singular'] ),  
  2304. esc_attr( $item['slug'] ),  
  2305. esc_attr( $item['sanitized_plugin'] ) 
  2306. ); 
  2307.  
  2308. /** 
  2309. * Create default title column along with the action links. 
  2310. * 
  2311. * @since 2.2.0 
  2312. * 
  2313. * @param array $item Array of item data. 
  2314. * @return string The plugin name and action links. 
  2315. */ 
  2316. public function column_plugin( $item ) { 
  2317. return sprintf( 
  2318. '%1$s %2$s',  
  2319. $item['plugin'],  
  2320. $this->row_actions( $this->get_row_actions( $item ), true ) 
  2321. ); 
  2322.  
  2323. /** 
  2324. * Create version information column. 
  2325. * 
  2326. * @since 2.5.0 
  2327. * 
  2328. * @param array $item Array of item data. 
  2329. * @return string HTML-formatted version information. 
  2330. */ 
  2331. public function column_version( $item ) { 
  2332. $output = array(); 
  2333.  
  2334. if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) { 
  2335. $installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'tgmpa' ); 
  2336.  
  2337. $color = ''; 
  2338. if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) { 
  2339. $color = ' color: #ff0000; font-weight: bold;'; 
  2340.  
  2341. $output[] = sprintf( 
  2342. '<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'tgmpa' ) . '</p>',  
  2343. $color,  
  2344. $installed 
  2345. ); 
  2346.  
  2347. if ( ! empty( $item['minimum_version'] ) ) { 
  2348. $output[] = sprintf( 
  2349. '<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'tgmpa' ) . '</p>',  
  2350. $item['minimum_version'] 
  2351. ); 
  2352.  
  2353. if ( ! empty( $item['available_version'] ) ) { 
  2354. $color = ''; 
  2355. if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) { 
  2356. $color = ' color: #71C671; font-weight: bold;'; 
  2357.  
  2358. $output[] = sprintf( 
  2359. '<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'tgmpa' ) . '</p>',  
  2360. $color,  
  2361. $item['available_version'] 
  2362. ); 
  2363.  
  2364. if ( empty( $output ) ) { 
  2365. return ' '; // Let's not break the table layout. 
  2366. } else { 
  2367. return implode( "\n", $output ); 
  2368.  
  2369. /** 
  2370. * Sets default message within the plugins table if no plugins 
  2371. * are left for interaction. 
  2372. * 
  2373. * Hides the menu item to prevent the user from clicking and 
  2374. * getting a permissions error. 
  2375. * 
  2376. * @since 2.2.0 
  2377. */ 
  2378. public function no_items() { 
  2379. echo esc_html__( 'No plugins to install, update or activate.', 'tgmpa' ) . ' <a href="' . esc_url( self_admin_url() ) . '"> ' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>'; 
  2380. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; 
  2381.  
  2382. /** 
  2383. * Output all the column information within the table. 
  2384. * 
  2385. * @since 2.2.0 
  2386. * 
  2387. * @return array $columns The column names. 
  2388. */ 
  2389. public function get_columns() { 
  2390. $columns = array( 
  2391. 'cb' => '<input type="checkbox" />',  
  2392. 'plugin' => __( 'Plugin', 'tgmpa' ),  
  2393. 'source' => __( 'Source', 'tgmpa' ),  
  2394. 'type' => __( 'Type', 'tgmpa' ),  
  2395. ); 
  2396.  
  2397. if ( 'all' === $this->view_context || 'update' === $this->view_context ) { 
  2398. $columns['version'] = __( 'Version', 'tgmpa' ); 
  2399. $columns['status'] = __( 'Status', 'tgmpa' ); 
  2400.  
  2401. return apply_filters( 'tgmpa_table_columns', $columns ); 
  2402.  
  2403. /** 
  2404. * Get name of default primary column 
  2405. * 
  2406. * @since 2.5.0 / WP 4.3+ compatibility 
  2407. * @access protected 
  2408. * 
  2409. * @return string 
  2410. */ 
  2411. protected function get_default_primary_column_name() { 
  2412. return 'plugin'; 
  2413.  
  2414. /** 
  2415. * Get the name of the primary column. 
  2416. * 
  2417. * @since 2.5.0 / WP 4.3+ compatibility 
  2418. * @access protected 
  2419. * 
  2420. * @return string The name of the primary column. 
  2421. */ 
  2422. protected function get_primary_column_name() { 
  2423. if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) { 
  2424. return parent::get_primary_column_name(); 
  2425. } else { 
  2426. return $this->get_default_primary_column_name(); 
  2427.  
  2428. /** 
  2429. * Get the actions which are relevant for a specific plugin row. 
  2430. * 
  2431. * @since 2.5.0 
  2432. * 
  2433. * @param array $item Array of item data. 
  2434. * @return array Array with relevant action links. 
  2435. */ 
  2436. protected function get_row_actions( $item ) { 
  2437. $actions = array(); 
  2438. $action_links = array(); 
  2439.  
  2440. // Display the 'Install' action link if the plugin is not yet available. 
  2441. if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) { 
  2442. /** translators: %2$s: plugin name in screen reader markup */ 
  2443. $actions['install'] = __( 'Install %2$s', 'tgmpa' ); 
  2444. } else { 
  2445. // Display the 'Update' action link if an update is available and WP complies with plugin minimum. 
  2446. if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) { 
  2447. /** translators: %2$s: plugin name in screen reader markup */ 
  2448. $actions['update'] = __( 'Update %2$s', 'tgmpa' ); 
  2449.  
  2450. // Display the 'Activate' action link, but only if the plugin meets the minimum version. 
  2451. if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) { 
  2452. /** translators: %2$s: plugin name in screen reader markup */ 
  2453. $actions['activate'] = __( 'Activate %2$s', 'tgmpa' ); 
  2454.  
  2455. // Create the actual links. 
  2456. foreach ( $actions as $action => $text ) { 
  2457. $nonce_url = wp_nonce_url( 
  2458. add_query_arg( 
  2459. array( 
  2460. 'plugin' => urlencode( $item['slug'] ),  
  2461. 'tgmpa-' . $action => $action . '-plugin',  
  2462. ),  
  2463. $this->tgmpa->get_tgmpa_url() 
  2464. ),  
  2465. 'tgmpa-' . $action,  
  2466. 'tgmpa-nonce' 
  2467. ); 
  2468.  
  2469. $action_links[ $action ] = sprintf( 
  2470. '<a href="%1$s">' . esc_html( $text ) . '</a>', // $text contains the second placeholder. 
  2471. esc_url( $nonce_url ),  
  2472. '<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>' 
  2473. ); 
  2474.  
  2475. $prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : ''; 
  2476. return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context ); 
  2477.  
  2478. /** 
  2479. * Generates content for a single row of the table. 
  2480. * 
  2481. * @since 2.5.0 
  2482. * 
  2483. * @param object $item The current item. 
  2484. */ 
  2485. public function single_row( $item ) { 
  2486. parent::single_row( $item ); 
  2487.  
  2488. /** 
  2489. * Fires after each specific row in the TGMPA Plugins list table. 
  2490. * 
  2491. * The dynamic portion of the hook name, `$item['slug']`, refers to the slug 
  2492. * for the plugin. 
  2493. * 
  2494. * @since 2.5.0 
  2495. */ 
  2496. do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context ); 
  2497.  
  2498. /** 
  2499. * Show the upgrade notice below a plugin row if there is one. 
  2500. * 
  2501. * @since 2.5.0 
  2502. * 
  2503. * @see /wp-admin/includes/update.php 
  2504. * 
  2505. * @param string $slug Plugin slug. 
  2506. * @param array $item The information available in this table row. 
  2507. * @return null Return early if upgrade notice is empty. 
  2508. */ 
  2509. public function wp_plugin_update_row( $slug, $item ) { 
  2510. if ( empty( $item['upgrade_notice'] ) ) { 
  2511. return; 
  2512.  
  2513. echo ' 
  2514. <tr class="plugin-update-tr"> 
  2515. <td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange"> 
  2516. <div class="update-message">',  
  2517. esc_html__( 'Upgrade message from the plugin author:', 'tgmpa' ),  
  2518. ' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong> 
  2519. </div> 
  2520. </td> 
  2521. </tr>'; 
  2522.  
  2523. /** 
  2524. * Extra controls to be displayed between bulk actions and pagination. 
  2525. * 
  2526. * @since 2.5.0 
  2527. * 
  2528. * @param string $which 'top' or 'bottom' table navigation. 
  2529. */ 
  2530. public function extra_tablenav( $which ) { 
  2531. if ( 'bottom' === $which ) { 
  2532. $this->tgmpa->show_tgmpa_version(); 
  2533.  
  2534. /** 
  2535. * Defines the bulk actions for handling registered plugins. 
  2536. * 
  2537. * @since 2.2.0 
  2538. * 
  2539. * @return array $actions The bulk actions for the plugin install table. 
  2540. */ 
  2541. public function get_bulk_actions() { 
  2542.  
  2543. $actions = array(); 
  2544.  
  2545. if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) { 
  2546. if ( current_user_can( 'install_plugins' ) ) { 
  2547. $actions['tgmpa-bulk-install'] = __( 'Install', 'tgmpa' ); 
  2548.  
  2549. if ( 'install' !== $this->view_context ) { 
  2550. if ( current_user_can( 'update_plugins' ) ) { 
  2551. $actions['tgmpa-bulk-update'] = __( 'Update', 'tgmpa' ); 
  2552. if ( current_user_can( 'activate_plugins' ) ) { 
  2553. $actions['tgmpa-bulk-activate'] = __( 'Activate', 'tgmpa' ); 
  2554.  
  2555. return $actions; 
  2556.  
  2557. /** 
  2558. * Processes bulk installation and activation actions. 
  2559. * 
  2560. * The bulk installation process looks for the $_POST information and passes that 
  2561. * through if a user has to use WP_Filesystem to enter their credentials. 
  2562. * 
  2563. * @since 2.2.0 
  2564. */ 
  2565. public function process_bulk_actions() { 
  2566. // Bulk installation process. 
  2567. if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) { 
  2568.  
  2569. check_admin_referer( 'bulk-' . $this->_args['plural'] ); 
  2570.  
  2571. $install_type = 'install'; 
  2572. if ( 'tgmpa-bulk-update' === $this->current_action() ) { 
  2573. $install_type = 'update'; 
  2574.  
  2575. $plugins_to_install = array(); 
  2576.  
  2577. // Did user actually select any plugins to install/update ? 
  2578. if ( empty( $_POST['plugin'] ) ) { 
  2579. if ( 'install' === $install_type ) { 
  2580. $message = __( 'No plugins were selected to be installed. No action taken.', 'tgmpa' ); 
  2581. } else { 
  2582. $message = __( 'No plugins were selected to be updated. No action taken.', 'tgmpa' ); 
  2583.  
  2584. echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>'; 
  2585.  
  2586. return false; 
  2587.  
  2588. if ( is_array( $_POST['plugin'] ) ) { 
  2589. $plugins_to_install = (array) $_POST['plugin']; 
  2590. } elseif ( is_string( $_POST['plugin'] ) ) { 
  2591. // Received via Filesystem page - un-flatten array (WP bug #19643). 
  2592. $plugins_to_install = explode( ', ', $_POST['plugin'] ); 
  2593.  
  2594. // Sanitize the received input. 
  2595. $plugins_to_install = array_map( 'urldecode', $plugins_to_install ); 
  2596. $plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install ); 
  2597.  
  2598. // Validate the received input. 
  2599. foreach ( $plugins_to_install as $key => $slug ) { 
  2600. // Check if the plugin was registered with TGMPA and remove if not. 
  2601. if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) { 
  2602. unset( $plugins_to_install[ $key ] ); 
  2603. continue; 
  2604.  
  2605. // For install: make sure this is a plugin we *can* install and not one already installed. 
  2606. if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) { 
  2607. unset( $plugins_to_install[ $key ] ); 
  2608.  
  2609. // For updates: make sure this is a plugin we *can* update (update available and WP version ok). 
  2610. if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) { 
  2611. unset( $plugins_to_install[ $key ] ); 
  2612.  
  2613. // No need to proceed further if we have no plugins to handle. 
  2614. if ( empty( $plugins_to_install ) ) { 
  2615. if ( 'install' === $install_type ) { 
  2616. $message = __( 'No plugins are available to be installed at this time.', 'tgmpa' ); 
  2617. } else { 
  2618. $message = __( 'No plugins are available to be updated at this time.', 'tgmpa' ); 
  2619.  
  2620. echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>'; 
  2621.  
  2622. return false; 
  2623.  
  2624. // Pass all necessary information if WP_Filesystem is needed. 
  2625. $url = wp_nonce_url( 
  2626. $this->tgmpa->get_tgmpa_url(),  
  2627. 'bulk-' . $this->_args['plural'] 
  2628. ); 
  2629.  
  2630. // Give validated data back to $_POST which is the only place the filesystem looks for extra fields. 
  2631. $_POST['plugin'] = implode( ', ', $plugins_to_install ); // Work around for WP bug #19643. 
  2632.  
  2633. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary. 
  2634. $fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem. 
  2635.  
  2636. if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) { 
  2637. return true; // Stop the normal page form from displaying, credential request form will be shown. 
  2638.  
  2639. // Now we have some credentials, setup WP_Filesystem. 
  2640. if ( ! WP_Filesystem( $creds ) ) { 
  2641. // Our credentials were no good, ask the user for them again. 
  2642. request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields ); 
  2643.  
  2644. return true; 
  2645.  
  2646. /** If we arrive here, we have the filesystem */ 
  2647.  
  2648. // Store all information in arrays since we are processing a bulk installation. 
  2649. $names = array(); 
  2650. $sources = array(); // Needed for installs. 
  2651. $file_paths = array(); // Needed for upgrades. 
  2652. $to_inject = array(); // Information to inject into the update_plugins transient. 
  2653.  
  2654. // Prepare the data for validated plugins for the install/upgrade. 
  2655. foreach ( $plugins_to_install as $slug ) { 
  2656. $name = $this->tgmpa->plugins[ $slug ]['name']; 
  2657. $source = $this->tgmpa->get_download_url( $slug ); 
  2658.  
  2659. if ( ! empty( $name ) && ! empty( $source ) ) { 
  2660. $names[] = $name; 
  2661.  
  2662. switch ( $install_type ) { 
  2663.  
  2664. case 'install': 
  2665. $sources[] = $source; 
  2666. break; 
  2667.  
  2668. case 'update': 
  2669. $file_paths[] = $this->tgmpa->plugins[ $slug ]['file_path']; 
  2670. $to_inject[ $slug ] = $this->tgmpa->plugins[ $slug ]; 
  2671. $to_inject[ $slug ]['source'] = $source; 
  2672. break; 
  2673. unset( $slug, $name, $source ); 
  2674.  
  2675. // Create a new instance of TGMPA_Bulk_Installer. 
  2676. $installer = new TGMPA_Bulk_Installer( 
  2677. new TGMPA_Bulk_Installer_Skin( 
  2678. array( 
  2679. 'url' => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),  
  2680. 'nonce' => 'bulk-' . $this->_args['plural'],  
  2681. 'names' => $names,  
  2682. 'install_type' => $install_type,  
  2683. ); 
  2684.  
  2685. // Wrap the install process with the appropriate HTML. 
  2686. echo '<div class="tgmpa">',  
  2687. '<h2 style="font-size: 23px; font-weight: 400; line-height: 29px; margin: 0; padding: 9px 15px 4px 0;">', esc_html( get_admin_page_title() ), '</h2> 
  2688. <div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">'; 
  2689.  
  2690. // Process the bulk installation submissions. 
  2691. add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 ); 
  2692.  
  2693. if ( 'tgmpa-bulk-update' === $this->current_action() ) { 
  2694. // Inject our info into the update transient. 
  2695. $this->tgmpa->inject_update_info( $to_inject ); 
  2696.  
  2697. $installer->bulk_upgrade( $file_paths ); 
  2698. } else { 
  2699. $installer->bulk_install( $sources ); 
  2700.  
  2701. remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1 ); 
  2702.  
  2703. echo '</div></div>'; 
  2704.  
  2705. return true; 
  2706.  
  2707. // Bulk activation process. 
  2708. if ( 'tgmpa-bulk-activate' === $this->current_action() ) { 
  2709. check_admin_referer( 'bulk-' . $this->_args['plural'] ); 
  2710.  
  2711. // Did user actually select any plugins to activate ? 
  2712. if ( empty( $_POST['plugin'] ) ) { 
  2713. echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>'; 
  2714.  
  2715. return false; 
  2716.  
  2717. // Grab plugin data from $_POST. 
  2718. $plugins = array(); 
  2719. if ( isset( $_POST['plugin'] ) ) { 
  2720. $plugins = array_map( 'urldecode', (array) $_POST['plugin'] ); 
  2721. $plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins ); 
  2722.  
  2723. $plugins_to_activate = array(); 
  2724. $plugin_names = array(); 
  2725.  
  2726. // Grab the file paths for the selected & inactive plugins from the registration array. 
  2727. foreach ( $plugins as $slug ) { 
  2728. if ( $this->tgmpa->can_plugin_activate( $slug ) ) { 
  2729. $plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path']; 
  2730. $plugin_names[] = $this->tgmpa->plugins[ $slug ]['name']; 
  2731. unset( $slug ); 
  2732.  
  2733. // Return early if there are no plugins to activate. 
  2734. if ( empty( $plugins_to_activate ) ) { 
  2735. echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>'; 
  2736.  
  2737. return false; 
  2738.  
  2739. // Now we are good to go - let's start activating plugins. 
  2740. $activate = activate_plugins( $plugins_to_activate ); 
  2741.  
  2742. if ( is_wp_error( $activate ) ) { 
  2743. echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>'; 
  2744. } else { 
  2745. $count = count( $plugin_names ); // Count so we can use _n function. 
  2746. $plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names ); 
  2747. $last_plugin = array_pop( $plugin_names ); // Pop off last name to prep for readability. 
  2748. $imploded = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin ); 
  2749.  
  2750. printf( // WPCS: xss ok. 
  2751. '<div id="message" class="updated"><p>%1$s %2$s.</p></div>',  
  2752. esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),  
  2753. $imploded 
  2754. ); 
  2755.  
  2756. // Update recently activated plugins option. 
  2757. $recent = (array) get_option( 'recently_activated' ); 
  2758. foreach ( $plugins_to_activate as $plugin => $time ) { 
  2759. if ( isset( $recent[ $plugin ] ) ) { 
  2760. unset( $recent[ $plugin ] ); 
  2761. update_option( 'recently_activated', $recent ); 
  2762.  
  2763. unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another. 
  2764.  
  2765. return true; 
  2766.  
  2767. return false; 
  2768.  
  2769. /** 
  2770. * Prepares all of our information to be outputted into a usable table. 
  2771. * 
  2772. * @since 2.2.0 
  2773. */ 
  2774. public function prepare_items() { 
  2775. $columns = $this->get_columns(); // Get all necessary column information. 
  2776. $hidden = array(); // No columns to hide, but we must set as an array. 
  2777. $sortable = array(); // No reason to make sortable columns. 
  2778. $primary = $this->get_primary_column_name(); // Column which has the row actions. 
  2779. $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers. 
  2780.  
  2781. // Process our bulk activations here. 
  2782. if ( 'tgmpa-bulk-activate' === $this->current_action() ) { 
  2783. $this->process_bulk_actions(); 
  2784.  
  2785. // Store all of our plugin data into $items array so WP_List_Table can use it. 
  2786. $this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() ); 
  2787.  
  2788. /** *********** DEPRECATED METHODS *********** */ 
  2789.  
  2790. /** 
  2791. * Retrieve plugin data, given the plugin name. 
  2792. * 
  2793. * @since 2.2.0 
  2794. * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead. 
  2795. * @see TGM_Plugin_Activation::_get_plugin_data_from_name() 
  2796. * 
  2797. * @param string $name Name of the plugin, as it was registered. 
  2798. * @param string $data Optional. Array key of plugin data to return. Default is slug. 
  2799. * @return string|boolean Plugin slug if found, false otherwise. 
  2800. */ 
  2801. protected function _get_plugin_data_from_name( $name, $data = 'slug' ) { 
  2802. _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' ); 
  2803.  
  2804. return $this->tgmpa->_get_plugin_data_from_name( $name, $data ); 
  2805.  
  2806.  
  2807. if ( ! class_exists( 'TGM_Bulk_Installer' ) ) { 
  2808.  
  2809. /** 
  2810. * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+. 
  2811. * 
  2812. * @since 2.5.2 
  2813. * 
  2814. * {@internal The TGMPA_Bulk_Installer class was originally called TGM_Bulk_Installer. 
  2815. * For more information, see that class.}} 
  2816. */ 
  2817. class TGM_Bulk_Installer { 
  2818. if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) { 
  2819.  
  2820. /** 
  2821. * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+. 
  2822. * 
  2823. * @since 2.5.2 
  2824. * 
  2825. * {@internal The TGMPA_Bulk_Installer_Skin class was originally called TGM_Bulk_Installer_Skin. 
  2826. * For more information, see that class.}} 
  2827. */ 
  2828. class TGM_Bulk_Installer_Skin { 
  2829.  
  2830. /** 
  2831. * The WP_Upgrader file isn't always available. If it isn't available,  
  2832. * we load it here. 
  2833. * 
  2834. * We check to make sure no action or activation keys are set so that WordPress 
  2835. * does not try to re-include the class when processing upgrades or installs outside 
  2836. * of the class. 
  2837. * 
  2838. * @since 2.2.0 
  2839. */ 
  2840. add_action( 'admin_init', 'tgmpa_load_bulk_installer' ); 
  2841. if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) { 
  2842. /** 
  2843. * Load bulk installer 
  2844. */ 
  2845. function tgmpa_load_bulk_installer() { 
  2846. // Silently fail if 2.5+ is loaded *after* an older version. 
  2847. if ( ! isset( $GLOBALS['tgmpa'] ) ) { 
  2848. return; 
  2849.  
  2850. // Get TGMPA class instance. 
  2851. $tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); 
  2852.  
  2853. if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) { 
  2854. if ( ! class_exists( 'Plugin_Upgrader', false ) ) { 
  2855. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
  2856.  
  2857. if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) { 
  2858.  
  2859. /** 
  2860. * Installer class to handle bulk plugin installations. 
  2861. * 
  2862. * Extends WP_Upgrader and customizes to suit the installation of multiple 
  2863. * plugins. 
  2864. * 
  2865. * @since 2.2.0 
  2866. * 
  2867. * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}} 
  2868. * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer. 
  2869. * This was done to prevent backward compatibility issues with v2.3.6.}} 
  2870. * 
  2871. * @package TGM-Plugin-Activation 
  2872. * @author Thomas Griffin 
  2873. * @author Gary Jones 
  2874. */ 
  2875. class TGMPA_Bulk_Installer extends Plugin_Upgrader { 
  2876. /** 
  2877. * Holds result of bulk plugin installation. 
  2878. * 
  2879. * @since 2.2.0 
  2880. * 
  2881. * @var string 
  2882. */ 
  2883. public $result; 
  2884.  
  2885. /** 
  2886. * Flag to check if bulk installation is occurring or not. 
  2887. * 
  2888. * @since 2.2.0 
  2889. * 
  2890. * @var boolean 
  2891. */ 
  2892. public $bulk = false; 
  2893.  
  2894. /** 
  2895. * TGMPA instance 
  2896. * 
  2897. * @since 2.5.0 
  2898. * 
  2899. * @var object 
  2900. */ 
  2901. protected $tgmpa; 
  2902.  
  2903. /** 
  2904. * Whether or not the destination directory needs to be cleared ( = on update). 
  2905. * 
  2906. * @since 2.5.0 
  2907. * 
  2908. * @var bool 
  2909. */ 
  2910. protected $clear_destination = false; 
  2911.  
  2912. /** 
  2913. * References parent constructor and sets defaults for class. 
  2914. * 
  2915. * @since 2.2.0 
  2916. * 
  2917. * @param \Bulk_Upgrader_Skin|null $skin Installer skin. 
  2918. */ 
  2919. public function __construct( $skin = null ) { 
  2920. // Get TGMPA class instance. 
  2921. $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); 
  2922.  
  2923. parent::__construct( $skin ); 
  2924.  
  2925. if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) { 
  2926. $this->clear_destination = true; 
  2927.  
  2928. if ( $this->tgmpa->is_automatic ) { 
  2929. $this->activate_strings(); 
  2930.  
  2931. add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) ); 
  2932.  
  2933. /** 
  2934. * Sets the correct activation strings for the installer skin to use. 
  2935. * 
  2936. * @since 2.2.0 
  2937. */ 
  2938. public function activate_strings() { 
  2939. $this->strings['activation_failed'] = __( 'Plugin activation failed.', 'tgmpa' ); 
  2940. $this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' ); 
  2941.  
  2942. /** 
  2943. * Performs the actual installation of each plugin. 
  2944. * 
  2945. * @since 2.2.0 
  2946. * 
  2947. * @see WP_Upgrader::run() 
  2948. * 
  2949. * @param array $options The installation config options. 
  2950. * @return null|array Return early if error, array of installation data on success. 
  2951. */ 
  2952. public function run( $options ) { 
  2953. $result = parent::run( $options ); 
  2954.  
  2955. // Reset the strings in case we changed one during automatic activation. 
  2956. if ( $this->tgmpa->is_automatic ) { 
  2957. if ( 'update' === $this->skin->options['install_type'] ) { 
  2958. $this->upgrade_strings(); 
  2959. } else { 
  2960. $this->install_strings(); 
  2961.  
  2962. return $result; 
  2963.  
  2964. /** 
  2965. * Processes the bulk installation of plugins. 
  2966. * 
  2967. * @since 2.2.0 
  2968. * 
  2969. * {@internal This is basically a near identical copy of the WP Core 
  2970. * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with 
  2971. * new installs instead of upgrades. 
  2972. * For ease of future synchronizations, the adjustments are clearly commented, but no other 
  2973. * comments are added. Code style has been made to comply.}} 
  2974. * 
  2975. * @see Plugin_Upgrader::bulk_upgrade() 
  2976. * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838 
  2977. * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}} 
  2978. * 
  2979. * @param array $plugins The plugin sources needed for installation. 
  2980. * @param array $args Arbitrary passed extra arguments. 
  2981. * @return array|false Install confirmation messages on success, false on failure. 
  2982. */ 
  2983. public function bulk_install( $plugins, $args = array() ) { 
  2984. // [TGMPA + ] Hook auto-activation in. 
  2985. add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); 
  2986.  
  2987. $defaults = array( 
  2988. 'clear_update_cache' => true,  
  2989. ); 
  2990. $parsed_args = wp_parse_args( $args, $defaults ); 
  2991.  
  2992. $this->init(); 
  2993. $this->bulk = true; 
  2994.  
  2995. $this->install_strings(); // [TGMPA + ] adjusted. 
  2996.  
  2997. /** [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */ 
  2998.  
  2999. /** [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */ 
  3000.  
  3001. $this->skin->header(); 
  3002.  
  3003. // Connect to the Filesystem first. 
  3004. $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) ); 
  3005. if ( ! $res ) { 
  3006. $this->skin->footer(); 
  3007. return false; 
  3008.  
  3009. $this->skin->bulk_header(); 
  3010.  
  3011. /** 
  3012. * Only start maintenance mode if: 
  3013. * - running Multisite and there are one or more plugins specified, OR 
  3014. * - a plugin with an update available is currently active. 
  3015. * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible. 
  3016. */ 
  3017. $maintenance = ( is_multisite() && ! empty( $plugins ) ); 
  3018.  
  3019. /** 
  3020. [TGMPA - ] 
  3021. foreach ( $plugins as $plugin ) 
  3022. $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) ); 
  3023. */ 
  3024. if ( $maintenance ) { 
  3025. $this->maintenance_mode( true ); 
  3026.  
  3027. $results = array(); 
  3028.  
  3029. $this->update_count = count( $plugins ); 
  3030. $this->update_current = 0; 
  3031. foreach ( $plugins as $plugin ) { 
  3032. $this->update_current++; 
  3033.  
  3034. /** 
  3035. [TGMPA - ] 
  3036. $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true); 
  3037.   
  3038. if ( !isset( $current->response[ $plugin ] ) ) { 
  3039. $this->skin->set_result('up_to_date'); 
  3040. $this->skin->before(); 
  3041. $this->skin->feedback('up_to_date'); 
  3042. $this->skin->after(); 
  3043. $results[$plugin] = true; 
  3044. continue; 
  3045. } 
  3046.   
  3047. // Get the URL to the zip file. 
  3048. $r = $current->response[ $plugin ]; 
  3049.   
  3050. $this->skin->plugin_active = is_plugin_active($plugin); 
  3051. */ 
  3052.  
  3053. $result = $this->run( 
  3054. array( 
  3055. 'package' => $plugin, // [TGMPA + ] adjusted. 
  3056. 'destination' => WP_PLUGIN_DIR,  
  3057. 'clear_destination' => false, // [TGMPA + ] adjusted. 
  3058. 'clear_working' => true,  
  3059. 'is_multi' => true,  
  3060. 'hook_extra' => array( 
  3061. 'plugin' => $plugin,  
  3062. ),  
  3063. ); 
  3064.  
  3065. $results[ $plugin ] = $this->result; 
  3066.  
  3067. // Prevent credentials auth screen from displaying multiple times. 
  3068. if ( false === $result ) { 
  3069. break; 
  3070. } //end foreach $plugins 
  3071.  
  3072. $this->maintenance_mode( false ); 
  3073.  
  3074. /** 
  3075. * Fires when the bulk upgrader process is complete. 
  3076. * 
  3077. * @since WP 3.6.0 / TGMPA 2.5.0 
  3078. * 
  3079. * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might 
  3080. * be a Theme_Upgrader or Core_Upgrade instance. 
  3081. * @param array $data { 
  3082. * Array of bulk item update data. 
  3083. * 
  3084. * @type string $action Type of action. Default 'update'. 
  3085. * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'. 
  3086. * @type bool $bulk Whether the update process is a bulk update. Default true. 
  3087. * @type array $packages Array of plugin, theme, or core packages to update. 
  3088. * } 
  3089. */ 
  3090. do_action( 'upgrader_process_complete', $this, array( 
  3091. 'action' => 'install', // [TGMPA + ] adjusted. 
  3092. 'type' => 'plugin',  
  3093. 'bulk' => true,  
  3094. 'plugins' => $plugins,  
  3095. ) ); 
  3096.  
  3097. $this->skin->bulk_footer(); 
  3098.  
  3099. $this->skin->footer(); 
  3100.  
  3101. // Cleanup our hooks, in case something else does a upgrade on this connection. 
  3102. /** [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */ 
  3103.  
  3104. // [TGMPA + ] Remove our auto-activation hook. 
  3105. remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); 
  3106.  
  3107. // Force refresh of plugin update information. 
  3108. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] ); 
  3109.  
  3110. return $results; 
  3111.  
  3112. /** 
  3113. * Handle a bulk upgrade request. 
  3114. * 
  3115. * @since 2.5.0 
  3116. * 
  3117. * @see Plugin_Upgrader::bulk_upgrade() 
  3118. * 
  3119. * @param array $plugins The local WP file_path's of the plugins which should be upgraded. 
  3120. * @param array $args Arbitrary passed extra arguments. 
  3121. * @return string|bool Install confirmation messages on success, false on failure. 
  3122. */ 
  3123. public function bulk_upgrade( $plugins, $args = array() ) { 
  3124.  
  3125. add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); 
  3126.  
  3127. $result = parent::bulk_upgrade( $plugins, $args ); 
  3128.  
  3129. remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); 
  3130.  
  3131. return $result; 
  3132.  
  3133. /** 
  3134. * Abuse a filter to auto-activate plugins after installation. 
  3135. * 
  3136. * Hooked into the 'upgrader_post_install' filter hook. 
  3137. * 
  3138. * @since 2.5.0 
  3139. * 
  3140. * @param bool $bool The value we need to give back (true). 
  3141. * @return bool 
  3142. */ 
  3143. public function auto_activate( $bool ) { 
  3144. // Only process the activation of installed plugins if the automatic flag is set to true. 
  3145. if ( $this->tgmpa->is_automatic ) { 
  3146. // Flush plugins cache so the headers of the newly installed plugins will be read correctly. 
  3147. wp_clean_plugins_cache(); 
  3148.  
  3149. // Get the installed plugin file. 
  3150. $plugin_info = $this->plugin_info(); 
  3151.  
  3152. // Don't try to activate on upgrade of active plugin as WP will do this already. 
  3153. if ( ! is_plugin_active( $plugin_info ) ) { 
  3154. $activate = activate_plugin( $plugin_info ); 
  3155.  
  3156. // Adjust the success string based on the activation result. 
  3157. $this->strings['process_success'] = $this->strings['process_success'] . "<br />\n"; 
  3158.  
  3159. if ( is_wp_error( $activate ) ) { 
  3160. $this->skin->error( $activate ); 
  3161. $this->strings['process_success'] .= $this->strings['activation_failed']; 
  3162. } else { 
  3163. $this->strings['process_success'] .= $this->strings['activation_success']; 
  3164.  
  3165. return $bool; 
  3166.  
  3167. if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) { 
  3168.  
  3169. /** 
  3170. * Installer skin to set strings for the bulk plugin installations.. 
  3171. * 
  3172. * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple 
  3173. * plugins. 
  3174. * 
  3175. * @since 2.2.0 
  3176. * 
  3177. * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to 
  3178. * TGMPA_Bulk_Installer_Skin. 
  3179. * This was done to prevent backward compatibility issues with v2.3.6.}} 
  3180. * 
  3181. * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php 
  3182. * 
  3183. * @package TGM-Plugin-Activation 
  3184. * @author Thomas Griffin 
  3185. * @author Gary Jones 
  3186. */ 
  3187. class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin { 
  3188. /** 
  3189. * Holds plugin info for each individual plugin installation. 
  3190. * 
  3191. * @since 2.2.0 
  3192. * 
  3193. * @var array 
  3194. */ 
  3195. public $plugin_info = array(); 
  3196.  
  3197. /** 
  3198. * Holds names of plugins that are undergoing bulk installations. 
  3199. * 
  3200. * @since 2.2.0 
  3201. * 
  3202. * @var array 
  3203. */ 
  3204. public $plugin_names = array(); 
  3205.  
  3206. /** 
  3207. * Integer to use for iteration through each plugin installation. 
  3208. * 
  3209. * @since 2.2.0 
  3210. * 
  3211. * @var integer 
  3212. */ 
  3213. public $i = 0; 
  3214.  
  3215. /** 
  3216. * TGMPA instance 
  3217. * 
  3218. * @since 2.5.0 
  3219. * 
  3220. * @var object 
  3221. */ 
  3222. protected $tgmpa; 
  3223.  
  3224. /** 
  3225. * Constructor. Parses default args with new ones and extracts them for use. 
  3226. * 
  3227. * @since 2.2.0 
  3228. * 
  3229. * @param array $args Arguments to pass for use within the class. 
  3230. */ 
  3231. public function __construct( $args = array() ) { 
  3232. // Get TGMPA class instance. 
  3233. $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); 
  3234.  
  3235. // Parse default and new args. 
  3236. $defaults = array( 
  3237. 'url' => '',  
  3238. 'nonce' => '',  
  3239. 'names' => array(),  
  3240. 'install_type' => 'install',  
  3241. ); 
  3242. $args = wp_parse_args( $args, $defaults ); 
  3243.  
  3244. // Set plugin names to $this->plugin_names property. 
  3245. $this->plugin_names = $args['names']; 
  3246.  
  3247. // Extract the new args. 
  3248. parent::__construct( $args ); 
  3249.  
  3250. /** 
  3251. * Sets install skin strings for each individual plugin. 
  3252. * 
  3253. * Checks to see if the automatic activation flag is set and uses the 
  3254. * the proper strings accordingly. 
  3255. * 
  3256. * @since 2.2.0 
  3257. */ 
  3258. public function add_strings() { 
  3259. if ( 'update' === $this->options['install_type'] ) { 
  3260. parent::add_strings(); 
  3261. /** translators: 1: plugin name, 2: action number 3: total number of actions. */ 
  3262. $this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' ); 
  3263. } else { 
  3264. /** translators: 1: plugin name, 2: error message. */ 
  3265. $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' ); 
  3266. /** translators: 1: plugin name. */ 
  3267. $this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'tgmpa' ); 
  3268.  
  3269. if ( $this->tgmpa->is_automatic ) { 
  3270. // Automatic activation strings. 
  3271. $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation and activation process is starting. This process may take a while on some hosts, so please be patient.', 'tgmpa' ); 
  3272. /** translators: 1: plugin name. */ 
  3273. $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>'; 
  3274. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', 'tgmpa' ); 
  3275. /** translators: 1: plugin name, 2: action number 3: total number of actions. */ 
  3276. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' ); 
  3277. } else { 
  3278. // Default installation strings. 
  3279. $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation process is starting. This process may take a while on some hosts, so please be patient.', 'tgmpa' ); 
  3280. /** translators: 1: plugin name. */ 
  3281. $this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>'; 
  3282. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations have been completed.', 'tgmpa' ); 
  3283. /** translators: 1: plugin name, 2: action number 3: total number of actions. */ 
  3284. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' ); 
  3285.  
  3286. /** 
  3287. * Outputs the header strings and necessary JS before each plugin installation. 
  3288. * 
  3289. * @since 2.2.0 
  3290. * 
  3291. * @param string $title Unused in this implementation. 
  3292. */ 
  3293. public function before( $title = '' ) { 
  3294. if ( empty( $title ) ) { 
  3295. $title = esc_html( $this->plugin_names[ $this->i ] ); 
  3296. parent::before( $title ); 
  3297.  
  3298. /** 
  3299. * Outputs the footer strings and necessary JS after each plugin installation. 
  3300. * 
  3301. * Checks for any errors and outputs them if they exist, else output 
  3302. * success strings. 
  3303. * 
  3304. * @since 2.2.0 
  3305. * 
  3306. * @param string $title Unused in this implementation. 
  3307. */ 
  3308. public function after( $title = '' ) { 
  3309. if ( empty( $title ) ) { 
  3310. $title = esc_html( $this->plugin_names[ $this->i ] ); 
  3311. parent::after( $title ); 
  3312.  
  3313. $this->i++; 
  3314.  
  3315. /** 
  3316. * Outputs links after bulk plugin installation is complete. 
  3317. * 
  3318. * @since 2.2.0 
  3319. */ 
  3320. public function bulk_footer() { 
  3321. // Serve up the string to say installations (and possibly activations) are complete. 
  3322. parent::bulk_footer(); 
  3323.  
  3324. // Flush plugins cache so we can make sure that the installed plugins list is always up to date. 
  3325. wp_clean_plugins_cache(); 
  3326.  
  3327. $this->tgmpa->show_tgmpa_version(); 
  3328.  
  3329. // Display message based on if all plugins are now active or not. 
  3330. $update_actions = array(); 
  3331.  
  3332. if ( $this->tgmpa->is_tgmpa_complete() ) { 
  3333. // All plugins are active, so we display the complete string and hide the menu to protect users. 
  3334. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; 
  3335. $update_actions['dashboard'] = sprintf( 
  3336. esc_html( $this->tgmpa->strings['complete'] ),  
  3337. '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' 
  3338. ); 
  3339. } else { 
  3340. $update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>'; 
  3341.  
  3342. /** 
  3343. * Filter the list of action links available following bulk plugin installs/updates. 
  3344. * 
  3345. * @since 2.5.0 
  3346. * 
  3347. * @param array $update_actions Array of plugin action links. 
  3348. * @param array $plugin_info Array of information for the last-handled plugin. 
  3349. */ 
  3350. $update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info ); 
  3351.  
  3352. if ( ! empty( $update_actions ) ) { 
  3353. $this->feedback( implode( ' | ', (array) $update_actions ) ); 
  3354.  
  3355. /** *********** DEPRECATED METHODS *********** */ 
  3356.  
  3357. /** 
  3358. * Flush header output buffer. 
  3359. * 
  3360. * @since 2.2.0 
  3361. * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead 
  3362. * @see Bulk_Upgrader_Skin::flush_output() 
  3363. */ 
  3364. public function before_flush_output() { 
  3365. _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' ); 
  3366. $this->flush_output(); 
  3367.  
  3368. /** 
  3369. * Flush footer output buffer and iterate $this->i to make sure the 
  3370. * installation strings reference the correct plugin. 
  3371. * 
  3372. * @since 2.2.0 
  3373. * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead 
  3374. * @see Bulk_Upgrader_Skin::flush_output() 
  3375. */ 
  3376. public function after_flush_output() { 
  3377. _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' ); 
  3378. $this->flush_output(); 
  3379. $this->i++; 
  3380.  
  3381. if ( ! class_exists( 'TGMPA_Utils' ) ) { 
  3382.  
  3383. /** 
  3384. * Generic utilities for TGMPA. 
  3385. * 
  3386. * All methods are static, poor-dev name-spacing class wrapper. 
  3387. * 
  3388. * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy. 
  3389. * 
  3390. * @since 2.5.0 
  3391. * 
  3392. * @package TGM-Plugin-Activation 
  3393. * @author Juliette Reinders Folmer 
  3394. */ 
  3395. class TGMPA_Utils { 
  3396. /** 
  3397. * Whether the PHP filter extension is enabled. 
  3398. * 
  3399. * @see http://php.net/book.filter 
  3400. * 
  3401. * @since 2.5.0 
  3402. * 
  3403. * @static 
  3404. * 
  3405. * @var bool $has_filters True is the extension is enabled. 
  3406. */ 
  3407. public static $has_filters; 
  3408.  
  3409. /** 
  3410. * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map(). 
  3411. * 
  3412. * @since 2.5.0 
  3413. * 
  3414. * @static 
  3415. * 
  3416. * @param string $string Text to be wrapped. 
  3417. * @return string 
  3418. */ 
  3419. public static function wrap_in_em( $string ) { 
  3420. return '<em>' . wp_kses_post( $string ) . '</em>'; 
  3421.  
  3422. /** 
  3423. * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map(). 
  3424. * 
  3425. * @since 2.5.0 
  3426. * 
  3427. * @static 
  3428. * 
  3429. * @param string $string Text to be wrapped. 
  3430. * @return string 
  3431. */ 
  3432. public static function wrap_in_strong( $string ) { 
  3433. return '<strong>' . wp_kses_post( $string ) . '</strong>'; 
  3434.  
  3435. /** 
  3436. * Helper function: Validate a value as boolean 
  3437. * 
  3438. * @since 2.5.0 
  3439. * 
  3440. * @static 
  3441. * 
  3442. * @param mixed $value Arbitrary value. 
  3443. * @return bool 
  3444. */ 
  3445. public static function validate_bool( $value ) { 
  3446. if ( ! isset( self::$has_filters ) ) { 
  3447. self::$has_filters = extension_loaded( 'filter' ); 
  3448.  
  3449. if ( self::$has_filters ) { 
  3450. return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); 
  3451. } else { 
  3452. return self::emulate_filter_bool( $value ); 
  3453.  
  3454. /** 
  3455. * Helper function: Cast a value to bool 
  3456. * 
  3457. * @since 2.5.0 
  3458. * 
  3459. * @static 
  3460. * 
  3461. * @param mixed $value Value to cast. 
  3462. * @return bool 
  3463. */ 
  3464. protected static function emulate_filter_bool( $value ) { 
  3465. // @codingStandardsIgnoreStart 
  3466. static $true = array( 
  3467. '1',  
  3468. 'true', 'True', 'TRUE',  
  3469. 'y', 'Y',  
  3470. 'yes', 'Yes', 'YES',  
  3471. 'on', 'On', 'ON',  
  3472. ); 
  3473. static $false = array( 
  3474. '0',  
  3475. 'false', 'False', 'FALSE',  
  3476. 'n', 'N',  
  3477. 'no', 'No', 'NO',  
  3478. 'off', 'Off', 'OFF',  
  3479. ); 
  3480. // @codingStandardsIgnoreEnd 
  3481.  
  3482. if ( is_bool( $value ) ) { 
  3483. return $value; 
  3484. } elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) { 
  3485. return (bool) $value; 
  3486. } elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) { 
  3487. return (bool) $value; 
  3488. } elseif ( is_string( $value ) ) { 
  3489. $value = trim( $value ); 
  3490. if ( in_array( $value, $true, true ) ) { 
  3491. return true; 
  3492. } elseif ( in_array( $value, $false, true ) ) { 
  3493. return false; 
  3494. } else { 
  3495. return false; 
  3496.  
  3497. return false; 
  3498. } // End of class TGMPA_Utils 
  3499. } // End of class_exists wrapper 
.