apps
8 months ago
assets
1 year ago
icons
1 year ago
scripts
8 months ago
class-wpchill-about-us.php
1 year ago
class-wpchill-notifications.php
8 months ago
class-wpchill-rest-api.php
1 year ago
class-wpchill-upsells.php
1 month ago
class-wpchill-rest-api.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound |
| 8 | class WPChill_Rest_Api { |
| 9 | protected $namespace = 'wpchill/v1'; |
| 10 | |
| 11 | public function __construct() { |
| 12 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 13 | } |
| 14 | |
| 15 | public function register_routes() { |
| 16 | register_rest_route( |
| 17 | $this->namespace, |
| 18 | '/notifications', |
| 19 | array( |
| 20 | 'methods' => 'GET', |
| 21 | 'callback' => array( $this, 'get_notifications' ), |
| 22 | 'permission_callback' => array( $this, '_permissions_check' ), |
| 23 | ) |
| 24 | ); |
| 25 | |
| 26 | register_rest_route( |
| 27 | $this->namespace, |
| 28 | '/notifications', |
| 29 | array( |
| 30 | 'methods' => 'DELETE', |
| 31 | 'callback' => array( $this, 'delete_notifications' ), |
| 32 | 'permission_callback' => array( $this, '_permissions_check' ), |
| 33 | ) |
| 34 | ); |
| 35 | |
| 36 | register_rest_route( |
| 37 | $this->namespace, |
| 38 | '/notifications/(?P<id>[\w-]+)', |
| 39 | array( |
| 40 | 'methods' => 'DELETE', |
| 41 | 'callback' => array( $this, 'delete_notification' ), |
| 42 | 'permission_callback' => array( $this, '_permissions_check' ), |
| 43 | ) |
| 44 | ); |
| 45 | |
| 46 | register_rest_route( |
| 47 | $this->namespace, |
| 48 | '/activate-plugin', |
| 49 | array( |
| 50 | 'methods' => 'POST', |
| 51 | 'callback' => array( $this, 'plugin_activation' ), |
| 52 | 'permission_callback' => array( $this, '_permissions_check' ), |
| 53 | ) |
| 54 | ); |
| 55 | |
| 56 | do_action( 'wpchill_rest_api_register_routes', $this ); |
| 57 | } |
| 58 | |
| 59 | public function process_request( $request ) { |
| 60 | $manager = WPChill_Notifications::get_instance(); |
| 61 | if ( 'DELETE' === $request->get_method() ) { |
| 62 | $body = $request->get_json_params(); |
| 63 | $post_id = isset( $body['id'] ) ? $body['id'] : false; |
| 64 | if ( $post_id ) { |
| 65 | $permanent = isset( $body['permanent'] ) ? $body['permanent'] : false; |
| 66 | $manager->clear_notification( $post_id, $permanent ); |
| 67 | return rest_ensure_response( true ); |
| 68 | } |
| 69 | $manager->clear_notifications(); |
| 70 | return rest_ensure_response( true ); |
| 71 | } |
| 72 | |
| 73 | $notifications = $manager->get_notifications(); |
| 74 | |
| 75 | $is_empty = array_reduce( |
| 76 | $notifications, |
| 77 | function ( $carry, $item ) { |
| 78 | return $carry && empty( $item ); |
| 79 | }, |
| 80 | true |
| 81 | ); |
| 82 | |
| 83 | if ( ! $is_empty ) { |
| 84 | return rest_ensure_response( $notifications ); |
| 85 | } |
| 86 | |
| 87 | return rest_ensure_response( array() ); |
| 88 | } |
| 89 | |
| 90 | public function delete_notifications() { |
| 91 | $manager = WPChill_Notifications::get_instance(); |
| 92 | $manager->clear_notifications(); |
| 93 | return rest_ensure_response( true ); |
| 94 | } |
| 95 | |
| 96 | public function delete_notification( $request ) { |
| 97 | $manager = WPChill_Notifications::get_instance(); |
| 98 | $body = $request->get_json_params(); |
| 99 | $notification_id = $request->get_param( 'id' ); |
| 100 | |
| 101 | if ( ! $notification_id ) { |
| 102 | return rest_ensure_response( false ); |
| 103 | } |
| 104 | |
| 105 | $permanent = isset( $body['permanent'] ) ? $body['permanent'] : false; |
| 106 | $manager->clear_notification( $notification_id, $permanent ); |
| 107 | return rest_ensure_response( true ); |
| 108 | } |
| 109 | |
| 110 | public function get_notifications() { |
| 111 | $manager = WPChill_Notifications::get_instance(); |
| 112 | $notifications = $manager->get_notifications(); |
| 113 | |
| 114 | $is_empty = array_reduce( |
| 115 | $notifications, |
| 116 | function ( $carry, $item ) { |
| 117 | return $carry && empty( $item ); |
| 118 | }, |
| 119 | true |
| 120 | ); |
| 121 | |
| 122 | return rest_ensure_response( $is_empty ? array() : $notifications ); |
| 123 | } |
| 124 | |
| 125 | public function plugin_activation( $request ) { |
| 126 | $plugin_slug = $request->get_param( 'plugin' ); |
| 127 | |
| 128 | return rest_ensure_response( WPChill_About_Us::activate_plugin( $plugin_slug ) ); |
| 129 | } |
| 130 | |
| 131 | public function _permissions_check() { |
| 132 | return current_user_can( 'manage_options' ); |
| 133 | } |
| 134 | } |
| 135 |