Gjorde en minimal version

This commit is contained in:
2026-04-03 13:07:09 +02:00
parent 4762de4c32
commit 09e078878d
4 changed files with 20 additions and 120 deletions

View File

@@ -1,17 +1,6 @@
(function (blocks, element) { (function (blocks, element) {
var el = element.createElement;
blocks.registerBlockType("spotify-recent-oauth/recent-track", { blocks.registerBlockType("spotify-recent-oauth/recent-track", {
title: "Spotify Recent Track", edit: () => element.createElement("p", {}, "Spotify Recent Track visas bara i frontend"),
icon: "format-audio", save: () => null,
category: "widgets",
edit: function () {
return el("p", {}, "Spotify Recent Track visas i frontend.");
},
save: function () {
return null; // Server-side render
},
}); });
})(window.wp.blocks, window.wp.element); })(window.wp.blocks, window.wp.element);

View File

@@ -3,13 +3,7 @@
"name": "spotify-recent-oauth/recent-track", "name": "spotify-recent-oauth/recent-track",
"title": "Spotify Recent Track", "title": "Spotify Recent Track",
"category": "widgets", "category": "widgets",
"icon": "format-audio",
"description": "Visar din senast spelade låt från Spotify.",
"supports": {
"html": false
},
"editorScript": "sro-block-js", "editorScript": "sro-block-js",
"style": "sro-style", "style": "sro-style",
"editorStyle": "sro-editor-style",
"render": "sro_render_block" "render": "sro_render_block"
} }

View File

@@ -1,23 +1,4 @@
.sro-container { .sro-container {
display: flex; display: flex;
align-items: center; gap: 10px;
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;
} }

View File

@@ -1,32 +1,26 @@
<?php <?php
/* /*
Plugin Name: Spotify Recent Oauth Plugin Name: Spotify Recent OAuth
Description: Gutenbergblock som visar den senast spelade låten från Spotify via OAuth. Description: Visar senast spelade låt från Spotify (som block).
Version: 1.1.0 Version: 1.0.0
Author: Christian Ohlsson Author: Christian Ohlsson
*/ */
if (!defined('ABSPATH')) exit; if (!defined('ABSPATH')) exit;
// Inkludera adminfiler // Ladda admin
require_once plugin_dir_path(__FILE__) . 'admin/settings.php'; require_once plugin_dir_path(__FILE__) . 'admin/settings.php';
require_once plugin_dir_path(__FILE__) . 'admin/oauth-handler.php'; require_once plugin_dir_path(__FILE__) . 'admin/oauth-handler.php';
/*
* Registrera block, script och stilmallar
*/
function sro_register_block() { function sro_register_block() {
// --- JS för blockeditorn ---
wp_register_script( wp_register_script(
'sro-block-js', 'sro-block-js',
plugins_url('blocks/block.js', __FILE__), plugins_url('blocks/block.js', __FILE__),
array('wp-blocks', 'wp-element', 'wp-i18n', 'wp-block-editor'), array('wp-blocks', 'wp-element', 'wp-block-editor'),
filemtime(plugin_dir_path(__FILE__) . 'blocks/block.js') filemtime(plugin_dir_path(__FILE__) . 'blocks/block.js')
); );
// --- Frontend-stil ---
wp_register_style( wp_register_style(
'sro-style', 'sro-style',
plugins_url('blocks/style.css', __FILE__), plugins_url('blocks/style.css', __FILE__),
@@ -34,96 +28,38 @@ function sro_register_block() {
filemtime(plugin_dir_path(__FILE__) . 'blocks/style.css') 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'); register_block_type(__DIR__ . '/blocks');
} }
add_action('init', 'sro_register_block'); add_action('init', 'sro_register_block');
/*
* Hämta senast spelade låt via Spotify API
*/
function sro_fetch_recent_track() { function sro_fetch_recent_track() {
$access = get_option('sro_access_token'); $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');
// 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(
'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; if(!$access) return false;
// Hämta senaste låten $res = wp_remote_get(
$resp = wp_remote_get(
"https://api.spotify.com/v1/me/player/recently-played?limit=1", "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; if(is_wp_error($res)) return false;
$data = json_decode(wp_remote_retrieve_body($res), true);
$data = json_decode(wp_remote_retrieve_body($resp), true); return $data['items'][0] ?? false;
if (!isset($data['items'][0])) return false;
return $data['items'][0];
} }
/*
* Rendera blocket i frontend
*/
function sro_render_block() { function sro_render_block() {
$item = sro_fetch_recent_track();
if ( ! $item ) {
return "<p>Spotify är inte anslutet eller så finns ingen nyligen spelad låt.</p>";
}
$item = sro_fetch_recent_track();
if(!$item) return "<p>Ingen nyligen spelad låt hittad.</p>";
$img = esc_url($item['track']['album']['images'][0]['url']);
$track = esc_html($item['track']['name']); $track = esc_html($item['track']['name']);
$artist = esc_html($item['track']['artists'][0]['name']); $artist = esc_html($item['track']['artists'][0]['name']);
$img = esc_url($item['track']['album']['images'][0]['url']);
$played = strtotime($item['played_at']); ob_start(); ?>
$mins = floor((time() - $played) / 60);
ob_start();
?>
<div class="sro-container"> <div class="sro-container">
<div class="sro-album"> <img src="<?php echo $img; ?>" class="sro-album" alt="">
<img src="<?php echo $img; ?>" alt="Albumomslag">
</div>
<div class="sro-info"> <div class="sro-info">
<div class="sro-track"><?php echo $track; ?></div> <div class="sro-track"><?php echo "$artist $track"; ?></div>
<div class="sro-artist"><?php echo $artist; ?></div>
<div class="sro-time">Spelades för <?php echo intval($mins); ?> minuter sedan</div>
</div> </div>
</div> </div>
<?php <?php