PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.1
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Installers / PluginSilentUpgrader.php

PluginSilentUpgrader.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.1, at classes/Installers/PluginSilentUpgrader.php

614 lines 23.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PluginSilentUpgrader class.
4 *
5 * @package ContentControl
6 * @subpackage Installers
7 * @since 2.0.0
8 */
9
10 namespace ContentControl\Installers;
11
12 use WP_Error;
13 use WP_Upgrader;
14 use WP_Filesystem_Base;
15
16 /** \WP_Upgrader class */
17 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
18
19 /** \Plugin_Upgrader class */
20 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
21
22 /**
23 * In WP 5.3 a PHP 5.6 splat operator (...$args) was added to \WP_Upgrader_Skin::feedback().
24 * We need to remove all calls to *Skin::feedback() method, as we can't override it in own Skins
25 * without breaking support for PHP 5.3-5.5.
26 *
27 * @internal Please do not use this class outside of core plugin development. May be removed at any time.
28 *
29 * @since 2.0.0
30 */
31 class PluginSilentUpgrader extends \Plugin_Upgrader {
32
33 /**
34 * Run an upgrade/installation.
35 *
36 * Attempt to download the package (if it is not a local file), unpack it, and
37 * install it in the destination folder.
38 *
39 * @since 2.0.0
40 *
41 * @param array $options {
42 * Array or string of arguments for upgrading/installing a package.
43 *
44 * @type string $package The full path or URI of the package to install.
45 * Default empty.
46 * @type string $destination The full path to the destination folder.
47 * Default empty.
48 * @type bool $clear_destination Whether to delete any files already in the
49 * destination folder. Default false.
50 * @type bool $clear_working Whether to delete the files form the working
51 * directory after copying to the destination.
52 * Default false.
53 * @type bool $abort_if_destination_exists Whether to abort the installation if the destination
54 * folder already exists. When true, `$clear_destination`
55 * should be false. Default true.
56 * @type bool $is_multi Whether this run is one of multiple upgrade/installation
57 * actions being performed in bulk. When true, the skin
58 * WP_Upgrader::header() and WP_Upgrader::footer()
59 * aren't called. Default false.
60 * @type array $hook_extra Extra arguments to pass to the filter hooks called by
61 * WP_Upgrader::run().
62 * }
63 * @return array|false|WP_error The result from self::install_package() on success, otherwise a WP_Error,
64 * or false if unable to connect to the filesystem.
65 */
66 public function run( $options ) {
67 $defaults = [
68 'package' => '', // Please always pass this.
69 'destination' => '', // And this.
70 'clear_destination' => false,
71 'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please.
72 'clear_working' => true,
73 'is_multi' => false,
74 'hook_extra' => [], // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
75 ];
76
77 $options = wp_parse_args( $options, $defaults );
78
79 /**
80 * Filter the package options before running an update.
81 *
82 * See also {@see 'upgrader_process_complete'}.
83 *
84 * @since 4.3.0
85 *
86 * @param array $options {
87 * Options used by the upgrader.
88 *
89 * @type string $package Package for update.
90 * @type string $destination Update location.
91 * @type bool $clear_destination Clear the destination resource.
92 * @type bool $clear_working Clear the working resource.
93 * @type bool $abort_if_destination_exists Abort if the Destination directory exists.
94 * @type bool $is_multi Whether the upgrader is running multiple times.
95 * @type array $hook_extra {
96 * Extra hook arguments.
97 *
98 * @type string $action Type of action. Default 'update'.
99 * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'.
100 * @type bool $bulk Whether the update process is a bulk update. Default true.
101 * @type string $plugin Path to the plugin file relative to the plugins directory.
102 * @type string $theme The stylesheet or template name of the theme.
103 * @type string $language_update_type The language pack update type. Accepts 'plugin', 'theme',
104 * or 'core'.
105 * @type object $language_update The language pack update offer.
106 * }
107 * }
108 */
109 $options = apply_filters( 'upgrader_package_options', $options );
110
111 if ( ! $options['is_multi'] ) { // call $this->header separately if running multiple times.
112 $this->skin->header();
113 }
114
115 // Connect to the Filesystem first.
116 $res = $this->fs_connect( [ WP_CONTENT_DIR, $options['destination'] ] );
117 // Mainly for non-connected filesystem.
118 if ( ! $res ) {
119 if ( ! $options['is_multi'] ) {
120 $this->skin->footer();
121 }
122 return false;
123 }
124
125 $this->skin->before();
126
127 if ( is_wp_error( $res ) ) {
128 $this->skin->error( $res );
129 $this->skin->after();
130 if ( ! $options['is_multi'] ) {
131 $this->skin->footer();
132 }
133 return $res;
134 }
135
136 /*
137 * Download the package (Note, This just returns the filename
138 * of the file if the package is a local file)
139 */
140 $download = $this->download_package( $options['package'], true );
141
142 // Allow for signature soft-fail.
143 // WARNING: This may be removed in the future.
144 if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) {
145
146 // Don't output the 'no signature could be found' failure message for now.
147 if ( (string) $download->get_error_code() !== 'signature_verification_no_signature' || WP_DEBUG ) {
148 // Outout the failure error as a normal feedback, and not as an error:
149 // $this->skin->feedback( $download->get_error_message() );
150
151 // Report this failure back to WordPress.org for debugging purposes.
152 wp_version_check(
153 [
154 'signature_failure_code' => $download->get_error_code(),
155 'signature_failure_data' => $download->get_error_data(),
156 ]
157 );
158 }
159
160 // Pretend this error didn't happen.
161 $download = $download->get_error_data( 'softfail-filename' );
162 }
163
164 if ( is_wp_error( $download ) ) {
165 $this->skin->error( $download );
166 $this->skin->after();
167 if ( ! $options['is_multi'] ) {
168 $this->skin->footer();
169 }
170 return $download;
171 }
172
173 $delete_package = ( (string) $download !== (string) $options['package'] ); // Do not delete a "local" file.
174
175 // Unzips the file into a temporary directory.
176 $working_dir = $this->unpack_package( $download, $delete_package );
177 if ( is_wp_error( $working_dir ) ) {
178 $this->skin->error( $working_dir );
179 $this->skin->after();
180 if ( ! $options['is_multi'] ) {
181 $this->skin->footer();
182 }
183 return $working_dir;
184 }
185
186 // With the given options, this installs it to the destination directory.
187 $result = $this->install_package(
188 [
189 'source' => $working_dir,
190 'destination' => $options['destination'],
191 'clear_destination' => $options['clear_destination'],
192 'abort_if_destination_exists' => $options['abort_if_destination_exists'],
193 'clear_working' => $options['clear_working'],
194 'hook_extra' => $options['hook_extra'],
195 ]
196 );
197
198 $this->skin->set_result( $result );
199 if ( is_wp_error( $result ) ) {
200 $this->skin->error( $result );
201 // $this->skin->feedback( 'process_failed' );
202 } else {
203 // Installation succeeded.
204 // $this->skin->feedback( 'process_success' );
205 }
206
207 $this->skin->after();
208
209 if ( ! $options['is_multi'] ) {
210
211 /**
212 * Fire when the upgrader process is complete.
213 *
214 * See also {@see 'upgrader_package_options'}.
215 *
216 * @since 3.6.0
217 * @since 3.7.0 Added to WP_Upgrader::run().
218 * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
219 *
220 * @param WP_Upgrader $this WP_Upgrader instance. In other contexts, $this, might be a
221 * Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
222 * @param array $hook_extra {
223 * Array of bulk item update data.
224 *
225 * @type string $action Type of action. Default 'update'.
226 * @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
227 * @type bool $bulk Whether the update process is a bulk update. Default true.
228 * @type array $plugins Array of the basename paths of the plugins' main files.
229 * @type array $themes The theme slugs.
230 * @type array $translations {
231 * Array of translations update data.
232 *
233 * @type string $language The locale the translation is for.
234 * @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'.
235 * @type string $slug Text domain the translation is for. The slug of a theme/plugin or
236 * 'default' for core translations.
237 * @type string $version The version of a theme, plugin, or core.
238 * }
239 * }
240 */
241 do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
242
243 $this->skin->footer();
244 }
245
246 return $result;
247 }
248
249 /**
250 * Toggle maintenance mode for the site.
251 *
252 * Create/delete the maintenance file to enable/disable maintenance mode.
253 *
254 * @since 2.8.0
255 *
256 * @global WP_Filesystem_Base $wp_filesystem Subclass
257 *
258 * @param bool $enable True to enable maintenance mode, false to disable.
259 */
260 public function maintenance_mode( $enable = false ) {
261 global $wp_filesystem;
262 $file = $wp_filesystem->abspath() . '.maintenance';
263 if ( $enable ) {
264 // $this->skin->feedback( 'maintenance_start' );
265 // Create maintenance file to signal that we are upgrading
266 $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
267 $wp_filesystem->delete( $file );
268 $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE );
269 } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
270 // $this->skin->feedback( 'maintenance_end' );
271 $wp_filesystem->delete( $file );
272 }
273 }
274
275 /**
276 * Download a package.
277 *
278 * @since 2.8.0
279 * @since 5.5.0 Added the `$hook_extra` parameter.
280 *
281 * @param string $package The URI of the package. If this is the full path to an
282 * existing local file, it will be returned untouched.
283 * @param bool $check_signatures Whether to validate file signatures. Default false.
284 * @param array $hook_extra Extra arguments to pass to the filter hooks. Default empty array.
285 * @return string|WP_Error The full path to the downloaded package file, or a WP_Error object.
286 */
287 public function download_package( $package, $check_signatures = false, $hook_extra = [] ) {
288
289 /**
290 * Filters whether to return the package.
291 *
292 * @since 3.7.0
293 * @since 5.5.0 Added the `$hook_extra` parameter.
294 *
295 * @param bool $reply Whether to bail without returning the package.
296 * Default false.
297 * @param string $package The package file name.
298 * @param WP_Upgrader $this The WP_Upgrader instance.
299 * @param array $hook_extra Extra arguments passed to hooked filters.
300 */
301 $reply = apply_filters( 'upgrader_pre_download', false, $package, $this, $hook_extra );
302 if ( false !== $reply ) {
303 return $reply;
304 }
305
306 if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { // Local file or remote?
307 return $package; // Must be a local file.
308 }
309
310 if ( empty( $package ) ) {
311 return new WP_Error( 'no_package', $this->strings['no_package'] );
312 }
313
314 // $this->skin->feedback( 'downloading_package', $package );
315
316 $download_file = download_url( $package, 300, $check_signatures );
317
318 if ( is_wp_error( $download_file ) && ! $download_file->get_error_data( 'softfail-filename' ) ) {
319 return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() );
320 }
321
322 return $download_file;
323 }
324
325 /**
326 * Unpack a compressed package file.
327 *
328 * @since 2.8.0
329 *
330 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
331 *
332 * @param string $package Full path to the package file.
333 * @param bool $delete_package Optional. Whether to delete the package file after attempting
334 * to unpack it. Default true.
335 * @return string|WP_Error The path to the unpacked contents, or a WP_Error on failure.
336 */
337 public function unpack_package( $package, $delete_package = true ) {
338 global $wp_filesystem;
339
340 // $this->skin->feedback( 'unpack_package' );
341
342 $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
343
344 // Clean up contents of upgrade directory beforehand.
345 $upgrade_files = $wp_filesystem->dirlist( $upgrade_folder );
346 if ( ! empty( $upgrade_files ) ) {
347 foreach ( $upgrade_files as $file ) {
348 $wp_filesystem->delete( $upgrade_folder . $file['name'], true );
349 }
350 }
351
352 // We need a working directory - Strip off any .tmp or .zip suffixes
353 $working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
354
355 // Clean up working directory
356 if ( $wp_filesystem->is_dir( $working_dir ) ) {
357 $wp_filesystem->delete( $working_dir, true );
358 }
359
360 // Unzip package to working directory
361 $result = unzip_file( $package, $working_dir );
362
363 // Once extracted, delete the package if required.
364 if ( $delete_package ) {
365 unlink( $package );
366 }
367
368 if ( is_wp_error( $result ) ) {
369 $wp_filesystem->delete( $working_dir, true );
370 if ( $result->get_error_code() === 'incompatible_archive' ) {
371 return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
372 }
373
374 return $result;
375 }
376
377 return $working_dir;
378 }
379
380 /**
381 * Install a package.
382 *
383 * Copies the contents of a package form a source directory, and installs them in
384 * a destination directory. Optionally removes the source. It can also optionally
385 * clear out the destination folder if it already exists.
386 *
387 * @since 2.8.0
388 *
389 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
390 * @global array $wp_theme_directories
391 *
392 * @param array|string $args {
393 * Optional. Array or string of arguments for installing a package. Default empty array.
394 *
395 * @type string $source Required path to the package source. Default empty.
396 * @type string $destination Required path to a folder to install the package in.
397 * Default empty.
398 * @type bool $clear_destination Whether to delete any files already in the destination
399 * folder. Default false.
400 * @type bool $clear_working Whether to delete the files form the working directory
401 * after copying to the destination. Default false.
402 * @type bool $abort_if_destination_exists Whether to abort the installation if
403 * the destination folder already exists. Default true.
404 * @type array $hook_extra Extra arguments to pass to the filter hooks called by
405 * WP_Upgrader::install_package(). Default empty array.
406 * }
407 *
408 * @return array|WP_Error The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure.
409 */
410 public function install_package( $args = [] ) {
411 global $wp_filesystem, $wp_theme_directories;
412
413 $defaults = [
414 'source' => '', // Please always pass this
415 'destination' => '', // and this
416 'clear_destination' => false,
417 'clear_working' => false,
418 'abort_if_destination_exists' => true,
419 'hook_extra' => [],
420 ];
421
422 $args = wp_parse_args( $args, $defaults );
423
424 // These were previously extract()'d.
425 $source = $args['source'];
426 $destination = $args['destination'];
427 $clear_destination = $args['clear_destination'];
428
429 // @todo does this need to be replaced?
430 // wpforms_set_time_limit( 300 );
431
432 if ( empty( $source ) || empty( $destination ) ) {
433 return new WP_Error( 'bad_request', $this->strings['bad_request'] );
434 }
435 // $this->skin->feedback( 'installing_package' );
436
437 /**
438 * Filter the install response before the installation has started.
439 *
440 * Returning a truthy value, or one that could be evaluated as a WP_Error
441 * will effectively short-circuit the installation, returning that value
442 * instead.
443 *
444 * @since 2.8.0
445 *
446 * @param bool|WP_Error $response Response.
447 * @param array $hook_extra Extra arguments passed to hooked filters.
448 */
449 $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
450
451 if ( is_wp_error( $res ) ) {
452 return $res;
453 }
454
455 // Retain the Original source and destinations.
456 $remote_source = $args['source'];
457 $local_destination = $destination;
458
459 $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
460 $remote_destination = $wp_filesystem->find_folder( $local_destination );
461 $count_source_files = count( $source_files );
462
463 // Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
464 if ( $count_source_files === 1 && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) { // Only one folder? Then we want its contents.
465 $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
466 } elseif ( $count_source_files === 0 ) {
467 return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
468 } else { // It's only a single file, the upgrader will use the folder name of this file as the destination folder. Folder name is based on zip filename.
469 $source = trailingslashit( $args['source'] );
470 }
471
472 /**
473 * Filter the source file location for the upgrade package.
474 *
475 * @since 2.8.0
476 * @since 4.4.0 The $hook_extra parameter became available.
477 *
478 * @param string $source File source location.
479 * @param string $remote_source Remote file source location.
480 * @param WP_Upgrader $this WP_Upgrader instance.
481 * @param array $hook_extra Extra arguments passed to hooked filters.
482 */
483 $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );
484
485 if ( is_wp_error( $source ) ) {
486 return $source;
487 }
488
489 // Has the source location changed? If so, we need a new source_files list.
490 if ( $source !== $remote_source ) {
491 $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
492 }
493
494 /*
495 * Protection against deleting files in any important base directories.
496 * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
497 * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
498 * to copy the directory into the directory, whilst they pass the source
499 * as the actual files to copy.
500 */
501 $protected_directories = [ ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' ];
502
503 if ( is_array( $wp_theme_directories ) ) {
504 $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
505 }
506
507 if ( in_array( $destination, $protected_directories ) ) {
508 $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
509 $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
510 }
511
512 if ( $clear_destination ) {
513 // We're going to clear the destination if there's something there.
514 $removed = $this->clear_destination( $remote_destination );
515
516 /**
517 * Filter whether the upgrader cleared the destination.
518 *
519 * @since 2.8.0
520 *
521 * @param mixed $removed Whether the destination was cleared. true on success, WP_Error on failure
522 * @param string $local_destination The local package destination.
523 * @param string $remote_destination The remote package destination.
524 * @param array $hook_extra Extra arguments passed to hooked filters.
525 */
526 $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
527
528 if ( is_wp_error( $removed ) ) {
529 return $removed;
530 }
531 } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination ) ) {
532 // If we're not clearing the destination folder and something exists there already, Bail.
533 // But first check to see if there are actually any files in the folder.
534 $_files = $wp_filesystem->dirlist( $remote_destination );
535
536 if ( ! empty( $_files ) ) {
537 $wp_filesystem->delete( $remote_source, true ); // Clear out the source files.
538
539 return new WP_Error( 'folder_exists', $this->strings['folder_exists'], $remote_destination );
540 }
541 }
542
543 // Create destination if needed.
544 if ( ! $wp_filesystem->exists( $remote_destination ) ) {
545 if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
546 return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
547 }
548 }
549
550 // Copy new version of item into place.
551 $result = copy_dir( $source, $remote_destination );
552
553 if ( is_wp_error( $result ) ) {
554 if ( $args['clear_working'] ) {
555 $wp_filesystem->delete( $remote_source, true );
556 }
557
558 return $result;
559 }
560
561 // Clear the Working folder?
562 if ( $args['clear_working'] ) {
563 $wp_filesystem->delete( $remote_source, true );
564 }
565
566 $destination_name = basename( str_replace( $local_destination, '', $destination ) );
567
568 if ( $destination_name === '.' ) {
569 $destination_name = '';
570 }
571
572 $this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
573
574 /**
575 * Filter the installation response after the installation has finished.
576 *
577 * @since 2.8.0
578 *
579 * @param bool $response Installation response.
580 * @param array $hook_extra Extra arguments passed to hooked filters.
581 * @param array $result Installation result data.
582 */
583 $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
584
585 if ( is_wp_error( $res ) ) {
586 $this->result = $res;
587
588 return $res;
589 }
590
591 // Bombard the calling function will all the info which we've just used.
592 return $this->result;
593 }
594
595 /**
596 * Install a plugin package.
597 *
598 * @since 1.6.3
599 *
600 * @param string $package The full local path or URI of the package.
601 * @param array $args Optional. Other arguments for installing a plugin package. Default empty array.
602 *
603 * @return bool|\WP_Error True if the installation was successful, false or a WP_Error otherwise.
604 */
605 public function install( $package, $args = [] ) {
606 $result = parent::install( $package, $args );
607 if ( true === $result ) {
608 do_action( 'content_control_plugin_installed', $package );
609 }
610
611 return $result;
612 }
613 }
614