diff --git a/blocks/block.js b/blocks/block.js index fdae0bf..db3288c 100644 --- a/blocks/block.js +++ b/blocks/block.js @@ -1,17 +1,6 @@ (function (blocks, element) { - var el = element.createElement; - blocks.registerBlockType("spotify-recent-oauth/recent-track", { - title: "Spotify Recent Track", - icon: "format-audio", - category: "widgets", - - edit: function () { - return el("p", {}, "Spotify Recent Track – visas i frontend."); - }, - - save: function () { - return null; // Server-side render - }, + edit: () => element.createElement("p", {}, "Spotify Recent Track – visas bara i frontend"), + save: () => null, }); })(window.wp.blocks, window.wp.element); diff --git a/blocks/block.json b/blocks/block.json index 12e98a0..95fc410 100644 --- a/blocks/block.json +++ b/blocks/block.json @@ -3,13 +3,7 @@ "name": "spotify-recent-oauth/recent-track", "title": "Spotify Recent Track", "category": "widgets", - "icon": "format-audio", - "description": "Visar din senast spelade låt från Spotify.", - "supports": { - "html": false - }, "editorScript": "sro-block-js", "style": "sro-style", - "editorStyle": "sro-editor-style", "render": "sro_render_block" } diff --git a/blocks/style.css b/blocks/style.css index 96ad200..93d7ee3 100644 --- a/blocks/style.css +++ b/blocks/style.css @@ -1,23 +1,4 @@ .sro-container { display: flex; - align-items: center; - gap: 12px; - padding: 10px; - background: #111; - color: #fff; - border-radius: 8px; -} - -.sro-album img { - width: 300px; - height: 300px; - border-radius: 4px; -} - -.sro-info { - line-height: 1.2; -} - -.sro-track { - font-weight: bold; + gap: 10px; } diff --git a/spotify-recent-oauth.php b/spotify-recent-oauth.php index 5e286fe..effe592 100644 --- a/spotify-recent-oauth.php +++ b/spotify-recent-oauth.php @@ -1,32 +1,26 @@ $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( + $res = 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]; + if(is_wp_error($res)) return false; + $data = json_decode(wp_remote_retrieve_body($res), true); + return $data['items'][0] ?? false; } - -/* - * 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.

"; - } + if(!$item) return "

Ingen nyligen spelad låt hittad.

"; - $track = esc_html($item['track']['name']); + $img = esc_url($item['track']['album']['images'][0]['url']); + $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(); - ?> + ob_start(); ?>
-
- Albumomslag -
+
-
-
-
Spelades för minuter sedan
+