wordpress-status-plugin/mozout-status/includes/class-display.php
2026-12-01 14:09:10 +02:00

164 lines
8.1 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* The status box on the site ([mozout_service_status] and block), its REST route for the live refresh,
* and a widget on the WordPress dashboard.
*/
class MOZOUT_STATUS_Display
{
public static function init()
{
add_shortcode('mozout_service_status', array(__CLASS__, 'shortcode'));
add_action('init', array(__CLASS__, 'register_block'));
add_action('rest_api_init', array(__CLASS__, 'routes'));
add_action('wp_dashboard_setup', array(__CLASS__, 'dashboard'));
}
public static function register_block()
{
wp_register_script('mozout-status-block', MOZOUT_STATUS_URL . 'assets/block.js', array('wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-server-side-render'), MOZOUT_STATUS_VERSION, true);
wp_localize_script('mozout-status-block', 'mozoutStatusBlock', array(
'title' => __('Service status', 'mozout-status'),
'settings' => __('Settings', 'mozout-status'),
'heading' => __('Title (empty hides it)', 'mozout-status'),
'types' => __('Show', 'mozout-status'),
'all' => __('Everything chosen in the settings', 'mozout-status'),
'api' => __('API only', 'mozout-status'),
'hosting' => __('Hosting only', 'mozout-status'),
'vps' => __('Servers only', 'mozout-status'),
'domain' => __('Domains only', 'mozout-status'),
'refresh' => __('Refresh every (seconds)', 'mozout-status'),
));
register_block_type(MOZOUT_STATUS_DIR . 'blocks/service-status', array(
'render_callback' => function ($a) {
return MOZOUT_STATUS_Display::shortcode(array(
'title' => isset($a['title']) ? $a['title'] : '',
'show' => isset($a['show']) ? $a['show'] : '',
'refresh' => isset($a['refresh']) ? $a['refresh'] : 60,
));
},
'editor_script' => 'mozout-status-block',
));
}
private static function types($csv)
{
$out = array();
foreach (preg_split('/[\s,]+/', strtolower((string) $csv)) as $t) {
if ($t === 'domains') {
$t = 'domain';
}
if (in_array($t, MOZOUT_STATUS_Monitor::TYPES, true) && !in_array($t, $out, true)) {
$out[] = $t;
}
}
return $out;
}
public static function labels($lang)
{
return array(
'ok' => MOZOUT_STATUS_I18n::in($lang, 'Everything is working'),
'warn' => MOZOUT_STATUS_I18n::in($lang, 'Some services need attention'),
'bad' => MOZOUT_STATUS_I18n::in($lang, 'Some services have a problem'),
'checked' => MOZOUT_STATUS_I18n::in($lang, 'Checked at %s'),
'error' => MOZOUT_STATUS_I18n::in($lang, 'The status could not be read now.'),
'more' => MOZOUT_STATUS_I18n::in($lang, 'More on the status page'),
);
}
public static function shortcode($atts)
{
$a = shortcode_atts(array('title' => '', 'show' => '', 'refresh' => 60), $atts, 'mozout_service_status');
$lang = MOZOUT_STATUS_I18n::lang();
$types = self::types($a['show']);
if (MOZOUT_STATUS_Settings::api_key() === '' && $types !== array('api')) {
return current_user_can('manage_options') ? '<!-- ' . esc_html__('The API key is missing. Save it in the plugin settings.', 'mozout-status') . ' -->' : '';
}
$snap = MOZOUT_STATUS_Monitor::snapshot($types, $lang);
if (!$snap['items']) {
return current_user_can('manage_options') ? '<p class="mozout-status-empty">' . esc_html__('Choose what to show in Settings > MozOut Status > Items.', 'mozout-status') . '</p>' : '';
}
$refresh = max(0, (int) $a['refresh']);
$refresh = $refresh ? max(30, min(3600, $refresh)) : 0;
$color = sanitize_hex_color((string) MOZOUT_STATUS_Settings::get('color'));
wp_enqueue_style('mozout-status-status', MOZOUT_STATUS_URL . 'assets/status.css', array(), MOZOUT_STATUS_VERSION);
wp_enqueue_script('mozout-status-status', MOZOUT_STATUS_URL . 'assets/status.js', array(), MOZOUT_STATUS_VERSION, true);
wp_localize_script('mozout-status-status', 'mozoutStatusStatus', array_merge(self::labels($lang), array(
'url' => esc_url_raw(rest_url('mozout-status/v1/status')),
'lang' => $lang,
)));
$l = self::labels($lang);
$link = (int) MOZOUT_STATUS_Settings::get('show_link') === 1 ? MOZOUT_STATUS_Brands::status_link() : '';
ob_start();
echo '<section class="mozout-status-box" data-show="' . esc_attr(implode(',', $types)) . '" data-refresh="' . esc_attr((string) $refresh) . '"' . ($color ? ' style="--mozout-status-accent:' . esc_attr($color) . '"' : '') . '>';
if ($a['title'] !== '') {
echo '<h3 class="mozout_status__title">' . esc_html($a['title']) . '</h3>';
}
echo self::inner($snap, $l);
if ($link !== '') {
echo '<p class="mozout_status__link"><a href="' . esc_url($link) . '" target="_blank" rel="noopener">' . esc_html($l['more']) . '</a></p>';
}
echo '</section>';
return ob_get_clean();
}
/** The banner and the list (also rebuilt by status.js with the same classes). */
public static function inner(array $snap, array $l)
{
$h = '<div class="mozout_status__banner is-' . esc_attr($snap['overall']) . '" role="status"><span class="mozout_status__dot" aria-hidden="true"></span><strong>' . esc_html($l[$snap['overall']]) . '</strong>'
. '<span class="mozout_status__time">' . esc_html(sprintf($l['checked'], wp_date('H:i', $snap['checked']))) . '</span></div><ul class="mozout_status__list">';
foreach ($snap['items'] as $it) {
$h .= '<li class="mozout_status__item is-' . esc_attr($it['state']) . '"><span class="mozout_status__dot" aria-hidden="true"></span><span class="mozout_status__name">' . esc_html($it['label']) . '</span><span class="mozout_status__state">' . esc_html($it['text']) . '</span></li>';
}
return $h . '</ul>';
}
public static function routes()
{
register_rest_route('mozout-status/v1', '/status', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'rest'),
'permission_callback' => '__return_true',
));
}
public static function rest(WP_REST_Request $req)
{
$lang = $req->get_param('lang') === 'pt' ? 'pt' : 'en';
$types = self::types($req->get_param('show'));
if (MOZOUT_STATUS_Settings::api_key() === '' && $types !== array('api')) {
return new WP_REST_Response(array('ok' => false), 503);
}
$snap = MOZOUT_STATUS_Monitor::snapshot($types, $lang);
$l = self::labels($lang);
return new WP_REST_Response(array(
'ok' => true,
'overall' => $snap['overall'],
'title' => $l[$snap['overall']],
'time' => sprintf($l['checked'], wp_date('H:i', $snap['checked'])),
'items' => $snap['items'],
), 200);
}
/* ---- Dashboard widget ---- */
public static function dashboard()
{
if (!current_user_can('manage_options') || (int) MOZOUT_STATUS_Settings::get('dashboard') !== 1 || !MOZOUT_STATUS_Monitor::chosen()) {
return;
}
wp_add_dashboard_widget('mozout-status-dashboard', __('Service status', 'mozout-status'), array(__CLASS__, 'dashboard_html'));
}
public static function dashboard_html()
{
$lang = MOZOUT_STATUS_I18n::lang();
wp_enqueue_style('mozout-status-status', MOZOUT_STATUS_URL . 'assets/status.css', array(), MOZOUT_STATUS_VERSION);
echo '<div class="mozout-status-box is-compact">' . self::inner(MOZOUT_STATUS_Monitor::snapshot(array(), $lang), self::labels($lang)) . '</div>';
echo '<p><a href="' . esc_url(MOZOUT_STATUS_Admin::url('items')) . '">' . esc_html__('Choose what to show', 'mozout-status') . '</a></p>';
}
}