diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 78ed595846b047..fd176f44b99e96 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 +198,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 +283,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 +325,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,40 +346,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, - '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 ) { @@ -374,6 +358,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']; } @@ -384,7 +369,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 ) ) { @@ -395,14 +380,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..cb7b21e89014f5 --- /dev/null +++ b/lib/compat/wordpress-5.9/register-global-styles-cpt.php @@ -0,0 +1,39 @@ + __( '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 a47a3b2a1c9025..27e8c7a69a9ccb 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 8171162dc7c00f..ecab647bf02ce0 100644 --- a/lib/load.php +++ b/lib/load.php @@ -93,6 +93,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.8.1/index.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';