Automattic\WooCommerce\Internal\StockNotifications\Emails

EmailActionController::process_unsubscribe_action │ private │ WC 1.0

If the unsubscribe key matches, it cancels the notification, unless it was already sent or cancelled.

An invalid key redirects with a notice instead of failing silently.

Method of the class: EmailActionController{}

No Hooks.

Returns

void. Nothing (null).

Usage

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

EmailActionController::process_unsubscribe_action() code WC 11.3.0

private function process_unsubscribe_action( Notification $notification, string $action_key ): void {
	if ( ! $notification->check_unsubscribe_key( $action_key ) ) {
		$this->redirect_with_notice( self::ACTION_UNSUBSCRIBE, $this->get_invalid_link_notice_text( self::ACTION_UNSUBSCRIBE ), 'error' );
	}

	$status = $notification->get_status();

	if ( NotificationStatus::SENT === $status ) {
		$this->redirect_with_notice( self::ACTION_UNSUBSCRIBE, esc_html__( 'This notification has already been sent, so there is nothing to unsubscribe from.', 'woocommerce' ), 'notice' );
	}

	if ( NotificationStatus::ACTIVE !== $status && NotificationStatus::PENDING !== $status ) {
		$this->redirect_with_notice( self::ACTION_UNSUBSCRIBE, esc_html__( 'You are already unsubscribed from this notification.', 'woocommerce' ), 'notice' );
	}

	$notification->set_status( NotificationStatus::CANCELLED );
	$notification->set_cancellation_source( NotificationCancellationSource::USER );
	$notification->set_date_cancelled( time() );
	$notification->save();

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

	/* translators: %2$s product name, %1$s user email */
	$notice_text = sprintf( esc_html__( 'Successfully unsubscribed %1$s. You will not receive a notification when "%2$s" becomes available.', 'woocommerce' ), $notification->get_user_email(), $product->get_name() );
	$this->redirect_with_notice( self::ACTION_UNSUBSCRIBE, $notice_text );
}