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,37 +1,55 @@
<?php
/*
Plugin Name: Spotify Recent Track
Description: Plugin som visar den senast spelade låten från Spotify med OAuth-autentisering.
Plugin Name: Senast spelade låt på Spotify
Description: Gutenbergblock som visar den senast spelade låten från Spotify via OAuth.
Version: 1.0.0
Author: Christian Ohlsson
*/
if (!defined('ABSPATH')) exit;
// Inkludera adminfiler
require_once plugin_dir_path(__FILE__) . 'admin/settings.php';
require_once plugin_dir_path(__FILE__) . 'admin/oauth-handler.php';
/*
* Register the Gutenberg block
* Registrera block, script och stilmallar
*/
function sro_register_block() {
// --- JS för blockeditorn ---
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')
filemtime(plugin_dir_path(__FILE__) . 'blocks/block.js')
);
register_block_type('sro/recent-track', array(
'editor_script' => '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 "<p>Spotify är inte anslutet eller så finns ingen nyligen spelad låt.</p>";
}
$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() {
<div class="sro-info">
<div class="sro-track"><?php echo $track; ?></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>
<?php
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' );
}