PluginProbe
Ultimate Cursor – Interactive and Animated Custom Cursor and Background Effects Toolkit / trunk
Ultimate Cursor – Interactive and Animated Custom Cursor and Background Effects Toolkit vtrunk
2.4.1 2.4.0 2.3.3 2.3.2 2.3.1 2.3.0 2.2.3 2.2.2 2.2.1 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 All 56 releases
ultimate-cursor / classes / class-rest.php

class-rest.php in Ultimate Cursor – Interactive and Animated Custom Cursor and Background Effects Toolkit trunk, at classes/class-rest.php

206 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Rest API functions
5 *
6 * @package ultimate cursor
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Class Ultimate_Cursor_Rest
15 */
16 class Ultimate_Cursor_Rest extends WP_REST_Controller {
17 /**
18 * The single class instance.
19 *
20 * @var $instance
21 */
22 private static $instance = null;
23
24 /**
25 * Get instance
26 */
27 public static function instance() {
28 if ( is_null( self::$instance ) ) {
29 self::$instance = new self();
30 }
31 return self::$instance;
32 }
33 /**
34 * Namespace.
35 *
36 * @var string
37 */
38 protected $namespace = 'ultimate/cursor/v';
39
40 /**
41 * Version.
42 *
43 * @var string
44 */
45 protected $version = '1';
46
47 /**
48 * Ultimate_Cursor_Rest constructor.
49 */
50 private function __construct() {
51 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
52 }
53
54 /**
55 * Register rest routes.
56 */
57 public function register_routes() {
58 $namespace = $this->namespace . $this->version;
59
60 $settings_args = array(
61 'settings' => array(
62 'description' => __( 'Settings object to merge into the stored option.', 'ultimate-cursor' ),
63 'type' => 'object',
64 'required' => true,
65 ),
66 );
67
68 // Update Settings.
69 register_rest_route(
70 $namespace,
71 '/update_settings/',
72 array(
73 'methods' => array( 'POST' ),
74 'callback' => array( $this, 'update_settings' ),
75 'permission_callback' => array( $this, 'update_settings_permission' ),
76 'args' => $settings_args,
77 )
78 );
79
80 // Update Background Settings.
81 register_rest_route(
82 $namespace,
83 '/update_background_settings/',
84 array(
85 'methods' => array( 'POST' ),
86 'callback' => array( $this, 'update_background_settings' ),
87 'permission_callback' => array( $this, 'update_settings_permission' ),
88 'args' => $settings_args,
89 )
90 );
91 }
92
93 /**
94 * Get edit options permissions.
95 *
96 * @return bool|WP_Error
97 */
98 public function update_settings_permission() {
99 if ( ! current_user_can( 'manage_options' ) ) {
100 return new WP_Error(
101 'rest_forbidden',
102 __( 'You do not have permission to change these options.', 'ultimate-cursor' ),
103 array( 'status' => rest_authorization_required_code() )
104 );
105 }
106
107 return true;
108 }
109
110 /**
111 * Update Settings.
112 *
113 * Input runs the schema-allowlist + premium-input-gate pipeline in
114 * Ultimate_Cursor_Settings_Schema::prepare_for_storage(). Unknown keys
115 * are dropped; premium fields are stripped from the payload (not from
116 * storage) when no valid license exists, so stored pro configuration
117 * stays dormant across a license lapse instead of being destroyed.
118 *
119 * @param WP_REST_Request $req request object.
120 *
121 * @return mixed
122 */
123 public function update_settings( WP_REST_Request $req ) {
124 $new_settings = $req->get_param( 'settings' );
125
126 if ( is_array( $new_settings ) ) {
127 $merged = Ultimate_Cursor_Settings_Schema::prepare_for_storage(
128 $new_settings,
129 get_option( 'ultimate_cursor_settings', array() ),
130 'cursor'
131 );
132
133 update_option( 'ultimate_cursor_settings', $merged );
134 }
135
136 return $this->success( true );
137 }
138
139
140 /**
141 * Update Background Settings.
142 *
143 * Same pipeline as update_settings() — see there for the gating rationale.
144 *
145 * @param WP_REST_Request $req request object.
146 *
147 * @return mixed
148 */
149 public function update_background_settings( WP_REST_Request $req ) {
150 $new_settings = $req->get_param( 'settings' );
151
152 if ( is_array( $new_settings ) ) {
153 $merged = Ultimate_Cursor_Settings_Schema::prepare_for_storage(
154 $new_settings,
155 get_option( 'ultimate_cursor_background_settings', array() ),
156 'background'
157 );
158
159 update_option( 'ultimate_cursor_background_settings', $merged );
160 }
161
162 return $this->success( true );
163 }
164
165 /**
166 * Success rest.
167 *
168 * @param mixed $response response data.
169 * @return mixed
170 */
171 public function success( $response ) {
172 return new WP_REST_Response(
173 array(
174 'success' => true,
175 'response' => $response,
176 ),
177 200
178 );
179 }
180
181 /**
182 * Error rest.
183 *
184 * @param mixed $code error code.
185 * @param mixed $response response data.
186 * @param boolean $true_error use true error response to stop the code processing.
187 * @return mixed
188 */
189 public function error( $code, $response, $true_error = false ) {
190 if ( $true_error ) {
191 return new WP_Error( $code, $response, array( 'status' => 401 ) );
192 }
193
194 return new WP_REST_Response(
195 array(
196 'error' => true,
197 'success' => false,
198 'error_code' => $code,
199 'response' => $response,
200 ),
201 401
202 );
203 }
204 }
205 Ultimate_Cursor_Rest::instance();
206