PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / packages / email-editor / src / Engine / class-email-api-controller.php
class-email-api-controller.php
178 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is part of the WooCommerce Email Editor package
4 *
5 * @package Automattic\WooCommerce\EmailEditor
6 */
7
8 declare(strict_types = 1);
9 namespace Automattic\WooCommerce\EmailEditor\Engine;
10
11 use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tag;
12 use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry;
13 use Automattic\WooCommerce\EmailEditor\Validator\Builder;
14 use WP_Post;
15 use WP_REST_Request;
16 use WP_REST_Response;
17
18 /**
19 * Class for email API controller.
20 */
21 class Email_Api_Controller {
22 /**
23 * Personalization tags registry to get all personalization tags.
24 *
25 * @var Personalization_Tags_Registry
26 */
27 private Personalization_Tags_Registry $personalization_tags_registry;
28
29 /**
30 * Email_Api_Controller constructor with all dependencies.
31 *
32 * @param Personalization_Tags_Registry $personalization_tags_registry Personalization tags registry.
33 */
34 public function __construct( Personalization_Tags_Registry $personalization_tags_registry ) {
35 $this->personalization_tags_registry = $personalization_tags_registry;
36 }
37
38 /**
39 * Returns email specific data.
40 *
41 * @return array - Email specific data such styles.
42 */
43 public function get_email_data(): array {
44 // Here comes code getting Email specific data that will be passed on 'email_data' attribute.
45 return array();
46 }
47
48 /**
49 * Update Email specific data we store.
50 *
51 * @param array $data - Email specific data.
52 * @param WP_Post $email_post - Email post object.
53 */
54 public function save_email_data( array $data, WP_Post $email_post ): void {
55 // Here comes code saving of Email specific data that will be passed on 'email_data' attribute.
56 }
57
58 /**
59 * Sends preview email.
60 *
61 * @param WP_REST_Request $request Route request parameters.
62 * @return WP_REST_Response
63 * @phpstan-param WP_REST_Request<array{_locale: string, email: string, postId: int}> $request
64 */
65 public function send_preview_email_data( WP_REST_Request $request ): WP_REST_Response {
66 /**
67 * $data - Post Data
68 * format
69 * [_locale] => user
70 * [email] => Provided email address
71 * [postId] => POST_ID
72 *
73 * @var array{_locale: string, email: string, postId: int} $data
74 */
75 $data = $request->get_params();
76 try {
77 $result = apply_filters( 'woocommerce_email_editor_send_preview_email', $data );
78 return new WP_REST_Response(
79 array(
80 'success' => (bool) $result,
81 'result' => $result,
82 ),
83 $result ? 200 : 400
84 );
85 } catch ( \Exception $exception ) {
86 return new WP_REST_Response( array( 'error' => $exception->getMessage() ), 400 );
87 }
88 }
89
90 /**
91 * Returns all registered personalization tags.
92 * We need to keep this endpoint for backward compatibility for older JS clients.
93 * We might consider removing it in the future (perhaps in late 2026).
94 *
95 * @deprecated Use get_personalization_tags_collection instead.
96 * @return WP_REST_Response
97 */
98 public function get_personalization_tags(): WP_REST_Response {
99 $tags = $this->personalization_tags_registry->get_all();
100 return new WP_REST_Response(
101 array(
102 'success' => true,
103 'result' => array_values(
104 array_map(
105 function ( Personalization_Tag $tag ) {
106 return array(
107 'name' => $tag->get_name(),
108 'token' => $tag->get_token(),
109 'category' => $tag->get_category(),
110 'attributes' => $tag->get_attributes(),
111 'valueToInsert' => $tag->get_value_to_insert(),
112 'postTypes' => $tag->get_post_types(),
113 );
114 },
115 $tags
116 ),
117 ),
118 ),
119 200
120 );
121 }
122
123 /**
124 * Returns all registered personalization tags as a collection.
125 * This endpoint follows WordPress REST API conventions by returning
126 * the array directly instead of wrapping it in a response object.
127 *
128 * @param WP_REST_Request $request The REST request object.
129 * @return WP_REST_Response
130 * @phpstan-param WP_REST_Request<array<string, mixed>> $request
131 */
132 public function get_personalization_tags_collection( WP_REST_Request $request ): WP_REST_Response {
133 $post_id = $request->get_param( 'post_id' );
134
135 // Allow extensions to extend or modify tags based on post context.
136 // This fires before getting tags so extensions can register additional tags.
137 $post_id = is_numeric( $post_id ) ? (int) $post_id : 0;
138 if ( $post_id > 0 ) {
139 /**
140 * Fires before retrieving personalization tags, allowing extensions
141 * to register or modify tags based on the post context.
142 *
143 * @param int $post_id The post ID for context-aware tag handling.
144 */
145 do_action( 'woocommerce_email_editor_personalization_tags_for_post', $post_id );
146 }
147
148 $tags = $this->personalization_tags_registry->get_all();
149 return new WP_REST_Response(
150 array_values(
151 array_map(
152 function ( Personalization_Tag $tag ) {
153 return array(
154 'name' => $tag->get_name(),
155 'token' => $tag->get_token(),
156 'category' => $tag->get_category(),
157 'attributes' => $tag->get_attributes(),
158 'valueToInsert' => $tag->get_value_to_insert(),
159 'postTypes' => $tag->get_post_types(),
160 );
161 },
162 $tags
163 )
164 ),
165 200
166 );
167 }
168
169 /**
170 * Returns the schema for email data.
171 *
172 * @return array
173 */
174 public function get_email_data_schema(): array {
175 return Builder::object()->to_array();
176 }
177 }
178