PluginProbe
WANotifier for Forms and Actions / 2.5.4
WANotifier for Forms and Actions v2.5.4
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.4, at includes/class-notifier.php

380 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.4' );
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 $as_clear_log = isset($notifier_plugin_data['as_clear_log']) ? $notifier_plugin_data['as_clear_log'] : 'no';
133 if ($as_clear_log !== 'yes') {
134 if (false === as_next_scheduled_action('notifier_clean_old_logs')) {
135 as_schedule_recurring_action(time(), DAY_IN_SECONDS, 'notifier_clean_old_logs', $args);
136 }
137 }
138 }
139
140 /**
141 * Check if plugin's page
142 */
143 public static function is_notifier_page() {
144 $current_screen = get_current_screen();
145 if ( strpos($current_screen->id, NOTIFIER_NAME) !== false) {
146 return true;
147 }
148
149 return false;
150 }
151
152 /**
153 * Add admin scripts and styles
154 */
155 public function admin_scripts () {
156 if (!self::is_notifier_page()) {
157 return;
158 }
159
160 // Select2
161 wp_enqueue_script(
162 NOTIFIER_NAME . '-select2-js',
163 NOTIFIER_URL . 'assets/js/select2.min.js',
164 array('jquery'),
165 NOTIFIER_VERSION,
166 true
167 );
168
169 // Admin JS file
170 wp_enqueue_script(
171 NOTIFIER_NAME . '-admin-js',
172 NOTIFIER_URL . 'assets/js/admin.js',
173 array('jquery'),
174 NOTIFIER_VERSION,
175 true
176 );
177 wp_localize_script(
178 NOTIFIER_NAME . '-admin-js',
179 'notifierObj',
180 apply_filters( 'notifier_js_variables', array('ajaxurl' => admin_url( 'admin-ajax.php' ) ) )
181 );
182
183 // Styles
184 wp_enqueue_style(
185 NOTIFIER_NAME . '-admin-css',
186 NOTIFIER_URL . 'assets/css/admin.css',
187 null,
188 NOTIFIER_VERSION
189 );
190 }
191
192 /**
193 * Add frontend scripts and styles
194 */
195 public function frontend_scripts () {
196 if('yes' === get_option('notifier_ctc_enable')){
197 // Styles
198 wp_enqueue_style(
199 NOTIFIER_NAME . '-frontend-css',
200 NOTIFIER_URL . 'assets/css/frontend.css',
201 null,
202 NOTIFIER_VERSION
203 );
204 }
205 }
206
207 /**
208 * Set up a div for the header embed to render into.
209 * The initial contents here are meant as a place loader for when the PHP page initialy loads.
210 */
211 public static function embed_page_header() {
212 if (!self::is_notifier_page()) {
213 return;
214 }
215 $current_screen = get_current_screen();
216 $cpt = ( '' !== $current_screen->post_type) ? $current_screen->post_type : '';
217 $tax = ( '' !== $current_screen->taxonomy) ? $current_screen->taxonomy : '';
218 ?>
219 <div id="notifier-admin-header" data-post-type="<?php echo esc_attr($cpt); ?>">
220 <div class="notifier-admin-header-content">
221 <div class="header-page-title w-30 d-flex align-content-center">
222 <a class="header-logo" href="admin.php?page=notifier"><img src="<?php echo NOTIFIER_URL; ?>assets/images/favicon.svg"></a>
223 <h2><?php echo esc_attr(get_admin_page_title()); ?></h2>
224 </div>
225 <div class="header-action-links w-30 d-flex justify-content-end">
226 <span class="review-us-link">Review us: <a href="https://wordpress.org/support/plugin/notifier/reviews/#new-post" target="_blank">⭐⭐⭐⭐⭐</a></span>
227 <span class="header-version">Version: <?php echo esc_html(NOTIFIER_VERSION); ?></span>
228 <a href="https://wanotifier.com/support/" target="_blank">Help</a>
229 </div>
230 </div>
231 </div>
232 <?php
233 }
234
235 /**
236 * For sending API requests
237 */
238 public static function send_api_request ( $endpoint, $args, $method = 'POST', $headers = array() ) {
239 $api_key = get_option('notifier_api_key');
240 $api_key = trim($api_key);
241 if('' == $api_key) {
242 return false;
243 }
244
245 if(empty($headers)){
246 $headers = array(
247 'Content-Type' => 'application/json; charset=utf-8'
248 );
249 }
250
251 $request_url = NOTIFIER_APP_API_URL . $endpoint . '?key=' . $api_key;
252 $request_args = array(
253 'method' => $method,
254 'headers' => $headers,
255 'timeout' => 120,
256 'body' => json_encode($args),
257 'sslverify' => false
258 );
259
260 $response = wp_remote_request( $request_url, $request_args );
261
262 $response_body = wp_remote_retrieve_body( $response );
263
264 if ( is_wp_error( $response ) ) {
265 error_log( $response->get_error_message() );
266 return false;
267 } else {
268 $response_body = wp_remote_retrieve_body( $response );
269 return json_decode($response_body);
270 }
271 }
272
273 /**
274 * Load Woocommerce class if it's present is activated
275 */
276 public static function maybe_include_integrations () {
277 // Woocommerce
278 if ( class_exists( 'WooCommerce' ) ) {
279 require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-woocommerce.php';
280 Notifier_Woocommerce::init();
281 }
282 if ( ! class_exists( 'WC_Session' ) ) {
283 include_once( WP_PLUGIN_DIR . '/woocommerce/includes/abstracts/abstract-wc-session.php' );
284 }
285
286 // WooCommerce Cart Abandonment Recovery by CartFlows
287 if ( class_exists( 'CARTFLOWS_CA_Loader' ) && defined('CARTFLOWS_CA_VER') && version_compare(CARTFLOWS_CA_VER, '1.2.25', '>=')) {
288 require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-wcar.php';
289 Notifier_WCAR::init();
290 }
291
292 // Gravity Forms
293 if ( class_exists( 'GFCommon' ) ) {
294 require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-gravityforms.php';
295 Notifier_GravityForms::init();
296 }
297
298 // Contact Form 7
299 if ( class_exists( 'WPCF7' ) ) {
300 require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-cf7.php';
301 Notifier_ContactForm7::init();
302 }
303
304 // WPForms
305 if ( class_exists( 'WPForms' ) ) {
306 require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-wpforms.php';
307 Notifier_WPForms::init();
308 }
309
310 // Ninja Forms
311 if ( class_exists( 'Ninja_Forms' ) ) {
312 require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-ninjaforms.php';
313 Notifier_NinjaForms::init();
314 }
315
316 //FrmForm
317 if ( class_exists( 'FrmForm' ) ) {
318 require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-formidable.php';
319 Notifier_Formidable::init();
320 }
321
322 //WPFluentForm
323 if ( function_exists( 'fluentFormApi' ) ) {
324 require_once NOTIFIER_PATH . 'includes/classes/integrations/class-notifier-fluentforms.php';
325 Notifier_FluentForms::init();
326 }
327 }
328
329 /**
330 * Is connection to WANotifier.com active?
331 */
332 public static function is_api_active () {
333 $activated = get_option(NOTIFIER_PREFIX . 'api_activated');
334 if('yes' == $activated) {
335 return true;
336 }
337 else{
338 return false;
339 }
340 }
341
342 /**
343 * Create New db table-- wanotifier_activity_log
344 */
345 public static function create_notifier_activity_log_table() {
346 global $wpdb;
347 $table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME;
348
349 $charset_collate = $wpdb->get_charset_collate();
350
351 // Include IF NOT EXISTS clause in the CREATE TABLE query
352 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
353 log_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
354 timestamp timestamp NOT NULL,
355 message text NOT NULL,
356 type varchar(16) NOT NULL,
357 PRIMARY KEY (log_id),
358 INDEX idx_timestamp (timestamp)
359 ) $charset_collate;";
360
361 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
362 dbDelta( $sql );
363
364 $notifier_plugin_data = get_option('notifier_meta', array());
365 $notifier_plugin_data['activity_log_table'] = 'yes';
366 update_option('notifier_meta', $notifier_plugin_data);
367 }
368
369 /**
370 * Delete old log from activity table according to interval
371 */
372 public function notifier_delete_old_activity_logs() {
373 global $wpdb;
374 $table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME;
375 $retention_time = apply_filters( 'notifier_logs_retention_time', 7 );
376 $retention_time = intval($retention_time);
377 $wpdb->query( $wpdb->prepare( "DELETE FROM `$table_name` WHERE timestamp <= DATE_SUB(NOW(), INTERVAL %d DAY)", $retention_time ) );
378 }
379 }
380