Automattic\WooCommerce\Internal\Admin\Emails

EmailListingRestController::recreate_email_post │ public │ WC 1.0

Handle the POST /settings/email/listing/recreate-email-post.

Returns the post for the given email type, creating a draft with the file template content when none exists yet. This follows the WordPress Site Editor pattern: posts are only created when the user opens the editor, and only become the rendering source once published.

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

Хуков нет.

Возвращает

array|WP_Error. Request response or an error.

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

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

Код EmailListingRestController::recreate_email_post() WC 11.3.0

public function recreate_email_post( WP_REST_Request $request ) {
	$email_id     = $request->get_param( 'email_id' );
	$post_manager = WCTransactionalEmailPostsManager::get_instance();

	$this->maybe_flush_rewrite_rules();

	// A mapped, non-trashed post already exists (the email was saved before).
	$existing_post = $post_manager->get_email_post( $email_id );
	if ( $existing_post && 'trash' !== $existing_post->post_status ) {
		return array(
			// translators: %s: WooCommerce transactional email ID.
			'message' => sprintf( __( 'Email post already exists for %s.', 'woocommerce' ), $email_id ),
			'post_id' => (string) $existing_post->ID,
		);
	}

	$email = $post_manager->get_email_by_id( (string) $email_id );
	if ( ! $email ) {
		return new WP_Error(
			'woocommerce_rest_email_post_generation_failed',
			// translators: %s: WooCommerce transactional email ID.
			sprintf( __( 'Error generating email post. Email type "%s" is not registered.', 'woocommerce' ), $email_id ),
			array( 'status' => 500 )
		);
	}

	// Reuse an existing editing scratchpad. When it was never edited, refresh
	// its content in place so it reflects the current file template (the post
	// ID stays stable — another admin may have the editor open on it).
	$scratchpad = $this->find_unpublished_post_for_email_type( $email_id );
	if ( $scratchpad ) {
		$this->scratchpad_refresher->maybe_refresh( $scratchpad, $email );

		return array(
			// translators: %s: WooCommerce transactional email ID.
			'message' => sprintf( __( 'Email draft already exists for %s.', 'woocommerce' ), $email_id ),
			'post_id' => (string) $scratchpad->ID,
		);
	}

	try {
		$post_id = $this->email_template_generator->create_draft( $email );
	} catch ( \Exception $e ) {
		return new WP_Error(
			'woocommerce_rest_email_post_generation_failed',
			// translators: %s: Error message.
			sprintf( __( 'Error generating email post. Error: %s.', 'woocommerce' ), $e->getMessage() ),
			array( 'status' => 500 )
		);
	}

	return array(
		// translators: %s: WooCommerce transactional email ID.
		'message' => sprintf( __( 'Email draft created for %s.', 'woocommerce' ), $email_id ),
		'post_id' => (string) $post_id,
	);
}