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

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

179 lines 4.1 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_block_types;
13 use function ContentControl\sanitize_block_type;
14 use function ContentControl\update_block_types;
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Rest API Settings Controller Class.
20 */
21 class BlockTypes extends WP_REST_Controller {
22
23 /**
24 * Endpoint namespace.
25 *
26 * @var string
27 */
28 protected $namespace = 'content-control/v2';
29
30 /**
31 * Route base.
32 *
33 * @var string
34 */
35 protected $base = 'blockTypes';
36
37 /**
38 * Register API endpoint routes.
39 *
40 * @return void
41 */
42 public function register_routes() {
43 register_rest_route(
44 $this->namespace,
45 '/' . $this->base,
46 [
47 [
48 'methods' => WP_REST_Server::READABLE,
49 'callback' => [ $this, 'get_block_types' ],
50 'permission_callback' => '__return_true', // Read only, so anyone can view.
51 ],
52 [
53 'methods' => WP_REST_Server::EDITABLE,
54 'callback' => [ $this, 'update_block_types' ],
55 'permission_callback' => [ $this, 'update_block_types_permissions' ],
56 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
57 ],
58 'schema' => [ $this, 'get_schema' ],
59 ]
60 );
61 }
62
63 /**
64 * Get block type list.
65 *
66 * @return WP_Error|WP_REST_Response
67 */
68 public function get_block_types() {
69 $block_types = get_block_types();
70
71 if ( $block_types ) {
72 return new WP_REST_Response( $block_types, 200 );
73 } else {
74 return new WP_Error( '404', __( 'Something went wrong, the block types could not be found.', 'content-control' ), [ 'status' => 404 ] );
75 }
76 }
77
78 /**
79 * Update plugin settings.
80 *
81 * @param \WP_REST_Request<array<string,mixed>> $request Request object.
82 *
83 * @return \WP_Error|\WP_REST_Response
84 */
85 public function update_block_types( $request ) {
86 // Get request json params.
87
88 /**
89 * Block types.
90 *
91 * @var array<int,array<string,string|string[]>> $block_types
92 */
93 $block_types = $request->get_json_params();
94
95 $error_message = __( 'Something went wrong, the block types could not be updated.', 'content-control' );
96
97 if ( ! is_array( get_block_types() ) ) {
98 return new WP_Error( '500', $error_message, [ 'status' => 500 ] );
99 }
100
101 foreach ( $block_types as $key => $type ) {
102 // Sanitize each new block type.
103 $block_types[ $key ] = sanitize_block_type( $type );
104 }
105
106 // Add or update incoming block types into the array.
107 update_block_types( $block_types );
108
109 $new_block_types = get_block_types();
110
111 if ( $new_block_types ) {
112 return new WP_REST_Response( $new_block_types, 200 );
113 } else {
114 return new WP_Error( '404', $error_message, [ 'status' => 404 ] );
115 }
116 }
117
118 /**
119 * Check update settings permissions.
120 *
121 * @return WP_Error|bool
122 */
123 public function update_block_types_permissions() {
124 return current_user_can( 'manage_options' ) || current_user_can( 'activate_plugins' );
125 }
126
127 /**
128 * Get settings schema.
129 *
130 * @return array<string,mixed>
131 */
132 public function get_schema() {
133 if ( $this->schema ) {
134 // Bail early if already cached.
135 return $this->schema;
136 }
137
138 $this->schema = apply_filters(
139 'content_control_rest_settings_schema',
140 [
141 '$schema' => 'http://json-schema.org/draft-04/schema#',
142 'title' => 'settings',
143 'type' => 'object',
144 'properties' => [
145 'block_controls' => [
146 'type' => 'object',
147 'properties' => [
148 'enable' => [
149 'type' => 'boolean',
150 ],
151 'controls' => [
152 'type' => 'object',
153 'properties' => [
154 'device_rules' => [
155 'type' => 'object',
156 'properties' => [
157 'enable' => [
158 'type' => 'boolean',
159 ],
160 ],
161 ],
162 ],
163 ],
164 'disabled_blocks' => [
165 'type' => 'array',
166 'items' => [
167 'type' => 'string',
168 ],
169 ],
170 ],
171 ],
172 ],
173 ]
174 );
175
176 return $this->schema;
177 }
178 }
179