| | 1 | <?php |
| | 2 | /** |
| | 3 | * Handles the restore siteurl operation when the user updates either 'home' or 'siteurl' options |
| | 4 | * and wants to revert it back. |
| | 5 | * |
| | 6 | * @since 5.3.0 |
| | 7 | * |
| | 8 | * @link https://core.trac.wordpress.org/ticket/47954 |
| | 9 | */ |
| | 10 | class WP_Restore_Siteurl { |
| | 11 | /** |
| | 12 | * Expiry time of the created transients for Restore Siteurl. |
| | 13 | * |
| | 14 | * @since 5.3.0 |
| | 15 | * @var string |
| | 16 | */ |
| | 17 | const RESTORE_TRANSIENTS_EXPIRY_IN_SECONDS = 1800; |
| | 18 | |
| | 19 | /** |
| | 20 | * Restore key to authenticate the restore siteurl request. |
| | 21 | * |
| | 22 | * @since 5.3.0 |
| | 23 | * @var string |
| | 24 | */ |
| | 25 | private $restore_key = null; |
| | 26 | |
| | 27 | /** |
| | 28 | * Is the restore siteurl email sent to the user. |
| | 29 | * |
| | 30 | * @since 5.3.0 |
| | 31 | * @var boolean |
| | 32 | */ |
| | 33 | private $is_restore_siteurl_email_sent = false; |
| | 34 | |
| | 35 | /** |
| | 36 | * Old 'home' option value. |
| | 37 | * |
| | 38 | * @since 5.3.0 |
| | 39 | * @var string |
| | 40 | */ |
| | 41 | private $old_home_value = null; |
| | 42 | |
| | 43 | /** |
| | 44 | * Hook the functions. |
| | 45 | * |
| | 46 | * @since 5.3.0 |
| | 47 | */ |
| | 48 | public function __construct() { |
| | 49 | add_action( 'registered_taxonomy', array( $this, 'perform_siteurl_restore' ) ); |
| | 50 | add_action( 'updated_option', array( $this, 'setup_siteurl_restore' ), 10, 3 ); |
| | 51 | add_action( 'admin_init', array( $this, 'restore_siteurl_success_notice' ) ); |
| | 52 | } |
| | 53 | |
| | 54 | /** |
| | 55 | * Generates a restore key. |
| | 56 | * |
| | 57 | * @since 5.3.0 |
| | 58 | * |
| | 59 | * @return string $restore_key A random string to authenticate the restore siteurl request. |
| | 60 | */ |
| | 61 | private function generate_restore_key() { |
| | 62 | if ( null === $this->restore_key ) { |
| | 63 | $this->restore_key = wp_generate_password( 16, false ); |
| | 64 | } |
| | 65 | |
| | 66 | return $this->restore_key; |
| | 67 | } |
| | 68 | |
| | 69 | /** |
| | 70 | * Listen to updates for 'home' and 'siteurl' options. |
| | 71 | * |
| | 72 | * This function performs the following tasks when either 'home' and 'siteurl' options are updated: |
| | 73 | * 1. Caches the old 'home' value if it's the option updated. |
| | 74 | * 2. Creates a 'siteurl_restore_key' transient that will be used to authenticate the restore operation. |
| | 75 | * 3. Backups the old 'home' or 'siteurl' values. |
| | 76 | * 4. Sends an email to the admin with the restore link. |
| | 77 | * |
| | 78 | * @since 5.3.0 |
| | 79 | * |
| | 80 | * @param string $option Name of the updated option. |
| | 81 | * @param string $old_value The old option value. |
| | 82 | * @param string $value The new option value. |
| | 83 | */ |
| | 84 | public function setup_siteurl_restore( $option, $old_value, $value ) { |
| | 85 | if ( 'siteurl' === $option || 'home' === $option ) { |
| | 86 | // Prevent Restore Siteurl protocol if the update is the restoration. |
| | 87 | $backup_value = get_transient( "old_{$option}" ); |
| | 88 | if ( $value === $backup_value ) { |
| | 89 | return; |
| | 90 | } |
| | 91 | |
| | 92 | // Backup the old 'home' option value. |
| | 93 | if ( 'home' === $option ) { |
| | 94 | $this->old_home_value = $old_value; |
| | 95 | } |
| | 96 | |
| | 97 | $restore_key_transient = get_transient( 'siteurl_restore_key' ); |
| | 98 | $set_restore_key_transient = false; |
| | 99 | if ( $this->generate_restore_key() != $restore_key_transient ) { |
| | 100 | // Create a restore key transient. |
| | 101 | $set_restore_key_transient = set_transient( 'siteurl_restore_key', $this->generate_restore_key(), self::RESTORE_TRANSIENTS_EXPIRY_IN_SECONDS ); |
| | 102 | } |
| | 103 | |
| | 104 | // Keep a backup of the old `siteurl` and `home`. |
| | 105 | delete_transient( "old_{$option}" ); |
| | 106 | $set_backup_transient = set_transient( "old_{$option}", $old_value, self::RESTORE_TRANSIENTS_EXPIRY_IN_SECONDS ); |
| | 107 | |
| | 108 | if ( $set_restore_key_transient && $set_backup_transient ) { |
| | 109 | $this->send_restore_link_email_to_admin(); |
| | 110 | } |
| | 111 | } |
| | 112 | } |
| | 113 | |
| | 114 | /** |
| | 115 | * Send restore link email to admin. |
| | 116 | * |
| | 117 | * @since 5.3.0 |
| | 118 | */ |
| | 119 | private function send_restore_link_email_to_admin() { |
| | 120 | if ( ! $this->is_restore_siteurl_email_sent ) { |
| | 121 | $restore_link = add_query_arg( [ |
| | 122 | 'srk' => $this->generate_restore_key(), |
| | 123 | ], $this->get_old_home_value() ); |
| | 124 | |
| | 125 | $admin_email = get_option( 'admin_email' ); |
| | 126 | /** |
| | 127 | * Filters the subject of the restore siteurl email. |
| | 128 | * |
| | 129 | * @since 5.3.0 |
| | 130 | * |
| | 131 | * @param string $email_subject The subject of the restore siteurl email. |
| | 132 | */ |
| | 133 | $email_subject = apply_filters( 'restore_siteurl_email_subject', __( 'Your WordPress site url was changed.' ) ); |
| | 134 | /** |
| | 135 | * Filters the content body of the restore siteurl email. |
| | 136 | * |
| | 137 | * @since 5.3.0 |
| | 138 | * |
| | 139 | * @param string $email_content The content body of the restore siteurl email. |
| | 140 | */ |
| | 141 | $email_content = apply_filters( |
| | 142 | 'restore_siteurl_email_message', |
| | 143 | __( 'You can undo this change by clicking this link ###restore_link###' ) |
| | 144 | ); |
| | 145 | |
| | 146 | // Replace placeholder with actual restore link. |
| | 147 | $email_content = str_replace( '###restore_link###', $restore_link, $email_content ); |
| | 148 | |
| | 149 | $email = wp_mail( $admin_email, $email_subject, $email_content ); |
| | 150 | |
| | 151 | if ( $email ) { |
| | 152 | $this->is_restore_siteurl_email_sent = true; |
| | 153 | } |
| | 154 | |
| | 155 | /** |
| | 156 | * Fires after the attempt to send restore link email to admin. |
| | 157 | * |
| | 158 | * @since 5.3.0 |
| | 159 | * |
| | 160 | * @param boolean $email Whether the email is sent successfully. |
| | 161 | */ |
| | 162 | do_action( 'send_restore_link_email', $email ); |
| | 163 | } |
| | 164 | } |
| | 165 | |
| | 166 | /** |
| | 167 | * Get the old 'home' option value. |
| | 168 | * |
| | 169 | * @since 5.3.0 |
| | 170 | * |
| | 171 | * @return string $old_home_value Old 'home' value. |
| | 172 | */ |
| | 173 | public function get_old_home_value() { |
| | 174 | if ( null !== $this->old_home_value ) { |
| | 175 | return esc_url( $this->old_home_value ); |
| | 176 | } |
| | 177 | |
| | 178 | $old_home_transient = get_transient( 'old_home' ); |
| | 179 | if ( $old_home_transient !== false ) { |
| | 180 | $this->old_home_value = $old_home_transient; |
| | 181 | } |
| | 182 | else { |
| | 183 | $this->old_home_value = get_option( 'home' ); |
| | 184 | } |
| | 185 | |
| | 186 | return esc_url( $this->old_home_value ); |
| | 187 | } |
| | 188 | |
| | 189 | /** |
| | 190 | * Perform the siteurl restore operation. |
| | 191 | * |
| | 192 | * If the provided restore key is invalid, the process is terminated using `wp_die()`. |
| | 193 | * Else if the restore key is valid, then it will perform the restore siteurl operation. |
| | 194 | * |
| | 195 | * If the restore siteurl operation is a success, it will perform the following. |
| | 196 | * 1. Delete the backup transients. |
| | 197 | * 2. Create a success-tracker transient named 'siteurl_restore_success'. |
| | 198 | * 3. Redirect the user to the old admin dashboard url. |
| | 199 | * |
| | 200 | * @since 5.3.0 |
| | 201 | */ |
| | 202 | public function perform_siteurl_restore() { |
| | 203 | if ( isset( $_GET['srk'] ) && ! empty( $_GET['srk'] ) ) { |
| | 204 | $restore_key = get_transient( 'siteurl_restore_key' ); |
| | 205 | |
| | 206 | if ( $_GET['srk'] === $restore_key ) { |
| | 207 | $old_siteurl = get_transient( 'old_siteurl' ); |
| | 208 | $old_home = get_transient( 'old_home' ); |
| | 209 | |
| | 210 | $is_restore_success = false; |
| | 211 | |
| | 212 | if ( $old_siteurl ) { |
| | 213 | $update_siteurl = update_option( 'siteurl', $old_siteurl ); |
| | 214 | |
| | 215 | if ( $update_siteurl ) { |
| | 216 | delete_transient( 'old_siteurl' ); |
| | 217 | |
| | 218 | $is_restore_success = true; |
| | 219 | } |
| | 220 | } |
| | 221 | |
| | 222 | if ( $old_home ) { |
| | 223 | $update_home = update_option( 'home', $old_home ); |
| | 224 | |
| | 225 | if ( $update_home ) { |
| | 226 | delete_transient( 'old_home' ); |
| | 227 | |
| | 228 | $is_restore_success = true; |
| | 229 | } |
| | 230 | } |
| | 231 | |
| | 232 | if ( $is_restore_success ) { |
| | 233 | delete_transient( 'siteurl_restore_key' ); |
| | 234 | |
| | 235 | // Track success. |
| | 236 | set_transient( 'siteurl_restore_success', '1', 300 ); |
| | 237 | |
| | 238 | $this->success_redirect(); |
| | 239 | } |
| | 240 | } |
| | 241 | else { |
| | 242 | wp_die( __( 'Restore key is invalid.' ) ); |
| | 243 | exit; |
| | 244 | } |
| | 245 | } |
| | 246 | } |
| | 247 | |
| | 248 | /** |
| | 249 | * Redirect the user to admin dashboard with restore siteurl success $_GET param. |
| | 250 | * |
| | 251 | * @since 5.3.0 |
| | 252 | */ |
| | 253 | protected function success_redirect() { |
| | 254 | $restored_site_admin_url = trailingslashit( get_option( 'home' ) ) . 'wp-admin'; |
| | 255 | |
| | 256 | // Success redirect url. |
| | 257 | $success_redirect_url = add_query_arg( 'srsuccess', '1', $restored_site_admin_url ); |
| | 258 | |
| | 259 | wp_redirect( $success_redirect_url ); |
| | 260 | exit; |
| | 261 | } |
| | 262 | |
| | 263 | /** |
| | 264 | * Display success notice if the restore siteurl operation is a success. |
| | 265 | * |
| | 266 | * @since 5.3.0 |
| | 267 | */ |
| | 268 | public function restore_siteurl_success_notice() { |
| | 269 | if ( isset( $_GET['srsuccess'] ) && '1' === $_GET['srsuccess'] ) { |
| | 270 | $restore_success = get_transient( 'siteurl_restore_success' ); |
| | 271 | |
| | 272 | if ( '1' === $restore_success ) { |
| | 273 | add_action( 'admin_notices', [ $this, 'restore_siteurl_success_notice__success' ] ); |
| | 274 | |
| | 275 | delete_transient( 'siteurl_restore_success' ); |
| | 276 | } |
| | 277 | } |
| | 278 | } |
| | 279 | |
| | 280 | /** |
| | 281 | * Restore siteurl success notice. |
| | 282 | * |
| | 283 | * @since 5.3.0 |
| | 284 | */ |
| | 285 | public function restore_siteurl_success_notice__success() { |
| | 286 | $class = 'notice notice-success'; |
| | 287 | $message = __( 'Your WordPress site url was successfully restored.' ); |
| | 288 | |
| | 289 | printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); |
| | 290 | } |
| | 291 | } |
| | 292 | No newline at end of file |