Changeset 51815
- Timestamp:
- 09/15/2021 06:39:09 PM (3 years ago)
- Location:
- trunk/src/wp-admin/includes
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-plugin-upgrader.php
r51528 r51815 227 227 'clear_working' => true, 228 228 'hook_extra' => array( 229 'plugin' => $plugin, 230 'type' => 'plugin', 231 'action' => 'update', 229 'plugin' => $plugin, 230 'type' => 'plugin', 231 'action' => 'update', 232 'temp_backup' => array( 233 'slug' => dirname( $plugin ), 234 'src' => WP_PLUGIN_DIR, 235 'dir' => 'plugins', 236 ), 232 237 ), 233 238 ) … … 343 348 'is_multi' => true, 344 349 'hook_extra' => array( 345 'plugin' => $plugin, 350 'plugin' => $plugin, 351 'temp_backup' => array( 352 'slug' => dirname( $plugin ), 353 'src' => WP_PLUGIN_DIR, 354 'dir' => 'plugins', 355 ), 346 356 ), 347 357 ) -
trunk/src/wp-admin/includes/class-theme-upgrader.php
r51528 r51815 329 329 'clear_working' => true, 330 330 'hook_extra' => array( 331 'theme' => $theme, 332 'type' => 'theme', 333 'action' => 'update', 331 'theme' => $theme, 332 'type' => 'theme', 333 'action' => 'update', 334 'temp_backup' => array( 335 'slug' => $theme, 336 'src' => get_theme_root( $theme ), 337 'dir' => 'themes', 338 ), 334 339 ), 335 340 ) … … 444 449 'is_multi' => true, 445 450 'hook_extra' => array( 446 'theme' => $theme, 451 'theme' => $theme, 452 'temp_backup' => array( 453 'slug' => $theme, 454 'src' => get_theme_root( $theme ), 455 'dir' => 'themes', 456 ), 447 457 ), 448 458 ) -
trunk/src/wp-admin/includes/class-wp-site-health.php
r51805 r51815 1884 1884 1885 1885 /** 1886 * Test available disk space for updates. 1887 * 1888 * @since 5.9.0 1889 * 1890 * @return array The test results. 1891 */ 1892 public function get_test_available_updates_disk_space() { 1893 $available_space = function_exists( 'disk_free_space' ) ? (int) @disk_free_space( WP_CONTENT_DIR . '/upgrade/' ) : false; 1894 $available_space_in_mb = $available_space / MB_IN_BYTES; 1895 1896 $result = array( 1897 'label' => __( 'Disk space available to safely perform updates' ), 1898 'status' => 'good', 1899 'badge' => array( 1900 'label' => __( 'Security' ), 1901 'color' => 'blue', 1902 ), 1903 'description' => sprintf( 1904 /* translators: %s: Available disk space in MB or GB. */ 1905 '<p>' . __( '%s available disk space was detected, update routines can be performed safely.' ), 1906 size_format( $available_space ) 1907 ), 1908 'actions' => '', 1909 'test' => 'available_updates_disk_space', 1910 ); 1911 1912 if ( $available_space_in_mb < 100 ) { 1913 $result['description'] = __( 'Available disk space is low, less than 100 MB available.' ); 1914 $result['status'] = 'recommended'; 1915 } 1916 1917 if ( $available_space_in_mb < 20 ) { 1918 $result['description'] = __( 'Available disk space is critically low, less than 20 MB available. Proceed with caution, updates may fail.' ); 1919 $result['status'] = 'critical'; 1920 } 1921 1922 if ( ! $available_space ) { 1923 $result['description'] = __( 'Could not determine available disk space for updates.' ); 1924 $result['status'] = 'recommended'; 1925 } 1926 1927 return $result; 1928 } 1929 1930 /** 1931 * Test if plugin and theme updates temp-backup directories are writable or can be created. 1932 * 1933 * @since 5.9.0 1934 * 1935 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1936 * 1937 * @return array The test results. 1938 */ 1939 public function get_test_update_temp_backup_writable() { 1940 global $wp_filesystem; 1941 1942 $result = array( 1943 'label' => sprintf( 1944 /* translators: %s: temp-backup */ 1945 __( 'Plugin and theme update %s directory is writable' ), 1946 'temp-backup' 1947 ), 1948 'status' => 'good', 1949 'badge' => array( 1950 'label' => __( 'Security' ), 1951 'color' => 'blue', 1952 ), 1953 'description' => sprintf( 1954 /* translators: %s: wp-content/upgrade/temp-backup */ 1955 '<p>' . __( 'The %s directory used to improve the stability of plugin and theme updates is writable.' ), 1956 '<code>wp-content/upgrade/temp-backup</code>' 1957 ), 1958 'actions' => '', 1959 'test' => 'update_temp_backup_writable', 1960 ); 1961 1962 if ( ! $wp_filesystem ) { 1963 if ( ! function_exists( 'WP_Filesystem' ) ) { 1964 require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' ); 1965 } 1966 WP_Filesystem(); 1967 } 1968 $wp_content = $wp_filesystem->wp_content_dir(); 1969 1970 $upgrade_dir_exists = $wp_filesystem->is_dir( "$wp_content/upgrade" ); 1971 $upgrade_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade" ); 1972 $backup_dir_exists = $wp_filesystem->is_dir( "$wp_content/upgrade/temp-backup" ); 1973 $backup_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade/temp-backup" ); 1974 1975 $plugins_dir_exists = $wp_filesystem->is_dir( "$wp_content/upgrade/temp-backup/plugins" ); 1976 $plugins_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade/temp-backup/plugins" ); 1977 $themes_dir_exists = $wp_filesystem->is_dir( "$wp_content/upgrade/temp-backup/themes" ); 1978 $themes_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade/temp-backup/themes" ); 1979 1980 if ( $plugins_dir_exists && ! $plugins_dir_is_writable && $themes_dir_exists && ! $themes_dir_is_writable ) { 1981 $result['status'] = 'critical'; 1982 $result['label'] = sprintf( 1983 /* translators: %s: temp-backup */ 1984 __( 'Plugins and themes %s directories exist but are not writable' ), 1985 'temp-backup' 1986 ); 1987 $result['description'] = sprintf( 1988 /* translators: 1: wp-content/upgrade/temp-backup/plugins, 2: wp-content/upgrade/temp-backup/themes. */ 1989 '<p>' . __( 'The %1$s and %2$s directories exist but are not writable. These directories are used to improve the stability of plugin updates. Please make sure the server has write permissions to these directories.' ) . '</p>', 1990 '<code>wp-content/upgrade/temp-backup/plugins</code>', 1991 '<code>wp-content/upgrade/temp-backup/themes</code>' 1992 ); 1993 return $result; 1994 } 1995 1996 if ( $plugins_dir_exists && ! $plugins_dir_is_writable ) { 1997 $result['status'] = 'critical'; 1998 $result['label'] = sprintf( 1999 /* translators: %s: temp-backup */ 2000 __( 'Plugins %s directory exists but is not writable' ), 2001 'temp-backup' 2002 ); 2003 $result['description'] = sprintf( 2004 /* translators: %s: wp-content/upgrade/temp-backup/plugins */ 2005 '<p>' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of plugin updates. Please make sure the server has write permissions to this directory.' ) . '</p>', 2006 '<code>wp-content/upgrade/temp-backup/plugins</code>' 2007 ); 2008 return $result; 2009 } 2010 2011 if ( $themes_dir_exists && ! $themes_dir_is_writable ) { 2012 $result['status'] = 'critical'; 2013 $result['label'] = sprintf( 2014 /* translators: %s: temp-backup */ 2015 __( 'Themes %s directory exists but is not writable' ), 2016 'temp-backup' 2017 ); 2018 $result['description'] = sprintf( 2019 /* translators: %s: wp-content/upgrade/temp-backup/themes */ 2020 '<p>' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of theme updates. Please make sure the server has write permissions to this directory.' ) . '</p>', 2021 '<code>wp-content/upgrade/temp-backup/themes</code>' 2022 ); 2023 return $result; 2024 } 2025 2026 if ( ( ! $plugins_dir_exists || ! $themes_dir_exists ) && $backup_dir_exists && ! $backup_dir_is_writable ) { 2027 $result['status'] = 'critical'; 2028 $result['label'] = sprintf( 2029 /* translators: %s: temp-backup */ 2030 __( 'The %s directory exists but is not writable' ), 2031 'temp-backup' 2032 ); 2033 $result['description'] = sprintf( 2034 /* translators: %s: wp-content/upgrade/temp-backup */ 2035 '<p>' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of plugin and theme updates. Please make sure the server has write permissions to this directory.' ) . '</p>', 2036 '<code>wp-content/upgrade/temp-backup</code>' 2037 ); 2038 return $result; 2039 } 2040 2041 if ( ! $backup_dir_exists && $upgrade_dir_exists && ! $upgrade_dir_is_writable ) { 2042 $result['status'] = 'critical'; 2043 $result['label'] = sprintf( 2044 /* translators: %s: upgrade */ 2045 __( 'The %s directory exists but is not writable' ), 2046 'upgrade' 2047 ); 2048 $result['description'] = sprintf( 2049 /* translators: %s: wp-content/upgrade */ 2050 '<p>' . __( 'The %s directory exists but is not writable. This directory is used for plugin and theme updates. Please make sure the server has write permissions to this directory.' ) . '</p>', 2051 '<code>wp-content/upgrade</code>' 2052 ); 2053 return $result; 2054 } 2055 2056 if ( ! $upgrade_dir_exists && ! $wp_filesystem->is_writable( $wp_content ) ) { 2057 $result['status'] = 'critical'; 2058 $result['label'] = sprintf( 2059 /* translators: %s: upgrade */ 2060 __( 'The %s directory cannot be created' ), 2061 'upgrade' 2062 ); 2063 $result['description'] = sprintf( 2064 /* translators: 1: wp-content/upgrade, 2: wp-content. */ 2065 '<p>' . __( 'The %1$s directory does not exist, and the server does not have write permissions in %2$s to create it. This directory is used for plugin and theme updates. Please make sure the server has write permissions in %2$s.' ) . '</p>', 2066 '<code>wp-content/upgrade</code>', 2067 '<code>wp-content</code>' 2068 ); 2069 return $result; 2070 } 2071 2072 return $result; 2073 } 2074 2075 /** 1886 2076 * Test if loopbacks work as expected. 1887 2077 * … … 2267 2457 $tests = array( 2268 2458 'direct' => array( 2269 'wordpress_version' => array(2459 'wordpress_version' => array( 2270 2460 'label' => __( 'WordPress Version' ), 2271 2461 'test' => 'wordpress_version', 2272 2462 ), 2273 'plugin_version' => array(2463 'plugin_version' => array( 2274 2464 'label' => __( 'Plugin Versions' ), 2275 2465 'test' => 'plugin_version', 2276 2466 ), 2277 'theme_version' => array(2467 'theme_version' => array( 2278 2468 'label' => __( 'Theme Versions' ), 2279 2469 'test' => 'theme_version', 2280 2470 ), 2281 'php_version' => array(2471 'php_version' => array( 2282 2472 'label' => __( 'PHP Version' ), 2283 2473 'test' => 'php_version', 2284 2474 ), 2285 'php_extensions' => array(2475 'php_extensions' => array( 2286 2476 'label' => __( 'PHP Extensions' ), 2287 2477 'test' => 'php_extensions', 2288 2478 ), 2289 'php_default_timezone' => array(2479 'php_default_timezone' => array( 2290 2480 'label' => __( 'PHP Default Timezone' ), 2291 2481 'test' => 'php_default_timezone', 2292 2482 ), 2293 'php_sessions' => array(2483 'php_sessions' => array( 2294 2484 'label' => __( 'PHP Sessions' ), 2295 2485 'test' => 'php_sessions', 2296 2486 ), 2297 'sql_server' => array(2487 'sql_server' => array( 2298 2488 'label' => __( 'Database Server version' ), 2299 2489 'test' => 'sql_server', 2300 2490 ), 2301 'utf8mb4_support' => array(2491 'utf8mb4_support' => array( 2302 2492 'label' => __( 'MySQL utf8mb4 support' ), 2303 2493 'test' => 'utf8mb4_support', 2304 2494 ), 2305 'ssl_support' => array(2495 'ssl_support' => array( 2306 2496 'label' => __( 'Secure communication' ), 2307 2497 'test' => 'ssl_support', 2308 2498 ), 2309 'scheduled_events' => array(2499 'scheduled_events' => array( 2310 2500 'label' => __( 'Scheduled events' ), 2311 2501 'test' => 'scheduled_events', 2312 2502 ), 2313 'http_requests' => array(2503 'http_requests' => array( 2314 2504 'label' => __( 'HTTP Requests' ), 2315 2505 'test' => 'http_requests', 2316 2506 ), 2317 'rest_availability' => array(2507 'rest_availability' => array( 2318 2508 'label' => __( 'REST API availability' ), 2319 2509 'test' => 'rest_availability', 2320 2510 'skip_cron' => true, 2321 2511 ), 2322 'debug_enabled' => array(2512 'debug_enabled' => array( 2323 2513 'label' => __( 'Debugging enabled' ), 2324 2514 'test' => 'is_in_debug_mode', 2325 2515 ), 2326 'file_uploads' => array(2516 'file_uploads' => array( 2327 2517 'label' => __( 'File uploads' ), 2328 2518 'test' => 'file_uploads', 2329 2519 ), 2330 'plugin_theme_auto_updates' => array(2520 'plugin_theme_auto_updates' => array( 2331 2521 'label' => __( 'Plugin and theme auto-updates' ), 2332 2522 'test' => 'plugin_theme_auto_updates', 2523 ), 2524 'update_temp_backup_writable' => array( 2525 /* translators: %s: temp-backup */ 2526 'label' => sprintf( __( 'Updates %s directory access' ), 'temp-backup' ), 2527 'test' => 'update_temp_backup_writable', 2528 ), 2529 'available_updates_disk_space' => array( 2530 'label' => __( 'Available disk space' ), 2531 'test' => 'available_updates_disk_space', 2333 2532 ), 2334 2533 ), -
trunk/src/wp-admin/includes/class-wp-upgrader.php
r51655 r51815 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 * 136 138 * @since 2.8.0 139 * @since 5.9.0 Added the `schedule_temp_backup_cleanup()` task. 137 140 */ 138 141 public function init() { 139 142 $this->skin->set_upgrader( $this ); 140 143 $this->generic_strings(); 144 $this->schedule_temp_backup_cleanup(); 145 } 146 147 /** 148 * Schedule cleanup of the temp-backup directory. 149 * 150 * @since 5.9.0 151 */ 152 protected function schedule_temp_backup_cleanup() { 153 wp_schedule_event( time(), 'weekly', 'delete_temp_updater_backups' ); 154 add_action( 'delete_temp_updater_backups', array( $this, 'delete_all_temp_backups' ) ); 141 155 } 142 156 … … 167 181 $this->strings['maintenance_start'] = __( 'Enabling Maintenance mode…' ); 168 182 $this->strings['maintenance_end'] = __( 'Disabling Maintenance mode…' ); 183 184 /* translators: %s: temp-backup */ 185 $this->strings['temp_backup_mkdir_failed'] = sprintf( __( 'Could not create the %s directory.' ), 'temp-backup' ); 186 /* translators: %s: temp-backup */ 187 $this->strings['temp_backup_move_failed'] = sprintf( __( 'Could not move old version to the %s directory.' ), 'temp-backup' ); 188 $this->strings['temp_backup_restore_failed'] = __( 'Could not restore original version.' ); 189 169 190 } 170 191 … … 314 335 if ( ! empty( $upgrade_files ) ) { 315 336 foreach ( $upgrade_files as $file ) { 337 if ( 'temp-backup' === $file['name'] ) { 338 continue; 339 } 316 340 $wp_filesystem->delete( $upgrade_folder . $file['name'], true ); 317 341 } … … 492 516 if ( is_wp_error( $res ) ) { 493 517 return $res; 518 } 519 520 if ( ! empty( $args['hook_extra']['temp_backup'] ) ) { 521 $temp_backup = $this->move_to_temp_backup_dir( $args['hook_extra']['temp_backup'] ); 522 if ( is_wp_error( $temp_backup ) ) { 523 return $temp_backup; 524 } 494 525 } 495 526 … … 812 843 $this->skin->set_result( $result ); 813 844 if ( is_wp_error( $result ) ) { 845 if ( ! empty( $options['hook_extra']['temp_backup'] ) ) { 846 $this->restore_temp_backup( $options['hook_extra']['temp_backup'] ); 847 } 814 848 $this->skin->error( $result ); 815 849 … … 823 857 824 858 $this->skin->after(); 859 860 // Clean up the backup kept in the temp-backup directory. 861 if ( ! empty( $options['hook_extra']['temp_backup'] ) ) { 862 $this->delete_temp_backup( $options['hook_extra']['temp_backup'] ); 863 } 825 864 826 865 if ( ! $options['is_multi'] ) { … … 949 988 } 950 989 990 /** 991 * Moves the plugin/theme being updated into a temp-backup directory. 992 * 993 * @since 5.9.0 994 * 995 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 996 * 997 * @param array $args Array of data for the temp-backup. Must include a slug, the source, and directory. 998 * @return bool|WP_Error 999 */ 1000 public function move_to_temp_backup_dir( $args ) { 1001 global $wp_filesystem; 1002 1003 if ( empty( $args['slug'] ) || empty( $args['src'] ) || empty( $args['dir'] ) ) { 1004 return false; 1005 } 1006 1007 $dest_dir = $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/'; 1008 // Create the temp-backup directory if it doesn't exist. 1009 if ( ( 1010 ! $wp_filesystem->is_dir( $dest_dir ) 1011 && ! $wp_filesystem->mkdir( $dest_dir ) 1012 ) || ( 1013 ! $wp_filesystem->is_dir( $dest_dir . $args['dir'] . '/' ) 1014 && ! $wp_filesystem->mkdir( $dest_dir . $args['dir'] . '/' ) 1015 ) 1016 ) { 1017 return new WP_Error( 'fs_temp_backup_mkdir', $this->strings['temp_backup_mkdir_failed'] ); 1018 } 1019 1020 $src = trailingslashit( $args['src'] ) . $args['slug']; 1021 $dest = $dest_dir . $args['dir'] . '/' . $args['slug']; 1022 1023 // Delete the temp-backup directory if it already exists. 1024 if ( $wp_filesystem->is_dir( $dest ) ) { 1025 $wp_filesystem->delete( $dest, true ); 1026 } 1027 1028 // Move to the temp-backup directory. 1029 if ( ! $wp_filesystem->move( $src, $dest, true ) ) { 1030 return new WP_Error( 'fs_temp_backup_move', $this->strings['temp_backup_move_failed'] ); 1031 } 1032 1033 return true; 1034 } 1035 1036 /** 1037 * Restores the plugin/theme from the temp-backup directory. 1038 * 1039 * @since 5.9.0 1040 * 1041 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1042 * 1043 * @param array $args Array of data for the temp-backup. Must include a slug, the source, and directory. 1044 * @return bool|WP_Error 1045 */ 1046 public function restore_temp_backup( $args ) { 1047 global $wp_filesystem; 1048 1049 if ( empty( $args['slug'] ) || empty( $args['src'] ) || empty( $args['dir'] ) ) { 1050 return false; 1051 } 1052 1053 $src = $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/' . $args['dir'] . '/' . $args['slug']; 1054 $dest = trailingslashit( $args['src'] ) . $args['slug']; 1055 1056 if ( $wp_filesystem->is_dir( $src ) ) { 1057 // Cleanup. 1058 if ( $wp_filesystem->is_dir( $dest ) && ! $wp_filesystem->delete( $dest, true ) ) { 1059 return new WP_Error( 'fs_temp_backup_delete', $this->strings['temp_backup_restore_failed'] ); 1060 } 1061 1062 // Move it. 1063 if ( ! $wp_filesystem->move( $src, $dest, true ) ) { 1064 return new WP_Error( 'fs_temp_backup_delete', $this->strings['temp_backup_restore_failed'] ); 1065 } 1066 } 1067 1068 return true; 1069 } 1070 1071 /** 1072 * Deletes a temp-backup. 1073 * 1074 * @since 5.9.0 1075 * 1076 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1077 * 1078 * @param array $args Array of data for the temp-backup. Must include a slug, the source, and directory. 1079 * @return bool 1080 */ 1081 public function delete_temp_backup( $args ) { 1082 global $wp_filesystem; 1083 1084 if ( empty( $args['slug'] ) || empty( $args['dir'] ) ) { 1085 return false; 1086 } 1087 1088 return $wp_filesystem->delete( 1089 $wp_filesystem->wp_content_dir() . "upgrade/temp-backup/{$args['dir']}/{$args['slug']}", 1090 true 1091 ); 1092 } 1093 1094 /** 1095 * Deletes all contents of the temp-backup directory. 1096 * 1097 * @since 5.9.0 1098 * 1099 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1100 */ 1101 public function delete_all_temp_backups() { 1102 /* 1103 * Check if there's a lock, or if currently performing an Ajax request, 1104 * in which case there's a chance we're doing an update. 1105 * Reschedule for an hour from now and exit early. 1106 */ 1107 if ( get_option( 'core_updater.lock' ) || get_option( 'auto_updater.lock' ) || wp_doing_ajax() ) { 1108 wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'delete_temp_updater_backups' ); 1109 return; 1110 } 1111 1112 add_action( 1113 'shutdown', 1114 /* 1115 * This action runs on shutdown to make sure there's no plugin updates currently running. 1116 * Using a closure in this case is OK since the action can be removed by removing the parent hook. 1117 */ 1118 function() { 1119 global $wp_filesystem; 1120 1121 if ( ! $wp_filesystem ) { 1122 include_once ABSPATH . '/wp-admin/includes/file.php'; 1123 WP_Filesystem(); 1124 } 1125 1126 $dirlist = $wp_filesystem->dirlist( $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/' ); 1127 1128 foreach ( array_keys( $dirlist ) as $dir ) { 1129 if ( '.' === $dir || '..' === $dir ) { 1130 continue; 1131 } 1132 1133 $wp_filesystem->delete( $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/' . $dir, true ); 1134 } 1135 } 1136 ); 1137 } 951 1138 } 952 1139
Note: See TracChangeset
for help on using the changeset viewer.