Completed
Pull Request — develop (#689)
by
unknown
01:46
created

TGMPA_Bulk_Installer::activate_strings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
	
3
	namespace TGM;
4
	
5
	/**
6
	 * Installer class to handle bulk plugin installations.
7
	 *
8
	 * Extends WP_Upgrader and customizes to suit the installation of multiple
9
	 * plugins.
10
	 *
11
	 * @since 2.2.0
12
	 *
13
	 * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}}
14
	 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
15
	 *            This was done to prevent backward compatibility issues with v2.3.6.}}
16
	 *
17
	 * @package TGM-Plugin-Activation
18
	 * @author  Thomas Griffin
19
	 * @author  Gary Jones
20
	 */
21
	class TGMPA_Bulk_Installer extends Plugin_Upgrader {
22
	  /**
23
	   * Holds result of bulk plugin installation.
24
	   *
25
	   * @since 2.2.0
26
	   *
27
	   * @var string
28
	   */
29
	  public $result;
30
	
31
	  /**
32
	   * Flag to check if bulk installation is occurring or not.
33
	   *
34
	   * @since 2.2.0
35
	   *
36
	   * @var boolean
37
	   */
38
	  public $bulk = false;
39
	
40
	  /**
41
	   * TGMPA instance
42
	   *
43
	   * @since 2.5.0
44
	   *
45
	   * @var object
46
	   */
47
	  protected $tgmpa;
48
	
49
	  /**
50
	   * Whether or not the destination directory needs to be cleared ( = on update).
51
	   *
52
	   * @since 2.5.0
53
	   *
54
	   * @var bool
55
	   */
56
	  protected $clear_destination = false;
57
	
58
	  /**
59
	   * References parent constructor and sets defaults for class.
60
	   *
61
	   * @since 2.2.0
62
	   *
63
	   * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
64
	   */
65
	  public function __construct( $skin = null ) {
66
	    // Get TGMPA class instance.
67
	    $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
68
	
69
	    parent::__construct( $skin );
70
	
71
	    if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
72
	      $this->clear_destination = true;
73
	    }
74
	
75
	    if ( $this->tgmpa->is_automatic ) {
76
	      $this->activate_strings();
77
	    }
78
	
79
	    add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
80
	  }
81
	
82
	  /**
83
	   * Sets the correct activation strings for the installer skin to use.
84
	   *
85
	   * @since 2.2.0
86
	   */
87
	  public function activate_strings() {
88
	    $this->strings['activation_failed']  = __( 'Plugin activation failed.', 'tgmpa' );
89
	    $this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' );
90
	  }
91
	
92
	  /**
93
	   * Performs the actual installation of each plugin.
94
	   *
95
	   * @since 2.2.0
96
	   *
97
	   * @see WP_Upgrader::run()
98
	   *
99
	   * @param array $options The installation config options.
100
	   * @return null|array Return early if error, array of installation data on success.
101
	   */
102
	  public function run( $options ) {
103
	    $result = parent::run( $options );
104
	
105
	    // Reset the strings in case we changed one during automatic activation.
106
	    if ( $this->tgmpa->is_automatic ) {
107
	      if ( 'update' === $this->skin->options['install_type'] ) {
108
	        $this->upgrade_strings();
109
	      } else {
110
	        $this->install_strings();
111
	      }
112
	    }
113
	
114
	    return $result;
115
	  }
116
	
117
	  /**
118
	   * Processes the bulk installation of plugins.
119
	   *
120
	   * @since 2.2.0
121
	   *
122
	   * {@internal This is basically a near identical copy of the WP Core
123
	   * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with
124
	   * new installs instead of upgrades.
125
	   * For ease of future synchronizations, the adjustments are clearly commented, but no other
126
	   * comments are added. Code style has been made to comply.}}
127
	   *
128
	   * @see Plugin_Upgrader::bulk_upgrade()
129
	   * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
130
	   * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}}
131
	   *
132
	   * @param array $plugins The plugin sources needed for installation.
133
	   * @param array $args    Arbitrary passed extra arguments.
134
	   * @return array|false   Install confirmation messages on success, false on failure.
135
	   */
136
	  public function bulk_install( $plugins, $args = array() ) {
137
	    // [TGMPA + ] Hook auto-activation in.
138
	    add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
139
	
140
	    $defaults    = array(
141
	      'clear_update_cache' => true,
142
	    );
143
	    $parsed_args = wp_parse_args( $args, $defaults );
144
	
145
	    $this->init();
146
	    $this->bulk = true;
147
	
148
	    $this->install_strings(); // [TGMPA + ] adjusted.
149
	
150
	    /* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
151
	
152
	    /* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
153
	
154
	    $this->skin->header();
155
	
156
	    // Connect to the Filesystem first.
157
	    $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
158
	    if ( ! $res ) {
159
	      $this->skin->footer();
160
	      return false;
161
	    }
162
	
163
	    $this->skin->bulk_header();
164
	
165
	    /*
166
	     * Only start maintenance mode if:
167
	     * - running Multisite and there are one or more plugins specified, OR
168
	     * - a plugin with an update available is currently active.
169
	     * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
170
	     */
171
	    $maintenance = ( is_multisite() && ! empty( $plugins ) );
172
	
173
	    /*
174
	    [TGMPA - ]
175
	    foreach ( $plugins as $plugin )
176
	      $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
177
	    */
178
	    if ( $maintenance ) {
179
	      $this->maintenance_mode( true );
180
	    }
181
	
182
	    $results = array();
183
	
184
	    $this->update_count   = count( $plugins );
185
	    $this->update_current = 0;
186
	    foreach ( $plugins as $plugin ) {
187
	      $this->update_current++;
188
	
189
	      /*
190
	      [TGMPA - ]
191
	      $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
192
	
193
	      if ( !isset( $current->response[ $plugin ] ) ) {
194
	        $this->skin->set_result('up_to_date');
195
	        $this->skin->before();
196
	        $this->skin->feedback('up_to_date');
197
	        $this->skin->after();
198
	        $results[$plugin] = true;
199
	        continue;
200
	      }
201
	
202
	      // Get the URL to the zip file.
203
	      $r = $current->response[ $plugin ];
204
	
205
	      $this->skin->plugin_active = is_plugin_active($plugin);
206
	      */
207
	
208
	      $result = $this->run(
209
	        array(
210
	          'package'           => $plugin, // [TGMPA + ] adjusted.
211
	          'destination'       => WP_PLUGIN_DIR,
212
	          'clear_destination' => false, // [TGMPA + ] adjusted.
213
	          'clear_working'     => true,
214
	          'is_multi'          => true,
215
	          'hook_extra'        => array(
216
	            'plugin' => $plugin,
217
	          ),
218
	        )
219
	      );
220
	
221
	      $results[ $plugin ] = $this->result;
222
	
223
	      // Prevent credentials auth screen from displaying multiple times.
224
	      if ( false === $result ) {
225
	        break;
226
	      }
227
	    }
228
	
229
	    $this->maintenance_mode( false );
230
	
231
	    /**
232
	     * Fires when the bulk upgrader process is complete.
233
	     *
234
	     * @since WP 3.6.0 / TGMPA 2.5.0
235
	     *
236
	     * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
237
	     *                              be a Theme_Upgrader or Core_Upgrade instance.
238
	     * @param array           $data {
239
	     *     Array of bulk item update data.
240
	     *
241
	     *     @type string $action   Type of action. Default 'update'.
242
	     *     @type string $type     Type of update process. Accepts 'plugin', 'theme', or 'core'.
243
	     *     @type bool   $bulk     Whether the update process is a bulk update. Default true.
244
	     *     @type array  $packages Array of plugin, theme, or core packages to update.
245
	     * }
246
	     */
247
	    do_action( // WPCS: prefix OK.
248
	      'upgrader_process_complete',
249
	      $this,
250
	      array(
251
	        'action'  => 'install', // [TGMPA + ] adjusted.
252
	        'type'    => 'plugin',
253
	        'bulk'    => true,
254
	        'plugins' => $plugins,
255
	      )
256
	    );
257
	
258
	    $this->skin->bulk_footer();
259
	
260
	    $this->skin->footer();
261
	
262
	    // Cleanup our hooks, in case something else does a upgrade on this connection.
263
	    /* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
264
	
265
	    // [TGMPA + ] Remove our auto-activation hook.
266
	    remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
267
	
268
	    // Force refresh of plugin update information.
269
	    wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
270
	
271
	    return $results;
272
	  }
273
	
274
	  /**
275
	   * Handle a bulk upgrade request.
276
	   *
277
	   * @since 2.5.0
278
	   *
279
	   * @see Plugin_Upgrader::bulk_upgrade()
280
	   *
281
	   * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
282
	   * @param array $args    Arbitrary passed extra arguments.
283
	   * @return string|bool Install confirmation messages on success, false on failure.
284
	   */
285
	  public function bulk_upgrade( $plugins, $args = array() ) {
286
	
287
	    add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
288
	
289
	    $result = parent::bulk_upgrade( $plugins, $args );
290
	
291
	    remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
292
	
293
	    return $result;
294
	  }
295
	
296
	  /**
297
	   * Abuse a filter to auto-activate plugins after installation.
298
	   *
299
	   * Hooked into the 'upgrader_post_install' filter hook.
300
	   *
301
	   * @since 2.5.0
302
	   *
303
	   * @param bool $bool The value we need to give back (true).
304
	   * @return bool
305
	   */
306
	  public function auto_activate( $bool ) {
0 ignored issues
show
Coding Style introduced by
function auto_activate() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
307
	    // Only process the activation of installed plugins if the automatic flag is set to true.
308
	    if ( $this->tgmpa->is_automatic ) {
309
	      // Flush plugins cache so the headers of the newly installed plugins will be read correctly.
310
	      wp_clean_plugins_cache();
311
	
312
	      // Get the installed plugin file.
313
	      $plugin_info = $this->plugin_info();
314
	
315
	      // Don't try to activate on upgrade of active plugin as WP will do this already.
316
	      if ( ! is_plugin_active( $plugin_info ) ) {
317
	        $activate = activate_plugin( $plugin_info );
318
	
319
	        // Adjust the success string based on the activation result.
320
	        $this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
321
	
322
	        if ( is_wp_error( $activate ) ) {
323
	          $this->skin->error( $activate );
324
	          $this->strings['process_success'] .= $this->strings['activation_failed'];
325
	        } else {
326
	          $this->strings['process_success'] .= $this->strings['activation_success'];
327
	        }
328
	      }
329
	    }
330
	
331
	    return $bool;
332
	  }
333
	}
334