fix
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: Door Status Indicator
|
||||
* Plugin URI: https://gitea.alluna.pt/jfig/wp-api-consumer
|
||||
* Description: Lightweight, dependency‑free plugin that polls <https://nodered.jfig.net/api/v1/door-open>, caches the status for a few seconds, and shows a real‑time door open/closed indicator (🟢/🔴) via [door_status] shortcode and the Admin Bar.
|
||||
* Version: 1.1.0
|
||||
* Author: Joao Figueiredo (ChatGPT helper)
|
||||
* Author URI: https://jfig.net
|
||||
* Plugin URI: https://example.com/
|
||||
* Description: Lightweight, dependency‑free plugin that polls a JSON endpoint (default: https://nodered.jfig.net/api/v1/door-open), caches the value for a few seconds, and shows a real‑time door open/closed indicator (🟢/🔴) via the [door_status] shortcode and the Admin Bar.
|
||||
* Version: 1.1.1
|
||||
* Author: J (ChatGPT helper)
|
||||
* Author URI: https://example.com/
|
||||
* License: GPL v2 or later
|
||||
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
||||
* Text Domain: door-status-indicator
|
||||
@@ -19,21 +19,21 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
|--------------------------------------------------------------------------
|
||||
| Configuration
|
||||
|--------------------------------------------------------------------------
|
||||
| Place environment‑specific constants inside door-status-config.php.
|
||||
| They will be loaded automatically. If the file is missing we fall back to
|
||||
| bundled defaults so the plugin keeps working out‑of‑the‑box.
|
||||
| Constants are expected in door-status-config.php located in the same
|
||||
| directory. If that file is missing, sane defaults are used so the plugin
|
||||
| still works out‑of‑the‑box.
|
||||
*/
|
||||
|
||||
$config_file = plugin_dir_path( __FILE__ ) . 'door-status-config.php';
|
||||
if ( file_exists( $config_file ) ) {
|
||||
require_once $config_file;
|
||||
} else {
|
||||
// Fallback defaults – safe to adjust directly here if you prefer.
|
||||
define( 'DSI_API_URL', 'https://nodered.jfig.net/api/v1/door-open' );
|
||||
define( 'DSI_CACHE_KEY', 'dsi_door_open_status' );
|
||||
define( 'DSI_CACHE_TTL', 10 );
|
||||
}
|
||||
|
||||
// Fallback defaults (only if not already defined by the config file).
|
||||
! defined( 'DSI_API_URL' ) && define( 'DSI_API_URL', 'https://nodered.jfig.net/api/v1/door-open' );
|
||||
! defined( 'DSI_CACHE_KEY' ) && define( 'DSI_CACHE_KEY', 'dsi_door_open_status' );
|
||||
! defined( 'DSI_CACHE_TTL' ) && define( 'DSI_CACHE_TTL', 10 ); // seconds
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Core – fetch & cache
|
||||
@@ -41,9 +41,14 @@ if ( file_exists( $config_file ) ) {
|
||||
*/
|
||||
|
||||
/**
|
||||
* Retrieves the door‑open status (boolean) with transient caching.
|
||||
* Retrieves door‑open status (boolean) with transient caching.
|
||||
*
|
||||
* @return bool True if open, false if closed.
|
||||
* Supported JSON payloads (any case):
|
||||
* – true / false (bare boolean)
|
||||
* – {"open": true}
|
||||
* – {"doorOpen": true}
|
||||
*
|
||||
* @return bool True if open, false if closed (or on error).
|
||||
*/
|
||||
function dsi_get_status() : bool {
|
||||
$cached = get_transient( DSI_CACHE_KEY );
|
||||
@@ -53,23 +58,26 @@ function dsi_get_status() : bool {
|
||||
|
||||
$response = wp_remote_get( DSI_API_URL, [
|
||||
'timeout' => 3,
|
||||
'user-agent' => 'DoorStatusIndicator/1.1 (+https://wordpress.org/)',
|
||||
'user-agent' => 'DoorStatusIndicator/1.1.1 (+https://wordpress.org/)',
|
||||
] );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
// On error, assume closed and set a short transient to avoid hammering.
|
||||
set_transient( DSI_CACHE_KEY, false, DSI_CACHE_TTL );
|
||||
return false;
|
||||
}
|
||||
|
||||
$body = wp_remote_retrieve_body( $response );
|
||||
$decoded = json_decode( $body, true );
|
||||
$status = false;
|
||||
$status = false; // default to closed
|
||||
|
||||
if ( is_bool( $decoded ) ) {
|
||||
$status = $decoded;
|
||||
} elseif ( is_array( $decoded ) && isset( $decoded['open'] ) ) {
|
||||
} elseif ( is_array( $decoded ) ) {
|
||||
if ( isset( $decoded['open'] ) ) {
|
||||
$status = (bool) $decoded['open'];
|
||||
} elseif ( isset( $decoded['doorOpen'] ) ) {
|
||||
$status = (bool) $decoded['doorOpen'];
|
||||
}
|
||||
}
|
||||
|
||||
set_transient( DSI_CACHE_KEY, $status, DSI_CACHE_TTL );
|
||||
@@ -82,12 +90,6 @@ function dsi_get_status() : bool {
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Outputs a 🟢 or 🔴 indicator reflecting current door status.
|
||||
* Usage: [door_status]
|
||||
*
|
||||
* @return string HTML span with coloured emoji.
|
||||
*/
|
||||
function dsi_shortcode() : string {
|
||||
$open = dsi_get_status();
|
||||
$emoji = $open ? '🟢' : '🔴';
|
||||
@@ -106,43 +108,35 @@ add_shortcode( 'door_status', 'dsi_shortcode' );
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Adds a live indicator to the WordPress Admin Bar (front‑ and back‑end).
|
||||
*
|
||||
* @param WP_Admin_Bar $wp_admin_bar The admin bar instance.
|
||||
*/
|
||||
function dsi_admin_bar( WP_Admin_Bar $wp_admin_bar ) : void {
|
||||
function dsi_admin_bar( WP_Admin_Bar $bar ) : void {
|
||||
if ( ! current_user_can( 'read' ) ) {
|
||||
return; // Only show to logged‑in users who can read.
|
||||
return; // Logged‑in users only.
|
||||
}
|
||||
|
||||
$open = dsi_get_status();
|
||||
$emoji = $open ? '🟢' : '🔴';
|
||||
$text = $open ? __( 'Door Open', 'door-status-indicator' ) : __( 'Door Closed', 'door-status-indicator' );
|
||||
|
||||
$wp_admin_bar->add_node( [
|
||||
$bar->add_node( [
|
||||
'id' => 'door-status-indicator',
|
||||
'title' => sprintf( '%s %s', $emoji, $text ),
|
||||
'title' => "$emoji $text",
|
||||
'href' => '#',
|
||||
'meta' => [
|
||||
'title' => $text,
|
||||
],
|
||||
'meta' => [ 'title' => $text ],
|
||||
] );
|
||||
}
|
||||
add_action( 'admin_bar_menu', 'dsi_admin_bar', 1000 );
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Front‑end inline CSS
|
||||
| Front‑end inline CSS (kept minimal)
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
function dsi_enqueue_assets() : void {
|
||||
if ( is_admin() ) {
|
||||
return;
|
||||
return; // Front‑end only.
|
||||
}
|
||||
$css = '.door-status-indicator{font-size:1.2em;line-height:1}';
|
||||
wp_add_inline_style( 'wp-block-library', $css );
|
||||
wp_add_inline_style( 'wp-block-library', '.door-status-indicator{font-size:1.2em;line-height:1}' );
|
||||
}
|
||||
add_action( 'wp_enqueue_scripts', 'dsi_enqueue_assets' );
|
||||
|
||||
@@ -154,8 +148,7 @@ add_action( 'wp_enqueue_scripts', 'dsi_enqueue_assets' );
|
||||
|
||||
if ( defined( 'WP_CLI' ) && WP_CLI ) {
|
||||
WP_CLI::add_command( 'door-status', function() {
|
||||
$status = dsi_get_status() ? 'open 🟢' : 'closed 🔴';
|
||||
WP_CLI::success( "Door is $status" );
|
||||
WP_CLI::success( dsi_get_status() ? 'Door is open 🟢' : 'Door is closed 🔴' );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user