Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use Woocommerce Reset Password email for ResetPassword Mutation #878

Merged
merged 13 commits into from
Aug 6, 2024
56 changes: 56 additions & 0 deletions includes/class-woocommerce-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public static function setup() {

// Add better support for Stripe payment gateway.
add_filter( 'graphql_stripe_process_payment_args', [ self::class, 'woographql_stripe_gateway_args' ], 10, 2 );

// WPGraphQL Reset password -> Use woocommerce email password template when requested.
add_filter( 'retrieve_password_message', [ self::class, 'get_reset_password_message' ], 10, 3 );
add_filter( 'retrieve_password_title', [ self::class, 'get_reset_password_title' ] );
}

/**
Expand Down Expand Up @@ -145,4 +149,56 @@ public static function woographql_stripe_gateway_args( $gateway_args, $payment_m

return $gateway_args;
}

/**
* Customizes the password reset message for ResetPassword Mutation.
*
* This function modifies the password reset message to use WooCommerce's email template
* if the `WC_Email_Customer_Reset_Password` email is enabled. It sets the email subject
* and content type based on WooCommerce settings and returns the styled email content.
*
* @param string $message The original password reset message.
* @param string $key The password reset key.
* @param string $user_login The username or email of the user requesting the password reset.
*
* @return string The customized password reset message. Returns the original message if
* the `WC_Email_Customer_Reset_Password` email is not enabled.
*/
public static function get_reset_password_message( $message, $key, $user_login ) {
/** @var \WC_Email_Customer_Reset_Password $wc_reset_email */
$wc_reset_email = \WC()->mailer()->emails['WC_Email_Customer_Reset_Password'];

if ( $wc_reset_email && $wc_reset_email->is_enabled() ) {
add_filter( 'wp_mail_content_type', [ $wc_reset_email, 'get_content_type' ] );

$wc_reset_email->user_login = $user_login;
$wc_reset_email->reset_key = $key;
$message = $wc_reset_email->style_inline( $wc_reset_email->get_content() );
return $message;
}

return $message;
}

/**
* Customizes the password reset title for ResetPassword Mutation.
*
* This function modifies the password reset email title to use WooCommerce's email subject
* if the `WC_Email_Customer_Reset_Password` email is enabled.
*
* @param string $title The original password reset email title.
*
* @return string The customized password reset email title. Returns the original title if
* the `WC_Email_Customer_Reset_Password` email is not enabled.
*/
public static function get_reset_password_title( $title ) {
/** @var \WC_Email_Customer_Reset_Password $wc_reset_email */
$wc_reset_email = \WC()->mailer()->emails['WC_Email_Customer_Reset_Password'];

if ( $wc_reset_email && $wc_reset_email->is_enabled() ) {
return $wc_reset_email->get_subject();
}

return $title;
}
}
Loading