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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Admin / EmailPreview / EmailPreviewRestController.php
woocommerce / src / Internal / Admin / EmailPreview Last commit date
EmailPreview.php 1 month ago EmailPreviewRestController.php 1 month ago PreviewOrder.php 1 month ago
EmailPreviewRestController.php
348 lines
1 <?php
2 declare( strict_types=1 );
3
4 namespace Automattic\WooCommerce\Internal\Admin\EmailPreview;
5
6 use Automattic\WooCommerce\Internal\RestApiControllerBase;
7 use WP_Error;
8 use WP_REST_Request;
9
10 /**
11 * Controller for the REST endpoint to send an email preview.
12 */
13 class EmailPreviewRestController extends RestApiControllerBase {
14
15 /**
16 * Email preview nonce.
17 *
18 * @var string
19 */
20 const NONCE_KEY = 'email-preview-nonce';
21
22 /**
23 * Holds the EmailPreview instance for rendering email previews.
24 *
25 * @var EmailPreview
26 */
27 private EmailPreview $email_preview;
28
29 /**
30 * The root namespace for the JSON REST API endpoints.
31 *
32 * @var string
33 */
34 protected string $route_namespace = 'wc-admin-email';
35
36 /**
37 * Route base.
38 *
39 * @var string
40 */
41 protected string $rest_base = 'settings/email';
42
43 /**
44 * Get the WooCommerce REST API namespace for the class.
45 *
46 * @return string
47 */
48 protected function get_rest_api_namespace(): string {
49 return 'wc-admin-email';
50 }
51
52 /**
53 * The constructor.
54 */
55 public function __construct() {
56 $this->email_preview = wc_get_container()->get( EmailPreview::class );
57 }
58
59 /**
60 * Register the REST API endpoints handled by this controller.
61 */
62 public function register_routes() {
63 register_rest_route(
64 $this->route_namespace,
65 '/' . $this->rest_base . '/send-preview',
66 array(
67 array(
68 'methods' => \WP_REST_Server::CREATABLE,
69 'callback' => fn( $request ) => $this->send_email_preview( $request ),
70 'permission_callback' => fn( $request ) => $this->check_permissions( $request ),
71 'args' => $this->get_args_for_send_preview(),
72 'schema' => $this->get_schema_with_message(),
73 ),
74 )
75 );
76
77 register_rest_route(
78 $this->route_namespace,
79 '/' . $this->rest_base . '/preview-subject',
80 array(
81 array(
82 'methods' => \WP_REST_Server::READABLE,
83 'callback' => fn() => array(
84 'subject' => $this->email_preview->get_subject(),
85 ),
86 'permission_callback' => fn( $request ) => $this->check_permissions( $request ),
87 'args' => $this->get_args_for_preview_subject(),
88 'schema' => $this->get_schema_for_preview_subject(),
89 ),
90 )
91 );
92
93 register_rest_route(
94 $this->route_namespace,
95 '/' . $this->rest_base . '/save-transient',
96 array(
97 array(
98 'methods' => \WP_REST_Server::CREATABLE,
99 'callback' => fn( $request ) => $this->save_transient( $request ),
100 'permission_callback' => fn( $request ) => $this->check_permissions( $request ),
101 'args' => $this->get_args_for_save_transient(),
102 'schema' => $this->get_schema_with_message(),
103 ),
104 )
105 );
106 }
107
108 /**
109 * Get the accepted arguments for the POST send-preview request.
110 *
111 * @return array[]
112 */
113 private function get_args_for_send_preview() {
114 return array(
115 'type' => array(
116 'description' => __( 'The email type to preview.', 'woocommerce' ),
117 'type' => 'string',
118 'required' => true,
119 'validate_callback' => fn( $key ) => $this->validate_email_type( $key ),
120 'sanitize_callback' => 'sanitize_text_field',
121 ),
122 'email' => array(
123 'description' => __( 'Email address to send the email preview to.', 'woocommerce' ),
124 'type' => 'string',
125 'format' => 'email',
126 'required' => true,
127 'validate_callback' => 'rest_validate_request_arg',
128 'sanitize_callback' => 'sanitize_email',
129 ),
130 );
131 }
132
133 /**
134 * Get the accepted arguments for the GET preview-subject request.
135 *
136 * @return array[]
137 */
138 private function get_args_for_preview_subject() {
139 return array(
140 'type' => array(
141 'description' => __( 'The email type to get subject for.', 'woocommerce' ),
142 'type' => 'string',
143 'required' => true,
144 'validate_callback' => fn( $key ) => $this->validate_email_type( $key ),
145 'sanitize_callback' => 'sanitize_text_field',
146 ),
147 );
148 }
149
150 /**
151 * Get the accepted arguments for the POST save-transient request.
152 *
153 * @return array[]
154 */
155 private function get_args_for_save_transient() {
156 return array(
157 'key' => array(
158 'required' => true,
159 'type' => 'string',
160 'description' => 'The key for the transient. Must be one of the allowed options.',
161 'validate_callback' => function ( $key ) {
162 if ( ! in_array( $key, EmailPreview::get_all_email_setting_ids(), true ) ) {
163 return new \WP_Error(
164 'woocommerce_rest_not_allowed_key',
165 sprintf( 'The provided key "%s" is not allowed.', $key ),
166 array( 'status' => 400 ),
167 );
168 }
169 return true;
170 },
171 'sanitize_callback' => 'sanitize_text_field',
172 ),
173 'value' => array(
174 'required' => true,
175 'type' => 'string',
176 'description' => 'The value to be saved for the transient.',
177 'validate_callback' => 'rest_validate_request_arg',
178 'sanitize_callback' => function ( $value, $request ) {
179 $key = $request->get_param( 'key' );
180 if (
181 'woocommerce_email_footer_text' === $key
182 || preg_match( '/_additional_content$/', $key )
183 ) {
184 return wp_kses_post( trim( $value ) );
185 }
186 return sanitize_text_field( $value );
187 },
188 ),
189 );
190 }
191
192 /**
193 * Get the schema for the POST send-preview and save-transient requests.
194 *
195 * @return array[]
196 */
197 private function get_schema_with_message() {
198 return array(
199 '$schema' => 'http://json-schema.org/draft-04/schema#',
200 'title' => 'email-preview-with-message',
201 'type' => 'object',
202 'properties' => array(
203 'message' => array(
204 'description' => __( 'A message indicating that the action completed successfully.', 'woocommerce' ),
205 'type' => 'string',
206 'context' => array( 'view', 'edit' ),
207 'readonly' => true,
208 ),
209 ),
210 );
211 }
212
213 /**
214 * Get the schema for the GET preview_subject request.
215 *
216 * @return array[]
217 */
218 private function get_schema_for_preview_subject() {
219 return array(
220 '$schema' => 'http://json-schema.org/draft-04/schema#',
221 'title' => 'email-preview-subject',
222 'type' => 'object',
223 'properties' => array(
224 'subject' => array(
225 'description' => __( 'A subject for provided email type after filters are applied and placeholders replaced.', 'woocommerce' ),
226 'type' => 'string',
227 'context' => array( 'view' ),
228 'readonly' => true,
229 ),
230 ),
231 );
232 }
233
234 /**
235 * Validate the email type.
236 *
237 * @param string $email_type The email type to validate.
238 * @return bool|WP_Error True if the email type is valid, otherwise a WP_Error object.
239 */
240 private function validate_email_type( string $email_type ) {
241 try {
242 $this->email_preview->set_email_type( $email_type );
243 } catch ( \InvalidArgumentException $e ) {
244 return new WP_Error(
245 'woocommerce_rest_invalid_email_type',
246 __( 'Invalid email type.', 'woocommerce' ),
247 array( 'status' => 400 ),
248 );
249 }
250 return true;
251 }
252
253 /**
254 * Permission check for REST API endpoint.
255 *
256 * @param WP_REST_Request $request The request for which the permission is checked.
257 * @return bool|WP_Error True if the current user has the capability, otherwise a WP_Error object.
258 */
259 private function check_permissions( WP_REST_Request $request ) {
260 $nonce = $request->get_param( 'nonce' );
261 if ( ! wp_verify_nonce( $nonce, self::NONCE_KEY ) ) {
262 return new WP_Error(
263 'invalid_nonce',
264 __( 'Invalid nonce.', 'woocommerce' ),
265 array( 'status' => 403 ),
266 );
267 }
268 return $this->check_permission( $request, 'manage_woocommerce' );
269 }
270
271 /**
272 * Handle the POST /settings/email/send-preview.
273 *
274 * @param WP_REST_Request $request The received request.
275 * @return array|WP_Error Request response or an error.
276 */
277 public function send_email_preview( WP_REST_Request $request ) {
278 $email_address = $request->get_param( 'email' );
279 // Start output buffering to prevent partial renders with PHP notices or warnings.
280 ob_start();
281 try {
282 $email_content = $this->email_preview->render();
283 } catch ( \Throwable $e ) {
284 ob_end_clean();
285 return new WP_Error(
286 'woocommerce_rest_email_preview_not_rendered',
287 __( 'There was an error rendering an email preview.', 'woocommerce' ),
288 array( 'status' => 500 )
289 );
290 }
291 ob_end_clean();
292 $email_subject = $this->email_preview->get_subject();
293 // Clone so the recipient mutation below does not persist on the EmailPreview
294 // singleton: under long-lived runtimes (Roadrunner/FrankenPHP) the cached
295 // instance is reused across requests, which would leak the test admin's
296 // address into a later send for a different recipient.
297 $email = clone $this->email_preview->get_email();
298 // Set the recipient on the WC_Email instance so it is available to hooks
299 // (e.g. woocommerce_email_sent) and log entries when the test email is sent.
300 $email->recipient = $email_address;
301 $mark_as_test = function ( array $context ): array {
302 $context['is_test'] = true;
303 return $context;
304 };
305 add_filter( 'woocommerce_email_log_context', $mark_as_test );
306 try {
307 $sent = $email->send( $email_address, $email_subject, $email_content, $email->get_headers(), $email->get_attachments() );
308 } finally {
309 remove_filter( 'woocommerce_email_log_context', $mark_as_test );
310 }
311
312 if ( $sent ) {
313 return array(
314 // translators: %s: Email address.
315 'message' => sprintf( __( 'Test email sent to %s.', 'woocommerce' ), $email_address ),
316 );
317 }
318 return new WP_Error(
319 'woocommerce_rest_email_preview_not_sent',
320 __( 'Error sending test email. Please try again.', 'woocommerce' ),
321 array( 'status' => 500 )
322 );
323 }
324
325 /**
326 * Handle the POST /settings/email/save-transient.
327 *
328 * @param WP_REST_Request $request The received request.
329 * @return array|WP_Error Request response or an error.
330 */
331 public function save_transient( WP_REST_Request $request ) {
332 $key = $request->get_param( 'key' );
333 $value = $request->get_param( 'value' );
334 $is_set = set_transient( $key, $value, HOUR_IN_SECONDS );
335 if ( ! $is_set ) {
336 return new WP_Error(
337 'woocommerce_rest_transient_not_set',
338 __( 'Error saving transient. Please try again.', 'woocommerce' ),
339 array( 'status' => 500 )
340 );
341 }
342 return array(
343 // translators: %s: Email settings color key, e.g., "woocommerce_email_base_color".
344 'message' => sprintf( __( 'Transient saved for key %s.', 'woocommerce' ), $key ),
345 );
346 }
347 }
348