| 1 |
<?php |
| 2 |
namespace YayMail\Abstracts; |
| 3 |
|
| 4 |
/** |
| 5 |
* BaseElement Class |
| 6 |
*/ |
| 7 |
abstract class BaseElement { |
| 8 |
|
| 9 |
protected static $type = null; |
| 10 |
|
| 11 |
/** |
| 12 |
* List of available email IDs |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
public $available_email_ids = [ YAYMAIL_WITH_ORDER_EMAILS ]; |
| 17 |
|
| 18 |
public static $icon = '<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" |
| 19 |
viewBox="0 0 24 24" style="enable-background:new 0 0 24 24;" xml:space="preserve"> |
| 20 |
<title>Third party elements</title> |
| 21 |
<path d="M18,2h-7.6c-0.1,0-0.1,0-0.2,0.1l0,0c-0.1,0-0.1,0.1-0.2,0.1L4.6,7.4c0,0-0.1,0.1-0.1,0.2l0,0c0,0.1,0,0.1-0.1,0.2l0,0 |
| 22 |
c0,0.1,0,0.1,0,0.2v12.5C4.4,21.3,5.1,22,6,22h12c0.9,0,1.6-0.7,1.6-1.6l0,0V3.6C19.6,2.7,18.9,2,18,2L18,2z M9.8,4.5v2.7H7L9.8,4.5 |
| 23 |
z M18.1,20.3c0,0.1-0.1,0.2-0.2,0.2c0,0,0,0,0,0H6.1c-0.1,0-0.2-0.1-0.2-0.2c0,0,0,0,0,0V8.7h4.6c0.4,0,0.7-0.3,0.8-0.7V3.5h6.6 |
| 24 |
c0.1,0,0.2,0.1,0.2,0.2c0,0,0,0,0,0L18.1,20.3z M7.4,11.6c0-0.4,0.3-0.7,0.7-0.7c0,0,0,0,0,0h4.8c0.4,0,0.8,0.2,0.8,0.7 |
| 25 |
c0,0.4-0.2,0.8-0.7,0.8c-0.1,0-0.1,0-0.2,0H8.1C7.7,12.4,7.4,12.1,7.4,11.6C7.4,11.7,7.4,11.7,7.4,11.6z M16.6,14.6 |
| 26 |
c0,0.4-0.3,0.7-0.8,0.8H8.1c-0.4,0-0.8-0.3-0.8-0.8s0.3-0.8,0.8-0.8h7.7C16.3,13.9,16.6,14.2,16.6,14.6z M8.6,17.6 |
| 27 |
c0,0.4-0.3,0.7-0.7,0.7S7.2,18,7.2,17.6s0.3-0.7,0.7-0.7S8.6,17.2,8.6,17.6z M11,17.6c0,0.4-0.3,0.7-0.7,0.7S9.6,18,9.6,17.6 |
| 28 |
c0-0.4,0.3-0.7,0.7-0.7C10.7,16.9,11,17.2,11,17.6L11,17.6z M13.4,17.6c0,0.4-0.2,0.7-0.6,0.8c-0.4,0-0.7-0.2-0.8-0.6 |
| 29 |
c0-0.1,0-0.1,0-0.2c0-0.4,0.2-0.7,0.6-0.8c0.4,0,0.7,0.2,0.8,0.6C13.4,17.5,13.4,17.5,13.4,17.6z"/> |
| 30 |
</svg>'; |
| 31 |
|
| 32 |
abstract public static function get_data( $attributes = [] ); |
| 33 |
/** |
| 34 |
* Render list elements |
| 35 |
* |
| 36 |
* @param $element Element data |
| 37 |
* @param $args includes |
| 38 |
* $render_data |
| 39 |
* $template |
| 40 |
* $is_nested |
| 41 |
*/ |
| 42 |
public static function get_layout( $element_data, $args ) { |
| 43 |
$file_name = str_replace( '_', '-', static::$type ); |
| 44 |
$path = 'templates/elements/' . $file_name . '.php'; |
| 45 |
return apply_filters( 'yaymail_' . static::$type . '_layout', yaymail_get_content( $path, array_merge( [ 'element' => $element_data ], $args ) ), $element_data, $args ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Check available in email. |
| 50 |
* |
| 51 |
* @param string|array $email_ids The email ID(s) to be set as available. Special placeholders such as |
| 52 |
* `NON_ORDER_EMAILS`, `WITH_ORDER_EMAILS`, and `GLOBAL_HEADER_FOOTER_ID` can be used to include predefined sets |
| 53 |
* of email IDs or the global header/footer ID. |
| 54 |
* If provided as a string, it represents a single email ID. |
| 55 |
* If provided as an array, it represents multiple email IDs. |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function is_available_in_email( $email ) { |
| 59 |
|
| 60 |
if ( ! ( $email instanceof BaseEmail ) && ! is_string( $email ) ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
if ( is_string( $email ) ) { |
| 65 |
|
| 66 |
$email = yaymail_get_email( $email ); |
| 67 |
|
| 68 |
if ( empty( $email ) || ! $email instanceof BaseEmail ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
$available_email_ids = (array) $this->available_email_ids; |
| 74 |
$available_email_ids = apply_filters( 'yaymail_element_available_email_ids', $available_email_ids, $this ); |
| 75 |
|
| 76 |
return ( in_array( YAYMAIL_ALL_EMAILS, $available_email_ids, true ) ) |
| 77 |
|| ( in_array( YAYMAIL_NON_ORDER_EMAILS, $available_email_ids, true ) && in_array( YAYMAIL_NON_ORDER_EMAILS, $email->email_types, true ) ) |
| 78 |
|| ( in_array( YAYMAIL_WITH_ORDER_EMAILS, $available_email_ids, true ) && in_array( YAYMAIL_WITH_ORDER_EMAILS, $email->email_types, true ) ) |
| 79 |
|| ( in_array( YAYMAIL_GLOBAL_HEADER_FOOTER_ID, $available_email_ids, true ) && in_array( YAYMAIL_GLOBAL_HEADER_FOOTER_ID, $email->email_types, true ) ) |
| 80 |
|| in_array( $email->get_id(), $available_email_ids, true ); |
| 81 |
} |
| 82 |
|
| 83 |
public static function get_type() { |
| 84 |
return static::$type; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* "This element is available in {brand} Pro" text for PRO-only elements' |
| 89 |
* disabled_reason. Brand name follows the email currently being edited (set |
| 90 |
* via the $yaymail_current_email_id global in |
| 91 |
* yaymail_get_email_elements_data()) -- WordPress-platform emails use the |
| 92 |
* 'wp-core-' id prefix. |
| 93 |
* |
| 94 |
* @return string |
| 95 |
*/ |
| 96 |
public static function get_pro_upgrade_text() { |
| 97 |
global $yaymail_current_email_id; |
| 98 |
$is_wp_platform_email = ! empty( $yaymail_current_email_id ) |
| 99 |
&& ( 0 === strpos( $yaymail_current_email_id, 'wp-core-' ) || 0 === strpos( $yaymail_current_email_id, 'wp_' ) ); |
| 100 |
$brand_name = $is_wp_platform_email ? 'Email Builder' : 'YayMail'; |
| 101 |
|
| 102 |
return sprintf( |
| 103 |
// translators: %s: brand name (YayMail or Email Builder) |
| 104 |
__( 'This element is available in %s Pro', 'yaymail' ), |
| 105 |
$brand_name |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Converts an element's data to use default values. |
| 111 |
* |
| 112 |
* This function iterates through the 'data' array of an element, replacing each value with its 'default_value'. |
| 113 |
* It then updates the element's 'data' with the formatted data and returns the modified element. |
| 114 |
* |
| 115 |
* @param array $element The element to be modified. |
| 116 |
* @return array The modified element with default values applied. |
| 117 |
*/ |
| 118 |
public static function reduce_new_element( $element ) { |
| 119 |
$data = $element['data']; |
| 120 |
$formatted_data = []; |
| 121 |
foreach ( $data as $key => $value ) { |
| 122 |
$fallback_value = $value; |
| 123 |
if ( isset( $value['value_path'] ) ) { |
| 124 |
$fallback_value = ''; |
| 125 |
} |
| 126 |
$formatted_data[ $key ] = $value['default_value'] ?? $fallback_value; |
| 127 |
} |
| 128 |
|
| 129 |
$element['data'] = $formatted_data; |
| 130 |
return $element; |
| 131 |
} |
| 132 |
|
| 133 |
public static function merge_common_data( $element, $attributes = [] ) { |
| 134 |
if ( empty( $attributes['custom_css_classes'] ) ) { |
| 135 |
return $element; |
| 136 |
} |
| 137 |
|
| 138 |
$element['data']['custom_css_classes'] = $attributes['custom_css_classes']; |
| 139 |
|
| 140 |
return $element; |
| 141 |
} |
| 142 |
|
| 143 |
public static function get_object_data( ...$args ) { |
| 144 |
return self::reduce_new_element( static::get_data( ...$args ) ); |
| 145 |
} |
| 146 |
} |
| 147 |
|