Skip to content

Commit 9d91179

Browse files
committed
Hook into user creation
1 parent 1f5720a commit 9d91179

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

projects/plugins/wpcomsh/connection/class-protected-owner-error-handler.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*
1313
* This class handles errors related to protected owner accounts in the Jetpack Connection.
1414
* 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.
1518
*/
1619
class Protected_Owner_Error_Handler {
1720

@@ -40,6 +43,10 @@ private function __construct() {
4043
add_action( 'jetpack_unlinked_user', array( $this, 'delete_error' ) );
4144
add_action( 'jetpack_updated_user_token', array( $this, 'delete_error' ) );
4245

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+
4350
// Handle context-specific error integration
4451
add_action( 'admin_enqueue_scripts', array( $this, 'setup_context_specific_error_handling' ) );
4552
}
@@ -293,4 +300,51 @@ public function add_to_react_connection_errors( $errors ) {
293300

294301
return $errors;
295302
}
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+
}
296350
}

0 commit comments

Comments
 (0)