PluginProbe
Gutenberg / 10.7.4
Gutenberg v10.7.4
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat.php

compat.php in Gutenberg 10.7.4, at lib/compat.php

220 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Temporary compatibility shims for features present in Gutenberg, pending
4 * upstream commit to the WordPress core source repository. Functions here
5 * exist only as long as necessary for corresponding WordPress support, and
6 * each should be associated with a Trac ticket.
7 *
8 * @package gutenberg
9 */
10
11 /**
12 * Determine if the current theme needs to load separate block styles or not.
13 *
14 * @todo Remove this function when the minimum supported version is WordPress 5.8.
15 *
16 * @return bool
17 */
18 function gutenberg_should_load_separate_block_assets() {
19 if ( function_exists( 'wp_should_load_separate_core_block_assets' ) ) {
20 return wp_should_load_separate_core_block_assets();
21 }
22
23 if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
24 return false;
25 }
26
27 // The `should_load_separate_core_block_assets` filter was added in WP 5.8.
28 $load_separate_styles = apply_filters( 'should_load_separate_core_block_assets', gutenberg_is_fse_theme() );
29
30 /**
31 * Determine if separate styles will be loaded for blocks on-render or not.
32 *
33 * @param bool $load_separate_styles Whether separate styles will be loaded or not.
34 *
35 * @return bool
36 */
37 return apply_filters( 'load_separate_block_assets', $load_separate_styles );
38 }
39
40 /**
41 * Opt-in to separate styles loading for block themes in WordPress 5.8.
42 *
43 * @todo Remove this function when the minimum supported version is WordPress 5.8.
44 */
45 add_filter(
46 'separate_core_block_assets',
47 function( $load_separate_styles ) {
48 if ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) {
49 return true;
50 }
51 return $load_separate_styles;
52 }
53 );
54
55 /**
56 * Remove the `wp_enqueue_registered_block_scripts_and_styles` hook if needed.
57 *
58 * @return void
59 */
60 function gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles() {
61 if ( gutenberg_should_load_separate_block_assets() ) {
62 /**
63 * Avoid enqueueing block assets of all registered blocks for all posts, instead
64 * deferring to block render mechanics to enqueue scripts, thereby ensuring only
65 * blocks of the content have their assets enqueued.
66 *
67 * This can be removed once minimum support for the plugin is outside the range
68 * of the version associated with closure of the following ticket.
69 *
70 * @see https://core.trac.wordpress.org/ticket/50328
71 *
72 * @see WP_Block::render
73 */
74 remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
75 }
76 }
77
78 add_action( 'init', 'gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles' );
79
80 /**
81 * Callback hooked to the register_block_type_args filter.
82 *
83 * This hooks into block registration to inject the default context into the block object.
84 * It can be removed once the default context is added into Core.
85 *
86 * @param array $args Block attributes.
87 * @return array Block attributes.
88 */
89 function gutenberg_inject_default_block_context( $args ) {
90 if ( is_callable( $args['render_callback'] ) ) {
91 $block_render_callback = $args['render_callback'];
92 $args['render_callback'] = function( $attributes, $content, $block = null ) use ( $block_render_callback ) {
93 global $post, $wp_query;
94
95 // Check for null for back compatibility with WP_Block_Type->render
96 // which is unused since the introduction of WP_Block class.
97 //
98 // See:
99 // - https://core.trac.wordpress.org/ticket/49927
100 // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop.
101
102 if ( null === $block ) {
103 return $block_render_callback( $attributes, $content );
104 }
105
106 $registry = WP_Block_Type_Registry::get_instance();
107 $block_type = $registry->get_registered( $block->name );
108
109 // For WordPress versions that don't support the context API.
110 if ( ! $block->context ) {
111 $block->context = array();
112 }
113
114 // Inject the post context if not done by Core.
115 $needs_post_id = ! empty( $block_type->uses_context ) && in_array( 'postId', $block_type->uses_context, true );
116 if ( $post instanceof WP_Post && $needs_post_id && ! isset( $block->context['postId'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) {
117 $block->context['postId'] = $post->ID;
118 }
119 $needs_post_type = ! empty( $block_type->uses_context ) && in_array( 'postType', $block_type->uses_context, true );
120 if ( $post instanceof WP_Post && $needs_post_type && ! isset( $block->context['postType'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) {
121 /*
122 * The `postType` context is largely unnecessary server-side, since the
123 * ID is usually sufficient on its own. That being said, since a block's
124 * manifest is expected to be shared between the server and the client,
125 * it should be included to consistently fulfill the expectation.
126 */
127 $block->context['postType'] = $post->post_type;
128 }
129
130 // Inject the query context if not done by Core.
131 $needs_query = ! empty( $block_type->uses_context ) && in_array( 'query', $block_type->uses_context, true );
132 if ( ! isset( $block->context['query'] ) && $needs_query ) {
133 if ( isset( $wp_query->tax_query->queried_terms['category'] ) ) {
134 $block->context['query'] = array( 'categoryIds' => array() );
135
136 foreach ( $wp_query->tax_query->queried_terms['category']['terms'] as $category_slug_or_id ) {
137 $block->context['query']['categoryIds'][] = 'slug' === $wp_query->tax_query->queried_terms['category']['field'] ? get_cat_ID( $category_slug_or_id ) : $category_slug_or_id;
138 }
139 }
140
141 if ( isset( $wp_query->tax_query->queried_terms['post_tag'] ) ) {
142 if ( isset( $block->context['query'] ) ) {
143 $block->context['query']['tagIds'] = array();
144 } else {
145 $block->context['query'] = array( 'tagIds' => array() );
146 }
147
148 foreach ( $wp_query->tax_query->queried_terms['post_tag']['terms'] as $tag_slug_or_id ) {
149 $tag_ID = $tag_slug_or_id;
150
151 if ( 'slug' === $wp_query->tax_query->queried_terms['post_tag']['field'] ) {
152 $tag = get_term_by( 'slug', $tag_slug_or_id, 'post_tag' );
153
154 if ( $tag ) {
155 $tag_ID = $tag->term_id;
156 }
157 }
158 $block->context['query']['tagIds'][] = $tag_ID;
159 }
160 }
161 }
162
163 return $block_render_callback( $attributes, $content, $block );
164 };
165 }
166 return $args;
167 }
168
169 add_filter( 'register_block_type_args', 'gutenberg_inject_default_block_context' );
170
171 /**
172 * Override post type labels for Reusable Block custom post type.
173 * The labels are different from the ones in Core.
174 *
175 * Remove this when Core receives the new labels (minimum supported version WordPress 5.8)
176 *
177 * @return array Array of new labels for Reusable Block post type.
178 */
179 function gutenberg_override_reusable_block_post_type_labels() {
180 return array(
181 'name' => _x( 'Reusable blocks', 'post type general name', 'gutenberg' ),
182 'singular_name' => _x( 'Reusable block', 'post type singular name', 'gutenberg' ),
183 'menu_name' => _x( 'Reusable blocks', 'admin menu', 'gutenberg' ),
184 'name_admin_bar' => _x( 'Reusable block', 'add new on admin bar', 'gutenberg' ),
185 'add_new' => _x( 'Add New', 'Reusable block', 'gutenberg' ),
186 'add_new_item' => __( 'Add new Reusable block', 'gutenberg' ),
187 'new_item' => __( 'New Reusable block', 'gutenberg' ),
188 'edit_item' => __( 'Edit Reusable block', 'gutenberg' ),
189 'view_item' => __( 'View Reusable block', 'gutenberg' ),
190 'all_items' => __( 'All Reusable blocks', 'gutenberg' ),
191 'search_items' => __( 'Search Reusable blocks', 'gutenberg' ),
192 'not_found' => __( 'No reusable blocks found.', 'gutenberg' ),
193 'not_found_in_trash' => __( 'No reusable blocks found in Trash.', 'gutenberg' ),
194 'filter_items_list' => __( 'Filter reusable blocks list', 'gutenberg' ),
195 'items_list_navigation' => __( 'Reusable blocks list navigation', 'gutenberg' ),
196 'items_list' => __( 'Reusable blocks list', 'gutenberg' ),
197 'item_published' => __( 'Reusable block published.', 'gutenberg' ),
198 'item_published_privately' => __( 'Reusable block published privately.', 'gutenberg' ),
199 'item_reverted_to_draft' => __( 'Reusable block reverted to draft.', 'gutenberg' ),
200 'item_scheduled' => __( 'Reusable block scheduled.', 'gutenberg' ),
201 'item_updated' => __( 'Reusable block updated.', 'gutenberg' ),
202 );
203 }
204 add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 );
205
206 /**
207 * Update allowed inline style attributes list.
208 *
209 * Note: This should be removed when the minimum required WP version is >= 5.8.
210 *
211 * @param string[] $attrs Array of allowed CSS attributes.
212 * @return string[] CSS attributes.
213 */
214 function gutenberg_safe_style_attrs( $attrs ) {
215 $attrs[] = 'object-position';
216
217 return $attrs;
218 }
219 add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs' );
220