Changeset 3102947
- Timestamp:
- 06/15/2024 01:59:05 AM (22 months ago)
- Location:
- spinitron-player
- Files:
-
- 14 added
- 5 edited
-
tags/1.0.5 (added)
-
tags/1.0.5/ajax (added)
-
tags/1.0.5/ajax/ajax-handler-today.php (added)
-
tags/1.0.5/app (added)
-
tags/1.0.5/app/plugin-settings.php (added)
-
tags/1.0.5/app/spinitron-api-client.php (added)
-
tags/1.0.5/app/spinitron-get-client.php (added)
-
tags/1.0.5/js (added)
-
tags/1.0.5/js/spinitron-fetch-today.js (added)
-
tags/1.0.5/readme.txt (added)
-
tags/1.0.5/spinitron-player.php (added)
-
tags/1.0.5/style.css (added)
-
tags/1.0.5/ui (added)
-
tags/1.0.5/ui/today.php (added)
-
trunk/ajax/ajax-handler-today.php (modified) (5 diffs)
-
trunk/app/plugin-settings.php (modified) (10 diffs)
-
trunk/app/spinitron-api-client.php (modified) (5 diffs)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/spinitron-player.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
spinitron-player/trunk/ajax/ajax-handler-today.php
r3102938 r3102947 5 5 /** 6 6 * Handles AJAX request to fetch the current Spinitron show for the 'TODAY' layout. 7 *8 7 * @return void 9 8 */ … … 24 23 } 25 24 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 } 27 45 28 46 ob_start(); 29 47 30 foreach ($shows['items'] as $show) : 31 48 foreach ($shows['items'] as $show) { 32 49 $show_title = esc_html($show['title']); 33 50 $show_image = esc_url($show['image']); 34 51 $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')); 37 54 38 $persona_url = esc_url($show['_links']['personas'][0]['href']);55 $persona_url = esc_url($show['_links']['personas'][0]['href']); 39 56 $persona_parts = explode('/', $persona_url); 40 $persona_id = end($persona_parts);57 $persona_id = end($persona_parts); 41 58 $persona_array = $client->fetch('personas', $persona_id); 42 $show_dj = esc_html($persona_array['name']);59 $show_dj = esc_html($persona_array['name']); 43 60 44 61 // Check if the image URL is valid … … 48 65 } 49 66 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'; 55 68 56 69 echo '<div class="spinitron-player">'; 57 58 // Check if the duplicate_show_image option is enabled59 70 if ($duplicate_show_image) { 60 71 echo '<img class="show-image-outter" src="' . esc_html($show_image) . '" alt="' . esc_attr($show_title) . '" />'; … … 62 73 63 74 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>'; 69 80 70 // Check if the separate_time_dj option is enabled71 81 if ($separate_time_dj) { 72 82 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>'; 74 84 } else { 75 85 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>'; … … 77 87 78 88 echo '</div></div>'; 79 80 endforeach; 89 } 81 90 82 91 $output = ob_get_clean(); -
spinitron-player/trunk/app/plugin-settings.php
r3102938 r3102947 5 5 /** 6 6 * Initializes the Spinitron Player plugin settings by registering settings, sections, and fields. 7 *8 7 * @return void 9 8 */ … … 87 86 /** 88 87 * Registers the Spinitron Player settings page in the WordPress admin area. 89 *90 88 * @return void 91 89 */ … … 103 101 /** 104 102 * Renders the HTML for the Spinitron Player options page in the admin area. 105 *106 103 * @return void 107 104 */ … … 129 126 /** 130 127 * 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 *134 128 * @param string $hook_suffix The current admin page. 135 *136 129 * @return void 137 130 */ … … 168 161 /** 169 162 * Outputs the developers section description for Spinitron Player settings. 170 *171 163 * @param array $args Configuration for the section. 172 *173 164 * @return void 174 165 */ … … 181 172 /** 182 173 * 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. 186 175 * @return void 187 176 */ … … 198 187 <p class="description"><?php esc_html_e('Find the API key at spinitron.com, under Admin > Automation & API > Control Panel.', 'spinitron-player'); ?></p> 199 188 <?php 189 190 // Clear the transient cache when the API key is updated 191 delete_transient('spinitron_show_today'); 200 192 } 201 193 202 194 /** 203 195 * 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. 207 197 * @return void 208 198 */ … … 224 214 /** 225 215 * 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. 229 217 * @return void 230 218 */ … … 244 232 /** 245 233 * 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. 249 235 * @return void 250 236 */ … … 260 246 /** 261 247 * 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. 265 249 * @return void 266 250 */ -
spinitron-player/trunk/app/spinitron-api-client.php
r3102918 r3102947 5 5 * This file defines the SpinitronApiClient class, which facilitates communication with the Spinitron API. 6 6 * 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 calls8 * and comply with rate limits.9 7 * 10 8 * PHP version 7.2 … … 28 26 class SpinitronApiClient 29 27 { 30 /**31 * Spinitron base URL.32 *33 * @var string34 */35 28 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 array43 */44 protected static $cache_timeout = array(45 'personas' => 60, // 1 minute46 'shows' => 60, // 1 minute47 'playlists' => 60, // 1 minute48 'spins' => 30,49 );50 51 /**52 * The API key used for authenticating with the Spinitron API.53 *54 * @var string55 */56 29 private $_api_key; 57 30 58 31 /** 59 32 * Constructor for the SpinitronApiClient class. 60 *61 * Sets up the API client using the provided API key.62 *63 33 * @param string $_api_key The API key for Spinitron. 64 34 */ … … 70 40 /** 71 41 * 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 *76 42 * @param string $endpoint e.g., 'spins', 'shows'... This is the specific API endpoint to query. 77 43 * @param array $params e.g., ['playlist_id' => 1234, 'page' => 2]. These are the search parameters for the query. 78 *79 44 * @throws \Exception If the request to the API fails or if the response cannot be decoded. 80 *81 45 * @return array Response with an array of resources of the endpoint's type plus metadata. 82 46 */ … … 88 52 } 89 53 90 return json_decode($this->query Cached($endpoint,$url), true);54 return json_decode($this->queryApi($url), true); 91 55 } 92 56 93 57 /** 94 58 * Request a resource from an endpoint using its ID. 95 *96 59 * @param string $endpoint e.g. 'shows', 'personas', ... 97 60 * @param int $id e.g. 2344. 98 *99 61 * @throws \Exception If the request fails. 100 *101 62 * @return array Response with one resource of the endpoint's type plus metadata. 102 63 */ … … 105 66 $url = '/' . $endpoint . '/' . $id; 106 67 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); 141 69 } 142 70 143 71 /** 144 72 * Queries the API and returns the response JSON document. 145 *146 73 * @param string $url The API endpoint URL. 147 *148 74 * @throws \Exception When the request fails or the response is invalid. 149 *150 75 * @return string JSON document. 151 76 */ -
spinitron-player/trunk/readme.txt
r3102938 r3102947 5 5 Tested up to: 6.5.4 6 6 Requires PHP: 7.2 7 Stable tag: 1.0. 47 Stable tag: 1.0.5 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 42 42 == Changelog == 43 43 44 = 1.0.5 = 45 - Improved API client requests 46 - Improved WordPress transients 47 - Improved AJAX request handling 48 44 49 = 1.0.4 = 45 50 - Added ui/today.php -
spinitron-player/trunk/spinitron-player.php
r3102938 r3102947 5 5 * Description: A streaming player for radio stations using Spinitron, with live data integration. 6 6 * Author: Razorfrog Web Design 7 * Version: 1.0. 47 * Version: 1.0.5 8 8 * Author URI: https://razorfrog.com/ 9 9 * License: GPLv2 or later … … 28 28 // Enqueue the plugin's CSS file 29 29 $css_url = plugin_dir_url(__FILE__) . 'style.css'; 30 $css_version = '1.0. 4';30 $css_version = '1.0.5'; 31 31 wp_enqueue_style('spinitron-player-styles', $css_url, array(), $css_version); 32 32 33 33 // Enqueue the JavaScript file for fetching show data 34 34 $js_url = plugin_dir_url(__FILE__) . 'js/spinitron-fetch-today.js'; 35 $js_version = '1.0. 21';35 $js_version = '1.0.5'; 36 36 wp_enqueue_script('spinitron-player-fetch-today', $js_url, array('jquery'), $js_version, true); 37 37 wp_localize_script('spinitron-player-fetch-today', 'spinitron_params', array(
Note: See TracChangeset
for help on using the changeset viewer.