| 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 |
$jobsDirectory = __DIR__ . '/./jobs'; |
| 143 |
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($jobsDirectory)); |
| 144 |
foreach ($iterator as $job) { |
| 145 |
if ($job->isFile() && $job->getExtension() === 'php') { |
| 146 |
$class = pathinfo($job->getFilename(), PATHINFO_FILENAME); |
| 147 |
if ($class === 'WooMailerLiteAbstractJob') { |
| 148 |
continue; |
| 149 |
} |
| 150 |
if (class_exists($class) && method_exists($class, 'getInstance')) { |
| 151 |
$this->loader->add_action($class, $class::getInstance(), 'runSafely'); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Register all of the hooks related to the public-facing functionality |
| 159 |
* of the plugin. |
| 160 |
* |
| 161 |
* @since 1.0.0 |
| 162 |
* @access private |
| 163 |
*/ |
| 164 |
private function define_public_hooks() { |
| 165 |
|
| 166 |
$pluginPublicInstance = WooMailerLitePublic::instance(); |
| 167 |
|
| 168 |
$this->loader->add_action('wp_enqueue_scripts', $pluginPublicInstance, 'enqueueScripts'); |
| 169 |
|
| 170 |
$checkout_position = WooMailerLiteOptions::get('settings.selectedCheckoutPosition', 'checkout_billing'); |
| 171 |
$checkout_position_hook = 'woocommerce_' . $checkout_position; |
| 172 |
|
| 173 |
$this->loader->add_action($checkout_position_hook, WooMailerLitePluginController::instance(), 'addMlSubscribeCheckbox', PHP_INT_MAX); |
| 174 |
$this->loader->add_action('init', WooMailerLitePluginController::instance(), 'reloadCheckout'); |
| 175 |
$this->loader->add_filter('woocommerce_form_field', $pluginPublicInstance, 'removeOptionalFromMlCheckbox', 10, 4); |
| 176 |
$this->loader->add_filter('woocommerce_checkout_fields', WooMailerLitePluginController::instance(), 'addBillingCheckoutFields', PHP_INT_MAX); |
| 177 |
} |
| 178 |
|
| 179 |
private function setupWooMailerLite() |
| 180 |
{ |
| 181 |
$service = WooMailerLiteService::instance(); |
| 182 |
$this->loader->add_filter('woocommerce_update_cart_action_cart_updated', $service, 'handleCartUpdated'); |
| 183 |
$this->loader->add_action('woocommerce_cart_item_set_quantity', $service, 'handleCartUpdated'); |
| 184 |
$this->loader->add_action('woocommerce_add_to_cart', $service, 'handleCartUpdated'); |
| 185 |
$this->loader->add_action('woocommerce_cart_item_removed', $service, 'handleCartUpdated'); |
| 186 |
$this->loader->add_action('woocommerce_before_checkout_form', $service, 'handleCheckoutPage'); |
| 187 |
$this->loader->add_action('woocommerce_order_status_changed', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged'); |
| 188 |
$this->loader->add_action('woocommerce_saved_order_items', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged'); |
| 189 |
$this->loader->add_action('woocommerce_order_status_completed', WooMailerLiteOrderController::instance(), 'handleOrderStatusChanged'); |
| 190 |
$this->loader->add_action('wp_ajax_woo_mailerlite_set_cart_email', $service, 'setCartEmail'); |
| 191 |
$this->loader->add_action('wp_ajax_nopriv_woo_mailerlite_set_cart_email', $service, 'setCartEmail'); |
| 192 |
} |
| 193 |
|
| 194 |
public function getCustomFields() |
| 195 |
{ |
| 196 |
if (!WooMailerLiteOptions::get('apiKey')) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
$client = WooMailerLiteApi::client(); |
| 200 |
$fieldsOnApp = get_transient('mailerlite_custom_fields'); |
| 201 |
if ($fieldsOnApp === false) { |
| 202 |
$fieldsOnApp = $client->getFields(); |
| 203 |
set_transient('mailerlite_custom_fields', $fieldsOnApp, 24 * HOUR_IN_SECONDS); |
| 204 |
} else { |
| 205 |
return true; |
| 206 |
} |
| 207 |
$fields = $this->getPluginFields(); |
| 208 |
|
| 209 |
try { |
| 210 |
if ($fieldsOnApp->success && !empty($fieldsOnApp->data)) { |
| 211 |
|
| 212 |
foreach ($fieldsOnApp->data as $appField) { |
| 213 |
if (isset($appField->key) && isset($fields[$appField->key])) { |
| 214 |
unset($fields[$appField->key]); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if (sizeof($fields) > 0) { |
| 219 |
foreach ($fields as $field) { |
| 220 |
$tempName = false; |
| 221 |
|
| 222 |
if (isset($field['key'])) { |
| 223 |
$tempName = $field['key'] . ' ' . $field['title']; |
| 224 |
} |
| 225 |
$data = [ |
| 226 |
'title' => $tempName ?: $field['title'], |
| 227 |
'type' => $field['type'] |
| 228 |
]; |
| 229 |
if ($client->apiType === WooMailerLiteApi::REWRITE_API) { |
| 230 |
$data['name'] = $data['title']; |
| 231 |
unset($data['title']); |
| 232 |
} |
| 233 |
$fieldAdded = $client->createField($data); |
| 234 |
if (isset($fieldAdded->data->id)) { |
| 235 |
if ($client->apiType === WooMailerLiteApi::REWRITE_API) { |
| 236 |
|
| 237 |
$client->updateField($fieldAdded->data->id, [ |
| 238 |
'name' => $field['name'] . ' ' . $field['title'] |
| 239 |
]); |
| 240 |
} |
| 241 |
return $fieldAdded; |
| 242 |
} else { |
| 243 |
return false; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
} |
| 249 |
} catch (Exception $e) { |
| 250 |
return false; |
| 251 |
} |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
public function getPluginFields() |
| 256 |
{ |
| 257 |
if (WooMailerLiteApi::client()->apiType === WooMailerLiteApi::CLASSIC_API) { |
| 258 |
|
| 259 |
return [ |
| 260 |
'woo_orders_count' => [ |
| 261 |
'title' => 'Woo Orders Count', |
| 262 |
'type' => 'NUMBER' |
| 263 |
], |
| 264 |
'woo_total_spent' => [ |
| 265 |
'title' => 'Woo Total Spent', |
| 266 |
'type' => 'NUMBER' |
| 267 |
], |
| 268 |
'woo_last_order' => [ |
| 269 |
'title' => 'Woo Last Order', |
| 270 |
'type' => 'DATE' |
| 271 |
], |
| 272 |
'woo_last_order_id' => [ |
| 273 |
'title' => 'Woo Last Order ID', |
| 274 |
'type' => 'NUMBER' |
| 275 |
], |
| 276 |
]; |
| 277 |
} else { |
| 278 |
|
| 279 |
$shopUrl = home_url(); |
| 280 |
$shopKey = preg_replace('/[^A-Za-z0-9 ]/', '', $shopUrl); |
| 281 |
|
| 282 |
$shop_name = get_bloginfo('name'); |
| 283 |
|
| 284 |
if (empty($shop_name)) { |
| 285 |
$shop_name = $shopUrl; |
| 286 |
} |
| 287 |
|
| 288 |
return [ |
| 289 |
$shopKey . '_total_spent' => [ |
| 290 |
'key' => $shopKey, |
| 291 |
'name' => $shop_name, |
| 292 |
'title' => 'Total spent', |
| 293 |
'type' => 'number', |
| 294 |
], |
| 295 |
$shopKey . '_orders_count' => [ |
| 296 |
'key' => $shopKey, |
| 297 |
'name' => $shop_name, |
| 298 |
'title' => 'Orders count', |
| 299 |
'type' => 'number', |
| 300 |
], |
| 301 |
$shopKey . '_accepts_marketing' => [ |
| 302 |
'key' => $shopKey, |
| 303 |
'name' => $shop_name, |
| 304 |
'title' => 'Accepts marketing', |
| 305 |
'type' => 'number', |
| 306 |
], |
| 307 |
]; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
public function run() |
| 313 |
{ |
| 314 |
$this->loader->run(); |
| 315 |
} |
| 316 |
|
| 317 |
public function handleUpgrade() |
| 318 |
{ |
| 319 |
if (!WooMailerLiteOptions::get('customTableCheck')) { |
| 320 |
WooMailerLiteMigration::customPrefixTablesMigrate(); |
| 321 |
} |
| 322 |
if (get_option('woo_ml_key', false) && (get_option('woo_ml_wizard_setup', 0) == 2)) { |
| 323 |
$settings = get_option('woocommerce_mailerlite_settings', []); |
| 324 |
if (!(get_option('woo_ml_key') == WooMailerLiteOptions::get('apiKey'))) { |
| 325 |
return false; |
| 326 |
} |
| 327 |
if (!empty($settings)) { |
| 328 |
WooMailerLiteOptions::updateMultiple([ |
| 329 |
'apiKey' => get_option('woo_ml_key'), |
| 330 |
'wizardStep' => 2, |
| 331 |
'accountName' => get_option('woo_ml_account_name', ''), |
| 332 |
'accountId' => get_option('account_id', false), |
| 333 |
'accountSubdomain' => get_option('account_subdomain', ''), |
| 334 |
'shopId' => get_option('woo_ml_shop_id'), |
| 335 |
'woo_ml_shop_id' => get_option('woo_ml_shop_id'), |
| 336 |
'popupsEnabled' => $settings['popups'] ?? false, |
| 337 |
'syncFields' => $settings['sync_fields'] ?? [], |
| 338 |
'enabled' => true, |
| 339 |
'consumerKey' => $settings['consumer_key'] ?? '', |
| 340 |
'consumerSecret' => $settings['consumer_secret'] ?? '', |
| 341 |
'lastSyncedCustomer' => get_option('woo_ml_last_synced_guest_id', 0), |
| 342 |
'settings' => [ |
| 343 |
'languageField' => false, |
| 344 |
'subscribeOnCheckout' => ($settings['checkout'] ?? null) == 'yes', |
| 345 |
'resubscribe' => ($settings['resubscribe'] ?? null) == 'yes', |
| 346 |
'selectedCheckoutPosition' => $settings['checkout_position'] ?? 'checkout_billing', |
| 347 |
'checkoutPreselect' => ($settings['checkout_preselect'] ?? null) == 'yes', |
| 348 |
'checkoutHidden' => ($settings['checkout_hide'] ?? null) == 'yes', |
| 349 |
'syncAfterCheckout' => ($settings['disable_checkout_sync'] ?? null) == 'yes', |
| 350 |
'checkoutLabel' => $settings['checkout_label'] ?? 'Yes, I want to receive your newsletter.', |
| 351 |
'doubleOptIn' => ($settings['double_optin'] ?? 'no') == 'yes', |
| 352 |
'popUps' => $settings['popups'] ?? false, |
| 353 |
'autoUpdatePlugin' => $settings['auto_update_plugin'] ?? false, |
| 354 |
'group' => $settings['group'] ?? null, |
| 355 |
'syncFields' => $settings['sync_fields'] ?? [], |
| 356 |
'shopId' => get_option('woo_ml_shop_id'), |
| 357 |
'woo_ml_shop_id' => get_option('woo_ml_shop_id'), |
| 358 |
] |
| 359 |
]); |
| 360 |
} |
| 361 |
if (isset($settings['group'])) { |
| 362 |
$response = WooMailerLiteApi::client()->getGroupById($settings['group']); |
| 363 |
if (!$response->success) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
WooMailerLiteOptions::update('group', ['id' => $response->data->id, 'name' => $response->data->name]); |
| 367 |
} |
| 368 |
|
| 369 |
delete_option('woo_ml_key'); |
| 370 |
delete_option('woo_ml_wizard_setup'); |
| 371 |
} |
| 372 |
return true; |
| 373 |
} |
| 374 |
|
| 375 |
} |
| 376 |
|