| 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.0.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 |
} |
| 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 |
// Libraries |
| 62 |
require_once NOTIFIER_PATH . 'libraries/action-scheduler/action-scheduler.php'; |
| 63 |
|
| 64 |
// Functions |
| 65 |
require_once NOTIFIER_PATH . 'includes/functions/functions-notifier-helpers.php'; |
| 66 |
require_once NOTIFIER_PATH . 'includes/functions/functions-notifier-meta-box-fields.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 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Hook into actions and filters. |
| 78 |
*/ |
| 79 |
private function init_hooks() { |
| 80 |
register_activation_hook ( NOTIFIER_FILE, array( $this, 'install') ); |
| 81 |
|
| 82 |
add_action( 'plugins_loaded', array( 'Notifier_Admin_Notices', 'init' ) ); |
| 83 |
add_action( 'plugins_loaded', array( 'Notifier_Dashboard', 'init' ) ); |
| 84 |
add_action( 'plugins_loaded', array( 'Notifier_Notification_Merge_Tags', 'init' ) ); |
| 85 |
add_action( 'plugins_loaded', array( 'Notifier_Notification_Triggers', 'init' ) ); |
| 86 |
add_action( 'plugins_loaded', array( 'Notifier_Settings', 'init' ) ); |
| 87 |
|
| 88 |
add_action( 'plugins_loaded', array( $this, 'maybe_include_integrations' ) ); |
| 89 |
|
| 90 |
add_action( 'admin_enqueue_scripts', array( $this , 'admin_scripts') ); |
| 91 |
add_action( 'in_admin_header', array( $this , 'embed_page_header' ) ); |
| 92 |
|
| 93 |
add_action( 'in_plugin_update_message-notifier/notifier.php', array( $this , 'plugin_upgrade_message' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Setup during plugin activation |
| 98 |
*/ |
| 99 |
public function install() { |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Check if plugin's page |
| 105 |
*/ |
| 106 |
public static function is_notifier_page() { |
| 107 |
$current_screen = get_current_screen(); |
| 108 |
if ( strpos($current_screen->id, NOTIFIER_NAME) !== false) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Add admin scripts and styles |
| 117 |
*/ |
| 118 |
public function admin_scripts () { |
| 119 |
if (!self::is_notifier_page()) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
// Select2 |
| 124 |
wp_enqueue_script( |
| 125 |
NOTIFIER_NAME . '-select2-js', |
| 126 |
NOTIFIER_URL . 'assets/js/select2.min.js', |
| 127 |
array('jquery'), |
| 128 |
NOTIFIER_VERSION, |
| 129 |
true |
| 130 |
); |
| 131 |
|
| 132 |
// Admin JS file |
| 133 |
wp_enqueue_script( |
| 134 |
NOTIFIER_NAME . '-admin-js', |
| 135 |
NOTIFIER_URL . 'assets/js/admin.js', |
| 136 |
array('jquery'), |
| 137 |
NOTIFIER_VERSION, |
| 138 |
true |
| 139 |
); |
| 140 |
wp_localize_script( |
| 141 |
NOTIFIER_NAME . '-admin-js', |
| 142 |
'notifierObj', |
| 143 |
apply_filters( 'notifier_js_variables', array('ajaxurl' => admin_url( 'admin-ajax.php' ) ) ) |
| 144 |
); |
| 145 |
|
| 146 |
// Styles |
| 147 |
wp_enqueue_style( |
| 148 |
NOTIFIER_NAME . '-admin-css', |
| 149 |
NOTIFIER_URL . 'assets/css/admin.css', |
| 150 |
null, |
| 151 |
NOTIFIER_VERSION |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Set up a div for the header embed to render into. |
| 157 |
* The initial contents here are meant as a place loader for when the PHP page initialy loads. |
| 158 |
*/ |
| 159 |
public static function embed_page_header() { |
| 160 |
if (!self::is_notifier_page()) { |
| 161 |
return; |
| 162 |
} |
| 163 |
$current_screen = get_current_screen(); |
| 164 |
$cpt = ( '' !== $current_screen->post_type) ? $current_screen->post_type : ''; |
| 165 |
$tax = ( '' !== $current_screen->taxonomy) ? $current_screen->taxonomy : ''; |
| 166 |
?> |
| 167 |
<div id="notifier-admin-header" data-post-type="<?php echo esc_attr($cpt); ?>"> |
| 168 |
<div class="notifier-admin-header-content"> |
| 169 |
<div class="header-page-title w-30"> |
| 170 |
<h2><?php echo esc_attr(get_admin_page_title()); ?></h2> |
| 171 |
</div> |
| 172 |
<div class="header-action-links w-30 d-flex justify-content-end"> |
| 173 |
<span class="header-version">Version: <?php echo esc_html(NOTIFIER_VERSION); ?></span> |
| 174 |
<a href="https://wanotifier.com/support/" target="_blank">Help</a> |
| 175 |
</div> |
| 176 |
</div> |
| 177 |
</div> |
| 178 |
<?php |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* For sending API requests |
| 183 |
*/ |
| 184 |
public static function send_api_request ( $endpoint, $args, $method = 'POST', $headers = array() ) { |
| 185 |
$api_key = get_option('notifier_api_key'); |
| 186 |
$api_key = trim($api_key); |
| 187 |
if('' == $api_key) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
if(empty($headers)){ |
| 192 |
$headers = array( |
| 193 |
'Content-Type' => 'application/json; charset=utf-8' |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
$request_url = NOTIFIER_APP_API_URL . $endpoint . '?key=' . $api_key; |
| 198 |
$request_args = array( |
| 199 |
'method' => $method, |
| 200 |
'headers' => $headers, |
| 201 |
'timeout' => 120, |
| 202 |
'body' => json_encode($args), |
| 203 |
'sslverify' => false |
| 204 |
); |
| 205 |
|
| 206 |
$response = wp_remote_request( $request_url, $request_args); |
| 207 |
|
| 208 |
$response_body = wp_remote_retrieve_body( $response ); |
| 209 |
|
| 210 |
if ( is_wp_error( $response ) ) { |
| 211 |
error_log( $response->get_error_message() ); |
| 212 |
return false; |
| 213 |
} else { |
| 214 |
$response_body = wp_remote_retrieve_body( $response ); |
| 215 |
return json_decode($response_body); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Load Woocommerce class if it's present is activated |
| 221 |
*/ |
| 222 |
public static function maybe_include_integrations () { |
| 223 |
// Woocommerce |
| 224 |
if ( class_exists( 'WooCommerce' ) ) { |
| 225 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-woocommerce.php'; |
| 226 |
Notifier_Woocommerce::init(); |
| 227 |
} |
| 228 |
if ( ! class_exists( 'WC_Session' ) ) { |
| 229 |
include_once( WP_PLUGIN_DIR . '/woocommerce/includes/abstracts/abstract-wc-session.php' ); |
| 230 |
} |
| 231 |
|
| 232 |
// Gravity Forms |
| 233 |
if ( class_exists( 'GFCommon' ) ) { |
| 234 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-gravityforms.php'; |
| 235 |
Notifier_GravityForms::init(); |
| 236 |
} |
| 237 |
|
| 238 |
// Contact Form 7 |
| 239 |
if ( class_exists( 'WPCF7' ) ) { |
| 240 |
require_once NOTIFIER_PATH . 'includes/classes/class-notifier-cf7.php'; |
| 241 |
Notifier_ContactForm7::init(); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Is connection to WANotifier.com active? |
| 247 |
*/ |
| 248 |
public static function is_api_active () { |
| 249 |
$activated = get_option(NOTIFIER_PREFIX . 'api_activated'); |
| 250 |
if('yes' == $activated) { |
| 251 |
return true; |
| 252 |
} |
| 253 |
else{ |
| 254 |
return false; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Plugin upgrade notice on Plugins page |
| 260 |
*/ |
| 261 |
public static function plugin_upgrade_message () { |
| 262 |
if(version_compare(NOTIFIER_VERSION,'1.0.5', '<=') ){ |
| 263 |
echo '<br><br><b>HEADS UP! Major update ahead.</b><br><br>v2.0.0 introduces major plugin updates. You\'ll need to <b>re-configure your triggers</b> after you upgrade to make sure the notifications are fired correctly.'; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
|