Fix sorting to push Storefront to the top of the theme list (https://github.com/woocommerce/woocommerce-admin/pull/4187)
* Fix sorting to push Storefront to the top of the theme list * Add test to ensure Storefront is sorted in get_themes() * copy paste cleanup * Refactor sorting logic. * Fix linting errors * Updates per feedback. * Add check for products property * Fix for 5.6 CI. * Oh hey another CI fix Co-authored-by: Timmy Crawford <timmydcrawford@gmail.com>
This commit is contained in:
parent
f401b852f1
commit
ea2c2f7707
|
@ -269,6 +269,31 @@ class Onboarding {
|
|||
return apply_filters( 'woocommerce_admin_onboarding_product_types', $product_types );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort themes returned from WooCommerce.com
|
||||
*
|
||||
* @param array $themes Array of themes from WooCommerce.com.
|
||||
* @return array
|
||||
*/
|
||||
public static function sort_woocommerce_themes( $themes ) {
|
||||
usort(
|
||||
$themes,
|
||||
function ( $product_1, $product_2 ) {
|
||||
if ( ! property_exists( $product_1, 'id' ) || ! property_exists( $product_1, 'slug' ) ) {
|
||||
return 1;
|
||||
}
|
||||
if ( ! property_exists( $product_2, 'id' ) || ! property_exists( $product_2, 'slug' ) ) {
|
||||
return 1;
|
||||
}
|
||||
if ( in_array( 'Storefront', array( $product_1->slug, $product_2->slug ), true ) ) {
|
||||
return 'Storefront' === $product_1->slug ? -1 : 1;
|
||||
}
|
||||
return $product_1->id < $product_2->id ? 1 : -1;
|
||||
}
|
||||
);
|
||||
return $themes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of themes for the onboarding wizard.
|
||||
*
|
||||
|
@ -282,17 +307,10 @@ class Onboarding {
|
|||
|
||||
if ( ! is_wp_error( $theme_data ) ) {
|
||||
$theme_data = json_decode( $theme_data['body'] );
|
||||
usort(
|
||||
$theme_data->products,
|
||||
function ( $product_1, $product_2 ) {
|
||||
if ( 'Storefront' === $product_1->slug ) {
|
||||
return -1;
|
||||
}
|
||||
return $product_1->id < $product_2->id ? 1 : -1;
|
||||
}
|
||||
);
|
||||
$woo_themes = property_exists( $theme_data, 'products' ) ? $theme_data->products : array();
|
||||
$sorted_themes = self::sort_woocommerce_themes( $woo_themes );
|
||||
|
||||
foreach ( $theme_data->products as $theme ) {
|
||||
foreach ( $sorted_themes as $theme ) {
|
||||
$slug = sanitize_title_with_dashes( $theme->slug );
|
||||
$themes[ $slug ] = (array) $theme;
|
||||
$themes[ $slug ]['is_installed'] = false;
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
/**
|
||||
* Onboarding Themes Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\Onboarding-themes
|
||||
*/
|
||||
|
||||
use \Automattic\WooCommerce\Admin\Features\Onboarding;
|
||||
|
||||
/**
|
||||
* Class WC_Tests_Onboarding
|
||||
*/
|
||||
class WC_Tests_Onboarding extends WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Verifies that given an array of theme objects, the object containing Storefront will be sorted to the first position.
|
||||
*/
|
||||
public function test_sort_woocommerce_themes() {
|
||||
$theme1 = (object) array(
|
||||
'id' => 1,
|
||||
'slug' => 'ribs',
|
||||
);
|
||||
$theme2 = (object) array(
|
||||
'id' => 2,
|
||||
'slug' => 'chicken',
|
||||
);
|
||||
$theme3 = (object) array(
|
||||
'id' => 3,
|
||||
'slug' => 'Storefront',
|
||||
);
|
||||
$theme4 = (object) array(
|
||||
'id' => 4,
|
||||
'slug' => 'poutine',
|
||||
);
|
||||
$some_themes = array( $theme1, $theme2, $theme3, $theme4 );
|
||||
$sorted_themes = \Automattic\WooCommerce\Admin\Features\Onboarding::sort_woocommerce_themes( $some_themes );
|
||||
$this->assertEquals( 'Storefront', $sorted_themes[0]->slug );
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue