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

593 lines 20.3 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 $result = apply_filters('nx_rest_miscellaneous', null, $params);
463 if($result !== null){
464 return rest_ensure_response([
465 'success' => true,
466 ]);
467 }
468 else{
469 return rest_ensure_response([
470 'success' => false,
471 ]);
472 }
473 }
474
475 /**
476 * Return notices for frontend.
477 *
478 * @param \WP_REST_Request $request Full data about the request.
479 * @return void
480 */
481 public function notice($request) {
482 $params = $request->get_params();
483 return FrontEnd::get_instance()->get_notifications_data( $params );
484
485 }
486
487 public function delete_cookies($request)
488 {
489 return Helper::delete_server_cookies();
490 }
491
492 public function rest_data($nonce = true){
493 return apply_filters('nx_rest_data', array(
494 'root' => rest_url(),
495 'namespace' => $this->_namespace(),
496 'nonce' => $nonce ? wp_create_nonce( 'wp_rest' ) : '',
497 'omit_credentials' => Settings::get_instance()->get( 'settings.omit_credentials', true ),
498 ));
499 }
500
501 /**
502 * Undocumented function
503 *
504 * @param \WP_REST_Request $request
505 * @return
506 */
507 public function core_install( \WP_REST_Request $request ){
508 $params = $request->get_params();
509 $slug = $params['slug'];
510 $file = $params['file'];
511 $result = CoreInstaller::get_instance()->install_plugin($slug, $file);
512 return $result == null;
513 }
514
515 /**
516 * This is function will throw error for API
517 *
518 * @param string $type
519 * @return \WP_Error
520 */
521 public function error( $type = '' ) {
522 switch( $type ) {
523 case 'api':
524 return $this->formattedError( 'api_error', __( 'Unauthorized Access: You have to logged in first.', 'notificationx' ), 401 );
525 break;
526 case 'type':
527 return $this->formattedError( 'type_error', __( 'Invalid Type: You have to give a type.', 'notificationx' ), 401 );
528 break;
529 default:
530 return $this->formattedError( 'response_error', __( '400 Bad Request.', 'notificationx' ), 400 );
531 }
532 }
533
534 /**
535 * This function is responsible for format Error Message by \WP_Error
536 *
537 * @param string $code
538 * @param string $message
539 * @param integer $http_code
540 * @param array $args
541 * @return \WP_Error
542 */
543 private function formattedError( $code, $message, $http_code, $args = [] ){
544 return new \WP_Error( "nx_$code", $message, [ 'status' => $http_code ] );
545 }
546
547 /**
548 * JWT Whitelist
549 *
550 * @param array $endpoints
551 * @return array
552 */
553 public function jwt_whitelist( $endpoints ) {
554 $__endpoints = array(
555 '/wp-json/notificationx/v1',
556 '/wp-json/notificationx/v1/nx',
557 '/wp-json/notificationx/v1/nx/*',
558 '/wp-json/notificationx/v1/api-connect',
559 '/wp-json/notificationx/v1/notification/*',
560 '/wp-json/notificationx/v1/regenerate/*',
561 '/wp-json/notificationx/v1/reset/*',
562 '/wp-json/notificationx/v1/analytics',
563 '/wp-json/notificationx/v1/analytics/get',
564 '/wp-json/notificationx/v1/bulk-action/delete',
565 '/wp-json/notificationx/v1/bulk-action/regenerate',
566 '/wp-json/notificationx/v1/bulk-action/enable',
567 '/wp-json/notificationx/v1/bulk-action/disable',
568 '/wp-json/notificationx/v1/builder',
569 '/wp-json/notificationx/v1/core-install',
570 '/wp-json/notificationx/v1/elementor/import',
571 '/wp-json/notificationx/v1/gutenberg/import',
572 '/wp-json/notificationx/v1/elementor/remove',
573 '/wp-json/notificationx/v1/gutenberg/remove',
574 '/wp-json/notificationx/v1/exit-intent/elementor/import',
575 '/wp-json/notificationx/v1/exit-intent/elementor/remove',
576 '/wp-json/notificationx/v1/reporting-test',
577 '/wp-json/notificationx/v1/settings',
578 '/wp-json/notificationx/v1/miscellaneous',
579 '/wp-json/notificationx/v1/get-data',
580 '/wp-json/notificationx/v1/notice',
581 '/wp-json/notificationx/v1/delete-cookies',
582 '/wp-json/notificationx/v1/import',
583 '/wp-json/notificationx/v1/export',
584 '/wp-json/notificationx/v1/license/activate',
585 '/wp-json/notificationx/v1/license/deactivate',
586 '/wp-json/notificationx/v1/license/submit-otp',
587 '/wp-json/notificationx/v1/license/resend-otp',
588 );
589
590 return array_unique( array_merge( $endpoints, $__endpoints ) );
591 }
592 }
593