major refactor, numenculature change
remove mentions to "door" status, to space
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
|
||||
define( 'DSI_API_URL', 'https://lcdporto.org/api/spaceapi' );
|
||||
define( 'DSI_CACHE_KEY', 'dsi_space_api_status' );
|
||||
define( 'DSI_CACHE_TTL', 10 );
|
||||
define( 'SSI_API_URL', 'https://lcdporto.org/api/spaceapi' );
|
||||
define( 'SSI_CACHE_KEY', 'ssi_space_api_status' );
|
||||
define( 'SSI_CACHE_TTL', 10 );
|
||||
@@ -1,6 +1,6 @@
|
||||
/* door-status.css – styles the indicator as a pill‑shaped button */
|
||||
/* space-status.css – styles the indicator as a pill‑shaped button */
|
||||
|
||||
.door-status-indicator {
|
||||
.space-status-indicator {
|
||||
display:inline-flex;
|
||||
align-items:center;
|
||||
gap:0.4em;
|
||||
@@ -15,20 +15,20 @@
|
||||
}
|
||||
|
||||
/* Open = soft green background + dark green text */
|
||||
.door-status-indicator.open {
|
||||
.space-status-indicator.open {
|
||||
background:#e7f9ec;
|
||||
color:#1e7a34;
|
||||
}
|
||||
|
||||
/* Closed = soft red background + dark red text */
|
||||
.door-status-indicator.closed {
|
||||
.space-status-indicator.closed {
|
||||
background:#fdeeee;
|
||||
color:#c03c3c;
|
||||
}
|
||||
|
||||
/* (Optional) slight hover effect */
|
||||
.door-status-indicator:hover,
|
||||
.door-status-indicator:focus {
|
||||
.space-status-indicator:hover,
|
||||
.space-status-indicator:focus {
|
||||
filter:brightness(1.05);
|
||||
outline:none;
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
* Plugin Name: WordPress SpaceAPI Consumer
|
||||
* Plugin Slug: wp-spaceapi-consumer
|
||||
* Plugin URI: https://gitea.alluna.pt/jfig/wp-spaceapi-consumer
|
||||
* Description: Small WordPress plugin to consume an SpaceAPI endpoint and indicate on the WordPress website if the Space is Open or Closed
|
||||
* Version: 1.1.1
|
||||
* Description: WordPress plugin to consume a SpaceAPI endpoint and indicate if the Space is Open or Closed
|
||||
* Version: 0.3.0
|
||||
* Author: Joao Figueiredo, LCD Porto Team, ChatGPT o3
|
||||
* Author URI: https://lcdporto.org
|
||||
* License: MIT
|
||||
@@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
|--------------------------------------------------------------------------
|
||||
| Configuration
|
||||
|--------------------------------------------------------------------------
|
||||
| Constants are expected in door-status-config.php located in the same
|
||||
| Constants are expected in wp-spaceapi-consumer-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.
|
||||
*/
|
||||
@@ -31,9 +31,9 @@ if ( file_exists( $config_file ) ) {
|
||||
}
|
||||
|
||||
// Fallback defaults (only if not already defined by the config file).
|
||||
! defined( 'DSI_API_URL' ) && define( 'DSI_API_URL', 'https://lcdporto.org/api/spaceapi' );
|
||||
! defined( 'DSI_CACHE_KEY' ) && define( 'DSI_CACHE_KEY', 'dsi_space_api_status' );
|
||||
! defined( 'DSI_CACHE_TTL' ) && define( 'DSI_CACHE_TTL', 10 ); // seconds
|
||||
! defined( 'SSI_API_URL' ) && define( 'SSI_API_URL', 'https://lcdporto.org/api/spaceapi' );
|
||||
! defined( 'SSI_CACHE_KEY' ) && define( 'SSI_CACHE_KEY', 'ssi_space_api_status' );
|
||||
! defined( 'SSI_CACHE_TTL' ) && define( 'SSI_CACHE_TTL', 10 ); // seconds
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@@ -42,17 +42,16 @@ if ( file_exists( $config_file ) ) {
|
||||
*/
|
||||
|
||||
/**
|
||||
* Retrieves door‑open status (boolean) with transient caching.
|
||||
* Retrieves space open status (boolean) with transient caching.
|
||||
*
|
||||
* Supported JSON payloads (any case):
|
||||
* – true / false (bare boolean)
|
||||
* – {"open": true}
|
||||
* – {"doorOpen": true}
|
||||
* – {"state": {"open": true}} (SpaceAPI standard)
|
||||
*
|
||||
* @return bool True if open, false if closed (or on error).
|
||||
*/
|
||||
function dsi_get_status() : bool {
|
||||
$cached = get_transient( DSI_CACHE_KEY );
|
||||
function ssi_get_status() : bool {
|
||||
$cached = get_transient( SSI_CACHE_KEY );
|
||||
if ( false !== $cached ) {
|
||||
return (bool) $cached;
|
||||
}
|
||||
@@ -63,13 +62,13 @@ function dsi_get_status() : bool {
|
||||
'TextDomain' => 'Text Domain'
|
||||
), 'plugin' );
|
||||
|
||||
$response = wp_remote_get( DSI_API_URL, [
|
||||
$response = wp_remote_get( SSI_API_URL, [
|
||||
'timeout' => 3,
|
||||
'user-agent' => $plugin_data['TextDomain'] . '/' . $plugin_data['Version'] . ' (+https://wordpress.org/)',
|
||||
] );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
set_transient( DSI_CACHE_KEY, false, DSI_CACHE_TTL );
|
||||
set_transient( SSI_CACHE_KEY, false, SSI_CACHE_TTL );
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -80,14 +79,13 @@ function dsi_get_status() : bool {
|
||||
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'];
|
||||
// SpaceAPI standard format - state.open
|
||||
if ( isset( $decoded['state']['open'] ) ) {
|
||||
$status = (bool) $decoded['state']['open'];
|
||||
}
|
||||
}
|
||||
|
||||
set_transient( DSI_CACHE_KEY, $status, DSI_CACHE_TTL );
|
||||
set_transient( SSI_CACHE_KEY, $status, SSI_CACHE_TTL );
|
||||
return $status;
|
||||
}
|
||||
|
||||
@@ -97,17 +95,19 @@ function dsi_get_status() : bool {
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
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>';
|
||||
function ssi_shortcode() : string {
|
||||
$open = ssi_get_status();
|
||||
$emoji = $open ? '<div class="space-status-indicator open">🟢 Open</div>' : '<div class="space-status-indicator closed">🔴 Closed</div>';
|
||||
|
||||
return sprintf(
|
||||
'<span class="door-status-indicator" aria-label="Door is %s">%s</span>',
|
||||
'<span class="space-status-indicator" aria-label="Space is %s">%s</span>',
|
||||
$open ? 'open' : 'closed',
|
||||
$emoji
|
||||
);
|
||||
}
|
||||
add_shortcode( 'door_status', 'dsi_shortcode' );
|
||||
add_shortcode( 'space_status', 'ssi_shortcode' );
|
||||
// Keep the old shortcode for backward compatibility
|
||||
add_shortcode( 'door_status', 'ssi_shortcode' );
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@@ -115,23 +115,23 @@ add_shortcode( 'door_status', 'dsi_shortcode' );
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
function dsi_admin_bar( WP_Admin_Bar $bar ) : void {
|
||||
function ssi_admin_bar( WP_Admin_Bar $bar ) : void {
|
||||
if ( ! current_user_can( 'read' ) ) {
|
||||
return; // Logged‑in users only.
|
||||
}
|
||||
|
||||
$open = dsi_get_status();
|
||||
$open = ssi_get_status();
|
||||
$emoji = $open ? '🟢' : '🔴';
|
||||
$text = $open ? __( 'Door Open', 'door-status-indicator' ) : __( 'Door Closed', 'door-status-indicator' );
|
||||
$text = $open ? __( 'Space Open', 'wp-spaceapi-consumer' ) : __( 'Space Closed', 'wp-spaceapi-consumer' );
|
||||
|
||||
$bar->add_node( [
|
||||
'id' => 'door-status-indicator',
|
||||
'id' => 'space-status-indicator',
|
||||
'title' => "$emoji $text",
|
||||
'href' => '#',
|
||||
'meta' => [ 'title' => $text ],
|
||||
] );
|
||||
}
|
||||
add_action( 'admin_bar_menu', 'dsi_admin_bar', 1000 );
|
||||
add_action( 'admin_bar_menu', 'ssi_admin_bar', 1000 );
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@@ -142,17 +142,17 @@ add_action( 'admin_bar_menu', 'dsi_admin_bar', 1000 );
|
||||
/**
|
||||
* Front-end + admin-bar styles for the indicator.
|
||||
*/
|
||||
function dsi_enqueue_assets() : void {
|
||||
function ssi_enqueue_assets() : void {
|
||||
// register & enqueue the standalone CSS file
|
||||
wp_enqueue_style(
|
||||
'door-status-indicator',
|
||||
'space-status-indicator',
|
||||
plugins_url( 'wp-spaceapi-consumer-style.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
|
||||
add_action( 'wp_enqueue_scripts', 'ssi_enqueue_assets' );
|
||||
add_action( 'admin_enqueue_scripts', 'ssi_enqueue_assets' ); // so the Admin Bar icon also gets styled
|
||||
|
||||
|
||||
/*
|
||||
@@ -162,8 +162,8 @@ add_action( 'admin_enqueue_scripts', 'dsi_enqueue_assets' ); // so the Admin Bar
|
||||
*/
|
||||
|
||||
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 🔴' );
|
||||
WP_CLI::add_command( 'space-status', function() {
|
||||
WP_CLI::success( ssi_get_status() ? 'Space is open 🟢' : 'Space is closed 🔴' );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user