From cb7c810f463e33704c93b8ab3831ff8dd434bd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Mon, 29 Nov 2021 22:25:39 +0100 Subject: [PATCH 1/2] Port changes from WordPress core and register GS CPT in its own function --- ...class-wp-theme-json-resolver-gutenberg.php | 101 +++++++----------- .../register-global-styles-cpt.php | 33 ++++++ lib/global-styles.php | 2 +- lib/load.php | 1 + 4 files changed, 76 insertions(+), 61 deletions(-) create mode 100644 lib/compat/wordpress-5.9/register-global-styles-cpt.php diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 2d224fd138b2eb..5a843f140ab726 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -1,14 +1,19 @@ get( 'TextDomain' ) ); @@ -190,18 +197,19 @@ public static function get_theme_data() { } /** - * Returns the CPT that contains the user's origin config - * for the current theme or a void array if none found. + * Returns the custom post type that contains the user's origin config + * for the current theme or a void array if none are found. * - * It can also create and return a new draft CPT. - * - * @param WP_Theme $theme The theme object. - * If empty, it defaults to the current theme. - * @param bool $should_create_cpt Whether a new CPT should be created if no one was found. - * False by default. - * @param array $post_status_filter Filter CPT by post status. - * ['publish'] by default, so it only fetches published posts. + * This can also create and return a new draft custom post type. * + * @param WP_Theme $theme The theme object. If empty, it + * defaults to the current theme. + * @param bool $should_create_cpt Optional. Whether a new custom post + * type should be created if none are + * found. False by default. + * @param array $post_status_filter Filter Optional. custom post type by + * post status. ['publish'] by default, + * so it only fetches published posts. * @return array Custom Post Type for the user's origin config. */ public static function get_user_data_from_custom_post_type( $theme, $should_create_cpt = false, $post_status_filter = array( 'publish' ) ) { @@ -274,6 +282,7 @@ public static function get_user_data() { $config = array(); $user_cpt = self::get_user_data_from_custom_post_type( wp_get_theme() ); + if ( array_key_exists( 'post_content', $user_cpt ) ) { $decoded_data = json_decode( $user_cpt['post_content'], true ); @@ -315,13 +324,16 @@ public static function get_user_data() { * for the paragraph block, and the theme has done it as well, * the user preference wins. * - * @param string $origin To what level should we merge data. + * @param string $origin Optional. To what level should we merge data. * Valid values are 'theme' or 'custom'. * Default is 'custom'. - * * @return WP_Theme_JSON_Gutenberg */ public static function get_merged_data( $origin = 'custom' ) { + if ( is_array( $origin ) ) { + _deprecated_argument( __FUNCTION__, '5.9' ); + } + $result = new WP_Theme_JSON_Gutenberg(); $result->merge( self::get_core_data() ); $result->merge( self::get_theme_data() ); @@ -333,41 +345,11 @@ public static function get_merged_data( $origin = 'custom' ) { return $result; } - /** - * Registers a Custom Post Type to store the user's origin config. - */ - public static function register_user_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 ); - } - /** * Returns the ID of the custom post type * that stores user data. * - * @return integer + * @return integer|null */ public static function get_user_custom_post_type_id() { if ( null !== self::$user_custom_post_type_id ) { @@ -375,6 +357,7 @@ public static function get_user_custom_post_type_id() { } $user_cpt = self::get_user_data_from_custom_post_type( wp_get_theme(), true ); + if ( array_key_exists( 'ID', $user_cpt ) ) { self::$user_custom_post_type_id = $user_cpt['ID']; } @@ -385,7 +368,7 @@ public static function get_user_custom_post_type_id() { /** * Whether the current theme has a theme.json file. * - * @return boolean + * @return bool */ public static function theme_has_support() { if ( ! isset( self::$theme_has_support ) ) { @@ -399,14 +382,12 @@ public static function theme_has_support() { } /** - * Builds the path to the given file - * and checks that it is readable. + * Builds the path to the given file and checks that it is readable. * - * If it isn't, returns an empty string, - * otherwise returns the whole file path. + * If it isn't, returns an empty string, otherwise returns the whole file path. * * @param string $file_name Name of the file. - * @param bool $template Use template theme directroy. Default: false. + * @param bool $template Optional. Use template theme directory. Default false. * @return string The whole file path or empty if the file doesn't exist. */ private static function get_file_path_from_theme( $file_name, $template = false ) { diff --git a/lib/compat/wordpress-5.9/register-global-styles-cpt.php b/lib/compat/wordpress-5.9/register-global-styles-cpt.php new file mode 100644 index 00000000000000..7ed2251bdc176e --- /dev/null +++ b/lib/compat/wordpress-5.9/register-global-styles-cpt.php @@ -0,0 +1,33 @@ + __( '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 ); +} diff --git a/lib/global-styles.php b/lib/global-styles.php index 997003fffa37fc..2a2e1925ee1e11 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -181,7 +181,7 @@ function gutenberg_experimental_is_site_editor_available() { */ function gutenberg_experimental_global_styles_register_user_cpt() { if ( gutenberg_experimental_is_site_editor_available() ) { - WP_Theme_JSON_Resolver_Gutenberg::register_user_custom_post_type(); + register_global_styles_custom_post_type(); } } diff --git a/lib/load.php b/lib/load.php index d5886786bb1c2f..b34b9c1acbeb92 100644 --- a/lib/load.php +++ b/lib/load.php @@ -94,6 +94,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/blocks.php'; require __DIR__ . '/compat/wordpress-5.9/block-template-utils.php'; require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php'; +require __DIR__ . '/compat/wordpress-5.9/register-global-styles-cpt.php'; require __DIR__ . '/compat/wordpress-5.9/get-global-styles-and-settings.php'; require __DIR__ . '/compat/wordpress-5.9/json-file-decode.php'; require __DIR__ . '/compat/wordpress-5.9/translate-settings-using-i18n-schema.php'; From 2092ed69ce984873740db9f9fe3a7a04fb8089d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Mon, 29 Nov 2021 22:29:25 +0100 Subject: [PATCH 2/2] Fix lint issues --- lib/class-wp-theme-json-resolver-gutenberg.php | 1 + lib/compat/wordpress-5.9/register-global-styles-cpt.php | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 5a843f140ab726..4d6acd25122fc0 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -134,6 +134,7 @@ public static function get_core_data() { * is present in theme.json and in theme supports, * the theme.json takes precendence. * + * @param array $deprecated Deprecated argument. * @return WP_Theme_JSON_Gutenberg Entity that holds theme data. */ public static function get_theme_data( $deprecated = array() ) { 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 7ed2251bdc176e..cb7b21e89014f5 100644 --- a/lib/compat/wordpress-5.9/register-global-styles-cpt.php +++ b/lib/compat/wordpress-5.9/register-global-styles-cpt.php @@ -1,4 +1,10 @@