Advertisement
Guest User

TGM-Plugin-Activation

a guest
Aug 1st, 2013
2,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 76.83 KB | None | 0 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. /*
  15. Copyright 2012 Thomas Griffin (email : thomas@thomasgriffinmedia.com)
  16.  
  17. This program is free software; you can redistribute it and/or modify
  18. it under the terms of the GNU General Public License, version 3, as
  19. published by the Free Software Foundation.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30.  
  31. if ( ! class_exists( 'TGM_Plugin_Activation' ) ) {
  32. /**
  33. * Automatic plugin installation and activation library.
  34. *
  35. * Creates a way to automatically install and activate plugins from within themes.
  36. * The plugins can be either pre-packaged, downloaded from the WordPress
  37. * Plugin Repository or downloaded from a private repository.
  38. *
  39. * @since 1.0.0
  40. *
  41. * @package TGM-Plugin-Activation
  42. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  43. * @author Gary Jones <gamajo@gamajo.com>
  44. */
  45. class TGM_Plugin_Activation {
  46.  
  47. /**
  48. * Holds a copy of itself, so it can be referenced by the class name.
  49. *
  50. * @since 1.0.0
  51. *
  52. * @var TGM_Plugin_Activation
  53. */
  54. static $instance;
  55.  
  56. /**
  57. * Holds arrays of plugin details.
  58. *
  59. * @since 1.0.0
  60. *
  61. * @var array
  62. */
  63. public $plugins = array();
  64.  
  65. /**
  66. * Parent menu slug for plugins page.
  67. *
  68. * @since 2.2.0
  69. *
  70. * @var string Parent menu slug. Defaults to 'themes.php'.
  71. */
  72. public $parent_menu_slug = 'themes.php';
  73.  
  74. /**
  75. * Parent URL slug for URL references.
  76. *
  77. * This is useful if you want to place the custom plugins page as a
  78. * submenu item under a custom parent menu.
  79. *
  80. * @since 2.2.0
  81. *
  82. * @var string Parent URL slug. Defaults to 'themes.php'.
  83. */
  84. public $parent_url_slug = 'themes.php';
  85.  
  86. /**
  87. * Name of the querystring argument for the admin page.
  88. *
  89. * @since 1.0.0
  90. *
  91. * @var string
  92. */
  93. public $menu = 'install-required-plugins';
  94.  
  95. /**
  96. * Text domain for localization support.
  97. *
  98. * @since 1.1.0
  99. *
  100. * @var string
  101. */
  102. public $domain = 'tgmpa';
  103.  
  104. /**
  105. * Default absolute path to folder containing pre-packaged plugin zip files.
  106. *
  107. * @since 2.0.0
  108. *
  109. * @var string Absolute path prefix to packaged zip file location. Default is empty string.
  110. */
  111. public $default_path = '';
  112.  
  113. /**
  114. * Flag to show admin notices or not.
  115. *
  116. * @since 2.1.0
  117. *
  118. * @var boolean
  119. */
  120. public $has_notices = true;
  121.  
  122. /**
  123. * Flag to set automatic activation of plugins. Off by default.
  124. *
  125. * @since 2.2.0
  126. *
  127. * @var boolean
  128. */
  129. public $is_automatic = false;
  130.  
  131. /**
  132. * Optional message to display before the plugins table.
  133. *
  134. * @since 2.2.0
  135. *
  136. * @var string Message filtered by wp_kses_post(). Default is empty string.
  137. */
  138. public $message = '';
  139.  
  140. /**
  141. * Holds configurable array of strings.
  142. *
  143. * Default values are added in the constructor.
  144. *
  145. * @since 2.0.0
  146. *
  147. * @var array
  148. */
  149. public $strings = array();
  150.  
  151. /**
  152. * Adds a reference of this object to $instance, populates default strings,
  153. * does the tgmpa_init action hook, and hooks in the interactions to init.
  154. *
  155. * @since 1.0.0
  156. *
  157. * @see TGM_Plugin_Activation::init()
  158. */
  159. public function __construct() {
  160.  
  161. self::$instance =& $this;
  162.  
  163. $this->strings = array(
  164. 'page_title' => __( 'Install Required Plugins', $this->domain ),
  165. 'menu_title' => __( 'Install Plugins', $this->domain ),
  166. 'installing' => __( 'Installing Plugin: %s', $this->domain ),
  167. 'oops' => __( 'Something went wrong.', $this->domain ),
  168. 'notice_can_install_required' => _n_noop( 'This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.' ),
  169. 'notice_can_install_recommended' => _n_noop( 'This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.' ),
  170. '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.' ),
  171. 'notice_can_activate_required' => _n_noop( 'The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.' ),
  172. 'notice_can_activate_recommended' => _n_noop( 'The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.' ),
  173. '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.' ),
  174. '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.' ),
  175. '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.' ),
  176. 'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins' ),
  177. 'activate_link' => _n_noop( 'Activate installed plugin', 'Activate installed plugins' ),
  178. 'return' => __( 'Return to Required Plugins Installer', $this->domain ),
  179. 'plugin_activated' => __( 'Plugin activated successfully.', $this->domain ),
  180. 'complete' => __( 'All plugins installed and activated successfully. %1$s', $this->domain ),
  181. );
  182.  
  183. /** Annouce that the class is ready, and pass the object (for advanced use) */
  184. do_action_ref_array( 'tgmpa_init', array( &$this ) );
  185.  
  186. /** When the rest of WP has loaded, kick-start the rest of the class */
  187. add_action( 'init', array( &$this, 'init' ) );
  188.  
  189. }
  190.  
  191. /**
  192. * Initialise the interactions between this class and WordPress.
  193. *
  194. * Hooks in three new methods for the class: admin_menu, notices and styles.
  195. *
  196. * @since 2.0.0
  197. *
  198. * @see TGM_Plugin_Activation::admin_menu()
  199. * @see TGM_Plugin_Activation::notices()
  200. * @see TGM_Plugin_Activation::styles()
  201. */
  202. public function init() {
  203.  
  204. do_action( 'tgmpa_register' );
  205. /** After this point, the plugins should be registered and the configuration set */
  206.  
  207. /** Proceed only if we have plugins to handle */
  208. if ( $this->plugins ) {
  209. $sorted = array(); // Prepare variable for sorting
  210.  
  211. foreach ( $this->plugins as $plugin )
  212. $sorted[] = $plugin['name'];
  213.  
  214. array_multisort( $sorted, SORT_ASC, $this->plugins ); // Sort plugins alphabetically by name
  215.  
  216. add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
  217. add_action( 'admin_head', array( &$this, 'dismiss' ) );
  218. add_filter( 'install_plugin_complete_actions', array( &$this, 'actions' ) );
  219.  
  220. /** Load admin bar in the header to remove flash when installing plugins */
  221. if ( $this->is_tgmpa_page() ) {
  222. remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
  223. remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
  224. add_action( 'wp_head', 'wp_admin_bar_render', 1000 );
  225. add_action( 'admin_head', 'wp_admin_bar_render', 1000 );
  226. }
  227.  
  228. if ( $this->has_notices ) {
  229. add_action( 'admin_notices', array( &$this, 'notices' ) );
  230. add_action( 'admin_init', array( &$this, 'admin_init' ), 1 );
  231. add_action( 'admin_enqueue_scripts', array( &$this, 'thickbox' ) );
  232. add_action( 'switch_theme', array( &$this, 'update_dismiss' ) );
  233. }
  234.  
  235. /** Setup the force activation hook */
  236. foreach ( $this->plugins as $plugin ) {
  237. if ( isset( $plugin['force_activation'] ) && true === $plugin['force_activation'] ) {
  238. add_action( 'admin_init', array( &$this, 'force_activation' ) );
  239. break;
  240. }
  241. }
  242.  
  243. /** Setup the force deactivation hook */
  244. foreach ( $this->plugins as $plugin ) {
  245. if ( isset( $plugin['force_deactivation'] ) && true === $plugin['force_deactivation'] ) {
  246. add_action( 'switch_theme', array( &$this, 'force_deactivation' ) );
  247. break;
  248. }
  249. }
  250. }
  251.  
  252. }
  253.  
  254. /**
  255. * Handles calls to show plugin information via links in the notices.
  256. *
  257. * We get the links in the admin notices to point to the TGMPA page, rather
  258. * than the typical plugin-install.php file, so we can prepare everything
  259. * beforehand.
  260. *
  261. * WP doesn't make it easy to show the plugin information in the thickbox -
  262. * here we have to require a file that includes a function that does the
  263. * main work of displaying it, enqueue some styles, set up some globals and
  264. * finally call that function before exiting.
  265. *
  266. * Down right easy once you know how...
  267. *
  268. * @since 2.1.0
  269. *
  270. * @global string $tab Used as iframe div class names, helps with styling
  271. * @global string $body_id Used as the iframe body ID, helps with styling
  272. * @return null Returns early if not the TGMPA page.
  273. */
  274. public function admin_init() {
  275.  
  276. if ( ! $this->is_tgmpa_page() )
  277. return;
  278.  
  279. if ( isset( $_REQUEST['tab'] ) && 'plugin-information' == $_REQUEST['tab'] ) {
  280. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for install_plugin_information()
  281.  
  282. wp_enqueue_style( 'plugin-install' );
  283.  
  284. global $tab, $body_id;
  285. $body_id = $tab = 'plugin-information';
  286.  
  287. install_plugin_information();
  288.  
  289. exit;
  290. }
  291.  
  292. }
  293.  
  294. /**
  295. * Enqueues thickbox scripts/styles for plugin info.
  296. *
  297. * Thickbox is not automatically included on all admin pages, so we must
  298. * manually enqueue it for those pages.
  299. *
  300. * Thickbox is only loaded if the user has not dismissed the admin
  301. * notice or if there are any plugins left to install and activate.
  302. *
  303. * @since 2.1.0
  304. */
  305. public function thickbox() {
  306.  
  307. if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice', true ) )
  308. add_thickbox();
  309.  
  310. }
  311.  
  312. /**
  313. * Adds submenu page under 'Appearance' tab.
  314. *
  315. * This method adds the submenu page letting users know that a required
  316. * plugin needs to be installed.
  317. *
  318. * This page disappears once the plugin has been installed and activated.
  319. *
  320. * @since 1.0.0
  321. *
  322. * @see TGM_Plugin_Activation::init()
  323. * @see TGM_Plugin_Activation::install_plugins_page()
  324. */
  325. public function admin_menu() {
  326.  
  327. // Make sure privileges are correct to see the page
  328. if ( ! current_user_can( 'install_plugins' ) )
  329. return;
  330.  
  331. $this->populate_file_path();
  332.  
  333. foreach ( $this->plugins as $plugin ) {
  334. if ( ! is_plugin_active( $plugin['file_path'] ) ) {
  335. add_submenu_page(
  336. $this->parent_menu_slug, // Parent menu slug
  337. $this->strings['page_title'], // Page title
  338. $this->strings['menu_title'], // Menu title
  339. 'edit_theme_options', // Capability
  340. $this->menu, // Menu slug
  341. array( &$this, 'install_plugins_page' ) // Callback
  342. );
  343. break;
  344. }
  345. }
  346.  
  347. }
  348.  
  349. /**
  350. * Echoes plugin installation form.
  351. *
  352. * This method is the callback for the admin_menu method function.
  353. * This displays the admin page and form area where the user can select to install and activate the plugin.
  354. *
  355. * @since 1.0.0
  356. *
  357. * @return null Aborts early if we're processing a plugin installation action
  358. */
  359. public function install_plugins_page() {
  360.  
  361. /** Store new instance of plugin table in object */
  362. $plugin_table = new TGMPA_List_Table;
  363.  
  364. /** Return early if processing a plugin installation action */
  365. if ( isset( $_POST[sanitize_key( 'action' )] ) && 'tgmpa-bulk-install' == $_POST[sanitize_key( 'action' )] && $plugin_table->process_bulk_actions() || $this->do_plugin_install() )
  366. return;
  367.  
  368. ?>
  369. <div class="tgmpa wrap">
  370.  
  371. <?php screen_icon( apply_filters( 'tgmpa_default_screen_icon', 'themes' ) ); ?>
  372. <h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
  373. <?php $plugin_table->prepare_items(); ?>
  374.  
  375. <?php if ( isset( $this->message ) ) _e( wp_kses_post( $this->message ), $this->domain ); ?>
  376.  
  377. <form id="tgmpa-plugins" action="" method="post">
  378. <input type="hidden" name="tgmpa-page" value="<?php echo $this->menu; ?>" />
  379. <?php $plugin_table->display(); ?>
  380. </form>
  381.  
  382. </div>
  383. <?php
  384.  
  385. }
  386.  
  387. /**
  388. * Installs a plugin or activates a plugin depending on the hover
  389. * link clicked by the user.
  390. *
  391. * Checks the $_GET variable to see which actions have been
  392. * passed and responds with the appropriate method.
  393. *
  394. * Uses WP_Filesystem to process and handle the plugin installation
  395. * method.
  396. *
  397. * @since 1.0.0
  398. *
  399. * @uses WP_Filesystem
  400. * @uses WP_Error
  401. * @uses WP_Upgrader
  402. * @uses Plugin_Upgrader
  403. * @uses Plugin_Installer_Skin
  404. *
  405. * @return boolean True on success, false on failure
  406. */
  407. protected function do_plugin_install() {
  408.  
  409. /** All plugin information will be stored in an array for processing */
  410. $plugin = array();
  411.  
  412. /** Checks for actions from hover links to process the installation */
  413. if ( isset( $_GET[sanitize_key( 'plugin' )] ) && ( isset( $_GET[sanitize_key( 'tgmpa-install' )] ) && 'install-plugin' == $_GET[sanitize_key( 'tgmpa-install' )] ) ) {
  414. check_admin_referer( 'tgmpa-install' );
  415.  
  416. $plugin['name'] = $_GET[sanitize_key( 'plugin_name' )]; // Plugin name
  417. $plugin['slug'] = $_GET[sanitize_key( 'plugin' )]; // Plugin slug
  418. $plugin['source'] = $_GET[sanitize_key( 'plugin_source' )]; // Plugin source
  419.  
  420. /** Pass all necessary information via URL if WP_Filesystem is needed */
  421. $url = wp_nonce_url(
  422. add_query_arg(
  423. array(
  424. 'page' => $this->menu,
  425. 'plugin' => $plugin['slug'],
  426. 'plugin_name' => $plugin['name'],
  427. 'plugin_source' => $plugin['source'],
  428. 'tgmpa-install' => 'install-plugin',
  429. ),
  430. admin_url( $this->parent_url_slug )
  431. ),
  432. 'tgmpa-install'
  433. );
  434. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary
  435. $fields = array( sanitize_key( 'tgmpa-install' ) ); // Extra fields to pass to WP_Filesystem
  436.  
  437. if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, $fields ) ) )
  438. return true;
  439.  
  440. if ( ! WP_Filesystem( $creds ) ) {
  441. request_filesystem_credentials( $url, $method, true, false, $fields ); // Setup WP_Filesystem
  442. return true;
  443. }
  444.  
  445. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for plugins_api
  446. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes
  447.  
  448. /** Set plugin source to WordPress API link if available */
  449. if ( isset( $plugin['source'] ) && 'repo' == $plugin['source'] ) {
  450. $api = plugins_api( 'plugin_information', array( 'slug' => $plugin['slug'], 'fields' => array( 'sections' => false ) ) );
  451.  
  452. if ( is_wp_error( $api ) )
  453. wp_die( $this->strings['oops'] . var_dump( $api ) );
  454.  
  455. if ( isset( $api->download_link ) )
  456. $plugin['source'] = $api->download_link;
  457. }
  458.  
  459. /** Set type, based on whether the source starts with http:// or https:// */
  460. $type = preg_match( '|^http(s)?://|', $plugin['source'] ) ? 'web' : 'upload';
  461.  
  462. /** Prep variables for Plugin_Installer_Skin class */
  463. $title = sprintf( $this->strings['installing'], $plugin['name'] );
  464. $url = add_query_arg( array( 'action' => 'install-plugin', 'plugin' => $plugin['slug'] ), 'update.php' );
  465. if ( isset( $_GET['from'] ) )
  466. $url .= add_query_arg( 'from', urlencode( stripslashes( $_GET['from'] ) ), $url );
  467.  
  468. $nonce = 'install-plugin_' . $plugin['slug'];
  469.  
  470. /** Prefix a default path to pre-packaged plugins */
  471. $source = ( 'upload' == $type ) ? $this->default_path . $plugin['source'] : $plugin['source'];
  472.  
  473. /** Create a new instance of Plugin_Upgrader */
  474. $upgrader = new Plugin_Upgrader( $skin = new Plugin_Installer_Skin( compact( 'type', 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
  475.  
  476. /** Perform the action and install the plugin from the $source urldecode() */
  477. $upgrader->install( $source );
  478.  
  479. /** Flush plugins cache so we can make sure that the installed plugins list is always up to date */
  480. wp_cache_flush();
  481.  
  482. /** Only activate plugins if the config option is set to true */
  483. if ( $this->is_automatic ) {
  484. $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method
  485. $activate = activate_plugin( $plugin_activate ); // Activate the plugin
  486. $this->populate_file_path(); // Re-populate the file path now that the plugin has been installed and activated
  487.  
  488. if ( is_wp_error( $activate ) ) {
  489. echo '<div id="message" class="error"><p>' . $activate->get_error_message() . '</p></div>';
  490. 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', $this->domain ) . '</a></p>';
  491. return true; // End it here if there is an error with automatic activation
  492. }
  493. else {
  494. echo '<p>' . $this->strings['plugin_activated'] . '</p>';
  495. }
  496. }
  497.  
  498. /** Display message based on if all plugins are now active or not */
  499. $complete = array();
  500. foreach ( $this->plugins as $plugin ) {
  501. if ( ! is_plugin_active( $plugin['file_path'] ) ) {
  502. 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'], $this->domain ) . '</a></p>';
  503. $complete[] = $plugin;
  504. break;
  505. }
  506. /** Nothing to store */
  507. else {
  508. $complete[] = '';
  509. }
  510. }
  511.  
  512. /** Filter out any empty entries */
  513. $complete = array_filter( $complete );
  514.  
  515. /** All plugins are active, so we display the complete string and hide the plugin menu */
  516. if ( empty( $complete ) ) {
  517. echo '<p>' . sprintf( $this->strings['complete'], '<a href="' . admin_url() . '" title="' . __( 'Return to the Dashboard', $this->domain ) . '">' . __( 'Return to the Dashboard', $this->domain ) . '</a>' ) . '</p>';
  518. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  519. }
  520.  
  521. return true;
  522. }
  523. /** Checks for actions from hover links to process the activation */
  524. elseif ( isset( $_GET[sanitize_key( 'plugin' )] ) && ( isset( $_GET[sanitize_key( 'tgmpa-activate' )] ) && 'activate-plugin' == $_GET[sanitize_key( 'tgmpa-activate' )] ) ) {
  525. check_admin_referer( 'tgmpa-activate', 'tgmpa-activate-nonce' );
  526.  
  527. /** Populate $plugin array with necessary information */
  528. $plugin['name'] = $_GET[sanitize_key( 'plugin_name' )];
  529. $plugin['slug'] = $_GET[sanitize_key( 'plugin' )];
  530. $plugin['source'] = $_GET[sanitize_key( 'plugin_source' )];
  531.  
  532. $plugin_data = get_plugins( '/' . $plugin['slug'] ); // Retrieve all plugins
  533. $plugin_file = array_keys( $plugin_data ); // Retrieve all plugin files from installed plugins
  534. $plugin_to_activate = $plugin['slug'] . '/' . $plugin_file[0]; // Match plugin slug with appropriate plugin file
  535. $activate = activate_plugin( $plugin_to_activate ); // Activate the plugin
  536.  
  537. if ( is_wp_error( $activate ) ) {
  538. echo '<div id="message" class="error"><p>' . $activate->get_error_message() . '</p></div>';
  539. 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'], $this->domain ) . '</a></p>';
  540. return true; // End it here if there is an error with activation
  541. }
  542. else {
  543. /** Make sure message doesn't display again if bulk activation is performed immediately after a single activation */
  544. if ( ! isset( $_POST[sanitize_key( 'action' )] ) ) {
  545. $msg = sprintf( __( 'The following plugin was activated successfully: %s.', $this->domain ), '<strong>' . $plugin['name'] . '</strong>' );
  546. echo '<div id="message" class="updated"><p>' . $msg . '</p></div>';
  547. }
  548. }
  549. }
  550.  
  551. return false;
  552.  
  553. }
  554.  
  555. /**
  556. * Echoes required plugin notice.
  557. *
  558. * Outputs a message telling users that a specific plugin is required for
  559. * their theme. If appropriate, it includes a link to the form page where
  560. * users can install and activate the plugin.
  561. *
  562. * @since 1.0.0
  563. *
  564. * @global object $current_screen
  565. * @return null Returns early if we're on the Install page
  566. */
  567. public function notices() {
  568.  
  569. global $current_screen;
  570.  
  571. /** Remove nag on the install page */
  572. if ( $this->is_tgmpa_page() )
  573. return;
  574.  
  575. $installed_plugins = get_plugins(); // Retrieve a list of all the plugins
  576. $this->populate_file_path();
  577.  
  578. $message = array(); // Store the messages in an array to be outputted after plugins have looped through
  579. $install_link = false; // Set to false, change to true in loop if conditions exist, used for action link 'install'
  580. $install_link_count = 0; // Used to determine plurality of install action link text
  581. $activate_link = false; // Set to false, change to true in loop if conditions exist, used for action link 'activate'
  582. $activate_link_count = 0; // Used to determine plurality of activate action link text
  583.  
  584. foreach ( $this->plugins as $plugin ) {
  585. /** If the plugin is installed and active, check for minimum version argument before moving forward */
  586. if ( is_plugin_active( $plugin['file_path'] ) ) {
  587. /** A minimum version has been specified */
  588. if ( isset( $plugin['version'] ) ) {
  589. if ( isset( $installed_plugins[$plugin['file_path']]['Version'] ) ) {
  590. /** If the current version is less than the minimum required version, we display a message */
  591. if ( version_compare( $installed_plugins[$plugin['file_path']]['Version'], $plugin['version'], '<' ) ) {
  592. if ( current_user_can( 'install_plugins' ) )
  593. $message['notice_ask_to_update'][] = $plugin['name'];
  594. else
  595. $message['notice_cannot_update'][] = $plugin['name'];
  596. }
  597. }
  598. /** Can't find the plugin, so iterate to the next condition */
  599. else {
  600. continue;
  601. }
  602. }
  603. /** No minimum version specified, so iterate over the plugin */
  604. else {
  605. continue;
  606. }
  607. }
  608.  
  609. /** Not installed */
  610. if ( ! isset( $installed_plugins[$plugin['file_path']] ) ) {
  611. $install_link = true; // We need to display the 'install' action link
  612. $install_link_count++; // Increment the install link count
  613. if ( current_user_can( 'install_plugins' ) ) {
  614. if ( $plugin['required'] )
  615. $message['notice_can_install_required'][] = $plugin['name'];
  616. /** This plugin is only recommended */
  617. else
  618. $message['notice_can_install_recommended'][] = $plugin['name'];
  619. }
  620. /** Need higher privileges to install the plugin */
  621. else {
  622. $message['notice_cannot_install'][] = $plugin['name'];
  623. }
  624. }
  625. /** Installed but not active */
  626. elseif ( is_plugin_inactive( $plugin['file_path'] ) ) {
  627. $activate_link = true; // We need to display the 'activate' action link
  628. $activate_link_count++; // Increment the activate link count
  629. if ( current_user_can( 'activate_plugins' ) ) {
  630. if ( ( isset( $plugin['required'] ) ) && ( $plugin['required'] ) )
  631. $message['notice_can_activate_required'][] = $plugin['name'];
  632. /** This plugin is only recommended */
  633. else {
  634. $message['notice_can_activate_recommended'][] = $plugin['name'];
  635. }
  636. }
  637. /** Need higher privileges to activate the plugin */
  638. else {
  639. $message['notice_cannot_activate'][] = $plugin['name'];
  640. }
  641. }
  642. }
  643.  
  644. /** Only process the nag messages if the user has not dismissed them already */
  645. if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice', true ) ) {
  646. /** If we have notices to display, we move forward */
  647. if ( ! empty( $message ) ) {
  648. krsort( $message ); // Sort messages
  649. $rendered = ''; // Display all nag messages as strings
  650.  
  651. /** Grab all plugin names */
  652. foreach ( $message as $type => $plugin_groups ) {
  653. $linked_plugin_groups = array();
  654.  
  655. /** Count number of plugins in each message group to calculate singular/plural message */
  656. $count = count( $plugin_groups );
  657.  
  658. /** Loop through the plugin names to make the ones pulled from the .org repo linked */
  659. foreach ( $plugin_groups as $plugin_group_single_name ) {
  660. $external_url = $this->_get_plugin_data_from_name( $plugin_group_single_name, 'external_url' );
  661. $source = $this->_get_plugin_data_from_name( $plugin_group_single_name, 'source' );
  662.  
  663. if ( $external_url && preg_match( '|^http(s)?://|', $external_url ) ) {
  664. $linked_plugin_groups[] = '<a href="' . esc_url( $external_url ) . '" title="' . $plugin_group_single_name . '" target="_blank">' . $plugin_group_single_name . '</a>';
  665. }
  666. elseif ( ! $source || preg_match( '|^http://wordpress.org/extend/plugins/|', $source ) ) {
  667. $url = add_query_arg(
  668. array(
  669. 'tab' => 'plugin-information',
  670. 'plugin' => $this->_get_plugin_data_from_name( $plugin_group_single_name ),
  671. 'TB_iframe' => 'true',
  672. 'width' => '640',
  673. 'height' => '500',
  674. ),
  675. admin_url( 'plugin-install.php' )
  676. );
  677.  
  678. $linked_plugin_groups[] = '<a href="' . esc_url( $url ) . '" class="thickbox" title="' . $plugin_group_single_name . '">' . $plugin_group_single_name . '</a>';
  679. }
  680. else {
  681. $linked_plugin_groups[] = $plugin_group_single_name; // No hyperlink
  682. }
  683.  
  684. if ( isset( $linked_plugin_groups ) && (array) $linked_plugin_groups )
  685. $plugin_groups = $linked_plugin_groups;
  686. }
  687.  
  688. $last_plugin = array_pop( $plugin_groups ); // Pop off last name to prep for readability
  689. $imploded = empty( $plugin_groups ) ? '<em>' . $last_plugin . '</em>' : '<em>' . ( implode( ', ', $plugin_groups ) . '</em> and <em>' . $last_plugin . '</em>' );
  690.  
  691. $rendered .= '<p>' . sprintf( translate_nooped_plural( $this->strings[$type], $count, $this->domain ), $imploded, $count ) . '</p>'; // All messages now stored
  692. }
  693.  
  694. /** Setup variables to determine if action links are needed */
  695. $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, $this->domain ) . '</a>' : '';
  696. $show_activate_link = $activate_link ? '<a href="' . admin_url( 'plugins.php' ) . '">' . translate_nooped_plural( $this->strings['activate_link'], $activate_link_count, $this->domain ) . '</a>' : '';
  697.  
  698. /** Define all of the action links */
  699. $action_links = apply_filters(
  700. 'tgmpa_notice_action_links',
  701. array(
  702. 'install' => ( current_user_can( 'install_plugins' ) ) ? $show_install_link : '',
  703. 'activate' => ( current_user_can( 'activate_plugins' ) ) ? $show_activate_link : '',
  704. 'dismiss' => '<a class="dismiss-notice" href="' . add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ) . '" target="_parent">' . __( 'Dismiss this notice', $this->domain ) . '</a>',
  705. )
  706. );
  707.  
  708. $action_links = array_filter( $action_links ); // Remove any empty array items
  709. if ( $action_links )
  710. $rendered .= '<p>' . implode( ' | ', $action_links ) . '</p>';
  711.  
  712. /** Register the nag messages and prepare them to be processed */
  713. if ( isset( $this->strings['nag_type'] ) )
  714. add_settings_error( 'tgmpa', 'tgmpa', $rendered, sanitize_html_class( strtolower( $this->strings['nag_type'] ), 'updated' ) );
  715. else
  716. add_settings_error( 'tgmpa', 'tgmpa', $rendered, 'updated' );
  717. }
  718. }
  719.  
  720. /** Admin options pages already output settings_errors, so this is to avoid duplication */
  721. if ( 'options-general' !== $current_screen->parent_base )
  722. settings_errors( 'tgmpa' );
  723.  
  724. }
  725.  
  726. /**
  727. * Add dismissable admin notices.
  728. *
  729. * Appends a link to the admin nag messages. If clicked, the admin notice disappears and no longer is visible to users.
  730. *
  731. * @since 2.1.0
  732. */
  733. public function dismiss() {
  734.  
  735. if ( isset( $_GET[sanitize_key( 'tgmpa-dismiss' )] ) )
  736. update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice', 1 );
  737.  
  738. }
  739.  
  740. /**
  741. * Add individual plugin to our collection of plugins.
  742. *
  743. * If the required keys are not set, the plugin is not added.
  744. *
  745. * @since 2.0.0
  746. *
  747. * @param array $plugin Array of plugin arguments.
  748. */
  749. public function register( $plugin ) {
  750.  
  751. if ( ! isset( $plugin['slug'] ) || ! isset( $plugin['name'] ) )
  752. return;
  753.  
  754. $this->plugins[] = $plugin;
  755.  
  756. }
  757.  
  758. /**
  759. * Amend default configuration settings.
  760. *
  761. * @since 2.0.0
  762. *
  763. * @param array $config
  764. */
  765. public function config( $config ) {
  766.  
  767. $keys = array( 'default_path', 'parent_menu_slug', 'parent_url_slug', 'domain', 'has_notices', 'menu', 'is_automatic', 'message', 'strings' );
  768.  
  769. foreach ( $keys as $key ) {
  770. if ( isset( $config[$key] ) ) {
  771. if ( is_array( $config[$key] ) ) {
  772. foreach ( $config[$key] as $subkey => $value )
  773. $this->{$key}[$subkey] = $value;
  774. } else {
  775. $this->$key = $config[$key];
  776. }
  777. }
  778. }
  779.  
  780. }
  781.  
  782. /**
  783. * Amend action link after plugin installation.
  784. *
  785. * @since 2.0.0
  786. *
  787. * @param array $install_actions Existing array of actions
  788. * @return array Amended array of actions
  789. */
  790. public function actions( $install_actions ) {
  791.  
  792. /** Remove action links on the TGMPA install page */
  793. if ( $this->is_tgmpa_page() )
  794. return false;
  795.  
  796. return $install_actions;
  797.  
  798. }
  799.  
  800. /**
  801. * Set file_path key for each installed plugin.
  802. *
  803. * @since 2.1.0
  804. */
  805. public function populate_file_path() {
  806.  
  807. /** Add file_path key for all plugins */
  808. foreach ( $this->plugins as $plugin => $values )
  809. $this->plugins[$plugin]['file_path'] = $this->_get_plugin_basename_from_slug( $values['slug'] );
  810.  
  811. }
  812.  
  813. /**
  814. * Helper function to extract the file path of the plugin file from the
  815. * plugin slug, if the plugin is installed.
  816. *
  817. * @since 2.0.0
  818. *
  819. * @param string $slug Plugin slug (typically folder name) as provided by the developer
  820. * @return string Either file path for plugin if installed, or just the plugin slug
  821. */
  822. protected function _get_plugin_basename_from_slug( $slug ) {
  823.  
  824. $keys = array_keys( get_plugins() );
  825.  
  826. foreach ( $keys as $key ) {
  827. if ( preg_match( '|^' . $slug .'|', $key ) )
  828. return $key;
  829. }
  830.  
  831. return $slug;
  832.  
  833. }
  834.  
  835. /**
  836. * Retrieve plugin data, given the plugin name.
  837. *
  838. * Loops through the registered plugins looking for $name. If it finds it,
  839. * it returns the $data from that plugin. Otherwise, returns false.
  840. *
  841. * @since 2.1.0
  842. *
  843. * @param string $name Name of the plugin, as it was registered
  844. * @param string $data Optional. Array key of plugin data to return. Default is slug
  845. * @return string|boolean Plugin slug if found, false otherwise.
  846. */
  847. protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
  848.  
  849. foreach ( $this->plugins as $plugin => $values ) {
  850. if ( $name == $values['name'] && isset( $values[$data] ) )
  851. return $values[$data];
  852. }
  853.  
  854. return false;
  855.  
  856. }
  857.  
  858. /**
  859. * Determine if we're on the TGMPA Install page.
  860. *
  861. * We use $current_screen when it is available, and a slightly less ideal
  862. * conditional when it isn't (like when displaying the plugin information
  863. * thickbox).
  864. *
  865. * @since 2.1.0
  866. *
  867. * @global object $current_screen
  868. * @return boolean True when on the TGMPA page, false otherwise.
  869. */
  870. protected function is_tgmpa_page() {
  871.  
  872. global $current_screen;
  873.  
  874. if ( ! is_null( $current_screen ) && $this->parent_menu_slug == $current_screen->parent_file && isset( $_GET['page'] ) && $this->menu === $_GET['page'] )
  875. return true;
  876.  
  877. if ( isset( $_GET['page'] ) && $this->menu === $_GET['page'] )
  878. return true;
  879.  
  880. return false;
  881.  
  882. }
  883.  
  884. /**
  885. * Delete dismissable nag option when theme is switched.
  886. *
  887. * This ensures that the user is again reminded via nag of required
  888. * and/or recommended plugins if they re-activate the theme.
  889. *
  890. * @since 2.1.1
  891. */
  892. public function update_dismiss() {
  893.  
  894. delete_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice' );
  895.  
  896. }
  897.  
  898. /**
  899. * Forces plugin activation if the parameter 'force_activation' is
  900. * set to true.
  901. *
  902. * This allows theme authors to specify certain plugins that must be
  903. * active at all times while using the current theme.
  904. *
  905. * Please take special care when using this parameter as it has the
  906. * potential to be harmful if not used correctly. Setting this parameter
  907. * to true will not allow the specified plugin to be deactivated unless
  908. * the user switches themes.
  909. *
  910. * @since 2.2.0
  911. */
  912. public function force_activation() {
  913.  
  914. /** Set file_path parameter for any installed plugins */
  915. $this->populate_file_path();
  916.  
  917. $installed_plugins = get_plugins();
  918.  
  919. foreach ( $this->plugins as $plugin ) {
  920. /** Oops, plugin isn't there so iterate to next condition */
  921. if ( isset( $plugin['force_activation'] ) && $plugin['force_activation'] && ! isset( $installed_plugins[$plugin['file_path']] ) )
  922. continue;
  923. /** There we go, activate the plugin */
  924. elseif ( isset( $plugin['force_activation'] ) && $plugin['force_activation'] && is_plugin_inactive( $plugin['file_path'] ) )
  925. activate_plugin( $plugin['file_path'] );
  926. }
  927.  
  928. }
  929.  
  930. /**
  931. * Forces plugin deactivation if the parameter 'force_deactivation'
  932. * is set to true.
  933. *
  934. * This allows theme authors to specify certain plugins that must be
  935. * deactived upon switching from the current theme to another.
  936. *
  937. * Please take special care when using this parameter as it has the
  938. * potential to be harmful if not used correctly.
  939. *
  940. * @since 2.2.0
  941. */
  942. public function force_deactivation() {
  943.  
  944. /** Set file_path parameter for any installed plugins */
  945. $this->populate_file_path();
  946.  
  947. foreach ( $this->plugins as $plugin ) {
  948. /** Only proceed forward if the paramter is set to true and plugin is active */
  949. if ( isset( $plugin['force_deactivation'] ) && $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) )
  950. deactivate_plugins( $plugin['file_path'] );
  951. }
  952.  
  953. }
  954.  
  955. }
  956. }
  957.  
  958. /** Create a new instance of the class */
  959. new TGM_Plugin_Activation;
  960.  
  961. if ( ! function_exists( 'tgmpa' ) ) {
  962. /**
  963. * Helper function to register a collection of required plugins.
  964. *
  965. * @since 2.0.0
  966. * @api
  967. *
  968. * @param array $plugins An array of plugin arrays
  969. * @param array $config Optional. An array of configuration values
  970. */
  971. function tgmpa( $plugins, $config = array() ) {
  972.  
  973. foreach ( $plugins as $plugin )
  974. TGM_Plugin_Activation::$instance->register( $plugin );
  975.  
  976. if ( $config )
  977. TGM_Plugin_Activation::$instance->config( $config );
  978.  
  979. }
  980. }
  981.  
  982. /**
  983. * WP_List_Table isn't always available. If it isn't available,
  984. * we load it here.
  985. *
  986. * @since 2.2.0
  987. */
  988. if ( ! class_exists( 'WP_List_Table' ) )
  989. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  990.  
  991. if ( ! class_exists( 'TGMPA_List_Table' ) ) {
  992. /**
  993. * List table class for handling plugins.
  994. *
  995. * Extends the WP_List_Table class to provide a future-compatible
  996. * way of listing out all required/recommended plugins.
  997. *
  998. * Gives users an interface similar to the Plugin Administration
  999. * area with similar (albeit stripped down) capabilities.
  1000. *
  1001. * This class also allows for the bulk install of plugins.
  1002. *
  1003. * @since 2.2.0
  1004. *
  1005. * @package TGM-Plugin-Activation
  1006. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  1007. * @author Gary Jones <gamajo@gamajo.com>
  1008. */
  1009. class TGMPA_List_Table extends WP_List_Table {
  1010.  
  1011. /**
  1012. * References parent constructor and sets defaults for class.
  1013. *
  1014. * The constructor also grabs a copy of $instance from the TGMPA class
  1015. * and stores it in the global object TGM_Plugin_Activation::$instance.
  1016. *
  1017. * @since 2.2.0
  1018. *
  1019. * @global unknown $status
  1020. * @global string $page
  1021. */
  1022. public function __construct() {
  1023.  
  1024. global $status, $page;
  1025.  
  1026. parent::__construct(
  1027. array(
  1028. 'singular' => 'plugin',
  1029. 'plural' => 'plugins',
  1030. 'ajax' => false,
  1031. )
  1032. );
  1033.  
  1034. }
  1035.  
  1036. /**
  1037. * Gathers and renames all of our plugin information to be used by
  1038. * WP_List_Table to create our table.
  1039. *
  1040. * @since 2.2.0
  1041. *
  1042. * @return array $table_data Information for use in table
  1043. */
  1044. protected function _gather_plugin_data() {
  1045.  
  1046. /** Load thickbox for plugin links */
  1047. TGM_Plugin_Activation::$instance->admin_init();
  1048. TGM_Plugin_Activation::$instance->thickbox();
  1049.  
  1050. /** Prep variables for use and grab list of all installed plugins */
  1051. $table_data = array();
  1052. $i = 0;
  1053. $installed_plugins = get_plugins();
  1054.  
  1055. foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin ) {
  1056. if ( is_plugin_active( $plugin['file_path'] ) )
  1057. continue; // No need to display plugins if they are installed and activated
  1058.  
  1059. $table_data[$i]['sanitized_plugin'] = $plugin['name'];
  1060. $table_data[$i]['slug'] = $this->_get_plugin_data_from_name( $plugin['name'] );
  1061.  
  1062. $external_url = $this->_get_plugin_data_from_name( $plugin['name'], 'external_url' );
  1063. $source = $this->_get_plugin_data_from_name( $plugin['name'], 'source' );
  1064.  
  1065. if ( $external_url && preg_match( '|^http(s)?://|', $external_url ) ) {
  1066. $table_data[$i]['plugin'] = '<strong><a href="' . esc_url( $external_url ) . '" title="' . $plugin['name'] . '" target="_blank">' . $plugin['name'] . '</a></strong>';
  1067. }
  1068. elseif ( ! $source || preg_match( '|^http://wordpress.org/extend/plugins/|', $source ) ) {
  1069. $url = add_query_arg(
  1070. array(
  1071. 'tab' => 'plugin-information',
  1072. 'plugin' => $this->_get_plugin_data_from_name( $plugin['name'] ),
  1073. 'TB_iframe' => 'true',
  1074. 'width' => '640',
  1075. 'height' => '500',
  1076. ),
  1077. admin_url( 'plugin-install.php' )
  1078. );
  1079.  
  1080. $table_data[$i]['plugin'] = '<strong><a href="' . esc_url( $url ) . '" class="thickbox" title="' . $plugin['name'] . '">' . $plugin['name'] . '</a></strong>';
  1081. }
  1082. else {
  1083. $table_data[$i]['plugin'] = '<strong>' . $plugin['name'] . '</strong>'; // No hyperlink
  1084. }
  1085.  
  1086. if ( isset( $table_data[$i]['plugin'] ) && (array) $table_data[$i]['plugin'] )
  1087. $plugin['name'] = $table_data[$i]['plugin'];
  1088.  
  1089. if ( isset( $plugin['external_url'] ) ) {
  1090. /** The plugin is linked to an external source */
  1091. $table_data[$i]['source'] = __( 'External Link', TGM_Plugin_Activation::$instance->domain );
  1092. }
  1093. elseif ( isset( $plugin['source'] ) ) {
  1094. /** The plugin must be from a private repository */
  1095. if ( preg_match( '|^http(s)?://|', $plugin['source'] ) )
  1096. $table_data[$i]['source'] = __( 'Private Repository', TGM_Plugin_Activation::$instance->domain );
  1097. /** The plugin is pre-packaged with the theme */
  1098. else
  1099. $table_data[$i]['source'] = __( 'Pre-Packaged', TGM_Plugin_Activation::$instance->domain );
  1100. }
  1101. /** The plugin is from the WordPress repository */
  1102. else {
  1103. $table_data[$i]['source'] = __( 'WordPress Repository', TGM_Plugin_Activation::$instance->domain );
  1104. }
  1105.  
  1106. $table_data[$i]['type'] = $plugin['required'] ? __( 'Required', TGM_Plugin_Activation::$instance->domain ) : __( 'Recommended', TGM_Plugin_Activation::$instance->domain );
  1107.  
  1108. if ( ! isset( $installed_plugins[$plugin['file_path']] ) )
  1109. $table_data[$i]['status'] = sprintf( '%1$s', __( 'Not Installed', TGM_Plugin_Activation::$instance->domain ) );
  1110. elseif ( is_plugin_inactive( $plugin['file_path'] ) )
  1111. $table_data[$i]['status'] = sprintf( '%1$s', __( 'Installed But Not Activated', TGM_Plugin_Activation::$instance->domain ) );
  1112.  
  1113. $table_data[$i]['file_path'] = $plugin['file_path'];
  1114. $table_data[$i]['url'] = isset( $plugin['source'] ) ? $plugin['source'] : 'repo';
  1115.  
  1116. $i++;
  1117. }
  1118.  
  1119. /** Sort plugins by Required/Recommended type and by alphabetical listing within each type */
  1120. $resort = array();
  1121. $req = array();
  1122. $rec = array();
  1123.  
  1124. /** Grab all the plugin types */
  1125. foreach ( $table_data as $plugin )
  1126. $resort[] = $plugin['type'];
  1127.  
  1128. /** Sort each plugin by type */
  1129. foreach ( $resort as $type )
  1130. if ( 'Required' == $type )
  1131. $req[] = $type;
  1132. else
  1133. $rec[] = $type;
  1134.  
  1135. /** Sort alphabetically each plugin type array, merge them and then sort in reverse (lists Required plugins first) */
  1136. sort( $req );
  1137. sort( $rec );
  1138. array_merge( $resort, $req, $rec );
  1139. array_multisort( $resort, SORT_DESC, $table_data );
  1140.  
  1141. return $table_data;
  1142.  
  1143. }
  1144.  
  1145. /**
  1146. * Retrieve plugin data, given the plugin name. Taken from the
  1147. * TGM_Plugin_Activation class.
  1148. *
  1149. * Loops through the registered plugins looking for $name. If it finds it,
  1150. * it returns the $data from that plugin. Otherwise, returns false.
  1151. *
  1152. * @since 2.2.0
  1153. *
  1154. * @param string $name Name of the plugin, as it was registered
  1155. * @param string $data Optional. Array key of plugin data to return. Default is slug
  1156. * @return string|boolean Plugin slug if found, false otherwise
  1157. */
  1158. protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
  1159.  
  1160. foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin => $values ) {
  1161. if ( $name == $values['name'] && isset( $values[$data] ) )
  1162. return $values[$data];
  1163. }
  1164.  
  1165. return false;
  1166.  
  1167. }
  1168.  
  1169. /**
  1170. * Create default columns to display important plugin information
  1171. * like type, action and status.
  1172. *
  1173. * @since 2.2.0
  1174. *
  1175. * @param array $item
  1176. * @param string $column_name
  1177. */
  1178. public function column_default( $item, $column_name ) {
  1179.  
  1180. switch ( $column_name ) {
  1181. case 'source':
  1182. case 'type':
  1183. case 'status':
  1184. return $item[$column_name];
  1185. }
  1186.  
  1187. }
  1188.  
  1189. /**
  1190. * Create default title column along with action links of 'Install'
  1191. * and 'Activate'.
  1192. *
  1193. * @since 2.2.0
  1194. *
  1195. * @param array $item
  1196. * @return string The action hover links
  1197. */
  1198. public function column_plugin( $item ) {
  1199.  
  1200. $installed_plugins = get_plugins();
  1201.  
  1202. /** No need to display any hover links */
  1203. if ( is_plugin_active( $item['file_path'] ) )
  1204. $actions = array();
  1205.  
  1206. /** We need to display the 'Install' hover link */
  1207. if ( ! isset( $installed_plugins[$item['file_path']] ) ) {
  1208. $actions = array(
  1209. 'install' => sprintf(
  1210. '<a href="%1$s" title="Install %2$s">Install</a>',
  1211. wp_nonce_url(
  1212. add_query_arg(
  1213. array(
  1214. 'page' => TGM_Plugin_Activation::$instance->menu,
  1215. 'plugin' => $item['slug'],
  1216. 'plugin_name' => $item['sanitized_plugin'],
  1217. 'plugin_source' => $item['url'],
  1218. 'tgmpa-install' => 'install-plugin',
  1219. ),
  1220. admin_url( TGM_Plugin_Activation::$instance->parent_url_slug )
  1221. ),
  1222. 'tgmpa-install'
  1223. ),
  1224. $item['sanitized_plugin']
  1225. ),
  1226. );
  1227. }
  1228. /** We need to display the 'Activate' hover link */
  1229. elseif ( is_plugin_inactive( $item['file_path'] ) ) {
  1230. $actions = array(
  1231. 'activate' => sprintf(
  1232. '<a href="%1$s" title="Activate %2$s">Activate</a>',
  1233. add_query_arg(
  1234. array(
  1235. 'page' => TGM_Plugin_Activation::$instance->menu,
  1236. 'plugin' => $item['slug'],
  1237. 'plugin_name' => $item['sanitized_plugin'],
  1238. 'plugin_source' => $item['url'],
  1239. 'tgmpa-activate' => 'activate-plugin',
  1240. 'tgmpa-activate-nonce' => wp_create_nonce( 'tgmpa-activate' ),
  1241. ),
  1242. admin_url( TGM_Plugin_Activation::$instance->parent_url_slug )
  1243. ),
  1244. $item['sanitized_plugin']
  1245. ),
  1246. );
  1247. }
  1248.  
  1249. return sprintf( '%1$s %2$s', $item['plugin'], $this->row_actions( $actions ) );
  1250.  
  1251. }
  1252.  
  1253. /**
  1254. * Required for bulk installing.
  1255. *
  1256. * Adds a checkbox for each plugin.
  1257. *
  1258. * @since 2.2.0
  1259. *
  1260. * @param array $item
  1261. * @return string The input checkbox with all necessary info
  1262. */
  1263. public function column_cb( $item ) {
  1264.  
  1265. $value = $item['file_path'] . ',' . $item['url'] . ',' . $item['sanitized_plugin'];
  1266. return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />', $this->_args['singular'], $value, $item['sanitized_plugin'] );
  1267.  
  1268. }
  1269.  
  1270. /**
  1271. * Sets default message within the plugins table if no plugins
  1272. * are left for interaction.
  1273. *
  1274. * Hides the menu item to prevent the user from clicking and
  1275. * getting a permissions error.
  1276. *
  1277. * @since 2.2.0
  1278. */
  1279. public function no_items() {
  1280.  
  1281. printf( __( 'No plugins to install or activate. <a href="%1$s" title="Return to the Dashboard">Return to the Dashboard</a>', TGM_Plugin_Activation::$instance->domain ), admin_url() );
  1282. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  1283.  
  1284. }
  1285.  
  1286. /**
  1287. * Output all the column information within the table.
  1288. *
  1289. * @since 2.2.0
  1290. *
  1291. * @return array $columns The column names
  1292. */
  1293. public function get_columns() {
  1294.  
  1295. $columns = array(
  1296. 'cb' => '<input type="checkbox" />',
  1297. 'plugin' => __( 'Plugin', TGM_Plugin_Activation::$instance->domain ),
  1298. 'source' => __( 'Source', TGM_Plugin_Activation::$instance->domain ),
  1299. 'type' => __( 'Type', TGM_Plugin_Activation::$instance->domain ),
  1300. 'status' => __( 'Status', TGM_Plugin_Activation::$instance->domain )
  1301. );
  1302.  
  1303. return $columns;
  1304.  
  1305. }
  1306.  
  1307. /**
  1308. * Defines all types of bulk actions for handling
  1309. * registered plugins.
  1310. *
  1311. * @since 2.2.0
  1312. *
  1313. * @return array $actions The bulk actions for the plugin install table
  1314. */
  1315. public function get_bulk_actions() {
  1316.  
  1317. $actions = array(
  1318. 'tgmpa-bulk-install' => __( 'Install', TGM_Plugin_Activation::$instance->domain ),
  1319. 'tgmpa-bulk-activate' => __( 'Activate', TGM_Plugin_Activation::$instance->domain ),
  1320. );
  1321.  
  1322. return $actions;
  1323.  
  1324. }
  1325.  
  1326. /**
  1327. * Processes bulk installation and activation actions.
  1328. *
  1329. * The bulk installation process looks either for the $_POST
  1330. * information or for the plugin info within the $_GET variable if
  1331. * a user has to use WP_Filesystem to enter their credentials.
  1332. *
  1333. * @since 2.2.0
  1334. */
  1335. public function process_bulk_actions() {
  1336.  
  1337. /** Bulk installation process */
  1338. if ( 'tgmpa-bulk-install' === $this->current_action() ) {
  1339. check_admin_referer( 'bulk-' . $this->_args['plural'] );
  1340.  
  1341. /** Prep variables to be populated */
  1342. $plugins_to_install = array();
  1343. $plugin_installs = array();
  1344. $plugin_path = array();
  1345. $plugin_name = array();
  1346.  
  1347. /** Look first to see if information has been passed via WP_Filesystem */
  1348. if ( isset( $_GET[sanitize_key( 'plugins' )] ) )
  1349. $plugins = explode( ',', stripslashes( $_GET[sanitize_key( 'plugins' )] ) );
  1350. /** Looks like the user can use the direct method, take from $_POST */
  1351. elseif ( isset( $_POST[sanitize_key( 'plugin' )] ) )
  1352. $plugins = (array) $_POST[sanitize_key( 'plugin' )];
  1353. /** Nothing has been submitted */
  1354. else
  1355. $plugins = array();
  1356.  
  1357. $a = 0; // Incremental variable
  1358.  
  1359. /** Grab information from $_POST if available */
  1360. if ( isset( $_POST[sanitize_key( 'plugin' )] ) ) {
  1361. foreach ( $plugins as $plugin_data )
  1362. $plugins_to_install[] = explode( ',', $plugin_data );
  1363.  
  1364. foreach ( $plugins_to_install as $plugin_data ) {
  1365. $plugin_installs[] = $plugin_data[0];
  1366. $plugin_path[] = $plugin_data[1];
  1367. $plugin_name[] = $plugin_data[2];
  1368. }
  1369. }
  1370. /** Information has been passed via $_GET */
  1371. else {
  1372. foreach ( $plugins as $key => $value ) {
  1373. /** Grab plugin slug for each plugin */
  1374. if ( 0 == $key % 3 || 0 == $key ) {
  1375. $plugins_to_install[] = $value;
  1376. $plugin_installs[] = $value;
  1377. }
  1378. $a++;
  1379. }
  1380. }
  1381.  
  1382. /** Look first to see if information has been passed via WP_Filesystem */
  1383. if ( isset( $_GET[sanitize_key( 'plugin_paths' )] ) )
  1384. $plugin_paths = explode( ',', stripslashes( $_GET[sanitize_key( 'plugin_paths' )] ) );
  1385. /** Looks like the user doesn't need to enter his FTP creds */
  1386. elseif ( isset( $_POST[sanitize_key( 'plugin' )] ) )
  1387. $plugin_paths = (array) $plugin_path;
  1388. /** Nothing has been submitted */
  1389. else
  1390. $plugin_paths = array();
  1391.  
  1392. /** Look first to see if information has been passed via WP_Filesystem */
  1393. if ( isset( $_GET[sanitize_key( 'plugin_names' )] ) )
  1394. $plugin_names = explode( ',', stripslashes( $_GET[sanitize_key( 'plugin_names' )] ) );
  1395. /** Looks like the user doesn't need to enter his FTP creds */
  1396. elseif ( isset( $_POST[sanitize_key( 'plugin' )] ) )
  1397. $plugin_names = (array) $plugin_name;
  1398. /** Nothing has been submitted */
  1399. else
  1400. $plugin_names = array();
  1401.  
  1402. $b = 0; // Incremental variable
  1403.  
  1404. /** Loop through plugin slugs and remove already installed plugins from the list */
  1405. foreach ( $plugin_installs as $key => $plugin ) {
  1406. if ( preg_match( '|.php$|', $plugin ) ) {
  1407. unset( $plugin_installs[$key] );
  1408.  
  1409. /** If the plugin path isn't in the $_GET variable, we can unset the corresponding path */
  1410. if ( ! isset( $_GET[sanitize_key( 'plugin_paths' )] ) )
  1411. unset( $plugin_paths[$b] );
  1412.  
  1413. /** If the plugin name isn't in the $_GET variable, we can unset the corresponding name */
  1414. if ( ! isset( $_GET[sanitize_key( 'plugin_names' )] ) )
  1415. unset( $plugin_names[$b] );
  1416. }
  1417. $b++;
  1418. }
  1419.  
  1420. /** No need to proceed further if we have no plugins to install */
  1421. if ( empty( $plugin_installs ) )
  1422. return false;
  1423.  
  1424. /** Reset array indexes in case we removed already installed plugins */
  1425. $plugin_installs = array_values( $plugin_installs );
  1426. $plugin_paths = array_values( $plugin_paths );
  1427. $plugin_names = array_values( $plugin_names );
  1428.  
  1429. /** If we grabbed our plugin info from $_GET, we need to decode it for use */
  1430. $plugin_installs = array_map( 'urldecode', $plugin_installs );
  1431. $plugin_paths = array_map( 'urldecode', $plugin_paths );
  1432. $plugin_names = array_map( 'urldecode', $plugin_names );
  1433.  
  1434. /** Pass all necessary information via URL if WP_Filesystem is needed */
  1435. $url = wp_nonce_url(
  1436. add_query_arg(
  1437. array(
  1438. 'page' => TGM_Plugin_Activation::$instance->menu,
  1439. 'tgmpa-action' => 'install-selected',
  1440. 'plugins' => urlencode( implode( ',', $plugins ) ),
  1441. 'plugin_paths' => urlencode( implode( ',', $plugin_paths ) ),
  1442. 'plugin_names' => urlencode( implode( ',', $plugin_names ) ),
  1443. ),
  1444. admin_url( TGM_Plugin_Activation::$instance->parent_url_slug )
  1445. ),
  1446. 'bulk-plugins'
  1447. );
  1448. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary
  1449. $fields = array( sanitize_key( 'action' ), sanitize_key( '_wp_http_referer' ), sanitize_key( '_wpnonce' ) ); // Extra fields to pass to WP_Filesystem
  1450.  
  1451. if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, $fields ) ) )
  1452. return true;
  1453.  
  1454. if ( ! WP_Filesystem( $creds ) ) {
  1455. request_filesystem_credentials( $url, $method, true, false, $fields ); // Setup WP_Filesystem
  1456. return true;
  1457. }
  1458.  
  1459. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for plugins_api
  1460. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes
  1461.  
  1462. /** Store all information in arrays since we are processing a bulk installation */
  1463. $api = array();
  1464. $sources = array();
  1465. $install_path = array();
  1466.  
  1467. $c = 0; // Incremental variable
  1468.  
  1469. /** Loop through each plugin to install and try to grab information from WordPress API, if not create 'tgmpa-empty' scalar */
  1470. foreach ( $plugin_installs as $plugin ) {
  1471. $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';
  1472. $c++;
  1473. }
  1474.  
  1475. if ( is_wp_error( $api ) )
  1476. wp_die( TGM_Plugin_Activation::$instance->strings['oops'] . var_dump( $api ) );
  1477.  
  1478. $d = 0; // Incremental variable
  1479.  
  1480. /** Capture download links from $api or set install link to pre-packaged/private repo */
  1481. foreach ( $api as $object ) {
  1482. $sources[$d] = isset( $object->download_link ) && 'repo' == $plugin_paths[$d] ? $object->download_link : $plugin_paths[$d];
  1483. $d++;
  1484. }
  1485.  
  1486. /** Finally, all the data is prepared to be sent to the installer */
  1487. $url = add_query_arg( array( 'page' => TGM_Plugin_Activation::$instance->menu ), admin_url( TGM_Plugin_Activation::$instance->parent_url_slug ) );
  1488. $nonce = 'bulk-plugins';
  1489. $names = $plugin_names;
  1490.  
  1491. /** Create a new instance of TGM_Bulk_Installer */
  1492. $installer = new TGM_Bulk_Installer( $skin = new TGM_Bulk_Installer_Skin( compact( 'url', 'nonce', 'names' ) ) );
  1493.  
  1494. /** Wrap the install process with the appropriate HTML */
  1495. echo '<div class="tgmpa wrap">';
  1496. screen_icon( apply_filters( 'tgmpa_default_screen_icon', 'themes' ) );
  1497. echo '<h2>' . esc_html( get_admin_page_title() ) . '</h2>';
  1498. /** Process the bulk installation submissions */
  1499. $installer->bulk_install( $sources );
  1500. echo '</div>';
  1501.  
  1502. return true;
  1503. }
  1504.  
  1505. /** Bulk activation process */
  1506. if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
  1507. check_admin_referer( 'bulk-' . $this->_args['plural'] );
  1508.  
  1509. /** Grab plugin data from $_POST */
  1510. $plugins = isset( $_POST[sanitize_key( 'plugin' )] ) ? (array) $_POST[sanitize_key( 'plugin' )] : array();
  1511. $plugins_to_activate = array();
  1512.  
  1513. /** Split plugin value into array with plugin file path, plugin source and plugin name */
  1514. foreach ( $plugins as $i => $plugin )
  1515. $plugins_to_activate[] = explode( ',', $plugin );
  1516.  
  1517. foreach ( $plugins_to_activate as $i => $array ) {
  1518. if ( ! preg_match( '|.php$|', $array[0] ) ) // Plugins that haven't been installed yet won't have the correct file path
  1519. unset( $plugins_to_activate[$i] );
  1520. }
  1521.  
  1522. /** Return early if there are no plugins to activate */
  1523. if ( empty( $plugins_to_activate ) )
  1524. return;
  1525.  
  1526. $plugins = array();
  1527. $plugin_names = array();
  1528.  
  1529. foreach ( $plugins_to_activate as $plugin_string ) {
  1530. $plugins[] = $plugin_string[0];
  1531. $plugin_names[] = $plugin_string[2];
  1532. }
  1533.  
  1534. $count = count( $plugin_names ); // Count so we can use _n function
  1535. $last_plugin = array_pop( $plugin_names ); // Pop off last name to prep for readability
  1536. $imploded = empty( $plugin_names ) ? '<strong>' . $last_plugin . '</strong>' : '<strong>' . ( implode( ', ', $plugin_names ) . '</strong> and <strong>' . $last_plugin . '</strong>.' );
  1537.  
  1538. /** Now we are good to go - let's start activating plugins */
  1539. $activate = activate_plugins( $plugins );
  1540.  
  1541. if ( is_wp_error( $activate ) )
  1542. echo '<div id="message" class="error"><p>' . $activate->get_error_message() . '</p></div>';
  1543. else
  1544. 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, TGM_Plugin_Activation::$instance->domain ), $imploded );
  1545.  
  1546. /** Update recently activated plugins option */
  1547. $recent = (array) get_option( 'recently_activated' );
  1548.  
  1549. foreach ( $plugins as $plugin => $time )
  1550. if ( isset( $recent[$plugin] ) )
  1551. unset( $recent[$plugin] );
  1552.  
  1553. update_option( 'recently_activated', $recent );
  1554.  
  1555. unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another
  1556. }
  1557. }
  1558.  
  1559. /**
  1560. * Prepares all of our information to be outputted into a usable table.
  1561. *
  1562. * @since 2.2.0
  1563. */
  1564. public function prepare_items() {
  1565.  
  1566. $per_page = 100; // Set it high so we shouldn't have to worry about pagination
  1567. $columns = $this->get_columns(); // Get all necessary column information
  1568. $hidden = array(); // No columns to hide, but we must set as an array
  1569. $sortable = array(); // No reason to make sortable columns
  1570. $this->_column_headers = array( $columns, $hidden, $sortable ); // Get all necessary column headers
  1571.  
  1572. /** Process our bulk actions here */
  1573. $this->process_bulk_actions();
  1574.  
  1575. /** Store all of our plugin data into $items array so WP_List_Table can use it */
  1576. $this->items = $this->_gather_plugin_data();
  1577.  
  1578. }
  1579.  
  1580. }
  1581. }
  1582.  
  1583. /**
  1584. * The WP_Upgrader file isn't always available. If it isn't available,
  1585. * we load it here.
  1586. *
  1587. * We check to make sure no action or activation keys are set so that WordPress
  1588. * doesn't try to re-include the class when processing upgrades or installs outside
  1589. * of the class.
  1590. *
  1591. * @since 2.2.0
  1592. */
  1593. if ( ! class_exists( 'WP_Upgrader' ) && ( isset( $_GET[sanitize_key( 'page' )] ) && TGM_Plugin_Activation::$instance->menu = $_GET[sanitize_key( 'page' )] ) ) {
  1594. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  1595.  
  1596. if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
  1597. /**
  1598. * Installer class to handle bulk plugin installations.
  1599. *
  1600. * Extends WP_Upgrader and customizes to suit the installation of multiple
  1601. * plugins.
  1602. *
  1603. * @since 2.2.0
  1604. *
  1605. * @package TGM-Plugin-Activation
  1606. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  1607. * @author Gary Jones <gamajo@gamajo.com>
  1608. */
  1609. class TGM_Bulk_Installer extends WP_Upgrader {
  1610.  
  1611. /**
  1612. * Holds result of bulk plugin installation.
  1613. *
  1614. * @since 2.2.0
  1615. *
  1616. * @var string
  1617. */
  1618. public $result;
  1619.  
  1620. /**
  1621. * Flag to check if bulk installation is occurring or not.
  1622. *
  1623. * @since 2.2.0
  1624. *
  1625. * @var boolean
  1626. */
  1627. public $bulk = false;
  1628.  
  1629. /**
  1630. * Processes the bulk installation of plugins.
  1631. *
  1632. * @since 2.2.0
  1633. *
  1634. * @param array $packages The plugin sources needed for installation
  1635. * @return string|boolean Install confirmation messages on success, false on failure
  1636. */
  1637. public function bulk_install( $packages ) {
  1638.  
  1639. /** Pass installer skin object and set bulk property to true */
  1640. $this->init();
  1641. $this->bulk = true;
  1642.  
  1643. /** Set install strings and automatic activation strings (if config option is set to true) */
  1644. $this->install_strings();
  1645. if ( TGM_Plugin_Activation::$instance->is_automatic )
  1646. $this->activate_strings();
  1647.  
  1648. /** Run the header string to notify user that the process has begun */
  1649. $this->skin->header();
  1650.  
  1651. /** Connect to the Filesystem */
  1652. $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
  1653. if ( ! $res ) {
  1654. $this->skin->footer();
  1655. return false;
  1656. }
  1657.  
  1658. /** Set the bulk header and prepare results array */
  1659. $this->skin->bulk_header();
  1660. $results = array();
  1661.  
  1662. /** Get the total number of packages being processed and iterate as each package is successfully installed */
  1663. $this->update_count = count( $packages );
  1664. $this->update_current = 0;
  1665.  
  1666. /** Loop through each plugin and process the installation */
  1667. foreach ( $packages as $plugin ) {
  1668. $this->update_current++; // Increment counter
  1669.  
  1670. /** Do the plugin install */
  1671. $result = $this->run(
  1672. array(
  1673. 'package' => $plugin, // The plugin source
  1674. 'destination' => WP_PLUGIN_DIR, // The destination dir
  1675. 'clear_destination' => false, // Do we want to clear the destination or not?
  1676. 'clear_working' => true, // Remove original install file
  1677. 'is_multi' => true, // Are we processing multiple installs?
  1678. 'hook_extra' => array( 'plugin' => $plugin, ), // Pass plugin source as extra data
  1679. )
  1680. );
  1681.  
  1682. /** Store installation results in result property */
  1683. $results[$plugin] = $this->result;
  1684.  
  1685. /** Prevent credentials auth screen from displaying multiple times */
  1686. if ( false === $result )
  1687. break;
  1688. }
  1689.  
  1690. /** Pass footer skin strings */
  1691. $this->skin->bulk_footer();
  1692. $this->skin->footer();
  1693.  
  1694. /** Return our results */
  1695. return $results;
  1696.  
  1697. }
  1698.  
  1699. /**
  1700. * Performs the actual installation of each plugin.
  1701. *
  1702. * This method also activates the plugin in the automatic flag has been
  1703. * set to true for the TGMPA class.
  1704. *
  1705. * @since 2.2.0
  1706. *
  1707. * @param array $options The installation cofig options
  1708. * @return null/array Return early if error, array of installation data on success
  1709. */
  1710. public function run( $options ) {
  1711.  
  1712. /** Default config options */
  1713. $defaults = array(
  1714. 'package' => '',
  1715. 'destination' => '',
  1716. 'clear_destination' => false,
  1717. 'clear_working' => true,
  1718. 'is_multi' => false,
  1719. 'hook_extra' => array(),
  1720. );
  1721.  
  1722. /** Parse default options with config options from $this->bulk_upgrade and extract them */
  1723. $options = wp_parse_args( $options, $defaults );
  1724. extract( $options );
  1725.  
  1726. /** Connect to the Filesystem */
  1727. $res = $this->fs_connect( array( WP_CONTENT_DIR, $destination ) );
  1728. if ( ! $res )
  1729. return false;
  1730.  
  1731. /** Return early if there is an error connecting to the Filesystem */
  1732. if ( is_wp_error( $res ) ) {
  1733. $this->skin->error( $res );
  1734. return $res;
  1735. }
  1736.  
  1737. /** Call $this->header separately if running multiple times */
  1738. if ( ! $is_multi )
  1739. $this->skin->header();
  1740.  
  1741. /** Set strings before the package is installed */
  1742. $this->skin->before();
  1743.  
  1744. /** Download the package (this just returns the filename of the file if the package is a local file) */
  1745. $download = $this->download_package( $package );
  1746. if ( is_wp_error( $download ) ) {
  1747. $this->skin->error( $download );
  1748. $this->skin->after();
  1749. return $download;
  1750. }
  1751.  
  1752. /** Don't accidentally delete a local file */
  1753. $delete_package = ( $download != $package );
  1754.  
  1755. /** Unzip file into a temporary working directory */
  1756. $working_dir = $this->unpack_package( $download, $delete_package );
  1757. if ( is_wp_error( $working_dir ) ) {
  1758. $this->skin->error( $working_dir );
  1759. $this->skin->after();
  1760. return $working_dir;
  1761. }
  1762.  
  1763. /** Install the package into the working directory with all passed config options */
  1764. $result = $this->install_package(
  1765. array(
  1766. 'source' => $working_dir,
  1767. 'destination' => $destination,
  1768. 'clear_destination' => $clear_destination,
  1769. 'clear_working' => $clear_working,
  1770. 'hook_extra' => $hook_extra,
  1771. )
  1772. );
  1773.  
  1774. /** Pass the result of the installation */
  1775. $this->skin->set_result( $result );
  1776.  
  1777. /** Set correct strings based on results */
  1778. if ( is_wp_error( $result ) ) {
  1779. $this->skin->error( $result );
  1780. $this->skin->feedback( 'process_failed' );
  1781. }
  1782. /** The plugin install is successful */
  1783. else {
  1784. $this->skin->feedback( 'process_success' );
  1785. }
  1786.  
  1787. /** Only process the activation of installed plugins if the automatic flag is set to true */
  1788. if ( TGM_Plugin_Activation::$instance->is_automatic ) {
  1789. /** Flush plugins cache so we can make sure that the installed plugins list is always up to date */
  1790. wp_cache_flush();
  1791.  
  1792. /** Get the installed plugin file and activate it */
  1793. $plugin_info = $this->plugin_info( $package );
  1794. $activate = activate_plugin( $plugin_info );
  1795.  
  1796. /** Re-populate the file path now that the plugin has been installed and activated */
  1797. TGM_Plugin_Activation::$instance->populate_file_path();
  1798.  
  1799. /** Set correct strings based on results */
  1800. if ( is_wp_error( $activate ) ) {
  1801. $this->skin->error( $activate );
  1802. $this->skin->feedback( 'activation_failed' );
  1803. }
  1804. /** The plugin activation is successful */
  1805. else {
  1806. $this->skin->feedback( 'activation_success' );
  1807. }
  1808. }
  1809.  
  1810. /** Flush plugins cache so we can make sure that the installed plugins list is always up to date */
  1811. wp_cache_flush();
  1812.  
  1813. /** Set install footer strings */
  1814. $this->skin->after();
  1815. if ( ! $is_multi )
  1816. $this->skin->footer();
  1817.  
  1818. return $result;
  1819.  
  1820. }
  1821.  
  1822. /**
  1823. * Sets the correct install strings for the installer skin to use.
  1824. *
  1825. * @since 2.2.0
  1826. */
  1827. public function install_strings() {
  1828.  
  1829. $this->strings['no_package'] = __( 'Install package not available.', TGM_Plugin_Activation::$instance->domain );
  1830. $this->strings['downloading_package'] = __( 'Downloading install package from <span class="code">%s</span>&#8230;', TGM_Plugin_Activation::$instance->domain );
  1831. $this->strings['unpack_package'] = __( 'Unpacking the package&#8230;', TGM_Plugin_Activation::$instance->domain );
  1832. $this->strings['installing_package'] = __( 'Installing the plugin&#8230;', TGM_Plugin_Activation::$instance->domain );
  1833. $this->strings['process_failed'] = __( 'Plugin install failed.', TGM_Plugin_Activation::$instance->domain );
  1834. $this->strings['process_success'] = __( 'Plugin installed successfully.', TGM_Plugin_Activation::$instance->domain );
  1835.  
  1836. }
  1837.  
  1838. /**
  1839. * Sets the correct activation strings for the installer skin to use.
  1840. *
  1841. * @since 2.2.0
  1842. */
  1843. public function activate_strings() {
  1844.  
  1845. $this->strings['activation_failed'] = __( 'Plugin activation failed.', TGM_Plugin_Activation::$instance->domain );
  1846. $this->strings['activation_success'] = __( 'Plugin activated successfully.', TGM_Plugin_Activation::$instance->domain );
  1847.  
  1848. }
  1849.  
  1850. /**
  1851. * Grabs the plugin file from an installed plugin.
  1852. *
  1853. * @since 2.2.0
  1854. *
  1855. * @return string|boolean Return plugin file on success, false on failure
  1856. */
  1857. public function plugin_info() {
  1858.  
  1859. /** Return false if installation result isn't an array or the destination name isn't set */
  1860. if ( ! is_array( $this->result ) )
  1861. return false;
  1862. if ( empty( $this->result['destination_name'] ) )
  1863. return false;
  1864.  
  1865. /** Get the installed plugin file or return false if it isn't set */
  1866. $plugin = get_plugins( '/' . $this->result['destination_name'] );
  1867. if ( empty( $plugin ) )
  1868. return false;
  1869.  
  1870. /** Assume the requested plugin is the first in the list */
  1871. $pluginfiles = array_keys( $plugin );
  1872.  
  1873. return $this->result['destination_name'] . '/' . $pluginfiles[0];
  1874.  
  1875. }
  1876.  
  1877. }
  1878. }
  1879.  
  1880. if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
  1881. /**
  1882. * Installer skin to set strings for the bulk plugin installations..
  1883. *
  1884. * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
  1885. * plugins.
  1886. *
  1887. * @since 2.2.0
  1888. *
  1889. * @package TGM-Plugin-Activation
  1890. * @author Thomas Griffin <thomas@thomasgriffinmedia.com>
  1891. * @author Gary Jones <gamajo@gamajo.com>
  1892. */
  1893. class TGM_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
  1894.  
  1895. /**
  1896. * Holds plugin info for each individual plugin installation.
  1897. *
  1898. * @since 2.2.0
  1899. *
  1900. * @var array
  1901. */
  1902. public $plugin_info = array();
  1903.  
  1904. /**
  1905. * Holds names of plugins that are undergoing bulk installations.
  1906. *
  1907. * @since 2.2.0
  1908. *
  1909. * @var array
  1910. */
  1911. public $plugin_names = array();
  1912.  
  1913. /**
  1914. * Integer to use for iteration through each plugin installation.
  1915. *
  1916. * @since 2.2.0
  1917. *
  1918. * @var integer
  1919. */
  1920. public $i = 0;
  1921.  
  1922. /**
  1923. * Constructor. Parses default args with new ones and extracts them for use.
  1924. *
  1925. * @since 2.2.0
  1926. *
  1927. * @param array $args Arguments to pass for use within the class
  1928. */
  1929. public function __construct( $args = array() ) {
  1930.  
  1931. /** Parse default and new args */
  1932. $defaults = array( 'url' => '', 'nonce' => '', 'names' => array() );
  1933. $args = wp_parse_args( $args, $defaults );
  1934.  
  1935. /** Set plugin names to $this->plugin_names property */
  1936. $this->plugin_names = $args['names'];
  1937.  
  1938. /** Extract the new args */
  1939. parent::__construct( $args );
  1940.  
  1941. }
  1942.  
  1943. /**
  1944. * Sets install skin strings for each individual plugin.
  1945. *
  1946. * Checks to see if the automatic activation flag is set and uses the
  1947. * the proper strings accordingly.
  1948. *
  1949. * @since 2.2.0
  1950. */
  1951. public function add_strings() {
  1952.  
  1953. /** Automatic activation strings */
  1954. if ( TGM_Plugin_Activation::$instance->is_automatic ) {
  1955. $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.', TGM_Plugin_Activation::$instance->domain );
  1956. $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', TGM_Plugin_Activation::$instance->domain ) . ' <a onclick="%2$s" href="#" class="hide-if-no-js"><span>' . __( 'Show Details', TGM_Plugin_Activation::$instance->domain ) . '</span><span class="hidden">' . __( 'Hide Details', TGM_Plugin_Activation::$instance->domain ) . '</span>.</a>';
  1957. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', TGM_Plugin_Activation::$instance->domain );
  1958. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', TGM_Plugin_Activation::$instance->domain );
  1959. }
  1960. /** Default installation strings */
  1961. else {
  1962. $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation process is starting. This process may take a while on some hosts, so please be patient.', TGM_Plugin_Activation::$instance->domain );
  1963. $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', TGM_Plugin_Activation::$instance->domain );
  1964. $this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', TGM_Plugin_Activation::$instance->domain );
  1965. $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed successfully.', TGM_Plugin_Activation::$instance->domain ) . ' <a onclick="%2$s" href="#" class="hide-if-no-js"><span>' . __( 'Show Details', TGM_Plugin_Activation::$instance->domain ) . '</span><span class="hidden">' . __( 'Hide Details', TGM_Plugin_Activation::$instance->domain ) . '</span>.</a>';
  1966. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations have been completed.', TGM_Plugin_Activation::$instance->domain );
  1967. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', TGM_Plugin_Activation::$instance->domain );
  1968. }
  1969.  
  1970. }
  1971.  
  1972. /**
  1973. * Outputs the header strings and necessary JS before each plugin installation.
  1974. *
  1975. * @since 2.2.0
  1976. */
  1977. public function before($title = '') {
  1978.  
  1979. /** We are currently in the plugin installation loop, so set to true */
  1980. $this->in_loop = true;
  1981.  
  1982. 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 );
  1983. echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').show();</script>';
  1984. echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr( $this->upgrader->update_current ) . '"><p>';
  1985.  
  1986. /** Flush header output buffer */
  1987. $this->before_flush_output();
  1988.  
  1989. }
  1990.  
  1991. /**
  1992. * Outputs the footer strings and necessary JS after each plugin installation.
  1993. *
  1994. * Checks for any errors and outputs them if they exist, else output
  1995. * success strings.
  1996. *
  1997. * @since 2.2.0
  1998. */
  1999. public function after($title = '') {
  2000.  
  2001. /** Close install strings */
  2002. echo '</p></div>';
  2003.  
  2004. /** Output error strings if an error has occurred */
  2005. if ( $this->error || ! $this->result ) {
  2006. if ( $this->error )
  2007. echo '<div class="error"><p>' . sprintf( $this->upgrader->strings['skin_update_failed_error'], $this->plugin_names[$this->i], $this->error ) . '</p></div>';
  2008. else
  2009. echo '<div class="error"><p>' . sprintf( $this->upgrader->strings['skin_update_failed'], $this->plugin_names[$this->i] ) . '</p></div>';
  2010.  
  2011. echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js( $this->upgrader->update_current ) . '\').show();</script>';
  2012. }
  2013.  
  2014. /** If the result is set and there are no errors, success! */
  2015. if ( ! empty( $this->result ) && ! is_wp_error( $this->result ) ) {
  2016. echo '<div class="updated"><p>' . sprintf( $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>';
  2017. echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js( $this->upgrader->update_current ) . '\').hide();</script>';
  2018. }
  2019.  
  2020. /** Set in_loop and error to false and flush footer output buffer */
  2021. $this->reset();
  2022. $this->after_flush_output();
  2023.  
  2024. }
  2025.  
  2026. /**
  2027. * Outputs links after bulk plugin installation is complete.
  2028. *
  2029. * @since 2.2.0
  2030. */
  2031. public function bulk_footer() {
  2032.  
  2033. /** Serve up the string to say installations (and possibly activations) are complete */
  2034. parent::bulk_footer();
  2035.  
  2036. /** Flush plugins cache so we can make sure that the installed plugins list is always up to date */
  2037. wp_cache_flush();
  2038.  
  2039. /** Display message based on if all plugins are now active or not */
  2040. $complete = array();
  2041. foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin ) {
  2042. if ( ! is_plugin_active( $plugin['file_path'] ) ) {
  2043. 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'], TGM_Plugin_Activation::$instance->domain ) . '</a></p>';
  2044. $complete[] = $plugin;
  2045. break;
  2046. }
  2047. /** Nothing to store */
  2048. else {
  2049. $complete[] = '';
  2050. }
  2051. }
  2052.  
  2053. /** Filter out any empty entries */
  2054. $complete = array_filter( $complete );
  2055.  
  2056. /** All plugins are active, so we display the complete string and hide the menu to protect users */
  2057. if ( empty( $complete ) ) {
  2058. echo '<p>' . sprintf( TGM_Plugin_Activation::$instance->strings['complete'], '<a href="' . admin_url() . '" title="' . __( 'Return to the Dashboard', TGM_Plugin_Activation::$instance->domain ) . '">' . __( 'Return to the Dashboard', TGM_Plugin_Activation::$instance->domain ) . '</a>' ) . '</p>';
  2059. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  2060. }
  2061.  
  2062. }
  2063.  
  2064. /**
  2065. * Flush header output buffer.
  2066. *
  2067. * @since 2.2.0
  2068. */
  2069. public function before_flush_output() {
  2070.  
  2071. wp_ob_end_flush_all();
  2072. flush();
  2073.  
  2074. }
  2075.  
  2076. /**
  2077. * Flush footer output buffer and iterate $this->i to make sure the
  2078. * installation strings reference the correct plugin.
  2079. *
  2080. * @since 2.2.0
  2081. */
  2082. public function after_flush_output() {
  2083.  
  2084. wp_ob_end_flush_all();
  2085. flush();
  2086. $this->i++;
  2087.  
  2088. }
  2089.  
  2090. }
  2091. }
  2092. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement