Plugin Directory

Changeset 3102947


Ignore:
Timestamp:
06/15/2024 01:59:05 AM (22 months ago)
Author:
razorfrog
Message:

Spinitron Player v1.0.5

Location:
spinitron-player
Files:
14 added
5 edited

Legend:

Unmodified
Added
Removed
  • spinitron-player/trunk/ajax/ajax-handler-today.php

    r3102938 r3102947  
    55/**
    66 * Handles AJAX request to fetch the current Spinitron show for the 'TODAY' layout.
    7  *
    87 * @return void
    98 */
     
    2423        }
    2524
    26         $shows = $client->search('shows', array('count' => 1, 'cache_bust' => time()));
     25        // Try to get the transient
     26        $shows = get_transient('spinitron_show_today');
     27
     28        if ($shows === false) {
     29                // If the transient doesn't exist, fetch from API
     30                $shows = $client->search('shows', array('count' => 1, 'cache_bust' => time()));
     31
     32                if (empty($shows['items'])) {
     33                        echo '<p>No shows found. Please check the Spinitron API for available shows.</p>';
     34                        wp_die();
     35                }
     36
     37                // Calculate the number of seconds until the next multiple of 5 minutes
     38                $current_time = time();
     39                $next_cache_time = ceil($current_time / 300) * 300; // 300 seconds = 5 minutes
     40                $expiration = $next_cache_time - $current_time;
     41
     42                // Set the transient
     43                set_transient('spinitron_show_today', $shows, $expiration);
     44        }
    2745
    2846        ob_start();
    2947
    30         foreach ($shows['items'] as $show) :
    31 
     48        foreach ($shows['items'] as $show) {
    3249                $show_title = esc_html($show['title']);
    3350                $show_image = esc_url($show['image']);
    3451                $show_start = (new DateTime($show['start']))->setTimezone(new DateTimeZone($show['timezone'] ?? 'America/Los_Angeles'));
    35                 $show_end   = (new DateTime($show['end']))->setTimezone(new DateTimeZone($show['timezone'] ?? 'America/Los_Angeles'));
    36                 $time_now   = (new DateTime('now'))->setTimezone(new DateTimeZone($show['timezone'] ?? 'America/Los_Angeles'));
     52                $show_end = (new DateTime($show['end']))->setTimezone(new DateTimeZone($show['timezone'] ?? 'America/Los_Angeles'));
     53                $time_now = (new DateTime('now'))->setTimezone(new DateTimeZone($show['timezone'] ?? 'America/Los_Angeles'));
    3754
    38                 $persona_url   = esc_url($show['_links']['personas'][0]['href']);
     55                $persona_url = esc_url($show['_links']['personas'][0]['href']);
    3956                $persona_parts = explode('/', $persona_url);
    40                 $persona_id    = end($persona_parts);
     57                $persona_id = end($persona_parts);
    4158                $persona_array = $client->fetch('personas', $persona_id);
    42                 $show_dj       = esc_html($persona_array['name']);
     59                $show_dj = esc_html($persona_array['name']);
    4360
    4461                // Check if the image URL is valid
     
    4865                }
    4966
    50                 if ($show_start <= $time_now && $time_now <= $show_end) {
    51                         $show_status = 'On air';
    52                 } else {
    53                         $show_status = 'Up next';
    54                 }
     67                $show_status = ($show_start <= $time_now && $time_now <= $show_end) ? 'On air' : 'Up next';
    5568
    5669                echo '<div class="spinitron-player">';
    57 
    58                 // Check if the duplicate_show_image option is enabled
    5970                if ($duplicate_show_image) {
    6071                        echo '<img class="show-image-outter" src="' . esc_html($show_image) . '" alt="' . esc_attr($show_title) . '" />';
     
    6273
    6374                echo '<div class="show-image">
    64                         <img src="' . esc_html($show_image) . '" alt="' . esc_attr($show_title) . '" />
    65                         </div>
    66                         <div class="show-details">
    67                         <p class="show-status">' . esc_html($show_status) . '</p>
    68                         <p class="show-title">' . esc_html($show_title) . '</p>';
     75                                <img src="' . esc_html($show_image) . '" alt="' . esc_attr($show_title) . '" />
     76                            </div>
     77                            <div class="show-details">
     78                                <p class="show-status">' . esc_html($show_status) . '</p>
     79                                <p class="show-title">' . esc_html($show_title) . '</p>';
    6980
    70                 // Check if the separate_time_dj option is enabled
    7181                if ($separate_time_dj) {
    7282                        echo '<p class="show-time">' . esc_html($show_start->format('g:i A')) . ' - ' . esc_html($show_end->format('g:i A')) . '</p>
    73                                 <p class="show-dj">With ' . esc_html($show_dj) . '</p>';
     83                                    <p class="show-dj">With ' . esc_html($show_dj) . '</p>';
    7484                } else {
    7585                        echo '<p class="show-time-dj">' . esc_html($show_start->format('g:i A')) . ' - ' . esc_html($show_end->format('g:i A')) . ' with ' . esc_html($show_dj) . '</p>';
     
    7787
    7888                echo '</div></div>';
    79 
    80         endforeach;
     89        }
    8190
    8291        $output = ob_get_clean();
  • spinitron-player/trunk/app/plugin-settings.php

    r3102938 r3102947  
    55/**
    66 * Initializes the Spinitron Player plugin settings by registering settings, sections, and fields.
    7  *
    87 * @return void
    98 */
     
    8786/**
    8887 * Registers the Spinitron Player settings page in the WordPress admin area.
    89  *
    9088 * @return void
    9189 */
     
    103101/**
    104102 * Renders the HTML for the Spinitron Player options page in the admin area.
    105  *
    106103 * @return void
    107104 */
     
    129126/**
    130127 * Enqueue custom styles for the Spinitron Player settings page.
    131  *
    132  * This function adds inline CSS to the admin area to style the settings table.
    133  *
    134128 * @param string $hook_suffix The current admin page.
    135  *
    136129 * @return void
    137130 */
     
    168161/**
    169162 * Outputs the developers section description for Spinitron Player settings.
    170  *
    171163 * @param array $args Configuration for the section.
    172  *
    173164 * @return void
    174165 */
     
    181172/**
    182173 * Renders the API Key input field in plugin settings.
    183  *
    184  * @param array $args Configuration for the field.
    185  *
     174 * @param array $args Configuration for the field.
    186175 * @return void
    187176 */
     
    198187        <p class="description"><?php esc_html_e('Find the API key at spinitron.com, under Admin > Automation & API > Control Panel.', 'spinitron-player'); ?></p>
    199188        <?php
     189
     190        // Clear the transient cache when the API key is updated
     191        delete_transient('spinitron_show_today');
    200192}
    201193
    202194/**
    203195 * Outputs the Image Fallback input field in plugin settings.
    204  *
    205  * @param array $args Configuration for the field.
    206  *
     196 * @param array $args Configuration for the field.
    207197 * @return void
    208198 */
     
    224214/**
    225215 * Renders the Stream URL input field in plugin settings.
    226  *
    227  * @param array $args Configuration for the field.
    228  *
     216 * @param array $args Configuration for the field.
    229217 * @return void
    230218 */
     
    244232/**
    245233 * Outputs the Separate Time and DJ checkbox in plugin settings.
    246  *
    247  * @param array $args Configuration for the field.
    248  *
     234 * @param array $args Configuration for the field.
    249235 * @return void
    250236 */
     
    260246/**
    261247 * Outputs the Duplicate Show Image checkbox in plugin settings.
    262  *
    263  * @param array $args Configuration for the field.
    264  *
     248 * @param array $args Configuration for the field.
    265249 * @return void
    266250 */
  • spinitron-player/trunk/app/spinitron-api-client.php

    r3102918 r3102947  
    55 * This file defines the SpinitronApiClient class, which facilitates communication with the Spinitron API.
    66 * It provides methods to search for and fetch data from various endpoints such as spins, shows, and playlists.
    7  * Caching functionality is implemented using WordPress transients to reduce the number of API calls
    8  * and comply with rate limits.
    97 *
    108 * PHP version 7.2
     
    2826    class SpinitronApiClient
    2927    {
    30         /**
    31          * Spinitron base URL.
    32          *
    33          * @var string
    34          */
    3528        protected $api_base_url = 'https://spinitron.com/api';
    36 
    37         /**
    38          * Cache expiration time per endpoint.
    39          *
    40          * Specifies how long each type of data should be cached before it is considered stale.
    41          *
    42          * @var array
    43          */
    44         protected static $cache_timeout = array(
    45             'personas' => 60, // 1 minute
    46             'shows' => 60, // 1 minute
    47             'playlists' => 60, // 1 minute
    48             'spins' => 30,
    49         );
    50 
    51         /**
    52          * The API key used for authenticating with the Spinitron API.
    53          *
    54          * @var string
    55          */
    5629        private $_api_key;
    5730
    5831        /**
    5932         * Constructor for the SpinitronApiClient class.
    60          *
    61          * Sets up the API client using the provided API key.
    62          *
    6333         * @param string $_api_key The API key for Spinitron.
    6434         */
     
    7040        /**
    7141         * Request resources from an endpoint using search parameters.
    72          *
    73          * This method constructs a query URL using the specified endpoint and parameters,
    74          * then makes a request to the Spinitron API and returns the response.
    75          *
    7642         * @param string $endpoint e.g., 'spins', 'shows'... This is the specific API endpoint to query.
    7743         * @param array $params e.g., ['playlist_id' => 1234, 'page' => 2]. These are the search parameters for the query.
    78          *
    7944         * @throws \Exception If the request to the API fails or if the response cannot be decoded.
    80          *
    8145         * @return array Response with an array of resources of the endpoint's type plus metadata.
    8246         */
     
    8852            }
    8953
    90             return json_decode($this->queryCached($endpoint, $url), true);
     54            return json_decode($this->queryApi($url), true);
    9155        }
    9256
    9357        /**
    9458         * Request a resource from an endpoint using its ID.
    95          *
    9659         * @param string $endpoint e.g. 'shows', 'personas', ...
    9760         * @param int $id e.g. 2344.
    98          *
    9961         * @throws \Exception If the request fails.
    100          *
    10162         * @return array Response with one resource of the endpoint's type plus metadata.
    10263         */
     
    10566            $url = '/' . $endpoint . '/' . $id;
    10667
    107             return json_decode($this->queryCached($endpoint, $url), true);
    108         }
    109 
    110         /**
    111          * Query the API with the given URL, returning the response JSON document either from
    112          * the local file cache or from the API.
    113          *
    114          * @param string $endpoint e.g. 'spins', 'shows' ...
    115          * @param string $url The API endpoint URL.
    116          *
    117          * @throws \Exception When the request fails or the response is invalid.
    118          *
    119          * @return string JSON document
    120          */
    121         protected function queryCached($endpoint, $url)
    122         {
    123             $cache_key = 'spinitron_' . md5($url); // Unique key for the cache
    124             $cached = get_transient($cache_key);
    125 
    126             if ($cached !== false) {
    127                 // Cache hit. Return the cached content.
    128                 return $cached;
    129             }
    130 
    131             // Cache miss. Request resource from the API.
    132             $response = $this->queryApi($url);
    133 
    134             // Save the response in the transient, with expiration.
    135             if ($response) {
    136                 $timeout = static::$cache_timeout[$endpoint] ?? 60;
    137                 set_transient($cache_key, $response, $timeout);
    138             }
    139 
    140             return $response;
     68            return json_decode($this->queryApi($url), true);
    14169        }
    14270
    14371        /**
    14472         * Queries the API and returns the response JSON document.
    145          *
    14673         * @param string $url The API endpoint URL.
    147          *
    14874         * @throws \Exception When the request fails or the response is invalid.
    149          *
    15075         * @return string JSON document.
    15176         */
  • spinitron-player/trunk/readme.txt

    r3102938 r3102947  
    55Tested up to: 6.5.4
    66Requires PHP: 7.2
    7 Stable tag: 1.0.4
     7Stable tag: 1.0.5
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4242== Changelog ==
    4343
     44= 1.0.5 =
     45- Improved API client requests
     46- Improved WordPress transients
     47- Improved AJAX request handling
     48
    4449= 1.0.4 =
    4550- Added ui/today.php
  • spinitron-player/trunk/spinitron-player.php

    r3102938 r3102947  
    55 * Description: A streaming player for radio stations using Spinitron, with live data integration.
    66 * Author: Razorfrog Web Design
    7  * Version: 1.0.4
     7 * Version: 1.0.5
    88 * Author URI: https://razorfrog.com/
    99 * License: GPLv2 or later
     
    2828    // Enqueue the plugin's CSS file
    2929    $css_url = plugin_dir_url(__FILE__) . 'style.css';
    30     $css_version = '1.0.4';
     30    $css_version = '1.0.5';
    3131    wp_enqueue_style('spinitron-player-styles', $css_url, array(), $css_version);
    3232
    3333    // Enqueue the JavaScript file for fetching show data
    3434    $js_url = plugin_dir_url(__FILE__) . 'js/spinitron-fetch-today.js';
    35     $js_version = '1.0.21';
     35    $js_version = '1.0.5';
    3636    wp_enqueue_script('spinitron-player-fetch-today', $js_url, array('jquery'), $js_version, true);
    3737    wp_localize_script('spinitron-player-fetch-today', 'spinitron_params', array(
Note: See TracChangeset for help on using the changeset viewer.