PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.5
YayMail – WooCommerce Email Customizer v4.4.5
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
← All changes | src/Controllers/TemplateLibraryController.php +224 -3 4.4.1 → 4.4.5 View file →
@@ -2,8 +2,9 @@
2 2
3 3 namespace YayMail\Controllers;
4 4
5 5 use YayMail\Abstracts\BaseController;
6 +use YayMail\Models\LibraryTemplateModel;
6 7 use YayMail\TemplateLibrary\TemplateLibraryService;
7 8 use YayMail\Utils\SingletonTrait;
8 9
9 10 /**
@@ -40,11 +41,81 @@
40 41 ],
41 42 ],
42 43 ]
43 44 );
45 +
46 + register_rest_route(
47 + YAYMAIL_REST_NAMESPACE,
48 + '/template-library/saved',
49 + [
50 + [
51 + 'methods' => \WP_REST_Server::CREATABLE,
52 + 'callback' => [ $this, 'exec_save_template' ],
53 + 'permission_callback' => [ $this, 'permission_callback' ],
54 + 'args' => $this->get_save_template_args(),
55 + ],
56 + ]
57 + );
58 +
59 + register_rest_route(
60 + YAYMAIL_REST_NAMESPACE,
61 + '/template-library/saved/(?P<public_id>[a-f0-9-]{36})',
62 + [
63 + [
64 + 'methods' => \WP_REST_Server::DELETABLE,
65 + 'callback' => [ $this, 'exec_delete_saved_template' ],
66 + 'permission_callback' => [ $this, 'permission_callback' ],
67 + 'args' => [
68 + 'public_id' => [
69 + 'type' => 'string',
70 + 'required' => true,
71 + ],
72 + ],
73 + ],
74 + ]
75 + );
44 76 }
45 77
46 78 /**
79 + * REST args for saving a template to the library.
80 + *
81 + * @return array
82 + */
83 + protected function get_save_template_args() {
84 + return [
85 + 'scope' => [
86 + 'type' => 'string',
87 + 'required' => true,
88 + 'enum' => [ 'current', 'all' ],
89 + ],
90 + 'name' => [
91 + 'type' => 'string',
92 + 'required' => true,
93 + ],
94 + 'email_type' => [
95 + 'type' => 'string',
96 + 'required' => true,
97 + ],
98 + 'elements' => [
99 + 'type' => 'array',
100 + 'required' => true,
101 + ],
102 + 'email_settings' => [
103 + 'type' => 'object',
104 + 'required' => false,
105 + ],
106 + 'global_header_settings' => [
107 + 'type' => 'object',
108 + 'required' => false,
109 + ],
110 + 'global_footer_settings' => [
111 + 'type' => 'object',
112 + 'required' => false,
113 + ],
114 + ];
115 + }
116 +
117 + /**
47 118 * Exec wrapper for getting list of templates.
48 119 *
49 120 * @param \WP_REST_Request $request Request.
50 121 *
@@ -66,13 +137,13 @@
66 137
67 138 if ( empty( $email_type ) ) {
68 139 return [
69 140 'success' => false,
70 - 'message' => __( 'Template name is required.', 'yaymail' ),
141 + 'message' => __( 'Email type is required.', 'yaymail' ),
71 142 ];
72 143 }
73 144
74 - $templates = TemplateLibraryService::get_instance()->get_list( $email_type );
145 + $templates = TemplateLibraryService::get_instance()->get_merged_list( $email_type );
75 146
76 147 return [
77 148 'success' => true,
78 149 'templates' => $templates,
@@ -77,5 +148,155 @@
77 148 'success' => true,
78 149 'templates' => $templates,
79 150 ];
80 151 }
81 -}
152 +
153 + /**
154 + * Exec wrapper for saving a template to the library.
155 + *
156 + * @param \WP_REST_Request $request Request.
157 + *
158 + * @return \WP_REST_Response|\WP_Error
159 + */
160 + public function exec_save_template( \WP_REST_Request $request ) {
161 + return $this->exec( [ $this, 'save_template' ], $request );
162 + }
163 +
164 + /**
165 + * Save current customizer layout as a library template.
166 + *
167 + * @param \WP_REST_Request $request Request.
168 + *
169 + * @return array
170 + */
171 + public function save_template( \WP_REST_Request $request ) {
172 + $scope = sanitize_text_field( $request->get_param( 'scope' ) );
173 + $name = sanitize_text_field( $request->get_param( 'name' ) );
174 + $email_type = sanitize_text_field( $request->get_param( 'email_type' ) );
175 +
176 + if ( '' === $name ) {
177 + return [
178 + 'success' => false,
179 + 'message' => __( 'Template name is required.', 'yaymail' ),
180 + ];
181 + }
182 +
183 + if ( empty( $email_type ) ) {
184 + return [
185 + 'success' => false,
186 + 'message' => __( 'Email type is required.', 'yaymail' ),
187 + ];
188 + }
189 +
190 + if ( ! in_array( $scope, [ 'current', 'all' ], true ) ) {
191 + return [
192 + 'success' => false,
193 + 'message' => __( 'Invalid save scope.', 'yaymail' ),
194 + ];
195 + }
196 +
197 + $elements = $request->get_param( 'elements' );
198 + if ( ! is_array( $elements ) || empty( $elements ) ) {
199 + return [
200 + 'success' => false,
201 + 'message' => __( 'Template elements are required.', 'yaymail' ),
202 + ];
203 + }
204 +
205 + $base_payload = [
206 + 'name' => $name,
207 + 'elements' => $elements,
208 + 'email_settings' => $request->get_param( 'email_settings' ),
209 + 'global_header_settings' => $request->get_param( 'global_header_settings' ),
210 + 'global_footer_settings' => $request->get_param( 'global_footer_settings' ),
211 + ];
212 +
213 + $email_types = ( 'all' === $scope )
214 + ? TemplateLibraryService::get_instance()->get_supported_email_types_for_library()
215 + : [ $email_type ];
216 +
217 + if ( empty( $email_types ) ) {
218 + return [
219 + 'success' => false,
220 + 'message' => __( 'No supported email types found for bulk save.', 'yaymail' ),
221 + ];
222 + }
223 +
224 + $base_row = LibraryTemplateModel::build_row_from_save_payload(
225 + array_merge( $base_payload, [ 'email_type' => $email_types[0] ] )
226 + );
227 +
228 + $rows = [];
229 + foreach ( $email_types as $type ) {
230 + $row = $base_row;
231 + $row['email_type'] = sanitize_text_field( $type );
232 + $rows[] = $row;
233 + }
234 +
235 + if ( 1 === count( $rows ) ) {
236 + $public_id = LibraryTemplateModel::create( $rows[0] );
237 + $public_ids = false !== $public_id ? [ $public_id ] : [];
238 + } else {
239 + $public_ids = LibraryTemplateModel::create_many( $rows );
240 + }
241 +
242 + if ( empty( $public_ids ) ) {
243 + return [
244 + 'success' => false,
245 + 'message' => count( $email_types ) > 1
246 + ? __( 'Failed to save templates.', 'yaymail' )
247 + : __( 'Failed to save template.', 'yaymail' ),
248 + ];
249 + }
250 +
251 + return [
252 + 'success' => true,
253 + 'message' => __( 'Open templates to see your new one.', 'yaymail' ),
254 + 'created_count' => count( $public_ids ),
255 + 'public_ids' => $public_ids,
256 + 'effect_emails' => $email_types,
257 + ];
258 + }
259 +
260 + /**
261 + * Exec wrapper for deleting a saved library template.
262 + *
263 + * @param \WP_REST_Request $request Request.
264 + *
265 + * @return \WP_REST_Response|\WP_Error
266 + */
267 + public function exec_delete_saved_template( \WP_REST_Request $request ) {
268 + return $this->exec( [ $this, 'delete_saved_template' ], $request );
269 + }
270 +
271 + /**
272 + * Hard-delete a saved library template by public_id.
273 + *
274 + * @param \WP_REST_Request $request Request.
275 + *
276 + * @return array
277 + */
278 + public function delete_saved_template( \WP_REST_Request $request ) {
279 + $public_id = sanitize_text_field( $request->get_param( 'public_id' ) );
280 +
281 + if ( '' === $public_id ) {
282 + return [
283 + 'success' => false,
284 + 'message' => __( 'Template id is required.', 'yaymail' ),
285 + ];
286 + }
287 +
288 + $deleted = LibraryTemplateModel::delete_by_public_id( $public_id );
289 +
290 + if ( ! $deleted ) {
291 + return [
292 + 'success' => false,
293 + 'message' => __( 'Template not found or could not be deleted.', 'yaymail' ),
294 + ];
295 + }
296 +
297 + return [
298 + 'success' => true,
299 + 'message' => __( 'Template deleted successfully.', 'yaymail' ),
300 + ];
301 + }
302 +}