Automattic\WooCommerce\EmailEditor\Engine

Email_Api_Controller{}WC 1.0

Class for email API controller.

Хуки из класса

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

$Email_Api_Controller = new Email_Api_Controller();
// use class methods

Методы

  1. public __construct( Personalization_Tags_Registry $personalization_tags_registry )
  2. public get_email_data()
  3. public get_email_data_schema()
  4. public get_personalization_tags()
  5. public get_personalization_tags_collection()
  6. public save_email_data( array $data, WP_Post $email_post )
  7. public send_preview_email_data( WP_REST_Request $request )

Код Email_Api_Controller{} WC 10.4.3

class Email_Api_Controller {
	/**
	 * Personalization tags registry to get all personalization tags.
	 *
	 * @var Personalization_Tags_Registry
	 */
	private Personalization_Tags_Registry $personalization_tags_registry;

	/**
	 * Email_Api_Controller constructor with all dependencies.
	 *
	 * @param Personalization_Tags_Registry $personalization_tags_registry Personalization tags registry.
	 */
	public function __construct( Personalization_Tags_Registry $personalization_tags_registry ) {
		$this->personalization_tags_registry = $personalization_tags_registry;
	}

	/**
	 * Returns email specific data.
	 *
	 * @return array - Email specific data such styles.
	 */
	public function get_email_data(): array {
		// Here comes code getting Email specific data that will be passed on 'email_data' attribute.
		return array();
	}

	/**
	 * Update Email specific data we store.
	 *
	 * @param array   $data - Email specific data.
	 * @param WP_Post $email_post - Email post object.
	 */
	public function save_email_data( array $data, WP_Post $email_post ): void {
		// Here comes code saving of Email specific data that will be passed on 'email_data' attribute.
	}

	/**
	 * Sends preview email.
	 *
	 * @param WP_REST_Request $request Route request parameters.
	 * @return WP_REST_Response
	 * @phpstan-param WP_REST_Request<array{_locale: string, email: string, postId: int}> $request
	 */
	public function send_preview_email_data( WP_REST_Request $request ): WP_REST_Response {
		/**
		 * $data - Post Data
		 * format
		 * [_locale] => user
		 * [email] => Provided email address
		 * [postId] => POST_ID
		 *
		 * @var array{_locale: string, email: string, postId: int} $data
		 */
		$data = $request->get_params();
		try {
			$result = apply_filters( 'woocommerce_email_editor_send_preview_email', $data );
			return new WP_REST_Response(
				array(
					'success' => (bool) $result,
					'result'  => $result,
				),
				$result ? 200 : 400
			);
		} catch ( \Exception $exception ) {
			return new WP_REST_Response( array( 'error' => $exception->getMessage() ), 400 );
		}
	}

	/**
	 * Returns all registered personalization tags.
	 * We need to keep this endpoint for backward compatibility for older JS clients.
	 * We might consider removing it in the future (perhaps in late 2026).
	 *
	 * @deprecated Use get_personalization_tags_collection instead.
	 * @return WP_REST_Response
	 */
	public function get_personalization_tags(): WP_REST_Response {
		$tags = $this->personalization_tags_registry->get_all();
		return new WP_REST_Response(
			array(
				'success' => true,
				'result'  => array_values(
					array_map(
						function ( Personalization_Tag $tag ) {
							return array(
								'name'          => $tag->get_name(),
								'token'         => $tag->get_token(),
								'category'      => $tag->get_category(),
								'attributes'    => $tag->get_attributes(),
								'valueToInsert' => $tag->get_value_to_insert(),
								'postTypes'     => $tag->get_post_types(),
							);
						},
						$tags
					),
				),
			),
			200
		);
	}

	/**
	 * Returns all registered personalization tags as a collection.
	 * This endpoint follows WordPress REST API conventions by returning
	 * the array directly instead of wrapping it in a response object.
	 *
	 * @return WP_REST_Response
	 */
	public function get_personalization_tags_collection(): WP_REST_Response {
		$tags = $this->personalization_tags_registry->get_all();
		return new WP_REST_Response(
			array_values(
				array_map(
					function ( Personalization_Tag $tag ) {
						return array(
							'name'          => $tag->get_name(),
							'token'         => $tag->get_token(),
							'category'      => $tag->get_category(),
							'attributes'    => $tag->get_attributes(),
							'valueToInsert' => $tag->get_value_to_insert(),
							'postTypes'     => $tag->get_post_types(),
						);
					},
					$tags
				)
			),
			200
		);
	}

	/**
	 * Returns the schema for email data.
	 *
	 * @return array
	 */
	public function get_email_data_schema(): array {
		return Builder::object()->to_array();
	}
}