| @@ -87,28 +87,19 @@ | ||
| 87 | 87 | /* |
| 88 | 88 | * `WP_Dependencies::set_translations` will fall over on itself if setting |
| 89 | 89 | * translations on the `wp-i18n` handle, since it internally adds `wp-i18n` |
| 90 | 90 | * as a dependency of itself, exhausting memory. The same applies for the |
| 91 | - * polyfill and hooks scripts, which are dependencies _of_ `wp-i18n`. | |
| 91 | + * polyfill script, which is a dependency _of_ `wp-i18n`. | |
| 92 | 92 | * |
| 93 | 93 | * See: https://core.trac.wordpress.org/ticket/46089 |
| 94 | 94 | */ |
| 95 | - if ( ! in_array( $handle, array( 'wp-i18n', 'wp-polyfill', 'wp-hooks' ), true ) ) { | |
| 95 | + if ( 'wp-i18n' !== $handle && 'wp-polyfill' !== $handle ) { | |
| 96 | 96 | $scripts->set_translations( $handle, 'default' ); |
| 97 | 97 | } |
| 98 | - | |
| 99 | - /* | |
| 100 | - * Wp-editor module is exposed as window.wp.editor. | |
| 101 | - * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor. | |
| 102 | - * Solution: fuse the two objects together to maintain backward compatibility. | |
| 103 | - * For more context, see https://github.com/WordPress/gutenberg/issues/33203 | |
| 104 | - */ | |
| 105 | - if ( 'wp-editor' === $handle ) { | |
| 106 | - $scripts->add_inline_script( | |
| 107 | - 'wp-editor', | |
| 108 | - 'Object.assign( window.wp.editor, window.wp.oldEditor );', | |
| 109 | - 'after' | |
| 110 | - ); | |
| 98 | + if ( 'wp-i18n' === $handle ) { | |
| 99 | + $ltr = 'rtl' === _x( 'ltr', 'text direction', 'default' ) ? 'rtl' : 'ltr'; | |
| 100 | + $output = sprintf( "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ '%s' ] }, 'default' );", $ltr ); | |
| 101 | + $scripts->add_inline_script( 'wp-i18n', $output, 'after' ); | |
| 111 | 102 | } |
| 112 | 103 | } |
| 113 | 104 | |
| 114 | 105 | /** |
| @@ -133,9 +124,9 @@ | ||
| 133 | 124 | return $file; |
| 134 | 125 | } |
| 135 | 126 | |
| 136 | 127 | // Ignore scripts that are not found in the expected `build/` location. |
| 137 | - $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.min.js'; | |
| 128 | + $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.js'; | |
| 138 | 129 | if ( ! file_exists( $script_path ) ) { |
| 139 | 130 | return $file; |
| 140 | 131 | } |
| 141 | 132 | |
| @@ -163,8 +154,27 @@ | ||
| 163 | 154 | } |
| 164 | 155 | add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 ); |
| 165 | 156 | |
| 166 | 157 | /** |
| 158 | + * Filters the default labels for common post types to change the case style | |
| 159 | + * from capitalized (e.g. "Featured Image") to sentence-style (e.g. "Featured | |
| 160 | + * image"). | |
| 161 | + * | |
| 162 | + * See: https://github.com/WordPress/gutenberg/pull/18758 | |
| 163 | + * | |
| 164 | + * @param object $labels Object with all the labels as member variables. | |
| 165 | + * | |
| 166 | + * @return object Object with all the labels, including overridden ones. | |
| 167 | + */ | |
| 168 | +function gutenberg_override_posttype_labels( $labels ) { | |
| 169 | + $labels->featured_image = __( 'Featured image', 'gutenberg' ); | |
| 170 | + return $labels; | |
| 171 | +} | |
| 172 | +foreach ( array( 'post', 'page' ) as $post_type ) { | |
| 173 | + add_filter( "post_type_labels_{$post_type}", 'gutenberg_override_posttype_labels' ); | |
| 174 | +} | |
| 175 | + | |
| 176 | +/** | |
| 167 | 177 | * Registers a style according to `wp_register_style`. Honors this request by |
| 168 | 178 | * deregistering any style by the same handler before registration. |
| 169 | 179 | * |
| 170 | 180 | * @since 4.1.0 |
| @@ -206,18 +216,38 @@ | ||
| 206 | 216 | $react_suffix = ( SCRIPT_DEBUG ? '.development' : '.production' ) . $suffix; |
| 207 | 217 | gutenberg_register_vendor_script( |
| 208 | 218 | $scripts, |
| 209 | 219 | 'react', |
| 210 | - 'https://unpkg.com/react@17.0.1/umd/react' . $react_suffix . '.js', | |
| 211 | - // See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react. | |
| 212 | - SCRIPT_DEBUG ? array( 'wp-react-refresh-entry', 'wp-polyfill' ) : array( 'wp-polyfill' ) | |
| 220 | + 'https://unpkg.com/react@16.13.1/umd/react' . $react_suffix . '.js', | |
| 221 | + array( 'wp-polyfill' ) | |
| 213 | 222 | ); |
| 214 | 223 | gutenberg_register_vendor_script( |
| 215 | 224 | $scripts, |
| 216 | 225 | 'react-dom', |
| 217 | - 'https://unpkg.com/react-dom@17.0.1/umd/react-dom' . $react_suffix . '.js', | |
| 226 | + 'https://unpkg.com/react-dom@16.13.1/umd/react-dom' . $react_suffix . '.js', | |
| 218 | 227 | array( 'react' ) |
| 219 | 228 | ); |
| 229 | + | |
| 230 | + /* | |
| 231 | + * This script registration and the corresponding function should be removed | |
| 232 | + * removed once the plugin is updated to support WordPress 5.6.0 and newer. | |
| 233 | + */ | |
| 234 | + gutenberg_register_vendor_script( | |
| 235 | + $scripts, | |
| 236 | + 'lodash', | |
| 237 | + 'https://unpkg.com/lodash@4.17.19/lodash.js', | |
| 238 | + array(), | |
| 239 | + '4.17.19', | |
| 240 | + true | |
| 241 | + ); | |
| 242 | + | |
| 243 | + gutenberg_register_vendor_script( | |
| 244 | + $scripts, | |
| 245 | + 'object-fit-polyfill', | |
| 246 | + 'https://unpkg.com/objectFitPolyfill@2.3.0/dist/objectFitPolyfill.min.js', | |
| 247 | + array(), | |
| 248 | + '2.3.0' | |
| 249 | + ); | |
| 220 | 250 | } |
| 221 | 251 | add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' ); |
| 222 | 252 | |
| 223 | 253 | /** |
| @@ -228,24 +258,20 @@ | ||
| 228 | 258 | * |
| 229 | 259 | * @param WP_Scripts $scripts WP_Scripts instance. |
| 230 | 260 | */ |
| 231 | 261 | function gutenberg_register_packages_scripts( $scripts ) { |
| 232 | - // When in production, use the plugin's version as the default asset version; | |
| 233 | - // else (for development or test) default to use the current time. | |
| 234 | - $default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time(); | |
| 235 | - | |
| 236 | - foreach ( glob( gutenberg_dir_path() . 'build/*/index.min.js' ) as $path ) { | |
| 262 | + foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) { | |
| 237 | 263 | // Prefix `wp-` to package directory to get script handle. |
| 238 | - // For example, `…/build/a11y/index.min.js` becomes `wp-a11y`. | |
| 264 | + // For example, `…/build/a11y/index.js` becomes `wp-a11y`. | |
| 239 | 265 | $handle = 'wp-' . basename( dirname( $path ) ); |
| 240 | 266 | |
| 241 | - // Replace extension with `.asset.php` to find the generated dependencies file. | |
| 242 | - $asset_file = substr( $path, 0, -( strlen( '.js' ) ) ) . '.asset.php'; | |
| 267 | + // Replace `.js` extension with `.asset.php` to find the generated dependencies file. | |
| 268 | + $asset_file = substr( $path, 0, -3 ) . '.asset.php'; | |
| 243 | 269 | $asset = file_exists( $asset_file ) |
| 244 | 270 | ? require( $asset_file ) |
| 245 | 271 | : null; |
| 246 | 272 | $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array(); |
| 247 | - $version = isset( $asset['version'] ) ? $asset['version'] : $default_version; | |
| 273 | + $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path ); | |
| 248 | 274 | |
| 249 | 275 | // Add dependencies that cannot be detected and generated by build tools. |
| 250 | 276 | switch ( $handle ) { |
| 251 | 277 | case 'wp-block-library': |
| @@ -284,19 +310,15 @@ | ||
| 284 | 310 | |
| 285 | 311 | * @param WP_Styles $styles WP_Styles instance. |
| 286 | 312 | */ |
| 287 | 313 | function gutenberg_register_packages_styles( $styles ) { |
| 288 | - // When in production, use the plugin's version as the asset version; | |
| 289 | - // else (for development or test) default to use the current time. | |
| 290 | - $version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time(); | |
| 291 | - | |
| 292 | 314 | // Editor Styles. |
| 293 | 315 | gutenberg_override_style( |
| 294 | 316 | $styles, |
| 295 | 317 | 'wp-block-editor', |
| 296 | 318 | gutenberg_url( 'build/block-editor/style.css' ), |
| 297 | - array( 'wp-components' ), | |
| 298 | - $version | |
| 319 | + array( 'wp-components', 'wp-editor-font' ), | |
| 320 | + filemtime( gutenberg_dir_path() . 'build/editor/style.css' ) | |
| 299 | 321 | ); |
| 300 | 322 | $styles->add_data( 'wp-block-editor', 'rtl', 'replace' ); |
| 301 | 323 | |
| 302 | 324 | gutenberg_override_style( |
| @@ -302,10 +324,10 @@ | ||
| 302 | 324 | gutenberg_override_style( |
| 303 | 325 | $styles, |
| 304 | 326 | 'wp-editor', |
| 305 | 327 | gutenberg_url( 'build/editor/style.css' ), |
| 306 | - array( 'wp-components', 'wp-block-editor', 'wp-nux', 'wp-reusable-blocks' ), | |
| 307 | - $version | |
| 328 | + array( 'wp-components', 'wp-block-editor', 'wp-nux' ), | |
| 329 | + filemtime( gutenberg_dir_path() . 'build/editor/style.css' ) | |
| 308 | 330 | ); |
| 309 | 331 | $styles->add_data( 'wp-editor', 'rtl', 'replace' ); |
| 310 | 332 | |
| 311 | 333 | gutenberg_override_style( |
| @@ -312,9 +334,9 @@ | ||
| 312 | 334 | $styles, |
| 313 | 335 | 'wp-edit-post', |
| 314 | 336 | gutenberg_url( 'build/edit-post/style.css' ), |
| 315 | 337 | array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ), |
| 316 | - $version | |
| 338 | + filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' ) | |
| 317 | 339 | ); |
| 318 | 340 | $styles->add_data( 'wp-edit-post', 'rtl', 'replace' ); |
| 319 | 341 | |
| 320 | 342 | gutenberg_override_style( |
| @@ -321,22 +343,21 @@ | ||
| 321 | 343 | $styles, |
| 322 | 344 | 'wp-components', |
| 323 | 345 | gutenberg_url( 'build/components/style.css' ), |
| 324 | 346 | array( 'dashicons' ), |
| 325 | - $version | |
| 347 | + filemtime( gutenberg_dir_path() . 'build/components/style.css' ) | |
| 326 | 348 | ); |
| 327 | 349 | $styles->add_data( 'wp-components', 'rtl', 'replace' ); |
| 328 | 350 | |
| 329 | - $block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style'; | |
| 351 | + $block_library_filename = gutenberg_should_load_separate_block_styles() ? 'common' : 'style'; | |
| 330 | 352 | gutenberg_override_style( |
| 331 | 353 | $styles, |
| 332 | 354 | 'wp-block-library', |
| 333 | 355 | gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ), |
| 334 | 356 | array(), |
| 335 | - $version | |
| 357 | + filemtime( gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' ) | |
| 336 | 358 | ); |
| 337 | 359 | $styles->add_data( 'wp-block-library', 'rtl', 'replace' ); |
| 338 | - $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' ); | |
| 339 | 360 | |
| 340 | 361 | gutenberg_override_style( |
| 341 | 362 | $styles, |
| 342 | 363 | 'wp-format-library', |
| @@ -341,57 +362,24 @@ | ||
| 341 | 362 | $styles, |
| 342 | 363 | 'wp-format-library', |
| 343 | 364 | gutenberg_url( 'build/format-library/style.css' ), |
| 344 | 365 | array( 'wp-block-editor', 'wp-components' ), |
| 345 | - $version | |
| 366 | + filemtime( gutenberg_dir_path() . 'build/format-library/style.css' ) | |
| 346 | 367 | ); |
| 347 | 368 | $styles->add_data( 'wp-format-library', 'rtl', 'replace' ); |
| 348 | 369 | |
| 349 | - $wp_edit_blocks_dependencies = array( | |
| 350 | - 'wp-components', | |
| 351 | - 'wp-editor', | |
| 352 | - // This need to be added before the block library styles, | |
| 353 | - // The block library styles override the "reset" styles. | |
| 354 | - 'wp-reset-editor-styles', | |
| 355 | - 'wp-block-library', | |
| 356 | - 'wp-reusable-blocks', | |
| 357 | - ); | |
| 358 | - | |
| 359 | - // Only load the default layout and margin styles for themes without theme.json file. | |
| 360 | - if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { | |
| 361 | - $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles'; | |
| 362 | - } | |
| 363 | - | |
| 364 | - global $editor_styles; | |
| 365 | - if ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) { | |
| 366 | - // Include opinionated block styles if no $editor_styles are declared, so the editor never appears broken. | |
| 367 | - $wp_edit_blocks_dependencies[] = 'wp-block-library-theme'; | |
| 368 | - } | |
| 369 | - | |
| 370 | 370 | gutenberg_override_style( |
| 371 | 371 | $styles, |
| 372 | - 'wp-reset-editor-styles', | |
| 373 | - gutenberg_url( 'build/block-library/reset.css' ), | |
| 374 | - array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Admin styles. | |
| 375 | - $version | |
| 376 | - ); | |
| 377 | - $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' ); | |
| 378 | - | |
| 379 | - gutenberg_override_style( | |
| 380 | - $styles, | |
| 381 | - 'wp-editor-classic-layout-styles', | |
| 382 | - gutenberg_url( 'build/edit-post/classic.css' ), | |
| 383 | - array(), | |
| 384 | - $version | |
| 385 | - ); | |
| 386 | - $styles->add_data( 'wp-editor-classic-layout-styles', 'rtl', 'replace' ); | |
| 387 | - | |
| 388 | - gutenberg_override_style( | |
| 389 | - $styles, | |
| 390 | 372 | 'wp-edit-blocks', |
| 391 | 373 | gutenberg_url( 'build/block-library/editor.css' ), |
| 392 | - $wp_edit_blocks_dependencies, | |
| 393 | - $version | |
| 374 | + array( | |
| 375 | + 'wp-components', | |
| 376 | + 'wp-editor', | |
| 377 | + 'wp-block-library', | |
| 378 | + // Always include visual styles so the editor never appears broken. | |
| 379 | + 'wp-block-library-theme', | |
| 380 | + ), | |
| 381 | + filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' ) | |
| 394 | 382 | ); |
| 395 | 383 | $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' ); |
| 396 | 384 | |
| 397 | 385 | gutenberg_override_style( |
| @@ -398,9 +386,9 @@ | ||
| 398 | 386 | $styles, |
| 399 | 387 | 'wp-nux', |
| 400 | 388 | gutenberg_url( 'build/nux/style.css' ), |
| 401 | 389 | array( 'wp-components' ), |
| 402 | - $version | |
| 390 | + filemtime( gutenberg_dir_path() . 'build/nux/style.css' ) | |
| 403 | 391 | ); |
| 404 | 392 | $styles->add_data( 'wp-nux', 'rtl', 'replace' ); |
| 405 | 393 | |
| 406 | 394 | gutenberg_override_style( |
| @@ -407,9 +395,9 @@ | ||
| 407 | 395 | $styles, |
| 408 | 396 | 'wp-block-library-theme', |
| 409 | 397 | gutenberg_url( 'build/block-library/theme.css' ), |
| 410 | 398 | array(), |
| 411 | - $version | |
| 399 | + filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' ) | |
| 412 | 400 | ); |
| 413 | 401 | $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' ); |
| 414 | 402 | |
| 415 | 403 | gutenberg_override_style( |
| @@ -416,9 +404,9 @@ | ||
| 416 | 404 | $styles, |
| 417 | 405 | 'wp-list-reusable-blocks', |
| 418 | 406 | gutenberg_url( 'build/list-reusable-blocks/style.css' ), |
| 419 | 407 | array( 'wp-components' ), |
| 420 | - $version | |
| 408 | + filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' ) | |
| 421 | 409 | ); |
| 422 | 410 | $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' ); |
| 423 | 411 | |
| 424 | 412 | gutenberg_override_style( |
| @@ -425,9 +413,9 @@ | ||
| 425 | 413 | $styles, |
| 426 | 414 | 'wp-edit-navigation', |
| 427 | 415 | gutenberg_url( 'build/edit-navigation/style.css' ), |
| 428 | 416 | array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ), |
| 429 | - $version | |
| 417 | + filemtime( gutenberg_dir_path() . 'build/edit-navigation/style.css' ) | |
| 430 | 418 | ); |
| 431 | 419 | $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' ); |
| 432 | 420 | |
| 433 | 421 | gutenberg_override_style( |
| @@ -434,9 +422,9 @@ | ||
| 434 | 422 | $styles, |
| 435 | 423 | 'wp-edit-site', |
| 436 | 424 | gutenberg_url( 'build/edit-site/style.css' ), |
| 437 | 425 | array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ), |
| 438 | - $version | |
| 426 | + filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' ) | |
| 439 | 427 | ); |
| 440 | 428 | $styles->add_data( 'wp-edit-site', 'rtl', 'replace' ); |
| 441 | 429 | |
| 442 | 430 | gutenberg_override_style( |
| @@ -442,10 +430,10 @@ | ||
| 442 | 430 | gutenberg_override_style( |
| 443 | 431 | $styles, |
| 444 | 432 | 'wp-edit-widgets', |
| 445 | 433 | gutenberg_url( 'build/edit-widgets/style.css' ), |
| 446 | - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks', 'wp-widgets' ), | |
| 447 | - $version | |
| 434 | + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ), | |
| 435 | + filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' ) | |
| 448 | 436 | ); |
| 449 | 437 | $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' ); |
| 450 | 438 | |
| 451 | 439 | gutenberg_override_style( |
| @@ -452,39 +440,31 @@ | ||
| 452 | 440 | $styles, |
| 453 | 441 | 'wp-block-directory', |
| 454 | 442 | gutenberg_url( 'build/block-directory/style.css' ), |
| 455 | 443 | array( 'wp-block-editor', 'wp-components' ), |
| 456 | - $version | |
| 444 | + filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' ) | |
| 457 | 445 | ); |
| 458 | 446 | $styles->add_data( 'wp-block-directory', 'rtl', 'replace' ); |
| 447 | +} | |
| 448 | +add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' ); | |
| 459 | 449 | |
| 460 | - gutenberg_override_style( | |
| 461 | - $styles, | |
| 462 | - 'wp-customize-widgets', | |
| 463 | - gutenberg_url( 'build/customize-widgets/style.css' ), | |
| 464 | - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-widgets' ), | |
| 465 | - $version | |
| 466 | - ); | |
| 467 | - $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' ); | |
| 450 | +/** | |
| 451 | + * Registers common scripts and styles to be used as dependencies of the editor | |
| 452 | + * and plugins. | |
| 453 | + * | |
| 454 | + * @since 0.1.0 | |
| 455 | + */ | |
| 456 | +function gutenberg_enqueue_block_editor_assets() { | |
| 457 | + if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) { | |
| 458 | + $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD; | |
| 468 | 459 | |
| 469 | - gutenberg_override_style( | |
| 470 | - $styles, | |
| 471 | - 'wp-reusable-blocks', | |
| 472 | - gutenberg_url( 'build/reusable-blocks/style.css' ), | |
| 473 | - array( 'wp-components' ), | |
| 474 | - $version | |
| 475 | - ); | |
| 476 | - $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' ); | |
| 477 | - | |
| 478 | - gutenberg_override_style( | |
| 479 | - $styles, | |
| 480 | - 'wp-widgets', | |
| 481 | - gutenberg_url( 'build/widgets/style.css' ), | |
| 482 | - array( 'wp-components' ) | |
| 483 | - ); | |
| 484 | - $styles->add_data( 'wp-widgets', 'rtl', 'replace' ); | |
| 460 | + wp_enqueue_script( | |
| 461 | + 'gutenberg-live-reload', | |
| 462 | + $live_reload_url | |
| 463 | + ); | |
| 464 | + } | |
| 485 | 465 | } |
| 486 | -add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' ); | |
| 466 | +add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' ); | |
| 487 | 467 | |
| 488 | 468 | /** |
| 489 | 469 | * Retrieves a unique and reasonably short and human-friendly filename for a |
| 490 | 470 | * vendor script based on a URL and the script handle. |
| @@ -603,75 +583,165 @@ | ||
| 603 | 583 | ); |
| 604 | 584 | } |
| 605 | 585 | |
| 606 | 586 | /** |
| 587 | + * Extends block editor settings to include Gutenberg's `editor-styles.css` as | |
| 588 | + * taking precedent those styles shipped with core. | |
| 589 | + * | |
| 590 | + * @param array $settings Default editor settings. | |
| 591 | + * | |
| 592 | + * @return array Filtered editor settings. | |
| 593 | + */ | |
| 594 | +function gutenberg_extend_block_editor_styles( $settings ) { | |
| 595 | + $editor_styles_file = is_rtl() ? | |
| 596 | + gutenberg_dir_path() . 'build/editor/editor-styles-rtl.css' : | |
| 597 | + gutenberg_dir_path() . 'build/editor/editor-styles.css'; | |
| 598 | + | |
| 599 | + /* | |
| 600 | + * If, for whatever reason, the built editor styles do not exist, avoid | |
| 601 | + * override and fall back to the default. | |
| 602 | + */ | |
| 603 | + if ( ! file_exists( $editor_styles_file ) ) { | |
| 604 | + return $settings; | |
| 605 | + } | |
| 606 | + | |
| 607 | + if ( empty( $settings['styles'] ) ) { | |
| 608 | + $settings['styles'] = array(); | |
| 609 | + } else { | |
| 610 | + /* | |
| 611 | + * The styles setting is an array of CSS strings, so there is no direct | |
| 612 | + * way to find the default styles. To maximize stability, load (again) | |
| 613 | + * the default styles from disk and find its place in the array. | |
| 614 | + * | |
| 615 | + * See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L175 | |
| 616 | + */ | |
| 617 | + | |
| 618 | + $default_styles = file_get_contents( | |
| 619 | + is_rtl() ? | |
| 620 | + ABSPATH . WPINC . '/css/dist/editor/editor-styles-rtl.css' : | |
| 621 | + ABSPATH . WPINC . '/css/dist/editor/editor-styles.css' | |
| 622 | + ); | |
| 623 | + | |
| 624 | + /* | |
| 625 | + * Iterate backwards from the end of the array since the preferred | |
| 626 | + * insertion point in case not found is prepended as first entry. | |
| 627 | + */ | |
| 628 | + for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) { | |
| 629 | + if ( isset( $settings['styles'][ $i ]['css'] ) && | |
| 630 | + $default_styles === $settings['styles'][ $i ]['css'] ) { | |
| 631 | + break; | |
| 632 | + } | |
| 633 | + } | |
| 634 | + } | |
| 635 | + | |
| 636 | + $editor_styles = array( | |
| 637 | + 'css' => file_get_contents( $editor_styles_file ), | |
| 638 | + ); | |
| 639 | + | |
| 640 | + // Substitute default styles if found. Otherwise, prepend to setting array. | |
| 641 | + if ( isset( $i ) && $i >= 0 ) { | |
| 642 | + $settings['styles'][ $i ] = $editor_styles; | |
| 643 | + } else { | |
| 644 | + array_unshift( $settings['styles'], $editor_styles ); | |
| 645 | + } | |
| 646 | + | |
| 647 | + return $settings; | |
| 648 | +} | |
| 649 | +add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' ); | |
| 650 | + | |
| 651 | +/** | |
| 652 | + * Load the default editor styles. | |
| 653 | + * These styles are used if the "no theme styles" options is triggered. | |
| 654 | + * | |
| 655 | + * @param array $settings Default editor settings. | |
| 656 | + * | |
| 657 | + * @return array Filtered editor settings. | |
| 658 | + */ | |
| 659 | +function gutenberg_extend_block_editor_settings_with_default_editor_styles( $settings ) { | |
| 660 | + $editor_styles_file = is_rtl() ? | |
| 661 | + gutenberg_dir_path() . 'build/editor/editor-styles-rtl.css' : | |
| 662 | + gutenberg_dir_path() . 'build/editor/editor-styles.css'; | |
| 663 | + $settings['defaultEditorStyles'] = array( | |
| 664 | + array( | |
| 665 | + 'css' => file_get_contents( $editor_styles_file ), | |
| 666 | + ), | |
| 667 | + ); | |
| 668 | + | |
| 669 | + return $settings; | |
| 670 | +} | |
| 671 | +add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_default_editor_styles' ); | |
| 672 | + | |
| 673 | +/** | |
| 674 | + * Adds a flag to the editor settings to know whether we're in FSE theme or not. | |
| 675 | + * | |
| 676 | + * @param array $settings Default editor settings. | |
| 677 | + * | |
| 678 | + * @return array Filtered editor settings. | |
| 679 | + */ | |
| 680 | +function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) { | |
| 681 | + $settings['isFSETheme'] = gutenberg_is_fse_theme(); | |
| 682 | + return $settings; | |
| 683 | +} | |
| 684 | +add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' ); | |
| 685 | + | |
| 686 | +/** | |
| 607 | 687 | * Sets the editor styles to be consumed by JS. |
| 608 | 688 | */ |
| 609 | -function gutenberg_resolve_assets() { | |
| 610 | - global $pagenow; | |
| 611 | - | |
| 612 | - $script_handles = array(); | |
| 613 | - $style_handles = array( | |
| 689 | +function gutenberg_extend_block_editor_styles_html() { | |
| 690 | + $handles = array( | |
| 614 | 691 | 'wp-block-editor', |
| 615 | 692 | 'wp-block-library', |
| 616 | - 'wp-block-library-theme', | |
| 617 | 693 | 'wp-edit-blocks', |
| 618 | 694 | ); |
| 619 | 695 | |
| 620 | - if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) { | |
| 621 | - $style_handles[] = 'wp-widgets'; | |
| 622 | - $style_handles[] = 'wp-edit-widgets'; | |
| 623 | - } | |
| 624 | - | |
| 625 | 696 | $block_registry = WP_Block_Type_Registry::get_instance(); |
| 626 | 697 | |
| 627 | - foreach ( $block_registry->get_all_registered() as $block_type ) { | |
| 698 | + foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { | |
| 628 | 699 | if ( ! empty( $block_type->style ) ) { |
| 629 | - $style_handles[] = $block_type->style; | |
| 700 | + $handles[] = $block_type->style; | |
| 630 | 701 | } |
| 631 | 702 | |
| 632 | 703 | if ( ! empty( $block_type->editor_style ) ) { |
| 633 | - $style_handles[] = $block_type->editor_style; | |
| 704 | + $handles[] = $block_type->editor_style; | |
| 634 | 705 | } |
| 635 | - | |
| 636 | - if ( ! empty( $block_type->script ) ) { | |
| 637 | - $script_handles[] = $block_type->script; | |
| 638 | - } | |
| 639 | 706 | } |
| 640 | 707 | |
| 641 | - $style_handles = array_unique( $style_handles ); | |
| 642 | - $done = wp_styles()->done; | |
| 708 | + $handles = array_unique( $handles ); | |
| 709 | + $done = wp_styles()->done; | |
| 643 | 710 | |
| 644 | 711 | ob_start(); |
| 645 | 712 | |
| 646 | - // We do not need reset styles for the iframed editor. | |
| 647 | - wp_styles()->done = array( 'wp-reset-editor-styles' ); | |
| 648 | - wp_styles()->do_items( $style_handles ); | |
| 713 | + wp_styles()->done = array(); | |
| 714 | + wp_styles()->do_items( $handles ); | |
| 649 | 715 | wp_styles()->done = $done; |
| 650 | 716 | |
| 651 | - $styles = ob_get_clean(); | |
| 717 | + $editor_styles = wp_json_encode( array( 'html' => ob_get_clean() ) ); | |
| 652 | 718 | |
| 653 | - $script_handles = array_unique( $script_handles ); | |
| 654 | - $done = wp_scripts()->done; | |
| 719 | + echo "<script>window.__editorStyles = $editor_styles</script>"; | |
| 720 | +} | |
| 721 | +add_action( 'admin_footer-toplevel_page_gutenberg-edit-site', 'gutenberg_extend_block_editor_styles_html' ); | |
| 655 | 722 | |
| 656 | - ob_start(); | |
| 657 | - | |
| 658 | - wp_scripts()->done = array(); | |
| 659 | - wp_scripts()->do_items( $script_handles ); | |
| 660 | - wp_scripts()->done = $done; | |
| 661 | - | |
| 662 | - $scripts = ob_get_clean(); | |
| 663 | - | |
| 664 | - return array( | |
| 665 | - 'styles' => $styles, | |
| 666 | - 'scripts' => $scripts, | |
| 723 | +/** | |
| 724 | + * Adds a polyfill for object-fit in environments which do not support it. | |
| 725 | + * | |
| 726 | + * The script registration occurs in `gutenberg_register_vendor_scripts`, which | |
| 727 | + * should be removed in coordination with this function. | |
| 728 | + * | |
| 729 | + * @see gutenberg_register_vendor_scripts | |
| 730 | + * @see https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit | |
| 731 | + * | |
| 732 | + * @since 9.1.0 | |
| 733 | + * | |
| 734 | + * @param WP_Scripts $scripts WP_Scripts object. | |
| 735 | + */ | |
| 736 | +function gutenberg_add_object_fit_polyfill( $scripts ) { | |
| 737 | + did_action( 'init' ) && $scripts->add_inline_script( | |
| 738 | + 'wp-polyfill', | |
| 739 | + wp_get_script_polyfill( | |
| 740 | + $scripts, | |
| 741 | + array( | |
| 742 | + '"objectFit" in document.documentElement.style' => 'object-fit-polyfill', | |
| 743 | + ) | |
| 744 | + ) | |
| 667 | 745 | ); |
| 668 | 746 | } |
| 669 | - | |
| 670 | -add_filter( | |
| 671 | - 'block_editor_settings_all', | |
| 672 | - function( $settings ) { | |
| 673 | - // In the future we can allow WP Dependency handles to be passed. | |
| 674 | - $settings['__unstableResolvedAssets'] = gutenberg_resolve_assets(); | |
| 675 | - return $settings; | |
| 676 | - } | |
| 677 | -); | |
| 747 | +add_action( 'wp_default_scripts', 'gutenberg_add_object_fit_polyfill', 20 ); | |