PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.5
YayMail – WooCommerce Email Customizer v4.4.5
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / src / Elements / ElementsLoader.php

ElementsLoader.php in YayMail – WooCommerce Email Customizer 4.4.5, at src/Elements/ElementsLoader.php

184 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Elements;
4
5 use YayMail\Abstracts\BaseElement;
6 use YayMail\Shortcodes\ShortcodesExecutor;
7 use YayMail\Utils\SingletonTrait;
8 use YayMail\Utils\TemplateHelpers;
9
10 /**
11 * Email Loader Class
12 *
13 * @method static ElementsLoader get_instance()
14 */
15 class ElementsLoader {
16
17 use SingletonTrait;
18
19 private $elements = [];
20
21 private function __construct() {
22 $dir = new \DirectoryIterator( YAYMAIL_PLUGIN_PATH . '/src/Elements' );
23 foreach ( $dir as $fileinfo ) {
24 if ( ! $fileinfo->isDot() ) {
25 $file_name = $fileinfo->getFilename();
26 $class_name = basename( $file_name, '.php' );
27 $class = 'YayMail\\Elements\\' . $class_name;
28 if ( __CLASS__ === $class || 'ElementsHelper' === $class_name ) {
29 continue;
30 }
31 if ( class_exists( $class ) ) {
32 $instance = $class::get_instance();
33 $this->register_element( $instance );
34 }
35 }
36 }
37
38 do_action( 'yaymail_register_elements', $this );
39 }
40
41 public function register_element( $element ) {
42 if ( ! ( $element instanceof BaseElement ) ) {
43 return;
44 }
45 $this->elements[] = $element;
46 }
47
48 public function get_all() {
49 return $this->elements;
50 }
51
52 public function get_element_instance_by_type( $type ) {
53 foreach ( $this->elements as $element ) {
54 if ( $element->get_type() === $type ) {
55 return $element;
56 }
57 }
58 return null;
59 }
60
61 public static function load_elements( $elements ) {
62 $content = [];
63 if ( ! is_array( $elements ) ) {
64 return [];
65 }
66 foreach ( $elements as $element ) {
67 if ( isset( $element['integration'] ) && '3rd' === $element['integration'] ) {
68 $class = 'YayMail\Integrations\\' . $element['type'];
69 } elseif ( ! empty( $element['addon_namespace'] ) ) {
70 $class = $element['addon_namespace'] . '\\Elements\\' . $element['type'];
71 } elseif ( ! empty( $element['caller_class'] ) ) {
72 $class = $element['caller_class'];
73 } else {
74 $class = 'YayMail\Elements\\' . $element['type'];
75 }
76 if ( ! class_exists( $class ) ) {
77 continue;
78 }
79
80 $attributes = isset( $element['attributes'] ) ? $element['attributes'] : [];
81 $element_info = $class::get_data( $attributes );
82
83 // Map data to get the 'default_value' only
84 $mapped_data = array_map(
85 function( $attribute ) {
86 // If the attribute has default value, get the default value
87 // Else leave the whole value as is
88 return $attribute['default_value'] ?? $attribute;
89 },
90 $element_info['data']
91 );
92 $element_info['data'] = $mapped_data;
93
94 // Remove unneeded attributes
95 $unneeded_attributes = [ 'icon', 'group', 'position' ];
96 foreach ( $unneeded_attributes as $attribute ) {
97 unset( $element_info[ $attribute ] );
98 }
99
100 $content[] = $element_info;
101 }//end foreach
102 return $content;
103 }
104
105 /**
106 * Render list elements
107 *
108 * @param $args includes
109 * $render_data
110 * $template
111 * $settings
112 * $is_nested
113 * ...
114 */
115 public static function render_elements( $elements, $args ) {
116 $is_nested = isset( $args['is_nested'] ) ? $args['is_nested'] : false;
117 $is_horizontal = isset( $args['is_horizontal'] ) ? $args['is_horizontal'] : false;
118 $template_name = $args['template']->get_name();
119
120 $shortcodes = yaymail_get_email_shortcodes( $template_name );
121
122 // if ( ! empty( $args['render_data']['order'] ) ) {
123 // $order = $args['render_data']['order'];
124 // if ( $order instanceof \WC_Order ) {
125 // $order_id = $order->get_id();
126 // } elseif ( is_numeric( $order ) ) {
127 // $order_id = $order;
128 // } else {
129 // $order_id = null;
130 // }
131 // $shortcodes = apply_filters( 'yaymail_extra_shortcodes', $shortcodes, $template_name, $order_id );
132 // }
133
134 $args = apply_filters( 'yaymail_template_rendering_args', $args, $template_name, $elements );
135
136 foreach ( $elements as $element ) {
137 if ( empty( $element['available'] ) ) {
138 continue;
139 }
140
141 if ( ! apply_filters( 'yaymail_validate_element_before_sending', true, $element, $args ) ) {
142 continue;
143 }
144
145 $args['element'] = $element;
146
147 new ShortcodesExecutor( $shortcodes, $args );
148
149 $element_instance = yaymail_get_element( $element['type'] );
150
151 $layout = '';
152 if ( $element_instance ) {
153 $layout = $element_instance->get_layout( $element, $args );
154 }
155
156 $layout = TemplateHelpers::remove_empty_shortcodes( $layout );
157
158 /**
159 * Render Column content
160 */
161 if ( 'column' === $element['type'] || $is_nested ) {
162 yaymail_kses_post_e( $layout );
163 continue;
164 }
165
166 /**
167 * Render Container content
168 */
169 if ( $is_horizontal ) {
170 $width = count( $elements ) > 1 ? round( 100 / count( $elements ), 2 ) : 100;
171 ?>
172 <td style="padding: 0; width: <?php echo esc_attr( $width ); ?>%; max-width: <?php echo esc_attr( $width ); ?>%;padding-left: 0;padding-right: 0;"><?php yaymail_kses_post_e( $layout ); ?></td>
173 <?php
174 continue;
175 }
176 ?>
177 <tr>
178 <td style="padding: 0;"><?php yaymail_kses_post_e( $layout ); ?></td>
179 </tr>
180 <?php
181 }//end foreach
182 }
183 }
184