PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / trunk
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites vtrunk
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / admin / class-enable-disable-blocks.php

class-enable-disable-blocks.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites trunk, at admin/class-enable-disable-blocks.php

274 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (! defined('ABSPATH')) {
4 exit;
5 }
6
7 class Blockspare_Block_Manager
8 {
9
10 /**
11 * Option key for disabled blocks.
12 */
13 private $option_key = 'blockspare_disabled_blocks';
14
15 public function __construct()
16 {
17 add_filter(
18 'rest_request_after_callbacks',
19 [$this, 'rest_request_after_callbacks'],
20 10,
21 3
22 );
23
24 add_filter(
25 'allowed_block_types_all',
26 [$this, 'filter_blocks'],
27 10,
28 2
29 );
30
31 add_action(
32 'rest_api_init',
33 [$this, 'register_rest_routes']
34 );
35 }
36
37 /**
38 * Get all Blockspare blocks.
39 */
40 public function get_all_blocks()
41 {
42 $pattern = BLOCKSPARE_PLUGIN_DIR . 'build/block/*/block.json';
43 $files = glob($pattern);
44
45 $blocks = [];
46
47 foreach ($files as $file) {
48 $json = file_get_contents($file);
49 $data = json_decode($json, true);
50
51 if (
52 isset($data['name']) &&
53 strpos($data['name'], 'blockspare/') === 0
54 ) {
55 $blocks[] = $data['name'];
56 }
57 }
58
59 sort($blocks);
60
61 return $blocks;
62 }
63
64 /**
65 * Get disabled blocks.
66 */
67 public function get_disabled_blocks()
68 {
69 return get_option(
70 $this->option_key,
71 []
72 );
73 }
74
75 /**
76 * Save disabled blocks.
77 */
78 private function save_disabled_blocks($blocks)
79 {
80 update_option(
81 $this->option_key,
82 $blocks
83 );
84 }
85
86 /**
87 * Register REST routes.
88 */
89 public function register_rest_routes()
90 {
91 register_rest_route(
92 'blockspare/v1',
93 '/disabled-blocks',
94 [
95 'methods' => 'POST',
96 'callback' => [$this, 'save_disabled_blocks_rest'],
97 'permission_callback' => function () {
98 return current_user_can('manage_options');
99 },
100 ]
101 );
102 }
103
104 /**
105 * Save disabled blocks via REST.
106 */
107 public function save_disabled_blocks_rest($request)
108 {
109 $blocks = $request->get_param('blocks');
110
111 if (! is_array($blocks)) {
112 $blocks = [];
113 }
114
115 $blocks = array_map(
116 'sanitize_text_field',
117 $blocks
118 );
119
120 $this->save_disabled_blocks(
121 $blocks
122 );
123
124 return rest_ensure_response(
125 [
126 'success' => true,
127 ]
128 );
129 }
130
131 /**
132 * Remove disabled blocks from editor.
133 */
134 public function filter_blocks(
135 $allowed_blocks,
136 $editor_context
137 ) {
138 $disabled = $this->get_disabled_blocks();
139
140 if ($allowed_blocks === true) {
141 $registry = WP_Block_Type_Registry::get_instance();
142
143 $allowed_blocks = array_keys(
144 $registry->get_all_registered()
145 );
146 }
147
148 $filtered = [];
149
150 foreach ($allowed_blocks as $block) {
151
152 if (
153 is_object($block) &&
154 isset($block->name)
155 ) {
156 $name = $block->name;
157 } elseif (is_string($block)) {
158 $name = $block;
159 } else {
160 continue;
161 }
162
163 // Keep non-blockspare blocks.
164 if (
165 strpos($name, 'blockspare/') !== 0
166 ) {
167 $filtered[] = $block;
168 continue;
169 }
170
171 // Skip disabled blocks.
172 if (
173 in_array(
174 $name,
175 $disabled,
176 true
177 )
178 ) {
179 continue;
180 }
181
182 $filtered[] = $block;
183 }
184
185 return $filtered;
186 }
187
188 /**
189 * Filter block types endpoint.
190 */
191 public function rest_request_after_callbacks(
192 $response,
193 $handler,
194 $request
195 ) {
196 if (
197 ! $request ||
198 strpos(
199 $request->get_route(),
200 '/wp/v2/block-types'
201 ) === false
202 ) {
203 return $response;
204 }
205
206 $disabled = $this->get_disabled_blocks();
207
208 if (
209 ! method_exists(
210 $response,
211 'get_data'
212 )
213 ) {
214 return $response;
215 }
216
217 $data = $response->get_data();
218
219 // Single block response.
220 if (isset($data['name'])) {
221 if (
222 in_array(
223 $data['name'],
224 $disabled,
225 true
226 )
227 ) {
228 return new WP_REST_Response(
229 null,
230 404
231 );
232 }
233
234 return $response;
235 }
236
237 // Collection response.
238 $data = array_values(
239 array_filter(
240 $data,
241 function ($block) use ($disabled) {
242 if (! isset($block['name'])) {
243 return true;
244 }
245
246 $name = $block['name'];
247
248 if (
249 strpos(
250 $name,
251 'blockspare/'
252 ) !== 0
253 ) {
254 return true;
255 }
256
257 return ! in_array(
258 $name,
259 $disabled,
260 true
261 );
262 }
263 )
264 );
265
266 $response->set_data($data);
267
268 return $response;
269 }
270 }
271
272 global $blockspare_block_manager;
273 $blockspare_block_manager = new Blockspare_Block_Manager();
274