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 / BlockTypes.php

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

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