@@ -3,8 +3,8 @@
* Plugin Name: WordPress SpaceAPI Consumer
* Plugin Name: WordPress SpaceAPI Consumer
* Plugin Slug: wp-spaceapi-consumer
* Plugin Slug: wp-spaceapi-consumer
* Plugin URI: https://gitea.alluna.pt/jfig/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
* Description: WordPress plugin to consume a SpaceAPI endpoint and indicate if the Space is Open or Closed
* Version: 1.1.1
* Version: 0.3.0
* Author: Joao Figueiredo, LCD Porto Team, ChatGPT o3
* Author: Joao Figueiredo, LCD Porto Team, ChatGPT o3
* Author URI: https://lcdporto.org
* Author URI: https://lcdporto.org
* License: MIT
* License: MIT
@@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
| Configuration
| 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
| directory. If that file is missing, sane defaults are used so the plugin
| still works out‑ of‑ the‑ box.
| 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).
// Fallback defaults (only if not already defined by the config file).
! defined ( 'D SI_API_URL' ) && define ( 'D SI_API_URL' , 'https://lcdporto.org/api/spaceapi' );
! defined ( 'S SI_API_URL' ) && define ( 'S SI_API_URL' , 'https://lcdporto.org/api/spaceapi' );
! defined ( 'D SI_CACHE_KEY' ) && define ( 'D SI_CACHE_KEY' , 'd si_space_api_status' );
! defined ( 'S SI_CACHE_KEY' ) && define ( 'S SI_CACHE_KEY' , 's si_space_api_status' );
! defined ( 'D SI_CACHE_TTL' ) && define ( 'D SI_CACHE_TTL' , 10 ); // seconds
! defined ( 'S SI_CACHE_TTL' ) && define ( 'S SI_CACHE_TTL' , 10 ); // seconds
/*
/*
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
@@ -42,28 +42,33 @@ 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):
* Supported JSON payloads (any case):
* – true / false (bare boolean)
* – true / false (bare boolean)
* – {"open": true}
* – {"state": {" open": true}} (SpaceAPI standard)
* – {"doorOpen": true}
*
*
* @return bool True if open, false if closed (or on error).
* @return bool True if open, false if closed (or on error).
*/
*/
function d si_get_status() : bool {
function s si_get_status() : bool {
$cached = get_transient ( D SI_CACHE_KEY );
$cached = get_transient ( S SI_CACHE_KEY );
if ( false !== $cached ) {
if ( false !== $cached ) {
return ( bool ) $cached ;
return ( bool ) $cached ;
}
}
$response = wp_remote_get ( DSI_API_URL , [
// Get plugin data for dynamic user agent
$plugin_data = get_file_data ( __FILE__ , array (
'Version' => 'Version' ,
'TextDomain' => 'Text Domain'
), 'plugin' );
$response = wp_remote_get ( SSI_API_URL , [
'timeout' => 3 ,
'timeout' => 3 ,
'user-agent' => 'DoorStatusIndicator/1.1.1 (+https://wordpress.org/)',
'user-agent' => $plugin_data [ 'TextDomain' ] . '/' . $plugin_data [ 'Version' ] . ' (+https://wordpress.org/)',
] );
] );
if ( is_wp_error ( $response ) ) {
if ( is_wp_error ( $response ) ) {
set_transient ( D SI_CACHE_KEY, false , D SI_CACHE_TTL );
set_transient ( S SI_CACHE_KEY, false , S SI_CACHE_TTL );
return false ;
return false ;
}
}
@@ -74,14 +79,13 @@ function dsi_get_status() : bool {
if ( is_bool ( $decoded ) ) {
if ( is_bool ( $decoded ) ) {
$status = $decoded ;
$status = $decoded ;
} elseif ( is_array ( $decoded ) ) {
} elseif ( is_array ( $decoded ) ) {
if ( isset ( $decoded [ 'open' ] ) ) {
// SpaceAPI standard format - state.open
$status = ( bool ) $decoded [ 'open' ];
if ( isset ( $decoded [ 'state' ][ 'open' ] ) ) {
} elseif ( isset ( $decoded [ 'doorOpen' ] ) ) {
$status = ( bool ) $decoded [ 'state' ][ 'open' ];
$status = ( bool ) $decoded [ 'doorOpen' ];
}
}
}
}
set_transient ( D SI_CACHE_KEY, $status , D SI_CACHE_TTL );
set_transient ( S SI_CACHE_KEY, $status , S SI_CACHE_TTL );
return $status ;
return $status ;
}
}
@@ -91,17 +95,19 @@ function dsi_get_status() : bool {
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
*/
*/
function d si_shortcode() : string {
function s si_shortcode() : string {
$open = d si_get_status();
$open = s si_get_status();
$emoji = $open ? '<div class="door -status-indicator open">🟢 Open</div>' : '<div class="door -status-indicator closed">🔴 Closed</div>' ;
$emoji = $open ? '<div class="space -status-indicator open">🟢 Open</div>' : '<div class="space -status-indicator closed">🔴 Closed</div>' ;
return sprintf (
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' ,
$open ? 'open' : 'closed' ,
$emoji
$emoji
);
);
}
}
add_shortcode ( 'door _status' , 'd si_shortcode' );
add_shortcode ( 'space _status' , 's si_shortcode' );
// Keep the old shortcode for backward compatibility
add_shortcode ( 'door_status' , 'ssi_shortcode' );
/*
/*
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
@@ -109,23 +115,23 @@ add_shortcode( 'door_status', 'dsi_shortcode' );
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
*/
*/
function d si_admin_bar( WP_Admin_Bar $bar ) : void {
function s si_admin_bar( WP_Admin_Bar $bar ) : void {
if ( ! current_user_can ( 'read' ) ) {
if ( ! current_user_can ( 'read' ) ) {
return ; // Logged‑ in users only.
return ; // Logged‑ in users only.
}
}
$open = d si_get_status();
$open = s si_get_status();
$emoji = $open ? '🟢' : '🔴' ;
$emoji = $open ? '🟢' : '🔴' ;
$text = $open ? __ ( 'Door Open' , 'door-status-indicato r' ) : __ ( 'Door Closed' , 'door-status-indicato r' );
$text = $open ? __ ( 'Space Open' , 'wp-spaceapi-consume r' ) : __ ( 'Space Closed' , 'wp-spaceapi-consume r' );
$bar -> add_node ( [
$bar -> add_node ( [
'id' => 'door -status-indicator' ,
'id' => 'space -status-indicator' ,
'title' => " $emoji $text " ,
'title' => " $emoji $text " ,
'href' => '#' ,
'href' => '#' ,
'meta' => [ 'title' => $text ],
'meta' => [ 'title' => $text ],
] );
] );
}
}
add_action ( 'admin_bar_menu' , 'd si_admin_bar' , 1000 );
add_action ( 'admin_bar_menu' , 's si_admin_bar' , 1000 );
/*
/*
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
@@ -136,17 +142,17 @@ add_action( 'admin_bar_menu', 'dsi_admin_bar', 1000 );
/**
/**
* Front-end + admin-bar styles for the indicator.
* Front-end + admin-bar styles for the indicator.
*/
*/
function d si_enqueue_assets() : void {
function s si_enqueue_assets() : void {
// register & enqueue the standalone CSS file
// register & enqueue the standalone CSS file
wp_enqueue_style (
wp_enqueue_style (
'door -status-indicator' ,
'space -status-indicator' ,
plugins_url ( 'wp-spaceapi-consumer-style.css' , __FILE__ ),
plugins_url ( 'wp-spaceapi-consumer-style.css' , __FILE__ ),
[], // no dependencies
[], // no dependencies
'1.0.0' // file version
'1.0.0' // file version
);
);
}
}
add_action ( 'wp_enqueue_scripts' , 'd si_enqueue_assets' );
add_action ( 'wp_enqueue_scripts' , 's si_enqueue_assets' );
add_action ( 'admin_enqueue_scripts' , 'd si_enqueue_assets' ); // so the Admin Bar icon also gets styled
add_action ( 'admin_enqueue_scripts' , 's si_enqueue_assets' ); // so the Admin Bar icon also gets styled
/*
/*
@@ -156,8 +162,8 @@ add_action( 'admin_enqueue_scripts', 'dsi_enqueue_assets' ); // so the Admin Bar
*/
*/
if ( defined ( 'WP_CLI' ) && WP_CLI ) {
if ( defined ( 'WP_CLI' ) && WP_CLI ) {
WP_CLI :: add_command ( 'door -status' , function () {
WP_CLI :: add_command ( 'space -status' , function () {
WP_CLI :: success ( d si_get_status() ? 'Door is open 🟢' : 'Door is closed 🔴' );
WP_CLI :: success ( s si_get_status() ? 'Space is open 🟢' : 'Space is closed 🔴' );
} );
} );
}
}