'Version',
'TextDomain' => 'Text Domain'
), 'plugin' );
$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( SSI_CACHE_KEY, false, SSI_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 ) ) {
// SpaceAPI standard format - state.open
if ( isset( $decoded['state']['open'] ) ) {
$status = (bool) $decoded['state']['open'];
}
}
set_transient( SSI_CACHE_KEY, $status, SSI_CACHE_TTL );
return $status;
}
/*
|--------------------------------------------------------------------------
| Shortcode [door_status]
|--------------------------------------------------------------------------
*/
function ssi_shortcode() : string {
$open = ssi_get_status();
$emoji = $open ? '
🟢 Open
' : '🔴 Closed
';
return sprintf(
'%s',
$open ? 'open' : 'closed',
$emoji
);
}
add_shortcode( 'space_status', 'ssi_shortcode' );
// Keep the old shortcode for backward compatibility
add_shortcode( 'door_status', 'ssi_shortcode' );
/*
|--------------------------------------------------------------------------
| Admin Bar indicator
|--------------------------------------------------------------------------
*/
function ssi_admin_bar( WP_Admin_Bar $bar ) : void {
if ( ! current_user_can( 'read' ) ) {
return; // Logged-in users only.
}
$open = ssi_get_status();
$emoji = $open ? '🟢' : '🔴';
$text = $open ? __( 'Space Open', 'wp-spaceapi-consumer' ) : __( 'Space Closed', 'wp-spaceapi-consumer' );
$bar->add_node( [
'id' => 'space-status-indicator',
'title' => "$emoji $text",
'href' => '#',
'meta' => [ 'title' => $text ],
] );
}
add_action( 'admin_bar_menu', 'ssi_admin_bar', 1000 );
/*
|--------------------------------------------------------------------------
| Front-end inline CSS (kept minimal)
|--------------------------------------------------------------------------
*/
/**
* Front-end + admin-bar styles for the indicator.
*/
function ssi_enqueue_assets() : void {
// register & enqueue the standalone CSS file
wp_enqueue_style(
'space-status-indicator',
plugins_url( 'wp-spaceapi-consumer-style.css', __FILE__ ),
[], // no dependencies
'1.0.0' // file version
);
}
add_action( 'wp_enqueue_scripts', 'ssi_enqueue_assets' );
add_action( 'admin_enqueue_scripts', 'ssi_enqueue_assets' ); // so the Admin Bar icon also gets styled
/*
|--------------------------------------------------------------------------
| WP-CLI command (optional)
|--------------------------------------------------------------------------
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'space-status', function() {
WP_CLI::success( ssi_get_status() ? 'Space is open 🟢' : 'Space is closed 🔴' );
} );
}
// Gutenberg block support (dynamic Space Status block)
if ( function_exists( 'register_block_type' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'blocks-register.php';
}
// End of file