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

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