PluginProbe
Gutenberg / 11.5.0
Gutenberg v11.5.0
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 11.5.0, at lib/compat.php

193 lines 7.6 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 * Backporting wp_should_load_separate_core_block_assets from WP-Core.
13 *
14 * @todo Remove this function when the minimum supported version is WordPress 5.8.
15 */
16 if ( ! function_exists( 'wp_should_load_separate_core_block_assets' ) ) {
17 /**
18 * Checks whether separate assets should be loaded for core blocks on-render.
19 *
20 * @since 5.8.0
21 *
22 * @return bool Whether separate assets will be loaded.
23 */
24 function wp_should_load_separate_core_block_assets() {
25 if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
26 return false;
27 }
28
29 /**
30 * Filters the flag that decides whether separate scripts and styles
31 * will be loaded for core blocks on-render.
32 *
33 * @since 5.8.0
34 *
35 * @param bool $load_separate_assets Whether separate assets will be loaded.
36 * Default false.
37 */
38 return apply_filters( 'should_load_separate_core_block_assets', false );
39 }
40 }
41
42 /**
43 * Opt-in to separate styles loading for block themes in WordPress 5.8.
44 *
45 * @todo Remove this function when the minimum supported version is WordPress 5.8.
46 */
47 add_filter(
48 'separate_core_block_assets',
49 function( $load_separate_styles ) {
50 if ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) {
51 return true;
52 }
53 return $load_separate_styles;
54 }
55 );
56
57 /**
58 * Remove the `wp_enqueue_registered_block_scripts_and_styles` hook if needed.
59 *
60 * @return void
61 */
62 function gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles() {
63 if ( wp_should_load_separate_core_block_assets() ) {
64 /**
65 * Avoid enqueueing block assets of all registered blocks for all posts, instead
66 * deferring to block render mechanics to enqueue scripts, thereby ensuring only
67 * blocks of the content have their assets enqueued.
68 *
69 * This can be removed once minimum support for the plugin is outside the range
70 * of the version associated with closure of the following ticket.
71 *
72 * @see https://core.trac.wordpress.org/ticket/50328
73 *
74 * @see WP_Block::render
75 */
76 remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
77 }
78 }
79
80 add_action( 'init', 'gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles' );
81
82 /**
83 * Callback hooked to the register_block_type_args filter.
84 *
85 * This hooks into block registration to inject the default context into the block object.
86 * It can be removed once the default context is added into Core.
87 *
88 * @param array $args Block attributes.
89 * @return array Block attributes.
90 */
91 function gutenberg_inject_default_block_context( $args ) {
92 if ( is_callable( $args['render_callback'] ) ) {
93 $block_render_callback = $args['render_callback'];
94 $args['render_callback'] = function( $attributes, $content, $block = null ) use ( $block_render_callback ) {
95 global $post;
96
97 // Check for null for back compatibility with WP_Block_Type->render
98 // which is unused since the introduction of WP_Block class.
99 //
100 // See:
101 // - https://core.trac.wordpress.org/ticket/49927
102 // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop.
103
104 if ( null === $block ) {
105 return $block_render_callback( $attributes, $content );
106 }
107
108 $registry = WP_Block_Type_Registry::get_instance();
109 $block_type = $registry->get_registered( $block->name );
110
111 // For WordPress versions that don't support the context API.
112 if ( ! $block->context ) {
113 $block->context = array();
114 }
115
116 // Inject the post context if not done by Core.
117 $needs_post_id = ! empty( $block_type->uses_context ) && in_array( 'postId', $block_type->uses_context, true );
118 if ( $post instanceof WP_Post && $needs_post_id && ! isset( $block->context['postId'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) {
119 $block->context['postId'] = $post->ID;
120 }
121 $needs_post_type = ! empty( $block_type->uses_context ) && in_array( 'postType', $block_type->uses_context, true );
122 if ( $post instanceof WP_Post && $needs_post_type && ! isset( $block->context['postType'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) {
123 /*
124 * The `postType` context is largely unnecessary server-side, since the
125 * ID is usually sufficient on its own. That being said, since a block's
126 * manifest is expected to be shared between the server and the client,
127 * it should be included to consistently fulfill the expectation.
128 */
129 $block->context['postType'] = $post->post_type;
130 }
131
132 return $block_render_callback( $attributes, $content, $block );
133 };
134 }
135 return $args;
136 }
137
138 add_filter( 'register_block_type_args', 'gutenberg_inject_default_block_context' );
139
140 /**
141 * Override post type labels for Reusable Block custom post type.
142 * The labels are different from the ones in Core.
143 *
144 * Remove this when Core receives the new labels (minimum supported version WordPress 5.8)
145 *
146 * @return array Array of new labels for Reusable Block post type.
147 */
148 function gutenberg_override_reusable_block_post_type_labels() {
149 return array(
150 'name' => _x( 'Reusable blocks', 'post type general name', 'gutenberg' ),
151 'singular_name' => _x( 'Reusable block', 'post type singular name', 'gutenberg' ),
152 'menu_name' => _x( 'Reusable blocks', 'admin menu', 'gutenberg' ),
153 'name_admin_bar' => _x( 'Reusable block', 'add new on admin bar', 'gutenberg' ),
154 'add_new' => _x( 'Add New', 'Reusable block', 'gutenberg' ),
155 'add_new_item' => __( 'Add new Reusable block', 'gutenberg' ),
156 'new_item' => __( 'New Reusable block', 'gutenberg' ),
157 'edit_item' => __( 'Edit Reusable block', 'gutenberg' ),
158 'view_item' => __( 'View Reusable block', 'gutenberg' ),
159 'all_items' => __( 'All Reusable blocks', 'gutenberg' ),
160 'search_items' => __( 'Search Reusable blocks', 'gutenberg' ),
161 'not_found' => __( 'No reusable blocks found.', 'gutenberg' ),
162 'not_found_in_trash' => __( 'No reusable blocks found in Trash.', 'gutenberg' ),
163 'filter_items_list' => __( 'Filter reusable blocks list', 'gutenberg' ),
164 'items_list_navigation' => __( 'Reusable blocks list navigation', 'gutenberg' ),
165 'items_list' => __( 'Reusable blocks list', 'gutenberg' ),
166 'item_published' => __( 'Reusable block published.', 'gutenberg' ),
167 'item_published_privately' => __( 'Reusable block published privately.', 'gutenberg' ),
168 'item_reverted_to_draft' => __( 'Reusable block reverted to draft.', 'gutenberg' ),
169 'item_scheduled' => __( 'Reusable block scheduled.', 'gutenberg' ),
170 'item_updated' => __( 'Reusable block updated.', 'gutenberg' ),
171 );
172 }
173 add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 );
174
175 /**
176 * Update allowed inline style attributes list.
177 *
178 * Note: This should be removed when the minimum required WP version is >= 5.8.
179 *
180 * @param string[] $attrs Array of allowed CSS attributes.
181 * @return string[] CSS attributes.
182 */
183 function gutenberg_safe_style_attrs( $attrs ) {
184 $attrs[] = 'object-position';
185 $attrs[] = 'border-top-left-radius';
186 $attrs[] = 'border-top-right-radius';
187 $attrs[] = 'border-bottom-right-radius';
188 $attrs[] = 'border-bottom-left-radius';
189
190 return $attrs;
191 }
192 add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs' );
193