Changeset 3064521
- Timestamp:
- 04/04/2024 09:55:04 AM (23 months ago)
- Location:
- eh-gms-feed
- Files:
-
- 10 edited
-
tags/3.12/css/gms-default.css (modified) (2 diffs)
-
tags/3.12/eh-gms-feed.php (modified) (1 diff)
-
tags/3.12/includes/main-class.php (modified) (12 diffs)
-
tags/3.12/js/eh-gms-feed.js (modified) (4 diffs)
-
tags/3.12/readme.txt (modified) (2 diffs)
-
trunk/css/gms-default.css (modified) (2 diffs)
-
trunk/eh-gms-feed.php (modified) (1 diff)
-
trunk/includes/main-class.php (modified) (12 diffs)
-
trunk/js/eh-gms-feed.js (modified) (4 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
eh-gms-feed/tags/3.12/css/gms-default.css
r3060038 r3064521 11 11 /* This styling sets alignment for specific tables */ 12 12 .gms-table-results th:nth-child(4), .gms-table-results td:nth-child(4) { text-align: center } 13 .gms-table-league th:not(:first-child) { min-width: 5em }14 13 .gms-table-league td:nth-child(n+3), .gms-table-league th:nth-child(n+3) { text-align: right } 15 16 /* This ensures the 'Date' does not wrap on the fixtures or results table */ 17 .gms-table-fixtures td:first-child, .gms-table-results td:first-child { white-space: nowrap; } 14 .gms-table-fixtures td:first-child, .gms-table-results td:first-child, .gms-table-league.gms-table-custom td:last-child { white-space: nowrap } 18 15 19 16 /* The following will highlight team or club win, draw and loss on results */ 17 .gms-form { display: inline-block; width: 1.25rem; text-align: center } 20 18 .gms-win { background-color: green } 21 19 .gms-draw { background-color: dimgray } … … 30 28 .gms-list > * + * { margin-top: 1.5rem; margin-bottom: 1.5rem } 31 29 .gms-list + .gms-footnote { margin-top: 1rem } 32 .gms-card { text-align: center; padding: 3px 3px; border-radius: 0.5rem }30 .gms-card { text-align: center; padding: 3px 3px; border-radius: 0.5rem; position: relative } 33 31 .gms-card, .gms-cardtime, .gms-cardnoscore, .gms-fixturedetails > div { border: 1px solid; border-color: silver } 34 .gms-cardtitle { font-weight: 700; font-size:larger } 32 .gms-cardtitle, .gms-cancel, .gms-card .gms-directions-card { font-weight: 700; font-size: larger } 33 .gms-cancel { position: absolute; right: 0.5rem; top: 0 } 35 34 .gms-cardfixture, .gms-cardscorers { display: flex; gap: 0.5rem; align-items: center } 36 35 .gms-cardhome { flex: 1 1 0%; text-align: right } -
eh-gms-feed/tags/3.12/eh-gms-feed.php
r3060038 r3064521 4 4 * Plugin URI: https://wordpress.org/plugins/eh-gms-feed/ 5 5 * Description: Show fixtures, results and tables from England Hockey GMS 6 * Version: 3.1 16 * Version: 3.12 7 7 * Author: Matthew Treherne 8 8 * Author URI: https://profiles.wordpress.org/mtreherne -
eh-gms-feed/tags/3.12/includes/main-class.php
r3060038 r3064521 10 10 class EH_GMS_Feed { 11 11 12 private $basename, $settings, $atts_show, $not_valid, $filter_match, $table_name, $fixture_data, $display_name, $show_gender, $show_list, $show_cal, $cal_summary, $id, $show_detail, $directions ;12 private $basename, $settings, $atts_show, $not_valid, $filter_match, $table_name, $fixture_data, $display_name, $show_gender, $show_list, $show_cal, $cal_summary, $id, $show_detail, $directions, $multiple_teams = [], $isLaravel = false, $teamlimit = 12; 13 13 private $club_link, $team_link, $comp_link, $team_comps, $comp_name; 14 14 private $args = ['method','show','team','comp_id','club_id','whatson','link','sort_by','options']; … … 64 64 } 65 65 66 private function get_json($key, $keys=[] ) {66 private function get_json($key, $keys=[], $raw=false) { 67 67 $keys = (array) $keys; $valid_json = true; 68 68 $headers = ['headers' => ['Content-Type' => 'application/json;charset=UTF-8', 'x-functions-key' => $this->settings->gms_api_key]]; … … 98 98 } 99 99 } 100 if ($raw) return $valid_json ? $body : null; 100 101 if ($valid_json) { 101 102 $data = json_decode($body); … … 151 152 152 153 private function show_api_error ($message) { 153 return '<p>'. sprintf(esc_html__( '%1$s', 'eh-gms-feed' ), $message).'</p>';154 return '<p>'.$message.'</p>'; 154 155 } 155 156 … … 283 284 } 284 285 $html .= '<div class="gms-card'.($this->show_detail ? ' gms-detaillink' : '').'"'.($this->show_detail ? ' data-fixture="'.$fixture->id.'"': '').'>' 285 .($show_detail ? '<div class="gms-cardtitle">Fixture Details</div class="gms-cardcomp"><div>'.$fixture->competitionName.'</div>': '')286 .($show_detail ? '<div class="gms-cardtitle">Fixture Details</div><div class="gms-cardcomp">'.$fixture->competitionName.'</div>': '') 286 287 .'<div class="gms-carddate">'.$fixture_card['Date'].'</div>' 287 288 .'<div class="gms-cardfixture"><div class="gms-cardhome"><div '.implode(' ',$home_attr).'>'.$fixture_card['Home Team'].'</div></div><div'.$divide_attr.'>'.$divide.'</div><div class="gms-cardaway"><div '.implode(' ',$away_attr).'>'.$fixture_card['Away Team'].'</div></div></div>' … … 359 360 } 360 361 361 private function process_table ($data ) {362 private function process_table ($data, $custom=false) { 362 363 $html = ''; $link = $this->comp_link; 363 364 $league_data = ['Team' => 'teamName', 'Played' => 'gamesPlayed', 'Won' => 'gamesWon', 'Drawn' => 'gamesDrawn', 'Lost' => 'gamesLost', 'For' => 'goalsFor', 'Against' => 'goalsAgainst', 'GD' => 'goalsDifference', 'Points' => 'totalPoints']; 364 $html .= '<table class="gms-table gms-table-'.$this->display_name.'"><thead><tr><th></th>'; 365 if ($custom) { $league_data['PPG'] = 'PPG'; $league_data['Form'] = 'form'; } 366 $html .= '<table class="gms-table gms-table-'.$this->display_name.($custom ? ' gms-table-custom' : '').'"><thead><tr><th></th>'; 365 367 foreach ($league_data as $column_title => $field_name) $html .= '<th>'.$column_title.'</th>'; 366 368 $html .= '</tr></thead><tbody>'; 367 369 foreach ($data as $position => $team) { 368 370 $class = ''; 369 if ( $team->teamId == $this->atts_show['team']) {371 if (!$custom && $team->teamId == $this->atts_show['team']) { 370 372 $class = ' class="gms-clubteam"'; 371 373 if (!$link) $link = $this->get_link('team', $team->teamEntityUrlSlug); 372 374 } 373 375 $html .= '<tr'.$class.'><td>'.($position+1).'</td>'; 374 foreach ($league_data as $field_name) $html .= '<td>'.(isset($field_name, $team->{$field_name}) ? esc_html($team->{$field_name}) : '').'</td>'; 376 foreach ($league_data as $field_name) { 377 if ($field_name == 'form') { 378 if (count($team->form) > 5) $team->form = array_slice($team->form, -5, 5); 379 $team->form = implode(' ', $team->form); 380 $html .= '<td>'.$team->form.'</td>'; 381 } 382 else $html .= '<td>'.(isset($field_name, $team->{$field_name}) ? esc_html($team->{$field_name}) : '').'</td>'; 383 } 375 384 $html .= '</tr>'; 376 385 } 386 if (empty($data)) $html .= '<tr><td colspan="'.(count($league_data)+1).'">No data for teams found</td></tr>'; 377 387 $html .= '</tbody></table>'; 378 $html .= '<div class="gms-footnote">'.($link ? sprintf(esc_html__( '[ Table%3$s %1$s also found at%4$s %2$s]', 'eh-gms-feed' ), $this->comp_name ? 'for '.$this->comp_name : '', '<a href="'.$link.'" target="_blank">England Hockey Link</a>', '<span class="gms-nomobile">', '</span>') : '').'</div>';388 $html .= '<div class="gms-footnote">'.($link ? sprintf(esc_html__( '[%5$s%3$s %1$s also found at%4$s %2$s]', 'eh-gms-feed' ), $this->comp_name ? 'for '.$this->comp_name : '', '<a href="'.$link.'" target="_blank">England Hockey Link</a>', '<span class="gms-nomobile">', '</span>', ($custom ? esc_html__('Data') : esc_html__('Table'))) : ($custom ? esc_html__('[League table for selected teams]') : '')).'</div>'; 379 389 return $html; 380 390 } … … 503 513 $this->fixture_data = ['Date' => 'fixtureDate', 'Time' => 'fixtureTime', 'Home Team' => ['homeTeam','teamName'], 'Score' => ['homeTeamScore', 'awayTeamScore'], 'Away Team' => ['awayTeam','teamName'], 'Venue' => 'venue']; 504 514 if ($table_name == 'fixtures') unset($this->fixture_data['Score']); 505 if ($table_name == 'league' && $this->is_competition()) {515 if ($table_name == 'league') { 506 516 if ($this->atts_show['link']) $this->comp_link = $this->atts_show['link']; 507 $html .= $this->get_json('competition', $this->atts_show['comp_id']); 517 if ($this->is_competition()) $html .= $this->get_json('competition', $this->atts_show['comp_id']); 518 else $html .= $this->multiple_teams(); 508 519 } 509 520 elseif (($table_name == 'fixtures' || $table_name == 'results' || $table_name == 'combined') && $this->is_team()) { … … 542 553 foreach ($data as $competition) $competitions[$competition->competitionId] = ['name' => $competition->competitionName, 'slug' => $competition->competitionEntityUrlSlug]; 543 554 return $competitions; 555 } 556 557 private function multiple_teams() { 558 $html = ''; 559 $api = 'fixturesandresults'; 560 $json_data = []; 561 $required_data = []; 562 $no_data = 0; 563 // Try the default data cache first 564 foreach ($this->multiple_teams as $teamID) { 565 $body = get_transient('eh_gms_feed '.$this->api[$api]['transient'].' '.$teamID); 566 if ($body === false) $required_data[] = $teamID; 567 else $json_data[$teamID] = $body; 568 } 569 // If Laravel then use Guzzle and do requests in parallel 570 if ($this->isLaravel) { 571 $refreshlist = collect(); 572 foreach ($required_data as $teamID) { 573 $object = new \stdClass; 574 $object->api = $api; 575 $object->api_key = $teamID; 576 $refreshlist->push($object); 577 } 578 if ($refreshlist->isNotEmpty()) $this->refresh_cache($refreshlist); 579 } 580 foreach ($required_data as $teamID) { 581 $json_data[$teamID] = $this->get_json($api, $teamID, true); 582 } 583 if (!empty($json_data)) { 584 $league = []; 585 foreach ($json_data as $key => $snapshot) { 586 if ($snapshot && ($data = json_decode($snapshot)) && json_last_error() === JSON_ERROR_NONE && !empty($data)) { 587 $teamname = $next_fixture_date = null; 588 $gp = $gw = $gd = $gl = $gf = $ga = 0; 589 $form = []; 590 foreach ($data as $competition) { 591 foreach ($competition->fixtures as $fixture) { 592 $isHome = $fixture->homeTeamId == $key; 593 if (!$teamname) $teamname = $isHome ? $fixture->homeTeam->teamName : $fixture->awayTeam->teamName; 594 if ($fixture->isBye) continue; 595 $valid_date = new \DateTime($fixture->fixtureDate); 596 if ($fixture->isResult) { 597 if ($fixture->status == 'Postponed' || ($fixture->homeTeamScore == 'P' && $fixture->awayTeamScore == 'P')) continue; 598 elseif ($fixture->status == 'Cancelled' || ($fixture->homeTeamScore == 'C' && $fixture->awayTeamScore == 'C')) continue; 599 else { 600 $gp++; 601 $gf += $isHome ? $fixture->homeTeamScoreAsInt : $fixture->awayTeamScoreAsInt; 602 $ga += $isHome ? $fixture->awayTeamScoreAsInt : $fixture->homeTeamScoreAsInt; 603 if ($fixture->homeTeamScoreAsInt == $fixture->awayTeamScoreAsInt) { $gd++; $form[] = '<span class="gms-form gms-draw">D</span>'; } 604 else if (($isHome && $fixture->homeTeamScoreAsInt > $fixture->awayTeamScoreAsInt) || (!$isHome && $fixture->homeTeamScoreAsInt < $fixture->awayTeamScoreAsInt)) { $gw++; $form[] = '<span class="gms-form gms-win">W</span>'; } 605 else { $gl++; $form[] = '<span class="gms-form gms-loss">L</span>'; } 606 } 607 } 608 elseif ($this->is_beforetoday($valid_date)) continue; 609 elseif (!$next_fixture_date || $valid_date < $next_fixture_date) $next_fixture_date = $valid_date; 610 } 611 } 612 if ($teamname) { 613 $points = $gw * 3 + $gd; 614 $object = new \stdClass; 615 $object->teamId = $key; 616 $object->teamName = $teamname; 617 $object->gamesPlayed = $gp; 618 $object->gamesWon = $gw; 619 $object->gamesDrawn = $gd; 620 $object->gamesLost = $gl; 621 $object->goalsFor = $gf; 622 $object->goalsAgainst = $ga; 623 $object->goalsDifference = $gf - $ga; 624 $object->totalPoints = $points; 625 $object->PPG = number_format($gp ? $points/$gp : 0, 2, '.', ''); 626 $object->form = $form; 627 $league[] = $object; 628 } 629 } 630 else $no_data++; 631 } 632 usort($league, function ($a,$b) { 633 if ($a->PPG != $b->PPG) return $a->PPG < $b->PPG ? 1 : -1; 634 if ($a->goalsDifference != $b->goalsDifference) return $a->goalsDifference < $b->goalsDifference ? 1 : -1; 635 if ($a->goalsFor != $b->goalsFor) return $a->goalsFor < $b->goalsFor ? 1 : -1; 636 if ($a->gamesWon != $b->gamesWon) return $a->gamesWon < $b->gamesWon ? 1 : -1; 637 return 0; 638 }); 639 $html .= $this->process_table($league, true); 640 if ($no_data) $html .= $this->show_api_error(sprintf(esc_html__( 'Failed to retrieve data for %s of selected team(s)', 'eh-gms-feed' ), $no_data)); 641 } 642 else $html .= $this->show_api_error(esc_html__( 'You have selected a custom league table without any teams', 'eh-gms-feed' )); 643 return $html; 544 644 } 545 645 … … 598 698 $this->atts_show['sort_by'] = sanitize_text_field( $this->atts_show['sort_by'] ); 599 699 $this->atts_show['options'] = sanitize_text_field( $this->atts_show['options'] ); 700 $this->multiple_teams = array_unique(array_filter(array_map('trim', explode('+', $this->atts_show['team'])))); 600 701 if ($this->atts_show['method'] == 'api' || $this->atts_show['method'] == 'api-dynamic') { 601 702 if (empty($this->atts_show['club_id']) && ((!$this->is_competition() && !$this->is_team() && !$this->atts_show['show'] == 'fixture') || $this->atts_show['method'] == 'api-dynamic')) { … … 621 722 $valid = false; 622 723 } 623 if ($show == 'league' && !$this->is_competition() ) {724 if ($show == 'league' && !$this->is_competition() && empty($this->multiple_teams)) { 624 725 $message = sprintf(esc_html__( 'Specific competition is required to show league (select team first).', 'eh-gms-feed' )); 625 726 $this->not_valid .= '<p>'.$message.'</p>'; … … 646 747 } 647 748 } 749 $team_count = count($this->multiple_teams); 750 if ($team_count > 1 && ($this->atts_show['show'] != 'league' || $team_count > $this->teamlimit || $this->atts_show['method'] == 'api-dynamic')) { 751 $message = sprintf(esc_html__( 'You can only show a static league table with multiple teams (up to %s allowed).', 'eh-gms-feed' ), $this->teamlimit); 752 $this->not_valid .= '<p>'.$message.'</p>'; 753 $valid = false; 754 } 648 755 return $valid; 649 756 } … … 660 767 661 768 public function enqueue_scripts () { 662 wp_register_script('EH_GMS_Feed', plugin_dir_url(__DIR__).'js/eh-gms-feed.js', ['jquery'], '3.1 1', true);769 wp_register_script('EH_GMS_Feed', plugin_dir_url(__DIR__).'js/eh-gms-feed.js', ['jquery'], '3.12', true); 663 770 wp_localize_script('EH_GMS_Feed', 'eh_gms_feed_ajax_obj', ['ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce('get_gms_table')]); 664 wp_register_style('EH_GMS_Feed', plugin_dir_url(__DIR__).'css/gms-default.css',[],'3.1 1');771 wp_register_style('EH_GMS_Feed', plugin_dir_url(__DIR__).'css/gms-default.css',[],'3.12'); 665 772 if ($this->settings->default_css == 'yes') wp_enqueue_style('EH_GMS_Feed'); 666 773 } -
eh-gms-feed/tags/3.12/js/eh-gms-feed.js
r3060038 r3064521 83 83 var team = filter ? filter.find('select[name="team"]').val() : null; 84 84 var club_id = (wrapper.data('club_id') && !team) ? wrapper.data('club_id') : null; 85 var back_btns = []; 85 86 jQuery.post(eh_gms_feed_ajax_obj.ajax_url, { _ajax_nonce: eh_gms_feed_ajax_obj.nonce, action: "get_gms_table", method: 'api', show: 'fixture', club_id: club_id, options: 'id:' + fixture }) 86 87 .always(function() { … … 93 94 detail[0].scrollIntoView(); 94 95 if (filter && !team) club_team(message, filter, wrapper, detail); 95 var goback = jQuery('<button type="button" class="gms-button">Go Back</button>').appendTo(detail); 96 goback.on('click', function(e) { 97 detail.hide(); 98 if (filter) filter.show(); 99 wrapper.show(); 100 fixturelink.scrollIntoView({ behavior: 'smooth', block: 'center' }); 101 }); 96 var fixture_card = detail[0].querySelector('.gms-card'); 97 if (fixture_card) { 98 fixture_card.insertAdjacentHTML('afterbegin', '<button type="button" class="gms-cancel">×</button>'); 99 back_btns.push(fixture_card.firstElementChild); 100 } 102 101 if (data.details && data.details.venue) { 103 var fixture_card = detail[0].querySelector('.gms-card');104 102 if (fixture_card) { 105 103 fixture_card.insertAdjacentHTML('beforeend', '<div></div>'); … … 111 109 detail.html(''); 112 110 message.text(error_message); 113 if (filter) filter.show(); 114 wrapper.show(); 111 }) 112 .always(function() { 113 var goback = jQuery('<button type="button" class="gms-button">Go Back</button>').appendTo(detail); 114 back_btns.push = goback[0]; 115 back_btns.forEach(function (ele) { 116 ele.addEventListener('click', function() { 117 message.hide(); 118 detail.hide(); 119 if (filter) filter.show(); 120 wrapper.show(); 121 fixturelink.scrollIntoView({ behavior: 'smooth', block: 'center' }); 122 }); 123 }); 115 124 }); 116 125 }); … … 141 150 if (ele.attr('data-request') != 'true') { 142 151 ele.attr('data-request','true'); 143 var message = jQuery('<p style="text-align: center; display: none; scroll-margin-top: 1rem "></p>').insertBefore(ele);152 var message = jQuery('<p style="text-align: center; display: none; scroll-margin-top: 1rem; margin-bottom: 1rem"></p>').insertBefore(ele); 144 153 var loader = jQuery('<div class="gms-loader" style="display: none"></div>').insertBefore(ele); 145 154 message.text('Retrieving GMS Table'); -
eh-gms-feed/tags/3.12/readme.txt
r3060038 r3064521 3 3 Tags: hockey, GMS 4 4 Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&[email protected]¤cy_code=GBP&item_name=Donation+for+EH+GMS+Feed 5 Tested up to: 6. 45 Tested up to: 6.5 6 6 Requires PHP: 5.3 7 Stable tag: 3.1 17 Stable tag: 3.12 8 8 License: GPLv2 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 62 62 63 63 == Changelog == 64 65 = 3.12 = 66 * Capability to show custom league table for selected teams 67 * Extra cancel button to go back from fixture details 68 * Change to default CSS for custom league table and cancel button 69 * Tweak to message display on network failures 64 70 65 71 = 3.11 = -
eh-gms-feed/trunk/css/gms-default.css
r3060038 r3064521 11 11 /* This styling sets alignment for specific tables */ 12 12 .gms-table-results th:nth-child(4), .gms-table-results td:nth-child(4) { text-align: center } 13 .gms-table-league th:not(:first-child) { min-width: 5em }14 13 .gms-table-league td:nth-child(n+3), .gms-table-league th:nth-child(n+3) { text-align: right } 15 16 /* This ensures the 'Date' does not wrap on the fixtures or results table */ 17 .gms-table-fixtures td:first-child, .gms-table-results td:first-child { white-space: nowrap; } 14 .gms-table-fixtures td:first-child, .gms-table-results td:first-child, .gms-table-league.gms-table-custom td:last-child { white-space: nowrap } 18 15 19 16 /* The following will highlight team or club win, draw and loss on results */ 17 .gms-form { display: inline-block; width: 1.25rem; text-align: center } 20 18 .gms-win { background-color: green } 21 19 .gms-draw { background-color: dimgray } … … 30 28 .gms-list > * + * { margin-top: 1.5rem; margin-bottom: 1.5rem } 31 29 .gms-list + .gms-footnote { margin-top: 1rem } 32 .gms-card { text-align: center; padding: 3px 3px; border-radius: 0.5rem }30 .gms-card { text-align: center; padding: 3px 3px; border-radius: 0.5rem; position: relative } 33 31 .gms-card, .gms-cardtime, .gms-cardnoscore, .gms-fixturedetails > div { border: 1px solid; border-color: silver } 34 .gms-cardtitle { font-weight: 700; font-size:larger } 32 .gms-cardtitle, .gms-cancel, .gms-card .gms-directions-card { font-weight: 700; font-size: larger } 33 .gms-cancel { position: absolute; right: 0.5rem; top: 0 } 35 34 .gms-cardfixture, .gms-cardscorers { display: flex; gap: 0.5rem; align-items: center } 36 35 .gms-cardhome { flex: 1 1 0%; text-align: right } -
eh-gms-feed/trunk/eh-gms-feed.php
r3060038 r3064521 4 4 * Plugin URI: https://wordpress.org/plugins/eh-gms-feed/ 5 5 * Description: Show fixtures, results and tables from England Hockey GMS 6 * Version: 3.1 16 * Version: 3.12 7 7 * Author: Matthew Treherne 8 8 * Author URI: https://profiles.wordpress.org/mtreherne -
eh-gms-feed/trunk/includes/main-class.php
r3060038 r3064521 10 10 class EH_GMS_Feed { 11 11 12 private $basename, $settings, $atts_show, $not_valid, $filter_match, $table_name, $fixture_data, $display_name, $show_gender, $show_list, $show_cal, $cal_summary, $id, $show_detail, $directions ;12 private $basename, $settings, $atts_show, $not_valid, $filter_match, $table_name, $fixture_data, $display_name, $show_gender, $show_list, $show_cal, $cal_summary, $id, $show_detail, $directions, $multiple_teams = [], $isLaravel = false, $teamlimit = 12; 13 13 private $club_link, $team_link, $comp_link, $team_comps, $comp_name; 14 14 private $args = ['method','show','team','comp_id','club_id','whatson','link','sort_by','options']; … … 64 64 } 65 65 66 private function get_json($key, $keys=[] ) {66 private function get_json($key, $keys=[], $raw=false) { 67 67 $keys = (array) $keys; $valid_json = true; 68 68 $headers = ['headers' => ['Content-Type' => 'application/json;charset=UTF-8', 'x-functions-key' => $this->settings->gms_api_key]]; … … 98 98 } 99 99 } 100 if ($raw) return $valid_json ? $body : null; 100 101 if ($valid_json) { 101 102 $data = json_decode($body); … … 151 152 152 153 private function show_api_error ($message) { 153 return '<p>'. sprintf(esc_html__( '%1$s', 'eh-gms-feed' ), $message).'</p>';154 return '<p>'.$message.'</p>'; 154 155 } 155 156 … … 283 284 } 284 285 $html .= '<div class="gms-card'.($this->show_detail ? ' gms-detaillink' : '').'"'.($this->show_detail ? ' data-fixture="'.$fixture->id.'"': '').'>' 285 .($show_detail ? '<div class="gms-cardtitle">Fixture Details</div class="gms-cardcomp"><div>'.$fixture->competitionName.'</div>': '')286 .($show_detail ? '<div class="gms-cardtitle">Fixture Details</div><div class="gms-cardcomp">'.$fixture->competitionName.'</div>': '') 286 287 .'<div class="gms-carddate">'.$fixture_card['Date'].'</div>' 287 288 .'<div class="gms-cardfixture"><div class="gms-cardhome"><div '.implode(' ',$home_attr).'>'.$fixture_card['Home Team'].'</div></div><div'.$divide_attr.'>'.$divide.'</div><div class="gms-cardaway"><div '.implode(' ',$away_attr).'>'.$fixture_card['Away Team'].'</div></div></div>' … … 359 360 } 360 361 361 private function process_table ($data ) {362 private function process_table ($data, $custom=false) { 362 363 $html = ''; $link = $this->comp_link; 363 364 $league_data = ['Team' => 'teamName', 'Played' => 'gamesPlayed', 'Won' => 'gamesWon', 'Drawn' => 'gamesDrawn', 'Lost' => 'gamesLost', 'For' => 'goalsFor', 'Against' => 'goalsAgainst', 'GD' => 'goalsDifference', 'Points' => 'totalPoints']; 364 $html .= '<table class="gms-table gms-table-'.$this->display_name.'"><thead><tr><th></th>'; 365 if ($custom) { $league_data['PPG'] = 'PPG'; $league_data['Form'] = 'form'; } 366 $html .= '<table class="gms-table gms-table-'.$this->display_name.($custom ? ' gms-table-custom' : '').'"><thead><tr><th></th>'; 365 367 foreach ($league_data as $column_title => $field_name) $html .= '<th>'.$column_title.'</th>'; 366 368 $html .= '</tr></thead><tbody>'; 367 369 foreach ($data as $position => $team) { 368 370 $class = ''; 369 if ( $team->teamId == $this->atts_show['team']) {371 if (!$custom && $team->teamId == $this->atts_show['team']) { 370 372 $class = ' class="gms-clubteam"'; 371 373 if (!$link) $link = $this->get_link('team', $team->teamEntityUrlSlug); 372 374 } 373 375 $html .= '<tr'.$class.'><td>'.($position+1).'</td>'; 374 foreach ($league_data as $field_name) $html .= '<td>'.(isset($field_name, $team->{$field_name}) ? esc_html($team->{$field_name}) : '').'</td>'; 376 foreach ($league_data as $field_name) { 377 if ($field_name == 'form') { 378 if (count($team->form) > 5) $team->form = array_slice($team->form, -5, 5); 379 $team->form = implode(' ', $team->form); 380 $html .= '<td>'.$team->form.'</td>'; 381 } 382 else $html .= '<td>'.(isset($field_name, $team->{$field_name}) ? esc_html($team->{$field_name}) : '').'</td>'; 383 } 375 384 $html .= '</tr>'; 376 385 } 386 if (empty($data)) $html .= '<tr><td colspan="'.(count($league_data)+1).'">No data for teams found</td></tr>'; 377 387 $html .= '</tbody></table>'; 378 $html .= '<div class="gms-footnote">'.($link ? sprintf(esc_html__( '[ Table%3$s %1$s also found at%4$s %2$s]', 'eh-gms-feed' ), $this->comp_name ? 'for '.$this->comp_name : '', '<a href="'.$link.'" target="_blank">England Hockey Link</a>', '<span class="gms-nomobile">', '</span>') : '').'</div>';388 $html .= '<div class="gms-footnote">'.($link ? sprintf(esc_html__( '[%5$s%3$s %1$s also found at%4$s %2$s]', 'eh-gms-feed' ), $this->comp_name ? 'for '.$this->comp_name : '', '<a href="'.$link.'" target="_blank">England Hockey Link</a>', '<span class="gms-nomobile">', '</span>', ($custom ? esc_html__('Data') : esc_html__('Table'))) : ($custom ? esc_html__('[League table for selected teams]') : '')).'</div>'; 379 389 return $html; 380 390 } … … 503 513 $this->fixture_data = ['Date' => 'fixtureDate', 'Time' => 'fixtureTime', 'Home Team' => ['homeTeam','teamName'], 'Score' => ['homeTeamScore', 'awayTeamScore'], 'Away Team' => ['awayTeam','teamName'], 'Venue' => 'venue']; 504 514 if ($table_name == 'fixtures') unset($this->fixture_data['Score']); 505 if ($table_name == 'league' && $this->is_competition()) {515 if ($table_name == 'league') { 506 516 if ($this->atts_show['link']) $this->comp_link = $this->atts_show['link']; 507 $html .= $this->get_json('competition', $this->atts_show['comp_id']); 517 if ($this->is_competition()) $html .= $this->get_json('competition', $this->atts_show['comp_id']); 518 else $html .= $this->multiple_teams(); 508 519 } 509 520 elseif (($table_name == 'fixtures' || $table_name == 'results' || $table_name == 'combined') && $this->is_team()) { … … 542 553 foreach ($data as $competition) $competitions[$competition->competitionId] = ['name' => $competition->competitionName, 'slug' => $competition->competitionEntityUrlSlug]; 543 554 return $competitions; 555 } 556 557 private function multiple_teams() { 558 $html = ''; 559 $api = 'fixturesandresults'; 560 $json_data = []; 561 $required_data = []; 562 $no_data = 0; 563 // Try the default data cache first 564 foreach ($this->multiple_teams as $teamID) { 565 $body = get_transient('eh_gms_feed '.$this->api[$api]['transient'].' '.$teamID); 566 if ($body === false) $required_data[] = $teamID; 567 else $json_data[$teamID] = $body; 568 } 569 // If Laravel then use Guzzle and do requests in parallel 570 if ($this->isLaravel) { 571 $refreshlist = collect(); 572 foreach ($required_data as $teamID) { 573 $object = new \stdClass; 574 $object->api = $api; 575 $object->api_key = $teamID; 576 $refreshlist->push($object); 577 } 578 if ($refreshlist->isNotEmpty()) $this->refresh_cache($refreshlist); 579 } 580 foreach ($required_data as $teamID) { 581 $json_data[$teamID] = $this->get_json($api, $teamID, true); 582 } 583 if (!empty($json_data)) { 584 $league = []; 585 foreach ($json_data as $key => $snapshot) { 586 if ($snapshot && ($data = json_decode($snapshot)) && json_last_error() === JSON_ERROR_NONE && !empty($data)) { 587 $teamname = $next_fixture_date = null; 588 $gp = $gw = $gd = $gl = $gf = $ga = 0; 589 $form = []; 590 foreach ($data as $competition) { 591 foreach ($competition->fixtures as $fixture) { 592 $isHome = $fixture->homeTeamId == $key; 593 if (!$teamname) $teamname = $isHome ? $fixture->homeTeam->teamName : $fixture->awayTeam->teamName; 594 if ($fixture->isBye) continue; 595 $valid_date = new \DateTime($fixture->fixtureDate); 596 if ($fixture->isResult) { 597 if ($fixture->status == 'Postponed' || ($fixture->homeTeamScore == 'P' && $fixture->awayTeamScore == 'P')) continue; 598 elseif ($fixture->status == 'Cancelled' || ($fixture->homeTeamScore == 'C' && $fixture->awayTeamScore == 'C')) continue; 599 else { 600 $gp++; 601 $gf += $isHome ? $fixture->homeTeamScoreAsInt : $fixture->awayTeamScoreAsInt; 602 $ga += $isHome ? $fixture->awayTeamScoreAsInt : $fixture->homeTeamScoreAsInt; 603 if ($fixture->homeTeamScoreAsInt == $fixture->awayTeamScoreAsInt) { $gd++; $form[] = '<span class="gms-form gms-draw">D</span>'; } 604 else if (($isHome && $fixture->homeTeamScoreAsInt > $fixture->awayTeamScoreAsInt) || (!$isHome && $fixture->homeTeamScoreAsInt < $fixture->awayTeamScoreAsInt)) { $gw++; $form[] = '<span class="gms-form gms-win">W</span>'; } 605 else { $gl++; $form[] = '<span class="gms-form gms-loss">L</span>'; } 606 } 607 } 608 elseif ($this->is_beforetoday($valid_date)) continue; 609 elseif (!$next_fixture_date || $valid_date < $next_fixture_date) $next_fixture_date = $valid_date; 610 } 611 } 612 if ($teamname) { 613 $points = $gw * 3 + $gd; 614 $object = new \stdClass; 615 $object->teamId = $key; 616 $object->teamName = $teamname; 617 $object->gamesPlayed = $gp; 618 $object->gamesWon = $gw; 619 $object->gamesDrawn = $gd; 620 $object->gamesLost = $gl; 621 $object->goalsFor = $gf; 622 $object->goalsAgainst = $ga; 623 $object->goalsDifference = $gf - $ga; 624 $object->totalPoints = $points; 625 $object->PPG = number_format($gp ? $points/$gp : 0, 2, '.', ''); 626 $object->form = $form; 627 $league[] = $object; 628 } 629 } 630 else $no_data++; 631 } 632 usort($league, function ($a,$b) { 633 if ($a->PPG != $b->PPG) return $a->PPG < $b->PPG ? 1 : -1; 634 if ($a->goalsDifference != $b->goalsDifference) return $a->goalsDifference < $b->goalsDifference ? 1 : -1; 635 if ($a->goalsFor != $b->goalsFor) return $a->goalsFor < $b->goalsFor ? 1 : -1; 636 if ($a->gamesWon != $b->gamesWon) return $a->gamesWon < $b->gamesWon ? 1 : -1; 637 return 0; 638 }); 639 $html .= $this->process_table($league, true); 640 if ($no_data) $html .= $this->show_api_error(sprintf(esc_html__( 'Failed to retrieve data for %s of selected team(s)', 'eh-gms-feed' ), $no_data)); 641 } 642 else $html .= $this->show_api_error(esc_html__( 'You have selected a custom league table without any teams', 'eh-gms-feed' )); 643 return $html; 544 644 } 545 645 … … 598 698 $this->atts_show['sort_by'] = sanitize_text_field( $this->atts_show['sort_by'] ); 599 699 $this->atts_show['options'] = sanitize_text_field( $this->atts_show['options'] ); 700 $this->multiple_teams = array_unique(array_filter(array_map('trim', explode('+', $this->atts_show['team'])))); 600 701 if ($this->atts_show['method'] == 'api' || $this->atts_show['method'] == 'api-dynamic') { 601 702 if (empty($this->atts_show['club_id']) && ((!$this->is_competition() && !$this->is_team() && !$this->atts_show['show'] == 'fixture') || $this->atts_show['method'] == 'api-dynamic')) { … … 621 722 $valid = false; 622 723 } 623 if ($show == 'league' && !$this->is_competition() ) {724 if ($show == 'league' && !$this->is_competition() && empty($this->multiple_teams)) { 624 725 $message = sprintf(esc_html__( 'Specific competition is required to show league (select team first).', 'eh-gms-feed' )); 625 726 $this->not_valid .= '<p>'.$message.'</p>'; … … 646 747 } 647 748 } 749 $team_count = count($this->multiple_teams); 750 if ($team_count > 1 && ($this->atts_show['show'] != 'league' || $team_count > $this->teamlimit || $this->atts_show['method'] == 'api-dynamic')) { 751 $message = sprintf(esc_html__( 'You can only show a static league table with multiple teams (up to %s allowed).', 'eh-gms-feed' ), $this->teamlimit); 752 $this->not_valid .= '<p>'.$message.'</p>'; 753 $valid = false; 754 } 648 755 return $valid; 649 756 } … … 660 767 661 768 public function enqueue_scripts () { 662 wp_register_script('EH_GMS_Feed', plugin_dir_url(__DIR__).'js/eh-gms-feed.js', ['jquery'], '3.1 1', true);769 wp_register_script('EH_GMS_Feed', plugin_dir_url(__DIR__).'js/eh-gms-feed.js', ['jquery'], '3.12', true); 663 770 wp_localize_script('EH_GMS_Feed', 'eh_gms_feed_ajax_obj', ['ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce('get_gms_table')]); 664 wp_register_style('EH_GMS_Feed', plugin_dir_url(__DIR__).'css/gms-default.css',[],'3.1 1');771 wp_register_style('EH_GMS_Feed', plugin_dir_url(__DIR__).'css/gms-default.css',[],'3.12'); 665 772 if ($this->settings->default_css == 'yes') wp_enqueue_style('EH_GMS_Feed'); 666 773 } -
eh-gms-feed/trunk/js/eh-gms-feed.js
r3060038 r3064521 83 83 var team = filter ? filter.find('select[name="team"]').val() : null; 84 84 var club_id = (wrapper.data('club_id') && !team) ? wrapper.data('club_id') : null; 85 var back_btns = []; 85 86 jQuery.post(eh_gms_feed_ajax_obj.ajax_url, { _ajax_nonce: eh_gms_feed_ajax_obj.nonce, action: "get_gms_table", method: 'api', show: 'fixture', club_id: club_id, options: 'id:' + fixture }) 86 87 .always(function() { … … 93 94 detail[0].scrollIntoView(); 94 95 if (filter && !team) club_team(message, filter, wrapper, detail); 95 var goback = jQuery('<button type="button" class="gms-button">Go Back</button>').appendTo(detail); 96 goback.on('click', function(e) { 97 detail.hide(); 98 if (filter) filter.show(); 99 wrapper.show(); 100 fixturelink.scrollIntoView({ behavior: 'smooth', block: 'center' }); 101 }); 96 var fixture_card = detail[0].querySelector('.gms-card'); 97 if (fixture_card) { 98 fixture_card.insertAdjacentHTML('afterbegin', '<button type="button" class="gms-cancel">×</button>'); 99 back_btns.push(fixture_card.firstElementChild); 100 } 102 101 if (data.details && data.details.venue) { 103 var fixture_card = detail[0].querySelector('.gms-card');104 102 if (fixture_card) { 105 103 fixture_card.insertAdjacentHTML('beforeend', '<div></div>'); … … 111 109 detail.html(''); 112 110 message.text(error_message); 113 if (filter) filter.show(); 114 wrapper.show(); 111 }) 112 .always(function() { 113 var goback = jQuery('<button type="button" class="gms-button">Go Back</button>').appendTo(detail); 114 back_btns.push = goback[0]; 115 back_btns.forEach(function (ele) { 116 ele.addEventListener('click', function() { 117 message.hide(); 118 detail.hide(); 119 if (filter) filter.show(); 120 wrapper.show(); 121 fixturelink.scrollIntoView({ behavior: 'smooth', block: 'center' }); 122 }); 123 }); 115 124 }); 116 125 }); … … 141 150 if (ele.attr('data-request') != 'true') { 142 151 ele.attr('data-request','true'); 143 var message = jQuery('<p style="text-align: center; display: none; scroll-margin-top: 1rem "></p>').insertBefore(ele);152 var message = jQuery('<p style="text-align: center; display: none; scroll-margin-top: 1rem; margin-bottom: 1rem"></p>').insertBefore(ele); 144 153 var loader = jQuery('<div class="gms-loader" style="display: none"></div>').insertBefore(ele); 145 154 message.text('Retrieving GMS Table'); -
eh-gms-feed/trunk/readme.txt
r3060038 r3064521 3 3 Tags: hockey, GMS 4 4 Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&[email protected]¤cy_code=GBP&item_name=Donation+for+EH+GMS+Feed 5 Tested up to: 6. 45 Tested up to: 6.5 6 6 Requires PHP: 5.3 7 Stable tag: 3.1 17 Stable tag: 3.12 8 8 License: GPLv2 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 62 62 63 63 == Changelog == 64 65 = 3.12 = 66 * Capability to show custom league table for selected teams 67 * Extra cancel button to go back from fixture details 68 * Change to default CSS for custom league table and cancel button 69 * Tweak to message display on network failures 64 70 65 71 = 3.11 =
Note: See TracChangeset
for help on using the changeset viewer.