Automattic\WooCommerce\Internal\EmailEditor

EmailApiController::reset_responsepublicWC 10.8.0

Reset a woo_email post to its current core template render and (when sync-enabled) stamp sync meta.

Writes the canonical post content (byte-identical to what WCTransactionalEmailPostsGenerator{} would produce on a fresh recreate). For emails that are opted in to template sync (registered in WCEmailTemplateSyncRegistry{}), also stamps _wc_email_template_version, _wc_email_template_source_hash, _wc_email_last_synced_at, and _wc_email_template_status = in_sync. Meta writes are conditional on the post update succeeding, so a wp_update_post failure leaves the post — and any pre-existing meta — untouched.

Non-sync-enabled emails (e.g. third-party templates without an @version header) still receive a successful content reset, just without the meta stamp. This mirrors the pre-RSM-148 behaviour where the standalone REST PUT performed the content reset and stamping was a separate side effect, preserving backward compatibility.

Метод класса: EmailApiController{}

Хуков нет.

Возвращает

WP_REST_Response|WP_Error.

Использование

$EmailApiController = new EmailApiController();
$EmailApiController->reset_response( $request );
$request(WP_REST_Request) (обязательный)
The REST request.

Список изменений

С версии 10.8.0 Введена.

Код EmailApiController::reset_response() WC 10.9.1

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

	$result = WCEmailTemplateAutoApplier::apply_to_post(
		$email,
		$post_id,
		array( 'require_uncustomized' => false )
	);

	if ( is_wp_error( $result ) ) {
		return new WP_Error(
			'woocommerce_email_reset_failed',
			sprintf(
				/* translators: %s: underlying error message */
				__( 'Failed to reset email content: %s', 'woocommerce' ),
				$result->get_error_message()
			),
			array( 'status' => 500 )
		);
	}

	return new WP_REST_Response( $result, 200 );
}