| @@ -1,5 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | + exit; | |
| 4 | +} | |
| 5 | + | |
| 2 | 6 | /** |
| 3 | 7 | * The core plugin class. |
| 4 | 8 | * |
| 5 | 9 | * @package Notifier |
| @@ -22,14 +26,16 @@ | ||
| 22 | 26 | /** |
| 23 | 27 | * Define Constants. |
| 24 | 28 | */ |
| 25 | 29 | private function define_constants() { |
| 26 | - $this->define( 'NOTIFIER_VERSION', '0.1.1' ); | |
| 30 | + $this->define( 'NOTIFIER_VERSION', '3.1.1' ); | |
| 27 | 31 | $this->define( 'NOTIFIER_NAME', 'notifier' ); |
| 28 | 32 | $this->define( 'NOTIFIER_PREFIX', 'notifier_' ); |
| 29 | 33 | $this->define( 'NOTIFIER_URL', trailingslashit( plugins_url( '', dirname(__FILE__) ) ) ); |
| 30 | - $this->define( 'NOTIFIER_WA_API_VERSION', 'v14.0' ); | |
| 31 | - $this->define( 'NOTIFIER_WA_API_URL', 'https://graph.facebook.com/' . NOTIFIER_WA_API_VERSION . '/' ); | |
| 34 | + $this->define( 'NOTIFIER_APP_BASE_URL', 'https://app.wanotifier.com' ); | |
| 35 | + $this->define( 'NOTIFIER_APP_API_PATH', 'api/v1' ); | |
| 36 | + $this->define( 'NOTIFIER_ACTIVITY_TABLE_NAME', 'notifier_activity_log' ); | |
| 37 | + $this->define( 'NOTIFIER_CART_ABANDONMENT_TABLE', 'notifier_cart_abandonment' ); | |
| 32 | 38 | } |
| 33 | 39 | |
| 34 | 40 | /** |
| 35 | 41 | * Define constant if not already set. |
| @@ -58,23 +64,20 @@ | ||
| 58 | 64 | /** |
| 59 | 65 | * Include required core files used in admin and on the frontend. |
| 60 | 66 | */ |
| 61 | 67 | private function includes() { |
| 62 | - // Libraries | |
| 63 | - require_once NOTIFIER_PATH . 'libraries/action-scheduler/action-scheduler.php'; | |
| 64 | - | |
| 65 | 68 | // Functions |
| 66 | 69 | require_once NOTIFIER_PATH . 'includes/functions/functions-notifier-helpers.php'; |
| 67 | - require_once NOTIFIER_PATH . 'includes/functions/functions-notifier-meta-box-fields.php'; | |
| 70 | + require_once NOTIFIER_PATH . 'libraries/action-scheduler/action-scheduler.php'; | |
| 68 | 71 | |
| 69 | 72 | // Classes |
| 70 | 73 | require_once NOTIFIER_PATH . 'includes/classes/class-notifier-admin-notices.php'; |
| 71 | - require_once NOTIFIER_PATH . 'includes/classes/class-notifier-dashboard.php'; | |
| 72 | - require_once NOTIFIER_PATH . 'includes/classes/class-notifier-message-templates.php'; | |
| 73 | - require_once NOTIFIER_PATH . 'includes/classes/class-notifier-contacts.php'; | |
| 74 | - require_once NOTIFIER_PATH . 'includes/classes/class-notifier-notifications.php'; | |
| 74 | + require_once NOTIFIER_PATH . 'includes/classes/class-notifier-connection.php'; | |
| 75 | + require_once NOTIFIER_PATH . 'includes/classes/class-notifier-migration.php'; | |
| 76 | + require_once NOTIFIER_PATH . 'includes/classes/class-notifier-backend.php'; | |
| 77 | + require_once NOTIFIER_PATH . 'includes/classes/class-notifier-notification-merge-tags.php'; | |
| 75 | 78 | require_once NOTIFIER_PATH . 'includes/classes/class-notifier-notification-triggers.php'; |
| 76 | - require_once NOTIFIER_PATH . 'includes/classes/class-notifier-settings.php'; | |
| 79 | + require_once NOTIFIER_PATH . 'includes/classes/class-notifier-frontend.php'; | |
| 77 | 80 | } |
| 78 | 81 | |
| 79 | 82 | /** |
| 80 | 83 | * Hook into actions and filters. |
| @@ -79,54 +82,89 @@ | ||
| 79 | 82 | /** |
| 80 | 83 | * Hook into actions and filters. |
| 81 | 84 | */ |
| 82 | 85 | private function init_hooks() { |
| 83 | - register_activation_hook ( NOTIFIER_FILE, array( $this, 'install') ); | |
| 86 | + register_activation_hook ( NOTIFIER_FILE, array( $this, 'activate') ); | |
| 87 | + register_deactivation_hook ( NOTIFIER_FILE, array( $this, 'deactivate') ); | |
| 84 | 88 | |
| 85 | - add_action( 'plugins_loaded', array( 'Notifier_Dashboard', 'init' ) ); | |
| 86 | - add_action( 'plugins_loaded', array( 'Notifier_Message_Templates', 'init' ) ); | |
| 87 | - add_action( 'plugins_loaded', array( 'Notifier_Contacts', 'init' ) ); | |
| 88 | - add_action( 'plugins_loaded', array( 'Notifier_Notifications', 'init' ) ); | |
| 89 | - add_action( 'plugins_loaded', array( 'Notifier_Notification_Triggers', 'init' ) ); | |
| 90 | - add_action( 'plugins_loaded', array( 'Notifier_Settings', 'init' ) ); | |
| 91 | - add_action( 'plugins_loaded', array( $this, 'maybe_include_woocoomerce_class' ) ); | |
| 92 | - | |
| 93 | - add_filter( 'init', array( $this , 'handle_webhook_requests') ); | |
| 94 | - | |
| 89 | + add_action( 'after_setup_theme', array( 'Notifier_Admin_Notices', 'init' ) ); | |
| 90 | + add_action( 'after_setup_theme', array( 'Notifier_Connection', 'init' ) ); | |
| 91 | + add_action( 'after_setup_theme', array( 'Notifier_Migration', 'init' ) ); | |
| 92 | + add_action( 'after_setup_theme', array( 'Notifier_Backend', 'init' ) ); | |
| 93 | + add_action( 'after_setup_theme', array( 'Notifier_Notification_Merge_Tags', 'init' ) ); | |
| 94 | + add_action( 'after_setup_theme', array( 'Notifier_Notification_Triggers', 'init' ) ); | |
| 95 | + add_action( 'after_setup_theme', array( 'Notifier_Frontend', 'init' ) ); | |
| 95 | 96 | add_action( 'admin_enqueue_scripts', array( $this , 'admin_scripts') ); |
| 97 | + add_action( 'wp_enqueue_scripts', array( $this , 'frontend_scripts') ); | |
| 96 | 98 | add_action( 'in_admin_header', array( $this , 'embed_page_header' ) ); |
| 97 | 99 | |
| 98 | - add_action( 'removable_query_args', array( $this , 'remove_admin_query_args') ); | |
| 100 | + add_action( 'after_setup_theme', array( $this, 'maybe_include_integrations' ) ); | |
| 99 | 101 | |
| 100 | - add_action( 'admin_footer', array($this, 'add_admin_html_templates') ); | |
| 102 | + add_action( 'init', array( $this, 'load_textdomain' ) ); | |
| 103 | + add_action( 'wp_loaded', array( $this, 'setup_activity_log' ) ); | |
| 104 | + add_action( 'notifier_clean_old_logs', array( $this, 'notifier_delete_old_activity_logs' ) ); | |
| 105 | + | |
| 106 | + add_action( 'wp_loaded', array( $this, 'create_cart_abandonment_table' ) ); | |
| 101 | 107 | } |
| 102 | 108 | |
| 103 | 109 | /** |
| 110 | + * Load plugin text domain for translations. | |
| 111 | + */ | |
| 112 | + public function load_textdomain() { | |
| 113 | + load_plugin_textdomain( 'notifier', false, dirname( plugin_basename( NOTIFIER_FILE ) ) . '/languages' ); | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 104 | 117 | * Setup during plugin activation |
| 105 | 118 | */ |
| 106 | - public function install() { | |
| 107 | - $verify_token = get_option(NOTIFIER_PREFIX . 'verify_token'); | |
| 108 | - if (!$verify_token) { | |
| 109 | - $bytes = random_bytes(20); | |
| 110 | - $verify_token = NOTIFIER_NAME . '-' . substr(bin2hex($bytes), 0, 10); | |
| 111 | - update_option(NOTIFIER_PREFIX . 'verify_token', $verify_token); | |
| 119 | + public function activate() { | |
| 120 | + $notifier_plugin_data = get_option('notifier_meta', array()); | |
| 121 | + if (isset($notifier_plugin_data['as_clear_log']) && $notifier_plugin_data['as_clear_log'] === 'yes') { | |
| 122 | + unset($notifier_plugin_data['as_clear_log']); | |
| 123 | + update_option('notifier_meta', $notifier_plugin_data); | |
| 112 | 124 | } |
| 113 | 125 | } |
| 114 | 126 | |
| 115 | 127 | /** |
| 128 | + * Setup during plugin deactivation | |
| 129 | + */ | |
| 130 | + public function deactivate() { | |
| 131 | + as_unschedule_action('notifier_check_cart_abandonment'); | |
| 132 | + as_unschedule_action('notifier_clean_old_logs'); | |
| 133 | + $notifier_plugin_data = get_option('notifier_meta', array()); | |
| 134 | + $notifier_plugin_data['as_clear_log'] = 'yes'; | |
| 135 | + update_option('notifier_meta', $notifier_plugin_data); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Check if the plugin was updated and run the upgrade routine if necessary. | |
| 140 | + */ | |
| 141 | + public function setup_activity_log() { | |
| 142 | + $notifier_plugin_data = get_option('notifier_meta', array()); | |
| 143 | + if ( ! isset( $notifier_plugin_data['activity_log_table'] ) || $notifier_plugin_data['activity_log_table'] !== 'yes' ) { | |
| 144 | + self::create_notifier_activity_log_table(); | |
| 145 | + } | |
| 146 | + | |
| 147 | + $args = array(); | |
| 148 | + $as_clear_log = isset($notifier_plugin_data['as_clear_log']) ? $notifier_plugin_data['as_clear_log'] : 'no'; | |
| 149 | + if ($as_clear_log !== 'yes') { | |
| 150 | + if (false === as_next_scheduled_action('notifier_clean_old_logs')) { | |
| 151 | + as_schedule_recurring_action(time(), DAY_IN_SECONDS, 'notifier_clean_old_logs', $args); | |
| 152 | + } | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 116 | 157 | * Check if plugin's page |
| 117 | 158 | */ |
| 118 | 159 | public static function is_notifier_page() { |
| 119 | 160 | $current_screen = get_current_screen(); |
| 120 | - if ( strpos($current_screen->id, NOTIFIER_NAME) !== false) { | |
| 161 | + if ( strpos( $current_screen->id, NOTIFIER_NAME ) !== false | |
| 162 | + && strpos( $current_screen->id, 'order-notifications-for-woocommerce' ) === false | |
| 163 | + ) { | |
| 121 | 164 | return true; |
| 122 | 165 | } |
| 123 | 166 | |
| 124 | - $plugin_ctps = array( 'wa_message_template', 'wa_contact', 'wa_notification' ); | |
| 125 | - if ( '' !== $current_screen->post_type && in_array( $current_screen->post_type, $plugin_ctps ) ) { | |
| 126 | - return true; | |
| 127 | - } | |
| 128 | - | |
| 129 | 167 | return false; |
| 130 | 168 | } |
| 131 | 169 | |
| 132 | 170 | /** |
| @@ -132,97 +170,111 @@ | ||
| 132 | 170 | /** |
| 133 | 171 | * Add admin scripts and styles |
| 134 | 172 | */ |
| 135 | 173 | public function admin_scripts () { |
| 174 | + if ( ! Notifier_Migration::is_migration_complete() && 'dismissed' !== get_option( 'notifier_migration_notice_dismissed' ) ) { | |
| 175 | + wp_enqueue_script( | |
| 176 | + 'notifier-admin', | |
| 177 | + NOTIFIER_URL . 'assets/js/admin.js', | |
| 178 | + array( 'jquery' ), | |
| 179 | + NOTIFIER_VERSION, | |
| 180 | + true | |
| 181 | + ); | |
| 182 | + wp_localize_script( 'notifier-admin', 'notifier_migration', array( | |
| 183 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 184 | + 'nonce' => wp_create_nonce( 'notifier_dismiss_migration' ), | |
| 185 | + ) ); | |
| 186 | + } | |
| 187 | + | |
| 136 | 188 | if (!self::is_notifier_page()) { |
| 137 | 189 | return; |
| 138 | 190 | } |
| 139 | 191 | |
| 140 | - // Select2 | |
| 141 | - wp_enqueue_script( | |
| 142 | - NOTIFIER_NAME . '-select2-js', | |
| 143 | - NOTIFIER_URL . 'assets/js/select2.min.js', | |
| 144 | - array('jquery'), | |
| 145 | - NOTIFIER_VERSION, | |
| 146 | - true | |
| 147 | - ); | |
| 192 | + wp_enqueue_script( | |
| 193 | + NOTIFIER_NAME . '-admin-react', | |
| 194 | + NOTIFIER_URL . 'assets/js/admin-react.js', | |
| 195 | + array(), | |
| 196 | + NOTIFIER_VERSION, | |
| 197 | + true | |
| 198 | + ); | |
| 148 | 199 | |
| 149 | - // Date / time picker | |
| 150 | - wp_enqueue_script( 'jquery-ui-datepicker' ); | |
| 151 | - wp_enqueue_script( | |
| 152 | - NOTIFIER_NAME . '-timepicker-addon', | |
| 153 | - NOTIFIER_URL . 'assets/js/jquery-ui-timepicker-addon.min.js', | |
| 154 | - array( 'jquery-ui-datepicker' ), | |
| 155 | - NOTIFIER_VERSION, | |
| 156 | - true | |
| 157 | - ); | |
| 200 | + wp_enqueue_style( | |
| 201 | + NOTIFIER_NAME . '-admin-css', | |
| 202 | + NOTIFIER_URL . 'assets/css/admin.css', | |
| 203 | + null, | |
| 204 | + NOTIFIER_VERSION | |
| 205 | + ); | |
| 206 | + } | |
| 158 | 207 | |
| 159 | - // Admin JS file | |
| 160 | - wp_enqueue_script( | |
| 161 | - NOTIFIER_NAME . '-admin-js', | |
| 162 | - NOTIFIER_URL . 'assets/js/admin.js', | |
| 163 | - array('jquery'), | |
| 164 | - NOTIFIER_VERSION, | |
| 165 | - true | |
| 166 | - ); | |
| 167 | - wp_localize_script( | |
| 168 | - NOTIFIER_NAME . '-admin-js', | |
| 169 | - 'waNotifier', | |
| 170 | - apply_filters( 'notifier_js_variables', array('ajaxurl' => admin_url( 'admin-ajax.php' ) ) ) | |
| 171 | - ); | |
| 208 | + /** | |
| 209 | + * Add frontend scripts and styles | |
| 210 | + */ | |
| 211 | + public function frontend_scripts () { | |
| 212 | + if('yes' === get_option('notifier_ctc_enable')){ | |
| 213 | + // Styles | |
| 214 | + wp_enqueue_style( | |
| 215 | + NOTIFIER_NAME . '-frontend-css', | |
| 216 | + NOTIFIER_URL . 'assets/css/frontend.css', | |
| 217 | + null, | |
| 218 | + NOTIFIER_VERSION | |
| 219 | + ); | |
| 220 | + } | |
| 172 | 221 | |
| 173 | - // Media upload library | |
| 174 | - wp_enqueue_media(); | |
| 222 | + if ( ! self::is_woo_handled_by_standalone_plugin() && 'yes' === get_option('notifier_enable_abandonment_cart_tracking', 'no') && class_exists('WooCommerce') && is_checkout() ) { | |
| 223 | + wp_enqueue_script( | |
| 224 | + NOTIFIER_NAME . '-js', | |
| 225 | + NOTIFIER_URL . 'assets/js/script.js', | |
| 226 | + array('jquery'), | |
| 227 | + NOTIFIER_VERSION, | |
| 228 | + true | |
| 229 | + ); | |
| 175 | 230 | |
| 176 | - // Styles | |
| 177 | - wp_enqueue_style( | |
| 178 | - NOTIFIER_NAME . '-admin-css', | |
| 179 | - NOTIFIER_URL . 'assets/css/admin.css', | |
| 180 | - null, | |
| 181 | - NOTIFIER_VERSION | |
| 182 | - ); | |
| 231 | + wp_localize_script( | |
| 232 | + NOTIFIER_NAME . '-js', | |
| 233 | + 'notifierFrontObj', | |
| 234 | + array( | |
| 235 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 236 | + 'security' => wp_create_nonce('notifier_save_cart_abandonment_data'), | |
| 237 | + ) | |
| 238 | + ); | |
| 239 | + } | |
| 183 | 240 | } |
| 184 | 241 | |
| 185 | 242 | /** |
| 186 | 243 | * Set up a div for the header embed to render into. |
| 187 | - * The initial contents here are meant as a place loader for when the PHP page initialy loads. | |
| 244 | + * Only renders for legacy (non-SPA) pages like the trigger CPT editor. | |
| 245 | + * SPA pages render the header via the React app. | |
| 188 | 246 | */ |
| 189 | 247 | public static function embed_page_header() { |
| 190 | 248 | if (!self::is_notifier_page()) { |
| 191 | 249 | return; |
| 192 | 250 | } |
| 193 | - $current_screen = get_current_screen(); | |
| 194 | - $cpt = ( '' !== $current_screen->post_type) ? $current_screen->post_type : ''; | |
| 195 | - $tax = ( '' !== $current_screen->taxonomy) ? $current_screen->taxonomy : ''; | |
| 251 | + | |
| 252 | + $screen = get_current_screen(); | |
| 253 | + $spa_pages = array( | |
| 254 | + 'toplevel_page_notifier', | |
| 255 | + 'wanotifier_page_notifier-chat-button', | |
| 256 | + 'wanotifier_page_notifier-settings', | |
| 257 | + 'wanotifier_page_notifier-logs', | |
| 258 | + 'wanotifier_page_notifier-migration', | |
| 259 | + ); | |
| 260 | + | |
| 261 | + if ( in_array( $screen->id, $spa_pages, true ) ) { | |
| 262 | + return; | |
| 263 | + } | |
| 264 | + | |
| 265 | + $cpt = ( '' !== $screen->post_type ) ? $screen->post_type : ''; | |
| 196 | 266 | ?> |
| 197 | 267 | <div id="notifier-admin-header" data-post-type="<?php echo esc_attr($cpt); ?>"> |
| 198 | 268 | <div class="notifier-admin-header-content"> |
| 199 | - <div class="header-page-title w-30"> | |
| 200 | - <h2><?php echo esc_attr(get_admin_page_title()); ?></h2> | |
| 269 | + <div class="header-page-title w-30 d-flex align-content-center"> | |
| 270 | + <a class="header-logo" href="admin.php?page=notifier"><img src="<?php echo esc_url( NOTIFIER_URL . 'assets/images/favicon.svg' ); ?>"></a> | |
| 271 | + <h2>WANotifier</h2> | |
| 201 | 272 | </div> |
| 202 | - <div class="header-menu-items w-40 d-flex justify-content-center"> | |
| 203 | - <?php | |
| 204 | - if ( 'wa_contact' == $cpt ) { | |
| 205 | - ?> | |
| 206 | - <ul> | |
| 207 | - <li> | |
| 208 | - <a class="<?php echo ('wa_contact_list' !== $tax && 'wa_contact_tag' !== $tax) ? 'active' : ''; ?>" href="<?php echo esc_url( admin_url('edit.php?post_type=wa_contact') ); ?>" class="">Contacts</a> | |
| 209 | - </li> | |
| 210 | - <li> | |
| 211 | - <a class="<?php echo ('wa_contact_list' == $tax) ? 'active' : ''; ?>" href="<?php echo esc_url( admin_url('edit-tags.php?taxonomy=wa_contact_list&post_type=wa_contact') ); ?>">Lists</a> | |
| 212 | - </li> | |
| 213 | - <li> | |
| 214 | - <a class="<?php echo ('wa_contact_tag' == $tax) ? 'active' : ''; ?>" href="<?php echo esc_url( admin_url('edit-tags.php?taxonomy=wa_contact_tag&post_type=wa_contact') ); ?>">Tags</a> | |
| 215 | - </li> | |
| 216 | - </ul> | |
| 217 | - <?php | |
| 218 | - } | |
| 219 | - ?> | |
| 220 | - </div> | |
| 221 | 273 | <div class="header-action-links w-30 d-flex justify-content-end"> |
| 222 | - <span class="header-version">Version: <?php echo esc_html(NOTIFIER_VERSION); ?> (beta)</span> | |
| 223 | - <a href="mailto:ram@fantastech.co?subject=%5BWA%20Notifier%5D%20Help%20Needed%20on<?php echo esc_url(get_site_url()); ?>">Help</a> | |
| 224 | - <a href="admin.php?page=notifier&show=disclaimer">Disclaimer</a> | |
| 274 | + <span class="review-us-link">Review us: <a href="https://wordpress.org/support/plugin/notifier/reviews/#new-post" target="_blank">⭐⭐⭐⭐⭐</a></span> | |
| 275 | + <span class="header-version">Version: <?php echo esc_html(NOTIFIER_VERSION); ?></span> | |
| 276 | + <a href="https://wanotifier.com/support/" target="_blank">Help</a> | |
| 225 | 277 | </div> |
| 226 | 278 | </div> |
| 227 | 279 | </div> |
| 228 | 280 | <?php |
| @@ -228,81 +280,39 @@ | ||
| 228 | 280 | <?php |
| 229 | 281 | } |
| 230 | 282 | |
| 231 | 283 | /** |
| 232 | - * Handle response from Whatsapp | |
| 284 | + * For sending API requests | |
| 233 | 285 | */ |
| 234 | - public static function handle_webhook_requests () { | |
| 235 | - if ( ! isset($_GET['notifier']) ) { | |
| 236 | - return; | |
| 286 | + public static function send_api_request ( $endpoint, $args, $method = 'POST', $headers = array() ) { | |
| 287 | + $api_key = get_option('notifier_api_key'); | |
| 288 | + $api_key = trim($api_key); | |
| 289 | + if('' == $api_key) { | |
| 290 | + return false; | |
| 237 | 291 | } |
| 238 | 292 | |
| 239 | - if ( ! isset($_POST) ) { | |
| 240 | - return; | |
| 293 | + if(empty($headers)){ | |
| 294 | + $headers = array( | |
| 295 | + 'Content-Type' => 'application/json; charset=utf-8' | |
| 296 | + ); | |
| 241 | 297 | } |
| 242 | 298 | |
| 243 | - $hub_mode = isset($_GET['hub_mode']) ? sanitize_text_field( wp_unslash( $_GET['hub_mode'] ) ) : ''; | |
| 244 | - $hub_verify_token = isset($_GET['hub_verify_token']) ? sanitize_text_field( wp_unslash( $_GET['hub_verify_token'] ) ) : ''; | |
| 245 | - $hub_challenge = isset($_GET['hub_challenge']) ? sanitize_text_field( wp_unslash( $_GET['hub_challenge'] ) ) : ''; | |
| 246 | - | |
| 247 | - /* Validate WhastApp API webbook */ | |
| 248 | - if ( 'subscribe' === $hub_mode ) { | |
| 249 | - $verify_token = get_option(NOTIFIER_PREFIX . 'verify_token'); | |
| 250 | - if ($hub_verify_token == $verify_token) { | |
| 251 | - echo esc_html($hub_challenge); | |
| 252 | - } | |
| 253 | - } | |
| 254 | - | |
| 255 | - exit; | |
| 256 | - } | |
| 257 | - | |
| 258 | - /** | |
| 259 | - * For sending requests to Cloud API | |
| 260 | - */ | |
| 261 | - public static function wa_cloud_api_request ( $endpoint, $args = array(), $method = 'POST', $headers = array() ) { | |
| 262 | - $phone_number_id = get_option('notifier_phone_number_id'); | |
| 263 | - $request_url = NOTIFIER_WA_API_URL . $phone_number_id . '/' . untrailingslashit($endpoint); | |
| 264 | - return self::send_api_request($request_url, $args, $method, $headers); | |
| 265 | - } | |
| 266 | - | |
| 267 | - /** | |
| 268 | - * For sending requests to WA Business API | |
| 269 | - */ | |
| 270 | - public static function wa_business_api_request ( $endpoint, $args = array(), $method = 'POST', $headers = array() ) { | |
| 271 | - $business_account_id = get_option('notifier_business_account_id'); | |
| 272 | - $request_url = NOTIFIER_WA_API_URL . $business_account_id . '/' . untrailingslashit($endpoint); | |
| 273 | - return self::send_api_request($request_url, $args, $method, $headers); | |
| 274 | - } | |
| 275 | - | |
| 276 | - /** | |
| 277 | - * For sending graph api requests | |
| 278 | - */ | |
| 279 | - public static function wa_graph_api_request ( $endpoint, $args = array(), $method = 'POST', $headers = array() ) { | |
| 280 | - $request_url = NOTIFIER_WA_API_URL . untrailingslashit($endpoint); | |
| 281 | - return self::send_api_request($request_url, $args, $method, $headers); | |
| 282 | - } | |
| 283 | - | |
| 284 | - /** | |
| 285 | - * For sending API requests | |
| 286 | - */ | |
| 287 | - private static function send_api_request ( $request_url, $args, $method, $headers ) { | |
| 288 | - $permanent_access_token = get_option('notifier_permanent_access_token'); | |
| 289 | - $headers = wp_parse_args($headers, array( | |
| 290 | - 'Authorization' => 'Bearer ' . $permanent_access_token | |
| 291 | - )); | |
| 299 | + $request_url = NOTIFIER_APP_BASE_URL . '/' . NOTIFIER_APP_API_PATH . '/' . $endpoint . '?key=' . esc_attr($api_key); | |
| 292 | 300 | $request_args = array( |
| 293 | 301 | 'method' => $method, |
| 294 | 302 | 'headers' => $headers, |
| 295 | 303 | 'timeout' => 120, |
| 296 | - 'body' => $args | |
| 304 | + 'body' => json_encode($args), | |
| 305 | + 'sslverify' => true | |
| 297 | 306 | ); |
| 298 | - $response = wp_remote_request( $request_url, $request_args); | |
| 307 | + | |
| 308 | + $response = wp_remote_request( $request_url, $request_args ); | |
| 309 | + | |
| 310 | + $response_body = wp_remote_retrieve_body( $response ); | |
| 311 | + | |
| 299 | 312 | if ( is_wp_error( $response ) ) { |
| 300 | 313 | error_log( $response->get_error_message() ); |
| 301 | 314 | return false; |
| 302 | - } elseif (isset($response->error)) { | |
| 303 | - error_log($response->error_user_title . ' - ' . $response->eerror_user_msg); | |
| 304 | - return json_decode($response_body); | |
| 305 | 315 | } else { |
| 306 | 316 | $response_body = wp_remote_retrieve_body( $response ); |
| 307 | 317 | return json_decode($response_body); |
| 308 | 318 | } |
| @@ -308,95 +318,204 @@ | ||
| 308 | 318 | } |
| 309 | 319 | } |
| 310 | 320 | |
| 311 | 321 | /** |
| 312 | - * For uploading media to WhatsApp | |
| 322 | + * Whether the standalone WANotifier for WooCommerce plugin is active and | |
| 323 | + * has completed its connection/migration, meaning this plugin should defer | |
| 324 | + * all Woo/WCAR functionality to it. | |
| 325 | + * | |
| 326 | + * @return bool | |
| 313 | 327 | */ |
| 314 | - public static function wa_cloud_api_upload_media($attachment_id) { | |
| 315 | - $file_path = get_attached_file($attachment_id); | |
| 316 | - $file_size = filesize($file_path); | |
| 328 | + public static function is_woo_handled_by_standalone_plugin() { | |
| 329 | + static $result = null; | |
| 330 | + if ( null !== $result ) { | |
| 331 | + return $result; | |
| 332 | + } | |
| 317 | 333 | |
| 318 | - if (!$file_path) { | |
| 319 | - return; | |
| 334 | + if ( ! function_exists( 'is_plugin_active' ) ) { | |
| 335 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 320 | 336 | } |
| 321 | 337 | |
| 322 | - $file_args = array ( | |
| 323 | - 'file_length' => $file_size, | |
| 324 | - 'file_type' => get_post_mime_type($attachment_id) | |
| 325 | - ); | |
| 338 | + if ( ! is_plugin_active( 'order-notifications-for-woocommerce/order-notifications-for-woocommerce.php' ) ) { | |
| 339 | + $result = false; | |
| 340 | + return false; | |
| 341 | + } | |
| 326 | 342 | |
| 327 | - $permanent_access_token = get_option('notifier_permanent_access_token'); | |
| 343 | + if ( 'complete' === get_option( 'wanowc_migration_status' ) ) { | |
| 344 | + $result = true; | |
| 345 | + return true; | |
| 346 | + } | |
| 328 | 347 | |
| 329 | - // Create session ID for upload | |
| 330 | - $upload_session = self::wa_graph_api_request('app/uploads', $file_args); | |
| 331 | - if (!isset($upload_session->id)) { | |
| 332 | - error_log('Error creating WhatsApp image upload session: ' . $upload_session); | |
| 333 | - return false; | |
| 348 | + if ( get_option( 'wanowc_conn_woocommerce_auth_key' ) || get_option( 'wanowc_conn_wcar_auth_key' ) ) { | |
| 349 | + $result = true; | |
| 350 | + return true; | |
| 334 | 351 | } |
| 335 | 352 | |
| 336 | - // Upload the file | |
| 337 | - $file = @fopen( $file_path, 'r' ); | |
| 338 | - $file_data = fread( $file, $file_size ); | |
| 353 | + $result = false; | |
| 354 | + return false; | |
| 355 | + } | |
| 339 | 356 | |
| 340 | - $upload_headers = array( | |
| 341 | - 'Authorization' => 'OAuth ' . $permanent_access_token, | |
| 342 | - 'file_offset' => 0, | |
| 343 | - 'Connection' => 'Close', | |
| 344 | - 'Host' => 'graph.facebook.com', | |
| 345 | - 'Content-Type' => 'multipart/form-data' | |
| 346 | - ); | |
| 357 | + /** | |
| 358 | + * Load Woocommerce class if it's present is activated | |
| 359 | + */ | |
| 360 | + public static function maybe_include_integrations () { | |
| 361 | + $woo_handled = self::is_woo_handled_by_standalone_plugin(); | |
| 347 | 362 | |
| 348 | - $upload_handle = self::wa_graph_api_request( $upload_session->id, $file_data, null, $upload_headers ); | |
| 349 | - if (!isset($upload_handle->h)) { | |
| 350 | - error_log('Error while uploading profile image: ' . $upload_handle); | |
| 351 | - return false; | |
| 363 | + // Woocommerce — skip when the standalone plugin handles it | |
| 364 | + if ( ! $woo_handled && class_exists( 'WooCommerce' ) ) { | |
| 365 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-woocommerce.php'; | |
| 366 | + Notifier_Woocommerce::init(); | |
| 352 | 367 | } |
| 368 | + if ( ! $woo_handled && ! class_exists( 'WC_Session' ) && class_exists( 'WooCommerce' ) ) { | |
| 369 | + include_once( WP_PLUGIN_DIR . '/woocommerce/includes/abstracts/abstract-wc-session.php' ); | |
| 370 | + } | |
| 353 | 371 | |
| 354 | - return $upload_handle->h; | |
| 372 | + // WooCommerce Cart Abandonment Recovery — skip when the standalone plugin handles it | |
| 373 | + if ( ! $woo_handled && class_exists( 'CARTFLOWS_CA_Loader' ) && defined('CARTFLOWS_CA_VER') && version_compare(CARTFLOWS_CA_VER, '1.2.25', '>=')) { | |
| 374 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-wcar.php'; | |
| 375 | + Notifier_WCAR::init(); | |
| 376 | + } | |
| 377 | + | |
| 378 | + // Gravity Forms | |
| 379 | + if ( class_exists( 'GFCommon' ) ) { | |
| 380 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-gravityforms.php'; | |
| 381 | + Notifier_GravityForms::init(); | |
| 382 | + } | |
| 383 | + | |
| 384 | + // Contact Form 7 | |
| 385 | + if ( class_exists( 'WPCF7' ) ) { | |
| 386 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-cf7.php'; | |
| 387 | + Notifier_ContactForm7::init(); | |
| 388 | + } | |
| 389 | + | |
| 390 | + // WPForms | |
| 391 | + if ( class_exists( 'WPForms' ) ) { | |
| 392 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-wpforms.php'; | |
| 393 | + Notifier_WPForms::init(); | |
| 394 | + } | |
| 395 | + | |
| 396 | + // Ninja Forms | |
| 397 | + if ( class_exists( 'Ninja_Forms' ) ) { | |
| 398 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-ninjaforms.php'; | |
| 399 | + Notifier_NinjaForms::init(); | |
| 400 | + } | |
| 401 | + | |
| 402 | + //FrmForm | |
| 403 | + if ( class_exists( 'FrmForm' ) ) { | |
| 404 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-formidable.php'; | |
| 405 | + Notifier_Formidable::init(); | |
| 406 | + } | |
| 407 | + | |
| 408 | + //WPFluentForm | |
| 409 | + if ( function_exists( 'fluentFormApi' ) ) { | |
| 410 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-fluentforms.php'; | |
| 411 | + Notifier_FluentForms::init(); | |
| 412 | + } | |
| 413 | + | |
| 414 | + //Forminator | |
| 415 | + if ( class_exists( 'Forminator' ) ) { | |
| 416 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-forminator.php'; | |
| 417 | + Notifier_Forminator::init(); | |
| 418 | + } | |
| 419 | + | |
| 420 | + //SureForms | |
| 421 | + if ( class_exists( 'SRFM\Plugin_Loader' ) ) { | |
| 422 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-sureforms.php'; | |
| 423 | + Notifier_SureForms::init(); | |
| 424 | + } | |
| 425 | + | |
| 426 | + //WS Form | |
| 427 | + if ( class_exists( 'WS_Form' ) ) { | |
| 428 | + require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-wsform.php'; | |
| 429 | + Notifier_WSForm::init(); | |
| 430 | + } | |
| 355 | 431 | } |
| 356 | 432 | |
| 357 | 433 | /** |
| 358 | - * Remove query args from WP backend | |
| 434 | + * Is connection to WANotifier.com active? | |
| 359 | 435 | */ |
| 360 | - public static function remove_admin_query_args ($args) { | |
| 361 | - $remove_args = array('wa_import_count', 'wa_import_skipped', 'wa_contacts_import', 'import_contacts_from_users', 'refresh_status'); | |
| 362 | - return array_merge($args, $remove_args); | |
| 436 | + public static function is_api_active () { | |
| 437 | + $activated = get_option(NOTIFIER_PREFIX . 'api_activated'); | |
| 438 | + if('yes' == $activated) { | |
| 439 | + return true; | |
| 440 | + } | |
| 441 | + else{ | |
| 442 | + return false; | |
| 443 | + } | |
| 363 | 444 | } |
| 364 | 445 | |
| 365 | 446 | /** |
| 366 | - * Load Woocommerce class if it's present is activated | |
| 367 | - */ | |
| 368 | - public static function maybe_include_woocoomerce_class () { | |
| 369 | - if ( class_exists( 'WooCommerce' ) ) { | |
| 370 | - require_once NOTIFIER_PATH . 'includes/classes/class-notifier-woocommerce.php'; | |
| 371 | - Notifier_Woocommerce::init(); | |
| 372 | - } | |
| 447 | + * Create New db table-- wanotifier_activity_log | |
| 448 | + */ | |
| 449 | + public static function create_notifier_activity_log_table() { | |
| 450 | + global $wpdb; | |
| 451 | + $table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME; | |
| 452 | + | |
| 453 | + $charset_collate = $wpdb->get_charset_collate(); | |
| 454 | + | |
| 455 | + // Include IF NOT EXISTS clause in the CREATE TABLE query | |
| 456 | + $sql = "CREATE TABLE IF NOT EXISTS $table_name ( | |
| 457 | + log_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 458 | + timestamp timestamp NOT NULL, | |
| 459 | + message text NOT NULL, | |
| 460 | + type varchar(16) NOT NULL, | |
| 461 | + PRIMARY KEY (log_id), | |
| 462 | + INDEX idx_timestamp (timestamp) | |
| 463 | + ) $charset_collate;"; | |
| 464 | + | |
| 465 | + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
| 466 | + dbDelta( $sql ); | |
| 467 | + | |
| 468 | + $notifier_plugin_data = get_option('notifier_meta', array()); | |
| 469 | + $notifier_plugin_data['activity_log_table'] = 'yes'; | |
| 470 | + update_option('notifier_meta', $notifier_plugin_data); | |
| 373 | 471 | } |
| 374 | 472 | |
| 375 | 473 | /** |
| 376 | - * Add admin html templates to footer | |
| 377 | - */ | |
| 378 | - public static function add_admin_html_templates () { | |
| 379 | - if (!self::is_notifier_page()) { | |
| 474 | + * Delete old log from activity table according to interval | |
| 475 | + */ | |
| 476 | + public function notifier_delete_old_activity_logs() { | |
| 477 | + global $wpdb; | |
| 478 | + $table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME; | |
| 479 | + $retention_time = apply_filters( 'notifier_logs_retention_time', 7 ); | |
| 480 | + $retention_time = intval($retention_time); | |
| 481 | + $wpdb->query( $wpdb->prepare( "DELETE FROM `$table_name` WHERE timestamp <= DATE_SUB(NOW(), INTERVAL %d DAY)", $retention_time ) ); | |
| 482 | + } | |
| 483 | + | |
| 484 | + /** | |
| 485 | + * Create New db table-- notifier_cart_abandonment | |
| 486 | + */ | |
| 487 | + public static function create_cart_abandonment_table() { | |
| 488 | + if ( self::is_woo_handled_by_standalone_plugin() ) { | |
| 380 | 489 | return; |
| 381 | 490 | } |
| 382 | 491 | |
| 383 | - $templates = apply_filters('notifier_admin_html_templates', array()); | |
| 492 | + $notifier_plugin_data = get_option('notifier_meta', array()); | |
| 493 | + if ( ! isset( $notifier_plugin_data['cart_abandonment_table'] ) || $notifier_plugin_data['cart_abandonment_table'] !== 'yes' ) { | |
| 494 | + global $wpdb; | |
| 495 | + $table_name = $wpdb->prefix . NOTIFIER_CART_ABANDONMENT_TABLE; | |
| 496 | + $charset_collate = $wpdb->get_charset_collate(); | |
| 497 | + | |
| 498 | + // Updated CREATE TABLE query for notifier_cart_abandonment table | |
| 499 | + $sql = "CREATE TABLE IF NOT EXISTS $table_name ( | |
| 500 | + session_id BIGINT(20) AUTO_INCREMENT PRIMARY KEY, | |
| 501 | + session_key VARCHAR(16) NULL, | |
| 502 | + phone_number VARCHAR(15) NULL, | |
| 503 | + cart_data LONGTEXT NULL, | |
| 504 | + cart_total DECIMAL(10,2) NULL, | |
| 505 | + coupon_data LONGTEXT NULL, | |
| 506 | + customer_data LONGTEXT NULL, | |
| 507 | + order_id BIGINT(11) DEFAULT 0, | |
| 508 | + triggered TINYINT(1) DEFAULT 0, | |
| 509 | + created_time DATETIME NULL, | |
| 510 | + updated_time DATETIME NULL, | |
| 511 | + KEY session_key (session_key) -- Index for session_key | |
| 512 | + ) $charset_collate;"; | |
| 384 | 513 | |
| 385 | - if (count($templates) == 0) { | |
| 386 | - return; | |
| 387 | - } | |
| 514 | + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
| 515 | + dbDelta( $sql ); | |
| 388 | 516 | |
| 389 | - echo '<div class="notifier-templates">'; | |
| 390 | - foreach ($templates as $template) { | |
| 391 | - $file_path = NOTIFIER_PATH . '/views/templates/admin-' . $template . '.php'; | |
| 392 | - if ( ! file_exists($file_path) ) { | |
| 393 | - continue; | |
| 394 | - } | |
| 395 | - echo '<template id="notifier-template-' . esc_attr($template) . '">'; | |
| 396 | - require_once $file_path; | |
| 397 | - echo '</template>'; | |
| 517 | + $notifier_plugin_data['cart_abandonment_table'] = 'yes'; | |
| 518 | + update_option('notifier_meta', $notifier_plugin_data); | |
| 398 | 519 | } |
| 399 | - echo '</div>'; | |
| 400 | 520 | } |
| 401 | - | |
| 402 | 521 | } |