PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.9.19.4
Pods – Custom Content Types and Fields v2.9.19.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 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 2 weeks ago Types 2 weeks ago API.php 2 weeks ago Service_Provider.php 2 weeks ago
API.php
301 lines
1 <?php
2
3 namespace Pods\Blocks;
4
5 use Pods\Whatsit\Block;
6
7 /**
8 * Blocks functionality class.
9 *
10 * @since 2.8.0
11 */
12 class API {
13
14 /**
15 * Register blocks for the Pods Blocks API.
16 *
17 * @since 2.8.0
18 */
19 public function register_blocks() {
20 static $registered = false;
21
22 if ( $registered ) {
23 return;
24 }
25
26 $blocks = $this->get_blocks();
27 $js_blocks = $this->get_js_blocks();
28
29 // The Pods Blocks JS API.
30 $pods_blocks_options_file = file_get_contents( PODS_DIR . 'ui/js/blocks/pods-blocks-api.min.asset.json' );
31
32 $pods_blocks_options = null;
33
34 if ( $pods_blocks_options_file ) {
35 $pods_blocks_options = json_decode( $pods_blocks_options_file, true );
36 }
37
38 if ( ! is_array( $pods_blocks_options ) ) {
39 $pods_blocks_options = [
40 'dependencies' => [],
41 'version' => false,
42 ];
43 }
44
45 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 );
46
47 wp_set_script_translations( 'pods-blocks-api', 'pods' );
48
49 wp_localize_script( 'pods-blocks-api', 'podsBlocksConfig', [
50 'blocks' => $js_blocks,
51 // No custom collections to register directly with JS right now.
52 'collections' => [],
53 ] );
54
55 // The 'block_categories' filter has been deprecated in WordPress 5.8+ and replaced by 'block_categories_all'.
56 if ( pods_version_check( 'wp', '5.8-beta0' ) ) {
57 add_filter( 'block_categories_all', [ $this, 'register_block_collections' ] );
58 } else {
59 add_filter( 'block_categories', [ $this, 'register_block_collections' ] );
60 }
61
62 foreach ( $blocks as $block ) {
63 $block_name = $block['blockName'];
64
65 unset( $block['blockName'], $block['fields'] );
66
67 register_block_type( $block_name, $block );
68 }
69
70 $registered = true;
71 }
72
73 /**
74 * Setup core blocks.
75 *
76 * @since 2.8.0
77 */
78 public function setup_core_blocks() {
79 static $setup = false;
80
81 if ( $setup ) {
82 return;
83 }
84
85 /**
86 * Allow any integrations to be set up before core blocks and collections are called.
87 *
88 * @since 2.8.0
89 */
90 do_action( 'pods_blocks_api_pre_init' );
91
92 pods_container( 'pods.blocks.collection.pods' );
93
94 // Check if the feature is enabled.
95 if ( pods_can_use_dynamic_feature( 'display' ) ) {
96 pods_container( 'pods.blocks.field' );
97 pods_container( 'pods.blocks.list' );
98 pods_container( 'pods.blocks.single' );
99 pods_container( 'pods.blocks.single-list-fields' );
100 }
101
102 // Check if the feature is enabled.
103 if ( pods_can_use_dynamic_feature( 'form' ) ) {
104 pods_container( 'pods.blocks.form' );
105 }
106
107 // Check if the feature is enabled.
108 if ( pods_can_use_dynamic_feature( 'view' ) ) {
109 pods_container( 'pods.blocks.view' );
110 }
111
112 /**
113 * Allow custom blocks to be registered with Pods.
114 *
115 * @since 2.8.0
116 */
117 do_action( 'pods_blocks_api_init' );
118
119 $setup = true;
120 }
121
122 /**
123 * Get list of registered blocks for the Pods Blocks API.
124 *
125 * @since 2.8.0
126 *
127 * @return array List of registered blocks.
128 */
129 public function get_blocks() {
130 static $blocks = [];
131
132 if ( ! empty( $blocks ) ) {
133 return $blocks;
134 }
135
136 $this->setup_core_blocks();
137
138 $api = pods_api();
139
140 /**
141 * Allow filtering whether to bypass the post type find queries for blocks.
142 *
143 * @since 2.9.14
144 *
145 * @param bool $bypass_post_type_find Whether to bypass the post type find queries for blocks.
146 */
147 $bypass_post_type_find = apply_filters( 'pods_blocks_api_get_blocks_bypass_post_type_find', true );
148
149 /** @var Block[] $blocks */
150 $blocks = $api->_load_objects( [
151 'object_type' => 'block',
152 'bypass_cache' => true,
153 // Disable DB queries for now.
154 'bypass_post_type_find' => $bypass_post_type_find,
155 ] );
156
157 // Ensure the response is an array.
158 $blocks = array_values( $blocks );
159
160 $blocks = array_map( static function ( $block ) {
161 return $block->get_block_args();
162 }, $blocks );
163
164 return $blocks;
165 }
166
167 /**
168 * Get list of registered blocks for the Pods Blocks API and prepare them for JavaScript registerBlockType().
169 *
170 * @since 2.8.0
171 *
172 * @return array List of registered blocks prepared for JavaScript registerBlockType().
173 */
174 public function get_js_blocks() {
175 static $js_blocks = [];
176
177 if ( ! empty( $js_blocks ) ) {
178 return $js_blocks;
179 }
180
181 $cached = pods_transient_get( 'pods_blocks_js' );
182
183 if ( is_array( $cached ) ) {
184 return $cached;
185 }
186
187 $blocks = $this->get_blocks();
188
189 foreach ( $blocks as $block_key => $block ) {
190 $js_block = [];
191
192 // Remove render options.
193 unset( $block['render_callback'], $block['render_custom_callback'], $block['render_template'], $block['render_template_path'] );
194
195 // Remove assets options.
196 unset( $block['enqueue_assets'], $block['enqueue_script'], $block['enqueue_style'] );
197
198 foreach ( $block as $key => $value ) {
199 // Prepare the keys as camelCase.
200 $key = pods_js_camelcase_name( $key );
201
202 // Skip if the value is null.
203 if ( null === $value ) {
204 continue;
205 }
206
207 $js_block[ $key ] = $value;
208 }
209
210 if ( ! isset( $js_block['usesContext'] ) ) {
211 $js_block['usesContext'] = [];
212 }
213
214 $js_blocks[ $block_key ] = $js_block;
215 }
216
217 pods_transient_set( 'pods_blocks_js', $js_blocks, DAY_IN_SECONDS * 7 );
218
219 return $js_blocks;
220 }
221
222 /**
223 * Get list of registered block collections for the Pods Blocks API.
224 *
225 * @since 2.8.0
226 *
227 * @return array List of registered block collections.
228 */
229 public function get_block_collections() {
230 static $collections = [];
231
232 if ( ! empty( $collections ) ) {
233 return $collections;
234 }
235
236 $this->setup_core_blocks();
237
238 $api = pods_api();
239
240 /** @var Block_Collection[] $block_collections */
241 $block_collections = $api->_load_objects( [
242 'object_type' => 'block-collection',
243 ] );
244
245 // Ensure the response is an array.
246 $block_collections = array_values( $block_collections );
247
248 $block_collections = array_map( static function ( $block_collection ) {
249 return $block_collection->get_block_collection_args();
250 }, $block_collections );
251
252 return $block_collections;
253 }
254
255 /**
256 * Register block collections by adding them to the list of 'categories'.
257 *
258 * @since 2.8.0
259 *
260 * @param array $collections List of block 'categories' from WordPress.
261 *
262 * @return array List of block 'categories' with custom block collections added.
263 */
264 public function register_block_collections( array $collections ) {
265 $block_collections = $this->get_block_collections();
266
267 if ( empty( $block_collections ) ) {
268 return $collections;
269 }
270
271 foreach ( $block_collections as $collection ) {
272 $collections[] = [
273 'slug' => $collection['namespace'],
274 'title' => $collection['title'],
275 'icon' => $collection['icon'],
276 ];
277 }
278
279 return $collections;
280 }
281
282 /**
283 * Remove our legacy Pods widgets from the Legacy Widget block.
284 *
285 * @since 2.8.0
286 *
287 * @param array $widgets An array of excluded widget-type IDs.
288 *
289 * @return array An array of excluded widget-type IDs.
290 */
291 public function remove_from_legacy_widgets( $widgets ) {
292 $widgets[] = 'pods_widget_field';
293 $widgets[] = 'pods_widget_form';
294 $widgets[] = 'pods_widget_list';
295 $widgets[] = 'pods_widget_single';
296 $widgets[] = 'pods_widget_view';
297
298 return $widgets;
299 }
300 }
301