diff --git a/includes/abstracts/abstract-sp-custom-post.php b/includes/abstracts/abstract-sp-custom-post.php index d88d2f0d..2abe5dc9 100644 --- a/includes/abstracts/abstract-sp-custom-post.php +++ b/includes/abstracts/abstract-sp-custom-post.php @@ -5,7 +5,7 @@ * The SportsPress custom post class handles individual post data. * * @class SP_Custom_Post - * @version 0.8 + * @version 2.6.5 * @package SportsPress/Abstracts * @category Abstract Class * @author ThemeBoy @@ -71,4 +71,19 @@ abstract class SP_Custom_Post { public function get_post_data() { return $this->post; } + + /** + * Get terms sorted by order. + * + * @access public + * @param string $taxonomy The taxonomy. + * @return array|false|WP_Error See `get_the_terms()` + */ + public function get_terms_sorted_by_sp_order( $taxonomy ) { + $terms = get_the_terms( $this->ID, $taxonomy ); + if ( $terms ) { + usort( $terms, 'sp_sort_terms' ); + } + return $terms; + } } diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php index 1becb809..f46dd5e1 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Meta_Boxes - * @version 2.6 + * @version 2.6.5 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -20,20 +20,7 @@ class SP_Meta_Box_Player_Statistics { */ public static function output( $post ) { $player = new SP_Player( $post ); - $args = array( - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'sp_order', - 'compare' => 'NOT EXISTS', - ), - array( - 'key' => 'sp_order', - 'compare' => 'EXISTS', - ), - ), - ); - $leagues = get_the_terms( $post->ID, 'sp_league', $args ); + $leagues = $player->get_terms_sorted_by_sp_order( 'sp_league' ); $league_num = sizeof( $leagues ); $sections = get_option( 'sportspress_player_performance_sections', -1 ); $show_career_totals = 'yes' === get_option( 'sportspress_player_show_career_total', 'no' ) ? true : false; diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index 5ecafa73..5240860d 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -13,33 +13,33 @@ class SP_Player extends SP_Custom_Post { /** - * Returns positions + * Returns positions sorted by `sp_order`. * * @access public * @return array */ public function positions() { - return get_the_terms( $this->ID, 'sp_position' ); + return $this->get_terms_sorted_by_sp_order( 'sp_position' ); } /** - * Returns leagues + * Returns leagues sorted by `sp_order`. * * @access public * @return array */ public function leagues() { - return get_the_terms( $this->ID, 'sp_league' ); + return $this->get_terms_sorted_by_sp_order( 'sp_league' ); } /** - * Returns seasons + * Returns seasons sorted by `sp_order`. * * @access public * @return array */ public function seasons() { - return get_the_terms( $this->ID, 'sp_season' ); + return $this->get_terms_sorted_by_sp_order( 'sp_season' ); } /** @@ -110,23 +110,11 @@ class SP_Player extends SP_Custom_Post { * @return array */ public function data( $league_id, $admin = false, $section = -1 ) { - $args = array( - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'sp_order', - 'compare' => 'NOT EXISTS', - ), - array( - 'key' => 'sp_order', - 'compare' => 'EXISTS', - ), - ), - ); - $seasons = (array)get_the_terms( $this->ID, 'sp_season', $args ); + $seasons = (array) $this->get_terms_sorted_by_sp_order( 'sp_season' ); $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 ); + uksort( $leagues, 'sp_sort_terms' ); $manual_columns = 'manual' == get_option( 'sportspress_player_columns', 'auto' ) ? true : false; $season_ids = array_filter(wp_list_pluck( $seasons, 'term_id' )); @@ -136,7 +124,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(