Startpunkten
This commit is contained in:
37
admin/oauth-handler.php
Normal file
37
admin/oauth-handler.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
function sro_handle_oauth() {
|
||||
if (!isset($_GET['page']) || $_GET['page'] !== 'sro') return;
|
||||
if (!isset($_GET['oauth'])) return;
|
||||
|
||||
if (!isset($_GET['code'])) return;
|
||||
|
||||
$client_id = get_option('sro_client_id');
|
||||
$client_secret = get_option('sro_client_secret');
|
||||
$redirect_uri = admin_url('options-general.php?page=sro&oauth=1');
|
||||
|
||||
$response = wp_remote_post("https://accounts.spotify.com/api/token", array(
|
||||
'body' => 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');
|
||||
55
admin/settings.php
Normal file
55
admin/settings.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
function sro_menu() {
|
||||
add_options_page(
|
||||
"Spotify Recent",
|
||||
"Spotify Recent",
|
||||
"manage_options",
|
||||
"sro",
|
||||
"sro_page"
|
||||
);
|
||||
}
|
||||
add_action('admin_menu', 'sro_menu');
|
||||
|
||||
|
||||
function sro_page() {
|
||||
$client_id = get_option('sro_client_id');
|
||||
$client_secret = get_option('sro_client_secret');
|
||||
|
||||
if (isset($_POST['sro_save'])) {
|
||||
update_option('sro_client_id', sanitize_text_field($_POST['client_id']));
|
||||
update_option('sro_client_secret', sanitize_text_field($_POST['client_secret']));
|
||||
echo "<div class='updated'><p>Saved.</p></div>";
|
||||
}
|
||||
|
||||
$redirect_uri = admin_url('options-general.php?page=sro&oauth=1');
|
||||
?>
|
||||
<h1>Spotify OAuth Settings</h1>
|
||||
|
||||
<form method="post">
|
||||
<label>Client ID</label><br>
|
||||
<input type="text" name="client_id" value="<?php echo esc_attr($client_id); ?>" size="60"><br><br>
|
||||
|
||||
<label>Client Secret</label><br>
|
||||
<input type="text" name="client_secret" value="<?php echo esc_attr($client_secret); ?>" size="60"><br><br>
|
||||
|
||||
<button class="button-primary" name="sro_save">Save</button>
|
||||
</form>
|
||||
|
||||
<hr>
|
||||
|
||||
<h2>Step 2: Connect Spotify</h2>
|
||||
<p>Add this redirect URI in your Spotify App:</p>
|
||||
|
||||
<code><?php echo $redirect_uri; ?></code>
|
||||
|
||||
<br><br>
|
||||
|
||||
<a href="https://accounts.spotify.com/authorize?client_id=<?php echo $client_id; ?>&response_type=code&redirect_uri=<?php echo urlencode($redirect_uri); ?>&scope=user-read-recently-played"
|
||||
class="button button-primary">
|
||||
Connect Spotify
|
||||
</a>
|
||||
|
||||
<?php
|
||||
}
|
||||
17
blocks/block.js
Normal file
17
blocks/block.js
Normal file
@@ -0,0 +1,17 @@
|
||||
(function (blocks, element) {
|
||||
var el = element.createElement;
|
||||
|
||||
blocks.registerBlockType("sro/recent-track", {
|
||||
title: "Spotify Recent Track",
|
||||
icon: "format-audio",
|
||||
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);
|
||||
105
spotify-recent.php
Normal file
105
spotify-recent.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: Spotify Recent Track (OAuth)
|
||||
Description: Gutenberg block showing user's most recently played Spotify track using OAuth.
|
||||
Version: 1.0.0
|
||||
Author: You
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
require_once plugin_dir_path(__FILE__) . 'admin/settings.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'admin/oauth-handler.php';
|
||||
|
||||
/*
|
||||
* Register the Gutenberg block
|
||||
*/
|
||||
function sro_register_block() {
|
||||
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')
|
||||
);
|
||||
|
||||
register_block_type('sro/recent-track', array(
|
||||
'editor_script' => '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 "<p>Spotify not connected or no recent activity.</p>";
|
||||
}
|
||||
|
||||
$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 "
|
||||
<div class='sro-block'>
|
||||
<img src='$img' width='150'>
|
||||
<p><strong>$artist – $track</strong></p>
|
||||
<p>Spelades för $mins minuter sedan</p>
|
||||
</div>
|
||||
";
|
||||
}
|
||||
Reference in New Issue
Block a user