Making WordPress.org


Ignore:
Timestamp:
11/21/2024 07:16:33 PM (3 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.