From d66f1129710bb6ac0683a62e9d9678249f2a1c90 Mon Sep 17 00:00:00 2001 From: Sophia Atkinson Date: Sat, 21 Jun 2025 22:58:53 -0700 Subject: [PATCH] Updated to handle the separate post requests for artwork, and other now playing data, hope it works lol --- nowplaying-api.php | 360 +++++++++++++++++++++++++++++++-------------- 1 file changed, 246 insertions(+), 114 deletions(-) diff --git a/nowplaying-api.php b/nowplaying-api.php index d0163fd..1e03c9a 100644 --- a/nowplaying-api.php +++ b/nowplaying-api.php @@ -2,84 +2,155 @@ /* Plugin Name: Apple Music Now Playing API Description: Accepts JSON now-playing updates via REST, lets user set a secure API key on the plugin page, a shortcode widget, that can be added to Footer -Version: 1.3.4 +Version: 1.4.0 Author: Sophia Atkinson */ -defined('ABSPATH') || exit; +defined("ABSPATH") || exit(); -// === Register REST API endpoint === -add_action('rest_api_init', function () { - register_rest_route('nowplaying/v1', '/update', [ - 'methods' => 'POST', - 'callback' => 'nowplaying_api_handle_post', - 'permission_callback' => '__return_true', +// === Register REST API endpoints === +add_action("rest_api_init", function () { + register_rest_route("nowplaying/v1", "/update", [ + "methods" => "POST", + "callback" => "nowplaying_api_handle_post", + "permission_callback" => "__return_true", + ]); + register_rest_route("nowplaying/v1", "/update/artwork", [ + "methods" => "POST", + "callback" => "nowplaying_api_handle_art_post", + "permission_callback" => "__return_true", ]); }); -function nowplaying_api_handle_post(WP_REST_Request $request) { - $stored_key = get_option('nowplaying_api_key'); - $auth_header = $request->get_header('authorization'); +function nowplaying_api_handle_post(WP_REST_Request $request) +{ + $stored_key = get_option("nowplaying_api_key"); + $auth_header = $request->get_header("authorization"); - if (!$auth_header || $auth_header !== "Bearer $stored_key") { - return new WP_REST_Response('Forbidden: Invalid API key.', 403); + $expected_prefix = "bearer "; + $auth_header_normalized = strtolower(trim($auth_header ?? "")); + if ( + !$auth_header || + stripos($auth_header_normalized, $expected_prefix) !== 0 || + substr(trim($auth_header), strlen($expected_prefix)) !== $stored_key + ) { + return new WP_REST_Response("Forbidden: Invalid API key.", 403); } $data = json_decode($request->get_body(), true); if ($data === null) { - return new WP_REST_Response('Invalid JSON.', 400); + return new WP_REST_Response("Invalid JSON.", 400); } - $file_path = plugin_dir_path(__FILE__) . 'nowplaying.json'; + $file_path = plugin_dir_path(__FILE__) . "nowplaying.json"; + $written = file_put_contents( + $file_path, + json_encode($data, JSON_PRETTY_PRINT) + ); + try { + $written = file_put_contents( + $file_path, + json_encode($data, JSON_PRETTY_PRINT) + ); + if ($written === false) { + throw new Exception(); + } + } catch (Exception $e) { + return new WP_REST_Response("Failed to write to nowplaying.json.", 500); + } + + return new WP_REST_Response("Now Playing updated.", 200); +} + +function nowplaying_api_handle_art_post(WP_REST_Request $request) +{ + $stored_key = get_option("nowplaying_api_key"); + $auth_header = $request->get_header("authorization"); + + if (!$auth_header || $auth_header !== "Bearer $stored_key") { + return new WP_REST_Response("Forbidden: Invalid API key.", 403); + } + + $data = json_decode($request->get_body(), true); + + if ($data === null) { + return new WP_REST_Response("Invalid JSON.", 400); + } + + if (!isset($data["image"]) || !is_string($data["image"])) { + return new WP_REST_Response( + 'Missing or invalid "image" property.', + 400 + ); + } + + $file_path = plugin_dir_path(__FILE__) . "nowplaying-art.json"; try { - $written = file_put_contents($file_path, json_encode($data, JSON_PRETTY_PRINT)); - if (!$written) throw new Exception(); + $written = file_put_contents( + $file_path, + json_encode($data, JSON_PRETTY_PRINT) + ); + if ($written === false) { + throw new Exception(); + } } catch (Exception $e) { - return new WP_REST_Response('Failed to write to nowplaying.json.', 500); + return new WP_REST_Response( + "Failed to write to nowplaying-art.json.", + 500 + ); } - return new WP_REST_Response('Now Playing updated.', 200); + return new WP_REST_Response("Artwork updated.", 200); } // === Admin Settings Page === -add_action('admin_menu', function () { +add_action("admin_menu", function () { add_options_page( - 'Now Playing API Settings', - 'Now Playing API', - 'manage_options', - 'nowplaying-api', - 'nowplaying_api_settings_page' + "Now Playing API Settings", + "Now Playing API", + "manage_options", + "nowplaying-api", + "nowplaying_api_settings_page" ); }); -function nowplaying_api_settings_page() { - if (!current_user_can('manage_options')) return; +function nowplaying_api_settings_page() +{ + if (!current_user_can("manage_options")) { + return; + } - if (isset($_POST['nowplaying_api_key']) && check_admin_referer('nowplaying_save_key', 'nowplaying_nonce')) { - $new_key = sanitize_text_field($_POST['nowplaying_api_key']); + if ( + isset($_POST["nowplaying_api_key"]) && + check_admin_referer("nowplaying_save_key", "nowplaying_nonce") + ) { + $new_key = sanitize_text_field($_POST["nowplaying_api_key"]); if (strlen($new_key) === 64) { - update_option('nowplaying_api_key', $new_key); + update_option("nowplaying_api_key", $new_key); echo '

API key updated.

'; } else { echo '

API key must be exactly 64 characters.

'; } } - if (isset($_POST['generate_key']) && check_admin_referer('nowplaying_save_key', 'nowplaying_nonce')) { + if ( + isset($_POST["generate_key"]) && + check_admin_referer("nowplaying_save_key", "nowplaying_nonce") + ) { $generated_key = bin2hex(random_bytes(32)); - update_option('nowplaying_api_key', $generated_key); + update_option("nowplaying_api_key", $generated_key); echo '

New API key generated.

'; } - $current_key = get_option('nowplaying_api_key'); + $current_key = get_option("nowplaying_api_key"); if (!$current_key) { $current_key = bin2hex(random_bytes(32)); - update_option('nowplaying_api_key', $current_key); + update_option("nowplaying_api_key", $current_key); } - $url = esc_url(site_url('/wp-json/nowplaying/v1/update')); + $url = esc_url(site_url("/wp-json/nowplaying/v1/update")); ?>

Apple Music Now Playing API Settings

@@ -87,33 +158,40 @@ function nowplaying_api_settings_page() {
- +
- +

Keep this key secret.
Must be 64 characters.

- +
- - + +
- +
Album cover @@ -124,17 +202,19 @@ add_shortcode('now-playing-widget', function () {
0:000:00