Skrev in för att uppfylla modern standard
This commit is contained in:
@@ -1,37 +1,55 @@
|
|||||||
<?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: Gutenberg‑block 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 admin‑filer
|
||||||
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__),
|
||||||
array('wp-blocks', 'wp-element', 'wp-editor'),
|
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(
|
// --- 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,9 +82,10 @@ 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"))
|
||||||
);
|
);
|
||||||
|
|
||||||
if (is_wp_error($resp)) return false;
|
if (is_wp_error($resp)) return false;
|
||||||
@@ -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();
|
||||||
@@ -87,13 +107,12 @@ function sro_render_block() {
|
|||||||
return "<p>Spotify är inte anslutet eller så finns ingen nyligen spelad låt.</p>";
|
return "<p>Spotify är inte anslutet eller så finns ingen nyligen spelad låt.</p>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$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'] );
|
$img = esc_url($item['track']['album']['images'][0]['url']);
|
||||||
|
|
||||||
$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' );
|
|
||||||
Reference in New Issue
Block a user