PluginProbe
MailerLite – WooCommerce integration / 3.1.26
MailerLite – WooCommerce integration v3.1.26
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.26, at includes/WooMailerLite.php

394 lines 17.6 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_order_status_changed', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged');
206 $this->loader->add_action('woocommerce_saved_order_items', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged');
207 $this->loader->add_action('woocommerce_order_status_completed', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged');
208 $this->loader->add_action('wp_ajax_woo_mailerlite_set_cart_email', $service, 'setCartEmail');
209 $this->loader->add_action('wp_ajax_nopriv_woo_mailerlite_set_cart_email', $service, 'setCartEmail');
210 }
211
212 public function getCustomFields()
213 {
214 if (!WooMailerLiteOptions::get('apiKey')) {
215 return false;
216 }
217 $client = WooMailerLiteApi::client();
218 $fieldsOnApp = get_transient('mailerlite_custom_fields');
219 if ($fieldsOnApp === false) {
220 $fieldsOnApp = $client->getFields();
221 set_transient('mailerlite_custom_fields', $fieldsOnApp, 24 * HOUR_IN_SECONDS);
222 } else {
223 return true;
224 }
225 $fields = $this->getPluginFields();
226
227 try {
228 if ($fieldsOnApp->success && !empty($fieldsOnApp->data)) {
229
230 foreach ($fieldsOnApp->data as $appField) {
231 if (isset($appField->key) && isset($fields[$appField->key])) {
232 unset($fields[$appField->key]);
233 }
234 }
235
236 if (sizeof($fields) > 0) {
237 foreach ($fields as $field) {
238 $tempName = false;
239
240 if (isset($field['key'])) {
241 $tempName = $field['key'] . ' ' . $field['title'];
242 }
243 $data = [
244 'title' => $tempName ?: $field['title'],
245 'type' => $field['type']
246 ];
247 if ($client->apiType === WooMailerLiteApi::REWRITE_API) {
248 $data['name'] = $data['title'];
249 unset($data['title']);
250 }
251 $fieldAdded = $client->createField($data);
252 if (isset($fieldAdded->data->id)) {
253 if ($client->apiType === WooMailerLiteApi::REWRITE_API) {
254
255 $client->updateField($fieldAdded->data->id, [
256 'name' => $field['name'] . ' ' . $field['title']
257 ]);
258 }
259 return $fieldAdded;
260 } else {
261 return false;
262 }
263 }
264
265 }
266 }
267 } catch (Exception $e) {
268 return false;
269 }
270 return false;
271 }
272
273 public function getPluginFields()
274 {
275 if (WooMailerLiteApi::client()->apiType === WooMailerLiteApi::CLASSIC_API) {
276
277 return [
278 'woo_orders_count' => [
279 'title' => 'Woo Orders Count',
280 'type' => 'NUMBER'
281 ],
282 'woo_total_spent' => [
283 'title' => 'Woo Total Spent',
284 'type' => 'NUMBER'
285 ],
286 'woo_last_order' => [
287 'title' => 'Woo Last Order',
288 'type' => 'DATE'
289 ],
290 'woo_last_order_id' => [
291 'title' => 'Woo Last Order ID',
292 'type' => 'NUMBER'
293 ],
294 ];
295 } else {
296
297 $shopUrl = home_url();
298 $shopKey = preg_replace('/[^A-Za-z0-9 ]/', '', $shopUrl);
299
300 $shop_name = get_bloginfo('name');
301
302 if (empty($shop_name)) {
303 $shop_name = $shopUrl;
304 }
305
306 return [
307 $shopKey . '_total_spent' => [
308 'key' => $shopKey,
309 'name' => $shop_name,
310 'title' => 'Total spent',
311 'type' => 'number',
312 ],
313 $shopKey . '_orders_count' => [
314 'key' => $shopKey,
315 'name' => $shop_name,
316 'title' => 'Orders count',
317 'type' => 'number',
318 ],
319 $shopKey . '_accepts_marketing' => [
320 'key' => $shopKey,
321 'name' => $shop_name,
322 'title' => 'Accepts marketing',
323 'type' => 'number',
324 ],
325 ];
326 }
327 }
328
329
330 public function run()
331 {
332 $this->loader->run();
333 }
334
335 public function handleUpgrade()
336 {
337 if (!WooMailerLiteOptions::get('customTableCheck')) {
338 WooMailerLiteMigration::customPrefixTablesMigrate();
339 }
340 if (get_option('woo_ml_key', false) && (get_option('woo_ml_wizard_setup', 0) == 2)) {
341 $settings = get_option('woocommerce_mailerlite_settings', []);
342 if (!(get_option('woo_ml_key') == WooMailerLiteOptions::get('apiKey'))) {
343 return false;
344 }
345 if (!empty($settings)) {
346 WooMailerLiteOptions::updateMultiple([
347 'apiKey' => get_option('woo_ml_key'),
348 'wizardStep' => 2,
349 'accountName' => get_option('woo_ml_account_name', ''),
350 'accountId' => get_option('account_id', false),
351 'accountSubdomain' => get_option('account_subdomain', ''),
352 'shopId' => get_option('woo_ml_shop_id'),
353 'woo_ml_shop_id' => get_option('woo_ml_shop_id'),
354 'popupsEnabled' => $settings['popups'] ?? false,
355 'syncFields' => $settings['sync_fields'] ?? [],
356 'enabled' => true,
357 'consumerKey' => $settings['consumer_key'] ?? '',
358 'consumerSecret' => $settings['consumer_secret'] ?? '',
359 'lastSyncedCustomer' => get_option('woo_ml_last_synced_guest_id', 0),
360 'settings' => [
361 'languageField' => false,
362 'subscribeOnCheckout' => ($settings['checkout'] ?? null) == 'yes',
363 'resubscribe' => ($settings['resubscribe'] ?? null) == 'yes',
364 'selectedCheckoutPosition' => $settings['checkout_position'] ?? 'checkout_billing',
365 'checkoutPreselect' => ($settings['checkout_preselect'] ?? null) == 'yes',
366 'checkoutHidden' => ($settings['checkout_hide'] ?? null) == 'yes',
367 'syncAfterCheckout' => ($settings['disable_checkout_sync'] ?? null) == 'yes',
368 'checkoutLabel' => $settings['checkout_label'] ?? 'Yes, I want to receive your newsletter.',
369 'doubleOptIn' => ($settings['double_optin'] ?? 'no') == 'yes',
370 'popUps' => $settings['popups'] ?? false,
371 'autoUpdatePlugin' => $settings['auto_update_plugin'] ?? false,
372 'group' => $settings['group'] ?? null,
373 'syncFields' => $settings['sync_fields'] ?? [],
374 'shopId' => get_option('woo_ml_shop_id'),
375 'woo_ml_shop_id' => get_option('woo_ml_shop_id'),
376 ]
377 ]);
378 }
379 if (isset($settings['group'])) {
380 $response = WooMailerLiteApi::client()->getGroupById($settings['group']);
381 if (!$response->success) {
382 return false;
383 }
384 WooMailerLiteOptions::update('group', ['id' => $response->data->id, 'name' => $response->data->name]);
385 }
386
387 delete_option('woo_ml_key');
388 delete_option('woo_ml_wizard_setup');
389 }
390 return true;
391 }
392
393 }
394