Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Fix sorting to push Storefront to the top of the theme list (#4187)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
joshuatf and timmyc authored Apr 28, 2020
1 parent 9aa723c commit 5f3223f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/Features/Onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,31 @@ public static function get_allowed_product_types() {
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.
*
Expand All @@ -281,18 +306,11 @@ public static function get_themes() {
$themes = array();

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;
}
);

foreach ( $theme_data->products as $theme ) {
$theme_data = json_decode( $theme_data['body'] );
$woo_themes = property_exists( $theme_data, 'products' ) ? $theme_data->products : array();
$sorted_themes = self::sort_woocommerce_themes( $woo_themes );

foreach ( $sorted_themes as $theme ) {
$slug = sanitize_title_with_dashes( $theme->slug );
$themes[ $slug ] = (array) $theme;
$themes[ $slug ]['is_installed'] = false;
Expand Down
40 changes: 40 additions & 0 deletions tests/features/class-wc-tests-onboarding.php
Original file line number Diff line number Diff line change
@@ -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 );
}

}

0 comments on commit 5f3223f

Please sign in to comment.