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

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