Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Use wp_theme taxonomy for wp_global_styles cpt. #31436

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/class-wp-theme-json-resolver.php
Expand Up @@ -311,15 +311,20 @@ public static function get_theme_data( $theme_support_data = array() ) {
private static function get_user_data_from_custom_post_type( $should_create_cpt = false, $post_status_filter = array( 'publish' ) ) {
$user_cpt = array();
$post_type_filter = 'wp_global_styles';
$post_name_filter = 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braindump: the result of removing this is that the CPT name will inherit the title's, which is "Custom Styles", so we end up with CPTs with names (and guids) such as custom-styles, custom-styles-2, custom-styles-3, etc. Wondering if we could retain this or even have custom-styles-<theme> as a name.

Not that it matters much for our use as we'll have the taxonomy. However, I wonder what happens if/when we reach the last guid number. Does it start over from 0? At that point, is it ok that there are two CPTs with custom-styles-2 names? Those sort of situations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I guess we can keep the existing name, I updated the code.

$recent_posts = wp_get_recent_posts(
array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_type' => $post_type_filter,
'post_status' => $post_status_filter,
'name' => $post_name_filter,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => wp_get_theme()->get_stylesheet(),
),
),
)
);

Expand All @@ -332,7 +337,10 @@ private static function get_user_data_from_custom_post_type( $should_create_cpt
'post_status' => 'publish',
'post_title' => __( 'Custom Styles', 'default' ),
'post_type' => $post_type_filter,
'post_name' => $post_name_filter,
'post_name' => 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ),
'tax_input' => array(
'wp_theme' => array( wp_get_theme()->get_stylesheet() ),
),
),
true
);
Expand Down
4 changes: 2 additions & 2 deletions lib/full-site-editing/templates.php
Expand Up @@ -84,13 +84,13 @@ function gutenberg_register_template_post_type() {
* Registers block editor 'wp_theme' taxonomy.
*/
function gutenberg_register_wp_theme_taxonomy() {
if ( ! gutenberg_supports_block_templates() ) {
if ( ! gutenberg_supports_block_templates() && ! WP_Theme_JSON_Resolver::theme_has_support() ) {
return;
}

register_taxonomy(
'wp_theme',
array( 'wp_template', 'wp_template_part' ),
array( 'wp_template', 'wp_template_part', 'wp_global_styles' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be themes that have theme.json but aren't block-based themes and vice-versa, so we need to make sure that this relationship works as expected in those scenarios (a few lines above this there's a gutenberg_supports_block_templates check that makes this only work on block-based themes).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch I updated the condition used to register the taxonomy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok to register all of this or should we only register the individual pieces per each feature?

  • register wp_global_styles if a theme has theme.json support
  • register wp_template and wp_template_part if a theme has block-based templates

cc @vindl

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are not registering wp_global_styles or wp_template we are registering wp_theme taxonomy and saying the taxonomy can be used on wp_global_styles, wp_template and wp_template_part block types. I think there is no issue in registering a taxonomy and saying it can be used in a CPT that is not yet available.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At #35427 we're probably making changes to when we register the user CPT. I've checked the logic for gutenberg_register_wp_theme_taxonomy and it still works as expected. Just wanted to make a note in case I've missed anything.

array(
'public' => false,
'hierarchical' => false,
Expand Down