| 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 |
public static function get_dimension_value( $dimension, $unit = 'px' ) { |
| 72 |
$unit = esc_attr( $unit ); |
| 73 |
$dimension = floatval( $dimension ); |
| 74 |
return "$dimension$unit"; |
| 75 |
} |
| 76 |
|
| 77 |
public static function get_font_family_value( $font_family ) { |
| 78 |
if ( empty( $font_family ) ) { |
| 79 |
return 'inherit'; |
| 80 |
} |
| 81 |
return str_replace( [ '\"','"' ], '', $font_family ); |
| 82 |
} |
| 83 |
|
| 84 |
public static function wp_kses_allowed_html( $cus_attr_tags = [] ) { |
| 85 |
$allowed_html_tags = wp_kses_allowed_html( 'post' ); |
| 86 |
$allowed_html_tags['style'] = true; |
| 87 |
$allowed_html_tags['html'] = []; |
| 88 |
$allowed_html_tags['header'] = []; |
| 89 |
$allowed_html_tags['meta'] = []; |
| 90 |
$allowed_html_attr = $cus_attr_tags; |
| 91 |
|
| 92 |
$allowed_html_attr ['data-yaymail-element-type'] = true; |
| 93 |
$allowed_html_attr ['charset'] = true; |
| 94 |
$allowed_html_attr ['http-equiv'] = true; |
| 95 |
$allowed_html_attr ['content'] = true; |
| 96 |
$allowed_html_attr ['name'] = true; |
| 97 |
return array_map( |
| 98 |
function ( $item ) use ( $allowed_html_attr ) { |
| 99 |
return is_array( $item ) ? array_merge( $item, $allowed_html_attr ) : $item; |
| 100 |
}, |
| 101 |
$allowed_html_tags |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
public static function get_style( $css_properties = [] ) { |
| 106 |
return implode( |
| 107 |
';', |
| 108 |
array_map( |
| 109 |
function ( $css_value, $css_name ) { |
| 110 |
return "$css_name:$css_value"; |
| 111 |
}, |
| 112 |
$css_properties, |
| 113 |
array_keys( $css_properties ) |
| 114 |
) |
| 115 |
) . ';'; |
| 116 |
} |
| 117 |
|
| 118 |
public static function wrap_element_content( $content_html, $element, $wrapper_style = null ) { |
| 119 |
$html = yaymail_get_content( |
| 120 |
'templates/elements/element-wrapper.php', |
| 121 |
[ |
| 122 |
'content_html' => $content_html, |
| 123 |
'element' => $element, |
| 124 |
'wrapper_style' => $wrapper_style, |
| 125 |
] |
| 126 |
); |
| 127 |
|
| 128 |
yaymail_kses_post_e( $html ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* The function returns the value based on the provided key, default value, and placeholder |
| 133 |
* flag. |
| 134 |
* |
| 135 |
* @param key Key parameter |
| 136 |
* @param default The default value is the value that will be returned if the key is empty or if the |
| 137 |
* is_placeholder parameter is false. |
| 138 |
* @param is_placeholder A boolean value indicating whether the value should be treated as a |
| 139 |
* placeholder or not. |
| 140 |
* |
| 141 |
* @return either the value of the variable or the placeholder "[[]]" depending on |
| 142 |
* the values of the and variables. |
| 143 |
*/ |
| 144 |
public static function get_content_as_placeholder( $key, $default, $is_placeholder ) { |
| 145 |
return $is_placeholder && ! empty( $key ) ? "[[{$key}]]" : $default; |
| 146 |
} |
| 147 |
|
| 148 |
public static function get_booking_from_order( $order ) { |
| 149 |
$booking_ids = []; |
| 150 |
|
| 151 |
if ( null !== $order ) { |
| 152 |
if ( is_callable( 'WC_Booking_Data_Store::get_booking_ids_from_order_id' ) ) { |
| 153 |
$booking_data = new \WC_Booking_Data_Store(); |
| 154 |
$booking_ids = $booking_data->get_booking_ids_from_order_id( $order->get_id() ); |
| 155 |
} |
| 156 |
|
| 157 |
if ( ! empty( $booking_ids ) ) { |
| 158 |
return new \WC_Booking( $booking_ids[0] ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return null; |
| 163 |
} |
| 164 |
|
| 165 |
public static function get_font_size( $size, $is_subtitle = false ) { |
| 166 |
if ( 'default' === $size && $is_subtitle ) { |
| 167 |
return '13px'; |
| 168 |
} |
| 169 |
$result = isset( AttributesData::TITLE_SIZE_OPTIONS[ $size ] ) ? AttributesData::TITLE_SIZE_OPTIONS[ $size ] : 16; |
| 170 |
$result .= 'px'; |
| 171 |
return $result; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Remove empty shortcodes from the content |
| 176 |
* |
| 177 |
* @param string $content The content to remove empty shortcodes from |
| 178 |
* @return string The content with empty shortcodes removed |
| 179 |
* @since 4.0.2 |
| 180 |
*/ |
| 181 |
public static function remove_empty_shortcodes( $content ) { |
| 182 |
$content = preg_replace( '/<p\b[^>]*>\[yaymail_[^\]]*\]<\/p>/i', '', $content ); |
| 183 |
$content = preg_replace( '/\[yaymail_[^\]]*\]/', '', $content ); |
| 184 |
return $content; |
| 185 |
} |
| 186 |
|
| 187 |
public static function convert_rgb_to_hex( $color ) { |
| 188 |
if ( is_string( $color ) && strpos( $color, 'rgb' ) === 0 ) { |
| 189 |
$rgb = str_replace( 'rgb(', '', $color ); |
| 190 |
$rgb = str_replace( ')', '', $rgb ); |
| 191 |
$rgb = explode( ',', $rgb ); |
| 192 |
$hex = '#'; |
| 193 |
$hex .= str_pad( dechex( $rgb[0] ), 2, '0', STR_PAD_LEFT ); |
| 194 |
$hex .= str_pad( dechex( $rgb[1] ), 2, '0', STR_PAD_LEFT ); |
| 195 |
$hex .= str_pad( dechex( $rgb[2] ), 2, '0', STR_PAD_LEFT ); |
| 196 |
return $hex; |
| 197 |
} else { |
| 198 |
return $color; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Find element by id in the list of elements |
| 204 |
* |
| 205 |
* @param string $id The id of the element to find. |
| 206 |
* @param array $list_elements The list of elements to search in. |
| 207 |
* @return array|null The element if found, null otherwise |
| 208 |
* @since 4.1.0 |
| 209 |
*/ |
| 210 |
public static function find_element_by_id( $id, $list_elements ) { |
| 211 |
foreach ( $list_elements as $element ) { |
| 212 |
if ( $element['id'] === $id ) { |
| 213 |
return $element; |
| 214 |
} |
| 215 |
if ( $element['children'] && count( $element['children'] ) > 0 ) { |
| 216 |
$result = self::find_element_by_id( $id, $element['children'] ); |
| 217 |
if ( $result ) { |
| 218 |
return $result; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
return null; |
| 223 |
} |
| 224 |
|
| 225 |
public static function find_parent_element( $id, $list_elements ) { |
| 226 |
|
| 227 |
foreach ( $list_elements as $element ) { |
| 228 |
if ( empty( $element['children'] ) ) { |
| 229 |
continue; |
| 230 |
} |
| 231 |
if ( in_array( $id, array_column( $element['children'], 'id' ) ) ) { |
| 232 |
return $element; |
| 233 |
} |
| 234 |
$sub_query = self::find_parent_element( $id, $element['children'] ); |
| 235 |
if ( $sub_query ) { |
| 236 |
return $sub_query; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return null; |
| 241 |
} |
| 242 |
|
| 243 |
public static function get_current_column_index( $id, $list_elements ) { |
| 244 |
$element = self::find_element_by_id( $id, $list_elements ); |
| 245 |
if ( empty( $element ) ) { |
| 246 |
return 0; |
| 247 |
} |
| 248 |
$parent_element = self::find_parent_element( $element['id'], $list_elements ); |
| 249 |
if ( empty( $parent_element ) ) { |
| 250 |
return 0; |
| 251 |
} |
| 252 |
$current_column_index = array_search( $element['id'], array_column( $parent_element['children'], 'id' ) ); |
| 253 |
return $current_column_index; |
| 254 |
} |
| 255 |
|
| 256 |
public static function get_border_css_value( $border ) { |
| 257 |
if ( $border['side'] === 'none' ) { |
| 258 |
return ''; |
| 259 |
} |
| 260 |
if ( $border['side'] === 'all' ) { |
| 261 |
return self::get_style( |
| 262 |
[ |
| 263 |
'border' => self::get_border_style( $border ), |
| 264 |
] |
| 265 |
); |
| 266 |
} |
| 267 |
if ( $border['side'] === 'top' ) { |
| 268 |
return self::get_style( |
| 269 |
[ |
| 270 |
'border-top' => self::get_border_style( $border ), |
| 271 |
] |
| 272 |
); |
| 273 |
} |
| 274 |
if ( $border['side'] === 'bottom' ) { |
| 275 |
return self::get_style( |
| 276 |
[ |
| 277 |
'border-bottom' => self::get_border_style( $border ), |
| 278 |
] |
| 279 |
); |
| 280 |
} |
| 281 |
if ( $border['side'] === 'right' ) { |
| 282 |
return self::get_style( |
| 283 |
[ |
| 284 |
'border-right' => self::get_border_style( $border ), |
| 285 |
] |
| 286 |
); |
| 287 |
} |
| 288 |
if ( $border['side'] === 'left' ) { |
| 289 |
return self::get_style( |
| 290 |
[ |
| 291 |
'border-left' => self::get_border_style( $border ), |
| 292 |
] |
| 293 |
); |
| 294 |
} |
| 295 |
if ( $border['side'] === 'custom' ) { |
| 296 |
return self::get_style( |
| 297 |
[ |
| 298 |
'border-top' => self::get_border_style( |
| 299 |
[ |
| 300 |
'width' => $border['custom']['top'], |
| 301 |
'style' => $border['style'], |
| 302 |
'color' => $border['color'], |
| 303 |
] |
| 304 |
), |
| 305 |
'border-right' => self::get_border_style( |
| 306 |
[ |
| 307 |
'width' => $border['custom']['right'], |
| 308 |
'style' => $border['style'], |
| 309 |
'color' => $border['color'], |
| 310 |
] |
| 311 |
), |
| 312 |
'border-bottom' => self::get_border_style( |
| 313 |
[ |
| 314 |
'width' => $border['custom']['bottom'], |
| 315 |
'style' => $border['style'], |
| 316 |
'color' => $border['color'], |
| 317 |
] |
| 318 |
), |
| 319 |
'border-left' => self::get_border_style( |
| 320 |
[ |
| 321 |
'width' => $border['custom']['left'], |
| 322 |
'style' => $border['style'], |
| 323 |
'color' => $border['color'], |
| 324 |
] |
| 325 |
), |
| 326 |
] |
| 327 |
); |
| 328 |
}//end if |
| 329 |
return ''; |
| 330 |
} |
| 331 |
|
| 332 |
public static function get_border_style( $border, $unit = 'px' ) { |
| 333 |
$unit = esc_attr( $unit ); |
| 334 |
return sprintf( |
| 335 |
'%d%s %s %s', |
| 336 |
$border['width'], |
| 337 |
$unit, |
| 338 |
$border['style'], |
| 339 |
$border['color'] |
| 340 |
); |
| 341 |
} |
| 342 |
|
| 343 |
public static function sanitize_elements_recursive( $elements ) { |
| 344 |
if ( ! is_array( $elements ) ) { |
| 345 |
return []; |
| 346 |
} |
| 347 |
|
| 348 |
foreach ( $elements as &$element ) { |
| 349 |
if ( isset( $element['data']['rich_text'] ) ) { |
| 350 |
$element['data']['rich_text'] = yaymail_kses_post( $element['data']['rich_text'], $allowed ); |
| 351 |
} |
| 352 |
|
| 353 |
if ( isset( $element['data']['title'] ) ) { |
| 354 |
$element['data']['title'] = yaymail_kses_post( $element['data']['title'] ); |
| 355 |
} |
| 356 |
|
| 357 |
// Recursive cho nested elements |
| 358 |
if ( isset( $element['children'] ) ) { |
| 359 |
$element['children'] = self::sanitize_elements_recursive( $element['children'] ); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
return $elements; |
| 364 |
} |
| 365 |
} |
| 366 |
|