Automattic\WooCommerce\Internal\StockNotifications\Frontend

NotificationManagementService::maybe_process_resend_request │ public │ WC 1.0

Handle the resend-verification request if the current request carries one.

Method of the class: NotificationManagementService{}

No Hooks.

Returns

null. Nothing (null).

Usage

$NotificationManagementService = new NotificationManagementService();
$NotificationManagementService->maybe_process_resend_request(): void;

NotificationManagementService::maybe_process_resend_request() code WC 11.2.1

public function maybe_process_resend_request(): void {
	// Only run on frontend GET requests — skip admin, POST, CLI, etc. before doing any nonce/DB work.
	if ( is_admin() ) {
		return;
	}

	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.NonceVerification.Recommended
	$method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET';
	if ( 'GET' !== $method ) {
		return;
	}

	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( ! isset( $_GET[ self::RESEND_QUERY_ARG ] ) ) {
		return;
	}

	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$notification_id = absint( wp_unslash( $_GET[ self::RESEND_QUERY_ARG ] ) );
	$nonce           = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';

	if ( empty( $notification_id ) ) {
		return;
	}

	// Scope the nonce per-notification so one valid resend URL cannot be replayed
	// across the notification id query arg to trigger emails for other customers.
	if ( ! wp_verify_nonce( $nonce, self::RESEND_NONCE_ACTION . '_' . $notification_id ) ) {
		return;
	}

	$notification = Factory::get_notification( $notification_id );
	if ( ! $notification instanceof Notification ) {
		return;
	}

	// Only the owner may resend a customer-linked notification. Bail silently on a
	// mismatch so the response doesn't confirm that the id exists.
	$owner_id = (int) $notification->get_user_id();
	if ( $owner_id > 0 && is_user_logged_in() && get_current_user_id() !== $owner_id ) {
		return;
	}

	$this->ensure_notice_session();

	$redirect_url = $notification->get_product_permalink();
	if ( empty( $redirect_url ) ) {
		$redirect_url = wc_get_page_permalink( 'shop' );
	}

	$result = $this->resend_verification_email( $notification );
	if ( is_wp_error( $result ) ) {
		$notice_type = self::RESEND_ERROR_RATE_LIMITED === $result->get_error_code() ? 'notice' : 'error';
		wc_add_notice( esc_html( $result->get_error_message() ), $notice_type );
		wp_safe_redirect( $redirect_url );
		exit;
	}

	/* translators: %s user email. */
	wc_add_notice( sprintf( esc_html__( 'Verification email sent to %s.', 'woocommerce' ), $notification->get_user_email() ), 'success' );
	wp_safe_redirect( $redirect_url );
	exit;
}