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