PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.0
YayMail – WooCommerce Email Customizer v4.4.0
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 3.4.2 3.4.3 All 55 releases
yaymail / src / Elements / ElementsHelper.php

ElementsHelper.php in YayMail – WooCommerce Email Customizer 4.4.0, at src/Elements/ElementsHelper.php

421 lines 17.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Elements;
4
5 use YayMail\Constants\AttributesData;
6
7 /**
8 * Class ElementsHelper
9 *
10 * Helper class for managing elements in YayMail.
11 */
12 class ElementsHelper {
13 /**
14 * Private constructor to prevent instantiation of the class.
15 */
16 private function __construct() {}
17
18 /**
19 * Get alignment element data.
20 *
21 * @param array $attributes The attributes array containing the alignment data.
22 * @param array $config Config for the Align component. Default:
23 * [
24 * 'value_path' => "align",
25 * 'title' => "Align",
26 * 'default_value' => "center"
27 * ]
28 *
29 * @return array An array containing alignment property data.
30 */
31 public static function get_align( $attributes, $config = [] ) {
32 $default_config = [
33 'value_path' => 'align',
34 'title' => __( 'Align', 'yaymail' ),
35 'default_value' => 'center',
36 'type' => 'style',
37 ];
38
39 $result = self::get_component_data( $attributes, $config, $default_config );
40 $result['component'] = 'Align';
41
42 return $result;
43 }
44
45 /**
46 * Get Spacing element data. Default is 'padding'
47 *
48 * @param array $attributes The attributes array containing the spacing data.
49 * @param array|null $config (Optional) Configuration for the Spacing component.
50 * Default:
51 * [
52 * 'value_path' => "padding",
53 * 'title' => "Padding",
54 * 'default_value' => [
55 * 'top' => '15',
56 * 'right' => '50',
57 * 'bottom' => '15',
58 * 'left' => '50',
59 * ]
60 * ]
61 *
62 * @return array An array containing spacing property data.
63 */
64 public static function get_spacing( $attributes, $config = [] ) {
65
66 if ( ! is_array( $attributes ) || empty( $attributes ) ) {
67 $attributes = [];
68 }
69
70 $default_config = [
71 'value_path' => 'padding',
72 'title' => __( 'Padding', 'yaymail' ),
73 'default_value' => [
74 'top' => '15',
75 'right' => '50',
76 'bottom' => '15',
77 'left' => '50',
78 ],
79 'type' => 'style',
80 ];
81
82 $result = self::get_component_data( $attributes, $config, $default_config );
83 $result['component'] = 'Spacing';
84
85 return $result;
86 }
87
88 /**
89 * Get URL element data.
90 *
91 * @param array $attributes The attributes array containing the URL data.
92 * @param array $config Configuration for Media component.
93 * [
94 * 'value_path' => "src",
95 * 'title' => "Image URL",
96 * 'default_value' => YAYMAIL_PLUGIN_URL . 'assets/images/woocommerce-logo.png'
97 * ]
98 *
99 * @return array An array containing image URL property data.
100 */
101 public static function get_media( $attributes, $config = [] ) {
102 $default_src = esc_url( YAYMAIL_PLUGIN_URL . 'assets/images/woocommerce-logo.png' );
103
104 $default_config = [
105 'value_path' => 'src',
106 'title' => __( 'Image URL', 'yaymail' ),
107 'default_value' => $default_src,
108 ];
109
110 $result = self::get_component_data( $attributes, $config, $default_config );
111 $result['component'] = 'Media';
112
113 return $result;
114 }
115
116 /**
117 * Get dimension data. Default is 'width'
118 *
119 * @param array $attributes The attributes array containing the Dimension data.
120 * @param array $config Configuration for component Dimension.
121 * [
122 * 'value_path' => "width",
123 * 'title' => "Width",
124 * 'default_value' => "172"
125 * ]
126 *
127 * @return array An array containing Dimension property data.
128 */
129 public static function get_dimension( $attributes, $config = [] ) {
130 $default_config = [
131 'value_path' => 'width',
132 'title' => __( 'Width', 'yaymail' ),
133 'default_value' => '172',
134 'type' => 'style',
135 ];
136
137 $result = self::get_component_data( $attributes, $config, $default_config );
138 $result['component'] = 'Dimension';
139
140 return $result;
141 }
142
143 /**
144 * Get color data. Default is Background color
145 *
146 * @param array $attributes The attributes array containing the color data.
147 * @param array $config Configuration for Component Color.
148 * [
149 * 'value_path' => "background_color",
150 * 'title' => "Background Color",
151 * 'default_value' => "#f9f9f9",
152 * ]
153 *
154 * @return array An array containing color property data.
155 */
156 public static function get_color( $attributes, $config = [] ) {
157 $default_config = [
158 'value_path' => 'background_color',
159 'title' => __( 'Background color', 'yaymail' ),
160 'default_value' => '#f9f9f9',
161 'type' => 'style',
162 ];
163
164 $result = self::get_component_data( $attributes, $config, $default_config );
165 $result['component'] = 'Color';
166
167 return $result;
168 }
169
170 /**
171 * Get TextInput data. Default is URL
172 *
173 * @param array $attributes The attributes array containing the TextInput data.
174 * @param array $config Configuration for component TextInput.
175 * [
176 * 'value_path' => "url",
177 * 'title' => "URL",
178 * 'default_value' => "#"
179 * ]
180 *
181 * @return array An array containing TextInput property data.
182 */
183 public static function get_text_input( $attributes, $config = null ) {
184 $default_config = [
185 'value_path' => 'url',
186 'title' => __( 'URL', 'yaymail' ),
187 'default_value' => '#',
188 ];
189
190 $result = self::get_component_data( $attributes, $config, $default_config );
191 $result['component'] = 'TextInput';
192
193 return $result;
194 }
195
196 /**
197 * Retrieves configuration for a font family selector component.
198 *
199 * @param array $attributes The attributes for the font family selector component.
200 * @param array|null $config (Optional) Configuration for the font family selector component.
201 * If not provided, default configuration values are used.
202 * Default: null
203 * - 'value_path' (string): The path to the value in the attributes array.
204 * Default: 'font_family'
205 * - 'title' (string): The title of the font family selector component.
206 * Default: 'Font family'
207 * - 'default_value' (string): The default value for the font family selector component.
208 * Default: YAYMAIL_DEFAULT_FAMILY
209 * @return array An array containing configuration options for the font family selector component.
210 * - 'value_path' (string): The path to the value in the attributes array.
211 * - 'component' (string): The type of component, which is 'FontFamilySelector'.
212 * - 'title' (string): The title of the font family selector component.
213 * - 'default_value' (string): The default value for the font family selector component.
214 */
215 public static function get_font_family_selector( $attributes, $config = null ) {
216
217 $default_config = [
218 'value_path' => 'font_family',
219 'title' => __( 'Font family', 'yaymail' ),
220 'default_value' => YAYMAIL_DEFAULT_FAMILY,
221 'type' => 'style',
222 ];
223
224 $result = self::get_component_data( $attributes, $config, $default_config );
225 $result['component'] = 'FontFamilySelector';
226
227 return $result;
228 }
229
230 /**
231 * Retrieves data for a rich text component.
232 *
233 * @param array $attributes The attributes for the rich text component.
234 * @param array|null $config (Optional) The configuration options for the rich text component.
235 * The structure of $config should match $default_config:
236 * [
237 * 'value_path' => 'rich_text', // The path to the value in the attributes.
238 * 'title' => 'Content', // The title of the rich text component.
239 * 'default_value' => '', // The default value for the rich text component.
240 * ]
241 *
242 * @return array An array containing data for the rich text component, including value path, title,
243 * default value, and component type.
244 */
245 public static function get_rich_text( $attributes, $config = null ) {
246 $default_config = [
247 'value_path' => 'rich_text',
248 'title' => __( 'Content', 'yaymail' ),
249 'default_value' => '',
250 ];
251
252 $result = self::get_component_data( $attributes, $config, $default_config );
253 $result['component'] = 'RichTextEditor';
254
255 return $result;
256 }
257
258
259 /**
260 * Retrieves configuration for a Button type selector component.
261 *
262 * @param array $attributes The attributes for the Button type selector component.
263 * @param array|null $config (Optional)
264 * - 'value_path' (string): The path to the value in the attributes array.
265 * Default: 'button_type'
266 * - 'title' (string): The title of the Button type selector component.
267 * Default: 'Type'
268 * - 'default_value' (string): The default value for the Button type selector component.
269 * Default: 'default'
270 * @return array An array containing configuration options for the font family selector component.
271 * - 'value_path' (string): The path to the value in the attributes array.
272 * - 'component' (string): The type of component, which is 'ButtonTypeSelector'.
273 * - 'title' (string): The title of the Button type selector component.
274 * - 'default_value' (string): The default value for the Button type selector component.
275 */
276 public static function get_button_type_selector( $attributes, $config = null ) {
277
278 $default_config = [
279 'value_path' => 'button_type',
280 'title' => __( 'Type', 'yaymail' ),
281 'default_value' => 'default',
282 'type' => 'style',
283 ];
284
285 $result = self::get_component_data( $attributes, $config, $default_config );
286 $result['component'] = 'ButtonTypeSelector';
287
288 return $result;
289 }
290
291 /**
292 * Retrieves configuration for a Font weight selector component.
293 *
294 * @param array $attributes The attributes for the Font weight selector component.
295 * @param array|null $config (Optional)
296 * - 'value_path' (string): The path to the value in the attributes array.
297 * Default: 'weight'
298 * - 'title' (string): The title of the Font weight selector component.
299 * Default: 'Weight'
300 * - 'default_value' (string): The default value for the Font weight selector component.
301 * Default: 'normal'
302 * @return array An array containing configuration options for the Font weight selector component.
303 * - 'value_path' (string): The path to the value in the attributes array.
304 * - 'component' (string): The type of component, which is 'FontWeightSelector'.
305 * - 'title' (string): The title of the component.
306 * - 'default_value' (string): The default value for the component.
307 */
308 public static function get_font_weight_selector( $attributes, $config = null ) {
309
310 $default_config = [
311 'value_path' => 'weight',
312 'title' => __( 'Weight', 'yaymail' ),
313 'default_value' => 'normal',
314 'type' => 'style',
315 ];
316
317 $result = self::get_component_data( $attributes, $config, $default_config );
318 $result['component'] = 'FontWeightSelector';
319
320 return $result;
321 }
322
323
324 private static function get_component_data( $attributes, $config, $default_config ) {
325 $value_path = $config['value_path'] ?? $default_config['value_path'] ?? '';
326 $result = wp_parse_args(
327 [
328 'value_path' => $value_path,
329 'title' => $config['title'] ?? $default_config['title'] ?? '',
330 'default_value' => self::get_default_value( $attributes, $value_path, $config['default_value'] ?? $default_config['default_value'] ?? '' ),
331 'type' => $config['type'] ?? $default_config['type'] ?? 'content',
332 ],
333 $config ?? [],
334 );
335
336 return $result;
337 }
338
339 private static function get_default_value( $attributes, $value_path, $fallback_value ) {
340 return isset( $attributes[ $value_path ] ) ? $attributes[ $value_path ] : $fallback_value;
341 }
342
343 public static function filter_available_elements( $elements, $email_id = '' ): array {
344
345 $email = yaymail_get_email( $email_id );
346 // Optimize this to call get_email only once
347
348 if ( empty( $email ) ) {
349 return $elements;
350 }
351
352 $available_elements = [];
353
354 foreach ( $elements as $element ) {
355 $element_instance = ElementsLoader::get_instance()->get_element_instance_by_type( $element['type'] );
356 if ( ! empty( $element_instance ) && $element_instance->is_available_in_email( $email ) ) {
357 $available_elements[] = $element;
358 }
359 }
360 return $available_elements;
361 }
362
363 /**
364 * Get Spacing element data. Default is 'padding'
365 *
366 * @param array $attributes The attributes array containing the spacing data.
367 * @param array|null $config (Optional) Configuration for the Spacing component.
368 * Default:
369 * [
370 * 'value_path' => "padding",
371 * 'title' => "Padding",
372 * 'default_value' => [
373 * 'top' => '15',
374 * 'right' => '50',
375 * 'bottom' => '15',
376 * 'left' => '50',
377 * ]
378 * ]
379 *
380 * @since 4.2.0
381 * @return array An array containing spacing property data.
382 */
383 public static function get_border_radius( $attributes, $config = [] ) {
384
385 if ( ! is_array( $attributes ) || empty( $attributes ) ) {
386 $attributes = [];
387 }
388
389 $default_config = [
390 'value_path' => 'border_radius',
391 'title' => __( 'Border radius', 'yaymail' ),
392 'default_value' => [
393 'top' => '0',
394 'right' => '0',
395 'bottom' => '0',
396 'left' => '0',
397 ],
398 'type' => 'style',
399 ];
400
401 $result = self::get_component_data( $attributes, $config, $default_config );
402 $result['component'] = 'BorderRadius';
403
404 return $result;
405 }
406
407 public static function get_button_width_selector( $attributes, $config = [] ) {
408 $default_config = [
409 'value_path' => 'width',
410 'title' => __( 'Width', 'yaymail' ),
411 'default_value' => 'auto',
412 'type' => 'style',
413 ];
414
415 $result = self::get_component_data( $attributes, $config, $default_config );
416 $result['component'] = 'ButtonWidthSelector';
417
418 return $result;
419 }
420 }
421