diff --git a/blocks/block.json b/blocks/block.json new file mode 100644 index 0000000..4c4b520 --- /dev/null +++ b/blocks/block.json @@ -0,0 +1,15 @@ +{ + "apiVersion": 2, + "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_recent_track" +} diff --git a/blocks/editor.css b/blocks/editor.css new file mode 100644 index 0000000..526825c --- /dev/null +++ b/blocks/editor.css @@ -0,0 +1,4 @@ +.sro-container { + border: 1px dashed #888; + padding: 10px; +} diff --git a/blocks/style.css b/blocks/style.css new file mode 100644 index 0000000..96ad200 --- /dev/null +++ b/blocks/style.css @@ -0,0 +1,23 @@ +.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; +} diff --git a/spotify-recent.php b/spotify-recent.php index 8861d12..82da91c 100644 --- a/spotify-recent.php +++ b/spotify-recent.php @@ -78,28 +78,64 @@ function sro_fetch_recent_track() { return $data['items'][0]; } - /* * Render the Gutenberg block */ function sro_render_block() { $item = sro_fetch_recent_track(); - if (!$item) { - return "
Spotify not connected or no recent activity.
"; + if ( ! $item ) { + return "Spotify är inte anslutet eller så finns ingen nyligen spelad låt.
"; } - $track = $item['track']['name']; - $artist = $item['track']['artists'][0]['name']; - $img = $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); + $played = strtotime( $item['played_at'] ); + $mins = floor( ( time() - $played ) / 60 ); + $mins = intval( $mins ); - return " -$artist – $track
-Spelades för $mins minuter sedan
+ ob_start(); + ?> +