EmailPreview.php
4 weeks ago
EmailPreviewRestController.php
4 weeks ago
PreviewOrder.php
4 weeks 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 |