| @@ -207,10 +207,9 @@ | ||
| 207 | 207 | gutenberg_register_vendor_script( |
| 208 | 208 | $scripts, |
| 209 | 209 | 'react', |
| 210 | 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' ) | |
| 211 | + array( 'wp-polyfill' ) | |
| 213 | 212 | ); |
| 214 | 213 | gutenberg_register_vendor_script( |
| 215 | 214 | $scripts, |
| 216 | 215 | 'react-dom', |
| @@ -370,9 +369,9 @@ | ||
| 370 | 369 | gutenberg_override_style( |
| 371 | 370 | $styles, |
| 372 | 371 | 'wp-reset-editor-styles', |
| 373 | 372 | gutenberg_url( 'build/block-library/reset.css' ), |
| 374 | - array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Admin styles. | |
| 373 | + array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Adminn styles. | |
| 375 | 374 | $version |
| 376 | 375 | ); |
| 377 | 376 | $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' ); |
| 378 | 377 | |
| @@ -485,8 +484,26 @@ | ||
| 485 | 484 | } |
| 486 | 485 | add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' ); |
| 487 | 486 | |
| 488 | 487 | /** |
| 488 | + * Registers common scripts and styles to be used as dependencies of the editor | |
| 489 | + * and plugins. | |
| 490 | + * | |
| 491 | + * @since 0.1.0 | |
| 492 | + */ | |
| 493 | +function gutenberg_enqueue_block_editor_assets() { | |
| 494 | + if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) { | |
| 495 | + $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD; | |
| 496 | + | |
| 497 | + wp_enqueue_script( | |
| 498 | + 'gutenberg-live-reload', | |
| 499 | + $live_reload_url | |
| 500 | + ); | |
| 501 | + } | |
| 502 | +} | |
| 503 | +add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' ); | |
| 504 | + | |
| 505 | +/** | |
| 489 | 506 | * Retrieves a unique and reasonably short and human-friendly filename for a |
| 490 | 507 | * vendor script based on a URL and the script handle. |
| 491 | 508 | * |
| 492 | 509 | * @param string $handle The name of the script. |
| @@ -603,11 +620,98 @@ | ||
| 603 | 620 | ); |
| 604 | 621 | } |
| 605 | 622 | |
| 606 | 623 | /** |
| 624 | + * Extends block editor settings to remove the Gutenberg's `editor-styles.css`; | |
| 625 | + * | |
| 626 | + * This can be removed when plugin support requires WordPress 5.8.0+. | |
| 627 | + * | |
| 628 | + * @param array $settings Default editor settings. | |
| 629 | + * | |
| 630 | + * @return array Filtered editor settings. | |
| 631 | + */ | |
| 632 | +function gutenberg_extend_block_editor_styles( $settings ) { | |
| 633 | + if ( empty( $settings['styles'] ) ) { | |
| 634 | + $settings['styles'] = array(); | |
| 635 | + } else { | |
| 636 | + /* | |
| 637 | + * WordPress versions prior to 5.8 include a legacy editor styles file | |
| 638 | + * that need to be removed. | |
| 639 | + * This code can be removed from the Gutenberg plugin when the supported WP | |
| 640 | + * version is 5.8 | |
| 641 | + */ | |
| 642 | + $default_styles_file = is_rtl() ? | |
| 643 | + ABSPATH . WPINC . '/css/dist/editor/editor-styles-rtl.css' : | |
| 644 | + ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'; | |
| 645 | + | |
| 646 | + if ( file_exists( $default_styles_file ) ) { | |
| 647 | + $default_styles = file_get_contents( | |
| 648 | + $default_styles_file | |
| 649 | + ); | |
| 650 | + | |
| 651 | + /* | |
| 652 | + * Iterate backwards from the end of the array since the preferred | |
| 653 | + * insertion point in case not found is prepended as first entry. | |
| 654 | + */ | |
| 655 | + for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) { | |
| 656 | + if ( isset( $settings['styles'][ $i ]['css'] ) && | |
| 657 | + $default_styles === $settings['styles'][ $i ]['css'] ) { | |
| 658 | + break; | |
| 659 | + } | |
| 660 | + } | |
| 661 | + | |
| 662 | + if ( isset( $i ) && $i >= 0 ) { | |
| 663 | + unset( $settings['styles'][ $i ] ); | |
| 664 | + } | |
| 665 | + } | |
| 666 | + } | |
| 667 | + | |
| 668 | + // Remove the default font editor styles for FSE themes. | |
| 669 | + if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { | |
| 670 | + foreach ( $settings['styles'] as $j => $style ) { | |
| 671 | + if ( 0 === strpos( $style['css'], 'body { font-family:' ) ) { | |
| 672 | + unset( $settings['styles'][ $j ] ); | |
| 673 | + } | |
| 674 | + } | |
| 675 | + } | |
| 676 | + | |
| 677 | + return $settings; | |
| 678 | +} | |
| 679 | +// This can be removed when plugin support requires WordPress 5.8.0+. | |
| 680 | +if ( function_exists( 'get_block_editor_settings' ) ) { | |
| 681 | + add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_styles' ); | |
| 682 | +} else { | |
| 683 | + add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' ); | |
| 684 | +} | |
| 685 | + | |
| 686 | +/** | |
| 687 | + * Adds a flag to the editor settings to know whether we're in FSE theme or not. | |
| 688 | + * | |
| 689 | + * This can be removed when plugin support requires WordPress 5.8.0+. | |
| 690 | + * | |
| 691 | + * @param array $settings Default editor settings. | |
| 692 | + * | |
| 693 | + * @return array Filtered editor settings. | |
| 694 | + */ | |
| 695 | +function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) { | |
| 696 | + $settings['supportsTemplateMode'] = gutenberg_supports_block_templates(); | |
| 697 | + | |
| 698 | + // Enable the new layout options for themes with a theme.json file. | |
| 699 | + $settings['supportsLayout'] = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support(); | |
| 700 | + | |
| 701 | + return $settings; | |
| 702 | +} | |
| 703 | +// This can be removed when plugin support requires WordPress 5.8.0+. | |
| 704 | +if ( function_exists( 'get_block_editor_settings' ) ) { | |
| 705 | + add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' ); | |
| 706 | +} else { | |
| 707 | + add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' ); | |
| 708 | +} | |
| 709 | + | |
| 710 | +/** | |
| 607 | 711 | * Sets the editor styles to be consumed by JS. |
| 608 | 712 | */ |
| 609 | -function gutenberg_resolve_assets() { | |
| 713 | +function gutenberg_extend_block_editor_styles_html() { | |
| 610 | 714 | global $pagenow; |
| 611 | 715 | |
| 612 | 716 | $script_handles = array(); |
| 613 | 717 | $style_handles = array( |
| @@ -660,18 +764,17 @@ | ||
| 660 | 764 | wp_scripts()->done = $done; |
| 661 | 765 | |
| 662 | 766 | $scripts = ob_get_clean(); |
| 663 | 767 | |
| 664 | - return array( | |
| 665 | - 'styles' => $styles, | |
| 666 | - 'scripts' => $scripts, | |
| 768 | + $editor_assets = wp_json_encode( | |
| 769 | + array( | |
| 770 | + 'styles' => $styles, | |
| 771 | + 'scripts' => $scripts, | |
| 772 | + ) | |
| 667 | 773 | ); |
| 774 | + | |
| 775 | + echo "<script>window.__editorAssets = $editor_assets</script>"; | |
| 668 | 776 | } |
| 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 | -); | |
| 777 | +add_action( 'admin_footer-appearance_page_gutenberg-edit-site', 'gutenberg_extend_block_editor_styles_html' ); | |
| 778 | +add_action( 'admin_footer-post.php', 'gutenberg_extend_block_editor_styles_html' ); | |
| 779 | +add_action( 'admin_footer-post-new.php', 'gutenberg_extend_block_editor_styles_html' ); | |
| 780 | +add_action( 'admin_footer-widgets.php', 'gutenberg_extend_block_editor_styles_html' ); | |