PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / RestAPI / License.php

License.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at classes/RestAPI/License.php

349 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * RestAPI Global Settings Endpoint.
4 *
5 * @copyright (c) 2021, Code Atlantic LLC.
6 * @package ContentControl
7 */
8
9 namespace ContentControl\RestAPI;
10
11 use WP_REST_Controller, WP_REST_Response, WP_REST_Server, WP_Error, WP_REST_Request;
12
13 use function ContentControl\plugin;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Temporary REST bridge for Pro releases older than 1.3.0.
19 *
20 * Note for WordPress.org Plugin Review Team: these routes are registered only
21 * while Core's deprecated old-Pro license bridge is active. Every route also
22 * requires Content Control's manage-settings capability. Core-only sites and
23 * sites running Pro 1.3.0 or later do not register these routes.
24 *
25 * @see \ContentControl\Plugin\Core::is_legacy_pro_active()
26 * @see \ContentControl\Controllers\RestAPI::register_routes()
27 *
28 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
29 */
30 class License extends WP_REST_Controller {
31
32 /**
33 * Endpoint namespace.
34 *
35 * @var string
36 */
37 protected $namespace = 'content-control/v2';
38
39 /**
40 * Route base.
41 *
42 * @var string
43 */
44 protected $base = 'license';
45
46 /**
47 * Register API endpoint routes.
48 *
49 * @return void
50 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
51 */
52 public function register_routes() {
53 register_rest_route(
54 $this->namespace,
55 '/' . $this->base,
56 [
57 [
58 'methods' => WP_REST_Server::READABLE,
59 'callback' => [ $this, 'get_license' ],
60 'permission_callback' => [ $this, 'manage_license_permissions' ],
61 ],
62 [
63 'methods' => WP_REST_Server::EDITABLE,
64 'callback' => [ $this, 'update_license_key' ],
65 'permission_callback' => [ $this, 'manage_license_permissions' ],
66 'args' => [
67 'licenseKey' => [
68 'required' => true,
69 'validate_callback' => function ( $param, $request, $key ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
70 return is_string( $param );
71 },
72 ],
73 ],
74 ],
75 [
76 'methods' => WP_REST_Server::DELETABLE,
77 'callback' => [ $this, 'remove_license' ],
78 'permission_callback' => [ $this, 'manage_license_permissions' ],
79 ],
80 ]
81 );
82
83 register_rest_route(
84 $this->namespace,
85 '/' . $this->base . '/activate',
86 [
87 [
88 'methods' => WP_REST_Server::EDITABLE,
89 'callback' => [ $this, 'activate_license' ],
90 'permission_callback' => [ $this, 'manage_license_permissions' ],
91 'args' => [
92 'licenseKey' => [
93 'required' => false,
94 'validate_callback' => function ( $param, $request, $key ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
95 return is_string( $param );
96 },
97 ],
98 ],
99 ],
100 ]
101 );
102
103 register_rest_route(
104 $this->namespace,
105 '/' . $this->base . '/deactivate',
106 [
107 [
108 'methods' => WP_REST_Server::EDITABLE,
109 'callback' => [ $this, 'deactivate_license' ],
110 'permission_callback' => [ $this, 'manage_license_permissions' ],
111 ],
112 ]
113 );
114
115 register_rest_route(
116 $this->namespace,
117 '/' . $this->base . '/status',
118 [
119 [
120 'methods' => WP_REST_Server::READABLE,
121 'callback' => [ $this, 'get_status' ],
122 'permission_callback' => [ $this, 'manage_license_permissions' ],
123 ],
124 [
125 'methods' => WP_REST_Server::EDITABLE,
126 'callback' => [ $this, 'refresh_license_status' ],
127 'permission_callback' => [ $this, 'manage_license_permissions' ],
128 ],
129 ]
130 );
131 }
132
133 /**
134 * Clean private or unnecessary data from license data before returning it.
135 *
136 * @param array{key:string,status:array<string,mixed>} $license_data License data.
137 * @return array{key:string,status:array<string,mixed>}
138 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
139 */
140 public function clean_license_data( $license_data ) {
141 $license_data['status'] = $this->clean_license_status( $license_data['status'] );
142
143 return $license_data;
144 }
145
146 /**
147 * Clean license status.
148 *
149 * @param array{key:string,status:array<string,mixed>} $license_status License status.
150 *
151 * @return (array|string)[]
152 *
153 * @psalm-return array<'key'|'status', array<string, mixed>|string>
154 * @phpstan-return array{key: string, status: array<string, mixed>|string}
155 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
156 */
157 public function clean_license_status( $license_status ) {
158 // Remove customer_email, customer_name, payment_id, ..., checksum keys from status array.
159 $license_status = array_diff_key(
160 $license_status,
161 array_flip(
162 [
163 'customer_email',
164 'customer_name',
165 'payment_id',
166 'checksum',
167 'item_id',
168 'item_name',
169 ]
170 )
171 );
172
173 return $license_status;
174 }
175
176 /**
177 * Get plugin license.
178 *
179 * @return \WP_Error|\WP_REST_Response
180 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
181 */
182 public function get_license() {
183 $license_data = plugin( 'license' )->get_license_data();
184
185 if ( $license_data ) {
186 return new WP_REST_Response( $this->clean_license_data( $license_data ), 200 );
187 } else {
188 return new WP_Error( '404', __( 'Something went wrong.', 'content-control' ), [ 'status' => 404 ] );
189 }
190 }
191
192 /**
193 * Update plugin license key.
194 *
195 * @param \WP_REST_Request<array<string,mixed>> $request Request object.
196 *
197 * @return \WP_Error|\WP_REST_Response
198 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
199 */
200 public function update_license_key( $request ) {
201 $license_key = sanitize_text_field( $request->get_param( 'licenseKey' ) );
202
203 try {
204 $old_key = plugin( 'license' )->get_license_key();
205
206 if ( $old_key === $license_key ) {
207 $license_status = plugin( 'license' )->get_license_status();
208 return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 );
209 }
210
211 if ( plugin( 'license' )->is_license_active() ) {
212 plugin( 'license' )->deactivate_license();
213 }
214
215 plugin( 'license' )->update_license_key( $license_key );
216
217 $license_status = plugin( 'license' )->activate_license( $license_key );
218
219 return new WP_REST_Response(
220 [
221 'status' => $this->clean_license_status( $license_status ),
222 ],
223 200
224 );
225 } catch ( \Exception $e ) {
226 $message = __( 'Something went wrong, the license key could not be saved.', 'content-control' );
227
228 if ( '' !== $e->getMessage() ) {
229 $message = $e->getMessage();
230 }
231
232 return new WP_Error( '404', $message, [ 'status' => 404 ] );
233 }
234 }
235
236 /**
237 * Remove plugin license key.
238 *
239 * @return \WP_Error|\WP_REST_Response
240 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
241 */
242 public function remove_license() {
243 plugin( 'license' )->remove_license();
244
245 if ( '' === plugin( 'license' )->get_license_key() ) {
246 return new WP_REST_Response( true, 200 );
247 } else {
248 return new WP_Error( '404', __( 'Something went wrong.', 'content-control' ), [ 'status' => 404 ] );
249 }
250 }
251
252 /**
253 * Activate plugin license.
254 *
255 * @param WP_REST_Request<array<string,mixed>> $request Request object.
256 *
257 * @return \WP_Error|\WP_REST_Response
258 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
259 */
260 public function activate_license( $request ) {
261 $license_key = $request->has_param( 'licenseKey' )
262 ? sanitize_text_field( $request->get_param( 'licenseKey' ) )
263 : null;
264
265 try {
266 $license_status = plugin( 'license' )->activate_license( $license_key );
267
268 return new WP_REST_Response(
269 [
270 'status' => $this->clean_license_status( $license_status ),
271 ],
272 200
273 );
274 } catch ( \Exception $e ) {
275 $message = __( 'Something went wrong, the license could not be activated.', 'content-control' );
276
277 if ( '' !== $e->getMessage() ) {
278 $message = $e->getMessage();
279 }
280
281 return new WP_Error( '404', $message, [ 'status' => 404 ] );
282 }
283 }
284
285 /**
286 * Deactivate plugin license.
287 *
288 * @return \WP_Error|\WP_REST_Response
289 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
290 */
291 public function deactivate_license() {
292 try {
293 $license_status = plugin( 'license' )->deactivate_license();
294
295 return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 );
296 } catch ( \Exception $e ) {
297 $message = __( 'Something went wrong, the license could not be deactivated.', 'content-control' );
298
299 if ( '' !== $e->getMessage() ) {
300 $message = $e->getMessage();
301 }
302
303 return new WP_Error( '404', $message, [ 'status' => 404 ] );
304 }
305 }
306
307 /**
308 * Get plugin license status.
309 *
310 * @return \WP_Error|\WP_REST_Response
311 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
312 */
313 public function get_status() {
314 $license_status = plugin( 'license' )->get_license_status();
315
316 if ( $license_status ) {
317 return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 );
318 } else {
319 return new WP_Error( '404', __( 'Something went wrong.', 'content-control' ), [ 'status' => 404 ] );
320 }
321 }
322
323 /**
324 * Refresh plugin license status.
325 *
326 * @return \WP_Error|\WP_REST_Response
327 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
328 */
329 public function refresh_license_status() {
330 $license_status = plugin( 'license' )->get_license_status( true );
331
332 if ( $license_status ) {
333 return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 );
334 } else {
335 return new WP_Error( '404', __( 'Something went wrong.', 'content-control' ), [ 'status' => 404 ] );
336 }
337 }
338
339 /**
340 * Check update settings permissions.
341 *
342 * @return bool
343 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes.
344 */
345 public function manage_license_permissions() {
346 return current_user_can( plugin()->get_permission( 'manage_settings' ) );
347 }
348 }
349