Skrev in för att uppfylla modern standard

This commit is contained in:
2026-04-03 09:34:32 +02:00
parent cdef12ef18
commit ca929108af

View File

@@ -1,20 +1,24 @@
<?php <?php
/* /*
Plugin Name: Spotify Recent Track Plugin Name: Senast spelade låt på Spotify
Description: Plugin som visar den senast spelade låten från Spotify med OAuth-autentisering. Description: Gutenbergblock som visar den senast spelade låten från Spotify via OAuth.
Version: 1.0.0 Version: 1.0.0
Author: Christian Ohlsson Author: Christian Ohlsson
*/ */
if (!defined('ABSPATH')) exit; if (!defined('ABSPATH')) exit;
// Inkludera adminfiler
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';
/* /*
* Register the Gutenberg block * 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__),
@@ -22,16 +26,30 @@ function sro_register_block() {
filemtime(plugin_dir_path(__FILE__) . 'blocks/block.js') filemtime(plugin_dir_path(__FILE__) . 'blocks/block.js')
); );
register_block_type('sro/recent-track', array( // --- Frontend-stil ---
'editor_script' => 'sro-block-js', wp_register_style(
'render_callback' => 'sro_render_block' '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'); 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() { function sro_fetch_recent_track() {
@@ -40,7 +58,7 @@ function sro_fetch_recent_track() {
$client_id = get_option('sro_client_id'); $client_id = get_option('sro_client_id');
$client_secret = get_option('sro_client_secret'); $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'); $expires = get_option('sro_token_expires');
if (time() > $expires && $refresh) { if (time() > $expires && $refresh) {
$response = wp_remote_post("https://accounts.spotify.com/api/token", array( $response = wp_remote_post("https://accounts.spotify.com/api/token", array(
@@ -64,6 +82,7 @@ function sro_fetch_recent_track() {
if (!$access) return false; if (!$access) return false;
// Hämta senaste låten
$resp = 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"))
@@ -78,8 +97,9 @@ function sro_fetch_recent_track() {
return $data['items'][0]; return $data['items'][0];
} }
/* /*
* Render the Gutenberg block * Rendera blocket i frontend
*/ */
function sro_render_block() { function sro_render_block() {
$item = sro_fetch_recent_track(); $item = sro_fetch_recent_track();
@@ -93,7 +113,6 @@ function sro_render_block() {
$played = strtotime($item['played_at']); $played = strtotime($item['played_at']);
$mins = floor((time() - $played) / 60); $mins = floor((time() - $played) / 60);
$mins = intval( $mins );
ob_start(); ob_start();
?> ?>
@@ -104,38 +123,9 @@ function sro_render_block() {
<div class="sro-info"> <div class="sro-info">
<div class="sro-track"><?php echo $track; ?></div> <div class="sro-track"><?php echo $track; ?></div>
<div class="sro-artist"><?php echo $artist; ?></div> <div class="sro-artist"><?php echo $artist; ?></div>
<div class="sro-time">Spelades för <?php echo $mins; ?> minuter sedan</div> <div class="sro-time">Spelades för <?php echo intval($mins); ?> minuter sedan</div>
</div> </div>
</div> </div>
<?php <?php
return ob_get_clean(); return ob_get_clean();
} }
function sro_register_block() {
// Registrera blockets resurser
wp_register_script(
'sro-block-js',
plugins_url( 'blocks/block.js', __FILE__ ),
array( 'wp-blocks', 'wp-element', 'wp-editor' ),
filemtime( plugin_dir_path(__FILE__) . 'blocks/block.js' )
);
wp_register_style(
'sro-style',
plugins_url( 'blocks/style.css', __FILE__ ),
array(),
filemtime( plugin_dir_path(__FILE__) . 'blocks/style.css' )
);
wp_register_style(
'sro-editor-style',
plugins_url( 'blocks/editor.css', __FILE__ ),
array(),
filemtime( plugin_dir_path(__FILE__) . 'blocks/editor.css' )
);
// Registrera blocket (block.json sköter allt)
register_block_type( __DIR__ . '/blocks' );
}
add_action( 'init', 'sro_register_block' );