Total Complexity | 57 |
Total Lines | 551 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Plugin_Upgrader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_Plugin_Upgrader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class MonsterInsights_Plugin_Upgrader extends Plugin_Upgrader { |
||
20 | |||
21 | /** |
||
22 | * Run an upgrade/installation. |
||
23 | * |
||
24 | * Attempt to download the package (if it is not a local file), unpack it, and |
||
25 | * install it in the destination folder. |
||
26 | * |
||
27 | * @since 1.5.6.1 |
||
28 | * |
||
29 | * @param array $options { |
||
30 | * Array or string of arguments for upgrading/installing a package. |
||
31 | * |
||
32 | * @type string $package The full path or URI of the package to install. |
||
33 | * Default empty. |
||
34 | * @type string $destination The full path to the destination folder. |
||
35 | * Default empty. |
||
36 | * @type bool $clear_destination Whether to delete any files already in the |
||
37 | * destination folder. Default false. |
||
38 | * @type bool $clear_working Whether to delete the files form the working |
||
39 | * directory after copying to the destination. |
||
40 | * Default false. |
||
41 | * @type bool $abort_if_destination_exists Whether to abort the installation if the destination |
||
42 | * folder already exists. When true, `$clear_destination` |
||
43 | * should be false. Default true. |
||
44 | * @type bool $is_multi Whether this run is one of multiple upgrade/installation |
||
45 | * actions being performed in bulk. When true, the skin |
||
46 | * WP_Upgrader::header() and WP_Upgrader::footer() |
||
47 | * aren't called. Default false. |
||
48 | * @type array $hook_extra Extra arguments to pass to the filter hooks called by |
||
49 | * WP_Upgrader::run(). |
||
50 | * } |
||
51 | * @return array|false|WP_error The result from self::install_package() on success, otherwise a WP_Error, |
||
52 | * or false if unable to connect to the filesystem. |
||
53 | */ |
||
54 | public function run( $options ) { |
||
55 | |||
56 | $defaults = array( |
||
57 | 'package' => '', // Please always pass this. |
||
58 | 'destination' => '', // And this |
||
59 | 'clear_destination' => false, |
||
60 | 'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please |
||
61 | 'clear_working' => true, |
||
62 | 'is_multi' => false, |
||
63 | 'hook_extra' => array(), // Pass any extra $hook_extra args here, this will be passed to any hooked filters. |
||
64 | ); |
||
65 | |||
66 | $options = wp_parse_args( $options, $defaults ); |
||
67 | |||
68 | /** |
||
69 | * Filter the package options before running an update. |
||
70 | * |
||
71 | * See also {@see 'upgrader_process_complete'}. |
||
72 | * |
||
73 | * @since 4.3.0 |
||
74 | * |
||
75 | * @param array $options { |
||
76 | * Options used by the upgrader. |
||
77 | * |
||
78 | * @type string $package Package for update. |
||
79 | * @type string $destination Update location. |
||
80 | * @type bool $clear_destination Clear the destination resource. |
||
81 | * @type bool $clear_working Clear the working resource. |
||
82 | * @type bool $abort_if_destination_exists Abort if the Destination directory exists. |
||
83 | * @type bool $is_multi Whether the upgrader is running multiple times. |
||
84 | * @type array $hook_extra { |
||
85 | * Extra hook arguments. |
||
86 | * |
||
87 | * @type string $action Type of action. Default 'update'. |
||
88 | * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'. |
||
89 | * @type bool $bulk Whether the update process is a bulk update. Default true. |
||
90 | * @type string $plugin Path to the plugin file relative to the plugins directory. |
||
91 | * @type string $theme The stylesheet or template name of the theme. |
||
92 | * @type string $language_update_type The language pack update type. Accepts 'plugin', 'theme', |
||
93 | * or 'core'. |
||
94 | * @type object $language_update The language pack update offer. |
||
95 | * } |
||
96 | * } |
||
97 | */ |
||
98 | $options = apply_filters( 'upgrader_package_options', $options ); |
||
99 | |||
100 | if ( ! $options['is_multi'] ) { // call $this->header separately if running multiple times |
||
101 | $this->skin->header(); |
||
102 | } |
||
103 | |||
104 | // Connect to the Filesystem first. |
||
105 | $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) ); |
||
106 | // Mainly for non-connected filesystem. |
||
107 | if ( ! $res ) { |
||
108 | if ( ! $options['is_multi'] ) { |
||
109 | $this->skin->footer(); |
||
110 | } |
||
111 | return false; |
||
112 | } |
||
113 | |||
114 | $this->skin->before(); |
||
115 | |||
116 | if ( is_wp_error( $res ) ) { |
||
117 | $this->skin->error( $res ); |
||
|
|||
118 | $this->skin->after(); |
||
119 | if ( ! $options['is_multi'] ) { |
||
120 | $this->skin->footer(); |
||
121 | } |
||
122 | return $res; |
||
123 | } |
||
124 | |||
125 | /* |
||
126 | * Download the package (Note, This just returns the filename |
||
127 | * of the file if the package is a local file) |
||
128 | */ |
||
129 | $download = $this->download_package( $options['package'], true ); |
||
130 | |||
131 | // Allow for signature soft-fail. |
||
132 | // WARNING: This may be removed in the future. |
||
133 | if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) { |
||
134 | |||
135 | // Don't output the 'no signature could be found' failure message for now. |
||
136 | if ( 'signature_verification_no_signature' != $download->get_error_code() || WP_DEBUG ) { |
||
137 | // Outout the failure error as a normal feedback, and not as an error: |
||
138 | //$this->skin->feedback( $download->get_error_message() ); |
||
139 | |||
140 | // Report this failure back to WordPress.org for debugging purposes. |
||
141 | wp_version_check( |
||
142 | array( |
||
143 | 'signature_failure_code' => $download->get_error_code(), |
||
144 | 'signature_failure_data' => $download->get_error_data(), |
||
145 | ) |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | // Pretend this error didn't happen. |
||
150 | $download = $download->get_error_data( 'softfail-filename' ); |
||
151 | } |
||
152 | |||
153 | if ( is_wp_error( $download ) ) { |
||
154 | $this->skin->error( $download ); |
||
155 | $this->skin->after(); |
||
156 | if ( ! $options['is_multi'] ) { |
||
157 | $this->skin->footer(); |
||
158 | } |
||
159 | return $download; |
||
160 | } |
||
161 | |||
162 | $delete_package = ( $download != $options['package'] ); // Do not delete a "local" file |
||
163 | |||
164 | // Unzips the file into a temporary directory. |
||
165 | $working_dir = $this->unpack_package( $download, $delete_package ); |
||
166 | if ( is_wp_error( $working_dir ) ) { |
||
167 | $this->skin->error( $working_dir ); |
||
168 | $this->skin->after(); |
||
169 | if ( ! $options['is_multi'] ) { |
||
170 | $this->skin->footer(); |
||
171 | } |
||
172 | return $working_dir; |
||
173 | } |
||
174 | |||
175 | // With the given options, this installs it to the destination directory. |
||
176 | $result = $this->install_package( |
||
177 | array( |
||
178 | 'source' => $working_dir, |
||
179 | 'destination' => $options['destination'], |
||
180 | 'clear_destination' => $options['clear_destination'], |
||
181 | 'abort_if_destination_exists' => $options['abort_if_destination_exists'], |
||
182 | 'clear_working' => $options['clear_working'], |
||
183 | 'hook_extra' => $options['hook_extra'], |
||
184 | ) |
||
185 | ); |
||
186 | |||
187 | $this->skin->set_result( $result ); |
||
188 | if ( is_wp_error( $result ) ) { |
||
189 | $this->skin->error( $result ); |
||
190 | //$this->skin->feedback( 'process_failed' ); |
||
191 | } else { |
||
192 | // Installation succeeded. |
||
193 | //$this->skin->feedback( 'process_success' ); |
||
194 | } |
||
195 | |||
196 | $this->skin->after(); |
||
197 | |||
198 | if ( ! $options['is_multi'] ) { |
||
199 | |||
200 | /** |
||
201 | * Fire when the upgrader process is complete. |
||
202 | * |
||
203 | * See also {@see 'upgrader_package_options'}. |
||
204 | * |
||
205 | * @since 3.6.0 |
||
206 | * @since 3.7.0 Added to WP_Upgrader::run(). |
||
207 | * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`. |
||
208 | * |
||
209 | * @param WP_Upgrader $this WP_Upgrader instance. In other contexts, $this, might be a |
||
210 | * Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance. |
||
211 | * @param array $hook_extra { |
||
212 | * Array of bulk item update data. |
||
213 | * |
||
214 | * @type string $action Type of action. Default 'update'. |
||
215 | * @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'. |
||
216 | * @type bool $bulk Whether the update process is a bulk update. Default true. |
||
217 | * @type array $plugins Array of the basename paths of the plugins' main files. |
||
218 | * @type array $themes The theme slugs. |
||
219 | * @type array $translations { |
||
220 | * Array of translations update data. |
||
221 | * |
||
222 | * @type string $language The locale the translation is for. |
||
223 | * @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'. |
||
224 | * @type string $slug Text domain the translation is for. The slug of a theme/plugin or |
||
225 | * 'default' for core translations. |
||
226 | * @type string $version The version of a theme, plugin, or core. |
||
227 | * } |
||
228 | * } |
||
229 | */ |
||
230 | do_action( 'upgrader_process_complete', $this, $options['hook_extra'] ); |
||
231 | |||
232 | $this->skin->footer(); |
||
233 | } |
||
234 | |||
235 | return $result; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Toggle maintenance mode for the site. |
||
240 | * |
||
241 | * Create/delete the maintenance file to enable/disable maintenance mode. |
||
242 | * |
||
243 | * @since 2.8.0 |
||
244 | * |
||
245 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
||
246 | * |
||
247 | * @param bool $enable True to enable maintenance mode, false to disable. |
||
248 | */ |
||
249 | public function maintenance_mode( $enable = false ) { |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Download a package. |
||
266 | * |
||
267 | * @since 2.8.0 |
||
268 | * |
||
269 | * @param string $package The URI of the package. If this is the full path to an |
||
270 | * existing local file, it will be returned untouched. |
||
271 | * @param bool $check_signatures Whether to validate file signatures. Default false. |
||
272 | * @return string|WP_Error The full path to the downloaded package file, or a WP_Error object. |
||
273 | */ |
||
274 | public function download_package( $package, $check_signatures = false ) { |
||
275 | |||
276 | /** |
||
277 | * Filter whether to return the package. |
||
278 | * |
||
279 | * @since 3.7.0 |
||
280 | * |
||
281 | * @param bool $reply Whether to bail without returning the package. |
||
282 | * Default false. |
||
283 | * @param string $package The package file name. |
||
284 | * @param WP_Upgrader $this The WP_Upgrader instance. |
||
285 | */ |
||
286 | $reply = apply_filters( 'upgrader_pre_download', false, $package, $this ); |
||
287 | if ( false !== $reply ) { |
||
288 | return $reply; |
||
289 | } |
||
290 | |||
291 | if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { //Local file or remote? |
||
292 | return $package; //must be a local file.. |
||
293 | } |
||
294 | |||
295 | if ( empty( $package ) ) { |
||
296 | return new WP_Error( 'no_package', $this->strings['no_package'] ); |
||
297 | } |
||
298 | |||
299 | //$this->skin->feedback( 'downloading_package', $package ); |
||
300 | |||
301 | $download_file = download_url( $package, 300, $check_signatures ); |
||
302 | |||
303 | if ( is_wp_error( $download_file ) && ! $download_file->get_error_data( 'softfail-filename' ) ) { |
||
304 | return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() ); |
||
305 | } |
||
306 | |||
307 | return $download_file; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Unpack a compressed package file. |
||
312 | * |
||
313 | * @since 2.8.0 |
||
314 | * |
||
315 | * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
||
316 | * |
||
317 | * @param string $package Full path to the package file. |
||
318 | * @param bool $delete_package Optional. Whether to delete the package file after attempting |
||
319 | * to unpack it. Default true. |
||
320 | * @return string|WP_Error The path to the unpacked contents, or a WP_Error on failure. |
||
321 | */ |
||
322 | public function unpack_package( $package, $delete_package = true ) { |
||
323 | global $wp_filesystem; |
||
324 | |||
325 | //$this->skin->feedback( 'unpack_package' ); |
||
326 | |||
327 | $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/'; |
||
328 | |||
329 | //Clean up contents of upgrade directory beforehand. |
||
330 | $upgrade_files = $wp_filesystem->dirlist( $upgrade_folder ); |
||
331 | if ( ! empty( $upgrade_files ) ) { |
||
332 | foreach ( $upgrade_files as $file ) { |
||
333 | $wp_filesystem->delete( $upgrade_folder . $file['name'], true ); |
||
334 | } |
||
335 | } |
||
336 | |||
337 | // We need a working directory - Strip off any .tmp or .zip suffixes |
||
338 | $working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' ); |
||
339 | |||
340 | // Clean up working directory |
||
341 | if ( $wp_filesystem->is_dir( $working_dir ) ) { |
||
342 | $wp_filesystem->delete( $working_dir, true ); |
||
343 | } |
||
344 | |||
345 | // Unzip package to working directory |
||
346 | $result = unzip_file( $package, $working_dir ); |
||
347 | |||
348 | // Once extracted, delete the package if required. |
||
349 | if ( $delete_package ) { |
||
350 | unlink( $package ); |
||
351 | } |
||
352 | |||
353 | if ( is_wp_error( $result ) ) { |
||
354 | $wp_filesystem->delete( $working_dir, true ); |
||
355 | if ( 'incompatible_archive' == $result->get_error_code() ) { |
||
356 | return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() ); |
||
357 | } |
||
358 | return $result; |
||
359 | } |
||
360 | |||
361 | return $working_dir; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Install a package. |
||
366 | * |
||
367 | * Copies the contents of a package form a source directory, and installs them in |
||
368 | * a destination directory. Optionally removes the source. It can also optionally |
||
369 | * clear out the destination folder if it already exists. |
||
370 | * |
||
371 | * @since 2.8.0 |
||
372 | * |
||
373 | * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
||
374 | * @global array $wp_theme_directories |
||
375 | * |
||
376 | * @param array|string $args { |
||
377 | * Optional. Array or string of arguments for installing a package. Default empty array. |
||
378 | * |
||
379 | * @type string $source Required path to the package source. Default empty. |
||
380 | * @type string $destination Required path to a folder to install the package in. |
||
381 | * Default empty. |
||
382 | * @type bool $clear_destination Whether to delete any files already in the destination |
||
383 | * folder. Default false. |
||
384 | * @type bool $clear_working Whether to delete the files form the working directory |
||
385 | * after copying to the destination. Default false. |
||
386 | * @type bool $abort_if_destination_exists Whether to abort the installation if |
||
387 | * the destination folder already exists. Default true. |
||
388 | * @type array $hook_extra Extra arguments to pass to the filter hooks called by |
||
389 | * WP_Upgrader::install_package(). Default empty array. |
||
390 | * } |
||
391 | * |
||
392 | * @return array|WP_Error The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure. |
||
393 | */ |
||
394 | public function install_package( $args = array() ) { |
||
570 | } |
||
571 | } |
||
572 |