PluginProbe
WANotifier for Forms and Actions / 2.5.3
WANotifier for Forms and Actions v2.5.3
3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 1.0.3 All 66 releases
notifier / includes / class-notifier.php

class-notifier.php in WANotifier for Forms and Actions 2.5.3, at includes/class-notifier.php

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