From b13a9c4db4af1dcdc10a16579f32857a1a17313d Mon Sep 17 00:00:00 2001 From: Nabil Kadimi Date: Sat, 26 May 2018 15:39:02 +0000 Subject: [PATCH] Refactor --- .../abstracts/abstract-sp-custom-post.php | 4 +--- includes/class-sp-player.php | 7 ++++--- includes/sp-core-functions.php | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/includes/abstracts/abstract-sp-custom-post.php b/includes/abstracts/abstract-sp-custom-post.php index d30a030d..2abe5dc9 100644 --- a/includes/abstracts/abstract-sp-custom-post.php +++ b/includes/abstracts/abstract-sp-custom-post.php @@ -82,9 +82,7 @@ abstract class SP_Custom_Post { public function get_terms_sorted_by_sp_order( $taxonomy ) { $terms = get_the_terms( $this->ID, $taxonomy ); if ( $terms ) { - usort( $terms, function( $a, $b ) { - return get_term_meta( $a->term_id, 'sp_order', true ) > get_term_meta( $b->term_id, 'sp_order', true ); - } ); + usort( $terms, 'sp_sort_terms' ); } return $terms; } diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index 17871bf4..fa58d0d4 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -123,10 +123,11 @@ class SP_Player extends SP_Custom_Post { ), ), ); - $seasons = (array)get_the_terms( $this->ID, 'sp_season', $args ); + $seasons = (array) $this->get_terms_sorted_by_sp_order( 'sp_season' ); + $leagues = (array) sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id ); + uksort( $leagues, 'sp_sort_terms' ); $metrics = (array)get_post_meta( $this->ID, 'sp_metrics', true ); $stats = (array)get_post_meta( $this->ID, 'sp_statistics', true ); - $leagues = (array) sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id ); $manual_columns = 'manual' == get_option( 'sportspress_player_columns', 'auto' ) ? true : false; $season_ids = array_filter(wp_list_pluck( $seasons, 'term_id' )); @@ -136,7 +137,7 @@ class SP_Player extends SP_Custom_Post { } $leagues = array_replace( $season_order, $leagues ); - + // Get performance labels $args = array( 'post_type' => array( 'sp_performance' ), diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php index 6b7740fe..859d2f9a 100644 --- a/includes/sp-core-functions.php +++ b/includes/sp-core-functions.php @@ -1463,6 +1463,26 @@ if ( !function_exists( 'sp_sort_table_teams' ) ) { } } +if ( ! function_exists( 'sp_sort_terms' ) ) { + + /** + * Sorts terms by `sp_order`. + * + * @param int|object $a Term ID or term. + * @param int|object $b Term ID or term. + * @return int Sorting order. + */ + function sp_sort_terms( $a, $b ) { + if ( is_int( $a ) ) { + $a = get_term( $a ); + } + if ( is_int( $b ) ) { + $b = get_term( $b ); + } + return get_term_meta( $a->term_id, 'sp_order', true ) > get_term_meta( $b->term_id, 'sp_order', true ); + } +} + if ( !function_exists( 'sp_get_next_event' ) ) { function sp_get_next_event( $args = array() ) { $options = array(