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

595 lines 20.7 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 switch( $params['type'] ) {
402 case 'ContactForm' :
403 return ContactForm::restResponse( $request->get_json_params() );
404 break;
405 case 'notification_bar' :
406 return NotificationBar::restResponse( $request->get_json_params() );
407 break;
408 case 'reviews' :
409 switch ($params['source']) {
410 case 'google_reviews':
411 return GoogleReviews::get_instance()->restResponse($request->get_json_params() );
412 break;
413
414 default:
415 # code...
416 break;
417 }
418 break;
419 default:
420 $extension = ExtensionFactory::get_instance()->get( $params['source'] );
421 if (!empty($extension) && method_exists($extension, 'restResponse')) {
422 $result = $extension->restResponse($request->get_json_params());
423 return $result;
424 }
425 break;
426 }
427
428 return $this->error();
429 }
430
431 /**
432 *
433 *
434 * @param \WP_REST_Request $request Full data about the request.
435 * @return \WP_Error|\WP_REST_Response
436 */
437 public function save_settings($request) {
438 // $item = $this->prepare_item_for_database( $request );
439
440 $result = Settings::get_instance()->save_settings($request->get_params());
441 if($result){
442 return rest_ensure_response([
443 'success' => true,
444 ]);
445 }
446 else{
447 return rest_ensure_response([
448 'success' => false,
449 ]);
450 }
451 }
452
453 /**
454 *
455 *
456 * @param \WP_REST_Request $request Full data about the request.
457 * @return \WP_REST_Response
458 */
459 public function miscellaneous($request) {
460 $params = $request->get_params();
461
462 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
463 $result = apply_filters('nx_rest_miscellaneous', null, $params);
464 if($result !== null){
465 return rest_ensure_response([
466 'success' => true,
467 ]);
468 }
469 else{
470 return rest_ensure_response([
471 'success' => false,
472 ]);
473 }
474 }
475
476 /**
477 * Return notices for frontend.
478 *
479 * @param \WP_REST_Request $request Full data about the request.
480 * @return void
481 */
482 public function notice($request) {
483 $params = $request->get_params();
484 return FrontEnd::get_instance()->get_notifications_data( $params );
485
486 }
487
488 public function delete_cookies($request)
489 {
490 return Helper::delete_server_cookies();
491 }
492
493 public function rest_data($nonce = true){
494 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
495 return apply_filters('nx_rest_data', array(
496 'root' => rest_url(),
497 'namespace' => $this->_namespace(),
498 'nonce' => $nonce ? wp_create_nonce( 'wp_rest' ) : '',
499 'omit_credentials' => Settings::get_instance()->get( 'settings.omit_credentials', true ),
500 ));
501 }
502
503 /**
504 * Undocumented function
505 *
506 * @param \WP_REST_Request $request
507 * @return
508 */
509 public function core_install( \WP_REST_Request $request ){
510 $params = $request->get_params();
511 $slug = $params['slug'];
512 $file = $params['file'];
513 $result = CoreInstaller::get_instance()->install_plugin($slug, $file);
514 return $result == null;
515 }
516
517 /**
518 * This is function will throw error for API
519 *
520 * @param string $type
521 * @return \WP_Error
522 */
523 public function error( $type = '' ) {
524 switch( $type ) {
525 case 'api':
526 return $this->formattedError( 'api_error', __( 'Unauthorized Access: You have to logged in first.', 'notificationx' ), 401 );
527 break;
528 case 'type':
529 return $this->formattedError( 'type_error', __( 'Invalid Type: You have to give a type.', 'notificationx' ), 401 );
530 break;
531 default:
532 return $this->formattedError( 'response_error', __( '400 Bad Request.', 'notificationx' ), 400 );
533 }
534 }
535
536 /**
537 * This function is responsible for format Error Message by \WP_Error
538 *
539 * @param string $code
540 * @param string $message
541 * @param integer $http_code
542 * @param array $args
543 * @return \WP_Error
544 */
545 private function formattedError( $code, $message, $http_code, $args = [] ){
546 return new \WP_Error( "nx_$code", $message, [ 'status' => $http_code ] );
547 }
548
549 /**
550 * JWT Whitelist
551 *
552 * @param array $endpoints
553 * @return array
554 */
555 public function jwt_whitelist( $endpoints ) {
556 $__endpoints = array(
557 '/wp-json/notificationx/v1',
558 '/wp-json/notificationx/v1/nx',
559 '/wp-json/notificationx/v1/nx/*',
560 '/wp-json/notificationx/v1/api-connect',
561 '/wp-json/notificationx/v1/notification/*',
562 '/wp-json/notificationx/v1/regenerate/*',
563 '/wp-json/notificationx/v1/reset/*',
564 '/wp-json/notificationx/v1/analytics',
565 '/wp-json/notificationx/v1/analytics/get',
566 '/wp-json/notificationx/v1/bulk-action/delete',
567 '/wp-json/notificationx/v1/bulk-action/regenerate',
568 '/wp-json/notificationx/v1/bulk-action/enable',
569 '/wp-json/notificationx/v1/bulk-action/disable',
570 '/wp-json/notificationx/v1/builder',
571 '/wp-json/notificationx/v1/core-install',
572 '/wp-json/notificationx/v1/elementor/import',
573 '/wp-json/notificationx/v1/gutenberg/import',
574 '/wp-json/notificationx/v1/elementor/remove',
575 '/wp-json/notificationx/v1/gutenberg/remove',
576 '/wp-json/notificationx/v1/exit-intent/elementor/import',
577 '/wp-json/notificationx/v1/exit-intent/elementor/remove',
578 '/wp-json/notificationx/v1/reporting-test',
579 '/wp-json/notificationx/v1/settings',
580 '/wp-json/notificationx/v1/miscellaneous',
581 '/wp-json/notificationx/v1/get-data',
582 '/wp-json/notificationx/v1/notice',
583 '/wp-json/notificationx/v1/delete-cookies',
584 '/wp-json/notificationx/v1/import',
585 '/wp-json/notificationx/v1/export',
586 '/wp-json/notificationx/v1/license/activate',
587 '/wp-json/notificationx/v1/license/deactivate',
588 '/wp-json/notificationx/v1/license/submit-otp',
589 '/wp-json/notificationx/v1/license/resend-otp',
590 );
591
592 return array_unique( array_merge( $endpoints, $__endpoints ) );
593 }
594 }
595