PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.0
Pods – Custom Content Types and Fields v3.2.0
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / Blocks / API.php
pods / src / Pods / Blocks Last commit date
Collections 4 years ago Types 2 years ago API.php 2 years ago Blocks_Abstract.php 2 years ago Blocks_Interface.php 3 years ago Service_Provider.php 3 years ago
API.php
382 lines
1 <?php
2
3 namespace Pods\Blocks;
4
5 use Pods;
6 use Pods\Pod_Manager;
7 use Pods\Whatsit\Block;
8
9 /**
10 * Blocks functionality class.
11 *
12 * @since 2.8.0
13 */
14 class API {
15
16 /**
17 * Register blocks for the Pods Blocks API.
18 *
19 * @since 2.8.0
20 */
21 public function register_blocks() {
22 static $registered = false;
23
24 if ( $registered ) {
25 return;
26 }
27
28 // The 'block_categories' filter has been deprecated in WordPress 5.8+ and replaced by 'block_categories_all'.
29 if ( pods_version_check( 'wp', '5.8-beta0' ) ) {
30 add_filter( 'block_categories_all', [ $this, 'register_block_collections' ] );
31 } else {
32 add_filter( 'block_categories', [ $this, 'register_block_collections' ] );
33 }
34
35 $blocks = $this->get_blocks();
36
37 foreach ( $blocks as $block ) {
38 $block_name = $block['blockName'];
39
40 unset( $block['blockName'], $block['fields'] );
41
42 register_block_type( $block_name, $block );
43 }
44
45 add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ], 15 );
46
47 $registered = true;
48 }
49
50 /**
51 * @return void
52 */
53 public function register_assets() {
54 $js_blocks = $this->get_js_blocks();
55
56 // The Pods Blocks JS API.
57 $pods_blocks_options_file = file_get_contents( PODS_DIR . 'ui/js/blocks/pods-blocks-api.min.asset.json' );
58
59 $pods_blocks_options = null;
60
61 if ( $pods_blocks_options_file ) {
62 $pods_blocks_options = json_decode( $pods_blocks_options_file, true );
63 }
64
65 if ( ! is_array( $pods_blocks_options ) ) {
66 $pods_blocks_options = [
67 'dependencies' => [],
68 'version' => false,
69 ];
70 }
71
72 wp_register_script( 'pods-blocks-api', PODS_URL . 'ui/js/blocks/pods-blocks-api.min.js', $pods_blocks_options['dependencies'], $pods_blocks_options['version'], true );
73
74 wp_set_script_translations( 'pods-blocks-api', 'pods' );
75
76 $blocks_config = [
77 'blocks' => $js_blocks,
78 'commands' => [],
79 // No custom collections to register directly with JS right now.
80 'collections' => [],
81 ];
82
83 $is_admin = is_admin();
84 $screen = ( $is_admin && function_exists( 'get_current_screen' ) ) ? get_current_screen() : null;
85
86 // Maybe add commands if the person has the right access.
87 if ( $screen && 'post' === $screen->base && $screen->post_type && pods_is_admin( 'pods' ) ) {
88 // Check if this is a Pod or not.
89 $api = pods_api();
90
91 $pod = false;
92
93 try {
94 $pod = $api->load_pod( [
95 'name' => $screen->post_type,
96 'auto_setup' => false,
97 ] );
98
99 // Check if this was auto-setup before and isn't a full pod.
100 if ( $pod && true === $pod->get_arg( 'adhoc' ) ) {
101 $pod = false;
102 }
103 } catch ( \Exception $exception ) {
104 // Nothing to do here.
105 }
106
107 if ( $pod ) {
108 $blocks_config['commands'][] = [
109 'name' => 'pods/edit',
110 'label' => __( 'Edit this Pod configuration', 'pods' ),
111 'searchLabel' => __( 'Edit this Pod configuration > Manage Field Groups, Custom Fields, and other Custom Post Type settings in the Pods Admin', 'pods' ),
112 'icon' => 'pods',
113 'callbackUrl' => admin_url(
114 sprintf(
115 'admin.php?page=pods&action=edit&id=%d',
116 $pod->get_id()
117 )
118 ),
119 ];
120 } else {
121 $nonce = wp_create_nonce( 'pods_extend_post_type_' . $screen->post_type );
122
123 $blocks_config['commands'][] = [
124 'name' => 'pods/extend',
125 'label' => __( 'Extend this Post Type with Pods to add custom fields', 'pods' ),
126 'icon' => 'pods',
127 'callbackUrl' => admin_url(
128 sprintf(
129 'admin.php?page=pods-add-new&pods_extend_post_type=%1$s&pods_extend_post_type_nonce=%2$s',
130 $screen->post_type,
131 $nonce
132 )
133 ),
134 ];
135 }
136 }
137
138 /**
139 * Allow filtering the blocks API config data.
140 *
141 * @since 3.0.0
142 *
143 * @param array $blocks_config The blocks API config data.
144 */
145 $blocks_config = (array) apply_filters( 'pods_blocks_api_config', $blocks_config );
146
147 wp_localize_script( 'pods-blocks-api', 'podsBlocksConfig', $blocks_config );
148
149 wp_enqueue_style( 'pods-styles' );
150 }
151
152 /**
153 * Setup core blocks.
154 *
155 * @since 2.8.0
156 */
157 public function setup_core_blocks() {
158 static $setup = false;
159
160 if ( $setup ) {
161 return;
162 }
163
164 /**
165 * Allow any integrations to be set up before core blocks and collections are called.
166 *
167 * @since 2.8.0
168 */
169 do_action( 'pods_blocks_api_pre_init' );
170
171 pods_container( 'pods.blocks.collection.pods' );
172
173 // Check if the feature is enabled.
174 if ( pods_can_use_dynamic_feature( 'display' ) ) {
175 pods_container( 'pods.blocks.field' );
176 pods_container( 'pods.blocks.list' );
177 pods_container( 'pods.blocks.single' );
178 pods_container( 'pods.blocks.single-list-fields' );
179 }
180
181 // Check if the feature is enabled.
182 if ( pods_can_use_dynamic_feature( 'form' ) ) {
183 pods_container( 'pods.blocks.form' );
184 }
185
186 // Check if the feature is enabled.
187 if ( pods_can_use_dynamic_feature( 'view' ) ) {
188 pods_container( 'pods.blocks.view' );
189 }
190
191 /**
192 * Allow custom blocks to be registered with Pods.
193 *
194 * @since 2.8.0
195 */
196 do_action( 'pods_blocks_api_init' );
197
198 $setup = true;
199 }
200
201 /**
202 * Get list of registered blocks for the Pods Blocks API.
203 *
204 * @since 2.8.0
205 *
206 * @return array List of registered blocks.
207 */
208 public function get_blocks() {
209 $blocks = pods_static_cache_get( __FUNCTION__, __CLASS__ );
210
211 if ( ! empty( $blocks ) && is_array( $blocks ) ) {
212 return $blocks;
213 }
214
215 $this->setup_core_blocks();
216
217 $api = pods_api();
218
219 /**
220 * Allow filtering whether to bypass the post type find queries for blocks.
221 *
222 * @since 2.9.14
223 *
224 * @param bool $bypass_post_type_find Whether to bypass the post type find queries for blocks.
225 */
226 $bypass_post_type_find = apply_filters( 'pods_blocks_api_get_blocks_bypass_post_type_find', true );
227
228 /** @var Block[] $blocks */
229 $blocks = $api->_load_objects( [
230 'object_type' => 'block',
231 'bypass_cache' => true,
232 // Disable DB queries for now.
233 'bypass_post_type_find' => $bypass_post_type_find,
234 ] );
235
236 // Ensure the response is an array.
237 $blocks = array_values( $blocks );
238
239 $blocks = array_map( static function ( $block ) {
240 return $block->get_block_args();
241 }, $blocks );
242
243 pods_static_cache_set( __FUNCTION__, $blocks, __CLASS__ );
244
245 return $blocks;
246 }
247
248 /**
249 * Get list of registered blocks for the Pods Blocks API and prepare them for JavaScript registerBlockType().
250 *
251 * @since 2.8.0
252 *
253 * @return array List of registered blocks prepared for JavaScript registerBlockType().
254 */
255 public function get_js_blocks() {
256 static $js_blocks = [];
257
258 if ( ! empty( $js_blocks ) ) {
259 return $js_blocks;
260 }
261
262 $cached = pods_transient_get( 'pods_blocks_js' );
263
264 if ( is_array( $cached ) ) {
265 return $cached;
266 }
267
268 $blocks = $this->get_blocks();
269
270 foreach ( $blocks as $block_key => $block ) {
271 $js_block = [];
272
273 // Remove render options.
274 unset( $block['render_callback'], $block['render_custom_callback'], $block['render_template'], $block['render_template_path'] );
275
276 // Remove assets options.
277 unset( $block['enqueue_assets'], $block['enqueue_script'], $block['enqueue_style'] );
278
279 foreach ( $block as $key => $value ) {
280 // Prepare the keys as camelCase.
281 $key = pods_js_camelcase_name( $key );
282
283 // Skip if the value is null.
284 if ( null === $value ) {
285 continue;
286 }
287
288 $js_block[ $key ] = $value;
289 }
290
291 if ( ! isset( $js_block['usesContext'] ) ) {
292 $js_block['usesContext'] = [];
293 }
294
295 $js_blocks[ $block_key ] = $js_block;
296 }
297
298 pods_transient_set( 'pods_blocks_js', $js_blocks, DAY_IN_SECONDS * 7 );
299
300 return $js_blocks;
301 }
302
303 /**
304 * Get list of registered block collections for the Pods Blocks API.
305 *
306 * @since 2.8.0
307 *
308 * @return array List of registered block collections.
309 */
310 public function get_block_collections() {
311 static $collections = [];
312
313 if ( ! empty( $collections ) ) {
314 return $collections;
315 }
316
317 $this->setup_core_blocks();
318
319 $api = pods_api();
320
321 /** @var Block_Collection[] $block_collections */
322 $block_collections = $api->_load_objects( [
323 'object_type' => 'block-collection',
324 ] );
325
326 // Ensure the response is an array.
327 $block_collections = array_values( $block_collections );
328
329 $block_collections = array_map( static function ( $block_collection ) {
330 return $block_collection->get_block_collection_args();
331 }, $block_collections );
332
333 return $block_collections;
334 }
335
336 /**
337 * Register block collections by adding them to the list of 'categories'.
338 *
339 * @since 2.8.0
340 *
341 * @param array $collections List of block 'categories' from WordPress.
342 *
343 * @return array List of block 'categories' with custom block collections added.
344 */
345 public function register_block_collections( array $collections ) {
346 $block_collections = $this->get_block_collections();
347
348 if ( empty( $block_collections ) ) {
349 return $collections;
350 }
351
352 foreach ( $block_collections as $collection ) {
353 $collections[] = [
354 'slug' => $collection['namespace'],
355 'title' => $collection['title'],
356 'icon' => $collection['icon'],
357 ];
358 }
359
360 return $collections;
361 }
362
363 /**
364 * Remove our legacy Pods widgets from the Legacy Widget block.
365 *
366 * @since 2.8.0
367 *
368 * @param array $widgets An array of excluded widget-type IDs.
369 *
370 * @return array An array of excluded widget-type IDs.
371 */
372 public function remove_from_legacy_widgets( $widgets ) {
373 $widgets[] = 'pods_widget_field';
374 $widgets[] = 'pods_widget_form';
375 $widgets[] = 'pods_widget_list';
376 $widgets[] = 'pods_widget_single';
377 $widgets[] = 'pods_widget_view';
378
379 return $widgets;
380 }
381 }
382