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