Changeset 52351 for trunk/src/wp-admin/includes/class-wp-upgrader.php
- Timestamp:
- 12/10/2021 12:04:03 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-wp-upgrader.php
r52337 r52351 134 134 * and also add the generic strings to `WP_Upgrader::$strings`. 135 135 * 136 * Additionally, it will schedule a weekly task to clean up the temp-backup directory. 137 * 138 * @since 2.8.0 139 * @since 5.9.0 Added the `schedule_temp_backup_cleanup()` task. 136 * @since 2.8.0 140 137 */ 141 138 public function init() { 142 139 $this->skin->set_upgrader( $this ); 143 140 $this->generic_strings(); 144 145 if ( ! wp_installing() ) {146 $this->schedule_temp_backup_cleanup();147 }148 }149 150 /**151 * Schedule cleanup of the temp-backup directory.152 *153 * @since 5.9.0154 */155 protected function schedule_temp_backup_cleanup() {156 if ( false === wp_next_scheduled( 'wp_delete_temp_updater_backups' ) ) {157 wp_schedule_event( time(), 'weekly', 'wp_delete_temp_updater_backups' );158 }159 141 } 160 142 … … 185 167 $this->strings['maintenance_start'] = __( 'Enabling Maintenance mode…' ); 186 168 $this->strings['maintenance_end'] = __( 'Disabling Maintenance mode…' ); 187 188 /* translators: %s: temp-backup */189 $this->strings['temp_backup_mkdir_failed'] = sprintf( __( 'Could not create the %s directory.' ), 'temp-backup' );190 /* translators: %s: temp-backup */191 $this->strings['temp_backup_move_failed'] = sprintf( __( 'Could not move old version to the %s directory.' ), 'temp-backup' );192 $this->strings['temp_backup_restore_failed'] = __( 'Could not restore original version.' );193 194 169 } 195 170 … … 339 314 if ( ! empty( $upgrade_files ) ) { 340 315 foreach ( $upgrade_files as $file ) { 341 if ( 'temp-backup' === $file['name'] ) {342 continue;343 }344 316 $wp_filesystem->delete( $upgrade_folder . $file['name'], true ); 345 317 } … … 522 494 } 523 495 524 if ( ! empty( $args['hook_extra']['temp_backup'] ) ) {525 $temp_backup = $this->move_to_temp_backup_dir( $args['hook_extra']['temp_backup'] );526 if ( is_wp_error( $temp_backup ) ) {527 return $temp_backup;528 }529 }530 531 496 // Retain the original source and destinations. 532 497 $remote_source = $args['source']; … … 628 593 } 629 594 630 // Movenew version of item into place.631 $result = move_dir( $source, $remote_destination, $remote_source);595 // Copy new version of item into place. 596 $result = copy_dir( $source, $remote_destination ); 632 597 if ( is_wp_error( $result ) ) { 633 598 if ( $args['clear_working'] ) { … … 637 602 } 638 603 639 // Clear the working directory?604 // Clear the working folder? 640 605 if ( $args['clear_working'] ) { 641 606 $wp_filesystem->delete( $remote_source, true ); … … 847 812 $this->skin->set_result( $result ); 848 813 if ( is_wp_error( $result ) ) { 849 if ( ! empty( $options['hook_extra']['temp_backup'] ) ) {850 /*851 * Restore the backup on shutdown.852 * Actions running on `shutdown` are immune to PHP timeouts,853 * so in case the failure was due to a PHP timeout,854 * we'll still be able to properly restore the previous version.855 */856 add_action(857 'shutdown',858 function() use ( $options ) {859 $this->restore_temp_backup( $options['hook_extra']['temp_backup'] );860 }861 );862 }863 814 $this->skin->error( $result ); 864 815 … … 872 823 873 824 $this->skin->after(); 874 875 // Clean up the backup kept in the temp-backup directory.876 if ( ! empty( $options['hook_extra']['temp_backup'] ) ) {877 // Delete the backup on `shutdown` to avoid a PHP timeout.878 add_action(879 'shutdown',880 function() use ( $options ) {881 $this->delete_temp_backup( $options['hook_extra']['temp_backup'] );882 }883 );884 }885 825 886 826 if ( ! $options['is_multi'] ) { … … 1008 948 return delete_option( $lock_name . '.lock' ); 1009 949 } 1010 1011 /**1012 * Moves the plugin/theme being updated into a temp-backup directory.1013 *1014 * @since 5.9.01015 *1016 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.1017 *1018 * @param array $args Array of data for the temp-backup. Must include a slug, the source, and directory.1019 * @return bool|WP_Error1020 */1021 public function move_to_temp_backup_dir( $args ) {1022 global $wp_filesystem;1023 1024 if ( empty( $args['slug'] ) || empty( $args['src'] ) || empty( $args['dir'] ) ) {1025 return false;1026 }1027 1028 /**1029 * Skip any plugin that has "." as its slug.1030 * A slug of "." will result in a `$src` value ending in a period.1031 *1032 * On Windows, this will cause the 'plugins' folder to be moved,1033 * and will cause a failure when attempting to call `mkdir()`.1034 */1035 if ( '.' === $args['slug'] ) {1036 return false;1037 }1038 1039 $dest_dir = $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/';1040 // Create the temp-backup directory if it doesn't exist.1041 if ( (1042 ! $wp_filesystem->is_dir( $dest_dir )1043 && ! $wp_filesystem->mkdir( $dest_dir )1044 ) || (1045 ! $wp_filesystem->is_dir( $dest_dir . $args['dir'] . '/' )1046 && ! $wp_filesystem->mkdir( $dest_dir . $args['dir'] . '/' )1047 )1048 ) {1049 return new WP_Error( 'fs_temp_backup_mkdir', $this->strings['temp_backup_mkdir_failed'] );1050 }1051 1052 $src = trailingslashit( $args['src'] ) . $args['slug'];1053 $dest = $dest_dir . $args['dir'] . '/' . $args['slug'];1054 1055 // Delete the temp-backup directory if it already exists.1056 if ( $wp_filesystem->is_dir( $dest ) ) {1057 $wp_filesystem->delete( $dest, true );1058 }1059 1060 // Move to the temp-backup directory.1061 if ( ! move_dir( $src, $dest ) ) {1062 return new WP_Error( 'fs_temp_backup_move', $this->strings['temp_backup_move_failed'] );1063 }1064 1065 return true;1066 }1067 1068 /**1069 * Restores the plugin/theme from the temp-backup directory.1070 *1071 * @since 5.9.01072 *1073 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.1074 *1075 * @param array $args Array of data for the temp-backup. Must include a slug, the source, and directory.1076 * @return bool|WP_Error1077 */1078 public function restore_temp_backup( $args ) {1079 global $wp_filesystem;1080 1081 if ( empty( $args['slug'] ) || empty( $args['src'] ) || empty( $args['dir'] ) ) {1082 return false;1083 }1084 1085 $src = $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/' . $args['dir'] . '/' . $args['slug'];1086 $dest = trailingslashit( $args['src'] ) . $args['slug'];1087 1088 if ( $wp_filesystem->is_dir( $src ) ) {1089 // Cleanup.1090 if ( $wp_filesystem->is_dir( $dest ) && ! $wp_filesystem->delete( $dest, true ) ) {1091 return new WP_Error( 'fs_temp_backup_delete', $this->strings['temp_backup_restore_failed'] );1092 }1093 1094 // Move it.1095 if ( ! move_dir( $src, $dest ) ) {1096 return new WP_Error( 'fs_temp_backup_delete', $this->strings['temp_backup_restore_failed'] );1097 }1098 }1099 1100 return true;1101 }1102 1103 /**1104 * Deletes a temp-backup.1105 *1106 * @since 5.9.01107 *1108 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.1109 *1110 * @param array $args Array of data for the temp-backup. Must include a slug, the source, and directory.1111 * @return bool1112 */1113 public function delete_temp_backup( $args ) {1114 global $wp_filesystem;1115 1116 if ( empty( $args['slug'] ) || empty( $args['dir'] ) ) {1117 return false;1118 }1119 1120 return $wp_filesystem->delete(1121 $wp_filesystem->wp_content_dir() . "upgrade/temp-backup/{$args['dir']}/{$args['slug']}",1122 true1123 );1124 }1125 950 } 1126 951
Note: See TracChangeset
for help on using the changeset viewer.