From ea2c2f77071428f0fe1d340a6652f1b330c2bd9a Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Wed, 29 Apr 2020 00:47:05 +0300 Subject: [PATCH] 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 --- .../src/Features/Onboarding.php | 40 ++++++++++++++----- .../features/class-wc-tests-onboarding.php | 40 +++++++++++++++++++ 2 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 plugins/woocommerce-admin/tests/features/class-wc-tests-onboarding.php diff --git a/plugins/woocommerce-admin/src/Features/Onboarding.php b/plugins/woocommerce-admin/src/Features/Onboarding.php index 06cca1869d1..b37dd7972a8 100644 --- a/plugins/woocommerce-admin/src/Features/Onboarding.php +++ b/plugins/woocommerce-admin/src/Features/Onboarding.php @@ -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. * @@ -281,18 +306,11 @@ class Onboarding { $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; - } - ); + $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 ( $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; diff --git a/plugins/woocommerce-admin/tests/features/class-wc-tests-onboarding.php b/plugins/woocommerce-admin/tests/features/class-wc-tests-onboarding.php new file mode 100644 index 00000000000..e71fc90f3a1 --- /dev/null +++ b/plugins/woocommerce-admin/tests/features/class-wc-tests-onboarding.php @@ -0,0 +1,40 @@ + 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 ); + } + +}