Making WordPress.org

Changeset 14190


Ignore:
Timestamp:
11/21/2024 07:16:33 PM (22 months ago)
Author:
ryelle
Message:

Theme Previews: Filter out duplicate style variations

If there are duplicate style variations, it's likely that one is a subset (color palette or type set) and one is combined. Find the one with more settings and use that.

Merges WordPress/wordpress.org#425, fixes WordPress/wporg-theme-directory#158.

Location:
sites/trunk/wp-themes.com/public_html/wp-content/plugins/style-variations
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wp-themes.com/public_html/wp-content/plugins/style-variations/inc/page-intercept.php

    r13301 r14190  
    22
    33namespace WordPressdotorg\Theme_Preview\Style_Variations\Page_Intercept;
     4
     5use function WordPressdotorg\Theme_Preview\Style_Variations\get_style_variations;
    46
    57/**
     
    3032         * Retrieve all variations and match to make sure we have one with the same title.
    3133         */
    32         $variations = \WP_Theme_JSON_Resolver::get_style_variations();
     34        $variations = get_style_variations();
    3335        if ( empty( $variations ) ) {
    3436                return false;
     
    99101 * We need to call gutenberg's filter `theme_json_user` to make sure the styles are applied to the page.
    100102 * This use to work for both the page and card but a core change stopped that.
    101  * 
     103 *
    102104 * See: https://core.trac.wordpress.org/ticket/56812
    103  * 
     105 *
    104106 * We now need to also call the core filter `wp_theme_json_data_user` to get the card preview to work.
    105107 * Hopefully this code can be remove when we have a better component to use.
    106  * 
     108 *
    107109 * Ref: https://github.com/WordPress/gutenberg/issues/44886
    108110
  • sites/trunk/wp-themes.com/public_html/wp-content/plugins/style-variations/inc/styles-endpoint.php

    r13301 r14190  
    33namespace WordPressdotorg\Theme_Preview\Style_Variations\API_Endpoint;
    44
     5use function WordPressdotorg\Theme_Preview\Style_Variations\get_style_variations;
     6
    57function endpoint_handler() {
    6         $variations = \WP_Theme_JSON_Resolver::get_style_variations();
     8        $variations = get_style_variations();
    79        $theme_slug = get_option( 'stylesheet' );
    810        $styles     = array();
     
    2628                foreach ( $variations as $variation ) {
    2729                        $title = strtolower( $variation['title'] );
    28                         $link  = add_query_arg( 'style_variation', urlencode( $title ), $base );;
     30                        $link  = add_query_arg( 'style_variation', urlencode( $title ), $base );
    2931
    3032                        $styles[] = array(
  • sites/trunk/wp-themes.com/public_html/wp-content/plugins/style-variations/style-variations.php

    r12349 r14190  
    1313defined( 'WPINC' ) || die();
    1414
     15/**
     16 * Get unique style variations.
     17 *
     18 * If there are multiple variations matching a title, use the longest. This is
     19 * a rough way to check for full (combined) style variations, as opposed to
     20 * color palettes or type sets.
     21 *
     22 * @return array Variation list, or empty array.
     23 */
     24function get_style_variations() {
     25        $variations = array();
     26        $all_variations = \WP_Theme_JSON_Resolver::get_style_variations();
     27
     28        foreach ( $all_variations as $variation ) {
     29                // Safety check: variations must have a title and settings.
     30                if ( ! isset( $variation['title'], $variation['settings'] ) ) {
     31                        continue;
     32                }
     33                $found_variations = wp_list_filter(
     34                        $variations,
     35                        array( 'title' => $variation['title'] )
     36                );
     37                if ( ! $found_variations ) {
     38                        $variations[] = $variation;
     39                } else {
     40                        // Loop over the found variations, even though there should only
     41                        // ever be one, to keep track of the index value.
     42                        foreach ( $found_variations as $i => $found ) {
     43                                $found_settings = wp_json_encode( $found );
     44                                $new_settings = wp_json_encode( $variation );
     45                                if ( strlen( $new_settings ) > strlen( $found_settings ) ) {
     46                                        // Replace the item in the variations list.
     47                                        $variations[ $i ] = $variation;
     48                                }
     49                        }
     50                }
     51        }
     52
     53        return $variations;
     54}
     55
    1556require_once __DIR__ . '/inc/global-style-page.php';
    1657require_once __DIR__ . '/inc/page-intercept.php';
Note: See TracChangeset for help on using the changeset viewer.