PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.2
YayMail – WooCommerce Email Customizer v4.4.2
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.2, at src/Elements/ElementsHelper.php

441 lines 17.8 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 'text_color_path' => 'text_color',
251 'background_color_path' => 'background_color',
252 ];
253
254 $result = self::get_component_data( $attributes, $config, $default_config );
255 $result['component'] = 'RichTextEditor';
256
257 return $result;
258 }
259
260
261 /**
262 * Retrieves configuration for a Button type selector component.
263 *
264 * @param array $attributes The attributes for the Button type selector component.
265 * @param array|null $config (Optional)
266 * - 'value_path' (string): The path to the value in the attributes array.
267 * Default: 'button_type'
268 * - 'title' (string): The title of the Button type selector component.
269 * Default: 'Type'
270 * - 'default_value' (string): The default value for the Button type selector component.
271 * Default: 'default'
272 * @return array An array containing configuration options for the font family selector component.
273 * - 'value_path' (string): The path to the value in the attributes array.
274 * - 'component' (string): The type of component, which is 'ButtonTypeSelector'.
275 * - 'title' (string): The title of the Button type selector component.
276 * - 'default_value' (string): The default value for the Button type selector component.
277 */
278 public static function get_button_type_selector( $attributes, $config = null ) {
279
280 $default_config = [
281 'value_path' => 'button_type',
282 'title' => __( 'Type', 'yaymail' ),
283 'default_value' => 'default',
284 'type' => 'style',
285 ];
286
287 $result = self::get_component_data( $attributes, $config, $default_config );
288 $result['component'] = 'ButtonTypeSelector';
289
290 return $result;
291 }
292
293 /**
294 * Retrieves configuration for a Font weight selector component.
295 *
296 * @param array $attributes The attributes for the Font weight selector component.
297 * @param array|null $config (Optional)
298 * - 'value_path' (string): The path to the value in the attributes array.
299 * Default: 'weight'
300 * - 'title' (string): The title of the Font weight selector component.
301 * Default: 'Weight'
302 * - 'default_value' (string): The default value for the Font weight selector component.
303 * Default: 'normal'
304 * @return array An array containing configuration options for the Font weight selector component.
305 * - 'value_path' (string): The path to the value in the attributes array.
306 * - 'component' (string): The type of component, which is 'FontWeightSelector'.
307 * - 'title' (string): The title of the component.
308 * - 'default_value' (string): The default value for the component.
309 */
310 public static function get_font_weight_selector( $attributes, $config = null ) {
311
312 $default_config = [
313 'value_path' => 'weight',
314 'title' => __( 'Weight', 'yaymail' ),
315 'default_value' => 'normal',
316 'type' => 'style',
317 ];
318
319 $result = self::get_component_data( $attributes, $config, $default_config );
320 $result['component'] = 'FontWeightSelector';
321
322 return $result;
323 }
324
325
326 private static function get_component_data( $attributes, $config, $default_config ) {
327 $value_path = $config['value_path'] ?? $default_config['value_path'] ?? '';
328 $result = wp_parse_args(
329 [
330 'value_path' => $value_path,
331 'title' => $config['title'] ?? $default_config['title'] ?? '',
332 'default_value' => self::get_default_value( $attributes, $value_path, $config['default_value'] ?? $default_config['default_value'] ?? '' ),
333 'type' => $config['type'] ?? $default_config['type'] ?? 'content',
334 ],
335 $config ?? [],
336 );
337
338 return $result;
339 }
340
341 private static function get_default_value( $attributes, $value_path, $fallback_value ) {
342 return isset( $attributes[ $value_path ] ) ? $attributes[ $value_path ] : $fallback_value;
343 }
344
345 public static function filter_available_elements( $elements, $email_id = '' ): array {
346
347 $email = yaymail_get_email( $email_id );
348 // Optimize this to call get_email only once
349
350 if ( empty( $email ) ) {
351 return $elements;
352 }
353
354 $available_elements = [];
355
356 foreach ( $elements as $element ) {
357 $element_instance = ElementsLoader::get_instance()->get_element_instance_by_type( $element['type'] );
358 if ( ! empty( $element_instance ) && $element_instance->is_available_in_email( $email ) ) {
359 $available_elements[] = $element;
360 }
361 }
362 return $available_elements;
363 }
364
365 /**
366 * Get Spacing element data. Default is 'padding'
367 *
368 * @param array $attributes The attributes array containing the spacing data.
369 * @param array|null $config (Optional) Configuration for the Spacing component.
370 * Default:
371 * [
372 * 'value_path' => "padding",
373 * 'title' => "Padding",
374 * 'default_value' => [
375 * 'top' => '15',
376 * 'right' => '50',
377 * 'bottom' => '15',
378 * 'left' => '50',
379 * ]
380 * ]
381 *
382 * @since 4.2.0
383 * @return array An array containing spacing property data.
384 */
385 public static function get_border_radius( $attributes, $config = [] ) {
386
387 if ( ! is_array( $attributes ) || empty( $attributes ) ) {
388 $attributes = [];
389 }
390
391 $default_config = [
392 'value_path' => 'border_radius',
393 'title' => __( 'Border radius', 'yaymail' ),
394 'default_value' => [
395 'top' => '0',
396 'right' => '0',
397 'bottom' => '0',
398 'left' => '0',
399 ],
400 'type' => 'style',
401 ];
402
403 $result = self::get_component_data( $attributes, $config, $default_config );
404 $result['component'] = 'BorderRadius';
405
406 return $result;
407 }
408
409 public static function get_button_width_selector( $attributes, $config = [] ) {
410 $default_config = [
411 'value_path' => 'width',
412 'title' => __( 'Width', 'yaymail' ),
413 'default_value' => 'auto',
414 'type' => 'style',
415 ];
416
417 $result = self::get_component_data( $attributes, $config, $default_config );
418 $result['component'] = 'ButtonWidthSelector';
419
420 return $result;
421 }
422
423 public static function get_text_format_toggles( $attributes, $config = [] ) {
424 $default_config = [
425 'value_path' => 'text_format_toggles',
426 'title' => __( 'Text format toggles', 'yaymail' ),
427 'default_value' => [
428 'bold' => false,
429 'italic' => true,
430 'underline' => false,
431 ],
432 'type' => 'style',
433 ];
434
435 $result = self::get_component_data( $attributes, $config, $default_config );
436 $result['component'] = 'TextFormatToggles';
437
438 return $result;
439 }
440 }
441