wp_version = $wp_version; // Announce that the class is ready, and pass the object (for advanced use). do_action_ref_array( 'tgmpa_init', array( $this ) ); // When the rest of WP has loaded, kick-start the rest of the class. add_action( 'init', array( $this, 'init' ) ); } /** * Initialise the interactions between this class and WordPress. * * Hooks in three new methods for the class: admin_menu, notices and styles. * * @since 2.0.0 * * @see TGM_Plugin_Activation::admin_menu() * @see TGM_Plugin_Activation::notices() * @see TGM_Plugin_Activation::styles() */ public function init() { // Load class strings. $this->strings = array( 'page_title' => __( 'Install Required Plugins', 'tgmpa' ), 'menu_title' => __( 'Install Plugins', 'tgmpa' ), 'installing' => __( 'Installing Plugin: %s', 'tgmpa' ), 'oops' => __( 'Something went wrong.', 'tgmpa' ), 'notice_can_install_required' => _n_noop( 'This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.', 'tgmpa' ), 'notice_can_install_recommended' => _n_noop( 'This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.', 'tgmpa' ), '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' ), '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' ), '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' ), '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' ), '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' ), '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' ), 'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins', 'tgmpa' ), 'activate_link' => _n_noop( 'Begin activating plugin', 'Begin activating plugins', 'tgmpa' ), 'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ), 'dashboard' => __( 'Return to the dashboard', 'tgmpa' ), 'plugin_activated' => __( 'Plugin activated successfully.', 'tgmpa' ), 'activated_successfully' => __( 'The following plugin was activated successfully:', 'tgmpa' ), 'complete' => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ), 'dismiss' => __( 'Dismiss this notice', 'tgmpa' ), ); do_action( 'tgmpa_register' ); // After this point, the plugins should be registered and the configuration set. // Proceed only if we have plugins to handle. if ( $this->plugins ) { $sorted = array(); foreach ( $this->plugins as $plugin ) { $sorted[] = $plugin['name']; } array_multisort( $sorted, SORT_ASC, $this->plugins ); add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_action( 'admin_head', array( $this, 'dismiss' ) ); add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) ); add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) ); if ( $this->has_notices ) { add_action( 'admin_notices', array( $this, 'notices' ) ); add_action( 'admin_init', array( $this, 'admin_init' ), 1 ); add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) ); add_action( 'switch_theme', array( $this, 'update_dismiss' ) ); } // Setup the force activation hook. foreach ( $this->plugins as $plugin ) { if ( isset( $plugin['force_activation'] ) && true === $plugin['force_activation'] ) { add_action( 'admin_init', array( $this, 'force_activation' ) ); break; } } // Setup the force deactivation hook. foreach ( $this->plugins as $plugin ) { if ( isset( $plugin['force_deactivation'] ) && true === $plugin['force_deactivation'] ) { add_action( 'switch_theme', array( $this, 'force_deactivation' ) ); break; } } } } /** * Handles calls to show plugin information via links in the notices. * * We get the links in the admin notices to point to the TGMPA page, rather * than the typical plugin-install.php file, so we can prepare everything * beforehand. * * WP doesn't make it easy to show the plugin information in the thickbox - * here we have to require a file that includes a function that does the * main work of displaying it, enqueue some styles, set up some globals and * finally call that function before exiting. * * Down right easy once you know how... * * @since 2.1.0 * * @global string $tab Used as iframe div class names, helps with styling * @global string $body_id Used as the iframe body ID, helps with styling * @return null Returns early if not the TGMPA page. */ public function admin_init() { if ( ! $this->is_tgmpa_page() ) { return; } if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Need for install_plugin_information(). wp_enqueue_style( 'plugin-install' ); global $tab, $body_id; $body_id = $tab = 'plugin-information'; install_plugin_information(); exit; } } /** * Enqueues thickbox scripts/styles for plugin info. * * Thickbox is not automatically included on all admin pages, so we must * manually enqueue it for those pages. * * Thickbox is only loaded if the user has not dismissed the admin * notice or if there are any plugins left to install and activate. * * @since 2.1.0 */ public function thickbox() { if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) { add_thickbox(); } } /** * Adds submenu page if there are plugin actions to take. * * This method adds the submenu page letting users know that a required * plugin needs to be installed. * * This page disappears once the plugin has been installed and activated. * * @since 1.0.0 * * @see TGM_Plugin_Activation::init() * @see TGM_Plugin_Activation::install_plugins_page() */ public function admin_menu() { // Make sure privileges are correct to see the page if ( ! current_user_can( 'install_plugins' ) ) { return; } $this->populate_file_path(); foreach ( $this->plugins as $plugin ) { if ( ! is_plugin_active( $plugin['file_path'] ) ) { $args = apply_filters( 'tgmpa_admin_menu_args', array( 'parent_slug' => $this->parent_slug, // Parent Menu slug. 'page_title' => $this->strings['page_title'], // Page title. 'menu_title' => $this->strings['menu_title'], // Menu title. 'capability' => $this->capability, // Capability. 'menu_slug' => $this->menu, // Menu slug. 'function' => array( $this, 'install_plugins_page' ), // Callback. ) ); $this->add_admin_menu( $args ); break; } } } /** * Add the menu item. * * @since 2.5.0 * * @param array $args Menu item configuration. */ protected function add_admin_menu( array $args ) { if ( apply_filters( 'tgmpa_admin_menu_use_add_theme_page', true ) ) { $this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); } else { $this->page_hook = call_user_func( 'add_submenu_page', $args['parent_slug'], $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); } } /** * Echoes plugin installation form. * * This method is the callback for the admin_menu method function. * This displays the admin page and form area where the user can select to install and activate the plugin. * * @since 1.0.0 * * @return null Aborts early if we're processing a plugin installation action */ public function install_plugins_page() { // Store new instance of plugin table in object. $plugin_table = new TGMPA_List_Table; // Return early if processing a plugin installation action. if ( isset( $_POST['action'] ) && 'tgmpa-bulk-install' === $_POST['action'] && $plugin_table->process_bulk_actions() || $this->do_plugin_install() ) { return; } ?>
', wp_kses_post( $activate->get_error_message() ), '
', esc_html( $this->strings['return'] ), '
'; return true; // End it here if there is an error with automatic activation } else { echo '', esc_html( $this->strings['plugin_activated'] ), '
'; } } // Display message based on if all plugins are now active or not. $complete = array(); foreach ( $this->plugins as $plugin ) { if ( ! is_plugin_active( $plugin['file_path'] ) ) { echo '', esc_html( $this->strings['return'] ), '
'; $complete[] = $plugin; break; } // Nothing to store. else { $complete[] = ''; } } // Filter out any empty entries. $complete = array_filter( $complete ); // All plugins are active, so we display the complete string and hide the plugin menu. if ( empty( $complete ) ) { echo '', sprintf( esc_html( $this->strings['complete'] ), '' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '' ), '
'; echo ''; } return true; } // Checks for actions from hover links to process the activation. elseif ( isset( $_GET['plugin'] ) && ( isset( $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) ) { check_admin_referer( 'tgmpa-activate', 'tgmpa-activate-nonce' ); // Populate $plugin array with necessary information. $plugin['name'] = $_GET['plugin_name']; // @todo needs sanitizing, figure out how $plugin['slug'] = sanitize_title( $_GET['plugin'] ); $plugin['source'] = $_GET['plugin_source']; // @todo needs sanitizing, figure out how $plugin_data = get_plugins( '/' . $plugin['slug'] ); // Retrieve all plugins. $plugin_file = array_keys( $plugin_data ); // Retrieve all plugin files from installed plugins. $plugin_to_activate = $plugin['slug'] . '/' . $plugin_file[0]; // Match plugin slug with appropriate plugin file. $activate = activate_plugin( $plugin_to_activate ); // Activate the plugin. if ( is_wp_error( $activate ) ) { echo '', wp_kses_post( $activate->get_error_message() ), '
', esc_html( $this->strings['return'] ), '
'; return true; // End it here if there is an error with activation. } else { // Make sure message doesn't display again if bulk activation is performed immediately after a single activation. if ( ! isset( $_POST['action'] ) ) { echo '', esc_html( $this->strings['activated_successfully'] ), ' ', esc_html( $plugin['name'] ), '.
' . wp_kses_post( $this->dismiss_msg ) . '
'; } // Grab all plugin names. foreach ( $message as $type => $plugin_groups ) { $linked_plugin_groups = array(); // Count number of plugins in each message group to calculate singular/plural message. $count = count( $plugin_groups ); // Loop through the plugin names to make the ones pulled from the .org repo linked. foreach ( $plugin_groups as $plugin_group_single_name ) { $external_url = $this->_get_plugin_data_from_name( $plugin_group_single_name, 'external_url' ); $source = $this->_get_plugin_data_from_name( $plugin_group_single_name, 'source' ); if ( $external_url && preg_match( '|^http(s)?://|', $external_url ) ) { $linked_plugin_groups[] = '' . $plugin_group_single_name . ''; } elseif ( ! $source || preg_match( '|^http://wordpress.org/extend/plugins/|', $source ) ) { $url = add_query_arg( array( 'tab' => 'plugin-information', 'plugin' => urlencode( $this->_get_plugin_data_from_name( $plugin_group_single_name ) ), 'TB_iframe' => 'true', 'width' => '640', 'height' => '500', ), self_admin_url( 'plugin-install.php' ) ); $linked_plugin_groups[] = '' . $plugin_group_single_name . ''; } else { $linked_plugin_groups[] = $plugin_group_single_name; // No hyperlink. } if ( isset( $linked_plugin_groups ) && (array) $linked_plugin_groups ) { $plugin_groups = $linked_plugin_groups; } } $last_plugin = array_pop( $plugin_groups ); // Pop off last name to prep for readability. $imploded = empty( $plugin_groups ) ? '' . $last_plugin . '' : '' . ( implode( ', ', $plugin_groups ) . ' and ' . $last_plugin . '' ); $rendered .= '' . sprintf( translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ), $imploded, $count ) . '
'; } // Setup variables to determine if action links are needed. $show_install_link = $install_link ? '' . translate_nooped_plural( $this->strings['install_link'], $install_link_count, 'tgmpa' ) . '' : ''; $show_activate_link = $activate_link ? '' . translate_nooped_plural( $this->strings['activate_link'], $activate_link_count, 'tgmpa' ) . '' : ''; // Define all of the action links. $action_links = apply_filters( 'tgmpa_notice_action_links', array( 'install' => ( current_user_can( 'install_plugins' ) ) ? $show_install_link : '', 'activate' => ( current_user_can( 'activate_plugins' ) ) ? $show_activate_link : '', 'dismiss' => $this->dismissable ? '' . $this->strings['dismiss'] . '' : '', ) ); $action_links = array_filter( $action_links ); // Remove any empty array items. if ( $action_links ) { $rendered .= apply_filters( 'tgmpa_notice_rendered_action_links', '' . implode( ' | ', $action_links ) . '
' ); } // Register the nag messages and prepare them to be processed. $nag_class = version_compare( $this->wp_version, '3.8', '<' ) ? 'updated' : 'update-nag'; if ( ! empty( $this->strings['nag_type'] ) ) { add_settings_error( 'tgmpa', 'tgmpa', $rendered, sanitize_html_class( strtolower( $this->strings['nag_type'] ) ) ); } else { add_settings_error( 'tgmpa', 'tgmpa', $rendered, $nag_class ); } } // Admin options pages already output settings_errors, so this is to avoid duplication. if ( 'options-general' !== $current_screen->parent_base ) { $this->display_settings_errors(); } } /** * Display settings errors and remove those which have been displayed to avoid duplicate messages showing * * @since 2.5.0 */ protected function display_settings_errors() { global $wp_settings_errors; settings_errors( 'tgmpa' ); foreach ( (array) $wp_settings_errors as $key => $details ) { if ( 'tgmpa' === $details['setting'] ) { unset( $wp_settings_errors[ $key ] ); break; } } } /** * Add dismissable admin notices. * * Appends a link to the admin nag messages. If clicked, the admin notice disappears and no longer is visible to users. * * @since 2.1.0 */ public function dismiss() { if ( isset( $_GET['tgmpa-dismiss'] ) ) { update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 ); } } /** * Add individual plugin to our collection of plugins. * * If the required keys are not set or the plugin has already * been registered, the plugin is not added. * * @since 2.0.0 * * @param array $plugin Array of plugin arguments. */ public function register( $plugin ) { if ( ! isset( $plugin['slug'] ) || ! isset( $plugin['name'] ) ) { return; } foreach ( $this->plugins as $registered_plugin ) { if ( $plugin['slug'] === $registered_plugin['slug'] ) { return; } } $this->plugins[] = $plugin; } /** * Amend default configuration settings. * * @since 2.0.0 * * @param array $config Array of config options to pass as class properties. */ public function config( $config ) { $keys = array( 'id', 'default_path', 'has_notices', 'dismissable', 'dismiss_msg', 'menu', 'parent_slug', 'capability', 'is_automatic', 'message', 'strings', ); foreach ( $keys as $key ) { if ( isset( $config[ $key ] ) ) { if ( is_array( $config[ $key ] ) ) { foreach ( $config[ $key ] as $subkey => $value ) { $this->{$key}[ $subkey ] = $value; } } else { $this->$key = $config[ $key ]; } } } } /** * Amend action link after plugin installation. * * @since 2.0.0 * * @param array $install_actions Existing array of actions. * @return array Amended array of actions. */ public function actions( $install_actions ) { // Remove action links on the TGMPA install page. if ( $this->is_tgmpa_page() ) { return false; } return $install_actions; } /** * Flushes the plugins cache on theme switch to prevent stale entries * from remaining in the plugin table. * * @since 2.4.0 */ public function flush_plugins_cache() { wp_cache_flush(); } /** * Set file_path key for each installed plugin. * * @since 2.1.0 */ public function populate_file_path() { // Add file_path key for all plugins. foreach ( $this->plugins as $plugin => $values ) { $this->plugins[ $plugin ]['file_path'] = $this->_get_plugin_basename_from_slug( $values['slug'] ); } } /** * Helper function to extract the file path of the plugin file from the * plugin slug, if the plugin is installed. * * @since 2.0.0 * * @param string $slug Plugin slug (typically folder name) as provided by the developer. * @return string Either file path for plugin if installed, or just the plugin slug. */ protected function _get_plugin_basename_from_slug( $slug ) { $keys = array_keys( get_plugins() ); foreach ( $keys as $key ) { if ( preg_match( '|^' . $slug . '/|', $key ) ) { return $key; } } return $slug; } /** * Retrieve plugin data, given the plugin name. * * Loops through the registered plugins looking for $name. If it finds it, * it returns the $data from that plugin. Otherwise, returns false. * * @since 2.1.0 * * @param string $name Name of the plugin, as it was registered. * @param string $data Optional. Array key of plugin data to return. Default is slug. * @return string|boolean Plugin slug if found, false otherwise. */ protected function _get_plugin_data_from_name( $name, $data = 'slug' ) { foreach ( $this->plugins as $plugin => $values ) { if ( $name === $values['name'] && isset( $values[ $data ] ) ) { return $values[ $data ]; } } return false; } /** * Determine if we're on the TGMPA Install page. * * @since 2.1.0 * * @return boolean True when on the TGMPA page, false otherwise. */ protected function is_tgmpa_page() { if ( isset( $_GET['page'] ) && $this->menu === $_GET['page'] ) { return true; } return false; } /** * Delete dismissable nag option when theme is switched. * * This ensures that the user(s) is/are again reminded via nag of required * and/or recommended plugins if they re-activate the theme. * * @since 2.1.1 */ public function update_dismiss() { delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true ); } /** * Forces plugin activation if the parameter 'force_activation' is * set to true. * * This allows theme authors to specify certain plugins that must be * active at all times while using the current theme. * * Please take special care when using this parameter as it has the * potential to be harmful if not used correctly. Setting this parameter * to true will not allow the specified plugin to be deactivated unless * the user switches themes. * * @since 2.2.0 */ public function force_activation() { // Set file_path parameter for any installed plugins. $this->populate_file_path(); $installed_plugins = get_plugins(); foreach ( $this->plugins as $plugin ) { // Oops, plugin isn't there so iterate to next condition. if ( isset( $plugin['force_activation'] ) && $plugin['force_activation'] && ! isset( $installed_plugins[ $plugin['file_path'] ] ) ) { continue; } // There we go, activate the plugin. elseif ( isset( $plugin['force_activation'] ) && $plugin['force_activation'] && is_plugin_inactive( $plugin['file_path'] ) ) { activate_plugin( $plugin['file_path'] ); } } } /** * Forces plugin deactivation if the parameter 'force_deactivation' * is set to true. * * This allows theme authors to specify certain plugins that must be * deactivated upon switching from the current theme to another. * * Please take special care when using this parameter as it has the * potential to be harmful if not used correctly. * * @since 2.2.0 */ public function force_deactivation() { // Set file_path parameter for any installed plugins. $this->populate_file_path(); foreach ( $this->plugins as $plugin ) { // Only proceed forward if the parameter is set to true and plugin is active. if ( isset( $plugin['force_deactivation'] ) && $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) ) { deactivate_plugins( $plugin['file_path'] ); } } } /** * Returns the singleton instance of the class. * * @since 2.4.0 * * @return object The TGM_Plugin_Activation object. */ public static function get_instance() { if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { self::$instance = new self(); } return self::$instance; } } if ( ! function_exists( 'load_tgm_plugin_activation' ) ) { // Ensure only one instance of the class is ever invoked. function load_tgm_plugin_activation() { $GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance(); } } if ( did_action( 'plugins_loaded' ) ) { load_tgm_plugin_activation(); } else { add_action( 'plugins_loaded', 'load_tgm_plugin_activation' ); } } if ( ! function_exists( 'tgmpa' ) ) { /** * Helper function to register a collection of required plugins. * * @since 2.0.0 * @api * * @param array $plugins An array of plugin arrays. * @param array $config Optional. An array of configuration values. */ function tgmpa( $plugins, $config = array() ) { $instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); foreach ( $plugins as $plugin ) { call_user_func( array( $instance, 'register' ), $plugin ); } if ( $config ) { call_user_func( array( $instance, 'config' ), $config ); } } } /** * WP_List_Table isn't always available. If it isn't available, * we load it here. * * @since 2.2.0 */ if ( ! class_exists( 'WP_List_Table' ) ) { require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; } if ( ! class_exists( 'TGMPA_List_Table' ) ) { /** * List table class for handling plugins. * * Extends the WP_List_Table class to provide a future-compatible * way of listing out all required/recommended plugins. * * Gives users an interface similar to the Plugin Administration * area with similar (albeit stripped down) capabilities. * * This class also allows for the bulk install of plugins. * * @since 2.2.0 * * @package TGM-Plugin-Activation * @author Thomas Griffin * @author Gary Jones */ class TGMPA_List_Table extends WP_List_Table { /** * TGMPA instance * * @since 2.5.0 * * @var object */ protected $tgmpa; /** * Parent menu file slug. * * @since 2.5.0 * * @var string */ protected $admin_page_base; /** * References parent constructor and sets defaults for class. * * @since 2.2.0 */ public function __construct() { $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); $this->admin_page_base = $this->tgmpa->parent_slug; parent::__construct( array( 'singular' => 'plugin', 'ajax' => false, ) ); } /** * Gathers and renames all of our plugin information to be used by * WP_List_Table to create our table. * * @since 2.2.0 * * @return array $table_data Information for use in table. */ protected function _gather_plugin_data() { // Load thickbox for plugin links. $this->tgmpa->admin_init(); $this->tgmpa->thickbox(); // Prep variables for use and grab list of all installed plugins. $table_data = array(); $i = 0; $installed_plugins = get_plugins(); foreach ( $this->tgmpa->plugins as $plugin ) { if ( is_plugin_active( $plugin['file_path'] ) || ( isset( $plugin['is_callable'] ) && is_callable( $plugin['is_callable'] ) ) ) { continue; // No need to display plugins if they are installed and activated. } $table_data[ $i ]['sanitized_plugin'] = $plugin['name']; $table_data[ $i ]['slug'] = $this->_get_plugin_data_from_name( $plugin['name'] ); $external_url = $this->_get_plugin_data_from_name( $plugin['name'], 'external_url' ); $source = $this->_get_plugin_data_from_name( $plugin['name'], 'source' ); if ( $external_url && preg_match( '|^http(s)?://|', $external_url ) ) { $table_data[ $i ]['plugin'] = '' . $plugin['name'] . ''; } elseif ( ! $source || preg_match( '|^http://wordpress.org/extend/plugins/|', $source ) ) { $url = add_query_arg( array( 'tab' => 'plugin-information', 'plugin' => urlencode( $this->_get_plugin_data_from_name( $plugin['name'] ) ), 'TB_iframe' => 'true', 'width' => '640', 'height' => '500', ), self_admin_url( 'plugin-install.php' ) ); $table_data[ $i ]['plugin'] = '' . $plugin['name'] . ''; } else { $table_data[ $i ]['plugin'] = '' . $plugin['name'] . ''; // No hyperlink. } if ( isset( $table_data[ $i ]['plugin'] ) && (array) $table_data[ $i ]['plugin'] ) { $plugin['name'] = $table_data[ $i ]['plugin']; } if ( ! empty( $plugin['source'] ) ) { if ( preg_match( '|^http(s)?://|', $plugin['source'] ) ) { // The plugin must be from a private repository. $table_data[ $i ]['source'] = __( 'Private Repository', 'tgmpa' ); } else { // The plugin is pre-packaged with the theme. $table_data[ $i ]['source'] = __( 'Pre-Packaged', 'tgmpa' ); } } // The plugin is from the WordPress repository. else { $table_data[ $i ]['source'] = __( 'WordPress Repository', 'tgmpa' ); } $table_data[ $i ]['type'] = isset( $plugin['required'] ) && $plugin['required'] ? __( 'Required', 'tgmpa' ) : __( 'Recommended', 'tgmpa' ); if ( ! isset( $installed_plugins[ $plugin['file_path'] ] ) ) { $table_data[ $i ]['status'] = sprintf( '%1$s', __( 'Not Installed', 'tgmpa' ) ); } elseif ( is_plugin_inactive( $plugin['file_path'] ) ) { $table_data[ $i ]['status'] = sprintf( '%1$s', __( 'Installed But Not Activated', 'tgmpa' ) ); } $table_data[ $i ]['file_path'] = $plugin['file_path']; $table_data[ $i ]['url'] = isset( $plugin['source'] ) ? $plugin['source'] : 'repo'; $table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin ); $i++; } // Sort plugins by Required/Recommended type and by alphabetical listing within each type. $resort = array(); $req = array(); $rec = array(); // Grab all the plugin types. foreach ( $table_data as $plugin ) { $resort[] = $plugin['type']; } // Sort each plugin by type. foreach ( $resort as $type ) { if ( 'Required' === $type ) { $req[] = $type; } else { $rec[] = $type; } } // Sort alphabetically each plugin type array, merge them and then sort in reverse (lists Required plugins first). sort( $req ); sort( $rec ); array_merge( $resort, $req, $rec ); array_multisort( $resort, SORT_DESC, $table_data ); return $table_data; } /** * Retrieve plugin data, given the plugin name. Taken from the * TGM_Plugin_Activation class. * * Loops through the registered plugins looking for $name. If it finds it, * it returns the $data from that plugin. Otherwise, returns false. * * @since 2.2.0 * * @param string $name Name of the plugin, as it was registered. * @param string $data Optional. Array key of plugin data to return. Default is slug. * @return string|boolean Plugin slug if found, false otherwise. */ protected function _get_plugin_data_from_name( $name, $data = 'slug' ) { foreach ( $this->tgmpa->plugins as $plugin => $values ) { if ( $name === $values['name'] && isset( $values[ $data ] ) ) { return $values[ $data ]; } } return false; } /** * Create default columns to display important plugin information * like type, action and status. * * @since 2.2.0 * * @param array $item Array of item data. * @param string $column_name The name of the column. */ public function column_default( $item, $column_name ) { return $item[ $column_name ]; } /** * Create default title column along with action links of 'Install' * and 'Activate'. * * @since 2.2.0 * * @param array $item Array of item data. * @return string The action hover links. */ public function column_plugin( $item ) { $installed_plugins = get_plugins(); // No need to display any hover links. if ( is_plugin_active( $item['file_path'] ) ) { $actions = array(); } // We need to display the 'Install' hover link. if ( ! isset( $installed_plugins[ $item['file_path'] ] ) ) { $install_nonce_url = wp_nonce_url( add_query_arg( array( 'page' => urlencode( $this->tgmpa->menu ), 'plugin' => urlencode( $item['slug'] ), 'plugin_name' => urlencode( $item['sanitized_plugin'] ), 'plugin_source' => urlencode( $item['url'] ), 'tgmpa-install' => 'install-plugin', ), self_admin_url( $this->admin_page_base ) ), 'tgmpa-install' ); $actions = array( 'install' => sprintf( '' . esc_attr_x( 'Install %2$s', '%2$s = plugin name in screen reader markup', 'tgmpa' ) . '', esc_url( $install_nonce_url ), '' . $item['sanitized_plugin'] . '' ), ); } // We need to display the 'Activate' hover link. elseif ( is_plugin_inactive( $item['file_path'] ) ) { $activate_url = add_query_arg( array( 'page' => urlencode( $this->tgmpa->menu ), 'plugin' => urlencode( $item['slug'] ), 'plugin_name' => urlencode( $item['sanitized_plugin'] ), 'plugin_source' => urlencode( $item['url'] ), 'tgmpa-activate' => 'activate-plugin', 'tgmpa-activate-nonce' => urlencode( wp_create_nonce( 'tgmpa-activate' ) ), ), self_admin_url( $this->admin_page_base ) ); $actions = array( 'activate' => sprintf( '' . esc_attr_x( 'Activate %2$s', '%2$s = plugin name in screen reader markup', 'tgmpa' ) . '', esc_url( $activate_url ), '' . $item['sanitized_plugin'] . '' ), ); } $prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : ''; $actions = apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $actions ), $item['slug'] ); return sprintf( '%1$s %2$s', $item['plugin'], $this->row_actions( $actions ) ); } /** * Required for bulk installing. * * Adds a checkbox for each plugin. * * @since 2.2.0 * * @param array $item Array of item data. * @return string The input checkbox with all necessary info. */ public function column_cb( $item ) { $plugin_url = ( 'repo' === $item['url'] ) ? $item['url'] : esc_url( $item['url'] ); $value = $item['file_path'] . ',' . $plugin_url . ',' . $item['sanitized_plugin']; return sprintf( '', $this->_args['singular'], $value, $item['sanitized_plugin'] ); } /** * Sets default message within the plugins table if no plugins * are left for interaction. * * Hides the menu item to prevent the user from clicking and * getting a permissions error. * * @since 2.2.0 */ public function no_items() { printf( wp_kses_post( __( 'No plugins to install or activate. Return to the Dashboard', 'tgmpa' ) ), esc_url( self_admin_url() ) ); echo ''; } /** * Output all the column information within the table. * * @since 2.2.0 * * @return array $columns The column names. */ public function get_columns() { $columns = array( 'cb' => '', 'plugin' => __( 'Plugin', 'tgmpa' ), 'source' => __( 'Source', 'tgmpa' ), 'type' => __( 'Type', 'tgmpa' ), 'status' => __( 'Status', 'tgmpa' ), ); return apply_filters( 'tgmpa_table_columns', $columns ); } /** * Defines all types of bulk actions for handling * registered plugins. * * @since 2.2.0 * * @return array $actions The bulk actions for the plugin install table. */ public function get_bulk_actions() { $actions = array( 'tgmpa-bulk-install' => __( 'Install', 'tgmpa' ), 'tgmpa-bulk-activate' => __( 'Activate', 'tgmpa' ), ); return $actions; } /** * Processes bulk installation and activation actions. * * The bulk installation process looks either for the $_POST * information or for the plugin info within the $_GET variable if * a user has to use WP_Filesystem to enter their credentials. * * @since 2.2.0 */ public function process_bulk_actions() { // Bulk installation process. if ( 'tgmpa-bulk-install' === $this->current_action() ) { check_admin_referer( 'bulk-' . $this->_args['plural'] ); // Prep variables to be populated. $plugins_to_install = array(); $plugin_installs = array(); $plugin_path = array(); $plugin_name = array(); // Look first to see if information has been passed via WP_Filesystem. if ( isset( $_GET['plugins'] ) ) { $plugins = explode( ',', stripslashes( $_GET['plugins'] ) ); } // Looks like the user can use the direct method, take from $_POST. elseif ( isset( $_POST['plugin'] ) ) { $plugins = (array) $_POST['plugin']; // @todo needs sanitizing, figure out how } // Nothing has been submitted. else { $plugins = array(); } // Grab information from $_POST if available. if ( isset( $_POST['plugin'] ) ) { foreach ( $plugins as $plugin_data ) { $plugins_to_install[] = explode( ',', $plugin_data ); } foreach ( $plugins_to_install as $plugin_data ) { $plugin_installs[] = $plugin_data[0]; $plugin_path[] = $plugin_data[1]; $plugin_name[] = $plugin_data[2]; } } // Information has been passed via $_GET. else { foreach ( $plugins as $key => $value ) { // Grab plugin slug for each plugin. if ( 0 === ( $key % 3 ) || 0 === $key ) { $plugins_to_install[] = $value; $plugin_installs[] = $value; } } } // Look first to see if information has been passed via WP_Filesystem. if ( isset( $_GET['plugin_paths'] ) ) { $plugin_paths = explode( ',', stripslashes( $_GET['plugin_paths'] ) ); } // Looks like the user doesn't need to enter his FTP creds. elseif ( isset( $_POST['plugin'] ) ) { $plugin_paths = (array) $plugin_path; } // Nothing has been submitted. else { $plugin_paths = array(); } // Look first to see if information has been passed via WP_Filesystem. if ( isset( $_GET['plugin_names'] ) ) { $plugin_names = explode( ',', stripslashes( $_GET['plugin_names'] ) ); } // Looks like the user doesn't need to enter his FTP creds. elseif ( isset( $_POST['plugin'] ) ) { $plugin_names = (array) $plugin_name; } // Nothing has been submitted. else { $plugin_names = array(); } // Loop through plugin slugs and remove already installed plugins from the list. $i = 0; foreach ( $plugin_installs as $key => $plugin ) { if ( preg_match( '|.php$|', $plugin ) ) { unset( $plugin_installs[ $key ] ); // If the plugin path isn't in the $_GET variable, we can unset the corresponding path. if ( ! isset( $_GET['plugin_paths'] ) ) { unset( $plugin_paths[ $i ] ); } // If the plugin name isn't in the $_GET variable, we can unset the corresponding name. if ( ! isset( $_GET['plugin_names'] ) ) { unset( $plugin_names[ $i ] ); } } $i++; } // No need to proceed further if we have no plugins to install. if ( empty( $plugin_installs ) ) { echo '', esc_html__( 'No plugins are available to be installed at this time.', 'tgmpa' ), '
', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '
', wp_kses_post( $activate->get_error_message() ), '
%1$s %2$s.
', sprintf( wp_kses_post( $this->upgrader->strings['skin_update_failed_error'] ), esc_html( $this->plugin_names[ $this->i ] ), wp_kses_post( $this->error ) ), '
', sprintf( wp_kses_post( $this->upgrader->strings['skin_update_failed'] ), esc_html( $this->plugin_names[ $this->i ] ) ), '
', sprintf( wp_kses_post( $this->upgrader->strings['skin_update_successful'] ), esc_html( $this->plugin_names[ $this->i ] ), 'jQuery(\'#progress-' . esc_js( $this->upgrader->update_current ) . '\').toggle();jQuery(\'span\', this).toggle(); return false;' ), '
', esc_html( $this->tgmpa->strings['return'] ), '
'; $complete[] = $plugin; break; } // Nothing to store. else { $complete[] = ''; } } // Filter out any empty entries. $complete = array_filter( $complete ); // All plugins are active, so we display the complete string and hide the menu to protect users. if ( empty( $complete ) ) { echo '', sprintf( esc_html( $this->tgmpa->strings['complete'] ), '' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '' ), '
'; echo ''; } } /** * Flush header output buffer. * * @since 2.2.0 */ public function before_flush_output() { wp_ob_end_flush_all(); flush(); } /** * Flush footer output buffer and iterate $this->i to make sure the * installation strings reference the correct plugin. * * @since 2.2.0 */ public function after_flush_output() { wp_ob_end_flush_all(); flush(); $this->i++; } } } } } }