EmailListingRestController.php
204 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Admin\Emails; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\RestApiControllerBase; |
| 7 | use Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmails; |
| 8 | use Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmailPostsGenerator; |
| 9 | use WP_Error; |
| 10 | use WP_REST_Request; |
| 11 | |
| 12 | /** |
| 13 | * Controller for the REST endpoint for the new email listing page. |
| 14 | */ |
| 15 | class EmailListingRestController extends RestApiControllerBase { |
| 16 | |
| 17 | /** |
| 18 | * Email listing nonce. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | const NONCE_KEY = 'email-listing-nonce'; |
| 23 | |
| 24 | /** |
| 25 | * The root namespace for the JSON REST API endpoints. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected string $route_namespace = 'wc-admin-email'; |
| 30 | |
| 31 | /** |
| 32 | * Route base. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected string $rest_base = 'settings/email/listing'; |
| 37 | |
| 38 | /** |
| 39 | * Email template generator instance. |
| 40 | * |
| 41 | * @var WCTransactionalEmailPostsGenerator |
| 42 | */ |
| 43 | private $email_template_generator; |
| 44 | |
| 45 | /** |
| 46 | * Get the WooCommerce REST API namespace for the class. |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | protected function get_rest_api_namespace(): string { |
| 51 | return 'wc-admin-email-listing'; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * The constructor. |
| 56 | */ |
| 57 | public function __construct() { |
| 58 | $this->email_template_generator = new WCTransactionalEmailPostsGenerator(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Perform the initialization. |
| 63 | */ |
| 64 | public function initialize_template_generator() { |
| 65 | $this->email_template_generator->init_default_transactional_emails(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Register the REST API endpoints handled by this controller. |
| 70 | */ |
| 71 | public function register_routes() { |
| 72 | $this->initialize_template_generator(); |
| 73 | |
| 74 | register_rest_route( |
| 75 | $this->route_namespace, |
| 76 | '/' . $this->rest_base . '/recreate-email-post', |
| 77 | array( |
| 78 | array( |
| 79 | 'methods' => \WP_REST_Server::CREATABLE, |
| 80 | 'callback' => fn( $request ) => $this->recreate_email_post( $request ), |
| 81 | 'permission_callback' => fn( $request ) => $this->check_permissions( $request ), |
| 82 | 'args' => $this->get_args_for_recreate_email_post(), |
| 83 | 'schema' => $this->get_schema_with_message(), |
| 84 | ), |
| 85 | ) |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get the accepted arguments for the POST recreate-email-post request. |
| 91 | * |
| 92 | * @return array[] |
| 93 | */ |
| 94 | private function get_args_for_recreate_email_post() { |
| 95 | return array( |
| 96 | 'email_id' => array( |
| 97 | 'description' => __( 'The email ID to recreate the post for.', 'woocommerce' ), |
| 98 | 'type' => 'string', |
| 99 | 'required' => true, |
| 100 | 'validate_callback' => fn( $email_id ) => $this->validate_email_id( $email_id ), |
| 101 | 'sanitize_callback' => 'sanitize_text_field', |
| 102 | ), |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the schema for the POST recreate-email-post and save-transient requests. |
| 108 | * |
| 109 | * @return array[] |
| 110 | */ |
| 111 | private function get_schema_with_message() { |
| 112 | return array( |
| 113 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 114 | 'title' => 'email-listing-with-message', |
| 115 | 'type' => 'object', |
| 116 | 'properties' => array( |
| 117 | 'message' => array( |
| 118 | 'description' => __( 'A message indicating that the action completed successfully.', 'woocommerce' ), |
| 119 | 'type' => 'string', |
| 120 | 'context' => array( 'view', 'edit' ), |
| 121 | 'readonly' => true, |
| 122 | ), |
| 123 | 'post_id' => array( |
| 124 | 'description' => __( 'The post ID of the generated email post.', 'woocommerce' ), |
| 125 | 'type' => 'string', |
| 126 | 'context' => array( 'view', 'edit' ), |
| 127 | 'readonly' => true, |
| 128 | ), |
| 129 | ), |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Validate the email ID. |
| 135 | * |
| 136 | * @param string $email_id The email ID to validate. |
| 137 | * @return bool|WP_Error True if the email ID is valid, otherwise a WP_Error object. |
| 138 | */ |
| 139 | private function validate_email_id( string $email_id ) { |
| 140 | if ( ! in_array( $email_id, WCTransactionalEmails::get_transactional_emails(), true ) ) { |
| 141 | return new \WP_Error( |
| 142 | 'woocommerce_rest_not_allowed_email_id', |
| 143 | sprintf( 'The provided email ID "%s" is not allowed.', $email_id ), |
| 144 | array( 'status' => 400 ), |
| 145 | ); |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Permission check for REST API endpoint. |
| 152 | * |
| 153 | * @param WP_REST_Request $request The request for which the permission is checked. |
| 154 | * @return bool|WP_Error True if the current user has the capability, otherwise a WP_Error object. |
| 155 | */ |
| 156 | private function check_permissions( WP_REST_Request $request ) { |
| 157 | $nonce = $request->get_param( 'nonce' ); |
| 158 | if ( ! wp_verify_nonce( $nonce, self::NONCE_KEY ) ) { |
| 159 | return new WP_Error( |
| 160 | 'invalid_nonce', |
| 161 | __( 'Invalid nonce.', 'woocommerce' ), |
| 162 | array( 'status' => 403 ), |
| 163 | ); |
| 164 | } |
| 165 | return $this->check_permission( $request, 'manage_woocommerce' ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Handle the POST /settings/email/listing/recreate-email-post. |
| 170 | * |
| 171 | * @param WP_REST_Request $request The received request. |
| 172 | * @return array|WP_Error Request response or an error. |
| 173 | */ |
| 174 | public function recreate_email_post( WP_REST_Request $request ) { |
| 175 | $email_id = $request->get_param( 'email_id' ); |
| 176 | |
| 177 | $generated_post_id = ''; |
| 178 | |
| 179 | try { |
| 180 | $generated_post_id = $this->email_template_generator->generate_email_template_if_not_exists( $email_id ); |
| 181 | } catch ( \Exception $e ) { |
| 182 | return new WP_Error( |
| 183 | 'woocommerce_rest_email_post_generation_failed', |
| 184 | // translators: %s: Error message. |
| 185 | sprintf( __( 'Error generating email post. Error: %s.', 'woocommerce' ), $e->getMessage() ), |
| 186 | array( 'status' => 500 ) |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | if ( $generated_post_id ) { |
| 191 | return array( |
| 192 | // translators: %s: WooCommerce transactional email ID. |
| 193 | 'message' => sprintf( __( 'Email post generated for %s.', 'woocommerce' ), $email_id ), |
| 194 | 'post_id' => (string) $generated_post_id, |
| 195 | ); |
| 196 | } |
| 197 | return new WP_Error( |
| 198 | 'woocommerce_rest_email_post_generation_error', |
| 199 | __( 'Error unable to generate email post.', 'woocommerce' ), |
| 200 | array( 'status' => 500 ) |
| 201 | ); |
| 202 | } |
| 203 | } |
| 204 |