PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.14
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.14
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / NotificationX.php

NotificationX.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.14, at includes/NotificationX.php

391 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * NotificationX File
5 *
6 * @package NotificationX
7 */
8
9 namespace NotificationX;
10
11 use NotificationX\Admin\Admin;
12 use NotificationX\Admin\Cron;
13 use NotificationX\Admin\EntriesMailReceiver;
14 use NotificationX\Admin\Settings;
15 use NotificationX\Blocks\Blocks;
16 use NotificationX\Core\Database;
17 use NotificationX\Core\PostType;
18 use NotificationX\Core\QuickBuild;
19 use NotificationX\Core\REST;
20 use NotificationX\Core\ShortcodeInline;
21 use NotificationX\Core\Targeting;
22 use NotificationX\Core\Upgrader;
23 use NotificationX\Extensions\GlobalFields;
24 use NotificationX\FrontEnd\FrontEnd;
25 use NotificationX\Types\TypeFactory;
26 use NotificationX\Extensions\ExtensionFactory;
27 use NotificationX\ThirdParty\WPML;
28 use NotificationX\Core\WPDRoleManagement;
29 use NotificationX\ThirdParty\VisualPortfolio;
30 use NotificationX\Extensions\Elementor\ElementorManager;
31
32 /**
33 * Plugin Engine.
34 * @method static NotificationX get_instance($args = null)
35 */
36 class NotificationX {
37 /**
38 * Instance of NotificationX
39 * @var NotificationX
40 */
41 use GetInstance;
42 /**
43 * Option flag recording that NotificationX has already gone through its
44 * initial (first-ever) activation on this site.
45 *
46 * Set on the first activation and seeded for pre-existing installs by
47 * Upgrader, so a re-activation is never mistaken for a first run.
48 *
49 * @since 3.3.0
50 */
51 const FIRST_ACTIVATION_OPTION = 'nx_first_activation';
52 /**
53 * Settings
54 * @var Settings
55 */
56 public $settings;
57 /**
58 * WP_CLI
59 * @var boolean
60 */
61 public static $WP_CLI = false;
62 /**
63 * Invoked initially.
64 */
65 public function __construct() {
66 static::$WP_CLI = defined('WP_CLI') && WP_CLI;
67 $this->settings = Settings::get_instance([
68 'key' => 'notificationx',
69 'auto_commit' => true,
70 'debug' => false,
71 'store' => 'options',
72 ]);
73
74 $args = Settings::get_instance()->get_role_map();
75 new WPDRoleManagement($args);
76
77 Upgrader::get_instance();
78 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reviewed for the NotificationX codebase: acceptable in this context.
79 if (is_admin() || empty($_GET['frontend']) || $_GET['frontend'] != true) {
80 Admin::get_instance();
81 }
82 FrontEnd::get_instance();
83 add_action('admin_init', [$this, 'maybe_redirect'], 10);
84 add_action('init', [$this, 'init'], 10);
85 add_action('plugins_loaded', array($this, 'init_extension'));
86 add_filter('nx_pro_alert_popup', array($this, 'pro_alert_popup'));
87 add_action( 'init', [ $this, 'register_custom_image_size' ] );
88 /**
89 * Register all REST Endpoint
90 */
91 REST::get_instance();
92 // Country/user-role targeting for ALL notification types (decoupled from the bar module).
93 Targeting::get_instance();
94 Cron::get_instance();
95 QuickBuild::get_instance();
96 ShortcodeInline::get_instance();
97 Blocks::get_instance();
98
99 CoreInstaller::get_instance(basename(NOTIFICATIONX_FILE, '.php'));
100
101 // 3rd Party features.
102 WPML::get_instance();
103 VisualPortfolio::get_instance();
104 if ( did_action( 'elementor/loaded' ) ) {
105 ElementorManager::get_instance();
106 } else {
107 add_action( 'elementor/loaded', [ ElementorManager::class, 'get_instance' ] );
108 }
109 EntriesMailReceiver::get_instance();
110 }
111 /**
112 * The Plugin Activator
113 * @return void
114 */
115 public function activator(){
116 // nx_activated
117 Database::get_instance()->Create_DB();
118
119 // A first-ever activation is the only moment the onboarding wizard may
120 // be auto-launched; an abandoned wizard still counts as "already
121 // activated", so re-activations never force it again.
122 $is_first_activation = ! self::has_activated_before();
123 if ( $is_first_activation ) {
124 update_option( self::FIRST_ACTIVATION_OPTION, time(), 'no' );
125 }
126
127 if( ! static::$WP_CLI && current_user_can( 'delete_users' ) ) {
128 set_transient( 'nx_activated', $is_first_activation ? 'first' : 'again', 30 );
129 }
130
131 // Usage insights are normally wired up on `init`, which WordPress has
132 // already fired by the time it activates a plugin — so the
133 // register_activation_hook() callback registered inside the insights
134 // constructor never runs and the tracking event is left unscheduled.
135 // Build the instance and run its activation routine explicitly here so
136 // data collection is live from activation onwards.
137 Admin::get_instance()->plugin_usage_insights()->activate_this_plugin();
138
139 Upgrader::get_instance()->clear_transient();
140 }
141
142 /**
143 * Whether NotificationX has already gone through its initial activation on
144 * this site — i.e. it has been activated before, regardless of whether the
145 * Setup Wizard was ever opened or completed.
146 *
147 * @since 3.3.0
148 * @return bool
149 */
150 public static function has_activated_before() {
151 return (bool) get_option( self::FIRST_ACTIVATION_OPTION, false );
152 }
153
154 public function maybe_redirect(){
155 if( static::$WP_CLI || wp_doing_ajax() ) {
156 return;
157 }
158 // Bail if no activation transient is set.
159 $activation = get_transient( 'nx_activated' );
160 if ( ! $activation ) {
161 return;
162 }
163 // Delete the activation transient.
164 delete_transient( 'nx_activated' );
165
166 if ( ! is_multisite() ) {
167 // Only a genuine first-ever activation may launch the onboarding
168 // Setup Wizard (and only while it has not been completed/skipped).
169 // Every returning activation lands on the dashboard, as before.
170 // Transients written by an older version hold `true`, which is
171 // treated as a returning activation.
172 $is_first_activation = ( 'first' === $activation );
173 $page = ( $is_first_activation && ! \NotificationX\Core\SetupWizard::is_completed() ) ? 'nx-setup-wizard' : 'nx-dashboard';
174 wp_safe_redirect( add_query_arg( array(
175 'page' => $page
176 ), admin_url( 'admin.php' ) ) );
177 }
178 }
179
180 public function init_extension() {
181 // TypeFactory::get_instance();
182 ExtensionFactory::get_instance();
183 }
184
185 public function init() {
186 /**
187 * Run All Actions and Filters
188 * for GOOD.
189 */
190 // load_plugin_textdomain( 'notificationx', false, dirname( plugin_basename( NOTIFICATIONX_FILE ) ) . '/languages' );
191
192 // @todo remove
193 if(defined('NX_DEBUG') && NX_DEBUG){
194 add_action('wp_ajax_nx', [$this, 'get_tabs']); // executed when logged in
195 }
196
197 add_action( 'plugin_action_links_' . NOTIFICATIONX_BASENAME, array($this, 'nx_action_links'), 10, 1);
198 }
199
200 public function pro_alert_popup($args) {
201 if ( !empty($args)){
202 $args = wp_parse_args($args, [
203 "showConfirmButton"=> true,
204 "showCloseButton"=>true,
205 "title" => __('Opps! This is PRO Feature.', 'notificationx'),
206 "customClass"=> [
207 "container"=> 'pro-video-popup',
208 "closeButton"=> 'pro-video-close-button',
209 "icon"=> 'pro-video-icon',
210 "title"=> 'pro-video-title',
211 "content"=> 'pro-video-content',
212 "actions"=> 'nx-pro-alert-actions',
213 "confirmButton"=> 'pro-video-confirm-button',
214 "denyButton"=> 'pro-video-deny-button',
215 ],
216 ]);
217 }
218 return $args;
219 }
220
221 /**
222 * This function is hooked
223 * @hooked plugin_action_links_
224 * @param array $links
225 * @return array
226 * @since 1.2.4
227 */
228 public function nx_action_links( $links ) {
229 $deactivate_link = isset( $links['deactivate'] ) ? $links['deactivate'] : '';
230 unset($links['deactivate']);
231 $links['settings'] = '<a href="' . admin_url('admin.php?page=nx-settings') . '">' . __('Settings','notificationx') .'</a>';
232 if( ! empty( $deactivate_link ) ) {
233 $links['deactivate'] = $deactivate_link;
234 }
235 if( ! is_plugin_active('notificationx-pro/notificationx-pro.php' ) ) {
236 $links['pro'] = '<a href="' . esc_url('https://notificationx.com/#pricing') . '" target="_blank" style="color: #349e34;"><b>' . __('Go Pro','notificationx') .'</b></a>';
237 }
238 return $links;
239 }
240
241 /**
242 * Checks whether pro plugin is active.
243 *
244 * @return boolean
245 */
246 public static function is_pro() {
247 return class_exists('\NotificationXPro\NotificationX');
248 }
249 /**
250 * Get Tabs array in json.
251 *
252 * @return json
253 */
254 public function get_tabs() {
255 $tabs = GlobalFields::get_instance()->tabs();
256 // $tabs = Settings::get_instance()->get_form_data();
257 $fields = $this->get_field_names($tabs['tabs']);
258 wp_send_json($tabs);
259 }
260 /**
261 * Convert `fields` associative array to numeric array recursively.
262 * @todo improve implementation.
263 *
264 * @param array $arr
265 * @return array
266 */
267 public function normalize($arr) {
268
269 if (!empty($arr['fields'])) {
270 $arr['fields'] = array_values($arr['fields']);
271 }
272
273 if (!empty($arr['options'])) {
274 $arr['options'] = array_values($arr['options']);
275 }
276
277 if (!empty($arr['tabs'])) {
278 $arr['tabs'] = array_values($arr['tabs']);
279 }
280
281 if (is_array($arr)) {
282 foreach ($arr as $key => $value) {
283 if (is_array($value)) {
284 $arr[$key] = $this->normalize($value);
285 }
286 }
287 }
288 return $arr;
289 }
290
291 public function normalize_post($settings) {
292 $fields = $this->get_field_names();
293 foreach ($fields as $key => $value) {
294 if (!isset($settings[$key]) && isset($value['default'])) {
295 $settings[$key] = $value['default'];
296 }
297 if (isset( $value['type'] ) && ($value['type'] == 'checkbox' || $value['type'] == 'toggle')) {
298 $settings[$key] = (bool) (isset($settings[$key]) ? $settings[$key] : false);
299 }
300 if (isset( $value['type'] ) && $value['type'] == 'number') {
301 $settings[$key] = isset($settings[$key]) ? $settings[$key] : 0;
302 $settings[$key] = is_numeric($settings[$key]) ? $settings[$key] + 0 : 0;
303 }
304 }
305 return $settings;
306 }
307
308 public function get_tab(){
309 $tabs = get_transient('nx_builder_fields');
310 if(empty($tabs) || (defined('NX_DEBUG') && NX_DEBUG)){
311 $tabs = GlobalFields::get_instance()->tabs();
312 set_transient( 'nx_builder_fields', $tabs, DAY_IN_SECONDS );
313 }
314 return $tabs;
315 }
316
317 public function get_field_names($tabs = null){
318 $fields = [];
319 if(empty($tabs)){
320 $tabs = $this->get_tab();
321 }
322 if(!empty($tabs['tabs'])){
323 $fields = $this->_get_field_names($tabs['tabs']);
324 }
325 return $fields;
326 }
327
328 public function get_field($field_name){
329 $fields = $this->get_field_names();
330 if(!empty($fields[$field_name])){
331 return $fields[$field_name];
332 }
333 }
334
335 // @todo maybe remove if not used in future.
336 public function _get_field_names($fields, $names = []) {
337 foreach ($fields as $key => $field) {
338 if (empty($field['type'])) {
339 $names = $this->_get_field_names($field['fields'], $names);
340 } else if ($field['type'] == 'section' || $field['type'] == 'group') { //
341 $_names = $this->_get_field_names($field['fields'], []);
342 if ($field['type'] == 'section')
343 $names = array_merge($names, $_names);
344 else
345 foreach ($_names as $key => $value) {
346 $names[$field['name']][$key] = [
347 'type' => $value['type'],
348 'default' => isset($value['default']) ? $value['default'] : '',
349 'help' => isset($value['help']) ? $value['help'] : '',
350 'label' => isset($value['label']) ? $value['label'] : '',
351 'parent_label' => isset($field['label']) ? $field['label'] : '',
352 ];
353 }
354 } elseif (!empty($field['name'])) {
355 $names[$field['name']] = [
356 'type' => $field['type'],
357 'default' => isset($field['default']) ? $field['default'] : '',
358 'help' => isset($field['help']) ? $field['help'] : '',
359 'label' => isset($field['label']) ? $field['label'] : '',
360 'multiple' => isset($field['multiple']) ? $field['multiple'] : '',
361 ];
362 }
363 }
364
365 return $names;
366 }
367
368 function my_upgrade_function( $upgrader_object, $options ) {
369 $current_plugin_path_name = plugin_basename( __FILE__ );
370
371 if ($options['action'] == 'update' && $options['type'] == 'plugin' ) {
372 foreach($options['plugins'] as $each_plugin) {
373 if ($each_plugin==$current_plugin_path_name) {
374 // .......................... YOUR CODES .............
375
376 }
377 }
378 }
379 }
380
381
382 public function register_custom_image_size() {
383 add_image_size('_nx_notification_thumb_100_100', 100, 100, true);
384 add_image_size('_nx_notification_thumb_200_200', 200, 200, true);
385 add_image_size('_nx_notification_thumb_300_300', 300, 300, true);
386 add_image_size('_nx_notification_thumb_400_400', 400, 400, true);
387 add_image_size('_nx_notification_thumb_500_500', 500, 500, true);
388 }
389
390 }
391