From aee84a13a8002a537b9b4538d287e0cc14b196de Mon Sep 17 00:00:00 2001
From: Paul Biron <paul@sparrowhawkcomputing.com>
Date: Sun, 14 Nov 2021 12:32:08 -0700
Subject: [PATCH] Address the problems with the weekly cron job to delete
previous update backups raised in
https://core.trac.wordpress.org/ticket/51857#comment:155.
---
src/wp-admin/includes/class-wp-upgrader.php | 50 ++-------------------
src/wp-includes/update.php | 47 +++++++++++++++++++
2 files changed, 50 insertions(+), 47 deletions(-)
diff --git a/src/wp-admin/includes/class-wp-upgrader.php b/src/wp-admin/includes/class-wp-upgrader.php
index 225a7d4c69..d4db2b082d 100644
|
a
|
b
|
class WP_Upgrader { |
| 150 | 150 | * @since 5.9.0 |
| 151 | 151 | */ |
| 152 | 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' ) ); |
| | 153 | if ( false === wp_next_scheduled( 'wp_delete_temp_updater_backups' ) ) { |
| | 154 | wp_schedule_event( time(), 'weekly', 'wp_delete_temp_updater_backups' ); |
| | 155 | } |
| 155 | 156 | } |
| 156 | 157 | |
| 157 | 158 | /** |
| … |
… |
class WP_Upgrader { |
| 1107 | 1108 | true |
| 1108 | 1109 | ); |
| 1109 | 1110 | } |
| 1110 | | |
| 1111 | | /** |
| 1112 | | * Deletes all contents of the temp-backup directory. |
| 1113 | | * |
| 1114 | | * @since 5.9.0 |
| 1115 | | * |
| 1116 | | * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
| 1117 | | */ |
| 1118 | | public function delete_all_temp_backups() { |
| 1119 | | /* |
| 1120 | | * Check if there's a lock, or if currently performing an Ajax request, |
| 1121 | | * in which case there's a chance we're doing an update. |
| 1122 | | * Reschedule for an hour from now and exit early. |
| 1123 | | */ |
| 1124 | | if ( get_option( 'core_updater.lock' ) || get_option( 'auto_updater.lock' ) || wp_doing_ajax() ) { |
| 1125 | | wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'delete_temp_updater_backups' ); |
| 1126 | | return; |
| 1127 | | } |
| 1128 | | |
| 1129 | | add_action( |
| 1130 | | 'shutdown', |
| 1131 | | /* |
| 1132 | | * This action runs on shutdown to make sure there's no plugin updates currently running. |
| 1133 | | * Using a closure in this case is OK since the action can be removed by removing the parent hook. |
| 1134 | | */ |
| 1135 | | function() { |
| 1136 | | global $wp_filesystem; |
| 1137 | | |
| 1138 | | if ( ! $wp_filesystem ) { |
| 1139 | | include_once ABSPATH . '/wp-admin/includes/file.php'; |
| 1140 | | WP_Filesystem(); |
| 1141 | | } |
| 1142 | | |
| 1143 | | $dirlist = $wp_filesystem->dirlist( $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/' ); |
| 1144 | | |
| 1145 | | foreach ( array_keys( $dirlist ) as $dir ) { |
| 1146 | | if ( '.' === $dir || '..' === $dir ) { |
| 1147 | | continue; |
| 1148 | | } |
| 1149 | | |
| 1150 | | $wp_filesystem->delete( $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/' . $dir, true ); |
| 1151 | | } |
| 1152 | | } |
| 1153 | | ); |
| 1154 | | } |
| 1155 | 1111 | } |
| 1156 | 1112 | |
| 1157 | 1113 | /** Plugin_Upgrader class */ |
diff --git a/src/wp-includes/update.php b/src/wp-includes/update.php
index 42ae3ecf3a..e17c38d0ce 100644
|
a
|
b
|
function wp_clean_update_cache() { |
| 956 | 956 | delete_site_transient( 'update_core' ); |
| 957 | 957 | } |
| 958 | 958 | |
| | 959 | /** |
| | 960 | * Deletes all contents of the temp-backup directory. |
| | 961 | * |
| | 962 | * @since 5.9.0 |
| | 963 | * |
| | 964 | * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
| | 965 | */ |
| | 966 | function wp_delete_all_temp_backups() { |
| | 967 | /* |
| | 968 | * Check if there's a lock, or if currently performing an Ajax request, |
| | 969 | * in which case there's a chance we're doing an update. |
| | 970 | * Reschedule for an hour from now and exit early. |
| | 971 | */ |
| | 972 | if ( get_option( 'core_updater.lock' ) || get_option( 'auto_updater.lock' ) || wp_doing_ajax() ) { |
| | 973 | wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_delete_temp_updater_backups' ); |
| | 974 | return; |
| | 975 | } |
| | 976 | |
| | 977 | add_action( |
| | 978 | 'shutdown', |
| | 979 | /* |
| | 980 | * This action runs on shutdown to make sure there's no plugin updates currently running. |
| | 981 | * Using a closure in this case is OK since the action can be removed by removing the parent hook. |
| | 982 | */ |
| | 983 | function() { |
| | 984 | global $wp_filesystem; |
| | 985 | |
| | 986 | if ( ! $wp_filesystem ) { |
| | 987 | include_once ABSPATH . '/wp-admin/includes/file.php'; |
| | 988 | WP_Filesystem(); |
| | 989 | } |
| | 990 | |
| | 991 | $dirlist = $wp_filesystem->dirlist( $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/' ); |
| | 992 | |
| | 993 | foreach ( array_keys( $dirlist ) as $dir ) { |
| | 994 | if ( '.' === $dir || '..' === $dir ) { |
| | 995 | continue; |
| | 996 | } |
| | 997 | |
| | 998 | $wp_filesystem->delete( $wp_filesystem->wp_content_dir() . 'upgrade/temp-backup/' . $dir, true ); |
| | 999 | } |
| | 1000 | } |
| | 1001 | ); |
| | 1002 | } |
| | 1003 | |
| 959 | 1004 | if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) { |
| 960 | 1005 | return; |
| 961 | 1006 | } |
| … |
… |
add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 ); |
| 980 | 1025 | add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' ); |
| 981 | 1026 | |
| 982 | 1027 | add_action( 'init', 'wp_schedule_update_checks' ); |
| | 1028 | |
| | 1029 | add_action( 'wp_delete_temp_updater_backups', 'wp_delete_all_temp_backups' ); |