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