Automattic\WooCommerce\Internal\StockNotifications\Emails

EmailActionController::process_verification_action │ private │ WC 1.0

Verifies a pending notification when the verification key matches.

Expired, superseded or otherwise invalid keys, as well as notifications that are no longer pending, redirect with a notice instead of failing silently.

Method of the class: EmailActionController{}

Returns

void. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->process_verification_action( $notification, $action_key ): void;
$notification(Notification) (required)
The notification to process.
$action_key(string) (required)
The action key to verify.

EmailActionController::process_verification_action() code WC 11.3.0

private function process_verification_action( Notification $notification, string $action_key ): void {
	// A mismatched key gets the generic notice regardless of status, so the
	// response does not reveal the sign-up's state to anyone guessing keys.
	if ( ! $notification->verification_key_matches( $action_key ) ) {
		$this->redirect_with_notice( self::ACTION_VERIFY, $this->get_invalid_link_notice_text( self::ACTION_VERIFY ), 'error' );
	}

	// Guard against re-hits of a verification URL (double-click, email prefetch,
	// link-scanner bots). Without this, each hit would re-dispatch the verified email.
	// Checked before expiry so a verified sign-up reports as verified even once its link is stale.
	$status = $notification->get_status();
	if ( NotificationStatus::PENDING !== $status ) {
		$notice_text = NotificationStatus::CANCELLED === $status
			? esc_html__( 'This sign-up has been cancelled, so it can no longer be verified.', 'woocommerce' )
			: esc_html__( 'This sign-up has already been verified.', 'woocommerce' );
		$this->redirect_with_notice( self::ACTION_VERIFY, $notice_text, 'notice' );
	}

	if ( $notification->is_verification_key_expired() ) {
		$this->redirect_with_notice( self::ACTION_VERIFY, esc_html__( 'This verification link has expired. Sign up again to request a new one.', 'woocommerce' ), 'error' );
	}

	$notification->set_status( NotificationStatus::ACTIVE );
	$notification->set_date_confirmed( time() );
	$notification->save();

	/**
	 * Action: woocommerce_customer_stock_notifications_verified
	 *
	 * Fires after a stock-notification signup has been verified via the
	 * double opt-in email link. Mirrors `woocommerce_customer_stock_notifications_signup`.
	 *
	 * @since 10.9.0
	 *
	 * @param Notification $notification The notification.
	 */
	do_action( 'woocommerce_customer_stock_notifications_verified', $notification );

	$this->email_manager->send_verified_email( $notification );

	$product = wc_get_product( $notification->get_product_id() );

	/* translators: %s is product name */
	$notice_text = sprintf( esc_html__( 'Successfully verified stock notifications for "%s".', 'woocommerce' ), $product->get_name() );
	$this->redirect_with_notice( self::ACTION_VERIFY, $notice_text );
}