Automattic\WooCommerce\Internal\EmailEditor

EmailApiController::apply_responsepublicWC 10.9.0

Apply a partial set of core template changes to a woo_email post, driven by per-conflict merchant choices. Thin wrapper over WCEmailTemplateSelectiveApplier::apply_selectively().

The 404 path mirrors {@see self::get_change_summary_response()} — when the email type cannot be resolved from the post ID, the post is either non-existent or not a woo_email. 422 fires when the change-summary has no actionable diff (e.g. post outside the sync registry, or the inversion guard tripped).

Method of the class: EmailApiController{}

No Hooks.

Returns

WP_REST_Response|WP_Error.

Usage

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

Changelog

Since 10.9.0 Introduced.

EmailApiController::apply_response() code WC 10.9.1

public function apply_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 )
		);
	}

	$choices = $request->get_param( 'choices' );
	if ( ! is_array( $choices ) ) {
		$choices = array();
	}

	$result = WCEmailTemplateSelectiveApplier::apply_selectively( $post_id, $choices );

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

	return new WP_REST_Response( $result, 200 );
}