$expires && $refresh) { $response = wp_remote_post("https://accounts.spotify.com/api/token", array( 'body' => array( 'grant_type' => 'refresh_token', 'refresh_token' => $refresh, 'client_id' => $client_id, 'client_secret' => $client_secret ) )); if (!is_wp_error($response)) { $data = json_decode(wp_remote_retrieve_body($response), true); if (isset($data['access_token'])) { $access = $data['access_token']; update_option('sro_access_token', $access); update_option('sro_token_expires', time() + 3500); } } } if (!$access) return false; // Hämta senaste låten $resp = wp_remote_get( "https://api.spotify.com/v1/me/player/recently-played?limit=1", array('headers' => array('Authorization' => "Bearer $access")) ); if (is_wp_error($resp)) return false; $data = json_decode(wp_remote_retrieve_body($resp), true); if (!isset($data['items'][0])) return false; return $data['items'][0]; } /* * Rendera blocket i frontend */ function sro_render_block() { $item = sro_fetch_recent_track(); if ( ! $item ) { return "
Spotify är inte anslutet eller så finns ingen nyligen spelad låt.
"; } $track = esc_html($item['track']['name']); $artist = esc_html($item['track']['artists'][0]['name']); $img = esc_url($item['track']['album']['images'][0]['url']); $played = strtotime($item['played_at']); $mins = floor((time() - $played) / 60); ob_start(); ?>