From 0918b1b70357b59e3f85c24a1ef166977a72b1db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 10 Dec 2021 13:55:16 +0100 Subject: [PATCH 1/2] Do not register CPT twice and move code to lib/compat --- .../register-global-styles-cpt.php | 78 ++++++++++++------- lib/global-styles.php | 21 ----- lib/init.php | 2 +- 3 files changed, 50 insertions(+), 51 deletions(-) diff --git a/lib/compat/wordpress-5.9/register-global-styles-cpt.php b/lib/compat/wordpress-5.9/register-global-styles-cpt.php index cb7b21e89014f..ae2904e8c08fa 100644 --- a/lib/compat/wordpress-5.9/register-global-styles-cpt.php +++ b/lib/compat/wordpress-5.9/register-global-styles-cpt.php @@ -6,34 +6,54 @@ * @package gutenberg */ -/** - * Registers a Custom Post Type to store the user's origin config. - * - * This has been ported to src/wp-includes/post.php +/* + * If wp_get_global_settings is defined, it means the plugin + * is running on WordPress 5.9, so don't need to register the CPT + * as it was already done by WordPress core. */ -function register_global_styles_custom_post_type() { - $args = array( - 'label' => __( 'Global Styles', 'gutenberg' ), - 'description' => 'CPT to store user design tokens', - 'public' => false, - 'show_ui' => false, - 'show_in_rest' => false, - 'rewrite' => false, - 'capabilities' => array( - 'read' => 'edit_theme_options', - 'create_posts' => 'edit_theme_options', - 'edit_posts' => 'edit_theme_options', - 'edit_published_posts' => 'edit_theme_options', - 'delete_published_posts' => 'edit_theme_options', - 'edit_others_posts' => 'edit_theme_options', - 'delete_others_posts' => 'edit_theme_options', - ), - 'map_meta_cap' => true, - 'supports' => array( - 'title', - 'editor', - 'revisions', - ), - ); - register_post_type( 'wp_global_styles', $args ); +if ( ! function_exists( 'wp_get_global_settings' ) ) { + + /** + * Registers a Custom Post Type to store the user's origin config. + * + * This has been ported to src/wp-includes/post.php + */ + function register_global_styles_custom_post_type() { + $args = array( + 'label' => __( 'Global Styles', 'gutenberg' ), + 'description' => 'CPT to store user design tokens', + 'public' => false, + 'show_ui' => false, + 'show_in_rest' => false, + 'rewrite' => false, + 'capabilities' => array( + 'read' => 'edit_theme_options', + 'create_posts' => 'edit_theme_options', + 'edit_posts' => 'edit_theme_options', + 'edit_published_posts' => 'edit_theme_options', + 'delete_published_posts' => 'edit_theme_options', + 'edit_others_posts' => 'edit_theme_options', + 'delete_others_posts' => 'edit_theme_options', + ), + 'map_meta_cap' => true, + 'supports' => array( + 'title', + 'editor', + 'revisions', + ), + ); + register_post_type( 'wp_global_styles', $args ); + } + + /** + * Register CPT to store/access user data. + * + * @return void + */ + function maybe_register_global_styles_custom_post_type() { + if ( wp_is_block_theme() ) { + register_global_styles_custom_post_type(); + } + } + add_action( 'init', 'maybe_register_global_styles_custom_post_type' ); } diff --git a/lib/global-styles.php b/lib/global-styles.php index 78802343c5c32..df4d01203ccb7 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -153,26 +153,6 @@ function_exists( 'gutenberg_is_edit_site_page' ) && return $settings; } -/** - * Whether or not the Site Editor is available. - * - * @return boolean - */ -function gutenberg_experimental_is_site_editor_available() { - return wp_is_block_theme(); -} - -/** - * Register CPT to store/access user data. - * - * @return void - */ -function gutenberg_experimental_global_styles_register_user_cpt() { - if ( gutenberg_experimental_is_site_editor_available() ) { - register_global_styles_custom_post_type(); - } -} - /** * Sanitizes global styles user content removing unsafe rules. * @@ -293,7 +273,6 @@ function gutenberg_load_css_custom_properties() { add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); } -add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' ); add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' ); // kses actions&filters. diff --git a/lib/init.php b/lib/init.php index 606401eed7803..4264c4b3c69a1 100644 --- a/lib/init.php +++ b/lib/init.php @@ -94,7 +94,7 @@ function gutenberg_menu() { * @since 9.4.0 */ function gutenberg_site_editor_menu() { - if ( gutenberg_experimental_is_site_editor_available() ) { + if ( wp_is_block_theme() ) { add_theme_page( __( 'Editor (beta)', 'gutenberg' ), sprintf( From c801d70a20cc5f45e6dd46feb2d9e9d60a5f2679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 10 Dec 2021 17:34:27 +0100 Subject: [PATCH 2/2] Remove unnecessary check --- .../wordpress-5.9/register-global-styles-cpt.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/lib/compat/wordpress-5.9/register-global-styles-cpt.php b/lib/compat/wordpress-5.9/register-global-styles-cpt.php index ae2904e8c08fa..0a18bc547fe62 100644 --- a/lib/compat/wordpress-5.9/register-global-styles-cpt.php +++ b/lib/compat/wordpress-5.9/register-global-styles-cpt.php @@ -21,7 +21,7 @@ function register_global_styles_custom_post_type() { $args = array( 'label' => __( 'Global Styles', 'gutenberg' ), - 'description' => 'CPT to store user design tokens', + 'description' => 'Global styles to include in themes.', 'public' => false, 'show_ui' => false, 'show_in_rest' => false, @@ -45,15 +45,5 @@ function register_global_styles_custom_post_type() { register_post_type( 'wp_global_styles', $args ); } - /** - * Register CPT to store/access user data. - * - * @return void - */ - function maybe_register_global_styles_custom_post_type() { - if ( wp_is_block_theme() ) { - register_global_styles_custom_post_type(); - } - } - add_action( 'init', 'maybe_register_global_styles_custom_post_type' ); + add_action( 'init', 'register_global_styles_custom_post_type' ); }