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

297 lines 8.2 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.2.0' );
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 }
32
33 /**
34 * Define constant if not already set.
35 *
36 * @param string $name Constant name.
37 * @param string|bool $value Constant value.
38 */
39 private function define( $name, $value ) {
40 if ( ! defined( $name ) ) {
41 define( $name, $value );
42 }
43 }
44
45 /**
46 * Main Notifier Instance.
47 *
48 * @return Notifier instance.
49 */
50 public static function get_instance() {
51 if ( is_null( self::$_instance ) ) {
52 self::$_instance = new self();
53 }
54 return self::$_instance;
55 }
56
57 /**
58 * Include required core files used in admin and on the frontend.
59 */
60 private function includes() {
61 // Functions
62 require_once NOTIFIER_PATH . 'includes/functions/functions-notifier-helpers.php';
63 require_once NOTIFIER_PATH . 'includes/functions/functions-notifier-meta-box-fields.php';
64 require_once NOTIFIER_PATH . 'libraries/action-scheduler/action-scheduler.php';
65
66 // Classes
67 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-admin-notices.php';
68 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-dashboard.php';
69 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-notification-merge-tags.php';
70 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-notification-triggers.php';
71 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-settings.php';
72 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-frontend.php';
73 }
74
75 /**
76 * Hook into actions and filters.
77 */
78 private function init_hooks() {
79 register_activation_hook ( NOTIFIER_FILE, array( $this, 'install') );
80
81 add_action( 'after_setup_theme', array( 'Notifier_Admin_Notices', 'init' ) );
82 add_action( 'after_setup_theme', array( 'Notifier_Dashboard', 'init' ) );
83 add_action( 'after_setup_theme', array( 'Notifier_Notification_Merge_Tags', 'init' ) );
84 add_action( 'after_setup_theme', array( 'Notifier_Notification_Triggers', 'init' ) );
85 add_action( 'after_setup_theme', array( 'Notifier_Settings', 'init' ) );
86 add_action( 'after_setup_theme', array( 'Notifier_Frontend', 'init' ) );
87 add_action( 'admin_enqueue_scripts', array( $this , 'admin_scripts') );
88 add_action( 'wp_enqueue_scripts', array( $this , 'frontend_scripts') );
89 add_action( 'in_admin_header', array( $this , 'embed_page_header' ) );
90
91 add_action( 'after_setup_theme', array( $this, 'maybe_include_integrations' ) );
92 }
93
94 /**
95 * Setup during plugin activation
96 */
97 public function install() {
98
99 }
100
101 /**
102 * Check if plugin's page
103 */
104 public static function is_notifier_page() {
105 $current_screen = get_current_screen();
106 if ( strpos($current_screen->id, NOTIFIER_NAME) !== false) {
107 return true;
108 }
109
110 return false;
111 }
112
113 /**
114 * Add admin scripts and styles
115 */
116 public function admin_scripts () {
117 if (!self::is_notifier_page()) {
118 return;
119 }
120
121 // Select2
122 wp_enqueue_script(
123 NOTIFIER_NAME . '-select2-js',
124 NOTIFIER_URL . 'assets/js/select2.min.js',
125 array('jquery'),
126 NOTIFIER_VERSION,
127 true
128 );
129
130 // Admin JS file
131 wp_enqueue_script(
132 NOTIFIER_NAME . '-admin-js',
133 NOTIFIER_URL . 'assets/js/admin.js',
134 array('jquery'),
135 NOTIFIER_VERSION,
136 true
137 );
138 wp_localize_script(
139 NOTIFIER_NAME . '-admin-js',
140 'notifierObj',
141 apply_filters( 'notifier_js_variables', array('ajaxurl' => admin_url( 'admin-ajax.php' ) ) )
142 );
143
144 // Styles
145 wp_enqueue_style(
146 NOTIFIER_NAME . '-admin-css',
147 NOTIFIER_URL . 'assets/css/admin.css',
148 null,
149 NOTIFIER_VERSION
150 );
151 }
152
153 /**
154 * Add frontend scripts and styles
155 */
156 public function frontend_scripts () {
157 if('yes' === get_option('notifier_ctc_enable')){
158 // Styles
159 wp_enqueue_style(
160 NOTIFIER_NAME . '-frontend-css',
161 NOTIFIER_URL . 'assets/css/frontend.css',
162 null,
163 NOTIFIER_VERSION
164 );
165 }
166 }
167
168 /**
169 * Set up a div for the header embed to render into.
170 * The initial contents here are meant as a place loader for when the PHP page initialy loads.
171 */
172 public static function embed_page_header() {
173 if (!self::is_notifier_page()) {
174 return;
175 }
176 $current_screen = get_current_screen();
177 $cpt = ( '' !== $current_screen->post_type) ? $current_screen->post_type : '';
178 $tax = ( '' !== $current_screen->taxonomy) ? $current_screen->taxonomy : '';
179 ?>
180 <div id="notifier-admin-header" data-post-type="<?php echo esc_attr($cpt); ?>">
181 <div class="notifier-admin-header-content">
182 <div class="header-page-title w-30 d-flex align-content-center">
183 <a class="header-logo" href="admin.php?page=notifier"><img src="<?php echo NOTIFIER_URL; ?>assets/images/favicon.svg"></a>
184 <h2><?php echo esc_attr(get_admin_page_title()); ?></h2>
185 </div>
186 <div class="header-action-links w-30 d-flex justify-content-end">
187 <span class="header-version">Version: <?php echo esc_html(NOTIFIER_VERSION); ?></span>
188 <a href="https://wanotifier.com/support/" target="_blank">Help</a>
189 </div>
190 </div>
191 </div>
192 <?php
193 }
194
195 /**
196 * For sending API requests
197 */
198 public static function send_api_request ( $endpoint, $args, $method = 'POST', $headers = array() ) {
199 $api_key = get_option('notifier_api_key');
200 $api_key = trim($api_key);
201 if('' == $api_key) {
202 return false;
203 }
204
205 if(empty($headers)){
206 $headers = array(
207 'Content-Type' => 'application/json; charset=utf-8'
208 );
209 }
210
211 $request_url = NOTIFIER_APP_API_URL . $endpoint . '?key=' . $api_key;
212 $request_args = array(
213 'method' => $method,
214 'headers' => $headers,
215 'timeout' => 120,
216 'body' => json_encode($args),
217 'sslverify' => false
218 );
219
220 $response = wp_remote_request( $request_url, $request_args);
221
222 $response_body = wp_remote_retrieve_body( $response );
223
224 if ( is_wp_error( $response ) ) {
225 error_log( $response->get_error_message() );
226 return false;
227 } else {
228 $response_body = wp_remote_retrieve_body( $response );
229 return json_decode($response_body);
230 }
231 }
232
233 /**
234 * Load Woocommerce class if it's present is activated
235 */
236 public static function maybe_include_integrations () {
237 // Woocommerce
238 if ( class_exists( 'WooCommerce' ) ) {
239 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-woocommerce.php';
240 Notifier_Woocommerce::init();
241 }
242 if ( ! class_exists( 'WC_Session' ) ) {
243 include_once( WP_PLUGIN_DIR . '/woocommerce/includes/abstracts/abstract-wc-session.php' );
244 }
245
246 // Gravity Forms
247 if ( class_exists( 'GFCommon' ) ) {
248 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-gravityforms.php';
249 Notifier_GravityForms::init();
250 }
251
252 // Contact Form 7
253 if ( class_exists( 'WPCF7' ) ) {
254 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-cf7.php';
255 Notifier_ContactForm7::init();
256 }
257
258 // WPForms
259 if ( class_exists( 'WPForms' ) ) {
260 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-wpforms.php';
261 Notifier_WPForms::init();
262 }
263
264 // Ninja Forms
265 if ( class_exists( 'Ninja_Forms' ) ) {
266 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-ninjaforms.php';
267 Notifier_NinjaForms::init();
268 }
269
270 //FrmForm
271 if ( class_exists( 'FrmForm' ) ) {
272 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-formidable.php';
273 Notifier_Formidable::init();
274 }
275
276 //WPFluentForm
277 if ( function_exists( 'fluentFormApi' ) ) {
278 require_once NOTIFIER_PATH . 'includes/classes/class-notifier-fluentforms.php';
279 Notifier_FluentForms::init();
280 }
281 }
282
283 /**
284 * Is connection to WANotifier.com active?
285 */
286 public static function is_api_active () {
287 $activated = get_option(NOTIFIER_PREFIX . 'api_activated');
288 if('yes' == $activated) {
289 return true;
290 }
291 else{
292 return false;
293 }
294 }
295
296 }
297