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