| 1 |
<?php |
| 2 |
|
| 3 |
use MailerLite\Includes\Classes\Process\CheckoutProcess; |
| 4 |
use MailerLite\Includes\Classes\Settings\ShopSettings; |
| 5 |
|
| 6 |
/** |
| 7 |
* Plugin Name: MailerLite - WooCommerce integration |
| 8 |
* Plugin URI: https://wordpress.org/plugins/woo-mailerlite/ |
| 9 |
* Description: Official MailerLite integration for WooCommerce. Track sales and campaign ROI, import products details, automate emails based on purchases and seamlessly add your customers to your email marketing lists via WooCommerce's checkout process. |
| 10 |
* Version: 2.0 |
| 11 |
* Author: MailerLite |
| 12 |
* Author URI: https://mailerlite.com |
| 13 |
* Text Domain: woo-mailerlite |
| 14 |
* WC tested up to: 8.0.2 |
| 15 |
* WC requires at least: 3.0.0 |
| 16 |
*/ |
| 17 |
|
| 18 |
// Exit if accessed directly |
| 19 |
if (!defined('ABSPATH')) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
// Plugin path |
| 24 |
define('WOO_MAILERLITE_DIR', plugin_dir_path(__FILE__)); |
| 25 |
|
| 26 |
if (!class_exists('Woo_Mailerlite')) { |
| 27 |
|
| 28 |
/** |
| 29 |
* Main Woo_Mailerlite class |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
class Woo_Mailerlite |
| 34 |
{ |
| 35 |
|
| 36 |
/** |
| 37 |
* @var Woo_Mailerlite $instance The one true Woo_Mailerlite |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
private static $instance; |
| 41 |
|
| 42 |
/** |
| 43 |
* Get active instance |
| 44 |
* |
| 45 |
* @access public |
| 46 |
* @return object self::$instance The one true Woo_Mailerlite |
| 47 |
* @since 1.0.0 |
| 48 |
*/ |
| 49 |
public static function instance() |
| 50 |
{ |
| 51 |
if (!self::$instance) { |
| 52 |
self::$instance = new Woo_Mailerlite(); |
| 53 |
self::$instance->setup_constants(); |
| 54 |
self::$instance->includes(); |
| 55 |
self::$instance->hooks(); |
| 56 |
self::$instance->load_textdomain(); |
| 57 |
} |
| 58 |
|
| 59 |
return self::$instance; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Setup plugin constants |
| 65 |
* |
| 66 |
* @access private |
| 67 |
* @return void |
| 68 |
* @since 1.0.0 |
| 69 |
*/ |
| 70 |
private function setup_constants() |
| 71 |
{ |
| 72 |
// Plugin name |
| 73 |
define('WOO_MAILERLITE_NAME', 'MailerLite - WooCommerce integration'); |
| 74 |
|
| 75 |
// Plugin version |
| 76 |
define('WOO_MAILERLITE_VER', '2.0'); |
| 77 |
|
| 78 |
// Plugin URL |
| 79 |
define('WOO_MAILERLITE_URL', plugin_dir_url(__FILE__)); |
| 80 |
|
| 81 |
// Plugin prefix |
| 82 |
define('WOO_MAILERLITE_PREFIX', 'woo_ml_'); |
| 83 |
|
| 84 |
// API Key |
| 85 |
if (!defined('MAILERLITE_WP_API_KEY')) { |
| 86 |
$option_value = get_option('woo_ml_key'); |
| 87 |
|
| 88 |
$api_key = $option_value ?: ''; |
| 89 |
define('MAILERLITE_WP_API_KEY', $api_key); |
| 90 |
|
| 91 |
} |
| 92 |
// Other |
| 93 |
define('WOO_MAILERLITE_MIN_PHP_VERSION', '7.2.5'); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Include necessary files |
| 98 |
* |
| 99 |
* @access private |
| 100 |
* @return void |
| 101 |
* @since 1.0.0 |
| 102 |
*/ |
| 103 |
private function includes() |
| 104 |
{ |
| 105 |
|
| 106 |
// Get out if WooCommerce is not active |
| 107 |
if (!class_exists('WC_Integration')) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
if (!$this->check_server_requirements()) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$this->check_plugin_version(); |
| 116 |
|
| 117 |
// Classes |
| 118 |
initClasses(); |
| 119 |
// Dependencies |
| 120 |
require_once WOO_MAILERLITE_DIR . 'includes/shared/api/class.woo-mailerlite-api-type.php'; |
| 121 |
require_once WOO_MAILERLITE_DIR . 'includes/shared/api/class.woo-mailerlite-api-client.php'; |
| 122 |
require_once WOO_MAILERLITE_DIR . 'includes/shared/api/class.woo-mailerlite-platform-api.php'; |
| 123 |
require_once WOO_MAILERLITE_DIR . 'includes/shared/api/class.woo-mailerlite-classic-api.php'; |
| 124 |
require_once WOO_MAILERLITE_DIR . 'includes/shared/api/class.woo-mailerlite-api.php'; |
| 125 |
require_once WOO_MAILERLITE_DIR . 'includes/shared/mailerlite-wp-functions.php'; |
| 126 |
|
| 127 |
// Core functions and hooks |
| 128 |
require_once WOO_MAILERLITE_DIR . 'includes/functions.php'; |
| 129 |
require_once WOO_MAILERLITE_DIR . 'includes/integration-setup-functions.php'; |
| 130 |
require_once WOO_MAILERLITE_DIR . 'includes/hooks.php'; |
| 131 |
require_once WOO_MAILERLITE_DIR . 'includes/scripts.php'; |
| 132 |
|
| 133 |
// Admin functions and hooks |
| 134 |
if (is_admin()) { |
| 135 |
require_once WOO_MAILERLITE_DIR . 'includes/admin/hooks.php'; |
| 136 |
require_once WOO_MAILERLITE_DIR . 'includes/admin/ajax.php'; |
| 137 |
require_once WOO_MAILERLITE_DIR . 'includes/admin/meta-boxes.php'; |
| 138 |
} |
| 139 |
|
| 140 |
// Include our integration class. |
| 141 |
include_once WOO_MAILERLITE_DIR . 'includes/class.woo-mailerlite-integration.php'; |
| 142 |
|
| 143 |
// Register the integration. |
| 144 |
add_action('plugins_loaded', function() { |
| 145 |
new Woo_MailerLite_Integration(); |
| 146 |
}, 12); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Fire some hooks |
| 151 |
*/ |
| 152 |
private function hooks() |
| 153 |
{ |
| 154 |
add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2); |
| 155 |
|
| 156 |
$auto_update = get_option('woo_ml_auto_update'); |
| 157 |
|
| 158 |
if ($auto_update != false) { |
| 159 |
add_filter('auto_update_plugin', array($this, 'plugin_update'), PHP_INT_MAX, 2); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Add plugin action links |
| 165 |
* |
| 166 |
* @param $links |
| 167 |
* @param $file |
| 168 |
* |
| 169 |
* @return mixed |
| 170 |
*/ |
| 171 |
public function plugin_action_links($links, $file) |
| 172 |
{ |
| 173 |
|
| 174 |
if ($file !== 'woo-mailerlite/woo-mailerlite.php') { |
| 175 |
return $links; |
| 176 |
} |
| 177 |
|
| 178 |
if (!$this->check_server_requirements()) { |
| 179 |
$info = '<span style="color: #ff0000; font-weight: bold;">' . sprintf(esc_html__('PHP Version %1$s or newer required', |
| 180 |
'woo-mailerlaite'), WOO_MAILERLITE_MIN_PHP_VERSION) . '</span>'; |
| 181 |
array_unshift($links, $info); |
| 182 |
|
| 183 |
return $links; |
| 184 |
} |
| 185 |
|
| 186 |
if (class_exists('WC_Integration')) { |
| 187 |
$settings_link = '<a href="' . admin_url('admin.php?page=mailerlite') . '">' . esc_html__('Settings', |
| 188 |
'woo-mailerlite') . '</a>'; |
| 189 |
array_unshift($links, $settings_link); |
| 190 |
} |
| 191 |
|
| 192 |
return $links; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Enable always update for MailerLite plugin |
| 197 |
* |
| 198 |
* @param $update |
| 199 |
* @param $item |
| 200 |
* |
| 201 |
* @return bool |
| 202 |
*/ |
| 203 |
public function plugin_update($update, $item) |
| 204 |
{ |
| 205 |
|
| 206 |
$plugins = [ |
| 207 |
'woo-mailerlite' |
| 208 |
]; |
| 209 |
|
| 210 |
if (isset($item->slug)) { |
| 211 |
|
| 212 |
if (in_array($item->slug, $plugins)) { |
| 213 |
// Always update plugins |
| 214 |
return true; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return $update; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Internationalization |
| 224 |
* |
| 225 |
* @access public |
| 226 |
* @return void |
| 227 |
* @since 1.0.0 |
| 228 |
*/ |
| 229 |
public function load_textdomain() |
| 230 |
{ |
| 231 |
// Set filter for language directory |
| 232 |
$lang_dir = WOO_MAILERLITE_DIR . '/languages/'; |
| 233 |
$lang_dir = apply_filters('woo_mailerlite_languages_directory', $lang_dir); |
| 234 |
|
| 235 |
// Traditional WordPress plugin locale filter |
| 236 |
$locale = apply_filters('plugin_locale', get_locale(), 'woo-mailerlite'); |
| 237 |
$mofile = sprintf('%1$s-%2$s.mo', 'woo-mailerlite', $locale); |
| 238 |
|
| 239 |
// Setup paths to current locale file |
| 240 |
$mofile_local = $lang_dir . $mofile; |
| 241 |
$mofile_global = WP_LANG_DIR . '/woo-mailerlite/' . $mofile; |
| 242 |
|
| 243 |
if (file_exists($mofile_global)) { |
| 244 |
// Look in global /wp-content/languages/woo-mailerlite/ folder |
| 245 |
load_textdomain('woo-mailerlite', $mofile_global); |
| 246 |
} elseif (file_exists($mofile_local)) { |
| 247 |
// Look in local /wp-content/plugins/woo-mailerlite/languages/ folder |
| 248 |
load_textdomain('woo-mailerlite', $mofile_local); |
| 249 |
} else { |
| 250 |
// Load the default language files |
| 251 |
load_plugin_textdomain('woo-mailerlite', false, $lang_dir); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Check web server requirements |
| 257 |
* |
| 258 |
* @return bool |
| 259 |
*/ |
| 260 |
private function check_server_requirements() |
| 261 |
{ |
| 262 |
|
| 263 |
if (version_compare(phpversion(), WOO_MAILERLITE_MIN_PHP_VERSION, '<')) { |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
return true; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Check plugin is updated |
| 272 |
* |
| 273 |
* @return bool |
| 274 |
*/ |
| 275 |
|
| 276 |
private function check_plugin_version() |
| 277 |
{ |
| 278 |
|
| 279 |
if (!is_admin()) { |
| 280 |
return false; |
| 281 |
} |
| 282 |
|
| 283 |
return true; |
| 284 |
} |
| 285 |
} |
| 286 |
} // End if class_exists check |
| 287 |
|
| 288 |
/** |
| 289 |
* The main function responsible for returning the one true Woo_Mailerlite |
| 290 |
* instance to functions everywhere |
| 291 |
* @return \Woo_Mailerlite The one true Woo_Mailerlite |
| 292 |
* |
| 293 |
* @since 1.0.0 |
| 294 |
*/ |
| 295 |
function woo_ml_load() |
| 296 |
{ |
| 297 |
return Woo_Mailerlite::instance(); |
| 298 |
} |
| 299 |
|
| 300 |
add_action('plugins_loaded', 'woo_ml_load'); |
| 301 |
|
| 302 |
function woo_ml_deactivate() |
| 303 |
{ |
| 304 |
require_once WOO_MAILERLITE_DIR . 'includes/functions.php'; |
| 305 |
ShopSettings::getInstance()->toggleShopConnection(0); |
| 306 |
} |
| 307 |
|
| 308 |
register_deactivation_hook(__FILE__, 'woo_ml_deactivate'); |
| 309 |
|
| 310 |
function woo_ml_activate() |
| 311 |
{ |
| 312 |
require_once WOO_MAILERLITE_DIR . 'includes/functions.php'; |
| 313 |
initClasses(); |
| 314 |
ShopSettings::getInstance()->toggleShopConnection(1); |
| 315 |
} |
| 316 |
|
| 317 |
register_activation_hook(__FILE__, 'woo_ml_activate'); |
| 318 |
|
| 319 |
function woo_mlb_reload_checkout() |
| 320 |
{ |
| 321 |
require_once WOO_MAILERLITE_DIR . 'includes/functions.php'; |
| 322 |
initClasses(); |
| 323 |
CheckoutProcess::getInstance()->reloadCheckout(); |
| 324 |
} |
| 325 |
|
| 326 |
add_action('init', 'woo_mlb_reload_checkout'); |
| 327 |
|
| 328 |
function woo_ml_deactivate_woo_ml_plugin($deactivate = false) |
| 329 |
{ |
| 330 |
if ($deactivate) { |
| 331 |
woo_mlb_reload_checkout(); |
| 332 |
|
| 333 |
require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 334 |
deactivate_plugins(plugin_basename(__FILE__), true); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
add_action('woocommerce_blocks_loaded', function () { |
| 339 |
|
| 340 |
if (class_exists('\Automattic\WooCommerce\Blocks\Package') && |
| 341 |
interface_exists('\Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface')) { |
| 342 |
|
| 343 |
require_once __DIR__ . '/includes/woo-mailerlite-blocks-integration.php'; |
| 344 |
|
| 345 |
add_action( |
| 346 |
'woocommerce_blocks_checkout_block_registration', |
| 347 |
function ($integration_registry) { |
| 348 |
$integration_registry->register(new WooMlBlock_Integration()); |
| 349 |
} |
| 350 |
); |
| 351 |
|
| 352 |
add_filter( |
| 353 |
'__experimental_woocommerce_blocks_add_data_attributes_to_block', |
| 354 |
function ($allowed_blocks) { |
| 355 |
$allowed_blocks[] = 'mailerlite-block/woo-mailerlite'; |
| 356 |
|
| 357 |
return $allowed_blocks; |
| 358 |
}, |
| 359 |
10, |
| 360 |
1 |
| 361 |
); |
| 362 |
} |
| 363 |
}); |
| 364 |
|
| 365 |
function register_WooML_block_category($categories) |
| 366 |
{ |
| 367 |
|
| 368 |
return array_merge( |
| 369 |
$categories, |
| 370 |
[ |
| 371 |
[ |
| 372 |
'slug' => 'woo-mailerlite', |
| 373 |
'title' => __('MailerLite - WooCommerce integration block', 'woo-mailerlite'), |
| 374 |
], |
| 375 |
] |
| 376 |
); |
| 377 |
} |
| 378 |
|
| 379 |
function initClasses() |
| 380 |
{ |
| 381 |
// Classes |
| 382 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/Singleton.php'; |
| 383 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/data/TrackingData.php'; |
| 384 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/settings/MailerLiteSettings.php'; |
| 385 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/settings/ShopSettings.php'; |
| 386 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/settings/ApiSettings.php'; |
| 387 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/settings/SynchronizeSettings.php'; |
| 388 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/settings/ResetSettings.php'; |
| 389 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/process/OrderProcess.php'; |
| 390 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/process/ProductProcess.php'; |
| 391 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/process/CartProcess.php'; |
| 392 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/process/CheckoutProcess.php'; |
| 393 |
require_once WOO_MAILERLITE_DIR . 'includes/classes/process/OrderSyncProcess.php'; |
| 394 |
|
| 395 |
|
| 396 |
} |
| 397 |
|
| 398 |
add_action('block_categories_all', 'register_WooML_block_category', 10, 2); |
| 399 |
|
| 400 |
|
| 401 |
/** |
| 402 |
* Declaring MailerLite HPOS compatibility |
| 403 |
*/ |
| 404 |
function woo_ml_hpos_compatibility() |
| 405 |
{ |
| 406 |
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { |
| 407 |
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
add_action( 'before_woocommerce_init', 'woo_ml_hpos_compatibility'); |
| 412 |
|