| 1 |
<?php |
| 2 |
/** |
| 3 |
* The core plugin class. |
| 4 |
* |
| 5 |
* @package Notifier |
| 6 |
*/ |
| 7 |
class Notifier { |
| 8 |
/** |
| 9 |
* The single instance of the class. |
| 10 |
*/ |
| 11 |
protected static $_instance = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* Notifier Constructor. |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
$this->define_constants(); |
| 18 |
$this->includes(); |
| 19 |
$this->init_hooks(); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Define Constants. |
| 24 |
*/ |
| 25 |
private function define_constants() { |
| 26 |
$this->define( 'NOTIFIER_VERSION', '2.7.1' ); |
| 27 |
$this->define( 'NOTIFIER_NAME', 'notifier' ); |
| 28 |
$this->define( 'NOTIFIER_PREFIX', 'notifier_' ); |
| 29 |
$this->define( 'NOTIFIER_URL', trailingslashit( plugins_url( '', dirname(__FILE__) ) ) ); |
| 30 |
$this->define( 'NOTIFIER_APP_API_URL', 'https://app.wanotifier.com/api/v1/' ); |
| 31 |
$this->define( 'NOTIFIER_ACTIVITY_TABLE_NAME', 'notifier_activity_log' ); |
| 32 |
$this->define( 'NOTIFIER_CART_ABANDONMENT_TABLE', 'notifier_cart_abandonment' ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Define constant if not already set. |
| 37 |
* |
| 38 |
* @param string $name Constant name. |
| 39 |
* @param string|bool $value Constant value. |
| 40 |
*/ |
| 41 |
private function define( $name, $value ) { |
| 42 |
if ( ! defined( $name ) ) { |
| 43 |
define( $name, $value ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Main Notifier Instance. |
| 49 |
* |
| 50 |
* @return Notifier instance. |
| 51 |
*/ |
| 52 |
public static function get_instance() { |
| 53 |
if ( is_null( self::$_instance ) ) { |
| 54 |
self::$_instance = new self(); |
| 55 |
} |
| 56 |
return self::$_instance; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Include required core files used in admin and on the frontend. |
| 61 |
*/ |
| 62 |
private function includes() { |
| 63 |
// Functions |
| 64 |
require_once NOTIFIER_PATH . 'includes/functions/functions-notifier-helpers.php'; |
| 65 |
require_once NOTIFIER_PATH . 'includes/functions/functions-notifier-meta-box-fields.php'; |
| 66 |
require_once NOTIFIER_PATH . 'libraries/action-scheduler/action-scheduler.php'; |
| 67 |
|
| 68 |
// Classes |
| 69 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-admin-notices.php'; |
| 70 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-dashboard.php'; |
| 71 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-notification-merge-tags.php'; |
| 72 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-notification-triggers.php'; |
| 73 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-settings.php'; |
| 74 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-frontend.php'; |
| 75 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-tools.php'; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Hook into actions and filters. |
| 80 |
*/ |
| 81 |
private function init_hooks() { |
| 82 |
register_activation_hook ( NOTIFIER_FILE, array( $this, 'activate') ); |
| 83 |
register_deactivation_hook ( NOTIFIER_FILE, array( $this, 'deactivate') ); |
| 84 |
|
| 85 |
add_action( 'after_setup_theme', array( 'Notifier_Admin_Notices', 'init' ) ); |
| 86 |
add_action( 'after_setup_theme', array( 'Notifier_Dashboard', 'init' ) ); |
| 87 |
add_action( 'after_setup_theme', array( 'Notifier_Notification_Merge_Tags', 'init' ) ); |
| 88 |
add_action( 'after_setup_theme', array( 'Notifier_Notification_Triggers', 'init' ) ); |
| 89 |
add_action( 'after_setup_theme', array( 'Notifier_Settings', 'init' ) ); |
| 90 |
add_action( 'after_setup_theme', array( 'Notifier_Frontend', 'init' ) ); |
| 91 |
add_action( 'admin_enqueue_scripts', array( $this , 'admin_scripts') ); |
| 92 |
add_action( 'wp_enqueue_scripts', array( $this , 'frontend_scripts') ); |
| 93 |
add_action( 'in_admin_header', array( $this , 'embed_page_header' ) ); |
| 94 |
|
| 95 |
add_action( 'after_setup_theme', array( $this, 'maybe_include_integrations' ) ); |
| 96 |
add_action( 'after_setup_theme', array( 'Notifier_Tools', 'init' ) ); |
| 97 |
|
| 98 |
add_action( 'wp_loaded', array( $this, 'setup_activity_log' ) ); |
| 99 |
add_action( 'notifier_clean_old_logs', array( $this, 'notifier_delete_old_activity_logs' ) ); |
| 100 |
|
| 101 |
add_action( 'wp_loaded', array( $this, 'create_cart_abandonment_table' ) ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Setup during plugin activation |
| 106 |
*/ |
| 107 |
public function activate() { |
| 108 |
$notifier_plugin_data = get_option('notifier_meta', array()); |
| 109 |
if (isset($notifier_plugin_data['as_clear_log']) && $notifier_plugin_data['as_clear_log'] === 'yes') { |
| 110 |
unset($notifier_plugin_data['as_clear_log']); |
| 111 |
update_option('notifier_meta', $notifier_plugin_data); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Setup during plugin deactivation |
| 117 |
*/ |
| 118 |
public function deactivate() { |
| 119 |
as_unschedule_action('notifier_check_cart_abandonment'); |
| 120 |
as_unschedule_action('notifier_clean_old_logs'); |
| 121 |
$notifier_plugin_data = get_option('notifier_meta', array()); |
| 122 |
$notifier_plugin_data['as_clear_log'] = 'yes'; |
| 123 |
update_option('notifier_meta', $notifier_plugin_data); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Check if the plugin was updated and run the upgrade routine if necessary. |
| 128 |
*/ |
| 129 |
public function setup_activity_log() { |
| 130 |
$notifier_plugin_data = get_option('notifier_meta', array()); |
| 131 |
if (!isset($notifier_plugin_data['activity_log_table']) && $notifier_plugin_data['activity_log_table'] !== 'yes') { |
| 132 |
self::create_notifier_activity_log_table(); |
| 133 |
} |
| 134 |
|
| 135 |
$args = array(); |
| 136 |
$as_clear_log = isset($notifier_plugin_data['as_clear_log']) ? $notifier_plugin_data['as_clear_log'] : 'no'; |
| 137 |
if ($as_clear_log !== 'yes') { |
| 138 |
if (false === as_next_scheduled_action('notifier_clean_old_logs')) { |
| 139 |
as_schedule_recurring_action(time(), DAY_IN_SECONDS, 'notifier_clean_old_logs', $args); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Check if plugin's page |
| 146 |
*/ |
| 147 |
public static function is_notifier_page() { |
| 148 |
$current_screen = get_current_screen(); |
| 149 |
if ( strpos($current_screen->id, NOTIFIER_NAME) !== false) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Add admin scripts and styles |
| 158 |
*/ |
| 159 |
public function admin_scripts () { |
| 160 |
if (!self::is_notifier_page()) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
// Select2 |
| 165 |
wp_enqueue_script( |
| 166 |
NOTIFIER_NAME . '-select2-js', |
| 167 |
NOTIFIER_URL . 'assets/js/select2.min.js', |
| 168 |
array('jquery'), |
| 169 |
NOTIFIER_VERSION, |
| 170 |
true |
| 171 |
); |
| 172 |
|
| 173 |
// Admin JS file |
| 174 |
wp_enqueue_script( |
| 175 |
NOTIFIER_NAME . '-admin-js', |
| 176 |
NOTIFIER_URL . 'assets/js/admin.js', |
| 177 |
array('jquery'), |
| 178 |
NOTIFIER_VERSION, |
| 179 |
true |
| 180 |
); |
| 181 |
wp_localize_script( |
| 182 |
NOTIFIER_NAME . '-admin-js', |
| 183 |
'notifierObj', |
| 184 |
apply_filters( 'notifier_js_variables', array('ajaxurl' => admin_url( 'admin-ajax.php' ) ) ) |
| 185 |
); |
| 186 |
|
| 187 |
// Styles |
| 188 |
wp_enqueue_style( |
| 189 |
NOTIFIER_NAME . '-admin-css', |
| 190 |
NOTIFIER_URL . 'assets/css/admin.css', |
| 191 |
null, |
| 192 |
NOTIFIER_VERSION |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Add frontend scripts and styles |
| 198 |
*/ |
| 199 |
public function frontend_scripts () { |
| 200 |
if('yes' === get_option('notifier_ctc_enable')){ |
| 201 |
// Styles |
| 202 |
wp_enqueue_style( |
| 203 |
NOTIFIER_NAME . '-frontend-css', |
| 204 |
NOTIFIER_URL . 'assets/css/frontend.css', |
| 205 |
null, |
| 206 |
NOTIFIER_VERSION |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
if ('yes' === get_option('notifier_enable_abandonment_cart_tracking', 'no') && is_checkout()) { |
| 211 |
wp_enqueue_script( |
| 212 |
NOTIFIER_NAME . '-js', |
| 213 |
NOTIFIER_URL . 'assets/js/script.js', |
| 214 |
array('jquery'), |
| 215 |
NOTIFIER_VERSION, |
| 216 |
true |
| 217 |
); |
| 218 |
|
| 219 |
wp_localize_script( |
| 220 |
NOTIFIER_NAME . '-js', |
| 221 |
'notifierFrontObj', |
| 222 |
array( |
| 223 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 224 |
'security' => wp_create_nonce('notifier_save_cart_abandonment_data'), |
| 225 |
) |
| 226 |
); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Set up a div for the header embed to render into. |
| 232 |
* The initial contents here are meant as a place loader for when the PHP page initialy loads. |
| 233 |
*/ |
| 234 |
public static function embed_page_header() { |
| 235 |
if (!self::is_notifier_page()) { |
| 236 |
return; |
| 237 |
} |
| 238 |
$current_screen = get_current_screen(); |
| 239 |
$cpt = ( '' !== $current_screen->post_type) ? $current_screen->post_type : ''; |
| 240 |
$tax = ( '' !== $current_screen->taxonomy) ? $current_screen->taxonomy : ''; |
| 241 |
?> |
| 242 |
<div id="notifier-admin-header" data-post-type="<?php echo esc_attr($cpt); ?>"> |
| 243 |
<div class="notifier-admin-header-content"> |
| 244 |
<div class="header-page-title w-30 d-flex align-content-center"> |
| 245 |
<a class="header-logo" href="admin.php?page=notifier"><img src="<?php echo NOTIFIER_URL; ?>assets/images/favicon.svg"></a> |
| 246 |
<h2><?php echo esc_attr(get_admin_page_title()); ?></h2> |
| 247 |
</div> |
| 248 |
<div class="header-action-links w-30 d-flex justify-content-end"> |
| 249 |
<span class="review-us-link">Review us: <a href="https://wordpress.org/support/plugin/notifier/reviews/#new-post" target="_blank">⭐⭐⭐⭐⭐</a></span> |
| 250 |
<span class="header-version">Version: <?php echo esc_html(NOTIFIER_VERSION); ?></span> |
| 251 |
<a href="https://wanotifier.com/support/" target="_blank">Help</a> |
| 252 |
</div> |
| 253 |
</div> |
| 254 |
</div> |
| 255 |
<?php |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* For sending API requests |
| 260 |
*/ |
| 261 |
public static function send_api_request ( $endpoint, $args, $method = 'POST', $headers = array() ) { |
| 262 |
$api_key = get_option('notifier_api_key'); |
| 263 |
$api_key = trim($api_key); |
| 264 |
if('' == $api_key) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
if(empty($headers)){ |
| 269 |
$headers = array( |
| 270 |
'Content-Type' => 'application/json; charset=utf-8' |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
$request_url = NOTIFIER_APP_API_URL . $endpoint . '?key=' . esc_attr($api_key); |
| 275 |
$request_args = array( |
| 276 |
'method' => $method, |
| 277 |
'headers' => $headers, |
| 278 |
'timeout' => 120, |
| 279 |
'body' => json_encode($args), |
| 280 |
'sslverify' => false |
| 281 |
); |
| 282 |
|
| 283 |
$response = wp_remote_request( $request_url, $request_args ); |
| 284 |
|
| 285 |
$response_body = wp_remote_retrieve_body( $response ); |
| 286 |
|
| 287 |
if ( is_wp_error( $response ) ) { |
| 288 |
error_log( $response->get_error_message() ); |
| 289 |
return false; |
| 290 |
} else { |
| 291 |
$response_body = wp_remote_retrieve_body( $response ); |
| 292 |
return json_decode($response_body); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Load Woocommerce class if it's present is activated |
| 298 |
*/ |
| 299 |
public static function maybe_include_integrations () { |
| 300 |
// Woocommerce |
| 301 |
if ( class_exists( 'WooCommerce' ) ) { |
| 302 |
require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-woocommerce.php'; |
| 303 |
Notifier_Woocommerce::init(); |
| 304 |
} |
| 305 |
if ( ! class_exists( 'WC_Session' ) ) { |
| 306 |
include_once( WP_PLUGIN_DIR . '/woocommerce/includes/abstracts/abstract-wc-session.php' ); |
| 307 |
} |
| 308 |
|
| 309 |
// WooCommerce Cart Abandonment Recovery by CartFlows |
| 310 |
if ( class_exists( 'CARTFLOWS_CA_Loader' ) && defined('CARTFLOWS_CA_VER') && version_compare(CARTFLOWS_CA_VER, '1.2.25', '>=')) { |
| 311 |
require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-wcar.php'; |
| 312 |
Notifier_WCAR::init(); |
| 313 |
} |
| 314 |
|
| 315 |
// Gravity Forms |
| 316 |
if ( class_exists( 'GFCommon' ) ) { |
| 317 |
require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-gravityforms.php'; |
| 318 |
Notifier_GravityForms::init(); |
| 319 |
} |
| 320 |
|
| 321 |
// Contact Form 7 |
| 322 |
if ( class_exists( 'WPCF7' ) ) { |
| 323 |
require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-cf7.php'; |
| 324 |
Notifier_ContactForm7::init(); |
| 325 |
} |
| 326 |
|
| 327 |
// WPForms |
| 328 |
if ( class_exists( 'WPForms' ) ) { |
| 329 |
require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-wpforms.php'; |
| 330 |
Notifier_WPForms::init(); |
| 331 |
} |
| 332 |
|
| 333 |
// Ninja Forms |
| 334 |
if ( class_exists( 'Ninja_Forms' ) ) { |
| 335 |
require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-ninjaforms.php'; |
| 336 |
Notifier_NinjaForms::init(); |
| 337 |
} |
| 338 |
|
| 339 |
//FrmForm |
| 340 |
if ( class_exists( 'FrmForm' ) ) { |
| 341 |
require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-formidable.php'; |
| 342 |
Notifier_Formidable::init(); |
| 343 |
} |
| 344 |
|
| 345 |
//WPFluentForm |
| 346 |
if ( function_exists( 'fluentFormApi' ) ) { |
| 347 |
require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-fluentforms.php'; |
| 348 |
Notifier_FluentForms::init(); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Is connection to WANotifier.com active? |
| 354 |
*/ |
| 355 |
public static function is_api_active () { |
| 356 |
$activated = get_option(NOTIFIER_PREFIX . 'api_activated'); |
| 357 |
if('yes' == $activated) { |
| 358 |
return true; |
| 359 |
} |
| 360 |
else{ |
| 361 |
return false; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Create New db table-- wanotifier_activity_log |
| 367 |
*/ |
| 368 |
public static function create_notifier_activity_log_table() { |
| 369 |
global $wpdb; |
| 370 |
$table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME; |
| 371 |
|
| 372 |
$charset_collate = $wpdb->get_charset_collate(); |
| 373 |
|
| 374 |
// Include IF NOT EXISTS clause in the CREATE TABLE query |
| 375 |
$sql = "CREATE TABLE IF NOT EXISTS $table_name ( |
| 376 |
log_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 377 |
timestamp timestamp NOT NULL, |
| 378 |
message text NOT NULL, |
| 379 |
type varchar(16) NOT NULL, |
| 380 |
PRIMARY KEY (log_id), |
| 381 |
INDEX idx_timestamp (timestamp) |
| 382 |
) $charset_collate;"; |
| 383 |
|
| 384 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 385 |
dbDelta( $sql ); |
| 386 |
|
| 387 |
$notifier_plugin_data = get_option('notifier_meta', array()); |
| 388 |
$notifier_plugin_data['activity_log_table'] = 'yes'; |
| 389 |
update_option('notifier_meta', $notifier_plugin_data); |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Delete old log from activity table according to interval |
| 394 |
*/ |
| 395 |
public function notifier_delete_old_activity_logs() { |
| 396 |
global $wpdb; |
| 397 |
$table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME; |
| 398 |
$retention_time = apply_filters( 'notifier_logs_retention_time', 7 ); |
| 399 |
$retention_time = intval($retention_time); |
| 400 |
$wpdb->query( $wpdb->prepare( "DELETE FROM `$table_name` WHERE timestamp <= DATE_SUB(NOW(), INTERVAL %d DAY)", $retention_time ) ); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Create New db table-- notifier_cart_abandonment |
| 405 |
*/ |
| 406 |
public static function create_cart_abandonment_table() { |
| 407 |
$notifier_plugin_data = get_option('notifier_meta', array()); |
| 408 |
if (!isset($notifier_plugin_data['cart_abandonment_table']) && $notifier_plugin_data['cart_abandonment_table'] !== 'yes') { |
| 409 |
global $wpdb; |
| 410 |
$table_name = $wpdb->prefix . NOTIFIER_CART_ABANDONMENT_TABLE; |
| 411 |
$charset_collate = $wpdb->get_charset_collate(); |
| 412 |
|
| 413 |
// Updated CREATE TABLE query for notifier_cart_abandonment table |
| 414 |
$sql = "CREATE TABLE IF NOT EXISTS $table_name ( |
| 415 |
session_id BIGINT(20) AUTO_INCREMENT PRIMARY KEY, |
| 416 |
session_key VARCHAR(16) NULL, |
| 417 |
phone_number VARCHAR(15) NULL, |
| 418 |
cart_data LONGTEXT NULL, |
| 419 |
cart_total DECIMAL(10,2) NULL, |
| 420 |
coupon_data LONGTEXT NULL, |
| 421 |
customer_data LONGTEXT NULL, |
| 422 |
order_id BIGINT(11) DEFAULT 0, |
| 423 |
triggered TINYINT(1) DEFAULT 0, |
| 424 |
created_time DATETIME NULL, |
| 425 |
updated_time DATETIME NULL, |
| 426 |
KEY session_key (session_key) -- Index for session_key |
| 427 |
) $charset_collate;"; |
| 428 |
|
| 429 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 430 |
dbDelta( $sql ); |
| 431 |
|
| 432 |
$notifier_plugin_data['cart_abandonment_table'] = 'yes'; |
| 433 |
update_option('notifier_meta', $notifier_plugin_data); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|