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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
<?php
namespace YayMail\Helper;
defined( 'ABSPATH' ) || exit; class Helper {
public static function checkNonce() { $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) { wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) ); } }
public static function sanitize_array( $var ) { if ( is_array( $var ) ) { return array_map( [__CLASS__, 'sanitize_array'], $var ); } else { return is_scalar( $var ) ? wp_kses_allowed_html( $var ) : $var; } }
public static function unsanitize_array( $var ) { if ( is_array( $var ) ) { return array_map( [__CLASS__, 'unsanitize_array'], $var ); } else { return html_entity_decode( $var, ENT_QUOTES, 'UTF-8' ); } }
// Fix bugs for only Customer Invoice (core code of Woocommerce) public static function getCustomerInvoiceSubject( $objEmail ) { if ( $objEmail->object && $objEmail->object->has_status( array( 'completed', 'processing' ) ) ) { $subject = $objEmail->get_option( 'subject_paid', $objEmail->get_default_subject( true ) ); return apply_filters( 'woocommerce_email_subject_customer_invoice_paid', $objEmail->format_string( $subject ), $objEmail->object, $objEmail ); }
$subject = $objEmail->get_option( 'subject', $objEmail->get_default_subject() ); return apply_filters( 'woocommerce_email_subject_customer_invoice', $objEmail->format_string( $subject ), $objEmail->object, $objEmail ); }
// Get Subject for email WC_Email_New_Booking (Woo Bookings plugin) public static function getNewBookingSubject( $objEmail ) { $subject = $objEmail->get_option( 'subject', $objEmail->subject ); return apply_filters( 'woocommerce_email_subject_' . $objEmail->id, $objEmail->format_string( $subject ), $objEmail->object ); }
// Check Key Exist public static function checkKeyExist( $array, $key, $valueDefault ) { if ( isset( $array->$key ) ) { return $key; } else { return $valueDefault; } }
public static function preventXSS( $yaymail_elements ) { foreach ( $yaymail_elements as $key => $value ) { if ( isset( $value['settingRow']['content'] ) ) { $yaymail_elements[ $key ]['settingRow']['content'] = wp_kses_post( html_entity_decode( $value['settingRow']['content'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value['settingRow']['contentTitle'] ) ) { $yaymail_elements[ $key ]['settingRow']['contentTitle'] = wp_kses_post( html_entity_decode( $value['settingRow']['contentTitle'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value['settingRow']['contentAfter'] ) ) { $yaymail_elements[ $key ]['settingRow']['contentAfter'] = wp_kses_post( html_entity_decode( $value['settingRow']['contentAfter'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value['settingRow']['contentBefore'] ) ) { $yaymail_elements[ $key ]['settingRow']['contentBefore'] = wp_kses_post( html_entity_decode( $value['settingRow']['contentBefore'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value['settingRow']['col1TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['col1TtContent'] = wp_kses_post( html_entity_decode( $value['settingRow']['col1TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value['settingRow']['col2TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['col2TtContent'] = wp_kses_post( html_entity_decode( $value['settingRow']['col2TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value['settingRow']['col3TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['col3TtContent'] = wp_kses_post( html_entity_decode( $value['settingRow']['col3TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value['settingRow']['HTMLContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['HTMLContent'] = wp_kses_post( html_entity_decode( $value['settingRow']['HTMLContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value['settingRow']['col2Content'] ) ) { $yaymail_elements[ $key ]['settingRow']['col2Content'] = wp_kses_post( html_entity_decode( $value['settingRow']['col2Content'], ENT_COMPAT, 'UTF-8' ) ); }
// for column // column1 if ( isset( $value['settingRow']['column1'] ) ) { foreach ( $value['settingRow']['column1'] as $key1 => $value1 ) { if ( isset( $value1['settingRow']['content'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['content'] = wp_kses_post( html_entity_decode( $value1['settingRow']['content'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentTitle'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['contentTitle'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentTitle'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentAfter'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['contentAfter'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentAfter'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentBefore'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['contentBefore'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentBefore'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col1TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['col1TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col1TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col2TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['col2TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col2TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col3TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['col3TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col3TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['HTMLContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['HTMLContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['HTMLContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col2Content'] ) ) { $yaymail_elements[ $key ]['settingRow']['column1'][ $key1 ]['settingRow']['col2Content'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col2Content'], ENT_COMPAT, 'UTF-8' ) ); } } } // column2 if ( isset( $value['settingRow']['column2'] ) ) { foreach ( $value['settingRow']['column2'] as $key1 => $value1 ) { if ( isset( $value1['settingRow']['content'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['content'] = wp_kses_post( html_entity_decode( $value1['settingRow']['content'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentTitle'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['contentTitle'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentTitle'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentAfter'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['contentAfter'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentAfter'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentBefore'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['contentBefore'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentBefore'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col1TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['col1TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col1TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col2TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['col2TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col2TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col3TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['col3TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col3TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['HTMLContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['HTMLContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['HTMLContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col2Content'] ) ) { $yaymail_elements[ $key ]['settingRow']['column2'][ $key1 ]['settingRow']['col2Content'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col2Content'], ENT_COMPAT, 'UTF-8' ) ); } } } // column3 if ( isset( $value['settingRow']['column3'] ) ) { foreach ( $value['settingRow']['column3'] as $key1 => $value1 ) { if ( isset( $value1['settingRow']['content'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['content'] = wp_kses_post( html_entity_decode( $value1['settingRow']['content'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentTitle'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['contentTitle'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentTitle'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentAfter'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['contentAfter'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentAfter'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentBefore'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['contentBefore'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentBefore'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col1TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['col1TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col1TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col2TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['col2TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col2TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col3TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['col3TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col3TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['HTMLContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['HTMLContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['HTMLContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col2Content'] ) ) { $yaymail_elements[ $key ]['settingRow']['column3'][ $key1 ]['settingRow']['col2Content'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col2Content'], ENT_COMPAT, 'UTF-8' ) ); } } }
// column4 if ( isset( $value['settingRow']['column4'] ) ) { foreach ( $value['settingRow']['column4'] as $key1 => $value1 ) { if ( isset( $value1['settingRow']['content'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['content'] = wp_kses_post( html_entity_decode( $value1['settingRow']['content'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentTitle'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['contentTitle'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentTitle'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentAfter'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['contentAfter'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentAfter'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['contentBefore'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['contentBefore'] = wp_kses_post( html_entity_decode( $value1['settingRow']['contentBefore'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col1TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['col1TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col1TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col2TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['col2TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col2TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col3TtContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['col3TtContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col3TtContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['HTMLContent'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['HTMLContent'] = wp_kses_post( html_entity_decode( $value1['settingRow']['HTMLContent'], ENT_COMPAT, 'UTF-8' ) ); } if ( isset( $value1['settingRow']['col2Content'] ) ) { $yaymail_elements[ $key ]['settingRow']['column4'][ $key1 ]['settingRow']['col2Content'] = wp_kses_post( html_entity_decode( $value1['settingRow']['col2Content'], ENT_COMPAT, 'UTF-8' ) ); } } } } return $yaymail_elements; }
public static function isPreview( $is_preview = true ) { if ( $is_preview ) { return true; } return false; }
public static function OrderItemsTitle() { $orderTitle = array( 'order_title' => __( '[Order #', 'yaymail' ) . wp_kses_post( do_shortcode( '[yaymail_order_id]' ) . '] (' ) . wp_kses_post( do_shortcode( '[yaymail_order_date]' ) . ')' ), 'product_title' => __( 'Product', 'yaymail' ), 'cost_title' => __( 'Cost', 'yaymail' ), 'quantity_title' => __( 'Quantity', 'yaymail' ), 'price_title' => __( 'Price', 'yaymail' ), 'subtoltal_title' => __( 'Subtotal:', 'yaymail' ), 'discount_title' => __( 'Discount:', 'yaymail' ), 'shipping_title' => __( 'Shipping:', 'yaymail' ), 'payment_method_title' => __( 'Payment method:', 'yaymail' ), 'fully_refunded' => __( 'Order fully refunded.', 'yaymail' ), 'customer_note' => __( 'Note:', 'yaymail' ), 'total_title' => __( 'Total:', 'yaymail' ), 'subscript_id' => __( 'ID', 'yaymail' ), 'subscript_start_date' => __( 'Start date', 'yaymail' ), 'subscript_end_date' => __( 'End date', 'yaymail' ), 'subscript_recurring_total' => __( 'Recurring total', 'yaymail' ), 'subscript_subscription' => __( 'Subscription', 'yaymail' ), 'subscript_price' => __( 'Price', 'yaymail' ), 'subscript_last_order_date' => __( 'Last Order Date', 'yaymail' ), 'subscript_end_of_prepaid_term' => __( 'End of Prepaid Term', 'yaymail' ), 'subscript_date_suspended' => __( 'Date Suspended', 'yaymail' ), ); return $orderTitle; }
public static function OrderItemsDownloadsTitle() { $orderTitle = array( 'items_download_header_title' => __( 'Downloads', 'yaymail' ), 'items_download_product_title' => __( 'Product', 'yaymail' ), 'items_download_expires_title' => __( 'Expires', 'yaymail' ), 'items_download_download_title' => __( 'Download', 'yaymail' ), ); return $orderTitle; }
public static function inforShortcode( $postID, $template, $order ) { $yaymail_settings = get_option( 'yaymail_settings' ); $yaymail_informations = array( 'post_id' => $postID, 'template' => $template, 'order' => $order, 'yaymail_elements' => get_post_meta( $postID, '_yaymail_elements', true ), 'general_settings' => array( 'tableWidth' => $yaymail_settings['container_width'], 'emailBackgroundColor' => get_post_meta( $postID, '_email_backgroundColor_settings', true ) ? get_post_meta( $postID, '_email_backgroundColor_settings', true ) : '#ECECEC', 'textLinkColor' => get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) ? get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) : '#7f54b3', ), ); return $yaymail_informations; }
/** * This function returns an array of allowed HTML tags for a WordPress post, including any custom tags * specified by the user. * * @param cusTags An array of custom HTML tags and their attributes that should be allowed in addition * to the default allowed HTML tags. * * @return an array of allowed HTML tags for a WordPress post, with any custom HTML tags added to the * list. The custom HTML tags are passed as an argument to the function. */ public static function customAllowedHTMLTags( $cusTags = array() ) { $customs_html_attr = $cusTags; $allowed_html_tags = wp_kses_allowed_html( 'post' ); return array_map( function ( $item ) use ( $customs_html_attr ) { return is_array( $item ) ? array_merge( $item, $customs_html_attr ) : $item; }, $allowed_html_tags ); }
/** * It replaces the string 'yaymail-style' with ':style' in all the values of an array * * @param value The value to be sanitized. * * @return The value of the array. */ public static function replaceCustomAllowedHTMLTags( $value ) { if ( is_string( $value ) ) { return str_replace( ':style', 'yaymail-style', $value ); } else { array_walk_recursive( $value, function ( &$item, $key ) { $item = str_replace( 'yaymail-style', ':style', $item ); } ); return $value; } }
public static function yaymail_is_date( $value ) { $is_date = false;
//if a string contains any numbers if (is_string($value) && !preg_match('~[0-9]+~', $value)) { return false; }
//Check for 'x/x' format, for example "12/19" if (preg_match("/^(1[0-2]|0[1-9])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $value)){ return false; }
// Check if the value is a decimal number (e.g., "9.30" or "9,30") if (preg_match('/^-?\d+(\.\d+|,\d+)$/', $value)) { return false; }
if ( ! $value || self::yaymail_is_time( $value ) || self::yaymail_is_time_range( $value ) ) { return $is_date; }
try { $date = new \DateTime( $value ); $year = intval( $date->format( 'Y' ) ); if ( $year >= 1970 && $year <= 2100 ) { $is_date = true; } } catch ( \Exception $e ) { $is_date = false; }
return $is_date; }
public static function yaymail_is_time( $value ) { // Define an array of common time formats $time_formats = array( // 12-hour format with minutes 'h:i A', // 24-hour format with minutes 'H:i', // 12-hour format with minutes and seconds 'h:i:s A', // 24-hour format with minutes and seconds 'H:i:s', // Additional formats 'h.ia', // Example: 8.00am 'h.iA', // Example: 8.00AM // Add more formats as needed... );
foreach ( $time_formats as $format ) { $time = \DateTime::createFromFormat( $format, $value ); if ( false !== $time ) { return true; } }
return false; }
public static function yaymail_is_time_range( $value ) { $regex = '/^(\d{1,2}:\d{2}\s?(AM|PM|am|pm)?)\s?-\s?(\d{1,2}:\d{2}\s?(AM|PM|am|pm)?)$/';
if ( preg_match( $regex, $value ) ) { list($start_time, $end_time) = explode( '-', $value );
if ( self::yaymail_is_time( $start_time ) && self::yaymail_is_time( $end_time ) ) { return true; } }
return false; }
/** * Converts an RGB string to HEX, or validates and returns a HEX string. * * @param string $color The input color string (e.g., "rgb(52, 152, 219)" or "#3498db"). * @return string|false The HEX color string if valid, or false if the input is invalid. */ public static function yaymail_parse_color_to_hex($color) { // Check if the color is already in HEX format if (preg_match('/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/', $color)) { return $color; // Return the valid HEX color as is } // Check if the color is in RGB format if (preg_match('/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/', $color, $matches)) { // Extract RGB values $r = intval($matches[1]); $g = intval($matches[2]); $b = intval($matches[3]); // Ensure RGB values are within range if ($r >= 0 && $r <= 255 && $g >= 0 && $g <= 255 && $b >= 0 && $b <= 255) { // Convert RGB to HEX return sprintf("#%02x%02x%02x", $r, $g, $b); } } // Invalid color input return $color; } }
|