PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.3
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.3
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 / Settings.php

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

140 lines 3.2 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;
12 use function ContentControl\get_all_plugin_options;
13 use function ContentControl\update_plugin_options;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Rest API Settings Controller Class.
19 */
20 class Settings extends WP_REST_Controller {
21
22 /**
23 * Endpoint namespace.
24 *
25 * @var string
26 */
27 protected $namespace = 'content-control/v2';
28
29 /**
30 * Route base.
31 *
32 * @var string
33 */
34 protected $base = 'settings';
35
36 /**
37 * Register API endpoint routes.
38 *
39 * @return void
40 */
41 public function register_routes() {
42 register_rest_route(
43 $this->namespace,
44 '/' . $this->base,
45 [
46 [
47 'methods' => WP_REST_Server::READABLE,
48 'callback' => [ $this, 'get_settings' ],
49 'permission_callback' => '__return_true', // Read only, so anyone can view.
50 ],
51 [
52 'methods' => WP_REST_Server::EDITABLE,
53 'callback' => [ $this, 'update_settings' ],
54 'permission_callback' => [ $this, 'update_settings_permissions' ],
55 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
56 ],
57 'schema' => [ $this, 'get_schema' ],
58 ]
59 );
60 }
61
62 /**
63 * Get plugin settings.
64 *
65 * @return WP_Error|WP_REST_Response
66 */
67 public function get_settings() {
68 $settings = get_all_plugin_options();
69
70 if ( $settings ) {
71 return new WP_REST_Response( [ 'settings' => $settings ], 200 );
72 } else {
73 return new WP_Error( '404', __( 'Something went wrong, the settings could not be found.', 'content-control' ), [ 'status' => 404 ] );
74 }
75 }
76
77 /**
78 * Update plugin settings.
79 *
80 * @param \WP_REST_Request<array<string,mixed>> $request Request object.
81 *
82 * @return \WP_Error|\WP_REST_Response
83 */
84 public function update_settings( $request ) {
85 $settings = $request->get_param( 'settings' );
86
87 $error_message = __( 'Something went wrong, the settings could not be updated.', 'content-control' );
88
89 if ( ! get_all_plugin_options() ) {
90 return new WP_Error( '500', $error_message, [ 'status' => 500 ] );
91 }
92
93 $updated = update_plugin_options( $settings );
94 $new_settings = get_all_plugin_options();
95
96 if ( $updated ) {
97 return new WP_REST_Response( $new_settings, 200 );
98 } else {
99 return new WP_Error( '404', $error_message, [ 'status' => 404 ] );
100 }
101 }
102
103 /**
104 * Check update settings permissions.
105 *
106 * @return WP_Error|bool
107 */
108 public function update_settings_permissions() {
109 return current_user_can( 'manage_options' ) || current_user_can( 'activate_plugins' );
110 }
111
112 /**
113 * Get settings schema.
114 *
115 * @return array<string,array<string,mixed>>
116 */
117 public function get_schema() {
118 if ( $this->schema ) {
119 // Bail early if already cached.
120 return $this->schema;
121 }
122
123 $this->schema = apply_filters(
124 'content_control_rest_settings_schema',
125 [
126 '$schema' => 'http://json-schema.org/draft-04/schema#',
127 'title' => 'settings',
128 'type' => 'object',
129 'properties' => [
130 'settings' => [
131 'type' => 'object',
132 ],
133 ],
134 ]
135 );
136
137 return $this->schema;
138 }
139 }
140