| 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 determine whether to use drop cap feature. |
| 49 |
* |
| 50 |
* @param array $settings Default editor settings. |
| 51 |
* |
| 52 |
* @return array Filtered editor settings. |
| 53 |
*/ |
| 54 |
function gutenberg_extend_settings_drop_cap( $settings ) { |
| 55 |
$settings['__experimentalDisableDropCap'] = false; |
| 56 |
return $settings; |
| 57 |
} |
| 58 |
add_filter( 'block_editor_settings', 'gutenberg_extend_settings_drop_cap' ); |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Extends block editor settings to include a list of image dimensions per size. |
| 63 |
* |
| 64 |
* This can be removed when plugin support requires WordPress 5.4.0+. |
| 65 |
* |
| 66 |
* @see https://core.trac.wordpress.org/ticket/49389 |
| 67 |
* @see https://core.trac.wordpress.org/changeset/47240 |
| 68 |
* |
| 69 |
* @param array $settings Default editor settings. |
| 70 |
* |
| 71 |
* @return array Filtered editor settings. |
| 72 |
*/ |
| 73 |
function gutenberg_extend_settings_image_dimensions( $settings ) { |
| 74 |
/* |
| 75 |
* Only filter settings if: |
| 76 |
* 1. `imageDimensions` is not already assigned, in which case it can be |
| 77 |
* assumed to have been set from WordPress 5.4.0+ default settings. |
| 78 |
* 2. `imageSizes` is an array. Plugins may run `block_editor_settings` |
| 79 |
* directly and not provide all properties of the settings array. |
| 80 |
*/ |
| 81 |
if ( ! isset( $settings['imageDimensions'] ) && ! empty( $settings['imageSizes'] ) ) { |
| 82 |
$image_dimensions = array(); |
| 83 |
$all_sizes = wp_get_registered_image_subsizes(); |
| 84 |
foreach ( $settings['imageSizes'] as $size ) { |
| 85 |
$key = $size['slug']; |
| 86 |
if ( isset( $all_sizes[ $key ] ) ) { |
| 87 |
$image_dimensions[ $key ] = $all_sizes[ $key ]; |
| 88 |
} |
| 89 |
} |
| 90 |
$settings['imageDimensions'] = $image_dimensions; |
| 91 |
} |
| 92 |
|
| 93 |
return $settings; |
| 94 |
} |
| 95 |
add_filter( 'block_editor_settings', 'gutenberg_extend_settings_image_dimensions' ); |
| 96 |
|
| 97 |
/** |
| 98 |
* Adds a polyfill for the WHATWG URL in environments which do not support it. |
| 99 |
* The intention in how this action is handled is under the assumption that this |
| 100 |
* code would eventually be placed at `wp_default_packages_vendor`, which is |
| 101 |
* called as a result of `wp_default_packages` via the `wp_default_scripts`. |
| 102 |
* |
| 103 |
* This can be removed when plugin support requires WordPress 5.4.0+. |
| 104 |
* |
| 105 |
* The script registration occurs in `gutenberg_register_vendor_scripts`, which |
| 106 |
* should be removed in coordination with this function. |
| 107 |
* |
| 108 |
* @see gutenberg_register_vendor_scripts |
| 109 |
* @see https://core.trac.wordpress.org/ticket/49360 |
| 110 |
* @see https://developer.mozilla.org/en-US/docs/Web/API/URL/URL |
| 111 |
* @see https://developer.wordpress.org/reference/functions/wp_default_packages_vendor/ |
| 112 |
* |
| 113 |
* @since 7.3.0 |
| 114 |
* |
| 115 |
* @param WP_Scripts $scripts WP_Scripts object. |
| 116 |
*/ |
| 117 |
function gutenberg_add_url_polyfill( $scripts ) { |
| 118 |
did_action( 'init' ) && $scripts->add_inline_script( |
| 119 |
'wp-polyfill', |
| 120 |
wp_get_script_polyfill( |
| 121 |
$scripts, |
| 122 |
array( |
| 123 |
'window.URL && window.URL.prototype && window.URLSearchParams' => 'wp-polyfill-url', |
| 124 |
) |
| 125 |
) |
| 126 |
); |
| 127 |
} |
| 128 |
add_action( 'wp_default_scripts', 'gutenberg_add_url_polyfill', 20 ); |
| 129 |
|
| 130 |
/** |
| 131 |
* Adds a polyfill for DOMRect in environments which do not support it. |
| 132 |
* |
| 133 |
* This can be removed when plugin support requires WordPress 5.4.0+. |
| 134 |
* |
| 135 |
* The script registration occurs in `gutenberg_register_vendor_scripts`, which |
| 136 |
* should be removed in coordination with this function. |
| 137 |
* |
| 138 |
* @see gutenberg_register_vendor_scripts |
| 139 |
* @see gutenberg_add_url_polyfill |
| 140 |
* @see https://core.trac.wordpress.org/ticket/49360 |
| 141 |
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMRect |
| 142 |
* @see https://developer.wordpress.org/reference/functions/wp_default_packages_vendor/ |
| 143 |
* |
| 144 |
* @since 7.5.0 |
| 145 |
* |
| 146 |
* @param WP_Scripts $scripts WP_Scripts object. |
| 147 |
*/ |
| 148 |
function gutenberg_add_dom_rect_polyfill( $scripts ) { |
| 149 |
did_action( 'init' ) && $scripts->add_inline_script( |
| 150 |
'wp-polyfill', |
| 151 |
wp_get_script_polyfill( |
| 152 |
$scripts, |
| 153 |
array( |
| 154 |
'window.DOMRect' => 'wp-polyfill-dom-rect', |
| 155 |
) |
| 156 |
) |
| 157 |
); |
| 158 |
} |
| 159 |
add_action( 'wp_default_scripts', 'gutenberg_add_dom_rect_polyfill', 20 ); |
| 160 |
|
| 161 |
/** |
| 162 |
* Sets the current post for usage in template blocks. |
| 163 |
* |
| 164 |
* @return WP_Post|null The post if any, or null otherwise. |
| 165 |
*/ |
| 166 |
function gutenberg_get_post_from_context() { |
| 167 |
// TODO: Without this temporary fix, an infinite loop can occur where |
| 168 |
// posts with post content blocks render themselves recursively. |
| 169 |
if ( is_admin() || defined( 'REST_REQUEST' ) ) { |
| 170 |
return null; |
| 171 |
} |
| 172 |
|
| 173 |
_deprecated_function( __FUNCTION__, '8.1.0' ); |
| 174 |
|
| 175 |
if ( ! in_the_loop() ) { |
| 176 |
rewind_posts(); |
| 177 |
the_post(); |
| 178 |
} |
| 179 |
return get_post(); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Shim that hooks into `pre_render_block` so as to override `render_block` with |
| 184 |
* a function that assigns block context. |
| 185 |
* |
| 186 |
* This can be removed when plugin support requires WordPress 5.5.0+. |
| 187 |
* |
| 188 |
* @see https://core.trac.wordpress.org/ticket/49927 |
| 189 |
* |
| 190 |
* @param string|null $pre_render The pre-rendered content. Defaults to null. |
| 191 |
* @param array $parsed_block The parsed block being rendered. |
| 192 |
* |
| 193 |
* @return string String of rendered HTML. |
| 194 |
*/ |
| 195 |
function gutenberg_render_block_with_assigned_block_context( $pre_render, $parsed_block ) { |
| 196 |
global $post, $wp_query; |
| 197 |
|
| 198 |
/* |
| 199 |
* If a non-null value is provided, a filter has run at an earlier priority |
| 200 |
* and has already handled custom rendering and should take precedence. |
| 201 |
*/ |
| 202 |
if ( null !== $pre_render ) { |
| 203 |
return $pre_render; |
| 204 |
} |
| 205 |
|
| 206 |
$source_block = $parsed_block; |
| 207 |
|
| 208 |
/** This filter is documented in src/wp-includes/blocks.php */ |
| 209 |
$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block ); |
| 210 |
|
| 211 |
$context = array( |
| 212 |
'postId' => $post->ID, |
| 213 |
|
| 214 |
/* |
| 215 |
* The `postType` context is largely unnecessary server-side, since the |
| 216 |
* ID is usually sufficient on its own. That being said, since a block's |
| 217 |
* manifest is expected to be shared between the server and the client, |
| 218 |
* it should be included to consistently fulfill the expectation. |
| 219 |
*/ |
| 220 |
'postType' => $post->post_type, |
| 221 |
|
| 222 |
'query' => array( 'categoryIds' => array() ), |
| 223 |
); |
| 224 |
|
| 225 |
if ( isset( $wp_query->tax_query->queried_terms['category']['terms'] ) ) { |
| 226 |
foreach ( $wp_query->tax_query->queried_terms['category']['terms'] as $category_id ) { |
| 227 |
$context['query']['categoryIds'][] = $category_id; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Filters the default context provided to a rendered block. |
| 233 |
* |
| 234 |
* @param array $context Default context. |
| 235 |
* @param array $parsed_block Block being rendered, filtered by `render_block_data`. |
| 236 |
*/ |
| 237 |
$context = apply_filters( 'render_block_context', $context, $parsed_block ); |
| 238 |
|
| 239 |
$block = new WP_Block( $parsed_block, $context ); |
| 240 |
|
| 241 |
return $block->render(); |
| 242 |
} |
| 243 |
add_filter( 'pre_render_block', 'gutenberg_render_block_with_assigned_block_context', 9, 2 ); |
| 244 |
|