tgmpa_load_bulk_installer

Load bulk installer.

Description

tgmpa_load_bulk_installer(); 

Usage

  1. if ( !function_exists( 'tgmpa_load_bulk_installer' ) ) { 
  2. require_once '/class-tgm-plugin-activation.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = tgmpa_load_bulk_installer(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/class-tgm-plugin-activation.php  
  1. function tgmpa_load_bulk_installer() { 
  2. // Silently fail if 2.5+ is loaded *after* an older version. 
  3. if ( ! isset( $GLOBALS['tgmpa'] ) ) { 
  4. return; 
  5.  
  6. // Get TGMPA class instance. 
  7. $tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); 
  8.  
  9. if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) { 
  10. if ( ! class_exists( 'Plugin_Upgrader', false ) ) { 
  11. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
  12.  
  13. if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) { 
  14.  
  15. /** 
  16. * Installer class to handle bulk plugin installations. 
  17. * Extends WP_Upgrader and customizes to suit the installation of multiple 
  18. * plugins. 
  19. * @since 2.2.0 
  20. * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}} 
  21. * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer. 
  22. * This was done to prevent backward compatibility issues with v2.3.6.}} 
  23. * @package TGM-Plugin-Activation 
  24. * @author Thomas Griffin 
  25. * @author Gary Jones 
  26. */ 
  27. class TGMPA_Bulk_Installer extends Plugin_Upgrader { 
  28. /** 
  29. * Holds result of bulk plugin installation. 
  30. * @since 2.2.0 
  31. * @var string 
  32. */ 
  33. public $result; 
  34.  
  35. /** 
  36. * Flag to check if bulk installation is occurring or not. 
  37. * @since 2.2.0 
  38. * @var boolean 
  39. */ 
  40. public $bulk = false; 
  41.  
  42. /** 
  43. * TGMPA instance 
  44. * @since 2.5.0 
  45. * @var object 
  46. */ 
  47. protected $tgmpa; 
  48.  
  49. /** 
  50. * Whether or not the destination directory needs to be cleared ( = on update). 
  51. * @since 2.5.0 
  52. * @var bool 
  53. */ 
  54. protected $clear_destination = false; 
  55.  
  56. /** 
  57. * References parent constructor and sets defaults for class. 
  58. * @since 2.2.0 
  59. * @param \Bulk_Upgrader_Skin|null $skin Installer skin. 
  60. */ 
  61. public function __construct( $skin = null ) { 
  62. // Get TGMPA class instance. 
  63. $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); 
  64.  
  65. parent::__construct( $skin ); 
  66.  
  67. if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) { 
  68. $this->clear_destination = true; 
  69.  
  70. if ( $this->tgmpa->is_automatic ) { 
  71. $this->activate_strings(); 
  72.  
  73. add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) ); 
  74.  
  75. /** 
  76. * Sets the correct activation strings for the installer skin to use. 
  77. * @since 2.2.0 
  78. */ 
  79. public function activate_strings() { 
  80. $this->strings['activation_failed'] = __( 'Plugin activation failed.', 'enigma' ); 
  81. $this->strings['activation_success'] = __( 'Plugin activated successfully.', 'enigma' ); 
  82.  
  83. /** 
  84. * Performs the actual installation of each plugin. 
  85. * @since 2.2.0 
  86. * @see WP_Upgrader::run() 
  87. * @param array $options The installation config options. 
  88. * @return null|array Return early if error, array of installation data on success. 
  89. */ 
  90. public function run( $options ) { 
  91. $result = parent::run( $options ); 
  92.  
  93. // Reset the strings in case we changed one during automatic activation. 
  94. if ( $this->tgmpa->is_automatic ) { 
  95. if ( 'update' === $this->skin->options['install_type'] ) { 
  96. $this->upgrade_strings(); 
  97. } else { 
  98. $this->install_strings(); 
  99.  
  100. return $result; 
  101.  
  102. /** 
  103. * Processes the bulk installation of plugins. 
  104. * @since 2.2.0 
  105. * {@internal This is basically a near identical copy of the WP Core 
  106. * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with 
  107. * new installs instead of upgrades. 
  108. * For ease of future synchronizations, the adjustments are clearly commented, but no other 
  109. * comments are added. Code style has been made to comply.}} 
  110. * @see Plugin_Upgrader::bulk_upgrade() 
  111. * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838 
  112. * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}} 
  113. * @param array $plugins The plugin sources needed for installation. 
  114. * @param array $args Arbitrary passed extra arguments. 
  115. * @return array|false Install confirmation messages on success, false on failure. 
  116. */ 
  117. public function bulk_install( $plugins, $args = array() ) { 
  118. // [TGMPA + ] Hook auto-activation in. 
  119. add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); 
  120.  
  121. $defaults = array( 
  122. 'clear_update_cache' => true,  
  123. ); 
  124. $parsed_args = wp_parse_args( $args, $defaults ); 
  125.  
  126. $this->init(); 
  127. $this->bulk = true; 
  128.  
  129. $this->install_strings(); // [TGMPA + ] adjusted. 
  130.  
  131. /** [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */ 
  132.  
  133. /** [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */ 
  134.  
  135. $this->skin->header(); 
  136.  
  137. // Connect to the Filesystem first. 
  138. $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) ); 
  139. if ( ! $res ) { 
  140. $this->skin->footer(); 
  141. return false; 
  142.  
  143. $this->skin->bulk_header(); 
  144.  
  145. /** 
  146. * Only start maintenance mode if: 
  147. * - running Multisite and there are one or more plugins specified, OR 
  148. * - a plugin with an update available is currently active. 
  149. * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible. 
  150. */ 
  151. $maintenance = ( is_multisite() && ! empty( $plugins ) ); 
  152.  
  153. /** 
  154. [TGMPA - ] 
  155. foreach ( $plugins as $plugin ) 
  156. $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) ); 
  157. */ 
  158. if ( $maintenance ) { 
  159. $this->maintenance_mode( true ); 
  160.  
  161. $results = array(); 
  162.  
  163. $this->update_count = count( $plugins ); 
  164. $this->update_current = 0; 
  165. foreach ( $plugins as $plugin ) { 
  166. $this->update_current++; 
  167.  
  168. /** 
  169. [TGMPA - ] 
  170. $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true); 
  171.   
  172. if ( !isset( $current->response[ $plugin ] ) ) { 
  173. $this->skin->set_result('up_to_date'); 
  174. $this->skin->before(); 
  175. $this->skin->feedback('up_to_date'); 
  176. $this->skin->after(); 
  177. $results[$plugin] = true; 
  178. continue; 
  179.   
  180. // Get the URL to the zip file. 
  181. $r = $current->response[ $plugin ]; 
  182.   
  183. $this->skin->plugin_active = is_plugin_active($plugin); 
  184. */ 
  185.  
  186. $result = $this->run( 
  187. array( 
  188. 'package' => $plugin, // [TGMPA + ] adjusted. 
  189. 'destination' => WP_PLUGIN_DIR,  
  190. 'clear_destination' => false, // [TGMPA + ] adjusted. 
  191. 'clear_working' => true,  
  192. 'is_multi' => true,  
  193. 'hook_extra' => array( 
  194. 'plugin' => $plugin,  
  195. ),  
  196. ); 
  197.  
  198. $results[ $plugin ] = $this->result; 
  199.  
  200. // Prevent credentials auth screen from displaying multiple times. 
  201. if ( false === $result ) { 
  202. break; 
  203. } //end foreach $plugins 
  204.  
  205. $this->maintenance_mode( false ); 
  206.  
  207. /** 
  208. * Fires when the bulk upgrader process is complete. 
  209. * @since WP 3.6.0 / TGMPA 2.5.0 
  210. * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might 
  211. * be a Theme_Upgrader or Core_Upgrade instance. 
  212. * @param array $data { 
  213. * Array of bulk item update data. 
  214. * @type string $action Type of action. Default 'update'. 
  215. * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'. 
  216. * @type bool $bulk Whether the update process is a bulk update. Default true. 
  217. * @type array $packages Array of plugin, theme, or core packages to update. 
  218. * } 
  219. */ 
  220. do_action( 'upgrader_process_complete', $this, array( 
  221. 'action' => 'install', // [TGMPA + ] adjusted. 
  222. 'type' => 'plugin',  
  223. 'bulk' => true,  
  224. 'plugins' => $plugins,  
  225. ) ); 
  226.  
  227. $this->skin->bulk_footer(); 
  228.  
  229. $this->skin->footer(); 
  230.  
  231. // Cleanup our hooks, in case something else does a upgrade on this connection. 
  232. /** [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */ 
  233.  
  234. // [TGMPA + ] Remove our auto-activation hook. 
  235. remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); 
  236.  
  237. // Force refresh of plugin update information. 
  238. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] ); 
  239.  
  240. return $results; 
  241.  
  242. /** 
  243. * Handle a bulk upgrade request. 
  244. * @since 2.5.0 
  245. * @see Plugin_Upgrader::bulk_upgrade() 
  246. * @param array $plugins The local WP file_path's of the plugins which should be upgraded. 
  247. * @param array $args Arbitrary passed extra arguments. 
  248. * @return string|bool Install confirmation messages on success, false on failure. 
  249. */ 
  250. public function bulk_upgrade( $plugins, $args = array() ) { 
  251.  
  252. add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); 
  253.  
  254. $result = parent::bulk_upgrade( $plugins, $args ); 
  255.  
  256. remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); 
  257.  
  258. return $result; 
  259.  
  260. /** 
  261. * Abuse a filter to auto-activate plugins after installation. 
  262. * Hooked into the 'upgrader_post_install' filter hook. 
  263. * @since 2.5.0 
  264. * @param bool $bool The value we need to give back (true). 
  265. * @return bool 
  266. */ 
  267. public function auto_activate( $bool ) { 
  268. // Only process the activation of installed plugins if the automatic flag is set to true. 
  269. if ( $this->tgmpa->is_automatic ) { 
  270. // Flush plugins cache so the headers of the newly installed plugins will be read correctly. 
  271. wp_clean_plugins_cache(); 
  272.  
  273. // Get the installed plugin file. 
  274. $plugin_info = $this->plugin_info(); 
  275.  
  276. // Don't try to activate on upgrade of active plugin as WP will do this already. 
  277. if ( ! is_plugin_active( $plugin_info ) ) { 
  278. $activate = activate_plugin( $plugin_info ); 
  279.  
  280. // Adjust the success string based on the activation result. 
  281. $this->strings['process_success'] = $this->strings['process_success'] . "<br />\n"; 
  282.  
  283. if ( is_wp_error( $activate ) ) { 
  284. $this->skin->error( $activate ); 
  285. $this->strings['process_success'] .= $this->strings['activation_failed']; 
  286. } else { 
  287. $this->strings['process_success'] .= $this->strings['activation_success']; 
  288.  
  289. return $bool; 
  290.  
  291. if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) { 
  292.  
  293. /** 
  294. * Installer skin to set strings for the bulk plugin installations.. 
  295. * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple 
  296. * plugins. 
  297. * @since 2.2.0 
  298. * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to 
  299. * TGMPA_Bulk_Installer_Skin. 
  300. * This was done to prevent backward compatibility issues with v2.3.6.}} 
  301. * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php 
  302. * @package TGM-Plugin-Activation 
  303. * @author Thomas Griffin 
  304. * @author Gary Jones 
  305. */ 
  306. class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin { 
  307. /** 
  308. * Holds plugin info for each individual plugin installation. 
  309. * @since 2.2.0 
  310. * @var array 
  311. */ 
  312. public $plugin_info = array(); 
  313.  
  314. /** 
  315. * Holds names of plugins that are undergoing bulk installations. 
  316. * @since 2.2.0 
  317. * @var array 
  318. */ 
  319. public $plugin_names = array(); 
  320.  
  321. /** 
  322. * Integer to use for iteration through each plugin installation. 
  323. * @since 2.2.0 
  324. * @var integer 
  325. */ 
  326. public $i = 0; 
  327.  
  328. /** 
  329. * TGMPA instance 
  330. * @since 2.5.0 
  331. * @var object 
  332. */ 
  333. protected $tgmpa; 
  334.  
  335. /** 
  336. * Constructor. Parses default args with new ones and extracts them for use. 
  337. * @since 2.2.0 
  338. * @param array $args Arguments to pass for use within the class. 
  339. */ 
  340. public function __construct( $args = array() ) { 
  341. // Get TGMPA class instance. 
  342. $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); 
  343.  
  344. // Parse default and new args. 
  345. $defaults = array( 
  346. 'url' => '',  
  347. 'nonce' => '',  
  348. 'names' => array(),  
  349. 'install_type' => 'install',  
  350. ); 
  351. $args = wp_parse_args( $args, $defaults ); 
  352.  
  353. // Set plugin names to $this->plugin_names property. 
  354. $this->plugin_names = $args['names']; 
  355.  
  356. // Extract the new args. 
  357. parent::__construct( $args ); 
  358.  
  359. /** 
  360. * Sets install skin strings for each individual plugin. 
  361. * Checks to see if the automatic activation flag is set and uses the 
  362. * the proper strings accordingly. 
  363. * @since 2.2.0 
  364. */ 
  365. public function add_strings() { 
  366. if ( 'update' === $this->options['install_type'] ) { 
  367. parent::add_strings(); 
  368. /** translators: 1: plugin name, 2: action number 3: total number of actions. */ 
  369. $this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'enigma' ); 
  370. } else { 
  371. /** translators: 1: plugin name, 2:errormessage. */ 
  372. $this->upgrader->strings['skin_update_failed_error'] = __( 'Anerroroccurred while installing %1$s: <strong>%2$s</strong>.', 'enigma' ); 
  373. /** translators: 1: plugin name. */ 
  374. $this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'enigma' ); 
  375.  
  376. if ( $this->tgmpa->is_automatic ) { 
  377. // Automatic activation strings. 
  378. $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.', 'enigma' ); 
  379. /** translators: 1: plugin name. */ 
  380. $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'enigma' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'enigma' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'enigma' ) . '</span>.</a>'; 
  381. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', 'enigma' ); 
  382. /** translators: 1: plugin name, 2: action number 3: total number of actions. */ 
  383. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'enigma' ); 
  384. } else { 
  385. // Default installation strings. 
  386. $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation process is starting. This process may take a while on some hosts, so please be patient.', 'enigma' ); 
  387. /** translators: 1: plugin name. */ 
  388. $this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'enigma' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'enigma' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'enigma' ) . '</span>.</a>'; 
  389. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations have been completed.', 'enigma' ); 
  390. /** translators: 1: plugin name, 2: action number 3: total number of actions. */ 
  391. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'enigma' ); 
  392.  
  393. /** 
  394. * Outputs the header strings and necessary JS before each plugin installation. 
  395. * @since 2.2.0 
  396. * @param string $title Unused in this implementation. 
  397. */ 
  398. public function before( $title = '' ) { 
  399. if ( empty( $title ) ) { 
  400. $title = esc_html( $this->plugin_names[ $this->i ] ); 
  401. parent::before( $title ); 
  402.  
  403. /** 
  404. * Outputs the footer strings and necessary JS after each plugin installation. 
  405. * Checks for any errors and outputs them if they exist, else output 
  406. * success strings. 
  407. * @since 2.2.0 
  408. * @param string $title Unused in this implementation. 
  409. */ 
  410. public function after( $title = '' ) { 
  411. if ( empty( $title ) ) { 
  412. $title = esc_html( $this->plugin_names[ $this->i ] ); 
  413. parent::after( $title ); 
  414.  
  415. $this->i++; 
  416.  
  417. /** 
  418. * Outputs links after bulk plugin installation is complete. 
  419. * @since 2.2.0 
  420. */ 
  421. public function bulk_footer() { 
  422. // Serve up the string to say installations (and possibly activations) are complete. 
  423. parent::bulk_footer(); 
  424.  
  425. // Flush plugins cache so we can make sure that the installed plugins list is always up to date. 
  426. wp_clean_plugins_cache(); 
  427.  
  428. $this->tgmpa->show_tgmpa_version(); 
  429.  
  430. // Display message based on if all plugins are now active or not. 
  431. $update_actions = array(); 
  432.  
  433. if ( $this->tgmpa->is_tgmpa_complete() ) { 
  434. // All plugins are active, so we display the complete string and hide the menu to protect users. 
  435. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; 
  436. $update_actions['dashboard'] = sprintf( 
  437. esc_html( $this->tgmpa->strings['complete'] ),  
  438. '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'enigma' ) . '</a>' 
  439. ); 
  440. } else { 
  441. $update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>'; 
  442.  
  443. /** 
  444. * Filter the list of action links available following bulk plugin installs/updates. 
  445. * @since 2.5.0 
  446. * @param array $update_actions Array of plugin action links. 
  447. * @param array $plugin_info Array of information for the last-handled plugin. 
  448. */ 
  449. $update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info ); 
  450.  
  451. if ( ! empty( $update_actions ) ) { 
  452. $this->feedback( implode( ' | ', (array) $update_actions ) ); 
  453.  
  454. /** *********** DEPRECATED METHODS *********** */ 
  455.  
  456. /** 
  457. * Flush header output buffer. 
  458. * @since 2.2.0 
  459. * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead 
  460. * @see Bulk_Upgrader_Skin::flush_output() 
  461. */ 
  462. public function before_flush_output() { 
  463. _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' ); 
  464. $this->flush_output(); 
  465.  
  466. /** 
  467. * Flush footer output buffer and iterate $this->i to make sure the 
  468. * installation strings reference the correct plugin. 
  469. * @since 2.2.0 
  470. * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead 
  471. * @see Bulk_Upgrader_Skin::flush_output() 
  472. */ 
  473. public function after_flush_output() { 
  474. _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' ); 
  475. $this->flush_output(); 
  476. $this->i++;