PageRenderTime 70ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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 */
  1162. if ( isset( $_GET[sanitize_key( 'plugin_paths' )] ) )
  1163. $plugin_paths = explode( ',', stripslashes( $_GET[sanitize_key( 'plugin_paths' )] ) );
  1164. /** Looks like the user doesn't need to enter his FTP creds */
  1165. elseif ( isset( $_POST[sanitize_key( 'plugin' )] ) )
  1166. $plugin_paths = (array) $plugin_path;
  1167. /** Nothing has been submitted */
  1168. else
  1169. $plugin_paths = array();
  1170. /** Look first to see if information has been passed via WP_Filesystem */
  1171. if ( isset( $_GET[sanitize_key( 'plugin_names' )] ) )
  1172. $plugin_names = explode( ',', stripslashes( $_GET[sanitize_key( 'plugin_names' )] ) );
  1173. /** Looks like the user doesn't need to enter his FTP creds */
  1174. elseif ( isset( $_POST[sanitize_key( 'plugin' )] ) )
  1175. $plugin_names = (array) $plugin_name;
  1176. /** Nothing has been submitted */
  1177. else
  1178. $plugin_names = array();
  1179. $b = 0; // Incremental variable
  1180. /** Loop through plugin slugs and remove already installed plugins from the list */
  1181. foreach ( $plugin_installs as $key => $plugin ) {
  1182. if ( preg_match( '|.php$|', $plugin ) ) {
  1183. unset( $plugin_installs[$key] );
  1184. /** If the plugin path isn't in the $_GET variable, we can unset the corresponding path */
  1185. if ( ! isset( $_GET[sanitize_key( 'plugin_paths' )] ) )
  1186. unset( $plugin_paths[$b] );
  1187. /** If the plugin name isn't in the $_GET variable, we can unset the corresponding name */
  1188. if ( ! isset( $_GET[sanitize_key( 'plugin_names' )] ) )
  1189. unset( $plugin_names[$b] );
  1190. }
  1191. $b++;
  1192. }
  1193. /** No need to proceed further if we have no plugins to install */
  1194. if ( empty( $plugin_installs ) )
  1195. return false;
  1196. /** Reset array indexes in case we removed already installed plugins */
  1197. $plugin_installs = array_values( $plugin_installs );
  1198. $plugin_paths = array_values( $plugin_paths );
  1199. $plugin_names = array_values( $plugin_names );
  1200. /** If we grabbed our plugin info from $_GET, we need to decode it for use */
  1201. $plugin_installs = array_map( 'urldecode', $plugin_installs );
  1202. $plugin_paths = array_map( 'urldecode', $plugin_paths );
  1203. $plugin_names = array_map( 'urldecode', $plugin_names );
  1204. /** Pass all necessary information via URL if WP_Filesystem is needed */
  1205. $url = wp_nonce_url(
  1206. add_query_arg(
  1207. array(
  1208. 'page' => TGM_Plugin_Activation::$instance->menu,
  1209. 'tgmpa-action' => 'install-selected',
  1210. 'plugins' => urlencode( implode( ',', $plugins ) ),
  1211. 'plugin_paths' => urlencode( implode( ',', $plugin_paths ) ),
  1212. 'plugin_names' => urlencode( implode( ',', $plugin_names ) ),
  1213. ),
  1214. admin_url( TGM_Plugin_Activation::$instance->parent_url_slug )
  1215. ),
  1216. 'bulk-plugins'
  1217. );
  1218. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary
  1219. $fields = array( sanitize_key( 'action' ), sanitize_key( '_wp_http_referer' ), sanitize_key( '_wpnonce' ) ); // Extra fields to pass to WP_Filesystem
  1220. if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, $fields ) ) )
  1221. return true;
  1222. if ( ! WP_Filesystem( $creds ) ) {
  1223. request_filesystem_credentials( $url, $method, true, false, $fields ); // Setup WP_Filesystem
  1224. return true;
  1225. }
  1226. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for plugins_api
  1227. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes
  1228. /** Store all information in arrays since we are processing a bulk installation */
  1229. $api = array();
  1230. $sources = array();
  1231. $install_path = array();
  1232. $c = 0; // Incremental variable
  1233. /** Loop through each plugin to install and try to grab information from WordPress API, if not create 'tgmpa-empty' scalar */
  1234. foreach ( $plugin_installs as $plugin ) {
  1235. $api[$c] = plugins_api( 'plugin_information', array( 'slug' => $plugin, 'fields' => array( 'sections' => false ) ) ) ? plugins_api( 'plugin_information', array( 'slug' => $plugin, 'fields' => array( 'sections' => false ) ) ) : (object) $api[$c] = 'tgmpa-empty';
  1236. $c++;
  1237. }
  1238. if ( is_wp_error( $api ) )
  1239. wp_die( TGM_Plugin_Activation::$instance->strings['oops'] . var_dump( $api ) );
  1240. $d = 0; // Incremental variable
  1241. /** Capture download links from $api or set install link to pre-packaged/private repo */
  1242. foreach ( $api as $object ) {
  1243. $sources[$d] = isset( $object->download_link ) && 'repo' == $plugin_paths[$d] ? $object->download_link : $plugin_paths[$d];
  1244. $d++;
  1245. }
  1246. /** Finally, all the data is prepared to be sent to the installer */
  1247. $url = add_query_arg( array( 'page' => TGM_Plugin_Activation::$instance->menu ), admin_url( TGM_Plugin_Activation::$instance->parent_url_slug ) );
  1248. $nonce = 'bulk-plugins';
  1249. $names = $plugin_names;
  1250. /** Create a new instance of TGM_Bulk_Installer */
  1251. $installer = new TGM_Bulk_Installer( $skin = new TGM_Bulk_Installer_Skin( compact( 'url', 'nonce', 'names' ) ) );
  1252. /** Wrap the install process with the appropriate HTML */
  1253. echo '<div class="tgmpa wrap">';
  1254. screen_icon( apply_filters( 'tgmpa_default_screen_icon', 'themes' ) );
  1255. echo '<h2>' . esc_html( get_admin_page_title() ) . '</h2>';
  1256. /** Process the bulk installation submissions */
  1257. $installer->bulk_install( $sources );
  1258. echo '</div>';
  1259. return true;
  1260. }
  1261. /** Bulk activation process */
  1262. if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
  1263. check_admin_referer( 'bulk-' . $this->_args['plural'] );
  1264. /** Grab plugin data from $_POST */
  1265. $plugins = isset( $_POST[sanitize_key( 'plugin' )] ) ? (array) $_POST[sanitize_key( 'plugin' )] : array();
  1266. $plugins_to_activate = array();
  1267. /** Split plugin value into array with plugin file path, plugin source and plugin name */
  1268. foreach ( $plugins as $i => $plugin )
  1269. $plugins_to_activate[] = explode( ',', $plugin );
  1270. foreach ( $plugins_to_activate as $i => $array ) {
  1271. if ( ! preg_match( '|.php$|', $array[0] ) ) // Plugins that haven't been installed yet won't have the correct file path
  1272. unset( $plugins_to_activate[$i] );
  1273. }
  1274. /** Return early if there are no plugins to activate */
  1275. if ( empty( $plugins_to_activate ) )
  1276. return;
  1277. $plugins = array();
  1278. $plugin_names = array();
  1279. foreach ( $plugins_to_activate as $plugin_string ) {
  1280. $plugins[] = $plugin_string[0];
  1281. $plugin_names[] = $plugin_string[2];
  1282. }
  1283. $count = count( $plugin_names ); // Count so we can use _n function
  1284. $last_plugin = array_pop( $plugin_names ); // Pop off last name to prep for readability
  1285. $imploded = empty( $plugin_names ) ? '' . $last_plugin . '' : '' . ( implode( ', ', $plugin_names ) . ' and ' . $last_plugin . '.' );
  1286. /** Now we are good to go - let's start activating plugins */
  1287. $activate = activate_plugins( $plugins );
  1288. if ( is_wp_error( $activate ) )
  1289. echo '<div id="message" class="error"><p>' . $activate->get_error_message() . '</p></div>';
  1290. else
  1291. printf( '<div id="message" class="updated"><p>%1$s %2$s</p></div>', _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, SH_NAME ), $imploded );
  1292. /** Update recently activated plugins option */
  1293. $recent = (array) get_option( 'recently_activated' );
  1294. foreach ( $plugins as $plugin => $time )
  1295. if ( isset( $recent[$plugin] ) )
  1296. unset( $recent[$plugin] );
  1297. update_option( 'recently_activated', $recent );
  1298. unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another
  1299. }
  1300. }
  1301. /**
  1302. * Prepares all of our information to be outputted into a usable table.
  1303. *
  1304. * @since 2.2.0
  1305. */
  1306. public function prepare_items() {
  1307. $per_page = 100; // Set it high so we shouldn't have to worry about pagination
  1308. $columns = $this->get_columns(); // Get all necessary column information
  1309. $hidden = array(); // No columns to hide, but we must set as an array
  1310. $sortable = array(); // No reason to make sortable columns
  1311. $this->_column_headers = array( $columns, $hidden, $sortable ); // Get all necessary column headers
  1312. /** Process our bulk actions here */
  1313. $this->process_bulk_actions();
  1314. /** Store all of our plugin data into $items array so WP_List_Table can use it */
  1315. $this->items = $this->_gather_plugin_data();
  1316. }
  1317. }
  1318. }
  1319. /**
  1320. * The WP_Upgrader file isn't always available. If it isn't available,
  1321. * we load it here.
  1322. *
  1323. * We check to make sure no action or activation keys are set so that WordPress
  1324. * doesn't try to re-include the class when processing upgrades or installs outside
  1325. * of the class.
  1326. *
  1327. * @since 2.2.0
  1328. */
  1329. if ( ! class_exists( 'WP_Upgrader' ) && ( isset( $_GET[sanitize_key( 'page' )] ) && TGM_Plugin_Activation::$instance->menu = $_GET[sanitize_key( 'page' )] ) ) {
  1330. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  1331. if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
  1332. /**
  1333. * Installer class to handle bulk plugin installations.
  1334. *
  1335. * Extends WP_Upgrader and customizes to suit the installation of multiple
  1336. * plugins.
  1337. *
  1338. * @since 2.2.0
  1339. *
  1340. * @package TGM-Plugin-Activation
  1341. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  1342. * @author Gary Jones <gamajo@gamajo.com>
  1343. */
  1344. class TGM_Bulk_Installer extends WP_Upgrader {
  1345. /**
  1346. * Holds result of bulk plugin installation.
  1347. *
  1348. * @since 2.2.0
  1349. *
  1350. * @var string
  1351. */
  1352. public $result;
  1353. /**
  1354. * Flag to check if bulk installation is occurring or not.
  1355. *
  1356. * @since 2.2.0
  1357. *
  1358. * @var boolean
  1359. */
  1360. public $bulk = false;
  1361. /**
  1362. * Processes the bulk installation of plugins.
  1363. *
  1364. * @since 2.2.0
  1365. *
  1366. * @param array $packages The plugin sources needed for installation
  1367. * @return string|boolean Install confirmation messages on success, false on failure
  1368. */
  1369. public function bulk_install( $packages ) {
  1370. /** Pass installer skin object and set bulk property to true */
  1371. $this->init();
  1372. $this->bulk = true;
  1373. /** Set install strings and automatic activation strings (if config option is set to true) */
  1374. $this->install_strings();
  1375. if ( TGM_Plugin_Activation::$instance->is_automatic )
  1376. $this->activate_strings();
  1377. /** Run the header string to notify user that the process has begun */
  1378. $this->skin->header();
  1379. /** Connect to the Filesystem */
  1380. $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
  1381. if ( ! $res ) {
  1382. $this->skin->footer();
  1383. return false;
  1384. }
  1385. /** Set the bulk header and prepare results array */
  1386. $this->skin->bulk_header();
  1387. $results = array();
  1388. /** Get the total number of packages being processed and iterate as each package is successfully installed */
  1389. $this->update_count = count( $packages );
  1390. $this->update_current = 0;
  1391. /** Loop through each plugin and process the installation */
  1392. foreach ( $packages as $plugin ) {
  1393. $this->update_current++; // Increment counter
  1394. /** Do the plugin install */
  1395. $result = $this->run(
  1396. array(
  1397. 'package' => $plugin, // The plugin source
  1398. 'destination' => WP_PLUGIN_DIR, // The destination dir
  1399. 'clear_destination' => false, // Do we want to clear the destination or not?
  1400. 'clear_working' => true, // Remove original install file
  1401. 'is_multi' => true, // Are we processing multiple installs?
  1402. 'hook_extra' => array( 'plugin' => $plugin, ), // Pass plugin source as extra data
  1403. )
  1404. );
  1405. /** Store installation results in result property */
  1406. $results[$plugin] = $this->result;
  1407. /** Prevent credentials auth screen from displaying multiple times */
  1408. if ( false === $result )
  1409. break;
  1410. }
  1411. /** Pass footer skin strings */
  1412. $this->skin->bulk_footer();
  1413. $this->skin->footer();
  1414. /** Return our results */
  1415. return $results;
  1416. }
  1417. /**
  1418. * Performs the actual installation of each plugin.
  1419. *
  1420. * This method also activates the plugin in the automatic flag has been
  1421. * set to true for the TGMPA class.
  1422. *
  1423. * @since 2.2.0
  1424. *
  1425. * @param array $options The installation cofig options
  1426. * @return null/array Return early if error, array of installation data on success
  1427. */
  1428. public function run( $options ) {
  1429. /** Default config options */
  1430. $defaults = array(
  1431. 'package' => '',
  1432. 'destination' => '',
  1433. 'clear_destination' => false,
  1434. 'clear_working' => true,
  1435. 'is_multi' => false,
  1436. 'hook_extra' => array(),
  1437. );
  1438. /** Parse default options with config options from $this->bulk_upgrade and extract them */
  1439. $options = wp_parse_args( $options, $defaults );
  1440. extract( $options );
  1441. /** Connect to the Filesystem */
  1442. $res = $this->fs_connect( array( WP_CONTENT_DIR, $destination ) );
  1443. if ( ! $res )
  1444. return false;
  1445. /** Return early if there is an error connecting to the Filesystem */
  1446. if ( is_wp_error( $res ) ) {
  1447. $this->skin->error( $res );
  1448. return $res;
  1449. }
  1450. /** Call $this->header separately if running multiple times */
  1451. if ( ! $is_multi )
  1452. $this->skin->header();
  1453. /** Set strings before the package is installed */
  1454. $this->skin->before();
  1455. /** Download the package (this just returns the filename of the file if the package is a local file) */
  1456. $download = $this->download_package( $package );
  1457. if ( is_wp_error( $download ) ) {
  1458. $this->skin->error( $download );
  1459. $this->skin->after();
  1460. return $download;
  1461. }
  1462. /** Don't accidentally delete a local file */
  1463. $delete_package = ( $download != $package );
  1464. /** Unzip file into a temporary working directory */
  1465. $working_dir = $this->unpack_package( $download, $delete_package );
  1466. if ( is_wp_error( $working_dir ) ) {
  1467. $this->skin->error( $working_dir );
  1468. $this->skin->after();
  1469. return $working_dir;
  1470. }
  1471. /** Install the package into the working directory with all passed config options */
  1472. $result = $this->install_package(
  1473. array(
  1474. 'source' => $working_dir,
  1475. 'destination' => $destination,
  1476. 'clear_destination' => $clear_destination,
  1477. 'clear_working' => $clear_working,
  1478. 'hook_extra' => $hook_extra,
  1479. )
  1480. );
  1481. /** Pass the result of the installation */
  1482. $this->skin->set_result( $result );
  1483. /** Set correct strings based on results */
  1484. if ( is_wp_error( $result ) ) {
  1485. $this->skin->error( $result );
  1486. $this->skin->feedback( 'process_failed' );
  1487. }
  1488. /** The plugin install is successful */
  1489. else {
  1490. $this->skin->feedback( 'process_success' );
  1491. }
  1492. /** Only process the activation of installed plugins if the automatic flag is set to true */
  1493. if ( TGM_Plugin_Activation::$instance->is_automatic ) {
  1494. /** Flush plugins cache so we can make sure that the installed plugins list is always up to date */
  1495. wp_cache_flush();
  1496. /** Get the installed plugin file and activate it */
  1497. $plugin_info = $this->plugin_info( $package );
  1498. $activate = activate_plugin( $plugin_info );
  1499. /** Re-populate the file path now that the plugin has been installed and activated */
  1500. TGM_Plugin_Activation::$instance->populate_file_path();
  1501. /** Set correct strings based on results */
  1502. if ( is_wp_error( $activate ) ) {
  1503. $this->skin->error( $activate );
  1504. $this->skin->feedback( 'activation_failed' );
  1505. }
  1506. /** The plugin activation is successful */
  1507. else {
  1508. $this->skin->feedback( 'activation_success' );
  1509. }
  1510. }
  1511. /** Flush plugins cache so we can make sure that the installed plugins list is always up to date */
  1512. wp_cache_flush();
  1513. /** Set install footer strings */
  1514. $this->skin->after();
  1515. if ( ! $is_multi )
  1516. $this->skin->footer();
  1517. return $result;
  1518. }
  1519. /**
  1520. * Sets the correct install strings for the installer skin to use.
  1521. *
  1522. * @since 2.2.0
  1523. */
  1524. public function install_strings() {
  1525. $this->strings['no_package'] = __( 'Install package not available.', SH_NAME );
  1526. $this->strings['downloading_package'] = __( 'Downloading install package from <span class="code">%s</span>&#8230;', SH_NAME );
  1527. $this->strings['unpack_package'] = __( 'Unpacking the package&#8230;', SH_NAME );
  1528. $this->strings['installing_package'] = __( 'Installing the plugin&#8230;', SH_NAME );
  1529. $this->strings['process_failed'] = __( 'Plugin install failed.', SH_NAME );
  1530. $this->strings['process_success'] = __( 'Plugin installed successfully.', SH_NAME );
  1531. }
  1532. /**
  1533. * Sets the correct activation strings for the installer skin to use.
  1534. *
  1535. * @since 2.2.0
  1536. */
  1537. public function activate_strings() {
  1538. $this->strings['activation_failed'] = __( 'Plugin activation failed.', SH_NAME );
  1539. $this->strings['activation_success'] = __( 'Plugin activated successfully.', SH_NAME );
  1540. }
  1541. /**
  1542. * Grabs the plugin file from an installed plugin.
  1543. *
  1544. * @since 2.2.0
  1545. *
  1546. * @return string|boolean Return plugin file on success, false on failure
  1547. */
  1548. public function plugin_info() {
  1549. /** Return false if installation result isn't an array or the destination name isn't set */
  1550. if ( ! is_array( $this->result ) )
  1551. return false;
  1552. if ( empty( $this->result['destination_name'] ) )
  1553. return false;
  1554. /** Get the installed plugin file or return false if it isn't set */
  1555. $plugin = get_plugins( '/' . $this->result['destination_name'] );
  1556. if ( empty( $plugin ) )
  1557. return false;
  1558. /** Assume the requested plugin is the first in the list */
  1559. $pluginfiles = array_keys( $plugin );
  1560. return $this->result['destination_name'] . '/' . $pluginfiles[0];
  1561. }
  1562. }
  1563. }
  1564. if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
  1565. /**
  1566. * Installer skin to set strings for the bulk plugin installations..
  1567. *
  1568. * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
  1569. * plugins.
  1570. *
  1571. * @since 2.2.0
  1572. *
  1573. * @package TGM-Plugin-Activation
  1574. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  1575. * @author Gary Jones <gamajo@gamajo.com>
  1576. */
  1577. class TGM_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
  1578. /**
  1579. * Holds plugin info for each individual plugin installation.
  1580. *
  1581. * @since 2.2.0
  1582. *
  1583. * @var array
  1584. */
  1585. public $plugin_info = array();
  1586. /**
  1587. * Holds names of plugins that are undergoing bulk installations.
  1588. *
  1589. * @since 2.2.0
  1590. *
  1591. * @var array
  1592. */
  1593. public $plugin_names = array();
  1594. /**
  1595. * Integer to use for iteration through each plugin installation.
  1596. *
  1597. * @since 2.2.0
  1598. *
  1599. * @var integer
  1600. */
  1601. public $i = 0;
  1602. /**
  1603. * Constructor. Parses default args with new ones and extracts them for use.
  1604. *
  1605. * @since 2.2.0
  1606. *
  1607. * @param array $args Arguments to pass for use within the class
  1608. */
  1609. public function __construct( $args = array() ) {
  1610. /** Parse default and new args */
  1611. $defaults = array( 'url' => '', 'nonce' => '', 'names' => array() );
  1612. $args = wp_parse_args( $args, $defaults );
  1613. /** Set plugin names to $this->plugin_names property */
  1614. $this->plugin_names = $args['names'];
  1615. /** Extract the new args */
  1616. parent::__construct( $args );
  1617. }
  1618. /**
  1619. * Sets install skin strings for each individual plugin.
  1620. *
  1621. * Checks to see if the automatic activation flag is set and uses the
  1622. * the proper strings accordingly.
  1623. *
  1624. * @since 2.2.0
  1625. */
  1626. public function add_strings() {
  1627. /** Automatic activation strings */
  1628. if ( TGM_Plugin_Activation::$instance->is_automatic ) {
  1629. $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation and activation process is starting. This process may take a while on some hosts, so please be patient.', SH_NAME );
  1630. $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', SH_NAME ) . ' <a onclick="%2$s" href="#" class="hide-if-no-js"><span>' . __( 'Show Details', SH_NAME ) . '</span><span class="hidden">' . __( 'Hide Details', SH_NAME ) . '</span>.</a>';
  1631. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', SH_NAME );
  1632. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', SH_NAME );
  1633. }
  1634. /** Default installation strings */
  1635. else {
  1636. $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation process is starting. This process may take a while on some hosts, so please be patient.', SH_NAME );
  1637. $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', SH_NAME );
  1638. $this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', SH_NAME );
  1639. $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed successfully.', SH_NAME ) . ' <a onclick="%2$s" href="#" class="hide-if-no-js"><span>' . __( 'Show Details', SH_NAME ) . '</span><span class="hidden">' . __( 'Hide Details', SH_NAME ) . '</span>.</a>';
  1640. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations have been completed.', SH_NAME );
  1641. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', SH_NAME );
  1642. }
  1643. }
  1644. /**
  1645. * Outputs the header strings and necessary JS before each plugin installation.
  1646. *
  1647. * @since 2.2.0
  1648. */
  1649. public function before($title = '') {
  1650. /** We are currently in the plugin installation loop, so set to true */
  1651. $this->in_loop = true;
  1652. printf( '<h4>' . $this->upgrader->strings['skin_before_update_header'] . ' <img alt="" src="' . admin_url( 'images/wpspin_light.gif' ) . '" class="hidden waiting-' . $this->upgrader->update_current . '" style="vertical-align:middle;" /></h4>', $this->plugin_names[$this->i], $this->upgrader->update_current, $this->upgrader->update_count );
  1653. echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').show();</script>';
  1654. echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr( $this->upgrader->update_current ) . '"><p>';
  1655. /** Flush header output buffer */
  1656. $this->before_flush_output();
  1657. }
  1658. /**
  1659. * Outputs the footer strings and necessary JS after each plugin installation.
  1660. *
  1661. * Checks for any errors and outputs them if they exist, else output
  1662. * success strings.
  1663. *
  1664. * @since 2.2.0
  1665. */
  1666. public function after($title = '') {
  1667. /** Close install strings */
  1668. echo '</p></div>';
  1669. /** Output error strings if an error has occurred */
  1670. if ( $this->error || ! $this->result ) {
  1671. if ( $this->error )
  1672. echo '<div class="error"><p>' . sprintf( sh_set($this->upgrader->strings, 'skin_update_failed_error'), $this->plugin_names[$this->i], $this->error ) . '</p></div>';
  1673. else
  1674. echo '<div class="error"><p>' . sprintf( sh_set($this->upgrader->strings, 'skin_update_failed'), $this->plugin_names[$this->i] ) . '</p></div>';
  1675. echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js( $this->upgrader->update_current ) . '\').show();</script>';
  1676. }
  1677. /** If the result is set and there are no errors, success! */
  1678. if ( ! empty( $this->result ) && ! is_wp_error( $this->result ) ) {
  1679. echo '<div class="updated"><p>' . sprintf( sh_set($this->upgrader->strings, 'skin_update_successful'), $this->plugin_names[$this->i], 'jQuery(\'#progress-' . esc_js( $this->upgrader->update_current ) . '\').toggle();jQuery(\'span\', this).toggle(); return false;' ) . '</p></div>';
  1680. echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').hide();</script>';
  1681. }
  1682. /** Set in_loop and error to false and flush footer output buffer */
  1683. $this->reset();
  1684. $this->after_flush_output();
  1685. }
  1686. /**
  1687. * Outputs links after bulk plugin installation is complete.
  1688. *
  1689. * @since 2.2.0
  1690. */
  1691. public function bulk_footer() {
  1692. /** Serve up the string to say installations (and possibly activations) are complete */
  1693. parent::bulk_footer();
  1694. /** Flush plugins cache so we can make sure that the installed plugins list is always up to date */
  1695. wp_cache_flush();
  1696. /** Display message based on if all plugins are now active or not */
  1697. $complete = array();
  1698. foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin ) {
  1699. if ( ! is_plugin_active( $plugin['file_path'] ) ) {
  1700. echo '<p><a href="' . add_query_arg( 'page', TGM_Plugin_Activation::$instance->menu, admin_url( TGM_Plugin_Activation::$instance->parent_url_slug ) ) . '" title="' . esc_attr( TGM_Plugin_Activation::$instance->strings['return'] ) . '" target="_parent">' . TGM_Plugin_Activation::$instance->strings['return'] . '</a></p>';
  1701. $complete[] = $plugin;
  1702. printr($complete);
  1703. break;
  1704. }
  1705. /** Nothing to store */
  1706. else {
  1707. $complete[] = '';
  1708. }
  1709. }
  1710. /** Filter out any empty entries */
  1711. $complete = array_filter( $complete );
  1712. /** All plugins are active, so we display the complete string and hide the menu to protect users */
  1713. if ( empty( $complete ) ) {
  1714. echo '<p>' . sprintf( TGM_Plugin_Activation::$instance->strings['complete'], '<a href="' . admin_url() . '" title="' . __( 'Return to the Dashboard', SH_NAME ) . '">' . __( 'Return to the Dashboard', SH_NAME ) . '</a>' ) . '</p>';
  1715. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  1716. }
  1717. }
  1718. /**
  1719. * Flush header output buffer.
  1720. *
  1721. * @since 2.2.0
  1722. */
  1723. public function before_flush_output() {
  1724. wp_ob_end_flush_all();
  1725. flush();
  1726. }
  1727. /**
  1728. * Flush footer output buffer and iterate $this->i to make sure the
  1729. * installation strings reference the correct plugin.
  1730. *
  1731. * @since 2.2.0
  1732. */
  1733. public function after_flush_output() {
  1734. wp_ob_end_flush_all();
  1735. flush();
  1736. $this->i++;
  1737. }
  1738. }
  1739. }
  1740. }