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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 / Emails / EmailListingRestController.php
woocommerce / src / Internal / Admin / Emails Last commit date
EmailListingRestController.php 1 year ago
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