Settings saved.

'; } $username = get_option("lastfm_username", ""); $apikey = get_option("lastfm_apikey", ""); ?>

Last.fm Now Playing Settings

"Not configured", "artist" => "", "album" => "", "image" => "", "nowplaying" => false, "played_ago" => "" ]; } $url = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" . rawurlencode($username) . "&api_key=" . rawurlencode($apikey) . "&format=json&limit=1"; $response = wp_remote_get($url, ["timeout" => 10]); if (is_wp_error($response)) { return ["title" => "Fetch error", "artist" => "", "album" => "", "image" => "", "nowplaying" => false, "played_ago" => ""]; } $data = json_decode(wp_remote_retrieve_body($response), true); if (!$data || !isset($data["recenttracks"]["track"][0])) { return ["title" => "No tracks", "artist" => "", "album" => "", "image" => "", "nowplaying" => false, "played_ago" => ""]; } $track = $data["recenttracks"]["track"][0]; $image_url = $track["image"][2]["#text"] ?? ""; $image_data_uri = ""; if ($image_url) { $img_response = wp_remote_get($image_url, ["timeout" => 10]); if (!is_wp_error($img_response)) { $img_body = wp_remote_retrieve_body($img_response); if ($img_body) { $image_data_uri = "data:image/jpeg;base64," . base64_encode($img_body); } } } $played_ago = ""; if (empty($track["@attr"]["nowplaying"]) && isset($track["date"]["uts"])) { $timestamp = (int)$track["date"]["uts"]; $diff = time() - $timestamp; if ($diff < 60) { $played_ago = $diff . " seconds ago"; } elseif ($diff < 3600) { $played_ago = floor($diff / 60) . " minutes ago"; } elseif ($diff < 86400) { $played_ago = floor($diff / 3600) . " hours ago"; } else { $played_ago = floor($diff / 86400) . " days ago"; } } return [ "title" => $track["name"] ?? "Unknown", "artist" => $track["artist"]["#text"] ?? "", "album" => $track["album"]["#text"] ?? "", "image" => $image_data_uri, "nowplaying" => isset($track["@attr"]["nowplaying"]), "played_ago" => $played_ago ]; } add_shortcode("now-playing-widget", function () { wp_enqueue_style( "lastfm-nowplaying-style", plugin_dir_url(__FILE__) . "css/style.css" ); $track = lastfm_nowplaying_fetch(); ob_start(); ?>
" id="cover" src="" alt="Album cover" />
" id="title">
" id="artist">
">
__("Displays your Last.fm now playing track.", "lastfm_nowplaying")] ); } public function widget($args, $instance) { $title = apply_filters("widget_title", $instance["title"] ?? ""); echo $args["before_widget"]; if (!empty($title)) { echo $args["before_title"] . esc_html($title) . $args["after_title"]; } echo do_shortcode("[now-playing-widget]"); echo $args["after_widget"]; } public function form($instance) { $title = esc_attr($instance["title"] ?? ""); ?>

" name="get_field_name("title")); ?>" type="text" value="" />