tgmpa_load_bulk_installer

Load bulk installer.

Description

tgmpa_load_bulk_installer(); 

Usage

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

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