From 4e9f1c38ec2bcab0fe39cbaeae5c36ebb60fc0d2 Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Fri, 19 Apr 2024 00:30:01 +0900 Subject: [PATCH] Back Compat: Add Patterns submenu for WordPress 6.4 (#60804) Co-authored-by: t-hamano Co-authored-by: aaronrobertshaw Co-authored-by: youknowriad --- lib/compat/wordpress-6.5/compat.php | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/compat/wordpress-6.5/compat.php b/lib/compat/wordpress-6.5/compat.php index 39edaef83e5cc8..c41203e5d27b4e 100644 --- a/lib/compat/wordpress-6.5/compat.php +++ b/lib/compat/wordpress-6.5/compat.php @@ -51,3 +51,42 @@ function gutenberg_add_use_customizer_site_logo_url_flag() { } add_action( 'admin_init', 'gutenberg_add_use_customizer_site_logo_url_flag' ); + +/** + * Add a Patterns submenu (wp-admin/edit.php?post_type=wp_block) under the Appearance menu + * for the Classic theme. This function should not be backported to core, and should be + * removed when the required WP core version for Gutenberg is >= 6.5.0. + * + * @global array $submenu + */ +function gutenberg_add_patterns_page_submenu_item() { + if ( ! is_wp_version_compatible( '6.5' ) && ! wp_is_block_theme() ) { + // Move the Themes submenu forward and inject a Patterns submenu. + global $submenu; + $submenu['themes.php'][4] = $submenu['themes.php'][5]; + $submenu['themes.php'][5] = array( __( 'Patterns', 'gutenberg' ), 'edit_theme_options', 'edit.php?post_type=wp_block' ); + ksort( $submenu['themes.php'], SORT_NUMERIC ); + } +} +add_action( 'admin_init', 'gutenberg_add_patterns_page_submenu_item' ); + +/** + * Filter the `wp_die_handler` to allow access to the Site Editor's Patterns page + * (wp-admin/site-editor.php?path=%2Fpatterns) internally for the Classic theme. This + * function should not be backported to core, and should be removed when the required + * WP core version for Gutenberg is >= 6.5.0. + * + * @param callable $default_handler The default handler. + * @return callable The default handler or a custom handler. + */ +function gutenberg_patterns_page_wp_die_handler( $default_handler ) { + if ( ! is_wp_version_compatible( '6.5' ) && ! wp_is_block_theme() && str_contains( $_SERVER['REQUEST_URI'], 'site-editor.php' ) ) { + $is_patterns = isset( $_GET['postType'] ) && 'wp_block' === sanitize_key( $_GET['postType'] ); + $is_patterns_path = isset( $_GET['path'] ) && 'patterns' === sanitize_key( $_GET['path'] ); + if ( $is_patterns || $is_patterns_path ) { + return '__return_false'; + } + } + return $default_handler; +} +add_filter( 'wp_die_handler', 'gutenberg_patterns_page_wp_die_handler' );