Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active March 26, 2020 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/245893df4519d4678828abe5ba1f2cfb to your computer and use it in GitHub Desktop.
Save kellenmace/245893df4519d4678828abe5ba1f2cfb to your computer and use it in GitHub Desktop.
Disable "Password Changed" Email on WPGraphQL User Registration
<?php
class PasswordChangeEmailHandler {
/**
* User ID of the registered user.
*
* @var int
*/
private $registered_user_id = 0;
public function register_hooks() {
add_action( 'register_new_user', [ $this, 'set_registered_user_id'] );
add_filter( 'send_password_change_email', [ $this, 'maybe_prevent_password_change_email'], 10, 3 );
}
/**
* Capture the user ID of the newly registered user and use it to set
* the $registered_user_id property for use in the methods below.
*
* @param int $user_id ID of the registered user.
*/
public function set_registered_user_id( int $user_id ) {
$this->registered_user_id = $user_id;
}
/**
* @param bool $send Whether to send the email.
* @param array $user The original user array.
* @param array $userdata The updated user array.
*
* @return bool Whether to send the email.
*/
public function maybe_prevent_password_change_email( bool $send, array $user, array $userdata ) {
if ( ! $this->should_send_password_change_email( $userdata ) ) {
return false;
}
return $send;
}
/**
* If the user was registered during this same request, do not send
* the password change email.
*
* @param array $user The updated user array.
*
* @return bool Whether the password change email should be sent.
*/
private function should_send_password_change_email( array $userdata ) {
return $this->registered_user_id !== $userdata['ID'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment