PluginProbe
MailerLite – WooCommerce integration / 2.0
MailerLite – WooCommerce integration v2.0
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 2.1.0 All 147 releases
woo-mailerlite / includes / admin / hooks.php

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

345 lines 9.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\ProductProcess;
4 use MailerLite\Includes\Classes\Settings\MailerLiteSettings;
5 use MailerLite\Includes\Classes\Data\TrackingData;
6
7 /**
8 * Handle admin notices
9 */
10 function woo_ml_admin_notices()
11 {
12
13 $notices = array();
14
15 // Actions
16 $admin_notice = (isset($_GET['woo_ml_admin_notice'])) ? $_GET['woo_ml_admin_notice'] : null;
17
18 // Debug
19 /*
20 $notices[] = array(
21 'type' => 'warning',
22 'dismiss' => false,
23 'force' => false,
24 'message' => __('Plugin settings has been successfully reset.', 'woo-mailerlite')
25 );
26 */
27
28 // Integration setup (in case setup was not yet completed via settings handling)
29 if (MailerLiteSettings::getInstance()->isActive() && ! woo_ml_integration_setup_completed()) {
30 $notices[] = array(
31 'type' => 'warning',
32 'dismiss' => false,
33 'force' => true,
34 'message' => sprintf(wp_kses(__('In order to complete our integration setup, please <a href="%s">click here</a>.',
35 'woo-mailerlite'), array('a' => array('href' => array()))),
36 esc_url(TrackingData::getInstance()->getCompleteIntegrationSetupUrl()))
37 );
38 }
39
40 //message to be displayed for users who are only using the old functionalities just now
41 if (MailerLiteSettings::getInstance()->isActive() && woo_ml_shop_not_active()) {
42 $notices[] = array(
43 'type' => 'error',
44 'dismiss' => false,
45 'force' => true,
46 'message' => __('Your shop is currently not active. Please reconnect to MailerLite.', 'woo-mailerlite')
47 );
48 }
49
50 // Integration setup completed
51 if ('integration_setup_completed' === $admin_notice) {
52 $notices[] = array(
53 'type' => 'success',
54 'dismiss' => true,
55 'force' => false,
56 'message' => __('Integration setup has been successfully completed.', 'woo-mailerlite')
57 );
58 }
59
60 if (MailerLiteSettings::getInstance()->syncFailed()) {
61 $notices[] = array(
62 'type' => 'error',
63 'dismiss' => true,
64 'force' => true,
65 'message' => __('We did not manage to sync all of your shop resources. Please try again.', 'woo-mailerlite')
66 );
67 }
68
69 // Hook
70 $notices = apply_filters('woo_ml_admin_notices', $notices);
71
72 $is_plugin_area = true; // Maybe add a check here later
73
74 // Output messages
75 if (sizeof($notices) > 0) {
76 foreach ($notices as $notice_id => $notice) {
77
78 // Maybe showing the notice on plugin related admin pages only
79 if (isset($notice['force']) && false === $notice['force'] && ! $is_plugin_area) {
80 continue;
81 }
82
83 $classes = 'woo-ml-notice notice';
84
85 if ( ! empty($notice['type'])) {
86 $classes .= ' notice-' . $notice['type'];
87 }
88
89 if (isset($notice['dismiss']) && true === $notice['dismiss']) {
90 $classes .= ' is-dismissible';
91 }
92
93 ?>
94 <div id="woo-ml-notice-<?php echo ( ! empty($notice['id'])) ? $notice['id'] : $notice_id; ?>"
95 class="<?php echo $classes; ?>">
96 <p><strong>WooCommerce MailerLite:</strong> <?php echo $notice['message']; ?></p>
97 </div>
98 <?php
99 }
100 }
101 }
102
103 add_action('admin_notices', 'woo_ml_admin_notices');
104
105 /**
106 * Migration after updates
107 *
108 * @access private
109 * @return void
110 */
111 function woo_ml_migrations()
112 {
113
114 // get last installed version
115 $prev_version = get_option('woo_ml_version', false);
116
117 if ($prev_version !== false) {
118 return;
119 }
120
121 /**
122 * start migration to reset ml_data table data_value
123 * to store ignored products instead of all products
124 */
125
126 $settings = get_option('woocommerce_mailerlite_settings', array());
127
128 if ( ! isset($settings['ignore_product_list'])) {
129
130 $settings['ignore_product_list'] = [];
131 }
132
133 $products = [];
134
135 if ( ! empty($settings['ignore_product_list'])) {
136
137 $_pf = new WC_Product_Factory();
138
139 foreach ($settings['ignore_product_list'] as $product_id) {
140
141 $product = $_pf->get_product($product_id);
142
143 if ($product) {
144
145 $products[$product_id] = $product->get_title();
146 }
147 }
148 }
149
150 if (TrackingData::getInstance()->updateData($products) !== false) {
151 add_option('woo_ml_version', WOO_MAILERLITE_VER);
152 }
153 }
154
155 /**
156 * Handle admin actions
157 */
158 function woo_ml_admin_actions()
159 {
160
161 woo_ml_migrations();
162
163 if ( ! isset($_GET['woo_ml_action'])) {
164 return;
165 }
166
167 // Handle admin actions here
168 if ('setup_integration' === $_GET['woo_ml_action']) {
169 // Setup integration
170 woo_ml_setup_integration();
171 // Afterwards redirect to settings and show success notice
172 wp_redirect(add_query_arg('woo_ml_admin_notice', 'integration_setup_completed',
173 TrackingData::getInstance()->getSettingsPageUrl()));
174 exit;
175 }
176 }
177
178 add_action('admin_init', 'woo_ml_admin_actions');
179
180 /**
181 * Add checkbox element to bulk and quick edit menu on a product
182 */
183 function woo_ml_bulk_edit_quick_edit()
184 {
185
186 echo '<div class="inline-edit-group">';
187 woocommerce_wp_checkbox(array(
188 'id' => 'ml_ignore_product',
189 'label' => __('MailerLite e-commerce automations', 'woo-mailerlite'),
190 'description' => __('Ignore product', 'woo-mailerlite'),
191 'value' => '',
192 'desc_tip' => false
193 ));
194 echo '</div>';
195 }
196
197 add_action('woocommerce_product_quick_edit_end', 'woo_ml_bulk_edit_quick_edit', 99);
198 add_action('woocommerce_product_bulk_edit_end', 'woo_ml_bulk_edit_quick_edit', 99);
199
200 /**
201 * Populate MailerLite ignore product checkbox
202 */
203 function woo_ml_product_custom_column($column, $post_id)
204 {
205
206 $ignore_list = ProductProcess::getInstance()->getIgnoredProductList();
207
208 switch ($column) {
209 case 'name' :
210 ?>
211 <div class="hidden ml_ignore_product_inline" id="ml_ignore_product_inline_<?php echo $post_id; ?>">
212 <div id="_ml_ignore_product"><?php echo array_key_exists($post_id, $ignore_list) ? 'yes' : 'no' ?></div>
213 </div>
214 <?php
215
216 break;
217 }
218 }
219
220 add_action('manage_product_posts_custom_column', 'woo_ml_product_custom_column', 99, 2);
221
222 /**
223 * Handle save bulk and quick edit of a product
224 */
225 function woo_ml_bulk_edit_quick_edit_save($post_id, $post)
226 {
227
228 // If this is an autosave, skip because form was not submitted
229 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
230 return $post_id;
231 }
232
233 if ('product' !== $post->post_type) {
234 return $post_id;
235 }
236
237 $ignore_list = ProductProcess::getInstance()->getIgnoredProductList();
238
239 if (isset($_REQUEST['ml_ignore_product'])) {
240
241 $ignore_list[$post_id] = $post->post_title;
242
243 MailerLiteSettings::getInstance()->ignoreProduct($post_id);
244 } else {
245
246 MailerLiteSettings::getInstance()->removeIgnoreProduct($post_id);
247
248 if (array_key_exists($post_id, $ignore_list)) {
249
250 $ignore_list = TrackingData::getInstance()->removeProductFromList($ignore_list, array($post_id));
251 }
252 }
253
254 TrackingData::getInstance()->saveLocalIgnoreProducts($ignore_list);
255 }
256
257 add_action('woocommerce_product_bulk_and_quick_edit', 'woo_ml_bulk_edit_quick_edit_save', 10, 2);
258
259 /**
260 * Add a product tab for MailerLite options
261 */
262 function woo_ml_product_data_tab($product_data_tabs)
263 {
264
265 $product_data_tabs['woo-ml'] = array(
266 'label' => __('MailerLite', 'woo-mailerlite'),
267 'target' => 'woo-ml-product-data',
268 'priority' => 99
269 );
270
271 return $product_data_tabs;
272 }
273
274 add_filter('woocommerce_product_data_tabs', 'woo_ml_product_data_tab');
275
276 /**
277 * Add options to MailerLite product tab
278 */
279 function woo_ml_product_data_fields()
280 {
281
282 global $post;
283 ?>
284 <div id="woo-ml-product-data" class="panel woocommerce_options_panel">
285 <?php
286
287 $ignore_list = ProductProcess::getInstance()->getIgnoredProductList();
288
289 woocommerce_wp_checkbox(array(
290 'id' => 'ml_ignore_product',
291 'label' => __('Ignore Product', 'woo-mailerlite'),
292 'description' => __('Select if you do not wish to trigger any e-commerce automations for this product',
293 'woo-mailerlite'),
294 'value' => array_key_exists($post->ID, $ignore_list) ? 'yes' : 'no',
295 'desc_tip' => true,
296 ));
297 ?>
298 </div>
299 <?php
300 }
301
302 add_action('woocommerce_product_data_panels', 'woo_ml_product_data_fields');
303
304 /**
305 * Handle save options in MailerLite product tab
306 */
307 function woo_ml_product_data_save($post_id)
308 {
309
310 $title = $_POST['post_title'];
311
312 $ignore_list = ProductProcess::getInstance()->getIgnoredProductList();
313
314 if (isset($_POST['ml_ignore_product'])) {
315
316 $ignore_list[$post_id] = $title;
317
318 MailerLiteSettings::getInstance()->ignoreProduct($post_id);
319 } else {
320
321 MailerLiteSettings::getInstance()->removeIgnoreProduct($post_id);
322
323 if (array_key_exists($post_id, $ignore_list)) {
324
325 $ignore_list = TrackingData::getInstance()->removeProductFromList($ignore_list, array($post_id));
326 }
327 }
328
329 TrackingData::getInstance()->saveLocalIgnoreProducts($ignore_list);
330 }
331
332 add_action('woocommerce_process_product_meta', 'woo_ml_product_data_save');
333
334 /**
335 * Add additional script when editing a product
336 */
337 function woo_ml_edit_product_enqueue_admin_scripts()
338 {
339
340 wp_enqueue_script('woo-ml-bulk-quick-edit', WOO_MAILERLITE_URL . 'assets/js/bq_edit.js',
341 array('jquery', 'inline-edit-post'), WOO_MAILERLITE_VER, true);
342 }
343
344 add_action('admin_print_scripts-edit.php', 'woo_ml_edit_product_enqueue_admin_scripts');
345