Advertisement
artlessChristian

TGM-Plugin-Activation with update function

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