PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.0
YayMail – WooCommerce Email Customizer v4.4.0
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 3.4.2 3.4.3 All 55 releases
yaymail / src / Elements / ElementsLoader.php

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

194 lines 6.2 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 $emails = yaymail_get_emails();
41
42 foreach ( $this->elements as $element ) {
43 foreach ( $emails as $email ) {
44 if ( $element->is_available_in_email( $email ) ) {
45 $email->register_element( $element );
46 }
47 }
48 }
49 }
50
51 public function register_element( $element ) {
52 if ( ! ( $element instanceof BaseElement ) ) {
53 return;
54 }
55 $this->elements[] = $element;
56 }
57
58 public function get_all() {
59 return $this->elements;
60 }
61
62 public function get_element_instance_by_type( $type ) {
63 foreach ( $this->elements as $element ) {
64 if ( $element->get_type() === $type ) {
65 return $element;
66 }
67 }
68 return null;
69 }
70
71 public static function load_elements( $elements ) {
72 $content = [];
73 if ( ! is_array( $elements ) ) {
74 return [];
75 }
76 foreach ( $elements as $element ) {
77 if ( isset( $element['integration'] ) && '3rd' === $element['integration'] ) {
78 $class = 'YayMail\Integrations\\' . $element['type'];
79 } elseif ( ! empty( $element['addon_namespace'] ) ) {
80 $class = $element['addon_namespace'] . '\\Elements\\' . $element['type'];
81 } elseif ( ! empty( $element['caller_class'] ) ) {
82 $class = $element['caller_class'];
83 } else {
84 $class = 'YayMail\Elements\\' . $element['type'];
85 }
86 if ( ! class_exists( $class ) ) {
87 continue;
88 }
89
90 $attributes = isset( $element['attributes'] ) ? $element['attributes'] : [];
91 $element_info = $class::get_data( $attributes );
92
93 // Map data to get the 'default_value' only
94 $mapped_data = array_map(
95 function( $attribute ) {
96 // If the attribute has default value, get the default value
97 // Else leave the whole value as is
98 return $attribute['default_value'] ?? $attribute;
99 },
100 $element_info['data']
101 );
102 $element_info['data'] = $mapped_data;
103
104 // Remove unneeded attributes
105 $unneeded_attributes = [ 'icon', 'group', 'position' ];
106 foreach ( $unneeded_attributes as $attribute ) {
107 unset( $element_info[ $attribute ] );
108 }
109
110 $content[] = $element_info;
111 }//end foreach
112 return $content;
113 }
114
115 /**
116 * Render list elements
117 *
118 * @param $args includes
119 * $render_data
120 * $template
121 * $settings
122 * $is_nested
123 * ...
124 */
125 public static function render_elements( $elements, $args ) {
126 $is_nested = isset( $args['is_nested'] ) ? $args['is_nested'] : false;
127 $is_horizontal = isset( $args['is_horizontal'] ) ? $args['is_horizontal'] : false;
128 $template_name = $args['template']->get_name();
129
130 $shortcodes = yaymail_get_email_shortcodes( $template_name );
131
132 // if ( ! empty( $args['render_data']['order'] ) ) {
133 // $order = $args['render_data']['order'];
134 // if ( $order instanceof \WC_Order ) {
135 // $order_id = $order->get_id();
136 // } elseif ( is_numeric( $order ) ) {
137 // $order_id = $order;
138 // } else {
139 // $order_id = null;
140 // }
141 // $shortcodes = apply_filters( 'yaymail_extra_shortcodes', $shortcodes, $template_name, $order_id );
142 // }
143
144 $args = apply_filters( 'yaymail_template_rendering_args', $args, $template_name, $elements );
145
146 foreach ( $elements as $element ) {
147 if ( empty( $element['available'] ) ) {
148 continue;
149 }
150
151 if ( ! apply_filters( 'yaymail_validate_element_before_sending', true, $element, $args ) ) {
152 continue;
153 }
154
155 $args['element'] = $element;
156
157 new ShortcodesExecutor( $shortcodes, $args );
158
159 $element_instance = yaymail_get_element( $element['type'] );
160
161 $layout = '';
162 if ( $element_instance ) {
163 $layout = $element_instance->get_layout( $element, $args );
164 }
165
166 $layout = TemplateHelpers::remove_empty_shortcodes( $layout );
167
168 /**
169 * Render Column content
170 */
171 if ( 'column' === $element['type'] || $is_nested ) {
172 yaymail_kses_post_e( $layout );
173 continue;
174 }
175
176 /**
177 * Render Container content
178 */
179 if ( $is_horizontal ) {
180 $width = count( $elements ) > 1 ? round( 100 / count( $elements ), 2 ) : 100;
181 ?>
182 <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>
183 <?php
184 continue;
185 }
186 ?>
187 <tr>
188 <td style="padding: 0;"><?php yaymail_kses_post_e( $layout ); ?></td>
189 </tr>
190 <?php
191 }//end foreach
192 }
193 }
194