tgmpa_load_bulk_installer

Load bulk installer.

Description

tgmpa_load_bulk_installer(); 

Usage

  1. if ( !function_exists( 'tgmpa_load_bulk_installer' ) ) { 
  2. require_once '/functions/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).

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