|
12 | 12 | *
|
13 | 13 | * This class handles errors related to protected owner accounts in the Jetpack Connection.
|
14 | 14 | * It retrieves owner account errors stored in WordPress options and displays them in the UI.
|
| 15 | + * |
| 16 | + * The class automatically clears errors when the required local account is created, |
| 17 | + * allowing external healing code to establish the proper Jetpack connection. |
15 | 18 | */
|
16 | 19 | class Protected_Owner_Error_Handler {
|
17 | 20 |
|
@@ -40,6 +43,10 @@ private function __construct() {
|
40 | 43 | add_action( 'jetpack_unlinked_user', array( $this, 'delete_error' ) );
|
41 | 44 | add_action( 'jetpack_updated_user_token', array( $this, 'delete_error' ) );
|
42 | 45 |
|
| 46 | + // Clear errors when the missing user is created or updated (allows external healing code to work) |
| 47 | + add_action( 'user_register', array( $this, 'check_and_clear_error_on_user_creation' ) ); |
| 48 | + add_action( 'profile_update', array( $this, 'check_and_clear_error_on_user_update' ) ); |
| 49 | + |
43 | 50 | // Handle context-specific error integration
|
44 | 51 | add_action( 'admin_enqueue_scripts', array( $this, 'setup_context_specific_error_handling' ) );
|
45 | 52 | }
|
@@ -293,4 +300,51 @@ public function add_to_react_connection_errors( $errors ) {
|
293 | 300 |
|
294 | 301 | return $errors;
|
295 | 302 | }
|
| 303 | + |
| 304 | + /** |
| 305 | + * Clear the error when a user with the required email address is created |
| 306 | + * |
| 307 | + * @param int $user_id The ID of the newly created user. |
| 308 | + */ |
| 309 | + public function check_and_clear_error_on_user_creation( $user_id ) { |
| 310 | + $this->check_and_clear_error_for_user( $user_id ); |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * Clear the error when a user with the required email address is updated |
| 315 | + * |
| 316 | + * @param int $user_id The ID of the updated user. |
| 317 | + */ |
| 318 | + public function check_and_clear_error_on_user_update( $user_id ) { |
| 319 | + $this->check_and_clear_error_for_user( $user_id ); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * Check if the user matches the protected owner error and clear it if so |
| 324 | + * This allows external healing code to automatically establish the connection |
| 325 | + * |
| 326 | + * @param int $user_id The ID of the user to check. |
| 327 | + */ |
| 328 | + private function check_and_clear_error_for_user( $user_id ) { |
| 329 | + // Get the raw error data to check the wpcom_email |
| 330 | + $raw_error = get_option( self::STORED_ERRORS_OPTION, false ); |
| 331 | + |
| 332 | + // Return early if no error is stored |
| 333 | + if ( ! $raw_error || ! is_array( $raw_error ) || ! isset( $raw_error['wpcom_email'] ) ) { |
| 334 | + return; |
| 335 | + } |
| 336 | + |
| 337 | + // Get the user |
| 338 | + $user = get_user_by( 'id', $user_id ); |
| 339 | + if ( ! $user ) { |
| 340 | + return; |
| 341 | + } |
| 342 | + |
| 343 | + // Check if the user's email matches the required wpcom_email |
| 344 | + if ( strtolower( $user->user_email ) === strtolower( $raw_error['wpcom_email'] ) ) { |
| 345 | + // The user with the required email has been created/updated |
| 346 | + // Clear the error so external healing code can establish the connection |
| 347 | + $this->delete_error(); |
| 348 | + } |
| 349 | + } |
296 | 350 | }
|
0 commit comments