Files
wp-spaceapi-consumer/wp-api-consumer.php
2025-05-23 23:15:15 +01:00

164 lines
5.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Plugin Name: Door Status Indicator
* Plugin URI: https://example.com/
* Description: Lightweight, dependencyfree 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 realtime 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
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/*
|--------------------------------------------------------------------------
| Configuration
|--------------------------------------------------------------------------
| 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 outofthebox.
*/
$config_file = plugin_dir_path( __FILE__ ) . 'door-status-config.php';
if ( file_exists( $config_file ) ) {
require_once $config_file;
}
// 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
|--------------------------------------------------------------------------
*/
/**
* Retrieves dooropen status (boolean) with transient caching.
*
* 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 );
if ( false !== $cached ) {
return (bool) $cached;
}
$response = wp_remote_get( DSI_API_URL, [
'timeout' => 3,
'user-agent' => 'DoorStatusIndicator/1.1.1 (+https://wordpress.org/)',
] );
if ( is_wp_error( $response ) ) {
set_transient( DSI_CACHE_KEY, false, DSI_CACHE_TTL );
return false;
}
$body = wp_remote_retrieve_body( $response );
$decoded = json_decode( $body, true );
$status = false; // default to closed
if ( is_bool( $decoded ) ) {
$status = $decoded;
} 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 );
return $status;
}
/*
|--------------------------------------------------------------------------
| Shortcode [door_status]
|--------------------------------------------------------------------------
*/
function dsi_shortcode() : string {
$open = dsi_get_status();
$emoji = $open ? '<div class="door-status-indicator open">🟢 Open</div>' : '<div class="door-status-indicator closed">🔴 Closed</div>';
return sprintf(
'<span class="door-status-indicator" aria-label="Door is %s">%s</span>',
$open ? 'open' : 'closed',
$emoji
);
}
add_shortcode( 'door_status', 'dsi_shortcode' );
/*
|--------------------------------------------------------------------------
| Admin Bar indicator
|--------------------------------------------------------------------------
*/
function dsi_admin_bar( WP_Admin_Bar $bar ) : void {
if ( ! current_user_can( 'read' ) ) {
return; // Loggedin users only.
}
$open = dsi_get_status();
$emoji = $open ? '🟢' : '🔴';
$text = $open ? __( 'Door Open', 'door-status-indicator' ) : __( 'Door Closed', 'door-status-indicator' );
$bar->add_node( [
'id' => 'door-status-indicator',
'title' => "$emoji $text",
'href' => '#',
'meta' => [ 'title' => $text ],
] );
}
add_action( 'admin_bar_menu', 'dsi_admin_bar', 1000 );
/*
|--------------------------------------------------------------------------
| Frontend inline CSS (kept minimal)
|--------------------------------------------------------------------------
*/
/**
* Front-end + admin-bar styles for the indicator.
*/
function dsi_enqueue_assets() : void {
// register & enqueue the standalone CSS file
wp_enqueue_style(
'door-status-indicator',
plugins_url( 'door-status.css', __FILE__ ),
[], // no dependencies
'1.0.0' // file version
);
}
add_action( 'wp_enqueue_scripts', 'dsi_enqueue_assets' );
add_action( 'admin_enqueue_scripts', 'dsi_enqueue_assets' ); // so the Admin Bar icon also gets styled
/*
|--------------------------------------------------------------------------
| WPCLI command (optional)
|--------------------------------------------------------------------------
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'door-status', function() {
WP_CLI::success( dsi_get_status() ? 'Door is open 🟢' : 'Door is closed 🔴' );
} );
}
// End of file