PluginProbe
Gutenberg / 7.9.0
Gutenberg v7.9.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 7.9.0, at lib/compat.php

164 lines 5.2 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 if ( ! function_exists( 'register_block_type_from_metadata' ) ) {
12 /**
13 * Registers a block type from metadata stored in the `block.json` file.
14 *
15 * @since 7.9.0
16 *
17 * @param string $path Path to the folder where the `block.json` file is located.
18 * @param array $args {
19 * Optional. Array of block type arguments. Any arguments may be defined, however the
20 * ones described below are supported by default. Default empty array.
21 *
22 * @type callable $render_callback Callback used to render blocks of this block type.
23 * }
24 * @return WP_Block_Type|false The registered block type on success, or false on failure.
25 */
26 function register_block_type_from_metadata( $path, $args = array() ) {
27 $file = trailingslashit( $path ) . 'block.json';
28 if ( ! file_exists( $file ) ) {
29 return false;
30 }
31
32 $metadata = json_decode( file_get_contents( $file ), true );
33 if ( ! is_array( $metadata ) ) {
34 return false;
35 }
36
37 return register_block_type(
38 $metadata['name'],
39 array_merge(
40 $metadata,
41 $args
42 )
43 );
44 }
45 }
46
47 /**
48 * Extends block editor settings to include a list of image dimensions per size.
49 *
50 * This can be removed when plugin support requires WordPress 5.4.0+.
51 *
52 * @see https://core.trac.wordpress.org/ticket/49389
53 * @see https://core.trac.wordpress.org/changeset/47240
54 *
55 * @param array $settings Default editor settings.
56 *
57 * @return array Filtered editor settings.
58 */
59 function gutenberg_extend_settings_image_dimensions( $settings ) {
60 /*
61 * Only filter settings if:
62 * 1. `imageDimensions` is not already assigned, in which case it can be
63 * assumed to have been set from WordPress 5.4.0+ default settings.
64 * 2. `imageSizes` is an array. Plugins may run `block_editor_settings`
65 * directly and not provide all properties of the settings array.
66 */
67 if ( ! isset( $settings['imageDimensions'] ) && ! empty( $settings['imageSizes'] ) ) {
68 $image_dimensions = array();
69 $all_sizes = wp_get_registered_image_subsizes();
70 foreach ( $settings['imageSizes'] as $size ) {
71 $key = $size['slug'];
72 if ( isset( $all_sizes[ $key ] ) ) {
73 $image_dimensions[ $key ] = $all_sizes[ $key ];
74 }
75 }
76 $settings['imageDimensions'] = $image_dimensions;
77 }
78
79 return $settings;
80 }
81 add_filter( 'block_editor_settings', 'gutenberg_extend_settings_image_dimensions' );
82
83 /**
84 * Adds a polyfill for the WHATWG URL in environments which do not support it.
85 * The intention in how this action is handled is under the assumption that this
86 * code would eventually be placed at `wp_default_packages_vendor`, which is
87 * called as a result of `wp_default_packages` via the `wp_default_scripts`.
88 *
89 * This can be removed when plugin support requires WordPress 5.4.0+.
90 *
91 * The script registration occurs in `gutenberg_register_vendor_scripts`, which
92 * should be removed in coordination with this function.
93 *
94 * @see gutenberg_register_vendor_scripts
95 * @see https://core.trac.wordpress.org/ticket/49360
96 * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
97 * @see https://developer.wordpress.org/reference/functions/wp_default_packages_vendor/
98 *
99 * @since 7.3.0
100 *
101 * @param WP_Scripts $scripts WP_Scripts object.
102 */
103 function gutenberg_add_url_polyfill( $scripts ) {
104 did_action( 'init' ) && $scripts->add_inline_script(
105 'wp-polyfill',
106 wp_get_script_polyfill(
107 $scripts,
108 array(
109 'window.URL && window.URL.prototype && window.URLSearchParams' => 'wp-polyfill-url',
110 )
111 )
112 );
113 }
114 add_action( 'wp_default_scripts', 'gutenberg_add_url_polyfill', 20 );
115
116 /**
117 * Adds a polyfill for DOMRect in environments which do not support it.
118 *
119 * This can be removed when plugin support requires WordPress 5.4.0+.
120 *
121 * The script registration occurs in `gutenberg_register_vendor_scripts`, which
122 * should be removed in coordination with this function.
123 *
124 * @see gutenberg_register_vendor_scripts
125 * @see gutenberg_add_url_polyfill
126 * @see https://core.trac.wordpress.org/ticket/49360
127 * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMRect
128 * @see https://developer.wordpress.org/reference/functions/wp_default_packages_vendor/
129 *
130 * @since 7.5.0
131 *
132 * @param WP_Scripts $scripts WP_Scripts object.
133 */
134 function gutenberg_add_dom_rect_polyfill( $scripts ) {
135 did_action( 'init' ) && $scripts->add_inline_script(
136 'wp-polyfill',
137 wp_get_script_polyfill(
138 $scripts,
139 array(
140 'window.DOMRect' => 'wp-polyfill-dom-rect',
141 )
142 )
143 );
144 }
145 add_action( 'wp_default_scripts', 'gutenberg_add_dom_rect_polyfill', 20 );
146
147 /**
148 * Sets the current post for usage in template blocks.
149 *
150 * @return WP_Post|null The post if any, or null otherwise.
151 */
152 function gutenberg_get_post_from_context() {
153 // TODO: Without this temporary fix, an infinite loop can occur where
154 // posts with post content blocks render themselves recursively.
155 if ( is_admin() || defined( 'REST_REQUEST' ) ) {
156 return null;
157 }
158 if ( ! in_the_loop() ) {
159 rewind_posts();
160 the_post();
161 }
162 return get_post();
163 }
164