| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Utils; |
| 4 |
|
| 5 |
use YayMail\Constants\AttributesData; |
| 6 |
use YayMail\Constants\TemplatesData; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* TemplateHelpers Classes |
| 12 |
* Define all utility functions to be used inside templates |
| 13 |
*/ |
| 14 |
class TemplateHelpers { |
| 15 |
|
| 16 |
/** |
| 17 |
* @deprecated |
| 18 |
*/ |
| 19 |
public static function get_attribute_data( $attribute_data ) { |
| 20 |
$data = []; |
| 21 |
foreach ( $attribute_data as $key => $attribute ) { |
| 22 |
if ( 'image_box' === $key || 'image_list' === $key || 'text_list' === $key ) { |
| 23 |
if ( isset( $attribute['column_1'] ) ) { |
| 24 |
$data['column_1'] = self::get_attribute_data( $attribute['column_1'] ); |
| 25 |
} |
| 26 |
if ( isset( $attribute['column_2'] ) ) { |
| 27 |
$data['column_2'] = self::get_attribute_data( $attribute['column_2'] ); |
| 28 |
} |
| 29 |
if ( isset( $attribute['column_3'] ) ) { |
| 30 |
$data['column_3'] = self::get_attribute_data( $attribute['column_3'] ); |
| 31 |
} |
| 32 |
} elseif ( 'inner_background_color' === $key ) { |
| 33 |
$data[ $key ] = $attribute; |
| 34 |
} else { |
| 35 |
$data[ $key ] = $attribute['default_value']; |
| 36 |
} |
| 37 |
} |
| 38 |
return $data; |
| 39 |
} |
| 40 |
|
| 41 |
public static function get_spacing_value( $spacing, $unit = 'px' ) { |
| 42 |
$unit = esc_attr( $unit ); |
| 43 |
return sprintf( |
| 44 |
'%d%s %d%s %d%s %d%s', |
| 45 |
isset( $spacing['top'] ) ? $spacing['top'] : 0, |
| 46 |
$unit, |
| 47 |
isset( $spacing['right'] ) ? $spacing['right'] : 0, |
| 48 |
$unit, |
| 49 |
isset( $spacing['bottom'] ) ? $spacing['bottom'] : 0, |
| 50 |
$unit, |
| 51 |
isset( $spacing['left'] ) ? $spacing['left'] : 0, |
| 52 |
$unit |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
public static function get_border_radius_value( $border_radius, $unit = 'px' ) { |
| 57 |
$unit = esc_attr( $unit ); |
| 58 |
return sprintf( |
| 59 |
'%d%s %d%s %d%s %d%s', |
| 60 |
isset( $border_radius['top_left'] ) ? $border_radius['top_left'] : 0, |
| 61 |
$unit, |
| 62 |
isset( $border_radius['top_right'] ) ? $border_radius['top_right'] : 0, |
| 63 |
$unit, |
| 64 |
isset( $border_radius['bottom_right'] ) ? $border_radius['bottom_right'] : 0, |
| 65 |
$unit, |
| 66 |
isset( $border_radius['bottom_left'] ) ? $border_radius['bottom_left'] : 0, |
| 67 |
$unit |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Inner border radius for column_layout children: only the four outer corners of the row |
| 73 |
* (first column left, last column right; single column gets all four). |
| 74 |
* |
| 75 |
* @param array $inner_border_radius Keys top_left, top_right, bottom_left, bottom_right. |
| 76 |
* @param int $total_columns amount_of_columns from parent column_layout. |
| 77 |
* @param int $column_index Zero-based column index. |
| 78 |
* @return array{top_left:int,top_right:int,bottom_right:int,bottom_left:int} |
| 79 |
*/ |
| 80 |
public static function get_inner_column_border_radius( $inner_border_radius, $total_columns, $column_index ) { |
| 81 |
$tl = isset( $inner_border_radius['top_left'] ) ? (int) $inner_border_radius['top_left'] : 0; |
| 82 |
$tr = isset( $inner_border_radius['top_right'] ) ? (int) $inner_border_radius['top_right'] : 0; |
| 83 |
$br = isset( $inner_border_radius['bottom_right'] ) ? (int) $inner_border_radius['bottom_right'] : 0; |
| 84 |
$bl = isset( $inner_border_radius['bottom_left'] ) ? (int) $inner_border_radius['bottom_left'] : 0; |
| 85 |
|
| 86 |
$total_columns = max( 1, (int) $total_columns ); |
| 87 |
$column_index = max( 0, (int) $column_index ); |
| 88 |
|
| 89 |
if ( 1 === $total_columns ) { |
| 90 |
return [ |
| 91 |
'top_left' => $tl, |
| 92 |
'top_right' => $tr, |
| 93 |
'bottom_right' => $br, |
| 94 |
'bottom_left' => $bl, |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
if ( 0 === $column_index ) { |
| 99 |
return [ |
| 100 |
'top_left' => $tl, |
| 101 |
'top_right' => 0, |
| 102 |
'bottom_right' => 0, |
| 103 |
'bottom_left' => $bl, |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
if ( $column_index === $total_columns - 1 ) { |
| 108 |
return [ |
| 109 |
'top_left' => 0, |
| 110 |
'top_right' => $tr, |
| 111 |
'bottom_right' => $br, |
| 112 |
'bottom_left' => 0, |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
return [ |
| 117 |
'top_left' => 0, |
| 118 |
'top_right' => 0, |
| 119 |
'bottom_right' => 0, |
| 120 |
'bottom_left' => 0, |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
public static function get_dimension_value( $dimension, $unit = 'px' ) { |
| 125 |
$unit = esc_attr( $unit ); |
| 126 |
$dimension = floatval( $dimension ); |
| 127 |
return "$dimension$unit"; |
| 128 |
} |
| 129 |
|
| 130 |
public static function get_font_family_value( $font_family ) { |
| 131 |
if ( empty( $font_family ) ) { |
| 132 |
return 'inherit'; |
| 133 |
} |
| 134 |
return str_replace( [ '\"','"' ], '', $font_family ); |
| 135 |
} |
| 136 |
|
| 137 |
public static function wp_kses_allowed_html( $cus_attr_tags = [] ) { |
| 138 |
$allowed_html_tags = wp_kses_allowed_html( 'post' ); |
| 139 |
$allowed_html_tags['style'] = true; |
| 140 |
$allowed_html_tags['html'] = []; |
| 141 |
$allowed_html_tags['header'] = []; |
| 142 |
$allowed_html_tags['meta'] = []; |
| 143 |
$allowed_html_attr = $cus_attr_tags; |
| 144 |
|
| 145 |
$allowed_html_attr ['data-yaymail-element-type'] = true; |
| 146 |
$allowed_html_attr ['charset'] = true; |
| 147 |
$allowed_html_attr ['http-equiv'] = true; |
| 148 |
$allowed_html_attr ['content'] = true; |
| 149 |
$allowed_html_attr ['name'] = true; |
| 150 |
return array_map( |
| 151 |
function ( $item ) use ( $allowed_html_attr ) { |
| 152 |
return is_array( $item ) ? array_merge( $item, $allowed_html_attr ) : $item; |
| 153 |
}, |
| 154 |
$allowed_html_tags |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
public static function get_style( $css_properties = [] ) { |
| 159 |
return implode( |
| 160 |
';', |
| 161 |
array_map( |
| 162 |
function ( $css_value, $css_name ) { |
| 163 |
return "$css_name:$css_value"; |
| 164 |
}, |
| 165 |
$css_properties, |
| 166 |
array_keys( $css_properties ) |
| 167 |
) |
| 168 |
) . ';'; |
| 169 |
} |
| 170 |
|
| 171 |
public static function wrap_element_content( $content_html, $element, $wrapper_style = null ) { |
| 172 |
$html = yaymail_get_content( |
| 173 |
'templates/elements/element-wrapper.php', |
| 174 |
[ |
| 175 |
'content_html' => $content_html, |
| 176 |
'element' => $element, |
| 177 |
'wrapper_style' => $wrapper_style, |
| 178 |
] |
| 179 |
); |
| 180 |
|
| 181 |
yaymail_kses_post_e( $html ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* The function returns the value based on the provided key, default value, and placeholder |
| 186 |
* flag. |
| 187 |
* |
| 188 |
* @param key Key parameter |
| 189 |
* @param default The default value is the value that will be returned if the key is empty or if the |
| 190 |
* is_placeholder parameter is false. |
| 191 |
* @param is_placeholder A boolean value indicating whether the value should be treated as a |
| 192 |
* placeholder or not. |
| 193 |
* |
| 194 |
* @return either the value of the variable or the placeholder "[[]]" depending on |
| 195 |
* the values of the and variables. |
| 196 |
*/ |
| 197 |
public static function get_content_as_placeholder( $key, $default, $is_placeholder ) { |
| 198 |
return $is_placeholder && ! empty( $key ) ? "[[{$key}]]" : $default; |
| 199 |
} |
| 200 |
|
| 201 |
public static function get_booking_from_order( $order ) { |
| 202 |
$booking_ids = []; |
| 203 |
|
| 204 |
if ( null !== $order ) { |
| 205 |
if ( is_callable( 'WC_Booking_Data_Store::get_booking_ids_from_order_id' ) ) { |
| 206 |
$booking_data = new \WC_Booking_Data_Store(); |
| 207 |
$booking_ids = $booking_data->get_booking_ids_from_order_id( $order->get_id() ); |
| 208 |
} |
| 209 |
|
| 210 |
if ( ! empty( $booking_ids ) ) { |
| 211 |
return new \WC_Booking( $booking_ids[0] ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return null; |
| 216 |
} |
| 217 |
|
| 218 |
public static function get_font_size( $size, $is_subtitle = false ) { |
| 219 |
if ( 'default' === $size && $is_subtitle ) { |
| 220 |
return '13px'; |
| 221 |
} |
| 222 |
$result = isset( AttributesData::TITLE_SIZE_OPTIONS[ $size ] ) ? AttributesData::TITLE_SIZE_OPTIONS[ $size ] : 16; |
| 223 |
$result .= 'px'; |
| 224 |
return $result; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Remove empty shortcodes from the content |
| 229 |
* |
| 230 |
* @param string $content The content to remove empty shortcodes from |
| 231 |
* @return string The content with empty shortcodes removed |
| 232 |
* @since 4.0.2 |
| 233 |
*/ |
| 234 |
public static function remove_empty_shortcodes( $content ) { |
| 235 |
$content = preg_replace( '/<p\b[^>]*>\[yaymail_[^\]]*\]<\/p>/i', '', $content ); |
| 236 |
$content = preg_replace( '/\[yaymail_[^\]]*\]/', '', $content ); |
| 237 |
return $content; |
| 238 |
} |
| 239 |
|
| 240 |
public static function convert_rgb_to_hex( $color ) { |
| 241 |
if ( is_string( $color ) && strpos( $color, 'rgb' ) === 0 ) { |
| 242 |
$rgb = str_replace( 'rgb(', '', $color ); |
| 243 |
$rgb = str_replace( ')', '', $rgb ); |
| 244 |
$rgb = explode( ',', $rgb ); |
| 245 |
$hex = '#'; |
| 246 |
$hex .= str_pad( dechex( $rgb[0] ), 2, '0', STR_PAD_LEFT ); |
| 247 |
$hex .= str_pad( dechex( $rgb[1] ), 2, '0', STR_PAD_LEFT ); |
| 248 |
$hex .= str_pad( dechex( $rgb[2] ), 2, '0', STR_PAD_LEFT ); |
| 249 |
return $hex; |
| 250 |
} else { |
| 251 |
return $color; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Find element by id in the list of elements |
| 257 |
* |
| 258 |
* @param string $id The id of the element to find. |
| 259 |
* @param array $list_elements The list of elements to search in. |
| 260 |
* @return array|null The element if found, null otherwise |
| 261 |
* @since 4.1.0 |
| 262 |
*/ |
| 263 |
public static function find_element_by_id( $id, $list_elements ) { |
| 264 |
foreach ( $list_elements as $element ) { |
| 265 |
if ( $element['id'] === $id ) { |
| 266 |
return $element; |
| 267 |
} |
| 268 |
if ( $element['children'] && count( $element['children'] ) > 0 ) { |
| 269 |
$result = self::find_element_by_id( $id, $element['children'] ); |
| 270 |
if ( $result ) { |
| 271 |
return $result; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
return null; |
| 276 |
} |
| 277 |
|
| 278 |
public static function find_parent_element( $id, $list_elements ) { |
| 279 |
|
| 280 |
foreach ( $list_elements as $element ) { |
| 281 |
if ( empty( $element['children'] ) ) { |
| 282 |
continue; |
| 283 |
} |
| 284 |
if ( in_array( $id, array_column( $element['children'], 'id' ) ) ) { |
| 285 |
return $element; |
| 286 |
} |
| 287 |
$sub_query = self::find_parent_element( $id, $element['children'] ); |
| 288 |
if ( $sub_query ) { |
| 289 |
return $sub_query; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return null; |
| 294 |
} |
| 295 |
|
| 296 |
public static function get_current_column_index( $id, $list_elements ) { |
| 297 |
$element = self::find_element_by_id( $id, $list_elements ); |
| 298 |
if ( empty( $element ) ) { |
| 299 |
return 0; |
| 300 |
} |
| 301 |
$parent_element = self::find_parent_element( $element['id'], $list_elements ); |
| 302 |
if ( empty( $parent_element ) ) { |
| 303 |
return 0; |
| 304 |
} |
| 305 |
$current_column_index = array_search( $element['id'], array_column( $parent_element['children'], 'id' ) ); |
| 306 |
return $current_column_index; |
| 307 |
} |
| 308 |
|
| 309 |
public static function get_border_css_value( $border ) { |
| 310 |
if ( $border['side'] === 'none' ) { |
| 311 |
return ''; |
| 312 |
} |
| 313 |
if ( $border['side'] === 'all' ) { |
| 314 |
return self::get_style( |
| 315 |
[ |
| 316 |
'border' => self::get_border_style( $border ), |
| 317 |
] |
| 318 |
); |
| 319 |
} |
| 320 |
if ( $border['side'] === 'top' ) { |
| 321 |
return self::get_style( |
| 322 |
[ |
| 323 |
'border-top' => self::get_border_style( $border ), |
| 324 |
] |
| 325 |
); |
| 326 |
} |
| 327 |
if ( $border['side'] === 'bottom' ) { |
| 328 |
return self::get_style( |
| 329 |
[ |
| 330 |
'border-bottom' => self::get_border_style( $border ), |
| 331 |
] |
| 332 |
); |
| 333 |
} |
| 334 |
if ( $border['side'] === 'right' ) { |
| 335 |
return self::get_style( |
| 336 |
[ |
| 337 |
'border-right' => self::get_border_style( $border ), |
| 338 |
] |
| 339 |
); |
| 340 |
} |
| 341 |
if ( $border['side'] === 'left' ) { |
| 342 |
return self::get_style( |
| 343 |
[ |
| 344 |
'border-left' => self::get_border_style( $border ), |
| 345 |
] |
| 346 |
); |
| 347 |
} |
| 348 |
if ( $border['side'] === 'custom' ) { |
| 349 |
return self::get_style( |
| 350 |
[ |
| 351 |
'border-top' => self::get_border_style( |
| 352 |
[ |
| 353 |
'width' => $border['custom']['top'], |
| 354 |
'style' => $border['style'], |
| 355 |
'color' => $border['color'], |
| 356 |
] |
| 357 |
), |
| 358 |
'border-right' => self::get_border_style( |
| 359 |
[ |
| 360 |
'width' => $border['custom']['right'], |
| 361 |
'style' => $border['style'], |
| 362 |
'color' => $border['color'], |
| 363 |
] |
| 364 |
), |
| 365 |
'border-bottom' => self::get_border_style( |
| 366 |
[ |
| 367 |
'width' => $border['custom']['bottom'], |
| 368 |
'style' => $border['style'], |
| 369 |
'color' => $border['color'], |
| 370 |
] |
| 371 |
), |
| 372 |
'border-left' => self::get_border_style( |
| 373 |
[ |
| 374 |
'width' => $border['custom']['left'], |
| 375 |
'style' => $border['style'], |
| 376 |
'color' => $border['color'], |
| 377 |
] |
| 378 |
), |
| 379 |
] |
| 380 |
); |
| 381 |
}//end if |
| 382 |
return ''; |
| 383 |
} |
| 384 |
|
| 385 |
public static function get_border_style( $border, $unit = 'px' ) { |
| 386 |
$unit = esc_attr( $unit ); |
| 387 |
return sprintf( |
| 388 |
'%d%s %s %s', |
| 389 |
$border['width'], |
| 390 |
$unit, |
| 391 |
$border['style'], |
| 392 |
$border['color'] |
| 393 |
); |
| 394 |
} |
| 395 |
|
| 396 |
public static function sanitize_elements_recursive( $elements ) { |
| 397 |
if ( ! is_array( $elements ) ) { |
| 398 |
return []; |
| 399 |
} |
| 400 |
|
| 401 |
foreach ( $elements as &$element ) { |
| 402 |
if ( isset( $element['data']['rich_text'] ) ) { |
| 403 |
$element['data']['rich_text'] = yaymail_kses_post( $element['data']['rich_text'], $allowed ); |
| 404 |
} |
| 405 |
|
| 406 |
if ( isset( $element['data']['title'] ) ) { |
| 407 |
$element['data']['title'] = yaymail_kses_post( $element['data']['title'] ); |
| 408 |
} |
| 409 |
|
| 410 |
// Recursive cho nested elements |
| 411 |
if ( isset( $element['children'] ) ) { |
| 412 |
$element['children'] = self::sanitize_elements_recursive( $element['children'] ); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
return $elements; |
| 417 |
} |
| 418 |
|
| 419 |
public static function replace_color_paths( $value ) { |
| 420 |
return $value; |
| 421 |
} |
| 422 |
} |