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

116 lines 3.5 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 polyfill for the WHATWG URL in environments which do not support it.
13 * The intention in how this action is handled is under the assumption that this
14 * code would eventually be placed at `wp_default_packages_vendor`, which is
15 * called as a result of `wp_default_packages` via the `wp_default_scripts`.
16 *
17 * This can be removed when plugin support requires WordPress 5.4.0+.
18 *
19 * @see https://core.trac.wordpress.org/ticket/49360
20 * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
21 * @see https://developer.wordpress.org/reference/functions/wp_default_packages_vendor/
22 *
23 * @since 7.3.0
24 *
25 * @param WP_Scripts $scripts WP_Scripts object.
26 */
27 function gutenberg_add_url_polyfill( $scripts ) {
28 // Only register polyfill if not already registered. This prevents handling
29 // in an environment where core has updated to manage the polyfill. This
30 // depends on the action being handled after default script registration.
31 $is_polyfill_script_registered = (bool) $scripts->query( 'wp-polyfill-url', 'registered' );
32 if ( $is_polyfill_script_registered ) {
33 return;
34 }
35
36 gutenberg_register_vendor_script(
37 $scripts,
38 'wp-polyfill-url',
39 'https://unpkg.com/polyfill-library@3.42.0/polyfills/URL/polyfill.js',
40 array(),
41 '3.42.0'
42 );
43
44 did_action( 'init' ) && $scripts->add_inline_script(
45 'wp-polyfill',
46 wp_get_script_polyfill(
47 $scripts,
48 array(
49 '\'URL\' in window' => 'wp-polyfill-url',
50 )
51 )
52 );
53 }
54 add_action( 'wp_default_scripts', 'gutenberg_add_url_polyfill', 20 );
55
56 /**
57 * Adds a polyfill for DOMRect in environments which do not support it.
58 *
59 * This can be removed when plugin support requires WordPress 5.4.0+.
60 *
61 * @see gutenberg_add_url_polyfill
62 * @see https://core.trac.wordpress.org/ticket/49360
63 * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMRect
64 * @see https://developer.wordpress.org/reference/functions/wp_default_packages_vendor/
65 *
66 * @since 7.5.0
67 *
68 * @param WP_Scripts $scripts WP_Scripts object.
69 */
70 function gutenberg_add_dom_rect_polyfill( $scripts ) {
71 // Only register polyfill if not already registered. This prevents handling
72 // in an environment where core has updated to manage the polyfill. This
73 // depends on the action being handled after default script registration.
74 $is_polyfill_script_registered = (bool) $scripts->query( 'wp-polyfill-dom-rect', 'registered' );
75 if ( $is_polyfill_script_registered ) {
76 return;
77 }
78
79 gutenberg_register_vendor_script(
80 $scripts,
81 'wp-polyfill-dom-rect',
82 'https://unpkg.com/polyfill-library@3.42.0/polyfills/DOMRect/polyfill.js',
83 array(),
84 '3.42.0'
85 );
86
87 did_action( 'init' ) && $scripts->add_inline_script(
88 'wp-polyfill',
89 wp_get_script_polyfill(
90 $scripts,
91 array(
92 'window.DOMRect' => 'wp-polyfill-dom-rect',
93 )
94 )
95 );
96 }
97 add_action( 'wp_default_scripts', 'gutenberg_add_dom_rect_polyfill', 20 );
98
99 /**
100 * Sets the current post for usage in template blocks.
101 *
102 * @return WP_Post|null The post if any, or null otherwise.
103 */
104 function gutenberg_get_post_from_context() {
105 // TODO: Without this temporary fix, an infinite loop can occur where
106 // posts with post content blocks render themselves recursively.
107 if ( is_admin() || defined( 'REST_REQUEST' ) ) {
108 return null;
109 }
110 if ( ! in_the_loop() ) {
111 rewind_posts();
112 the_post();
113 }
114 return get_post();
115 }
116