From edeab146f011f8779aed529dd02ff746ee2b866c Mon Sep 17 00:00:00 2001 From: Christian Ohlsson Date: Thu, 2 Apr 2026 20:21:22 +0200 Subject: [PATCH] Startpunkten --- README.md | 0 admin/oauth-handler.php | 37 ++++++++++++++ admin/settings.php | 55 +++++++++++++++++++++ blocks/block.js | 17 +++++++ spotify-recent.php | 105 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 214 insertions(+) create mode 100644 README.md create mode 100644 admin/oauth-handler.php create mode 100644 admin/settings.php create mode 100644 blocks/block.js create mode 100644 spotify-recent.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/admin/oauth-handler.php b/admin/oauth-handler.php new file mode 100644 index 0000000..acbc001 --- /dev/null +++ b/admin/oauth-handler.php @@ -0,0 +1,37 @@ + array( + 'grant_type' => 'authorization_code', + 'code' => $_GET['code'], + 'redirect_uri' => $redirect_uri, + 'client_id' => $client_id, + 'client_secret' => $client_secret + ) + )); + + if (is_wp_error($response)) return; + + $data = json_decode(wp_remote_retrieve_body($response), true); + + if (isset($data['access_token'])) { + update_option('sro_access_token', $data['access_token']); + update_option('sro_refresh_token', $data['refresh_token']); + update_option('sro_token_expires', time() + 3500); + } + + wp_redirect(admin_url('options-general.php?page=sro')); + exit; +} +add_action('admin_init', 'sro_handle_oauth'); \ No newline at end of file diff --git a/admin/settings.php b/admin/settings.php new file mode 100644 index 0000000..b765889 --- /dev/null +++ b/admin/settings.php @@ -0,0 +1,55 @@ +

Saved.

"; + } + + $redirect_uri = admin_url('options-general.php?page=sro&oauth=1'); + ?> +

Spotify OAuth Settings

+ +
+
+

+ +
+

+ + +
+ +
+ +

Step 2: Connect Spotify

+

Add this redirect URI in your Spotify App:

+ + + +

+ + + Connect Spotify + + + 'sro-block-js', + 'render_callback' => 'sro_render_block' + )); +} +add_action('init', 'sro_register_block'); + + +/* + * Fetch recent track via Spotify API + */ +function sro_fetch_recent_track() { + + $access = get_option('sro_access_token'); + $refresh = get_option('sro_refresh_token'); + $client_id = get_option('sro_client_id'); + $client_secret = get_option('sro_client_secret'); + + // Refresh token if expired (Spotify tokens last 3600 sec) + $expires = get_option('sro_token_expires'); + if (time() > $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; + + $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]; +} + + +/* + * Render the Gutenberg block + */ +function sro_render_block() { + $item = sro_fetch_recent_track(); + if (!$item) { + return "

Spotify not connected or no recent activity.

"; + } + + $track = $item['track']['name']; + $artist = $item['track']['artists'][0]['name']; + $img = $item['track']['album']['images'][0]['url']; + + $played = strtotime($item['played_at']); + $mins = floor((time() - $played) / 60); + + return " +
+ +

$artist – $track

+

Spelades för $mins minuter sedan

+
+ "; +} \ No newline at end of file