PluginProbe
Chartify – WordPress Chart Plugin / 3.4.8
Chartify – WordPress Chart Plugin v3.4.8
3.8.0 3.7.9 3.7.8 3.7.7 3.7.6 3.7.5 trunk 1.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 All 83 releases
chart-builder / includes / admin / class-chart-builder-upgrader.php

class-chart-builder-upgrader.php in Chartify – WordPress Chart Plugin 3.4.8, at includes/admin/class-chart-builder-upgrader.php

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