PluginProbe
Gutenberg / 10.0.0
Gutenberg v10.0.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 10.0.0, at lib/compat.php

270 lines 10.4 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 * Adds a wp.date.setSettings with timezone abbr parameter
13 *
14 * This can be removed when plugin support requires WordPress 5.6.0+.
15 *
16 * The script registration occurs in core wp-includes/script-loader.php
17 * wp_default_packages_inline_scripts()
18 *
19 * @since 8.6.0
20 *
21 * @param WP_Scripts $scripts WP_Scripts object.
22 */
23 function gutenberg_add_date_settings_timezone( $scripts ) {
24 if ( ! did_action( 'init' ) ) {
25 return;
26 }
27
28 global $wp_locale;
29
30 // Calculate the timezone abbr (EDT, PST) if possible.
31 $timezone_string = get_option( 'timezone_string', 'UTC' );
32 $timezone_abbr = '';
33
34 if ( ! empty( $timezone_string ) ) {
35 $timezone_date = new DateTime( null, new DateTimeZone( $timezone_string ) );
36 $timezone_abbr = $timezone_date->format( 'T' );
37 }
38
39 $scripts->add_inline_script(
40 'wp-date',
41 sprintf(
42 'wp.date.setSettings( %s );',
43 wp_json_encode(
44 array(
45 'l10n' => array(
46 'locale' => get_user_locale(),
47 'months' => array_values( $wp_locale->month ),
48 'monthsShort' => array_values( $wp_locale->month_abbrev ),
49 'weekdays' => array_values( $wp_locale->weekday ),
50 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ),
51 'meridiem' => (object) $wp_locale->meridiem,
52 'relative' => array(
53 /* translators: %s: Duration. */
54 'future' => __( '%s from now', 'default' ),
55 /* translators: %s: Duration. */
56 'past' => __( '%s ago', 'default' ),
57 ),
58 ),
59 'formats' => array(
60 /* translators: Time format, see https://www.php.net/date */
61 'time' => get_option( 'time_format', __( 'g:i a', 'default' ) ),
62 /* translators: Date format, see https://www.php.net/date */
63 'date' => get_option( 'date_format', __( 'F j, Y', 'default' ) ),
64 /* translators: Date/Time format, see https://www.php.net/date */
65 'datetime' => __( 'F j, Y g:i a', 'default' ),
66 /* translators: Abbreviated date/time format, see https://www.php.net/date */
67 'datetimeAbbreviated' => __( 'M j, Y g:i a', 'default' ),
68 ),
69 'timezone' => array(
70 'offset' => get_option( 'gmt_offset', 0 ),
71 'string' => $timezone_string,
72 'abbr' => $timezone_abbr,
73 ),
74 )
75 )
76 ),
77 'after'
78 );
79 }
80 add_action( 'wp_default_scripts', 'gutenberg_add_date_settings_timezone', 20 );
81
82 /**
83 * Determine if the current theme needs to load separate block styles or not.
84 *
85 * @return bool
86 */
87 function gutenberg_should_load_separate_block_styles() {
88 $load_separate_styles = gutenberg_is_fse_theme();
89 /**
90 * Determine if separate styles will be loaded for blocks on-render or not.
91 *
92 * @param bool $load_separate_styles Whether separate styles will be loaded or not.
93 *
94 * @return bool
95 */
96 return apply_filters( 'load_separate_block_styles', $load_separate_styles );
97 }
98
99 /**
100 * Remove the `wp_enqueue_registered_block_scripts_and_styles` hook if needed.
101 *
102 * @return void
103 */
104 function gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles() {
105 if ( gutenberg_should_load_separate_block_styles() ) {
106 /**
107 * Avoid enqueueing block assets of all registered blocks for all posts, instead
108 * deferring to block render mechanics to enqueue scripts, thereby ensuring only
109 * blocks of the content have their assets enqueued.
110 *
111 * This can be removed once minimum support for the plugin is outside the range
112 * of the version associated with closure of the following ticket.
113 *
114 * @see https://core.trac.wordpress.org/ticket/50328
115 *
116 * @see WP_Block::render
117 */
118 remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
119 }
120 }
121
122 add_action( 'init', 'gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles' );
123
124 /**
125 * Callback hooked to the register_block_type_args filter.
126 *
127 * This hooks into block registration to inject the default context into the block object.
128 * It can be removed once the default context is added into Core.
129 *
130 * @param array $args Block attributes.
131 * @return array Block attributes.
132 */
133 function gutenberg_inject_default_block_context( $args ) {
134 if ( is_callable( $args['render_callback'] ) ) {
135 $block_render_callback = $args['render_callback'];
136 $args['render_callback'] = function( $attributes, $content, $block = null ) use ( $block_render_callback ) {
137 global $post, $wp_query;
138
139 // Check for null for back compatibility with WP_Block_Type->render
140 // which is unused since the introduction of WP_Block class.
141 //
142 // See:
143 // - https://core.trac.wordpress.org/ticket/49927
144 // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop.
145
146 if ( null === $block ) {
147 return $block_render_callback( $attributes, $content );
148 }
149
150 $registry = WP_Block_Type_Registry::get_instance();
151 $block_type = $registry->get_registered( $block->name );
152
153 // For WordPress versions that don't support the context API.
154 if ( ! $block->context ) {
155 $block->context = array();
156 }
157
158 // Inject the post context if not done by Core.
159 $needs_post_id = ! empty( $block_type->uses_context ) && in_array( 'postId', $block_type->uses_context, true );
160 if ( $post instanceof WP_Post && $needs_post_id && ! isset( $block->context['postId'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) {
161 $block->context['postId'] = $post->ID;
162 }
163 $needs_post_type = ! empty( $block_type->uses_context ) && in_array( 'postType', $block_type->uses_context, true );
164 if ( $post instanceof WP_Post && $needs_post_type && ! isset( $block->context['postType'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) {
165 /*
166 * The `postType` context is largely unnecessary server-side, since the
167 * ID is usually sufficient on its own. That being said, since a block's
168 * manifest is expected to be shared between the server and the client,
169 * it should be included to consistently fulfill the expectation.
170 */
171 $block->context['postType'] = $post->post_type;
172 }
173
174 // Inject the query context if not done by Core.
175 $needs_query = ! empty( $block_type->uses_context ) && in_array( 'query', $block_type->uses_context, true );
176 if ( ! isset( $block->context['query'] ) && $needs_query ) {
177 if ( isset( $wp_query->tax_query->queried_terms['category'] ) ) {
178 $block->context['query'] = array( 'categoryIds' => array() );
179
180 foreach ( $wp_query->tax_query->queried_terms['category']['terms'] as $category_slug_or_id ) {
181 $block->context['query']['categoryIds'][] = 'slug' === $wp_query->tax_query->queried_terms['category']['field'] ? get_cat_ID( $category_slug_or_id ) : $category_slug_or_id;
182 }
183 }
184
185 if ( isset( $wp_query->tax_query->queried_terms['post_tag'] ) ) {
186 if ( isset( $block->context['query'] ) ) {
187 $block->context['query']['tagIds'] = array();
188 } else {
189 $block->context['query'] = array( 'tagIds' => array() );
190 }
191
192 foreach ( $wp_query->tax_query->queried_terms['post_tag']['terms'] as $tag_slug_or_id ) {
193 $tag_ID = $tag_slug_or_id;
194
195 if ( 'slug' === $wp_query->tax_query->queried_terms['post_tag']['field'] ) {
196 $tag = get_term_by( 'slug', $tag_slug_or_id, 'post_tag' );
197
198 if ( $tag ) {
199 $tag_ID = $tag->term_id;
200 }
201 }
202 $block->context['query']['tagIds'][] = $tag_ID;
203 }
204 }
205 }
206
207 return $block_render_callback( $attributes, $content, $block );
208 };
209 }
210 return $args;
211 }
212
213 add_filter( 'register_block_type_args', 'gutenberg_inject_default_block_context' );
214
215 /**
216 * Amends the paths to preload when initializing edit post.
217 *
218 * @see https://core.trac.wordpress.org/ticket/50606
219 *
220 * @since 8.4.0
221 *
222 * @param array $preload_paths Default path list that will be preloaded.
223 * @return array Modified path list to preload.
224 */
225 function gutenberg_preload_edit_post( $preload_paths ) {
226 $additional_paths = array( '/?context=edit' );
227 return array_merge( $preload_paths, $additional_paths );
228 }
229
230 add_filter( 'block_editor_preload_paths', 'gutenberg_preload_edit_post' );
231
232 /**
233 * Override post type labels for Reusable Block custom post type.
234 *
235 * This shim can be removed when the Gutenberg plugin requires a WordPress
236 * version that has the ticket below.
237 *
238 * @see https://core.trac.wordpress.org/ticket/50755
239 *
240 * @since 8.6.0
241 *
242 * @return array Array of new labels for Reusable Block post type.
243 */
244 function gutenberg_override_reusable_block_post_type_labels() {
245 return array(
246 'name' => _x( 'Reusable Blocks', 'post type general name', 'gutenberg' ),
247 'singular_name' => _x( 'Reusable Block', 'post type singular name', 'gutenberg' ),
248 'menu_name' => _x( 'Reusable Blocks', 'admin menu', 'gutenberg' ),
249 'name_admin_bar' => _x( 'Reusable Block', 'add new on admin bar', 'gutenberg' ),
250 'add_new' => _x( 'Add New', 'Reusable Block', 'gutenberg' ),
251 'add_new_item' => __( 'Add New Reusable Block', 'gutenberg' ),
252 'new_item' => __( 'New Reusable Block', 'gutenberg' ),
253 'edit_item' => __( 'Edit Reusable Block', 'gutenberg' ),
254 'view_item' => __( 'View Reusable Block', 'gutenberg' ),
255 'all_items' => __( 'All Reusable Blocks', 'gutenberg' ),
256 'search_items' => __( 'Search Reusable Blocks', 'gutenberg' ),
257 'not_found' => __( 'No reusable blocks found.', 'gutenberg' ),
258 'not_found_in_trash' => __( 'No reusable blocks found in Trash.', 'gutenberg' ),
259 'filter_items_list' => __( 'Filter reusable blocks list', 'gutenberg' ),
260 'items_list_navigation' => __( 'Reusable Blocks list navigation', 'gutenberg' ),
261 'items_list' => __( 'Reusable Blocks list', 'gutenberg' ),
262 'item_published' => __( 'Reusable Block published.', 'gutenberg' ),
263 'item_published_privately' => __( 'Reusable Block published privately.', 'gutenberg' ),
264 'item_reverted_to_draft' => __( 'Reusable Block reverted to draft.', 'gutenberg' ),
265 'item_scheduled' => __( 'Reusable Block scheduled.', 'gutenberg' ),
266 'item_updated' => __( 'Reusable Block updated.', 'gutenberg' ),
267 );
268 }
269 add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 );
270