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