Plugin Directory

Changeset 3064521


Ignore:
Timestamp:
04/04/2024 09:55:04 AM (23 months ago)
Author:
mtreherne
Message:

Release of Version 3.12

Location:
eh-gms-feed
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • eh-gms-feed/tags/3.12/css/gms-default.css

    r3060038 r3064521  
    1111/* This styling sets alignment for specific tables */
    1212.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 }
    1413.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 }
    1815
    1916/* 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 }
    2018.gms-win { background-color: green }
    2119.gms-draw { background-color: dimgray }
     
    3028.gms-list > * + * { margin-top: 1.5rem; margin-bottom: 1.5rem }
    3129.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 }
    3331.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 }
    3534.gms-cardfixture, .gms-cardscorers { display: flex; gap: 0.5rem; align-items: center }
    3635.gms-cardhome { flex: 1 1 0%; text-align: right }
  • eh-gms-feed/tags/3.12/eh-gms-feed.php

    r3060038 r3064521  
    44 * Plugin URI: https://wordpress.org/plugins/eh-gms-feed/
    55 * Description: Show fixtures, results and tables from England Hockey GMS
    6  * Version: 3.11
     6 * Version: 3.12
    77 * Author: Matthew Treherne
    88 * Author URI: https://profiles.wordpress.org/mtreherne
  • eh-gms-feed/tags/3.12/includes/main-class.php

    r3060038 r3064521  
    1010class EH_GMS_Feed {
    1111
    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;
    1313    private $club_link, $team_link, $comp_link, $team_comps, $comp_name;
    1414    private $args = ['method','show','team','comp_id','club_id','whatson','link','sort_by','options'];
     
    6464    }
    6565
    66     private function get_json($key, $keys=[]) {
     66    private function get_json($key, $keys=[], $raw=false) {
    6767    $keys = (array) $keys; $valid_json = true;
    6868        $headers = ['headers' => ['Content-Type' => 'application/json;charset=UTF-8', 'x-functions-key' => $this->settings->gms_api_key]];
     
    9898            }
    9999        }
     100        if ($raw) return $valid_json ? $body : null;
    100101        if ($valid_json) {
    101102            $data = json_decode($body);
     
    151152
    152153    private function show_api_error ($message) {
    153         return '<p>'.sprintf(esc_html__( '%1$s', 'eh-gms-feed' ), $message).'</p>';
     154        return '<p>'.$message.'</p>';
    154155    }
    155156
     
    283284            }
    284285            $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>': '')
    286287                .'<div class="gms-carddate">'.$fixture_card['Date'].'</div>'
    287288                .'<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>'
     
    359360    }
    360361
    361     private function process_table ($data) {
     362    private function process_table ($data, $custom=false) {
    362363        $html = ''; $link = $this->comp_link;
    363364        $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>';
    365367        foreach ($league_data as $column_title => $field_name) $html .= '<th>'.$column_title.'</th>';
    366368        $html .= '</tr></thead><tbody>';
    367369        foreach ($data as $position => $team) {
    368370            $class = '';
    369             if ($team->teamId == $this->atts_show['team']) {
     371            if (!$custom && $team->teamId == $this->atts_show['team']) {
    370372                $class = ' class="gms-clubteam"';
    371373                if (!$link) $link = $this->get_link('team', $team->teamEntityUrlSlug);
    372374            }
    373375            $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            }
    375384            $html .= '</tr>';
    376385        }
     386        if (empty($data)) $html .= '<tr><td colspan="'.(count($league_data)+1).'">No data for teams found</td></tr>';
    377387        $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>';
    379389        return $html;
    380390    }
     
    503513            $this->fixture_data = ['Date' => 'fixtureDate', 'Time' => 'fixtureTime', 'Home Team' => ['homeTeam','teamName'], 'Score' => ['homeTeamScore', 'awayTeamScore'], 'Away Team' => ['awayTeam','teamName'], 'Venue' => 'venue'];
    504514            if ($table_name == 'fixtures') unset($this->fixture_data['Score']);
    505             if ($table_name == 'league' && $this->is_competition()) {
     515            if ($table_name == 'league') {
    506516                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();
    508519            }
    509520            elseif (($table_name == 'fixtures' || $table_name == 'results' || $table_name == 'combined') && $this->is_team()) {
     
    542553        foreach ($data as $competition) $competitions[$competition->competitionId] = ['name' => $competition->competitionName, 'slug' => $competition->competitionEntityUrlSlug];
    543554        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;
    544644    }
    545645
     
    598698        $this->atts_show['sort_by'] = sanitize_text_field( $this->atts_show['sort_by'] );
    599699        $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']))));
    600701        if ($this->atts_show['method'] == 'api' || $this->atts_show['method'] == 'api-dynamic') {
    601702            if (empty($this->atts_show['club_id']) && ((!$this->is_competition() && !$this->is_team() && !$this->atts_show['show'] == 'fixture') || $this->atts_show['method'] == 'api-dynamic')) {
     
    621722                $valid = false;
    622723            }
    623             if ($show == 'league' && !$this->is_competition()) {
     724            if ($show == 'league' && !$this->is_competition() && empty($this->multiple_teams)) {
    624725                $message = sprintf(esc_html__( 'Specific competition is required to show league (select team first).', 'eh-gms-feed' ));
    625726                $this->not_valid .= '<p>'.$message.'</p>';
     
    646747            }
    647748        }
     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        }
    648755        return $valid;
    649756    }
     
    660767
    661768    public function enqueue_scripts () {
    662         wp_register_script('EH_GMS_Feed', plugin_dir_url(__DIR__).'js/eh-gms-feed.js', ['jquery'], '3.11', true);
     769        wp_register_script('EH_GMS_Feed', plugin_dir_url(__DIR__).'js/eh-gms-feed.js', ['jquery'], '3.12', true);
    663770        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.11');
     771        wp_register_style('EH_GMS_Feed', plugin_dir_url(__DIR__).'css/gms-default.css',[],'3.12');
    665772        if ($this->settings->default_css == 'yes') wp_enqueue_style('EH_GMS_Feed');
    666773    }
  • eh-gms-feed/tags/3.12/js/eh-gms-feed.js

    r3060038 r3064521  
    8383                var team = filter ? filter.find('select[name="team"]').val() : null;
    8484                var club_id = (wrapper.data('club_id') && !team) ? wrapper.data('club_id') : null;
     85                var back_btns = [];
    8586                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 })
    8687                    .always(function() {
     
    9394                        detail[0].scrollIntoView();
    9495                        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                        }
    102101                        if (data.details && data.details.venue) {
    103                             var fixture_card = detail[0].querySelector('.gms-card');
    104102                            if (fixture_card) {
    105103                                fixture_card.insertAdjacentHTML('beforeend', '<div></div>');
     
    111109                        detail.html('');
    112110                        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                        });
    115124                    });
    116125            });
     
    141150        if (ele.attr('data-request') != 'true') {
    142151            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);
    144153            var loader = jQuery('<div class="gms-loader" style="display: none"></div>').insertBefore(ele);
    145154            message.text('Retrieving GMS Table');
  • eh-gms-feed/tags/3.12/readme.txt

    r3060038 r3064521  
    33Tags: hockey, GMS
    44Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&[email protected]&currency_code=GBP&item_name=Donation+for+EH+GMS+Feed
    5 Tested up to: 6.4
     5Tested up to: 6.5
    66Requires PHP: 5.3
    7 Stable tag: 3.11
     7Stable tag: 3.12
    88License: GPLv2
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6262
    6363== 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
    6470
    6571= 3.11 =
  • eh-gms-feed/trunk/css/gms-default.css

    r3060038 r3064521  
    1111/* This styling sets alignment for specific tables */
    1212.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 }
    1413.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 }
    1815
    1916/* 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 }
    2018.gms-win { background-color: green }
    2119.gms-draw { background-color: dimgray }
     
    3028.gms-list > * + * { margin-top: 1.5rem; margin-bottom: 1.5rem }
    3129.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 }
    3331.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 }
    3534.gms-cardfixture, .gms-cardscorers { display: flex; gap: 0.5rem; align-items: center }
    3635.gms-cardhome { flex: 1 1 0%; text-align: right }
  • eh-gms-feed/trunk/eh-gms-feed.php

    r3060038 r3064521  
    44 * Plugin URI: https://wordpress.org/plugins/eh-gms-feed/
    55 * Description: Show fixtures, results and tables from England Hockey GMS
    6  * Version: 3.11
     6 * Version: 3.12
    77 * Author: Matthew Treherne
    88 * Author URI: https://profiles.wordpress.org/mtreherne
  • eh-gms-feed/trunk/includes/main-class.php

    r3060038 r3064521  
    1010class EH_GMS_Feed {
    1111
    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;
    1313    private $club_link, $team_link, $comp_link, $team_comps, $comp_name;
    1414    private $args = ['method','show','team','comp_id','club_id','whatson','link','sort_by','options'];
     
    6464    }
    6565
    66     private function get_json($key, $keys=[]) {
     66    private function get_json($key, $keys=[], $raw=false) {
    6767    $keys = (array) $keys; $valid_json = true;
    6868        $headers = ['headers' => ['Content-Type' => 'application/json;charset=UTF-8', 'x-functions-key' => $this->settings->gms_api_key]];
     
    9898            }
    9999        }
     100        if ($raw) return $valid_json ? $body : null;
    100101        if ($valid_json) {
    101102            $data = json_decode($body);
     
    151152
    152153    private function show_api_error ($message) {
    153         return '<p>'.sprintf(esc_html__( '%1$s', 'eh-gms-feed' ), $message).'</p>';
     154        return '<p>'.$message.'</p>';
    154155    }
    155156
     
    283284            }
    284285            $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>': '')
    286287                .'<div class="gms-carddate">'.$fixture_card['Date'].'</div>'
    287288                .'<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>'
     
    359360    }
    360361
    361     private function process_table ($data) {
     362    private function process_table ($data, $custom=false) {
    362363        $html = ''; $link = $this->comp_link;
    363364        $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>';
    365367        foreach ($league_data as $column_title => $field_name) $html .= '<th>'.$column_title.'</th>';
    366368        $html .= '</tr></thead><tbody>';
    367369        foreach ($data as $position => $team) {
    368370            $class = '';
    369             if ($team->teamId == $this->atts_show['team']) {
     371            if (!$custom && $team->teamId == $this->atts_show['team']) {
    370372                $class = ' class="gms-clubteam"';
    371373                if (!$link) $link = $this->get_link('team', $team->teamEntityUrlSlug);
    372374            }
    373375            $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            }
    375384            $html .= '</tr>';
    376385        }
     386        if (empty($data)) $html .= '<tr><td colspan="'.(count($league_data)+1).'">No data for teams found</td></tr>';
    377387        $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>';
    379389        return $html;
    380390    }
     
    503513            $this->fixture_data = ['Date' => 'fixtureDate', 'Time' => 'fixtureTime', 'Home Team' => ['homeTeam','teamName'], 'Score' => ['homeTeamScore', 'awayTeamScore'], 'Away Team' => ['awayTeam','teamName'], 'Venue' => 'venue'];
    504514            if ($table_name == 'fixtures') unset($this->fixture_data['Score']);
    505             if ($table_name == 'league' && $this->is_competition()) {
     515            if ($table_name == 'league') {
    506516                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();
    508519            }
    509520            elseif (($table_name == 'fixtures' || $table_name == 'results' || $table_name == 'combined') && $this->is_team()) {
     
    542553        foreach ($data as $competition) $competitions[$competition->competitionId] = ['name' => $competition->competitionName, 'slug' => $competition->competitionEntityUrlSlug];
    543554        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;
    544644    }
    545645
     
    598698        $this->atts_show['sort_by'] = sanitize_text_field( $this->atts_show['sort_by'] );
    599699        $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']))));
    600701        if ($this->atts_show['method'] == 'api' || $this->atts_show['method'] == 'api-dynamic') {
    601702            if (empty($this->atts_show['club_id']) && ((!$this->is_competition() && !$this->is_team() && !$this->atts_show['show'] == 'fixture') || $this->atts_show['method'] == 'api-dynamic')) {
     
    621722                $valid = false;
    622723            }
    623             if ($show == 'league' && !$this->is_competition()) {
     724            if ($show == 'league' && !$this->is_competition() && empty($this->multiple_teams)) {
    624725                $message = sprintf(esc_html__( 'Specific competition is required to show league (select team first).', 'eh-gms-feed' ));
    625726                $this->not_valid .= '<p>'.$message.'</p>';
     
    646747            }
    647748        }
     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        }
    648755        return $valid;
    649756    }
     
    660767
    661768    public function enqueue_scripts () {
    662         wp_register_script('EH_GMS_Feed', plugin_dir_url(__DIR__).'js/eh-gms-feed.js', ['jquery'], '3.11', true);
     769        wp_register_script('EH_GMS_Feed', plugin_dir_url(__DIR__).'js/eh-gms-feed.js', ['jquery'], '3.12', true);
    663770        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.11');
     771        wp_register_style('EH_GMS_Feed', plugin_dir_url(__DIR__).'css/gms-default.css',[],'3.12');
    665772        if ($this->settings->default_css == 'yes') wp_enqueue_style('EH_GMS_Feed');
    666773    }
  • eh-gms-feed/trunk/js/eh-gms-feed.js

    r3060038 r3064521  
    8383                var team = filter ? filter.find('select[name="team"]').val() : null;
    8484                var club_id = (wrapper.data('club_id') && !team) ? wrapper.data('club_id') : null;
     85                var back_btns = [];
    8586                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 })
    8687                    .always(function() {
     
    9394                        detail[0].scrollIntoView();
    9495                        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                        }
    102101                        if (data.details && data.details.venue) {
    103                             var fixture_card = detail[0].querySelector('.gms-card');
    104102                            if (fixture_card) {
    105103                                fixture_card.insertAdjacentHTML('beforeend', '<div></div>');
     
    111109                        detail.html('');
    112110                        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                        });
    115124                    });
    116125            });
     
    141150        if (ele.attr('data-request') != 'true') {
    142151            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);
    144153            var loader = jQuery('<div class="gms-loader" style="display: none"></div>').insertBefore(ele);
    145154            message.text('Retrieving GMS Table');
  • eh-gms-feed/trunk/readme.txt

    r3060038 r3064521  
    33Tags: hockey, GMS
    44Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&[email protected]&currency_code=GBP&item_name=Donation+for+EH+GMS+Feed
    5 Tested up to: 6.4
     5Tested up to: 6.5
    66Requires PHP: 5.3
    7 Stable tag: 3.11
     7Stable tag: 3.12
    88License: GPLv2
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6262
    6363== 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
    6470
    6571= 3.11 =
Note: See TracChangeset for help on using the changeset viewer.