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

162 lines 3.6 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 public function register_routes() {
40 register_rest_route(
41 $this->namespace,
42 '/' . $this->base,
43 [
44 [
45 'methods' => WP_REST_Server::READABLE,
46 'callback' => [ $this, 'get_settings' ],
47 'permission_callback' => '__return_true', // Read only, so anyone can view.
48 ],
49 [
50 'methods' => WP_REST_Server::EDITABLE,
51 'callback' => [ $this, 'update_settings' ],
52 'permission_callback' => [ $this, 'update_settings_permissions' ],
53 'args' => $this->get_endpoint_args_for_item_schema( true ),
54 ],
55 'schema' => [ $this, 'get_schema' ],
56 ]
57 );
58 }
59
60 /**
61 * Get plugin settings.
62 *
63 * @return WP_Error|WP_REST_Response
64 */
65 public function get_settings() {
66 $settings = get_all_plugin_options();
67
68 if ( $settings ) {
69 return new WP_REST_Response( $settings, 200 );
70 } else {
71 return new WP_Error( '404', __( 'Something went wrong, the settings could not be found.', 'content-control' ), [ 'status' => 404 ] );
72 }
73 }
74
75 /**
76 * Update plugin settings.
77 *
78 * @param WP_REST_Request $request Request object.
79 *
80 * @return WP_Error|WP_REST_Response
81 */
82 public function update_settings( $request ) {
83 $settings = $request->get_params();
84
85 $error_message = __( 'Something went wrong, the settings could not be updated.', 'content-control' );
86
87 if ( ! get_all_plugin_options() ) {
88 return new WP_Error( '500', $error_message, [ 'status' => 500 ] );
89 }
90
91 update_plugin_options( $settings );
92 $new_settings = get_all_plugin_options();
93
94 if ( $new_settings ) {
95 return new WP_REST_Response( $new_settings, 200 );
96 } else {
97 return new WP_Error( '404', $error_message, [ 'status' => 404 ] );
98 }
99 }
100
101 /**
102 * Check update settings permissions.
103 *
104 * @return WP_Error|bool
105 */
106 public function update_settings_permissions() {
107 return current_user_can( 'manage_options' ) || current_user_can( 'activate_plugins' );
108 }
109
110 /**
111 * Get settings schema.
112 *
113 * @return array
114 */
115 public function get_schema() {
116 if ( $this->schema ) {
117 // Bail early if already cached.
118 return $this->schema;
119 }
120
121 $this->schema = apply_filters(
122 'content_control_rest_settings_schema',
123 [
124 '$schema' => 'http://json-schema.org/draft-04/schema#',
125 'title' => 'settings',
126 'type' => 'object',
127 'properties' => [
128 'block_controls' => [
129 'type' => 'object',
130 'properties' => [
131 'enable' => [
132 'type' => 'boolean',
133 ],
134 'controls' => [
135 'type' => 'object',
136 'properties' => [
137 'device_rules' => [
138 'type' => 'object',
139 'properties' => [
140 'enable' => [
141 'type' => 'boolean',
142 ],
143 ],
144 ],
145 ],
146 ],
147 'disabled_blocks' => [
148 'type' => 'array',
149 'items' => [
150 'type' => 'string',
151 ],
152 ],
153 ],
154 ],
155 ],
156 ]
157 );
158
159 return $this->schema;
160 }
161 }
162