Skip to content

Commit

Permalink
6.2: port changes to get_user_data_from_wp_global_styles
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 22, 2022
1 parent fc639bc commit fe37493
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 91 deletions.
24 changes: 13 additions & 11 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
/*
* Bail early if the theme does not support a theme.json.
*
* Since wp_theme_has_theme_json() only supports the active
* Since wp_theme_has_theme_json only supports the active
* theme, the extra condition for whether $theme is the active theme is
* present here.
*/
Expand All @@ -419,14 +419,16 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
$post_type_filter = 'wp_global_styles';
$stylesheet = $theme->get_stylesheet();
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_type' => $post_type_filter,
'post_status' => $post_status_filter,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'tax_query' => array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_type' => $post_type_filter,
'post_status' => $post_status_filter,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
Expand All @@ -438,7 +440,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
$global_style_query = new WP_Query();
$recent_posts = $global_style_query->query( $args );
if ( count( $recent_posts ) === 1 ) {
$user_cpt = get_post( $recent_posts[0], ARRAY_A );
$user_cpt = get_object_vars( $recent_posts[0] );
} elseif ( $create_post ) {
$cpt_post_id = wp_insert_post(
array(
Expand All @@ -454,7 +456,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
true
);
if ( ! is_wp_error( $cpt_post_id ) ) {
$user_cpt = get_post( $cpt_post_id, ARRAY_A );
$user_cpt = get_object_vars( get_post( $cpt_post_id ) );
}
}

Expand Down
80 changes: 0 additions & 80 deletions lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,86 +17,6 @@
*/
class WP_Theme_JSON_Resolver_6_2 extends WP_Theme_JSON_Resolver_Base {

/**
* Returns the custom post type that contains the user's origin config
* for the active theme or a void array if none are found.
*
* 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 active theme.
* @param bool $create_post Optional. Whether a new custom post
* type should be created if none are
* found. Default false.
* @param array $post_status_filter Optional. Filter custom post type by
* post status. Default `array( 'publish' )`,
* so it only fetches published posts.
* @return array Custom Post Type for the user's origin config.
*/
public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) {
if ( ! $theme instanceof WP_Theme ) {
$theme = wp_get_theme();
}

/*
* Bail early if the theme does not support a theme.json.
*
* Since wp_theme_has_theme_json only supports the active
* theme, the extra condition for whether $theme is the active theme is
* present here.
*/
if ( $theme->get_stylesheet() === get_stylesheet() && ! wp_theme_has_theme_json() ) {
return array();
}

$user_cpt = array();
$post_type_filter = 'wp_global_styles';
$stylesheet = $theme->get_stylesheet();
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_type' => $post_type_filter,
'post_status' => $post_status_filter,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $stylesheet,
),
),
);

$global_style_query = new WP_Query();
$recent_posts = $global_style_query->query( $args );
if ( count( $recent_posts ) === 1 ) {
$user_cpt = get_object_vars( $recent_posts[0] );
} elseif ( $create_post ) {
$cpt_post_id = wp_insert_post(
array(
'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
'post_status' => 'publish',
'post_title' => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518.
'post_type' => $post_type_filter,
'post_name' => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ),
'tax_input' => array(
'wp_theme' => array( $stylesheet ),
),
),
true
);
if ( ! is_wp_error( $cpt_post_id ) ) {
$user_cpt = get_object_vars( get_post( $cpt_post_id ) );
}
}

return $user_cpt;
}

/**
* Returns the data merged from multiple origins.
*
Expand Down

0 comments on commit fe37493

Please sign in to comment.