TGM_Plugin_Activation

Automatic plugin installation and activation library.

Defined (1)

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

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