| 1 |
<?php |
| 2 |
|
| 3 |
class SGPMBase |
| 4 |
{ |
| 5 |
/** |
| 6 |
* Holds the class object. |
| 7 |
* |
| 8 |
* @since 1.0.0 |
| 9 |
* |
| 10 |
* @var object |
| 11 |
*/ |
| 12 |
public static $instance; |
| 13 |
|
| 14 |
/** |
| 15 |
* Plugin version, used for cache-busting of style and script file references. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
public $version = SGPM_VERSION; |
| 22 |
|
| 23 |
/** |
| 24 |
* The name of the plugin. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public $pluginName = 'Popup Maker WP'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Unique plugin slug identifier. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public $pluginSlug = 'sgpmpopupmaker'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Notification engine object. |
| 43 |
* |
| 44 |
* @since 1.2.3.0 |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public $notificationEngine = null; |
| 49 |
|
| 50 |
/** |
| 51 |
* Helper utilities. |
| 52 |
* |
| 53 |
* @var SGPMHelper|null |
| 54 |
*/ |
| 55 |
protected $helper = null; |
| 56 |
|
| 57 |
/** |
| 58 |
* Menu handler instance. |
| 59 |
* |
| 60 |
* @var SGPMMenu|null |
| 61 |
*/ |
| 62 |
protected $menu = null; |
| 63 |
|
| 64 |
/** |
| 65 |
* API handler instance. |
| 66 |
* |
| 67 |
* @var SGPMApi|null |
| 68 |
*/ |
| 69 |
protected $api = null; |
| 70 |
|
| 71 |
/** |
| 72 |
* Front-end output handler. |
| 73 |
* |
| 74 |
* @var SGPMOutput|null |
| 75 |
*/ |
| 76 |
protected $output = null; |
| 77 |
|
| 78 |
/** |
| 79 |
* Loads the plugin into WordPress. |
| 80 |
* |
| 81 |
* @since 1.0.0 |
| 82 |
*/ |
| 83 |
public function init() |
| 84 |
{ |
| 85 |
// Autoload the class files. |
| 86 |
spl_autoload_register('SGPMBase::autoload'); |
| 87 |
register_activation_hook(SGPM_PATH.'popup-maker-api.php', array('SGPMBase', 'activate')); |
| 88 |
register_uninstall_hook(SGPM_PATH.'popup-maker-api.php', array('SGPMBase', 'uninstall')); |
| 89 |
register_deactivation_hook(SGPM_PATH.'popup-maker-api.php', array('SGPMBase', 'deactivate')); |
| 90 |
// update data of old user |
| 91 |
add_action('plugins_loaded', array($this, 'overallInit')); |
| 92 |
/** |
| 93 |
* remove comments for testing |
| 94 |
* |
| 95 |
* add_action('plugins_loaded', array($this, 'fetchNewNotifications')); |
| 96 |
*/ |
| 97 |
|
| 98 |
$this->helper = new SGPMHelper(); |
| 99 |
$this->menu = new SGPMMenu(); |
| 100 |
$this->api = new SGPMApi(); |
| 101 |
$this->output = new SGPMOutput(); |
| 102 |
add_action('init', array($this, 'registerDataConfig'), 99999); |
| 103 |
add_action('admin_init', array($this, 'registerNotificationEngine'), 99999); |
| 104 |
|
| 105 |
add_action('admin_notices', array($this, 'showNotificationsShade')); |
| 106 |
add_action('admin_enqueue_scripts', array($this, 'adminStyles')); |
| 107 |
|
| 108 |
// ajax call endpoint for all "Clear all Notiifcations" Button |
| 109 |
add_action('wp_ajax_sgpm_clear_all_notifications', array($this, 'clearAllNotifications')); |
| 110 |
add_action('wp_ajax_sgpm_remove_notification', array($this, 'removeNotification')); |
| 111 |
|
| 112 |
/* temprory fix : Delete old crons */ |
| 113 |
wp_clear_scheduled_hook("sgpm_fetch_new_notifications"); |
| 114 |
wp_unschedule_event(time(), 'sgpm_check_for_new_notifications_every_6_hours'); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
public function adminStyles() |
| 119 |
{ |
| 120 |
wp_register_style($this->pluginSlug.'-admin', SGPM_ASSETS_URL.'css/admin.css', array(), $this->version); |
| 121 |
wp_enqueue_style($this->pluginSlug.'-admin'); |
| 122 |
wp_register_style($this->pluginSlug.'-notification-shade', SGPM_ASSETS_URL.'css/notification-shade.css', array(), $this->version); |
| 123 |
wp_enqueue_style($this->pluginSlug.'-notification-shade'); |
| 124 |
} |
| 125 |
|
| 126 |
public function showNotificationsShade() |
| 127 |
{ |
| 128 |
$isPluginScreen = false; |
| 129 |
|
| 130 |
if (function_exists('get_current_screen')) { |
| 131 |
$screen = get_current_screen(); |
| 132 |
$isPluginScreen = strpos($screen->id, 'popup-maker-api-settings'); |
| 133 |
} |
| 134 |
|
| 135 |
$this->notificationEngine->setNotificationBadgeData(); |
| 136 |
|
| 137 |
if ($isPluginScreen) { |
| 138 |
$this->notificationEngine->create(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function clearAllNotifications() |
| 143 |
{ |
| 144 |
$this->notificationEngine->clearAllNotifications(); |
| 145 |
} |
| 146 |
|
| 147 |
public function removeNotification() |
| 148 |
{ |
| 149 |
// Verify nonce |
| 150 |
if ( ! isset($_POST['notificationType']) || ! isset($_POST['hash']) || ! isset($_POST['notificationId']) || ! wp_verify_nonce($_POST['notificationType'], 'notificationType_action') ) { |
| 151 |
wp_send_json_error('Nonce verification failed'); // Send an error response and exit |
| 152 |
} |
| 153 |
|
| 154 |
$notificationType = sanitize_text_field($_POST['notificationType']); |
| 155 |
|
| 156 |
if ($notificationType == 'review') { |
| 157 |
add_option('sgpm_popup_maker_dismiss_review_notice', 'true'); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
$hash = sanitize_key($_POST['hash']); |
| 162 |
$notificationId = sanitize_text_field($_POST['notificationId']); |
| 163 |
$this->notificationEngine->removeNotification($hash, $notificationId); |
| 164 |
} |
| 165 |
|
| 166 |
public function registerDataConfig() |
| 167 |
{ |
| 168 |
if (file_exists(SGPM_CLASSES.'SGPMDataConfig.php')) { |
| 169 |
require_once(SGPM_CLASSES.'SGPMDataConfig.php'); |
| 170 |
SGPMDataConfig::init(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
public function registerNotificationEngine() |
| 175 |
{ |
| 176 |
if (file_exists(SGPM_CLASSES.'SGPMNotificationEngine.php')) { |
| 177 |
require_once(SGPM_CLASSES.'SGPMNotificationEngine.php'); |
| 178 |
$this->notificationEngine = SGPMNotificationEngine::getInstance(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public static function validateNotificationsBody($body) |
| 183 |
{ |
| 184 |
if (isset($body[0])) { |
| 185 |
$notifications = $body[0]; |
| 186 |
|
| 187 |
return isset($notifications['title']); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
public function overallInit() |
| 192 |
{ |
| 193 |
$options = get_option('sgpm_popup_maker_api_option'); |
| 194 |
if (empty($options)) { |
| 195 |
$options = array(); |
| 196 |
} |
| 197 |
if (isset($options['pluginVersion']) && $options['pluginVersion'] >= '1.13') return; |
| 198 |
|
| 199 |
$options['pluginVersion'] = SGPM_VERSION; |
| 200 |
if (!isset($options['popups'])) return; |
| 201 |
|
| 202 |
foreach ($options['popups'] as $popupId => $popup) { |
| 203 |
if (!isset($options['popupsSettings'][$popupId])) continue; |
| 204 |
$popupSettings = $options['popupsSettings'][$popupId]; |
| 205 |
|
| 206 |
if (!isset($popupSettings['displayTarget'])) { |
| 207 |
$popupSettings['displayTarget'] = $this->getUpdatedSettingsForOldUser($popupSettings); |
| 208 |
$options['popupsSettings'][$popupId] = $popupSettings; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
update_option('sgpm_popup_maker_api_option', $options); |
| 213 |
} |
| 214 |
|
| 215 |
public function getUpdatedSettingsForOldUser($popupSettings) |
| 216 |
{ |
| 217 |
$updatedSettings = array(); |
| 218 |
|
| 219 |
if (isset($popupSettings['showOnAllPosts']) && $popupSettings['showOnAllPosts'] == 'on') { |
| 220 |
$updatedSettings[] = array( |
| 221 |
'param' => 'post_all', |
| 222 |
'operator' => '==' |
| 223 |
); |
| 224 |
} |
| 225 |
if (isset($popupSettings['showOnSomePosts']) && $popupSettings['showOnSomePosts'] == 'on') { |
| 226 |
$updatedSettings[] = array( |
| 227 |
'param' => 'post_selected', |
| 228 |
'operator' => '==', |
| 229 |
'value' => $this->getSelectedPostAssocArray($popupSettings['selectedPosts']) |
| 230 |
); |
| 231 |
} |
| 232 |
if (isset($popupSettings['showOnAllPages']) && $popupSettings['showOnAllPages'] == 'on') { |
| 233 |
$updatedSettings[] = array( |
| 234 |
'param' => 'page_all', |
| 235 |
'operator' => '==' |
| 236 |
); |
| 237 |
$updatedSettings[] = array( |
| 238 |
'param' => 'page_type', |
| 239 |
'operator' => '==', |
| 240 |
'value' => array('is_home_page') |
| 241 |
); |
| 242 |
} |
| 243 |
if (isset($popupSettings['showOnSomePages']) && $popupSettings['showOnSomePages'] == 'on') { |
| 244 |
$updatedSettings[] = array( |
| 245 |
'param' => 'page_selected', |
| 246 |
'operator' => '==', |
| 247 |
'value' => $this->getSelectedPostAssocArray($popupSettings['selectedPages']) |
| 248 |
); |
| 249 |
|
| 250 |
if (in_array('-1', $popupSettings['selectedPages'])) { |
| 251 |
$updatedSettings[] = array( |
| 252 |
'param' => 'page_type', |
| 253 |
'operator' => '==', |
| 254 |
'value' => array('is_home_page') |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
return $updatedSettings; |
| 260 |
} |
| 261 |
|
| 262 |
public function getSelectedPostAssocArray($selectedPost) |
| 263 |
{ |
| 264 |
$newSelectedPost = array(); |
| 265 |
foreach ($selectedPost as $key => $selectedPostId) { |
| 266 |
if ($selectedPostId == '-1') continue; |
| 267 |
$newSelectedPost[$selectedPostId] = get_the_title($selectedPostId); |
| 268 |
} |
| 269 |
return $newSelectedPost; |
| 270 |
} |
| 271 |
|
| 272 |
public static function autoload($classname) |
| 273 |
{ |
| 274 |
// Return early if not the proper classname. |
| 275 |
if ('SGPM' !== substr($classname, 0, 4)) { |
| 276 |
return; |
| 277 |
} |
| 278 |
// Check if the file exists. If so, load the file. |
| 279 |
$filename = SGPM_CLASSES.$classname.'.php'; |
| 280 |
if (file_exists($filename)) { |
| 281 |
require_once($filename); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
public static function activate() |
| 286 |
{ |
| 287 |
$activationDate = get_option('sgpm_popup_maker_activation_date'); |
| 288 |
if (!$activationDate) { |
| 289 |
add_option('sgpm_popup_maker_activation_date', strtotime("now")); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
public static function uninstall() |
| 294 |
{ |
| 295 |
delete_option('sgpm_popup_maker_api_option'); |
| 296 |
delete_option('sgpm_popup_maker_activation_date'); |
| 297 |
delete_option('sgpm_popup_maker_dismiss_review_notice'); |
| 298 |
delete_option('sgpm_popup_maker_notification_engine_source'); |
| 299 |
delete_option('sgpm_popup_maker_dismissed_notifacions'); |
| 300 |
delete_option('sgpm_popup_maker_user_roles'); |
| 301 |
delete_option('sgpm_popup_maker_custom_allowed_tags'); |
| 302 |
delete_option('sgpm_popup_maker_custom_allowed_attrs'); |
| 303 |
|
| 304 |
/* temprory fix : Delete old crons */ |
| 305 |
wp_clear_scheduled_hook("sgpm_fetch_new_notifications"); |
| 306 |
wp_clear_scheduled_hook("sgpm_fetch_notifications"); |
| 307 |
wp_unschedule_event(time(), 'sgpm_check_for_new_notifications_every_6_hours'); |
| 308 |
} |
| 309 |
|
| 310 |
public static function deactivate() |
| 311 |
{ |
| 312 |
/* temprory fix : Delete old crons */ |
| 313 |
wp_clear_scheduled_hook("sgpm_fetch_new_notifications"); |
| 314 |
wp_clear_scheduled_hook("sgpm_fetch_notifications"); |
| 315 |
wp_unschedule_event(time(), 'sgpm_check_for_new_notifications_every_6_hours'); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Returns the singleton instance of the class. |
| 320 |
* |
| 321 |
* @since 1.0.0 |
| 322 |
* |
| 323 |
* @return SGPMBase |
| 324 |
*/ |
| 325 |
/** |
| 326 |
* Start application statistics tracking |
| 327 |
* |
| 328 |
* Use the WISDOM Tracking plugin to track |
| 329 |
* application usage. |
| 330 |
* https://wisdomplugin.com/support/#getting-started |
| 331 |
* |
| 332 |
* Filter: plugins_loaded |
| 333 |
* |
| 334 |
* @since 1.4.4 |
| 335 |
* @return void |
| 336 |
*/ |
| 337 |
public function popup_maker_start_plugin_tracking() { |
| 338 |
if (class_exists('Plugin_Usage_Tracker')) { |
| 339 |
$wisdom = new Plugin_Usage_Tracker( |
| 340 |
SGPM_PATH.'popup-maker-api.php', |
| 341 |
'https://tracking.getawesomesupport.com', |
| 342 |
array(), |
| 343 |
true, |
| 344 |
true, |
| 345 |
1 |
| 346 |
); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
public static function getInstance() |
| 351 |
{ |
| 352 |
if (!isset( self::$instance ) && !(self::$instance instanceof SGPMBase)) { |
| 353 |
self::$instance = new SGPMBase(); |
| 354 |
} |
| 355 |
|
| 356 |
return self::$instance; |
| 357 |
} |
| 358 |
} |
| 359 |
|