diff --git a/includes/admin/class-sp-admin-settings.php b/includes/admin/class-sp-admin-settings.php index 6cb378d3..5fcf5027 100644 --- a/includes/admin/class-sp-admin-settings.php +++ b/includes/admin/class-sp-admin-settings.php @@ -419,6 +419,7 @@ class SP_Admin_Settings { case 'sport' : $option_value = self::get_option( $value['id'], $value['default'] ); + $categories = SP_Admin_Sports::sport_category_names(); ?>
> diff --git a/includes/admin/class-sp-admin-sports.php b/includes/admin/class-sp-admin-sports.php index e2d0dac0..064787b4 100644 --- a/includes/admin/class-sp-admin-sports.php +++ b/includes/admin/class-sp-admin-sports.php @@ -5,7 +5,7 @@ * The SportsPress admin sports class stores preset sport data. * * @class SP_Admin_Sports - * @version 1.7 + * @version 1.8 * @package SportsPress/Admin * @category Class * @author ThemeBoy @@ -22,50 +22,35 @@ class SP_Admin_Sports { if ( empty( self::$presets ) ) { $presets = array(); self::$options = array( - __( 'Sports', 'sportspress' ) => array(), - __( 'Esports', 'sportspress' ) => array(), - __( 'Other', 'sportspress' ) => array( 'custom' => __( 'Custom', 'sportspress' ) ), + 'team-sports' => array(), + 'racket-sports' => array(), + 'water-sports' => array(), + 'target-sports' => array(), + 'esports' => array(), + 'other' => array(), ); - $dir = scandir( SP()->plugin_path() . '/presets' ); - $files = array(); - if ( $dir ) { - foreach ( $dir as $key => $value ) { - if ( substr( $value, 0, 1 ) !== '.' && strpos( $value, '.' ) !== false ) { - $files[] = $value; + foreach ( self::$options as $slug => $options ) { + $dir = scandir( SP()->plugin_path() . '/presets/' . $slug ); + $files = array(); + if ( $dir ) { + foreach ( $dir as $key => $value ) { + if ( substr( $value, 0, 1 ) !== '.' && strpos( $value, '.' ) !== false ) { + $files[] = $value; + } } } - } - foreach( $files as $file ) { - $json_data = file_get_contents( SP()->plugin_path() . '/presets/' . $file ); - $data = json_decode( $json_data, true ); - if ( ! is_array( $data ) ) continue; - $id = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file ); - $presets[ $id ] = $data; - $name = array_key_exists( 'name', $data ) ? __( $data['name'], 'sportspress' ) : $id; - self::$options[ __( 'Sports', 'sportspress' ) ][ $id ] = $name; - } - asort( self::$options[ __( 'Sports', 'sportspress' ) ] ); - - $dir = scandir( SP()->plugin_path() . '/presets/esports' ); - $files = array(); - if ( $dir ) { - foreach ( $dir as $key => $value ) { - if ( substr( $value, 0, 1 ) !== '.' && strpos( $value, '.' ) !== false ) { - $files[] = $value; - } + foreach( $files as $file ) { + $json_data = file_get_contents( SP()->plugin_path() . '/presets/' . $slug . '/' . $file ); + $data = json_decode( $json_data, true ); + if ( ! is_array( $data ) ) continue; + $id = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file ); + $presets[ $id ] = $data; + $name = array_key_exists( 'name', $data ) ? __( $data['name'], 'sportspress' ) : $id; + self::$options[ $slug ][ $id ] = $name; } + asort( self::$options[ $slug ] ); } - foreach( $files as $file ) { - $json_data = file_get_contents( SP()->plugin_path() . '/presets/esports/' . $file ); - $data = json_decode( $json_data, true ); - if ( ! is_array( $data ) ) continue; - $id = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file ); - $presets[ $id ] = $data; - $name = array_key_exists( 'name', $data ) ? __( $data['name'], 'sportspress' ) : $id; - self::$options[ __( 'Esports', 'sportspress' ) ][ $id ] = $name; - } - asort( self::$options[ __( 'Esports', 'sportspress' ) ] ); self::$presets = apply_filters( 'sportspress_get_presets', $presets ); } @@ -100,17 +85,34 @@ class SP_Admin_Sports { * @return void */ public static function apply_preset( $id ) { - if ( 'custom' == $id ) { - $preset = array(); - } else { - $preset = self::get_preset( $id ); - } + $preset = self::get_preset( $id ); // Positions $positions = sp_array_value( $preset, 'positions', array() ); - foreach ( $positions as $index => $term ) { - $slug = $index . '-' . sanitize_title( $term ); - wp_insert_term( $term, 'sp_position', array( 'slug' => $slug ) ); + $i = 0; + foreach ( $positions as $parent => $position ) { + if ( is_array( $position ) ) { + + if ( ! term_exists( $parent, 'sp_position' ) ) { + // Insert parent position + $slug = $i . '-' . sanitize_title( $parent ); + wp_insert_term( $parent, 'sp_position', array( 'slug' => $slug ) ); + } + + // Insert positions with parent + foreach ( $position as $index => $child ) { + $parent_term = term_exists( $parent, 'sp_position' ); + $parent_id = $parent_term['term_id']; + $slug = $index . '-' . sanitize_title( $child ); + wp_insert_term( $child, 'sp_position', array( 'slug' => $slug, 'parent' => $parent_id ) ); + } + } else { + + // Insert single position + $slug = $i . '-' . sanitize_title( $position ); + wp_insert_term( $position, 'sp_position', array( 'slug' => $slug ) ); + } + $i++; } // Outcomes @@ -259,6 +261,21 @@ class SP_Admin_Sports { return $id; } + /** + * Sport category names + * @return null + */ + public static function sport_category_names() { + return apply_filters( 'sportspress_sport_categories', array( + 'team-sports' => __( 'Team Sports', 'sportspress' ), + 'racket-sports' => __( 'Racket Sports', 'sportspress' ), + 'water-sports' => __( 'Water Sports', 'sportspress' ), + 'target-sports' => __( 'Target Sports', 'sportspress' ), + 'esports' => __( 'Esports', 'sportspress' ), + 'other' => __( 'Other', 'sportspress' ), + ) ); + } + /** * Sport preset names for localization * @return null diff --git a/presets/baseball.json b/presets/baseball.json deleted file mode 100644 index db2baf7c..00000000 --- a/presets/baseball.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "name": "Baseball", - "positions": [ - "Pitcher", - "Catcher", - "Infielder", - "Outfielder" - ], - "outcomes": [ - { "name" : "Win", "condition" : ">" }, - { "name" : "Loss", "condition" : "<" }, - { "name" : "Tie", "condition" : "=" } - ], - "results": [ - { "name" : "1", "description" : "1st inning runs" }, - { "name" : "2", "description" : "2nd inning runs" }, - { "name" : "3", "description" : "3rd inning runs" }, - { "name" : "4", "description" : "4th inning runs" }, - { "name" : "5", "description" : "5th inning runs" }, - { "name" : "6", "description" : "6th inning runs" }, - { "name" : "7", "description" : "7th inning runs" }, - { "name" : "8", "description" : "8th inning runs" }, - { "name" : "9", "description" : "9th inning runs" }, - { "name" : "X", "id" : "extra", "description" : "Extra inning runs" }, - { "name" : "R", "description" : "Total runs", "primary" : 1 }, - { "name" : "H", "description" : "Hits" }, - { "name" : "E", "description" : "Errors" } - ], - "performance": [ - { "name" : "AB", "description" : "At bat" }, - { "name" : "R", "description" : "Runs" }, - { "name" : "H", "description" : "Hits" }, - { "name" : "RBI", "description" : "Runs batted in" }, - { "name" : "BB", "description" : "Base on balls" }, - { "name" : "SO", "description" : "Strike outs" }, - { "name" : "1B", "description" : "Singles" }, - { "name" : "2B", "description" : "Doubles" }, - { "name" : "3B", "description" : "Triples" }, - { "name" : "HR", "description" : "Home runs" } - ], - "columns": [ - { "name" : "W", "equation" : "$win", "description" : "Wins" }, - { "name" : "L", "equation" : "$loss", "description" : "Losses" }, - { "name" : "PCT", "equation" : "$win / $eventsplayed", "precision" : 3, "priority" : 1, "description" : "Win percentage" }, - { "name" : "RS", "equation" : "$rfor", "description" : "Runs scored" }, - { "name" : "RA", "equation" : "$ragainst", "description" : "Runs allowed" }, - { "name" : "DIFF", "equation" : "$rfor - $ragainst", "description" : "Run differential" }, - { "name" : "L10", "equation" : "$last10", "description" : "Last 10 games" }, - { "name" : "STRK", "equation" : "$streak", "description" : "Current streak" } - ], - "metrics": [ - { "name" : "B/T", "description": "Bats / Throws" }, - { "name" : "Ht", "description": "Height" }, - { "name" : "Wt", "description": "Weight" } - ], - "statistics": [ - { "name" : "G", "equation" : "$eventsplayed", "description" : "Games played" }, - { "name" : "AVG", "equation" : "$h / $ab", "precision" : 3, "description" : "Batting average" } - ] -} diff --git a/presets/other/custom.json b/presets/other/custom.json new file mode 100644 index 00000000..be8bf3d7 --- /dev/null +++ b/presets/other/custom.json @@ -0,0 +1,17 @@ +{ + "name": "Custom", + "positions": [ + ], + "outcomes": [ + ], + "results": [ + ], + "performance": [ + ], + "columns": [ + ], + "metrics": [ + ], + "statistics": [ + ] +} diff --git a/presets/squash.json b/presets/racket-sports/squash.json similarity index 100% rename from presets/squash.json rename to presets/racket-sports/squash.json diff --git a/presets/table-tennis.json b/presets/racket-sports/table-tennis.json similarity index 100% rename from presets/table-tennis.json rename to presets/racket-sports/table-tennis.json diff --git a/presets/tennis.json b/presets/racket-sports/tennis.json similarity index 100% rename from presets/tennis.json rename to presets/racket-sports/tennis.json diff --git a/presets/darts.json b/presets/target-sports/darts.json similarity index 100% rename from presets/darts.json rename to presets/target-sports/darts.json diff --git a/presets/snooker.json b/presets/target-sports/snooker.json similarity index 100% rename from presets/snooker.json rename to presets/target-sports/snooker.json diff --git a/presets/team-sports/baseball.json b/presets/team-sports/baseball.json new file mode 100644 index 00000000..989ff1f5 --- /dev/null +++ b/presets/team-sports/baseball.json @@ -0,0 +1,91 @@ +{ + "name": "Baseball", + "positions": { + "Batter" : [], + "Pitcher" : [], + "Fielder" : [ + "Catcher", + "First base", + "Second base", + "Third base", + "Shortstop", + "Left field", + "Center field", + "Right field" + ] + }, + "outcomes": [ + { "name" : "Win", "condition" : ">" }, + { "name" : "Loss", "condition" : "<" } + ], + "results": [ + { "name" : "1", "description" : "1st inning runs" }, + { "name" : "2", "description" : "2nd inning runs" }, + { "name" : "3", "description" : "3rd inning runs" }, + { "name" : "4", "description" : "4th inning runs" }, + { "name" : "5", "description" : "5th inning runs" }, + { "name" : "6", "description" : "6th inning runs" }, + { "name" : "7", "description" : "7th inning runs" }, + { "name" : "8", "description" : "8th inning runs" }, + { "name" : "9", "description" : "9th inning runs" }, + { "name" : " ", "id" : "extra", "description" : "Extra inning runs" }, + { "name" : "R", "description" : "Total runs", "primary" : 1 }, + { "name" : "H", "description" : "Hits" }, + { "name" : "E", "description" : "Errors" } + ], + "performance": [ + { "name" : "AB", "position" : "Batter", "description" : "At bat" }, + { "name" : "R", "position" : "Batter", "description" : "Runs" }, + { "name" : "H", "position" : "Batter", "description" : "Hits" }, + { "name" : "RBI", "position" : "Batter", "description" : "Runs batted in" }, + { "name" : "2B", "id" : "doubles", "position" : "Batter", "description" : "Doubles" }, + { "name" : "3B", "id" : "triples", "position" : "Batter", "description" : "Triples" }, + { "name" : "HR", "position" : "Batter", "description" : "Home runs" }, + { "name" : "SB", "position" : "Batter", "description" : "Stolen bases" }, + { "name" : "BB", "position" : "Batter", "description" : "Base on balls" }, + { "name" : "SO", "position" : "Batter", "description" : "Strike outs" }, + { "name" : "LOB", "position" : "Batter", "description" : "Left on base" }, + { "name" : "IP", "id" : "pitcher_ip", "position" : "Pitcher", "description" : "Innings pitched" }, + { "name" : "H", "id" : "pitcher_h", "position" : "Pitcher", "description" : "Hits allowed" }, + { "name" : "R", "id" : "pitcher_r", "position" : "Pitcher", "description" : "Runs allowed" }, + { "name" : "ER", "id" : "pitcher_er", "position" : "Pitcher", "description" : "Earned runs allowed" }, + { "name" : "BB", "id" : "pitcher_bb", "position" : "Pitcher", "description" : "Base on balls allowed" }, + { "name" : "SO", "id" : "pitcher_so", "position" : "Pitcher", "description" : "Strike outs pitched" }, + { "name" : "HR", "id" : "pitcher_hr", "position" : "Pitcher", "description" : "Home runs allowed" } + ], + "columns": [ + { "name" : "W", "equation" : "$win", "description" : "Wins" }, + { "name" : "L", "equation" : "$loss", "description" : "Losses" }, + { "name" : "Pct", "equation" : "$win / $eventsplayed", "precision" : 3, "priority" : 1, "description" : "Win percentage" }, + { "name" : "GB", "equation" : "$gamesback", "precision" : 1, "description" : "Games back" }, + { "name" : "RS", "equation" : "$rfor", "description" : "Runs scored" }, + { "name" : "RA", "equation" : "$ragainst", "description" : "Runs allowed" }, + { "name" : "Diff", "equation" : "$rfor - $ragainst", "description" : "Run differential" }, + { "name" : "Home", "equation" : "$homerecord", "description" : "Home record" }, + { "name" : "Road", "equation" : "$awayrecord", "description" : "Road record" }, + { "name" : "L10", "equation" : "$last10", "description" : "Last 10 games" }, + { "name" : "Strk", "equation" : "$streak", "description" : "Current streak" } + ], + "metrics": [ + { "name" : "B/T", "description": "Bats / Throws" }, + { "name" : "Ht", "description": "Height" }, + { "name" : "Wt", "description": "Weight" } + ], + "statistics": [ + { "name" : "G", "equation" : "$eventsplayed", "description" : "Games played" }, + { "name" : "AVG", "equation" : "$h / $ab", "precision" : 3, "description" : "Batting average" } + ], + "options": { + "event_teams" : "2", + "event_show_players" : "yes", + "event_show_extras" : "no", + "event_show_total" : "yes", + "sportspress_event_results_reverse_teams" : "yes", + "sportspress_event_performance_mode" : "values", + "sportspress_event_performance_reverse_teams" : "yes", + "event_show_player_numbers" : "no", + "event_split_players_by_team" : "yes", + "event_split_players_by_position" : "yes", + "sportspress_event_total_performance" : "all" + } +} diff --git a/presets/basketball.json b/presets/team-sports/basketball.json similarity index 97% rename from presets/basketball.json rename to presets/team-sports/basketball.json index 39f3a147..036c3774 100644 --- a/presets/basketball.json +++ b/presets/team-sports/basketball.json @@ -41,6 +41,7 @@ { "name" : "W", "equation" : "$win", "description" : "Wins" }, { "name" : "L", "equation" : "$loss", "description" : "Losses" }, { "name" : "PCT", "equation" : "$win / $eventsplayed", "precision" : 3, "priority" : 1, "description" : "Win percentage" }, + { "name" : "GB", "equation" : "$gamesback", "precision" : 1, "description" : "Games back" }, { "name" : "PF", "equation" : "$pointsfor / $eventsplayed", "priority" : 3, "description" : "Average points for" }, { "name" : "PA", "equation" : "$pointsagainst / $eventsplayed", "description" : "Average points against" }, { "name" : "DIFF", "equation" : "( $pointsfor - $pointsagainst ) / $eventsplayed", "priority" : 2, "description" : "Average point differential" }, diff --git a/presets/cricket.json b/presets/team-sports/cricket.json similarity index 100% rename from presets/cricket.json rename to presets/team-sports/cricket.json diff --git a/presets/floorball.json b/presets/team-sports/floorball.json similarity index 100% rename from presets/floorball.json rename to presets/team-sports/floorball.json diff --git a/presets/football.json b/presets/team-sports/football.json similarity index 100% rename from presets/football.json rename to presets/team-sports/football.json diff --git a/presets/footy.json b/presets/team-sports/footy.json similarity index 100% rename from presets/footy.json rename to presets/team-sports/footy.json diff --git a/presets/handball.json b/presets/team-sports/handball.json similarity index 100% rename from presets/handball.json rename to presets/team-sports/handball.json diff --git a/presets/ice-hockey.json b/presets/team-sports/ice-hockey.json similarity index 100% rename from presets/ice-hockey.json rename to presets/team-sports/ice-hockey.json diff --git a/presets/lacrosse.json b/presets/team-sports/lacrosse.json similarity index 100% rename from presets/lacrosse.json rename to presets/team-sports/lacrosse.json diff --git a/presets/netball.json b/presets/team-sports/netball.json similarity index 100% rename from presets/netball.json rename to presets/team-sports/netball.json diff --git a/presets/rugby-league.json b/presets/team-sports/rugby-league.json similarity index 100% rename from presets/rugby-league.json rename to presets/team-sports/rugby-league.json diff --git a/presets/rugby-union.json b/presets/team-sports/rugby-union.json similarity index 100% rename from presets/rugby-union.json rename to presets/team-sports/rugby-union.json diff --git a/presets/soccer.json b/presets/team-sports/soccer.json similarity index 100% rename from presets/soccer.json rename to presets/team-sports/soccer.json diff --git a/presets/volleyball.json b/presets/team-sports/volleyball.json similarity index 100% rename from presets/volleyball.json rename to presets/team-sports/volleyball.json diff --git a/presets/water-polo.json b/presets/water-sports/water-polo.json similarity index 100% rename from presets/water-polo.json rename to presets/water-sports/water-polo.json