PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.3.0
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.3.0
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 / Core / REST.php

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

601 lines 21.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Extension Factory
5 *
6 * @package NotificationX\Extensions
7 */
8
9 namespace NotificationX\Core;
10
11 use NotificationX\Admin\ImportExport;
12 use NotificationX\Types\ContactForm;
13 use NotificationX\Admin\Settings;
14 use NotificationX\CoreInstaller;
15 use NotificationX\Extensions\PressBar\PressBar;
16 use NotificationX\Extensions\ExitIntent\ExitIntentNotification;
17 use NotificationX\Admin\Reports\ReportEmail;
18 use NotificationX\Admin\EntriesMailReceiver;
19 use NotificationX\Extensions\ExtensionFactory;
20 use NotificationX\Extensions\Google\GoogleReviews;
21 use NotificationX\FrontEnd\FrontEnd;
22 use NotificationX\GetInstance;
23 use NotificationX\Types\NotificationBar;
24 use WP_REST_Controller;
25 use WP_REST_Response;
26 use WP_REST_Server;
27 use WP_Error;
28
29
30 /**
31 * @method static REST get_instance($args = null)
32 */
33 class REST {
34 /**
35 * Instance of REST
36 *
37 * @var REST
38 */
39 use GetInstance;
40
41 private static $_namespace = 'notificationx';
42 private static $_version = 1;
43
44 public static function _namespace(){
45 return self::$_namespace . '/v' . self::$_version;
46 }
47
48 /**
49 * Invoked Automatically
50 */
51 public function __construct(){
52 Rest\Posts::get_instance();
53 Rest\Integration::get_instance();
54 Rest\Entries::get_instance();
55 Rest\Analytics::get_instance();
56 Rest\BulkAction::get_instance();
57 Rest\Popup::get_instance();
58
59 add_action('rest_api_init', [$this, 'register_routes']);
60 $enable_rest_api = Settings::get_instance()->get('settings.enable_rest_api', false);
61 if($enable_rest_api){
62 add_action('rest_authentication_errors', [$this, 'rest_authentication_errors'], 999);
63 add_filter('bb_exclude_endpoints_from_restriction', [$this, 'bb_exclude_endpoints'], 10, 2);
64 }
65
66
67 // third party
68 add_filter('jwt_auth_whitelist', [$this, 'jwt_whitelist']);
69 }
70
71 /**
72 * Checks for a current route being requested, and processes the allowlist
73 *
74 * @param $access
75 *
76 * @return WP_Error|null|boolean
77 */
78 public function rest_authentication_errors( $access ) {
79 $namespace = self::_namespace();
80 $current_route = $this->get_current_route();
81 if($access instanceof \WP_Error && ($current_route == "/$namespace/notice" || $current_route == "/$namespace/analytics" || $current_route == "/$namespace/delete-cookies")){
82 return true;
83 }
84
85 // If we got all the way here, return the unmodified $access response
86 return $access;
87 }
88
89 /**
90 * Exclude endpoints from bbPress restriction
91 *
92 * @param array $endpoints
93 * @param string $current_endpoint
94 * @return array
95 */
96 public function bb_exclude_endpoints( $endpoints, $current_endpoint ) {
97 $namespace = self::_namespace();
98 $endpoints[] = "/$namespace/notice";
99 $endpoints[] = "/$namespace/analytics";
100 $endpoints[] = "/$namespace/delete-cookies";
101 $endpoints[] = "/$namespace/send-rating";
102 return $endpoints;
103 }
104
105 /**
106 * Current REST route getter.
107 *
108 * @return string
109 */
110 private function get_current_route() {
111 $rest_route = $GLOBALS['wp']->query_vars['rest_route'];
112
113 return ( empty( $rest_route ) || '/' == $rest_route ) ?
114 $rest_route :
115 untrailingslashit( $rest_route );
116 }
117
118 /**
119 * Check if a given request has access to get items
120 *
121 * @param \WP_REST_Request $request Full data about the request.
122 * @return \WP_Error|bool
123 */
124 public function read_permission( $request ) {
125 return current_user_can('read_notificationx');
126 }
127 public function edit_permission( $request ) {
128 return current_user_can('edit_notificationx');
129 }
130 public function settings_permission( $request ) {
131 return current_user_can('edit_notificationx_settings');
132 }
133 public function activate_plugin_permission( $request ) {
134 $params = $request->get_params();
135 if(isset($params['is_installed'])){
136 if($params['is_installed']){
137 return current_user_can('activate_plugins');
138 }
139 else{
140 return current_user_can('install_plugins');
141 }
142 }
143 return current_user_can('activate_plugins') && current_user_can('install_plugins');
144 }
145
146 /**
147 * Register the routes for the objects of the controller.
148 */
149 public function register_routes() {
150 $namespace = self::_namespace();
151 register_rest_route( $namespace, '/builder', array(
152 'methods' => WP_REST_Server::READABLE,
153 'callback' => array( $this, 'get_builder' ),
154 'permission_callback' => array($this, 'read_permission'),
155 ));
156 register_rest_route( $namespace, '/core-install', array(
157 'methods' => WP_REST_Server::EDITABLE,
158 'callback' => array( $this, 'core_install' ),
159 'permission_callback' => array($this, 'activate_plugin_permission'),
160 ));
161 // Elementor Import
162 register_rest_route( $namespace, '/elementor/import', array(
163 'methods' => WP_REST_Server::EDITABLE,
164 'callback' => array( $this, 'elementor_import' ),
165 'permission_callback' => array($this, 'edit_permission'),
166 ));
167 register_rest_route( $namespace, '/elementor/remove', array(
168 'methods' => WP_REST_Server::EDITABLE,
169 'callback' => array( $this, 'elementor_remove' ),
170 'permission_callback' => array($this, 'edit_permission'),
171 ));
172 // Gutenberg Import
173 register_rest_route( $namespace, '/gutenberg/import', array(
174 'methods' => WP_REST_Server::EDITABLE,
175 'callback' => array( $this, 'gutenberg_import' ),
176 'permission_callback' => array($this, 'edit_permission'),
177 ));
178 register_rest_route( $namespace, '/gutenberg/remove', array(
179 'methods' => WP_REST_Server::EDITABLE,
180 'callback' => array( $this, 'gutenberg_remove' ),
181 'permission_callback' => array($this, 'edit_permission'),
182 ));
183 // Exit Intent — Elementor Import / Remove
184 register_rest_route( $namespace, '/exit-intent/elementor/import', array(
185 'methods' => WP_REST_Server::EDITABLE,
186 'callback' => array( $this, 'exit_intent_elementor_import' ),
187 'permission_callback' => array( $this, 'edit_permission' ),
188 ));
189 register_rest_route( $namespace, '/exit-intent/elementor/remove', array(
190 'methods' => WP_REST_Server::EDITABLE,
191 'callback' => array( $this, 'exit_intent_elementor_remove' ),
192 'permission_callback' => array( $this, 'edit_permission' ),
193 ));
194 // Reporting Import
195 register_rest_route( $namespace, '/reporting-test', array(
196 'methods' => WP_REST_Server::EDITABLE,
197 'callback' => array( $this, 'reporting_test' ),
198 'permission_callback' => array($this, 'settings_permission'),
199 ));
200
201 // Entries Mail Receiver Test
202 register_rest_route( $namespace, '/entries-mail-test', array(
203 'methods' => WP_REST_Server::EDITABLE,
204 'callback' => array( $this, 'entries_mail_test' ),
205 'permission_callback' => array( $this, 'settings_permission' ),
206 ));
207
208 // NX Settings
209 register_rest_route($namespace, '/settings', array(
210 array(
211 'methods' => WP_REST_Server::EDITABLE,
212 'callback' => array( $this, 'save_settings' ),
213 'permission_callback' => array($this, 'settings_permission'),
214 'args' => array(),
215 ),
216 ));
217
218 // NX Settings
219 register_rest_route($namespace, '/miscellaneous', array(
220 array(
221 'methods' => WP_REST_Server::EDITABLE,
222 'callback' => array( $this, 'miscellaneous' ),
223 'permission_callback' => array($this, 'settings_permission'),
224 'args' => array(),
225 ),
226 ));
227
228 // ajax select
229 register_rest_route($namespace, '/get-data', array(
230 array(
231 'methods' => WP_REST_Server::EDITABLE,
232 'callback' => array($this, 'get_data'),
233 'permission_callback' => array($this, 'read_permission'),
234 'args' => [],
235 ),
236 ));
237 // For Frontend Notice
238 register_rest_route($namespace, '/notice', array(
239 array(
240 'methods' => WP_REST_Server::EDITABLE,
241 'callback' => array($this, 'notice'),
242 'permission_callback' => '__return_true',
243 'args' => [],
244 ),
245 ));
246 register_rest_route($namespace, '/delete-cookies', array(
247 array(
248 'methods' => WP_REST_Server::READABLE,
249 'callback' => array($this, 'delete_cookies'),
250 'permission_callback' => '__return_true',
251 'args' => [],
252 ),
253 ));
254
255 // For entries page.
256 // register_rest_route($namespace, '/entries/(?P<nx_id>[0-9]+)', array(
257 // array(
258 // 'methods' => WP_REST_Server::READABLE,
259 // 'callback' => array($this, 'get_entries'),
260 // 'permission_callback' => '__return_true',
261 // 'args' => [],
262 // ),
263 // ));
264
265 // import/export
266 register_rest_route( $namespace, '/import', array(
267 'methods' => WP_REST_Server::EDITABLE,
268 'callback' => array( ImportExport::get_instance(), 'import' ),
269 'permission_callback' => array($this, 'edit_permission'),
270 ));
271 register_rest_route( $namespace, '/export', array(
272 'methods' => WP_REST_Server::EDITABLE,
273 'callback' => array( ImportExport::get_instance(), 'export' ),
274 'permission_callback' => array($this, 'edit_permission'),
275 ));
276
277 // Admin notice for dashboard
278
279 register_rest_route( $namespace, '/admin-notice-close', array(
280 'methods' => WP_REST_Server::EDITABLE,
281 'callback' => array( $this, 'nx_dashboard_admin_notice_close' ),
282 'permission_callback' => array($this, 'read_permission'),
283 ));
284 }
285
286 public function nx_dashboard_admin_notice_close( $request ){
287 update_option('nx_admin_notice_close', true);
288 return new \WP_REST_Response(array('success' => true), 200);
289 }
290
291
292
293 public function get_builder( $request ){
294 return PostType::get_instance()->get_localize_scripts();
295 }
296
297 /**
298 * Elementor Import for PressBar design.
299 *
300 * @param [type] $request
301 * @return void
302 */
303 public function elementor_import( $request ){
304 $params = $request->get_params();
305 PressBar::get_instance()->create_bar_of_type_bar_with_elementor($params);
306 return true;
307 }
308
309 /**
310 * Elementor Import for PressBar design.
311 *
312 * @param [type] $request
313 * @return void
314 */
315 public function elementor_remove( $request ){
316 $params = $request->get_params();
317 PressBar::get_instance()->delete_elementor_post($params['elementor_id']);
318 return true;
319 }
320
321 /**
322 * Exit Intent — Elementor import.
323 * Creates an `nx_exit_intent` Elementor document from a packaged seed and
324 * returns its ID + edit URL via wp_send_json_success(['context' => ...]).
325 */
326 public function exit_intent_elementor_import( $request ) {
327 $params = $request->get_params();
328 ExitIntentNotification::get_instance()->create_exit_intent_with_elementor( $params );
329 return true;
330 }
331
332 /**
333 * Exit Intent — Elementor remove. Deletes the linked `nx_exit_intent` post.
334 */
335 public function exit_intent_elementor_remove( $request ) {
336 $params = $request->get_params();
337 ExitIntentNotification::get_instance()->delete_elementor_post( $params['elementor_id'] ?? 0 );
338 return true;
339 }
340
341 /**
342 * Gutenberg Import for PressBar design.
343 *
344 * @param [type] $request
345 * @return void
346 */
347 public function gutenberg_import( $request ){
348 $params = $request->get_params();
349 return PressBar::get_instance()->gutenberg_import($params);
350 }
351
352 /**
353 * Gutenberg Import for PressBar design.
354 *
355 * @param [type] $request
356 * @return void
357 */
358 public function gutenberg_remove( $request ){
359 $params = $request->get_params();
360 PressBar::get_instance()->gutenberg_remove($params['gutenberg_id']);
361 return true;
362 }
363
364 /**
365 * Analytics Reporting
366 *
367 * @param WP_REST_Request $request
368 * @return void|boolean|array
369 */
370 public function reporting_test( $request ){
371 return ReportEmail::get_instance()->reporting( $request );
372 }
373
374 public function entries_mail_test( $request ) {
375 return EntriesMailReceiver::get_instance()->send_test( $request );
376 }
377
378 public function get_notificationX( $request ){
379 if( $request->get_method() === 'GET' ) {
380 return PostType::get_instance()->get_post_with_analytics();
381 }
382 if( $request->get_method() === 'POST' ) {
383 $params = $request->get_params();
384 return PostType::get_instance()->save_post($params);
385 }
386 }
387
388 /**
389 * Get data for specific type
390 *
391 * @param \WP_REST_Request $request Full data about the request.
392 * @return \WP_Error|\WP_REST_Response
393 */
394 public function get_data( $request ){
395
396 $params = $request->get_params();
397 if( ! $request->has_param('type') ) {
398 return $this->error( 'type' );
399 }
400
401 // Country targeting search is available for every notification type,
402 // so route it by field regardless of the type/source.
403 if ( ! empty( $params['field'] ) && $params['field'] === 'country_targeting' ) {
404 return Targeting::restResponse( $request->get_json_params() );
405 }
406
407 switch( $params['type'] ) {
408 case 'ContactForm' :
409 return ContactForm::restResponse( $request->get_json_params() );
410 break;
411 case 'notification_bar' :
412 return NotificationBar::restResponse( $request->get_json_params() );
413 break;
414 case 'reviews' :
415 switch ($params['source']) {
416 case 'google_reviews':
417 return GoogleReviews::get_instance()->restResponse($request->get_json_params() );
418 break;
419
420 default:
421 # code...
422 break;
423 }
424 break;
425 default:
426 $extension = ExtensionFactory::get_instance()->get( $params['source'] );
427 if (!empty($extension) && method_exists($extension, 'restResponse')) {
428 $result = $extension->restResponse($request->get_json_params());
429 return $result;
430 }
431 break;
432 }
433
434 return $this->error();
435 }
436
437 /**
438 *
439 *
440 * @param \WP_REST_Request $request Full data about the request.
441 * @return \WP_Error|\WP_REST_Response
442 */
443 public function save_settings($request) {
444 // $item = $this->prepare_item_for_database( $request );
445
446 $result = Settings::get_instance()->save_settings($request->get_params());
447 if($result){
448 return rest_ensure_response([
449 'success' => true,
450 ]);
451 }
452 else{
453 return rest_ensure_response([
454 'success' => false,
455 ]);
456 }
457 }
458
459 /**
460 *
461 *
462 * @param \WP_REST_Request $request Full data about the request.
463 * @return \WP_REST_Response
464 */
465 public function miscellaneous($request) {
466 $params = $request->get_params();
467
468 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
469 $result = apply_filters('nx_rest_miscellaneous', null, $params);
470 if($result !== null){
471 return rest_ensure_response([
472 'success' => true,
473 ]);
474 }
475 else{
476 return rest_ensure_response([
477 'success' => false,
478 ]);
479 }
480 }
481
482 /**
483 * Return notices for frontend.
484 *
485 * @param \WP_REST_Request $request Full data about the request.
486 * @return void
487 */
488 public function notice($request) {
489 $params = $request->get_params();
490 return FrontEnd::get_instance()->get_notifications_data( $params );
491
492 }
493
494 public function delete_cookies($request)
495 {
496 return Helper::delete_server_cookies();
497 }
498
499 public function rest_data($nonce = true){
500 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
501 return apply_filters('nx_rest_data', array(
502 'root' => rest_url(),
503 'namespace' => $this->_namespace(),
504 'nonce' => $nonce ? wp_create_nonce( 'wp_rest' ) : '',
505 'omit_credentials' => Settings::get_instance()->get( 'settings.omit_credentials', true ),
506 ));
507 }
508
509 /**
510 * Undocumented function
511 *
512 * @param \WP_REST_Request $request
513 * @return
514 */
515 public function core_install( \WP_REST_Request $request ){
516 $params = $request->get_params();
517 $slug = $params['slug'];
518 $file = $params['file'];
519 $result = CoreInstaller::get_instance()->install_plugin($slug, $file);
520 return $result == null;
521 }
522
523 /**
524 * This is function will throw error for API
525 *
526 * @param string $type
527 * @return \WP_Error
528 */
529 public function error( $type = '' ) {
530 switch( $type ) {
531 case 'api':
532 return $this->formattedError( 'api_error', __( 'Unauthorized Access: You have to logged in first.', 'notificationx' ), 401 );
533 break;
534 case 'type':
535 return $this->formattedError( 'type_error', __( 'Invalid Type: You have to give a type.', 'notificationx' ), 401 );
536 break;
537 default:
538 return $this->formattedError( 'response_error', __( '400 Bad Request.', 'notificationx' ), 400 );
539 }
540 }
541
542 /**
543 * This function is responsible for format Error Message by \WP_Error
544 *
545 * @param string $code
546 * @param string $message
547 * @param integer $http_code
548 * @param array $args
549 * @return \WP_Error
550 */
551 private function formattedError( $code, $message, $http_code, $args = [] ){
552 return new \WP_Error( "nx_$code", $message, [ 'status' => $http_code ] );
553 }
554
555 /**
556 * JWT Whitelist
557 *
558 * @param array $endpoints
559 * @return array
560 */
561 public function jwt_whitelist( $endpoints ) {
562 $__endpoints = array(
563 '/wp-json/notificationx/v1',
564 '/wp-json/notificationx/v1/nx',
565 '/wp-json/notificationx/v1/nx/*',
566 '/wp-json/notificationx/v1/api-connect',
567 '/wp-json/notificationx/v1/notification/*',
568 '/wp-json/notificationx/v1/regenerate/*',
569 '/wp-json/notificationx/v1/reset/*',
570 '/wp-json/notificationx/v1/analytics',
571 '/wp-json/notificationx/v1/analytics/get',
572 '/wp-json/notificationx/v1/bulk-action/delete',
573 '/wp-json/notificationx/v1/bulk-action/regenerate',
574 '/wp-json/notificationx/v1/bulk-action/enable',
575 '/wp-json/notificationx/v1/bulk-action/disable',
576 '/wp-json/notificationx/v1/builder',
577 '/wp-json/notificationx/v1/core-install',
578 '/wp-json/notificationx/v1/elementor/import',
579 '/wp-json/notificationx/v1/gutenberg/import',
580 '/wp-json/notificationx/v1/elementor/remove',
581 '/wp-json/notificationx/v1/gutenberg/remove',
582 '/wp-json/notificationx/v1/exit-intent/elementor/import',
583 '/wp-json/notificationx/v1/exit-intent/elementor/remove',
584 '/wp-json/notificationx/v1/reporting-test',
585 '/wp-json/notificationx/v1/settings',
586 '/wp-json/notificationx/v1/miscellaneous',
587 '/wp-json/notificationx/v1/get-data',
588 '/wp-json/notificationx/v1/notice',
589 '/wp-json/notificationx/v1/delete-cookies',
590 '/wp-json/notificationx/v1/import',
591 '/wp-json/notificationx/v1/export',
592 '/wp-json/notificationx/v1/license/activate',
593 '/wp-json/notificationx/v1/license/deactivate',
594 '/wp-json/notificationx/v1/license/submit-otp',
595 '/wp-json/notificationx/v1/license/resend-otp',
596 );
597
598 return array_unique( array_merge( $endpoints, $__endpoints ) );
599 }
600 }
601