diff --git a/spotify-recent.php b/spotify-recent.php index 82da91c..de9e840 100644 --- a/spotify-recent.php +++ b/spotify-recent.php @@ -1,37 +1,55 @@ 'sro-block-js', - 'render_callback' => 'sro_render_block' - )); + // --- Frontend-stil --- + wp_register_style( + 'sro-style', + plugins_url('blocks/style.css', __FILE__), + array(), + filemtime(plugin_dir_path(__FILE__) . 'blocks/style.css') + ); + + // --- Editor-stil --- + wp_register_style( + 'sro-editor-style', + plugins_url('blocks/editor.css', __FILE__), + array(), + filemtime(plugin_dir_path(__FILE__) . 'blocks/editor.css') + ); + + // Registrera block via block.json + register_block_type(__DIR__ . '/blocks'); } add_action('init', 'sro_register_block'); /* - * Fetch recent track via Spotify API + * Hämta senast spelade låt via Spotify API */ function sro_fetch_recent_track() { @@ -40,7 +58,7 @@ function sro_fetch_recent_track() { $client_id = get_option('sro_client_id'); $client_secret = get_option('sro_client_secret'); - // Refresh token if expired (Spotify tokens last 3600 sec) + // Förnya token om den gått ut (Spotify token håller 3600 sek) $expires = get_option('sro_token_expires'); if (time() > $expires && $refresh) { $response = wp_remote_post("https://accounts.spotify.com/api/token", array( @@ -64,9 +82,10 @@ function sro_fetch_recent_track() { 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")) + array('headers' => array('Authorization' => "Bearer $access")) ); if (is_wp_error($resp)) return false; @@ -78,8 +97,9 @@ function sro_fetch_recent_track() { return $data['items'][0]; } + /* - * Render the Gutenberg block + * Rendera blocket i frontend */ function sro_render_block() { $item = sro_fetch_recent_track(); @@ -87,13 +107,12 @@ function sro_render_block() { 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'] ); + $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 ); - $mins = intval( $mins ); + $played = strtotime($item['played_at']); + $mins = floor((time() - $played) / 60); ob_start(); ?> @@ -104,38 +123,9 @@ function sro_render_block() {