PluginProbe
YayMail – WooCommerce Email Customizer / trunk
YayMail – WooCommerce Email Customizer vtrunk
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 / Shortcodes / OrderMetaShortcodes.php

OrderMetaShortcodes.php in YayMail – WooCommerce Email Customizer trunk, at src/Shortcodes/OrderMetaShortcodes.php

319 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Shortcodes;
4
5 use YayMail\Utils\Helpers;
6 use YayMail\Utils\SingletonTrait;
7 use YayMail\Utils\Logger;
8 /**
9 * @method: static OrderMetaShortcodes init()
10 */
11 class OrderMetaShortcodes {
12
13 use SingletonTrait;
14
15 private $logger;
16
17 protected function __construct() {
18 $this->logger = new Logger();
19 add_filter( 'yaymail_extra_shortcodes', [ $this, 'get_order_meta_shortcodes' ], 10, 2 );
20 add_filter( 'yaymail_extra_shortcodes', [ $this, 'get_order_tax_shortcodes' ], 10, 2 );
21 }
22
23 public function get_order_tax_shortcodes( $shortcodes, $data ) {
24 $order = $data['render_data']['order'] ?? null;
25
26 if ( ! $order ) {
27 return $shortcodes;
28 }
29
30 $tax_items = $order->get_items( 'tax' );
31
32 foreach ( $tax_items as $item_id => $item_tax ) {
33 $tax_rate_id = $item_tax->get_rate_id();
34 $new_shortcode = [
35 'name' => "yaymail_order_taxes_{$tax_rate_id}",
36 'description' => $item_tax->get_label(),
37 'group' => 'order_taxes',
38 'callback' => [ $this, 'order_tax_callback' ],
39 'callback_args' => [
40 'item_tax' => $item_tax,
41 ],
42 ];
43 $shortcodes[] = $new_shortcode;
44 }
45
46 return $shortcodes;
47 }
48
49 public function order_tax_callback( $data, $shortcode_attrs = [] ) {
50 $item_tax = $data['item_tax'] ?? '';
51
52 if ( empty( $item_tax ) || ! is_object( $item_tax ) ) {
53 return '';
54 }
55
56 $tax_amount_total = $item_tax->get_tax_total();
57 $tax_shipping_total = $item_tax->get_shipping_tax_total();
58 $totals_taxes = $tax_amount_total + $tax_shipping_total;
59 return wc_price( $totals_taxes );
60 }
61
62 /**
63 * Init order meta shortcodes
64 *
65 * @param array $shortcodes The shortcodes array.
66 * @param array $data The data array.
67 *
68 * @return array The shortcodes array.
69 */
70 public function get_order_meta_shortcodes( $shortcodes, $data ) {
71 $order = $data['render_data']['order'] ?? null;
72
73 if ( ! $order && ! empty( $data['render_data']['order_id'] ) ) {
74 $order = wc_get_order( $data['render_data']['order_id'] );
75 }
76
77 if ( empty( $order ) ) {
78 return $shortcodes;
79 }
80
81 $metadata = self::get_allowed_order_meta_data( $order );
82
83 foreach ( $metadata as $meta_item ) {
84 $data = $meta_item->get_data();
85
86 $field = Helpers::to_snake_case( $data['key'] );
87
88 $description = Helpers::snake_case_to_capitalized_words( $field ) . ' (' . $field . ')';
89
90 $callback = 'order_meta_callback';
91 if ( $field === 'pickup_date' || $field === 'delivery_date' ) {
92 $callback = 'date_meta_callback';
93 }
94
95 $new_shortcode = [
96 'name' => "yaymail_order_meta:{$field}",
97 'description' => $description,
98 'group' => 'order_meta',
99 'callback' => [ $this, $callback ],
100 'callback_args' => [
101 'meta_item' => $meta_item,
102 'field' => $field,
103 ],
104 'attributes' => [
105 'is_date' => false,
106 ],
107 ];
108
109 $shortcodes[] = $new_shortcode;
110 }//end foreach
111
112 return $shortcodes;
113 }
114
115 public function order_meta_callback( $data, $shortcode_attrs = [] ) {
116 $field = $data['field'] ?? '';
117 $meta_item = $data['meta_item'] ?? '';
118
119 if ( empty( $meta_item ) || ! method_exists( $meta_item, 'get_data' ) ) {
120 return '';
121 }
122
123 $meta_data = $meta_item->get_data();
124 $value = $meta_data['value'];
125
126 if ( ! empty( $shortcode_attrs['is_date'] ) ) {
127 $date = is_numeric( $value )
128 ? \DateTime::createFromFormat( 'U', $value )
129 : \DateTime::createFromFormat( 'Ymd', $value );
130
131 if ( $date ) {
132 return date_i18n( function_exists( 'wc_date_format' ) ? wc_date_format() : get_option( 'date_format' ), $date->getTimestamp() );
133 }
134
135 $this->logger->log( "Order meta shortcode: field {$field} with value {$value} is not a valid date" );
136 return nl2br( $value );
137 }
138
139 if ( is_array( $value ) || is_object( $value ) ) {
140 if ( is_object( $value ) ) {
141 $value = (array) $value;
142 }
143 $str = isset( $value['extra'] ) && ( is_string( $value['extra'] ) || is_numeric( $value['extra'] ) )
144 ? $value['extra']
145 : $this->yaymail_flatten_to_string( $value );
146 return str_replace( '|', '<br />', $str );
147 }
148
149 return $value;
150 }
151
152 public function date_meta_callback( $data ) {
153 $meta_item = $data['meta_item'] ?? '';
154
155 if ( empty( $meta_item ) || ! method_exists( $meta_item, 'get_data' ) ) {
156 return '';
157 }
158
159 $meta_data = $meta_item->get_data();
160 $value = $meta_data['value'];
161
162 if ( ! is_string( $value ) ) {
163 return $value;
164 }
165
166 if ( strtotime( $value ) ) {
167 return date_i18n( function_exists( 'wc_date_format' ) ? wc_date_format() : get_option( 'date_format' ), strtotime( $value ) );
168 }
169
170 return $value;
171 }
172
173 protected function yaymail_flatten_to_string( $value ) {
174 if ( is_array( $value ) || is_object( $value ) ) {
175 $result = [];
176 foreach ( (array) $value as $v ) {
177 $result[] = $this->yaymail_flatten_to_string( $v );
178 }
179 return implode( ', ', $result );
180 }
181 return strval( $value );
182 }
183
184 /**
185 * Get allowed order meta data
186 * Allow 3rd party to filter the which order meta data to be used in the email
187 *
188 * @since 4.1.0
189 */
190 public static function get_allowed_order_meta_data( $order ) {
191
192 if ( is_string( $order ) || is_numeric( $order ) ) {
193 $order = wc_get_order( $order );
194 }
195
196 if ( ! $order && ! ( $order instanceof \WC_Order ) ) {
197 return [];
198 }
199
200 $order_meta_data = $order->get_meta_data();
201
202 return $order_meta_data;
203
204 // Maybe we can use this in the future
205 // $order_id = $order->get_id();
206
207 // // ACF integration
208 // if ( function_exists( 'acf_is_field_key' ) ) {
209 // $order_meta_data = array_filter(
210 // $order_meta_data,
211 // function( $meta_item ) use ( $order_id ) {
212 // $data = $meta_item->get_data();
213 // $search_key = strpos( $data['key'], '_' ) === 0 ? $data['key'] : '_' . $data['key'];
214
215 // $acf_ref = get_post_meta( $order_id, $search_key, true );
216
217 // if ( ! $acf_ref && strpos( $acf_ref, 'field_' ) !== 0 ) {
218 // return true;
219 // }
220
221 // return ! acf_is_field_key( $acf_ref );
222 // }
223 // );
224 // }
225
226 // // Checkout fields by ThemeHigh
227 // if ( class_exists( 'THWCFD_Utils' ) ) {
228
229 // $thwcfd_data = [
230 // \THWCFD_Utils::get_fields( 'billing' ),
231 // \THWCFD_Utils::get_fields( 'shipping' ),
232 // \THWCFD_Utils::get_fields( 'additional' ),
233 // ];
234
235 // $custom_fields_sets = [];
236
237 // foreach ( $thwcfd_data as $set ) {
238 // $custom_fields = [];
239 // foreach ( array_keys( $set ) as $field_name ) {
240 // $custom_fields[] = '_' . $field_name;
241 // $custom_fields[] = $field_name;
242 // }
243 // $custom_fields_sets[] = $custom_fields;
244 // }
245
246 // foreach ( $custom_fields_sets as $custom_fields ) {
247 // if ( empty( $custom_fields ) ) {
248 // continue;
249 // }
250 // $order_meta_data = array_filter(
251 // $order_meta_data,
252 // function( $meta_item ) use ( $custom_fields ) {
253 // $data = $meta_item->get_data();
254 // return ! in_array( $data['key'], $custom_fields );
255 // }
256 // );
257 // }
258 // }//end if
259
260 // // Checkout fields editor by WooCommerce
261 // if ( class_exists( 'WC_Checkout_Field_Editor' ) && method_exists( 'WC_Checkout_Field_Editor', 'get_fields' ) ) {
262 // $custom_fields_sets = [
263 // \WC_Checkout_Field_Editor::get_fields( 'billing' ),
264 // \WC_Checkout_Field_Editor::get_fields( 'shipping' ),
265 // \WC_Checkout_Field_Editor::get_fields( 'additional' ),
266 // ];
267
268 // foreach ( $custom_fields_sets as $custom_fields ) {
269 // if ( empty( $custom_fields ) ) {
270 // continue;
271 // }
272 // $custom_fields = array_filter(
273 // $custom_fields,
274 // function( $field ) {
275 // return ! empty( $field['custom'] );
276 // }
277 // );
278 // $order_meta_data = array_filter(
279 // $order_meta_data,
280 // function( $meta_item ) use ( $custom_fields ) {
281 // $data = $meta_item->get_data();
282 // return ! in_array( $data['key'], array_keys( $custom_fields ) );
283 // }
284 // );
285 // }
286 // }//end if
287
288 // // Checkout fields by Acoweb
289 // if ( defined( 'AWCFE_ORDER_META_KEY' ) ) {
290 // $awcf_data = $order->get_meta( AWCFE_ORDER_META_KEY, true );
291 // $custom_fields_sets = [];
292
293 // foreach ( $awcf_data as $set ) {
294 // $custom_fields = [];
295 // foreach ( $set as $field ) {
296 // $custom_fields[] = '_' . $field['name'];
297 // }
298 // $custom_fields[] = AWCFE_ORDER_META_KEY;
299 // $custom_fields_sets[] = $custom_fields;
300 // }
301
302 // foreach ( $custom_fields_sets as $custom_fields ) {
303 // if ( empty( $custom_fields ) ) {
304 // continue;
305 // }
306 // $order_meta_data = array_filter(
307 // $order_meta_data,
308 // function( $meta_item ) use ( $custom_fields ) {
309 // $data = $meta_item->get_data();
310 // return ! in_array( $data['key'], $custom_fields );
311 // }
312 // );
313 // }
314 // }//end if
315
316 // return $order_meta_data;
317 }
318 }
319