PluginProbe
MailerLite – WooCommerce integration / 2.0.9
MailerLite – WooCommerce integration v2.0.9
3.1.27 3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 All 148 releases
woo-mailerlite / includes / hooks.php

hooks.php in MailerLite – WooCommerce integration 2.0.9, at includes/hooks.php

331 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MailerLite\Includes\Classes\Process\CartProcess;
4 use MailerLite\Includes\Classes\Process\OrderProcess;
5 use MailerLite\Includes\Classes\Process\OrderSyncProcess;
6 use MailerLite\Includes\Classes\Process\ProductProcess;
7 use MailerLite\Includes\Classes\Settings\MailerLiteSettings;
8 use MailerLite\Includes\Classes\Settings\SynchronizeSettings;
9
10
11 /**
12 * Shows the final purchase total at the bottom of the checkout page
13 *
14 * @return void
15 * @since 1.5
16 */
17 function woo_ml_checkout_label()
18 {
19 if ( ! MailerLiteSettings::getInstance()->isActive()) {
20 return;
21 }
22
23 $checkout = MailerLiteSettings::getInstance()->getMlOption('checkout', 'no');
24
25 if ('yes' != $checkout) {
26 return;
27 }
28
29 $group = MailerLiteSettings::getInstance()->getMlOption('group');
30
31 if (empty($group)) {
32 return;
33 }
34
35 $label = MailerLiteSettings::getInstance()->getMlOption('checkout_label');
36 $preselect = MailerLiteSettings::getInstance()->getMlOption('checkout_preselect', 'no');
37 $hidden = MailerLiteSettings::getInstance()->getMlOption('checkout_hide', 'no');
38
39 if ('yes' === $hidden) {
40 ?>
41 <input name="woo_ml_subscribe" type="hidden" id="woo_ml_subscribe" value="1" checked="checked"/>
42 <?php
43 } else {
44
45 woocommerce_form_field('woo_ml_subscribe', array(
46 'type' => 'checkbox',
47 'label' => __($label, 'woo-mailerlite'),
48 'checked' => (bool) ($_COOKIE['mailerlite_accepts_marketing'] ?? 'yes' === $preselect) ? 'checked' : ''
49 ), (bool) ($_COOKIE['mailerlite_accepts_marketing'] ?? 'yes' === $preselect));
50 }
51 }
52
53 $checkout_position = MailerLiteSettings::getInstance()->getMlOption('checkout_position', 'checkout_billing');
54 $checkout_position_hook = 'woocommerce_' . $checkout_position;
55
56 if ($checkout_position !== 'checkout_billing_email') {
57 add_action($checkout_position_hook, 'woo_ml_checkout_label', 20);
58 }
59
60 /**
61 * Remove (optional) string for ML label
62 *
63 */
64 function remove_ml_checkout_optional_text($field, $key, $args, $value)
65 {
66
67 if (is_checkout() && ! is_wc_endpoint_url() && strpos($field,
68 'woo_ml_subscribe') !== false && get_option('ml_account_authenticated')) {
69
70 $optional = '&nbsp;<span class="optional">(' . esc_html__('optional', 'woocommerce') . ')</span>';
71 $field = str_replace($optional, '', $field);
72 }
73
74 return $field;
75 }
76
77 add_filter('woocommerce_form_field', 'remove_ml_checkout_optional_text', 10, 4);
78
79 /**
80 * Maybe prepare signup
81 *
82 * @param $order_id
83 */
84 function woo_ml_checkout_maybe_prepare_signup($order_id)
85 {
86
87 if (isset($_POST['woo_ml_subscribe']) && '1' == $_POST['woo_ml_subscribe']) {
88 OrderProcess::getInstance()->setOrderCustomerSubscribe($order_id);
89 }
90 }
91
92 add_action('woocommerce_checkout_update_order_meta', 'woo_ml_checkout_maybe_prepare_signup');
93
94 /**
95 * Process order completed (and finally paid)
96 *
97 * @param $order_id
98 */
99 function woo_ml_process_order_completed($order_id)
100 {
101
102 if ( ! woo_ml_integration_setup_completed()) {
103 woo_ml_setup_integration();
104 }
105
106 if ( ! woo_ml_old_integration()) {
107 OrderProcess::getInstance()->sendCompletedOrder($order_id);
108 }
109
110 OrderProcess::getInstance()->processOrderTracking($order_id);
111 }
112
113 add_action('woocommerce_order_status_completed', 'woo_ml_process_order_completed');
114
115 function woo_ml_proceed_to_checkout()
116 {
117
118 if ( ! get_option('ml_account_authenticated')) {
119
120 return false;
121 }
122
123 if ( ! woo_ml_old_integration()) {
124 CartProcess::getInstance()->sendCart();
125 }
126 }
127
128 add_action('woocommerce_add_to_cart', 'woo_ml_proceed_to_checkout');
129 add_action('woocommerce_cart_item_removed', 'woo_ml_proceed_to_checkout');
130
131 function woo_ml_cart_action_updated($cart_updated)
132 {
133
134 woo_ml_proceed_to_checkout();
135
136 return $cart_updated;
137 }
138
139 add_filter('woocommerce_update_cart_action_cart_updated', 'woo_ml_cart_action_updated');
140
141 function woo_ml_order_status_change($order_id)
142 {
143 if ( ! woo_ml_old_integration()) {
144 OrderProcess::getInstance()->paymentStatusProcessing($order_id);
145 }
146 }
147
148 add_action('woocommerce_order_status_changed', 'woo_ml_order_status_change');
149
150
151 function woo_ml_enqueue_styles()
152 {
153
154 if ( ! get_option('ml_account_authenticated')) {
155
156 return false;
157 }
158
159 wp_enqueue_style('related-styles', plugins_url('/../public/css/style.css', __FILE__));
160 }
161
162 add_action('wp_enqueue_scripts', 'woo_ml_enqueue_styles');
163
164 /**
165 * WooCommerce product update
166 *
167 * @param $product_id
168 */
169 function woo_ml_product_update($product_id)
170 {
171
172 ProductProcess::getInstance()->wpSyncProduct($product_id);
173 }
174
175 add_action('woocommerce_update_product', 'woo_ml_product_update', 10, 1);
176
177 /**
178 * WooCommerce order update
179 *
180 * @param $order_id
181 */
182 function woo_ml_order_update($order_id, $order)
183 {
184
185 OrderSyncProcess::getInstance()->syncOrder($order_id, $order);
186 }
187
188 add_action('woocommerce_saved_order_items', 'woo_ml_order_update', 10, 2);
189
190 /**
191 * WooCommerce bulk order update
192 *
193 * @param $redirect_to
194 * @param $action
195 * @param $order_ids
196 */
197 function woo_ml_bulk_order_update($redirect_to, $action, $order_ids)
198 {
199
200 OrderSyncProcess::getInstance()->syncBulkOrder($action, $order_ids);
201
202 return $redirect_to;
203 }
204
205 add_filter('handle_bulk_actions-edit-shop_order', 'woo_ml_bulk_order_update', 10, 3);
206
207 /**
208 * WooCommerce order cancel
209 *
210 * @param $order_id
211 */
212 function woo_ml_order_cancel($order_id)
213 {
214
215 OrderProcess::getInstance()->cancelOrder($order_id);
216 }
217
218 add_action( 'woocommerce_order_status_cancelled', 'woo_ml_order_cancel', 21, 1 );
219
220 /**
221 * WP Post delete
222 *
223 * @param $post_id
224 */
225 function woo_ml_post_delete($post_id)
226 {
227
228 SynchronizeSettings::getInstance()->syncPostDelete($post_id);
229 }
230
231 add_action('delete_post', 'woo_ml_post_delete', 10);
232
233 function woo_ml_customer_update($customer_id, $customer_data = '')
234 {
235
236 mailerlite_wp_sync_ecommerce_customer($customer_id);
237 }
238
239 add_action('woocommerce_update_customer', 'woo_ml_customer_update', 99, 2);
240 add_action('user_register', 'woo_ml_customer_update', 10, 1);
241 add_action('profile_update', 'woo_ml_customer_update', 10, 2);
242
243 /**
244 * WP Product Category create/update
245 *
246 * @param $category_id
247 */
248 function woo_ml_product_category_sync($category_id)
249 {
250
251 ProductProcess::getInstance()->syncProductCategory($category_id);
252 }
253
254 add_action('created_product_cat', 'woo_ml_product_category_sync', 10, 2);
255 add_action('edited_product_cat', 'woo_ml_product_category_sync', 10, 2);
256
257 function woo_ml_product_category_delete($term, $tt_id, $deleted_term, $object_ids)
258 {
259
260 // Avoid auto save from calling API
261 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
262 return;
263 }
264
265 ProductProcess::getInstance()->deleteProductCategory($term);
266 }
267
268 add_action('delete_product_cat', 'woo_ml_product_category_delete', 10, 4);
269
270 function woo_ml_billing_checkout_fields($fields) {
271
272 if ( ! MailerLiteSettings::getInstance()->isActive()) {
273 return $fields;
274 }
275
276 $checkout = MailerLiteSettings::getInstance()->getMlOption('checkout', 'no');
277
278 if ('yes' != $checkout) {
279 return $fields;
280 }
281
282 $group = MailerLiteSettings::getInstance()->getMlOption('group');
283
284 if (empty($group)) {
285 return $fields;
286 }
287
288 $checkout_position = MailerLiteSettings::getInstance()->getMlOption('checkout_position', 'checkout_billing');
289
290 if ($checkout_position !== 'checkout_billing_email') {
291 return $fields;
292 }
293
294 $label = MailerLiteSettings::getInstance()->getMlOption('checkout_label');
295 $preselect = MailerLiteSettings::getInstance()->getMlOption('checkout_preselect', 'no');
296 $hidden = MailerLiteSettings::getInstance()->getMlOption('checkout_hide', 'no');
297
298 $new_billing_fields = [];
299
300 foreach ($fields['billing'] as $key => $field) {
301 $new_billing_fields[$key] = $field;
302
303 // Add the custom checkbox field right after 'billing_email'.
304 if ($key === 'billing_email') {
305
306 $new_billing_fields['woo_ml_subscribe'] = [
307 'type' => ($hidden === 'no') ? 'checkbox' : 'hidden',
308 'default' => (bool) ($_COOKIE['mailerlite_accepts_marketing'] ?? 'yes' === $preselect),
309 'required' => false,
310 ];
311 $new_billing_fields['woo_ml_preselect_enabled'] = [
312 'type' => 'hidden',
313 'default' => $preselect
314 ];
315 if ($hidden === 'no') {
316 $new_billing_fields['woo_ml_subscribe']['label'] = __($label, 'woo-mailerlite');
317 } else {
318 $new_billing_fields['woo_ml_subscribe']['custom_attributes'] = [
319 'checked' => 'checked',
320 ];
321 }
322 }
323 }
324
325 // Replace the 'billing' fields with the modified field order.
326 $fields['billing'] = $new_billing_fields;
327
328 return $fields;
329 }
330
331 add_filter('woocommerce_checkout_fields', 'woo_ml_billing_checkout_fields', PHP_INT_MAX);