PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/lifeline/framework/library/tgm-plugin-activation/class-tgm-plugin-activation.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 2093 lines | 975 code | 350 blank | 768 comment | 181 complexity | 8a0d78a282bb060a16052b63c0b344c3 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Plugin installation and activation for WordPress themes.
  4. *
  5. * @package TGM-Plugin-Activation
  6. * @version 2.3.6
  7. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  8. * @author Gary Jones <gamajo@gamajo.com>
  9. * @copyright Copyright (c) 2012, Thomas Griffin
  10. * @license http://opensource.org/licenses/gpl-2.0.php GPL v2 or later
  11. * @link https://github.com/thomasgriffin/TGM-Plugin-Activation
  12. */
  13. /*
  14. Copyright 2012 Thomas Griffin (email : thomas@thomasgriffinmedia.com)
  15. This program is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License, version 3, as
  17. published by the Free Software Foundation.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. if ( ! class_exists( 'TGM_Plugin_Activation' ) ) {
  27. /**
  28. * Automatic plugin installation and activation library.
  29. *
  30. * Creates a way to automatically install and activate plugins from within themes.
  31. * The plugins can be either pre-packaged, downloaded from the WordPress
  32. * Plugin Repository or downloaded from a private repository.
  33. *
  34. * @since 1.0.0
  35. *
  36. * @package TGM-Plugin-Activation
  37. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  38. * @author Gary Jones <gamajo@gamajo.com>
  39. */
  40. class TGM_Plugin_Activation {
  41. /**
  42. * Holds a copy of itself, so it can be referenced by the class name.
  43. *
  44. * @since 1.0.0
  45. *
  46. * @var TGM_Plugin_Activation
  47. */
  48. static $instance;
  49. /**
  50. * Holds arrays of plugin details.
  51. *
  52. * @since 1.0.0
  53. *
  54. * @var array
  55. */
  56. public $plugins = array();
  57. /**
  58. * Parent menu slug for plugins page.
  59. *
  60. * @since 2.2.0
  61. *
  62. * @var string Parent menu slug. Defaults to 'themes.php'.
  63. */
  64. public $parent_menu_slug = 'themes.php';
  65. /**
  66. * Parent URL slug for URL references.
  67. *
  68. * This is useful if you want to place the custom plugins page as a
  69. * submenu item under a custom parent menu.
  70. *
  71. * @since 2.2.0
  72. *
  73. * @var string Parent URL slug. Defaults to 'themes.php'.
  74. */
  75. public $parent_url_slug = 'themes.php';
  76. /**
  77. * Name of the querystring argument for the admin page.
  78. *
  79. * @since 1.0.0
  80. *
  81. * @var string
  82. */
  83. public $menu = 'install-required-plugins';
  84. /**
  85. * Text domain for localization support.
  86. *
  87. * @since 1.1.0
  88. *
  89. * @var string
  90. */
  91. public $domain = 'tgmpa';
  92. /**
  93. * Default absolute path to folder containing pre-packaged plugin zip files.
  94. *
  95. * @since 2.0.0
  96. *
  97. * @var string Absolute path prefix to packaged zip file location. Default is empty string.
  98. */
  99. public $default_path = '';
  100. /**
  101. * Flag to show admin notices or not.
  102. *
  103. * @since 2.1.0
  104. *
  105. * @var boolean
  106. */
  107. public $has_notices = true;
  108. /**
  109. * Flag to set automatic activation of plugins. Off by default.
  110. *
  111. * @since 2.2.0
  112. *
  113. * @var boolean
  114. */
  115. public $is_automatic = false;
  116. /**
  117. * Optional message to display before the plugins table.
  118. *
  119. * @since 2.2.0
  120. *
  121. * @var string Message filtered by wp_kses_post(). Default is empty string.
  122. */
  123. public $message = '';
  124. /**
  125. * Holds configurable array of strings.
  126. *
  127. * Default values are added in the constructor.
  128. *
  129. * @since 2.0.0
  130. *
  131. * @var array
  132. */
  133. public $strings = array();
  134. /**
  135. * Adds a reference of this object to $instance, populates default strings,
  136. * does the tgmpa_init action hook, and hooks in the interactions to init.
  137. *
  138. * @since 1.0.0
  139. *
  140. * @see TGM_Plugin_Activation::init()
  141. */
  142. public function __construct() {
  143. self::$instance =& $this;
  144. $this->strings = array(
  145. 'page_title' => __( 'Install Required Plugins', SH_NAME ),
  146. 'menu_title' => __( 'Install Plugins', SH_NAME ),
  147. 'installing' => __( 'Installing Plugin: %s', SH_NAME ),
  148. 'oops' => __( 'Something went wrong.', SH_NAME ),
  149. 'notice_can_install_required' => _n_noop( 'This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.' ),
  150. 'notice_can_install_recommended' => _n_noop( 'This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.' ),
  151. 'notice_cannot_install' => _n_noop( 'Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', 'Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.' ),
  152. 'notice_can_activate_required' => _n_noop( 'The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.' ),
  153. 'notice_can_activate_recommended' => _n_noop( 'The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.' ),
  154. 'notice_cannot_activate' => _n_noop( 'Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.' ),
  155. 'notice_ask_to_update' => _n_noop( 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.' ),
  156. 'notice_cannot_update' => _n_noop( 'Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.' ),
  157. 'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins' ),
  158. 'activate_link' => _n_noop( 'Activate installed plugin', 'Activate installed plugins' ),
  159. 'return' => __( 'Return to Required Plugins Installer', SH_NAME ),
  160. 'plugin_activated' => __( 'Plugin activated successfully.', SH_NAME ),
  161. 'complete' => __( 'All plugins installed and activated successfully. %1$s', SH_NAME ),
  162. );
  163. /** Annouce that the class is ready, and pass the object (for advanced use) */
  164. do_action_ref_array( 'tgmpa_init', array( &$this ) );
  165. /** When the rest of WP has loaded, kick-start the rest of the class */
  166. add_action( 'init', array( &$this, 'init' ) );
  167. }
  168. /**
  169. * Initialise the interactions between this class and WordPress.
  170. *
  171. * Hooks in three new methods for the class: admin_menu, notices and styles.
  172. *
  173. * @since 2.0.0
  174. *
  175. * @see TGM_Plugin_Activation::admin_menu()
  176. * @see TGM_Plugin_Activation::notices()
  177. * @see TGM_Plugin_Activation::styles()
  178. */
  179. public function init() {
  180. do_action( 'tgmpa_register' );
  181. /** After this point, the plugins should be registered and the configuration set */
  182. /** Proceed only if we have plugins to handle */
  183. if ( $this->plugins ) {
  184. $sorted = array(); // Prepare variable for sorting
  185. foreach ( $this->plugins as $plugin )
  186. $sorted[] = $plugin['name'];
  187. array_multisort( $sorted, SORT_ASC, $this->plugins ); // Sort plugins alphabetically by name
  188. add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
  189. add_action( 'admin_head', array( &$this, 'dismiss' ) );
  190. add_filter( 'install_plugin_complete_actions', array( &$this, 'actions' ) );
  191. /** Load admin bar in the header to remove flash when installing plugins */
  192. if ( $this->is_tgmpa_page() ) {
  193. remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
  194. remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
  195. add_action( 'wp_head', 'wp_admin_bar_render', 1000 );
  196. add_action( 'admin_head', 'wp_admin_bar_render', 1000 );
  197. }
  198. if ( $this->has_notices ) {
  199. add_action( 'admin_notices', array( &$this, 'notices' ) );
  200. add_action( 'admin_init', array( &$this, 'admin_init' ), 1 );
  201. add_action( 'admin_enqueue_scripts', array( &$this, 'thickbox' ) );
  202. add_action( 'switch_theme', array( &$this, 'update_dismiss' ) );
  203. }
  204. /** Setup the force activation hook */
  205. foreach ( $this->plugins as $plugin ) {
  206. if ( isset( $plugin['force_activation'] ) && true === $plugin['force_activation'] ) {
  207. add_action( 'admin_init', array( &$this, 'force_activation' ) );
  208. break;
  209. }
  210. }
  211. /** Setup the force deactivation hook */
  212. foreach ( $this->plugins as $plugin ) {
  213. if ( isset( $plugin['force_deactivation'] ) && true === $plugin['force_deactivation'] ) {
  214. add_action( 'switch_theme', array( &$this, 'force_deactivation' ) );
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * Handles calls to show plugin information via links in the notices.
  222. *
  223. * We get the links in the admin notices to point to the TGMPA page, rather
  224. * than the typical plugin-install.php file, so we can prepare everything
  225. * beforehand.
  226. *
  227. * WP doesn't make it easy to show the plugin information in the thickbox -
  228. * here we have to require a file that includes a function that does the
  229. * main work of displaying it, enqueue some styles, set up some globals and
  230. * finally call that function before exiting.
  231. *
  232. * Down right easy once you know how...
  233. *
  234. * @since 2.1.0
  235. *
  236. * @global string $tab Used as iframe div class names, helps with styling
  237. * @global string $body_id Used as the iframe body ID, helps with styling
  238. * @return null Returns early if not the TGMPA page.
  239. */
  240. public function admin_init() {
  241. if ( ! $this->is_tgmpa_page() )
  242. return;
  243. if ( isset( $_REQUEST['tab'] ) && 'plugin-information' == $_REQUEST['tab'] ) {
  244. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for install_plugin_information()
  245. wp_enqueue_style( 'plugin-install' );
  246. global $tab, $body_id;
  247. $body_id = $tab = 'plugin-information';
  248. install_plugin_information();
  249. exit;
  250. }
  251. }
  252. /**
  253. * Enqueues thickbox scripts/styles for plugin info.
  254. *
  255. * Thickbox is not automatically included on all admin pages, so we must
  256. * manually enqueue it for those pages.
  257. *
  258. * Thickbox is only loaded if the user has not dismissed the admin
  259. * notice or if there are any plugins left to install and activate.
  260. *
  261. * @since 2.1.0
  262. */
  263. public function thickbox() {
  264. if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice', true ) )
  265. add_thickbox();
  266. }
  267. /**
  268. * Adds submenu page under 'Appearance' tab.
  269. *
  270. * This method adds the submenu page letting users know that a required
  271. * plugin needs to be installed.
  272. *
  273. * This page disappears once the plugin has been installed and activated.
  274. *
  275. * @since 1.0.0
  276. *
  277. * @see TGM_Plugin_Activation::init()
  278. * @see TGM_Plugin_Activation::install_plugins_page()
  279. */
  280. public function admin_menu() {
  281. // Make sure privileges are correct to see the page
  282. if ( ! current_user_can( 'install_plugins' ) )
  283. return;
  284. $this->populate_file_path();
  285. foreach ( $this->plugins as $plugin ) {
  286. if ( ! is_plugin_active( $plugin['file_path'] ) ) {
  287. add_theme_page(
  288. $this->strings['page_title'], // Page title
  289. $this->strings['menu_title'], // Menu title
  290. 'edit_theme_options', // Capability
  291. $this->menu, // Menu slug
  292. array( &$this, 'install_plugins_page' ) // Callback
  293. );
  294. break;
  295. }
  296. }
  297. }
  298. /**
  299. * Echoes plugin installation form.
  300. *
  301. * This method is the callback for the admin_menu method function.
  302. * This displays the admin page and form area where the user can select to install and activate the plugin.
  303. *
  304. * @since 1.0.0
  305. *
  306. * @return null Aborts early if we're processing a plugin installation action
  307. */
  308. public function install_plugins_page() {
  309. /** Store new instance of plugin table in object */
  310. $plugin_table = new TGMPA_List_Table;
  311. /** Return early if processing a plugin installation action */
  312. if ( isset( $_POST[sanitize_key( 'action' )] ) && 'tgmpa-bulk-install' == $_POST[sanitize_key( 'action' )] && $plugin_table->process_bulk_actions() || $this->do_plugin_install() )
  313. return;
  314. ?>
  315. <div class="tgmpa wrap">
  316. <?php screen_icon( apply_filters( 'tgmpa_default_screen_icon', 'themes' ) ); ?>
  317. <h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
  318. <?php $plugin_table->prepare_items(); ?>
  319. <?php if ( isset( $this->message ) ) echo wp_kses_post( $this->message ); ?>
  320. <form id="tgmpa-plugins" action="" method="post">
  321. <input type="hidden" name="tgmpa-page" value="<?php echo $this->menu; ?>" />
  322. <?php $plugin_table->display(); ?>
  323. </form>
  324. </div>
  325. <?php
  326. }
  327. /**
  328. * Installs a plugin or activates a plugin depending on the hover
  329. * link clicked by the user.
  330. *
  331. * Checks the $_GET variable to see which actions have been
  332. * passed and responds with the appropriate method.
  333. *
  334. * Uses WP_Filesystem to process and handle the plugin installation
  335. * method.
  336. *
  337. * @since 1.0.0
  338. *
  339. * @uses WP_Filesystem
  340. * @uses WP_Error
  341. * @uses WP_Upgrader
  342. * @uses Plugin_Upgrader
  343. * @uses Plugin_Installer_Skin
  344. *
  345. * @return boolean True on success, false on failure
  346. */
  347. protected function do_plugin_install() {
  348. /** All plugin information will be stored in an array for processing */
  349. $plugin = array();
  350. /** Checks for actions from hover links to process the installation */
  351. if ( isset( $_GET[sanitize_key( 'plugin' )] ) && ( isset( $_GET[sanitize_key( 'tgmpa-install' )] ) && 'install-plugin' == $_GET[sanitize_key( 'tgmpa-install' )] ) ) {
  352. check_admin_referer( 'tgmpa-install' );
  353. $plugin['name'] = $_GET[sanitize_key( 'plugin_name' )]; // Plugin name
  354. $plugin['slug'] = $_GET[sanitize_key( 'plugin' )]; // Plugin slug
  355. $plugin['source'] = $_GET[sanitize_key( 'plugin_source' )]; // Plugin source
  356. /** Pass all necessary information via URL if WP_Filesystem is needed */
  357. $url = wp_nonce_url(
  358. add_query_arg(
  359. array(
  360. 'page' => $this->menu,
  361. 'plugin' => $plugin['slug'],
  362. 'plugin_name' => $plugin['name'],
  363. 'plugin_source' => $plugin['source'],
  364. 'tgmpa-install' => 'install-plugin',
  365. ),
  366. admin_url( $this->parent_url_slug )
  367. ),
  368. 'tgmpa-install'
  369. );
  370. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary
  371. $fields = array( sanitize_key( 'tgmpa-install' ) ); // Extra fields to pass to WP_Filesystem
  372. if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, $fields ) ) )
  373. return true;
  374. if ( ! WP_Filesystem( $creds ) ) {
  375. request_filesystem_credentials( $url, $method, true, false, $fields ); // Setup WP_Filesystem
  376. return true;
  377. }
  378. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for plugins_api
  379. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes
  380. /** Set plugin source to WordPress API link if available */
  381. if ( isset( $plugin['source'] ) && 'repo' == $plugin['source'] ) {
  382. $api = plugins_api( 'plugin_information', array( 'slug' => $plugin['slug'], 'fields' => array( 'sections' => false ) ) );
  383. if ( is_wp_error( $api ) )
  384. wp_die( $this->strings['oops'] . var_dump( $api ) );
  385. if ( isset( $api->download_link ) )
  386. $plugin['source'] = $api->download_link;
  387. }
  388. /** Set type, based on whether the source starts with http:// or https:// */
  389. $type = preg_match( '|^http(s)?://|', $plugin['source'] ) ? 'web' : 'upload';
  390. /** Prep variables for Plugin_Installer_Skin class */
  391. $title = sprintf( $this->strings['installing'], $plugin['name'] );
  392. $url = add_query_arg( array( 'action' => 'install-plugin', 'plugin' => $plugin['slug'] ), 'update.php' );
  393. if ( isset( $_GET['from'] ) )
  394. $url .= add_query_arg( 'from', urlencode( stripslashes( $_GET['from'] ) ), $url );
  395. $nonce = 'install-plugin_' . $plugin['slug'];
  396. /** Prefix a default path to pre-packaged plugins */
  397. $source = ( 'upload' == $type ) ? $this->default_path . $plugin['source'] : $plugin['source'];
  398. /** Create a new instance of Plugin_Upgrader */
  399. $upgrader = new Plugin_Upgrader( $skin = new Plugin_Installer_Skin( compact( 'type', 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
  400. /** Perform the action and install the plugin from the $source urldecode() */
  401. $upgrader->install( $source );
  402. /** Flush plugins cache so we can make sure that the installed plugins list is always up to date */
  403. wp_cache_flush();
  404. /** Only activate plugins if the config option is set to true */
  405. if ( $this->is_automatic ) {
  406. $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method
  407. $activate = activate_plugin( $plugin_activate ); // Activate the plugin
  408. $this->populate_file_path(); // Re-populate the file path now that the plugin has been installed and activated
  409. if ( is_wp_error( $activate ) ) {
  410. echo '<div id="message" class="error"><p>' . $activate->get_error_message() . '</p></div>';
  411. echo '<p><a href="' . add_query_arg( 'page', $this->menu, admin_url( $this->parent_url_slug ) ) . '" title="' . esc_attr( $this->strings['return'] ) . '" target="_parent">' . __( 'Return to Required Plugins Installer', SH_NAME ) . '</a></p>';
  412. return true; // End it here if there is an error with automatic activation
  413. }
  414. else {
  415. echo '<p>' . $this->strings['plugin_activated'] . '</p>';
  416. }
  417. }
  418. /** Display message based on if all plugins are now active or not */
  419. $complete = array();
  420. foreach ( $this->plugins as $plugin ) {
  421. if ( ! is_plugin_active( $plugin['file_path'] ) ) {
  422. echo '<p><a href="' . add_query_arg( 'page', $this->menu, admin_url( $this->parent_url_slug ) ) . '" title="' . esc_attr( $this->strings['return'] ) . '" target="_parent">' . $this->strings['return'] . '</a></p>';
  423. $complete[] = $plugin;
  424. break;
  425. }
  426. /** Nothing to store */
  427. else {
  428. $complete[] = '';
  429. }
  430. }
  431. /** Filter out any empty entries */
  432. $complete = array_filter( $complete );
  433. /** All plugins are active, so we display the complete string and hide the plugin menu */
  434. if ( empty( $complete ) ) {
  435. echo '<p>' . sprintf( $this->strings['complete'], '<a href="' . admin_url() . '" title="' . __( 'Return to the Dashboard', SH_NAME ) . '">' . __( 'Return to the Dashboard', SH_NAME ) . '</a>' ) . '</p>';
  436. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  437. }
  438. return true;
  439. }
  440. /** Checks for actions from hover links to process the activation */
  441. elseif ( isset( $_GET[sanitize_key( 'plugin' )] ) && ( isset( $_GET[sanitize_key( 'tgmpa-activate' )] ) && 'activate-plugin' == $_GET[sanitize_key( 'tgmpa-activate' )] ) ) {
  442. check_admin_referer( 'tgmpa-activate', 'tgmpa-activate-nonce' );
  443. /** Populate $plugin array with necessary information */
  444. $plugin['name'] = $_GET[sanitize_key( 'plugin_name' )];
  445. $plugin['slug'] = $_GET[sanitize_key( 'plugin' )];
  446. $plugin['source'] = $_GET[sanitize_key( 'plugin_source' )];
  447. $plugin_data = get_plugins( '/' . $plugin['slug'] ); // Retrieve all plugins
  448. $plugin_file = array_keys( $plugin_data ); // Retrieve all plugin files from installed plugins
  449. $plugin_to_activate = $plugin['slug'] . '/' . $plugin_file[0]; // Match plugin slug with appropriate plugin file
  450. $activate = activate_plugin( $plugin_to_activate ); // Activate the plugin
  451. if ( is_wp_error( $activate ) ) {
  452. echo '<div id="message" class="error"><p>' . $activate->get_error_message() . '</p></div>';
  453. echo '<p><a href="' . add_query_arg( 'page', $this->menu, admin_url( $this->parent_url_slug ) ) . '" title="' . esc_attr( $this->strings['return'] ) . '" target="_parent">' . $this->strings['return'] . '</a></p>';
  454. return true; // End it here if there is an error with activation
  455. }
  456. else {
  457. /** Make sure message doesn't display again if bulk activation is performed immediately after a single activation */
  458. if ( ! isset( $_POST[sanitize_key( 'action' )] ) ) {
  459. $msg = sprintf( __( 'The following plugin was activated successfully: %s.', SH_NAME ), '<strong>' . $plugin['name'] . '</strong>' );
  460. echo '<div id="message" class="updated"><p>' . $msg . '</p></div>';
  461. }
  462. }
  463. }
  464. return false;
  465. }
  466. /**
  467. * Echoes required plugin notice.
  468. *
  469. * Outputs a message telling users that a specific plugin is required for
  470. * their theme. If appropriate, it includes a link to the form page where
  471. * users can install and activate the plugin.
  472. *
  473. * @since 1.0.0
  474. *
  475. * @global object $current_screen
  476. * @return null Returns early if we're on the Install page
  477. */
  478. public function notices() {
  479. global $current_screen;
  480. /** Remove nag on the install page */
  481. if ( $this->is_tgmpa_page() )
  482. return;
  483. $installed_plugins = get_plugins(); // Retrieve a list of all the plugins
  484. $this->populate_file_path();
  485. $message = array(); // Store the messages in an array to be outputted after plugins have looped through
  486. $install_link = false; // Set to false, change to true in loop if conditions exist, used for action link 'install'
  487. $install_link_count = 0; // Used to determine plurality of install action link text
  488. $activate_link = false; // Set to false, change to true in loop if conditions exist, used for action link 'activate'
  489. $activate_link_count = 0; // Used to determine plurality of activate action link text
  490. foreach ( $this->plugins as $plugin ) {
  491. /** If the plugin is installed and active, check for minimum version argument before moving forward */
  492. if ( is_plugin_active( $plugin['file_path'] ) ) {
  493. /** A minimum version has been specified */
  494. if ( isset( $plugin['version'] ) ) {
  495. if ( isset( $installed_plugins[$plugin['file_path']]['Version'] ) ) {
  496. /** If the current version is less than the minimum required version, we display a message */
  497. if ( version_compare( $installed_plugins[$plugin['file_path']]['Version'], $plugin['version'], '<' ) ) {
  498. if ( current_user_can( 'install_plugins' ) )
  499. $message['notice_ask_to_update'][] = $plugin['name'];
  500. else
  501. $message['notice_cannot_update'][] = $plugin['name'];
  502. }
  503. }
  504. /** Can't find the plugin, so iterate to the next condition */
  505. else {
  506. continue;
  507. }
  508. }
  509. /** No minimum version specified, so iterate over the plugin */
  510. else {
  511. continue;
  512. }
  513. }
  514. /** Not installed */
  515. if ( ! isset( $installed_plugins[$plugin['file_path']] ) ) {
  516. $install_link = true; // We need to display the 'install' action link
  517. $install_link_count++; // Increment the install link count
  518. if ( current_user_can( 'install_plugins' ) ) {
  519. if ( $plugin['required'] )
  520. $message['notice_can_install_required'][] = $plugin['name'];
  521. /** This plugin is only recommended */
  522. else
  523. $message['notice_can_install_recommended'][] = $plugin['name'];
  524. }
  525. /** Need higher privileges to install the plugin */
  526. else {
  527. $message['notice_cannot_install'][] = $plugin['name'];
  528. }
  529. }
  530. /** Installed but not active */
  531. elseif ( is_plugin_inactive( $plugin['file_path'] ) ) {
  532. $activate_link = true; // We need to display the 'activate' action link
  533. $activate_link_count++; // Increment the activate link count
  534. if ( current_user_can( 'activate_plugins' ) ) {
  535. if ( ( isset( $plugin['required'] ) ) && ( $plugin['required'] ) )
  536. $message['notice_can_activate_required'][] = $plugin['name'];
  537. /** This plugin is only recommended */
  538. else {
  539. $message['notice_can_activate_recommended'][] = $plugin['name'];
  540. }
  541. }
  542. /** Need higher privileges to activate the plugin */
  543. else {
  544. $message['notice_cannot_activate'][] = $plugin['name'];
  545. }
  546. }
  547. }
  548. /** Only process the nag messages if the user has not dismissed them already */
  549. if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice', true ) ) {
  550. /** If we have notices to display, we move forward */
  551. if ( ! empty( $message ) ) {
  552. krsort( $message ); // Sort messages
  553. $rendered = ''; // Display all nag messages as strings
  554. /** Grab all plugin names */
  555. foreach ( $message as $type => $plugin_groups ) {
  556. $linked_plugin_groups = array();
  557. /** Count number of plugins in each message group to calculate singular/plural message */
  558. $count = count( $plugin_groups );
  559. /** Loop through the plugin names to make the ones pulled from the .org repo linked */
  560. foreach ( $plugin_groups as $plugin_group_single_name ) {
  561. $external_url = $this->_get_plugin_data_from_name( $plugin_group_single_name, 'external_url' );
  562. $source = $this->_get_plugin_data_from_name( $plugin_group_single_name, 'source' );
  563. if ( $external_url && preg_match( '|^http(s)?://|', $external_url ) ) {
  564. $linked_plugin_groups[] = '<a href="' . esc_url( $external_url ) . '" title="' . $plugin_group_single_name . '" target="_blank">' . $plugin_group_single_name . '</a>';
  565. }
  566. elseif ( ! $source || preg_match( '|^http://wordpress.org/extend/plugins/|', $source ) ) {
  567. $url = add_query_arg(
  568. array(
  569. 'tab' => 'plugin-information',
  570. 'plugin' => $this->_get_plugin_data_from_name( $plugin_group_single_name ),
  571. 'TB_iframe' => 'true',
  572. 'width' => '640',
  573. 'height' => '500',
  574. ),
  575. admin_url( 'plugin-install.php' )
  576. );
  577. $linked_plugin_groups[] = '<a href="' . esc_url( $url ) . '" class="thickbox" title="' . $plugin_group_single_name . '">' . $plugin_group_single_name . '</a>';
  578. }
  579. else {
  580. $linked_plugin_groups[] = $plugin_group_single_name; // No hyperlink
  581. }
  582. if ( isset( $linked_plugin_groups ) && (array) $linked_plugin_groups )
  583. $plugin_groups = $linked_plugin_groups;
  584. }
  585. $last_plugin = array_pop( $plugin_groups ); // Pop off last name to prep for readability
  586. $imploded = empty( $plugin_groups ) ? '' . $last_plugin . '' : '' . ( implode( ', ', $plugin_groups ) . ' and ' . $last_plugin . '' );
  587. $rendered .= '<p>' . sprintf( translate_nooped_plural( $this->strings[$type], $count, SH_NAME ), $imploded, $count ) . '</p>'; // All messages now stored
  588. }
  589. /** Setup variables to determine if action links are needed */
  590. $show_install_link = $install_link ? '<a href="' . add_query_arg( 'page', $this->menu, admin_url( $this->parent_url_slug ) ) . '">' . translate_nooped_plural( $this->strings['install_link'], $install_link_count, SH_NAME ) . '</a>' : '';
  591. $show_activate_link = $activate_link ? '<a href="' . admin_url( 'plugins.php' ) . '">' . translate_nooped_plural( $this->strings['activate_link'], $activate_link_count, SH_NAME ) . '</a>' : '';
  592. /** Define all of the action links */
  593. $action_links = apply_filters(
  594. 'tgmpa_notice_action_links',
  595. array(
  596. 'install' => ( current_user_can( 'install_plugins' ) ) ? $show_install_link : '',
  597. 'activate' => ( current_user_can( 'activate_plugins' ) ) ? $show_activate_link : '',
  598. 'dismiss' => '<a class="dismiss-notice" href="' . add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ) . '" target="_parent">' . __( 'Dismiss this notice', SH_NAME ) . '</a>',
  599. )
  600. );
  601. $action_links = array_filter( $action_links ); // Remove any empty array items
  602. if ( $action_links )
  603. $rendered .= '<p>' . implode( ' | ', $action_links ) . '</p>';
  604. /** Register the nag messages and prepare them to be processed */
  605. if ( isset( $this->strings['nag_type'] ) )
  606. add_settings_error( 'tgmpa', 'tgmpa', $rendered, sanitize_html_class( strtolower( $this->strings['nag_type'] ), 'updated' ) );
  607. else
  608. add_settings_error( 'tgmpa', 'tgmpa', $rendered, 'updated' );
  609. }
  610. }
  611. /** Admin options pages already output settings_errors, so this is to avoid duplication */
  612. if ( 'options-general' !== $current_screen->parent_base )
  613. settings_errors( 'tgmpa' );
  614. }
  615. /**
  616. * Add dismissable admin notices.
  617. *
  618. * Appends a link to the admin nag messages. If clicked, the admin notice disappears and no longer is visible to users.
  619. *
  620. * @since 2.1.0
  621. */
  622. public function dismiss() {
  623. if ( isset( $_GET[sanitize_key( 'tgmpa-dismiss' )] ) )
  624. update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice', 1 );
  625. }
  626. /**
  627. * Add individual plugin to our collection of plugins.
  628. *
  629. * If the required keys are not set, the plugin is not added.
  630. *
  631. * @since 2.0.0
  632. *
  633. * @param array $plugin Array of plugin arguments.
  634. */
  635. public function register( $plugin ) {
  636. if ( ! isset( $plugin['slug'] ) || ! isset( $plugin['name'] ) )
  637. return;
  638. $this->plugins[] = $plugin;
  639. }
  640. /**
  641. * Amend default configuration settings.
  642. *
  643. * @since 2.0.0
  644. *
  645. * @param array $config
  646. */
  647. public function config( $config ) {
  648. $keys = array( 'default_path', 'parent_menu_slug', 'parent_url_slug', 'domain', 'has_notices', 'menu', 'is_automatic', 'message', 'strings' );
  649. foreach ( $keys as $key ) {
  650. if ( isset( $config[$key] ) ) {
  651. if ( is_array( $config[$key] ) ) {
  652. foreach ( $config[$key] as $subkey => $value )
  653. $this->{$key}[$subkey] = $value;
  654. } else {
  655. $this->$key = $config[$key];
  656. }
  657. }
  658. }
  659. }
  660. /**
  661. * Amend action link after plugin installation.
  662. *
  663. * @since 2.0.0
  664. *
  665. * @param array $install_actions Existing array of actions
  666. * @return array Amended array of actions
  667. */
  668. public function actions( $install_actions ) {
  669. /** Remove action links on the TGMPA install page */
  670. if ( $this->is_tgmpa_page() )
  671. return false;
  672. return $install_actions;
  673. }
  674. /**
  675. * Set file_path key for each installed plugin.
  676. *
  677. * @since 2.1.0
  678. */
  679. public function populate_file_path() {
  680. /** Add file_path key for all plugins */
  681. foreach ( $this->plugins as $plugin => $values )
  682. $this->plugins[$plugin]['file_path'] = $this->_get_plugin_basename_from_slug( $values['slug'] );
  683. }
  684. /**
  685. * Helper function to extract the file path of the plugin file from the
  686. * plugin slug, if the plugin is installed.
  687. *
  688. * @since 2.0.0
  689. *
  690. * @param string $slug Plugin slug (typically folder name) as provided by the developer
  691. * @return string Either file path for plugin if installed, or just the plugin slug
  692. */
  693. protected function _get_plugin_basename_from_slug( $slug ) {
  694. $keys = array_keys( get_plugins() );
  695. foreach ( $keys as $key ) {
  696. if ( preg_match( '|^' . $slug .'|', $key ) )
  697. return $key;
  698. }
  699. return $slug;
  700. }
  701. /**
  702. * Retrieve plugin data, given the plugin name.
  703. *
  704. * Loops through the registered plugins looking for $name. If it finds it,
  705. * it returns the $data from that plugin. Otherwise, returns false.
  706. *
  707. * @since 2.1.0
  708. *
  709. * @param string $name Name of the plugin, as it was registered
  710. * @param string $data Optional. Array key of plugin data to return. Default is slug
  711. * @return string|boolean Plugin slug if found, false otherwise.
  712. */
  713. protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
  714. foreach ( $this->plugins as $plugin => $values ) {
  715. if ( $name == $values['name'] && isset( $values[$data] ) )
  716. return $values[$data];
  717. }
  718. return false;
  719. }
  720. /**
  721. * Determine if we're on the TGMPA Install page.
  722. *
  723. * We use $current_screen when it is available, and a slightly less ideal
  724. * conditional when it isn't (like when displaying the plugin information
  725. * thickbox).
  726. *
  727. * @since 2.1.0
  728. *
  729. * @global object $current_screen
  730. * @return boolean True when on the TGMPA page, false otherwise.
  731. */
  732. protected function is_tgmpa_page() {
  733. global $current_screen;
  734. if ( ! is_null( $current_screen ) && $this->parent_menu_slug == $current_screen->parent_file && isset( $_GET['page'] ) && $this->menu === $_GET['page'] )
  735. return true;
  736. if ( isset( $_GET['page'] ) && $this->menu === $_GET['page'] )
  737. return true;
  738. return false;
  739. }
  740. /**
  741. * Delete dismissable nag option when theme is switched.
  742. *
  743. * This ensures that the user is again reminded via nag of required
  744. * and/or recommended plugins if they re-activate the theme.
  745. *
  746. * @since 2.1.1
  747. */
  748. public function update_dismiss() {
  749. delete_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice' );
  750. }
  751. /**
  752. * Forces plugin activation if the parameter 'force_activation' is
  753. * set to true.
  754. *
  755. * This allows theme authors to specify certain plugins that must be
  756. * active at all times while using the current theme.
  757. *
  758. * Please take special care when using this parameter as it has the
  759. * potential to be harmful if not used correctly. Setting this parameter
  760. * to true will not allow the specified plugin to be deactivated unless
  761. * the user switches themes.
  762. *
  763. * @since 2.2.0
  764. */
  765. public function force_activation() {
  766. /** Set file_path parameter for any installed plugins */
  767. $this->populate_file_path();
  768. $installed_plugins = get_plugins();
  769. foreach ( $this->plugins as $plugin ) {
  770. /** Oops, plugin isn't there so iterate to next condition */
  771. if ( isset( $plugin['force_activation'] ) && $plugin['force_activation'] && ! isset( $installed_plugins[$plugin['file_path']] ) )
  772. continue;
  773. /** There we go, activate the plugin */
  774. elseif ( isset( $plugin['force_activation'] ) && $plugin['force_activation'] && is_plugin_inactive( $plugin['file_path'] ) ){
  775. if( isset($plugin['file_path']) ) activate_plugin( $plugin['file_path'] );
  776. }
  777. }
  778. }
  779. /**
  780. * Forces plugin deactivation if the parameter 'force_deactivation'
  781. * is set to true.
  782. *
  783. * This allows theme authors to specify certain plugins that must be
  784. * deactived upon switching from the current theme to another.
  785. *
  786. * Please take special care when using this parameter as it has the
  787. * potential to be harmful if not used correctly.
  788. *
  789. * @since 2.2.0
  790. */
  791. public function force_deactivation() {
  792. /** Set file_path parameter for any installed plugins */
  793. $this->populate_file_path();
  794. foreach ( $this->plugins as $plugin ) {
  795. /** Only proceed forward if the paramter is set to true and plugin is active */
  796. if ( isset( $plugin['force_deactivation'] ) && $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) )
  797. deactivate_plugins( $plugin['file_path'] );
  798. }
  799. }
  800. }
  801. }
  802. /** Create a new instance of the class */
  803. new TGM_Plugin_Activation;
  804. if ( ! function_exists( 'tgmpa' ) ) {
  805. /**
  806. * Helper function to register a collection of required plugins.
  807. *
  808. * @since 2.0.0
  809. * @api
  810. *
  811. * @param array $plugins An array of plugin arrays
  812. * @param array $config Optional. An array of configuration values
  813. */
  814. function tgmpa( $plugins, $config = array() ) {
  815. foreach ( $plugins as $plugin )
  816. TGM_Plugin_Activation::$instance->register( $plugin );
  817. if ( $config )
  818. TGM_Plugin_Activation::$instance->config( $config );
  819. }
  820. }
  821. /**
  822. * WP_List_Table isn't always available. If it isn't available,
  823. * we load it here.
  824. *
  825. * @since 2.2.0
  826. */
  827. if ( ! class_exists( 'WP_List_Table' ) )
  828. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  829. if ( ! class_exists( 'TGMPA_List_Table' ) ) {
  830. /**
  831. * List table class for handling plugins.
  832. *
  833. * Extends the WP_List_Table class to provide a future-compatible
  834. * way of listing out all required/recommended plugins.
  835. *
  836. * Gives users an interface similar to the Plugin Administration
  837. * area with similar (albeit stripped down) capabilities.
  838. *
  839. * This class also allows for the bulk install of plugins.
  840. *
  841. * @since 2.2.0
  842. *
  843. * @package TGM-Plugin-Activation
  844. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  845. * @author Gary Jones <gamajo@gamajo.com>
  846. */
  847. class TGMPA_List_Table extends WP_List_Table {
  848. /**
  849. * References parent constructor and sets defaults for class.
  850. *
  851. * The constructor also grabs a copy of $instance from the TGMPA class
  852. * and stores it in the global object TGM_Plugin_Activation::$instance.
  853. *
  854. * @since 2.2.0
  855. *
  856. * @global unknown $status
  857. * @global string $page
  858. */
  859. public function __construct() {
  860. global $status, $page;
  861. parent::__construct(
  862. array(
  863. 'singular' => 'plugin',
  864. 'plural' => 'plugins',
  865. 'ajax' => false,
  866. )
  867. );
  868. }
  869. /**
  870. * Gathers and renames all of our plugin information to be used by
  871. * WP_List_Table to create our table.
  872. *
  873. * @since 2.2.0
  874. *
  875. * @return array $table_data Information for use in table
  876. */
  877. protected function _gather_plugin_data() {
  878. /** Load thickbox for plugin links */
  879. TGM_Plugin_Activation::$instance->admin_init();
  880. TGM_Plugin_Activation::$instance->thickbox();
  881. /** Prep variables for use and grab list of all installed plugins */
  882. $table_data = array();
  883. $i = 0;
  884. $installed_plugins = get_plugins();
  885. foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin ) {
  886. if ( is_plugin_active( $plugin['file_path'] ) )
  887. continue; // No need to display plugins if they are installed and activated
  888. $table_data[$i]['sanitized_plugin'] = $plugin['name'];
  889. $table_data[$i]['slug'] = $this->_get_plugin_data_from_name( $plugin['name'] );
  890. $external_url = $this->_get_plugin_data_from_name( $plugin['name'], 'external_url' );
  891. $source = $this->_get_plugin_data_from_name( $plugin['name'], 'source' );
  892. if ( $external_url && preg_match( '|^http(s)?://|', $external_url ) ) {
  893. $table_data[$i]['plugin'] = '<a href="' . esc_url( $external_url ) . '" title="' . $plugin['name'] . '" target="_blank">' . $plugin['name'] . '</a>';
  894. }
  895. elseif ( ! $source || preg_match( '|^http://wordpress.org/extend/plugins/|', $source ) ) {
  896. $url = add_query_arg(
  897. array(
  898. 'tab' => 'plugin-information',
  899. 'plugin' => $this->_get_plugin_data_from_name( $plugin['name'] ),
  900. 'TB_iframe' => 'true',
  901. 'width' => '640',
  902. 'height' => '500',
  903. ),
  904. admin_url( 'plugin-install.php' )
  905. );
  906. $table_data[$i]['plugin'] = '<a href="' . esc_url( $url ) . '" class="thickbox" title="' . $plugin['name'] . '">' . $plugin['name'] . '</a>';
  907. }
  908. else {
  909. $table_data[$i]['plugin'] = '' . $plugin['name'] . ''; // No hyperlink
  910. }
  911. if ( isset( $table_data[$i]['plugin'] ) && (array) $table_data[$i]['plugin'] )
  912. $plugin['name'] = $table_data[$i]['plugin'];
  913. if ( isset( $plugin['external_url'] ) ) {
  914. /** The plugin is linked to an external source */
  915. $table_data[$i]['source'] = __( 'External Link', SH_NAME );
  916. }
  917. elseif ( isset( $plugin['source'] ) ) {
  918. /** The plugin must be from a private repository */
  919. if ( preg_match( '|^http(s)?://|', $plugin['source'] ) )
  920. $table_data[$i]['source'] = __( 'Private Repository', SH_NAME );
  921. /** The plugin is pre-packaged with the theme */
  922. else
  923. $table_data[$i]['source'] = __( 'Pre-Packaged', SH_NAME );
  924. }
  925. /** The plugin is from the WordPress repository */
  926. else {
  927. $table_data[$i]['source'] = __( 'WordPress Repository', SH_NAME );
  928. }
  929. $table_data[$i]['type'] = $plugin['required'] ? __( 'Required', SH_NAME ) : __( 'Recommended', SH_NAME );
  930. if ( ! isset( $installed_plugins[$plugin['file_path']] ) )
  931. $table_data[$i]['status'] = sprintf( '%1$s', __( 'Not Installed', SH_NAME ) );
  932. elseif ( is_plugin_inactive( $plugin['file_path'] ) )
  933. $table_data[$i]['status'] = sprintf( '%1$s', __( 'Installed But Not Activated', SH_NAME ) );
  934. $table_data[$i]['file_path'] = $plugin['file_path'];
  935. $table_data[$i]['url'] = isset( $plugin['source'] ) ? $plugin['source'] : 'repo';
  936. $i++;
  937. }
  938. /** Sort plugins by Required/Recommended type and by alphabetical listing within each type */
  939. $resort = array();
  940. $req = array();
  941. $rec = array();
  942. /** Grab all the plugin types */
  943. foreach ( $table_data as $plugin )
  944. $resort[] = $plugin['type'];
  945. /** Sort each plugin by type */
  946. foreach ( $resort as $type )
  947. if ( 'Required' == $type )
  948. $req[] = $type;
  949. else
  950. $rec[] = $type;
  951. /** Sort alphabetically each plugin type array, merge them and then sort in reverse (lists Required plugins first) */
  952. sort( $req );
  953. sort( $rec );
  954. array_merge( $resort, $req, $rec );
  955. array_multisort( $resort, SORT_DESC, $table_data );
  956. return $table_data;
  957. }
  958. /**
  959. * Retrieve plugin data, given the plugin name. Taken from the
  960. * TGM_Plugin_Activation class.
  961. *
  962. * Loops through the registered plugins looking for $name. If it finds it,
  963. * it returns the $data from that plugin. Otherwise, returns false.
  964. *
  965. * @since 2.2.0
  966. *
  967. * @param string $name Name of the plugin, as it was registered
  968. * @param string $data Optional. Array key of plugin data to return. Default is slug
  969. * @return string|boolean Plugin slug if found, false otherwise
  970. */
  971. protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
  972. foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin => $values ) {
  973. if ( $name == $values['name'] && isset( $values[$data] ) )
  974. return $values[$data];
  975. }
  976. return false;
  977. }
  978. /**
  979. * Create default columns to display important plugin information
  980. * like type, action and status.
  981. *
  982. * @since 2.2.0
  983. *
  984. * @param array $item
  985. * @param string $column_name
  986. */
  987. public function column_default( $item, $column_name ) {
  988. switch ( $column_name ) {
  989. case 'source':
  990. case 'type':
  991. case 'status':
  992. return $item[$column_name];
  993. }
  994. }
  995. /**
  996. * Create default title column along with action links of 'Install'
  997. * and 'Activate'.
  998. *
  999. * @since 2.2.0
  1000. *
  1001. * @param array $item
  1002. * @return string The action hover links
  1003. */
  1004. public function column_plugin( $item ) {
  1005. $installed_plugins = get_plugins();
  1006. /** No need to display any hover links */
  1007. if ( is_plugin_active( $item['file_path'] ) )
  1008. $actions = array();
  1009. /** We need to display the 'Install' hover link */
  1010. if ( ! isset( $installed_plugins[$item['file_path']] ) ) {
  1011. $actions = array(
  1012. 'install' => sprintf(
  1013. '<a href="%1$s" title="Install %2$s">Install</a>',
  1014. wp_nonce_url(
  1015. add_query_arg(
  1016. array(
  1017. 'page' => TGM_Plugin_Activation::$instance->menu,
  1018. 'plugin' => $item['slug'],
  1019. 'plugin_name' => $item['sanitized_plugin'],
  1020. 'plugin_source' => $item['url'],
  1021. 'tgmpa-install' => 'install-plugin',
  1022. ),
  1023. admin_url( TGM_Plugin_Activation::$instance->parent_url_slug )
  1024. ),
  1025. 'tgmpa-install'
  1026. ),
  1027. $item['sanitized_plugin']
  1028. ),
  1029. );
  1030. }
  1031. /** We need to display the 'Activate' hover link */
  1032. elseif ( is_plugin_inactive( $item['file_path'] ) ) {
  1033. $actions = array(
  1034. 'activate' => sprintf(
  1035. '<a href="%1$s" title="Activate %2$s">Activate</a>',
  1036. add_query_arg(
  1037. array(
  1038. 'page' => TGM_Plugin_Activation::$instance->menu,
  1039. 'plugin' => $item['slug'],
  1040. 'plugin_name' => $item['sanitized_plugin'],
  1041. 'plugin_source' => $item['url'],
  1042. 'tgmpa-activate' => 'activate-plugin',
  1043. 'tgmpa-activate-nonce' => wp_create_nonce( 'tgmpa-activate' ),
  1044. ),
  1045. admin_url( TGM_Plugin_Activation::$instance->parent_url_slug )
  1046. ),
  1047. $item['sanitized_plugin']
  1048. ),
  1049. );
  1050. }
  1051. return sprintf( '%1$s %2$s', $item['plugin'], $this->row_actions( $actions ) );
  1052. }
  1053. /**
  1054. * Required for bulk installing.
  1055. *
  1056. * Adds a checkbox for each plugin.
  1057. *
  1058. * @since 2.2.0
  1059. *
  1060. * @param array $item
  1061. * @return string The input checkbox with all necessary info
  1062. */
  1063. public function column_cb( $item ) {
  1064. $value = $item['file_path'] . ',' . $item['url'] . ',' . $item['sanitized_plugin'];
  1065. return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />', $this->_args['singular'], $value, $item['sanitized_plugin'] );
  1066. }
  1067. /**
  1068. * Sets default message within the plugins table if no plugins
  1069. * are left for interaction.
  1070. *
  1071. * Hides the menu item to prevent the user from clicking and
  1072. * getting a permissions error.
  1073. *
  1074. * @since 2.2.0
  1075. */
  1076. public function no_items() {
  1077. printf( __( 'No plugins to install or activate. <a href="%1$s" title="Return to the Dashboard">Return to the Dashboard</a>', SH_NAME ), admin_url() );
  1078. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  1079. }
  1080. /**
  1081. * Output all the column information within the table.
  1082. *
  1083. * @since 2.2.0
  1084. *
  1085. * @return array $columns The column names
  1086. */
  1087. public function get_columns() {
  1088. $columns = array(
  1089. 'cb' => '<input type="checkbox" />',
  1090. 'plugin' => __( 'Plugin', SH_NAME ),
  1091. 'source' => __( 'Source', SH_NAME ),
  1092. 'type' => __( 'Type', SH_NAME ),
  1093. 'status' => __( 'Status', SH_NAME )
  1094. );
  1095. return $columns;
  1096. }
  1097. /**
  1098. * Defines all types of bulk actions for handling
  1099. * registered plugins.
  1100. *
  1101. * @since 2.2.0
  1102. *
  1103. * @return array $actions The bulk actions for the plugin install table
  1104. */
  1105. public function get_bulk_actions() {
  1106. $actions = array(
  1107. 'tgmpa-bulk-install' => __( 'Install', SH_NAME ),
  1108. 'tgmpa-bulk-activate' => __( 'Activate', SH_NAME ),
  1109. );
  1110. return $actions;
  1111. }
  1112. /**
  1113. * Processes bulk installation and activation actions.
  1114. *
  1115. * The bulk installation process looks either for the $_POST
  1116. * information or for the plugin info within the $_GET variable if
  1117. * a user has to use WP_Filesystem to enter their credentials.
  1118. *
  1119. * @since 2.2.0
  1120. */
  1121. public function process_bulk_actions() {
  1122. /** Bulk installation process */
  1123. if ( 'tgmpa-bulk-install' === $this->current_action() ) {
  1124. check_admin_referer( 'bulk-' . $this->_args['plural'] );
  1125. /** Prep variables to be populated */
  1126. $plugins_to_install = array();
  1127. $plugin_installs = array();
  1128. $plugin_path = array();
  1129. $plugin_name = array();
  1130. /** Look first to see if information has been passed via WP_Filesystem */
  1131. if ( isset( $_GET[sanitize_key( 'plugins' )] ) )
  1132. $plugins = explode( ',', stripslashes( $_GET[sanitize_key( 'plugins' )] ) );
  1133. /** Looks like the user can use the direct method, take from $_POST */
  1134. elseif ( isset( $_POST[sanitize_key( 'plugin' )] ) )
  1135. $plugins = (array) $_POST[sanitize_key( 'plugin' )];
  1136. /** Nothing has been submitted */
  1137. else
  1138. $plugins = array();
  1139. $a = 0; // Incremental variable
  1140. /** Grab information from $_POST if available */
  1141. if ( isset( $_POST[sanitize_key( 'plugin' )] ) ) {
  1142. foreach ( $plugins as $plugin_data )
  1143. $plugins_to_install[] = explode( ',', $plugin_data );
  1144. foreach ( $plugins_to_install as $plugin_data ) {
  1145. $plugin_installs[] = $plugin_data[0];
  1146. $plugin_path[] = $plugin_data[1];
  1147. $plugin_name[] = $plugin_data[2];
  1148. }
  1149. }
  1150. /** Information has been passed via $_GET */
  1151. else {
  1152. foreach ( $plugins as $key => $value ) {
  1153. /** Grab plugin slug for each plugin */
  1154. if ( 0 == $key % 3 || 0 == $key ) {
  1155. $plugins_to_install[] = $value;
  1156. $plugin_installs[] = $value;
  1157. }
  1158. $a++;
  1159. }
  1160. }
  1161. /** Look first to see if information has been passed via WP_Filesystem */

Large files files are truncated, but you can click here to view the full file