PluginProbe
MailerLite – WooCommerce integration / 3.1.27
MailerLite – WooCommerce integration v3.1.27
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 / WooMailerLite.php

WooMailerLite.php in MailerLite – WooCommerce integration 3.1.27, at includes/WooMailerLite.php

396 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file that defines the core plugin class
4 *
5 * A class definition that includes attributes and functions used across both the
6 * public-facing side of the site and the admin area.
7 *
8 * @link https://mailerlite.com
9 * @since 3.0.0
10 *
11 * @package WooMailerlite
12 */
13
14 /**
15 * The core plugin class.
16 *
17 * This is used to define internationalization, admin-specific hooks, and
18 * public-facing site hooks.
19 *
20 * Also maintains the unique identifier of this plugin as well as the current
21 * version of the plugin.
22 *
23 * @since 3.0.0
24 * @package WooMailerlite
25 *
26 */
27 class WooMailerLite {
28
29 /**
30 * The loader that's responsible for maintaining and registering all hooks that power
31 * the plugin.
32 *
33 * @since 1.0.0
34 * @access protected
35 * @var WooMailerLiteLoader $loader Maintains and registers all hooks for the plugin.
36 */
37 protected $loader;
38
39 /**
40 * The unique identifier of this plugin.
41 *
42 * @since 1.0.0
43 * @access protected
44 * @var string $plugin_name The string used to uniquely identify this plugin.
45 */
46 protected $plugin_name;
47
48 /**
49 * The current version of the plugin.
50 *
51 * @since 1.0.0
52 * @access protected
53 * @var string $version The current version of the plugin.
54 */
55 protected $version;
56
57 /**
58 * @var string
59 */
60 protected $environment = 'production';
61
62 protected $is_configured;
63
64 protected static $logging_config = null;
65
66 public function __construct()
67 {
68 $this->loader = new WooMailerLiteLoader();
69 $this->getCustomFields();
70 $this->define_admin_hooks();
71 $this->define_public_hooks();
72 $this->setupWooMailerLite();
73 // $this->handleUpgrade();
74 }
75 /**
76 * Register all of the hooks related to the admin area functionality
77 * of the plugin.
78 *
79 * @since 1.0.0
80 * @access private
81 */
82 private function define_admin_hooks()
83 {
84 // Add menu item
85 function woo_ml_bulk_edit_quick_edit()
86 {
87
88 echo '<div class="inline-edit-group">';
89 woocommerce_wp_checkbox(array(
90 'id' => 'ml_ignore_product',
91 'label' => __('MailerLite e-commerce automations', 'woo-mailerlite'),
92 'description' => __('Ignore product', 'woo-mailerlite'),
93 'value' => '',
94 'desc_tip' => false
95 ));
96 echo '</div>';
97 }
98
99 $pluginAdmin = WooMailerLiteAdmin::instance();
100
101 $this->loader->add_action('woocommerce_product_quick_edit_end', $pluginAdmin, 'ignoreProductBlock');
102 $this->loader->add_action('woocommerce_product_bulk_edit_end', $pluginAdmin, 'ignoreProductBlock');
103 $this->loader->add_action('manage_product_posts_custom_column', $pluginAdmin, 'populateIgnoreProductBlock', 99, 2);
104 $this->loader->add_action('woocommerce_process_product_meta', WooMailerLiteAdminSettingsController::instance(), 'updateProduct');
105 $this->loader->add_action('woocommerce_product_data_panels', $pluginAdmin, 'abss');
106 $this->loader->add_action('woocommerce_update_product', WooMailerLiteAdminSettingsController::instance(), 'updateProduct', 10, 1);
107 $this->loader->add_action('created_product_cat', WooMailerLiteAdminSettingsController::instance(), 'updateCategory', 10, 2);
108 $this->loader->add_action('edited_product_cat', WooMailerLiteAdminSettingsController::instance(), 'updateCategory', 10, 2);
109 $this->loader->add_action('delete_product_cat', WooMailerLiteAdminSettingsController::instance(), 'deleteCategory', 10, 2);
110
111
112 $this->loader->add_filter('woocommerce_product_data_tabs', $pluginAdmin, 'woo_ml_product_data_tab');
113 $this->loader->add_filter('woocommerce_product_data_store_cpt_get_products_query', $pluginAdmin, 'handleCustomProductQuery', 10, 2);
114 $this->loader->add_filter('plugin_action_links_woo-mailerlite/woo-mailerlite.php', $pluginAdmin, 'addSettingsOptionInPluginList');
115 $this->loader->add_action('woocommerce_product_bulk_and_quick_edit', WooMailerLiteAdminSettingsController::instance(), 'updateIgnoreProductsBulkAndQuickEdit', 10, 2);
116 $this->loader->add_filter('script_loader_tag', $pluginAdmin, 'addModuleTypeScript', 10, 3);
117 $this->loader->add_action('admin_enqueue_scripts', $pluginAdmin, 'enqueueScripts', 20);
118 $this->loader->add_action('admin_enqueue_scripts', $pluginAdmin, 'enqueueStyles', 20);
119 $this->loader->add_action('admin_enqueue_scripts', $pluginAdmin, 'removeConflictingSelect2', 9999);
120 $this->loader->add_action('admin_menu', $pluginAdmin, 'addPluginAdminMenu', 71);
121 $this->loader->add_action('wp_ajax_woo_mailerlite_handle_connect_account', WooMailerLiteAdminWizardController::instance(), 'handleConnectAccount');
122 $this->loader->add_action('wp_ajax_woo_mailerlite_get_groups', WooMailerLiteAdminWizardController::instance(), 'getGroups');
123 $this->loader->add_action('wp_ajax_woo_mailerlite_shop_setup', WooMailerLiteAdminWizardController::instance(), 'shopSetup');
124 $this->loader->add_action('wp_ajax_woo_mailerlite_create_group', WooMailerLiteAdminGroupController::instance(), 'createGroup');
125 $this->loader->add_action('wp_ajax_woo_mailerlite_sync_handler', WooMailerLiteAdminSyncController::instance(), 'sync');
126 $this->loader->add_action('wp_ajax_woo_mailerlite_reset_sync_handler', WooMailerLiteAdminSyncController::instance(), 'resetSync');
127 $this->loader->add_action('wp_ajax_handle_save_settings', WooMailerLiteAdminSettingsController::instance(), 'saveSettings');
128 $this->loader->add_action('wp_ajax_woo_mailerlite_reset_integration_settings', WooMailerLiteAdminSettingsController::instance(), 'resetIntegration');
129 $this->loader->add_action('wp_ajax_handle_debug_log', WooMailerLiteAdminWizardController::instance(), 'getDebugLogs');
130 $this->loader->add_action('wp_ajax_woo_mailerlite_downgrade_plugin', WooMailerLiteAdminSettingsController::instance(), 'downgradePlugin');
131 $this->loader->add_action('wp_ajax_woo_mailerlite_enable_debug_mode', WooMailerLiteAdminSettingsController::instance(), 'enableDebugMode');
132 $this->loader->add_action('add_meta_boxes', WooMailerLiteAdminMetaBoxController::instance(), 'addMetaBoxes');
133 $this->asyncJobHandler();
134 }
135
136 public function asyncJobHandler()
137 {
138 if (!WooMailerLiteCache::get('table_check')) {
139 WooMailerLiteMigration::migrate();
140 WooMailerLiteCache::set('table_check', true, 86400);
141 }
142 WooMailerLiteMigration::addCartsCreatedAtIndex();
143 $this->loader->add_action('init', $this, 'scheduleCartCleanupJob');
144 $this->loader->add_action('init', WooMailerLiteAdmin::instance(), 'migrateProductVariantsOrIgnore');
145 $jobsDirectory = __DIR__ . '/./jobs';
146 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($jobsDirectory));
147 foreach ($iterator as $job) {
148 if ($job->isFile() && $job->getExtension() === 'php') {
149 $class = pathinfo($job->getFilename(), PATHINFO_FILENAME);
150 if ($class === 'WooMailerLiteAbstractJob') {
151 continue;
152 }
153 if (class_exists($class) && method_exists($class, 'getInstance')) {
154 $this->loader->add_action($class, $class::getInstance(), 'runSafely');
155 }
156 }
157 }
158 }
159
160 public function scheduleCartCleanupJob()
161 {
162 if (WooMailerLiteCache::get('cart_cleanup_scheduled')) {
163 return;
164 }
165 if (!function_exists('as_next_scheduled_action') || !function_exists('as_schedule_recurring_action')) {
166 return;
167 }
168
169 if (!as_next_scheduled_action(WooMailerLiteCartCleanupJob::class)) {
170 as_schedule_recurring_action(time(), DAY_IN_SECONDS, WooMailerLiteCartCleanupJob::class);
171 }
172 WooMailerLiteCache::set('cart_cleanup_scheduled', true, 86400);
173 }
174
175 /**
176 * Register all of the hooks related to the public-facing functionality
177 * of the plugin.
178 *
179 * @since 1.0.0
180 * @access private
181 */
182 private function define_public_hooks() {
183
184 $pluginPublicInstance = WooMailerLitePublic::instance();
185
186 $this->loader->add_action('wp_enqueue_scripts', $pluginPublicInstance, 'enqueueScripts');
187
188 $checkout_position = WooMailerLiteOptions::get('settings.selectedCheckoutPosition', 'checkout_billing');
189 $checkout_position_hook = 'woocommerce_' . $checkout_position;
190
191 $this->loader->add_action($checkout_position_hook, WooMailerLitePluginController::instance(), 'addMlSubscribeCheckbox', PHP_INT_MAX);
192 $this->loader->add_action('init', WooMailerLitePluginController::instance(), 'reloadCheckout');
193 $this->loader->add_filter('woocommerce_form_field', $pluginPublicInstance, 'removeOptionalFromMlCheckbox', 10, 4);
194 $this->loader->add_filter('woocommerce_checkout_fields', WooMailerLitePluginController::instance(), 'addBillingCheckoutFields', PHP_INT_MAX);
195 }
196
197 private function setupWooMailerLite()
198 {
199 $service = WooMailerLiteService::instance();
200 $this->loader->add_filter('woocommerce_update_cart_action_cart_updated', $service, 'handleCartUpdated');
201 $this->loader->add_action('woocommerce_cart_item_set_quantity', $service, 'handleCartUpdated');
202 $this->loader->add_action('woocommerce_add_to_cart', $service, 'handleCartUpdated');
203 $this->loader->add_action('woocommerce_cart_item_removed', $service, 'handleCartUpdated');
204 $this->loader->add_action('woocommerce_before_checkout_form', $service, 'handleCheckoutPage');
205 $this->loader->add_action('woocommerce_checkout_create_order', WooMailerLiteOrderController::instance(), 'persistLanguageForOrder');
206 $this->loader->add_action('woocommerce_store_api_checkout_update_order_from_request', WooMailerLiteOrderController::instance(), 'persistLanguageForOrder');
207 $this->loader->add_action('woocommerce_order_status_changed', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged');
208 $this->loader->add_action('woocommerce_saved_order_items', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged');
209 $this->loader->add_action('woocommerce_order_status_completed', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged');
210 $this->loader->add_action('wp_ajax_woo_mailerlite_set_cart_email', $service, 'setCartEmail');
211 $this->loader->add_action('wp_ajax_nopriv_woo_mailerlite_set_cart_email', $service, 'setCartEmail');
212 }
213
214 public function getCustomFields()
215 {
216 if (!WooMailerLiteOptions::get('apiKey')) {
217 return false;
218 }
219 $client = WooMailerLiteApi::client();
220 $fieldsOnApp = get_transient('mailerlite_custom_fields');
221 if ($fieldsOnApp === false) {
222 $fieldsOnApp = $client->getFields();
223 set_transient('mailerlite_custom_fields', $fieldsOnApp, 24 * HOUR_IN_SECONDS);
224 } else {
225 return true;
226 }
227 $fields = $this->getPluginFields();
228
229 try {
230 if ($fieldsOnApp->success && !empty($fieldsOnApp->data)) {
231
232 foreach ($fieldsOnApp->data as $appField) {
233 if (isset($appField->key) && isset($fields[$appField->key])) {
234 unset($fields[$appField->key]);
235 }
236 }
237
238 if (sizeof($fields) > 0) {
239 foreach ($fields as $field) {
240 $tempName = false;
241
242 if (isset($field['key'])) {
243 $tempName = $field['key'] . ' ' . $field['title'];
244 }
245 $data = [
246 'title' => $tempName ?: $field['title'],
247 'type' => $field['type']
248 ];
249 if ($client->apiType === WooMailerLiteApi::REWRITE_API) {
250 $data['name'] = $data['title'];
251 unset($data['title']);
252 }
253 $fieldAdded = $client->createField($data);
254 if (isset($fieldAdded->data->id)) {
255 if ($client->apiType === WooMailerLiteApi::REWRITE_API) {
256
257 $client->updateField($fieldAdded->data->id, [
258 'name' => $field['name'] . ' ' . $field['title']
259 ]);
260 }
261 return $fieldAdded;
262 } else {
263 return false;
264 }
265 }
266
267 }
268 }
269 } catch (Exception $e) {
270 return false;
271 }
272 return false;
273 }
274
275 public function getPluginFields()
276 {
277 if (WooMailerLiteApi::client()->apiType === WooMailerLiteApi::CLASSIC_API) {
278
279 return [
280 'woo_orders_count' => [
281 'title' => 'Woo Orders Count',
282 'type' => 'NUMBER'
283 ],
284 'woo_total_spent' => [
285 'title' => 'Woo Total Spent',
286 'type' => 'NUMBER'
287 ],
288 'woo_last_order' => [
289 'title' => 'Woo Last Order',
290 'type' => 'DATE'
291 ],
292 'woo_last_order_id' => [
293 'title' => 'Woo Last Order ID',
294 'type' => 'NUMBER'
295 ],
296 ];
297 } else {
298
299 $shopUrl = home_url();
300 $shopKey = preg_replace('/[^A-Za-z0-9 ]/', '', $shopUrl);
301
302 $shop_name = get_bloginfo('name');
303
304 if (empty($shop_name)) {
305 $shop_name = $shopUrl;
306 }
307
308 return [
309 $shopKey . '_total_spent' => [
310 'key' => $shopKey,
311 'name' => $shop_name,
312 'title' => 'Total spent',
313 'type' => 'number',
314 ],
315 $shopKey . '_orders_count' => [
316 'key' => $shopKey,
317 'name' => $shop_name,
318 'title' => 'Orders count',
319 'type' => 'number',
320 ],
321 $shopKey . '_accepts_marketing' => [
322 'key' => $shopKey,
323 'name' => $shop_name,
324 'title' => 'Accepts marketing',
325 'type' => 'number',
326 ],
327 ];
328 }
329 }
330
331
332 public function run()
333 {
334 $this->loader->run();
335 }
336
337 public function handleUpgrade()
338 {
339 if (!WooMailerLiteOptions::get('customTableCheck')) {
340 WooMailerLiteMigration::customPrefixTablesMigrate();
341 }
342 if (get_option('woo_ml_key', false) && (get_option('woo_ml_wizard_setup', 0) == 2)) {
343 $settings = get_option('woocommerce_mailerlite_settings', []);
344 if (!(get_option('woo_ml_key') == WooMailerLiteOptions::get('apiKey'))) {
345 return false;
346 }
347 if (!empty($settings)) {
348 WooMailerLiteOptions::updateMultiple([
349 'apiKey' => get_option('woo_ml_key'),
350 'wizardStep' => 2,
351 'accountName' => get_option('woo_ml_account_name', ''),
352 'accountId' => get_option('account_id', false),
353 'accountSubdomain' => get_option('account_subdomain', ''),
354 'shopId' => get_option('woo_ml_shop_id'),
355 'woo_ml_shop_id' => get_option('woo_ml_shop_id'),
356 'popupsEnabled' => $settings['popups'] ?? false,
357 'syncFields' => $settings['sync_fields'] ?? [],
358 'enabled' => true,
359 'consumerKey' => $settings['consumer_key'] ?? '',
360 'consumerSecret' => $settings['consumer_secret'] ?? '',
361 'lastSyncedCustomer' => get_option('woo_ml_last_synced_guest_id', 0),
362 'settings' => [
363 'languageField' => false,
364 'subscribeOnCheckout' => ($settings['checkout'] ?? null) == 'yes',
365 'resubscribe' => ($settings['resubscribe'] ?? null) == 'yes',
366 'selectedCheckoutPosition' => $settings['checkout_position'] ?? 'checkout_billing',
367 'checkoutPreselect' => ($settings['checkout_preselect'] ?? null) == 'yes',
368 'checkoutHidden' => ($settings['checkout_hide'] ?? null) == 'yes',
369 'syncAfterCheckout' => ($settings['disable_checkout_sync'] ?? null) == 'yes',
370 'checkoutLabel' => $settings['checkout_label'] ?? 'Yes, I want to receive your newsletter.',
371 'doubleOptIn' => ($settings['double_optin'] ?? 'no') == 'yes',
372 'popUps' => $settings['popups'] ?? false,
373 'autoUpdatePlugin' => $settings['auto_update_plugin'] ?? false,
374 'group' => $settings['group'] ?? null,
375 'syncFields' => $settings['sync_fields'] ?? [],
376 'shopId' => get_option('woo_ml_shop_id'),
377 'woo_ml_shop_id' => get_option('woo_ml_shop_id'),
378 ]
379 ]);
380 }
381 if (isset($settings['group'])) {
382 $response = WooMailerLiteApi::client()->getGroupById($settings['group']);
383 if (!$response->success) {
384 return false;
385 }
386 WooMailerLiteOptions::update('group', ['id' => $response->data->id, 'name' => $response->data->name]);
387 }
388
389 delete_option('woo_ml_key');
390 delete_option('woo_ml_wizard_setup');
391 }
392 return true;
393 }
394
395 }
396