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