Automattic\WooCommerce\Internal\EmailEditor

EmailApiController::undo_responsepublicWC 10.9.0

Restore the pre-apply snapshot for a woo_email post. Thin wrapper over WCEmailTemplateSelectiveApplier::undo().

Returns 410 Gone when no snapshot exists for the given post or when the supplied revision_id doesn't match the latest snapshot — matches the design's single-step undo model.

Method of the class: EmailApiController{}

No Hooks.

Returns

WP_REST_Response|WP_Error.

Usage

$EmailApiController = new EmailApiController();
$EmailApiController->undo_response( $request );
$request(WP_REST_Request) (required)
The REST request.

Changelog

Since 10.9.0 Introduced.

EmailApiController::undo_response() code WC 10.9.1

public function undo_response( WP_REST_Request $request ) {
	if ( ! ( $this->post_manager && $this->posts_generator ) ) {
		return new WP_Error(
			'woocommerce_email_editor_not_initialized',
			__( 'Email editor is not initialized.', 'woocommerce' ),
			array( 'status' => 500 )
		);
	}

	$post_id    = (int) $request->get_param( 'id' );
	$email_type = $this->post_manager->get_email_type_from_post_id( $post_id );
	$email      = $this->get_email_by_type( $email_type ?? '' );

	if ( ! $email ) {
		return new WP_Error(
			'woocommerce_email_not_found',
			__( 'No email found for the given post ID.', 'woocommerce' ),
			array( 'status' => 404 )
		);
	}

	$revision_id = (string) $request->get_param( 'revision_id' );

	$result = WCEmailTemplateSelectiveApplier::undo( $post_id, $revision_id );

	if ( is_wp_error( $result ) ) {
		return $result;
	}

	return new WP_REST_Response( $result, 200 );
}