271 lines
12 KiB
PHP
271 lines
12 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Settings page: Settings > MozOut Status, with tabs.
|
|
*/
|
|
class MOZOUT_STATUS_Admin extends MOZOUT_STATUS_Admin_Base
|
|
{
|
|
const PAGE = 'mozout-status';
|
|
|
|
public static function defaults()
|
|
{
|
|
return array(
|
|
'items' => array(),
|
|
'cache_seconds' => 60,
|
|
'show_link' => 1,
|
|
'dashboard' => 1,
|
|
'color' => '#d60037',
|
|
);
|
|
}
|
|
|
|
public static function needed_scopes()
|
|
{
|
|
return array(
|
|
'hosting:read' => __('hosting status', 'mozout-status'),
|
|
'vps:read' => __('server status', 'mozout-status'),
|
|
'domains:read' => __('domain status', 'mozout-status'),
|
|
);
|
|
}
|
|
|
|
public static function scopes_in_use()
|
|
{
|
|
$all = self::needed_scopes();
|
|
$map = array('hosting' => 'hosting:read', 'vps' => 'vps:read', 'domain' => 'domains:read');
|
|
$use = array();
|
|
foreach (MOZOUT_STATUS_Monitor::chosen() as $it) {
|
|
if (isset($map[$it['type']])) {
|
|
$use[$map[$it['type']]] = $all[$map[$it['type']]];
|
|
}
|
|
}
|
|
return $use;
|
|
}
|
|
|
|
public static function page_slug()
|
|
{
|
|
return self::PAGE;
|
|
}
|
|
|
|
public static function init()
|
|
{
|
|
add_action('admin_menu', array(__CLASS__, 'menu'));
|
|
add_action('admin_post_mozout_status_save', array(__CLASS__, 'save'));
|
|
add_action('admin_post_mozout_status_test', array(__CLASS__, 'test_connection'));
|
|
add_action('admin_post_mozout_status_reload', array(__CLASS__, 'reload'));
|
|
add_action('admin_enqueue_scripts', array(__CLASS__, 'assets'));
|
|
add_filter('plugin_action_links_' . plugin_basename(MOZOUT_STATUS_FILE), array(__CLASS__, 'action_links'));
|
|
}
|
|
|
|
public static function menu()
|
|
{
|
|
$title = __('MozOut Status', 'mozout-status');
|
|
add_options_page($title, self::menu_label($title), 'manage_options', self::PAGE, array(__CLASS__, 'page'));
|
|
}
|
|
|
|
public static function action_links($links)
|
|
{
|
|
array_unshift($links, '<a href="' . esc_url(self::url()) . '">' . esc_html__('Settings', 'mozout-status') . '</a>');
|
|
return $links;
|
|
}
|
|
|
|
public static function assets($hook)
|
|
{
|
|
if ($hook !== 'settings_page_' . self::PAGE) {
|
|
return;
|
|
}
|
|
wp_enqueue_style('mozout-status-admin', MOZOUT_STATUS_URL . 'assets/admin.css', array(), MOZOUT_STATUS_VERSION);
|
|
wp_enqueue_style('mozout-status-status', MOZOUT_STATUS_URL . 'assets/status.css', array(), MOZOUT_STATUS_VERSION);
|
|
wp_enqueue_script('mozout-status-select', MOZOUT_STATUS_URL . 'assets/select.js', array(), MOZOUT_STATUS_VERSION, true);
|
|
}
|
|
|
|
public static function url($tab = 'connection', $args = array())
|
|
{
|
|
return add_query_arg(array_merge(array('page' => self::PAGE, 'tab' => $tab), $args), admin_url('options-general.php'));
|
|
}
|
|
|
|
private static function tabs()
|
|
{
|
|
return array(
|
|
'connection' => __('Connection', 'mozout-status'),
|
|
'items' => __('Items', 'mozout-status'),
|
|
'display' => __('Display', 'mozout-status'),
|
|
'activity' => __('Activity', 'mozout-status'),
|
|
);
|
|
}
|
|
|
|
public static function page()
|
|
{
|
|
if (!current_user_can('manage_options')) {
|
|
return;
|
|
}
|
|
$tabs = self::tabs();
|
|
$tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : 'connection';
|
|
if (!isset($tabs[$tab])) {
|
|
$tab = 'connection';
|
|
}
|
|
echo '<div class="wrap mozout-status-wrap">';
|
|
self::header(__('MozOut Status', 'mozout-status'));
|
|
echo '<nav class="nav-tab-wrapper">';
|
|
foreach ($tabs as $id => $label) {
|
|
printf('<a href="%s" class="nav-tab %s">%s</a>', esc_url(self::url($id)), $id === $tab ? 'nav-tab-active' : '', esc_html($label));
|
|
}
|
|
echo '</nav>';
|
|
call_user_func(array(__CLASS__, 'tab_' . $tab));
|
|
echo '</div>';
|
|
}
|
|
|
|
/* ---- Items ---- */
|
|
|
|
public static function tab_items()
|
|
{
|
|
if (self::need_key_notice()) {
|
|
return;
|
|
}
|
|
echo '<p>' . esc_html__('Tick what appears on the site and give each item the name visitors see. Addresses and other details stay hidden.', 'mozout-status') . '</p>';
|
|
$cat = MOZOUT_STATUS_Monitor::catalog();
|
|
$chosen = array();
|
|
foreach (MOZOUT_STATUS_Monitor::chosen() as $it) {
|
|
$chosen[$it['type'] . '|' . $it['id']] = $it['label'];
|
|
}
|
|
$groups = array(
|
|
'api' => array(__('API', 'mozout-status'), array(array('id' => 'api', 'name' => __('API', 'mozout-status'), 'detail' => ''))),
|
|
'hosting' => array(__('Hosting', 'mozout-status'), $cat['hosting']),
|
|
'vps' => array(__('Servers', 'mozout-status'), $cat['vps']),
|
|
'domain' => array(__('Domains', 'mozout-status'), $cat['domain']),
|
|
);
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
|
|
echo '<input type="hidden" name="action" value="mozout_status_save"><input type="hidden" name="tab" value="items">';
|
|
wp_nonce_field('mozout_status_save_items');
|
|
foreach ($groups as $type => $g) {
|
|
echo '<h2>' . esc_html($g[0]) . '</h2>';
|
|
if (!empty($cat['errors'][$type])) {
|
|
echo '<p class="mozout-status-error">' . esc_html($cat['errors'][$type]) . '</p>';
|
|
continue;
|
|
}
|
|
if (!$g[1]) {
|
|
echo '<p class="mozout-status-muted">' . esc_html__('Nothing of this kind in the account.', 'mozout-status') . '</p>';
|
|
continue;
|
|
}
|
|
echo '<table class="widefat striped" style="max-width:900px"><thead><tr><th style="width:70px">' . esc_html__('Show', 'mozout-status') . '</th><th>' . esc_html__('In the account', 'mozout-status') . '</th><th>' . esc_html__('Name on the site', 'mozout-status') . '</th></tr></thead><tbody>';
|
|
foreach ($g[1] as $i => $row) {
|
|
$k = $type . '|' . $row['id'];
|
|
$on = isset($chosen[$k]);
|
|
$label = $on ? $chosen[$k] : ($type === 'api' ? __('API', 'mozout-status') : ($type === 'domain' ? $row['name'] : ''));
|
|
$field = 'items[' . esc_attr($type) . '][' . (int) $i . ']';
|
|
echo '<tr><td><input type="checkbox" name="' . esc_attr($field) . '[on]" value="1" ' . checked($on, true, false) . ' aria-label="' . esc_attr($row['name']) . '"><input type="hidden" name="' . esc_attr($field) . '[id]" value="' . esc_attr($row['id']) . '"></td>';
|
|
echo '<td><strong>' . esc_html($row['name']) . '</strong>' . ($row['detail'] !== '' && $type !== 'domain' ? '<br><span class="mozout-status-muted">' . esc_html($row['detail']) . '</span>' : '') . '</td>';
|
|
echo '<td><input type="text" class="regular-text" name="' . esc_attr($field) . '[label]" value="' . esc_attr($label) . '" maxlength="60" placeholder="' . esc_attr__('For example: Website', 'mozout-status') . '"></td></tr>';
|
|
}
|
|
echo '</tbody></table>';
|
|
}
|
|
submit_button(__('Save changes', 'mozout-status'));
|
|
echo '</form>';
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
|
|
echo '<input type="hidden" name="action" value="mozout_status_reload">';
|
|
wp_nonce_field('mozout_status_reload');
|
|
submit_button(__('Read the account again', 'mozout-status'), 'secondary', 'submit', false);
|
|
echo '</form>';
|
|
}
|
|
|
|
/* ---- Display ---- */
|
|
|
|
public static function tab_display()
|
|
{
|
|
echo '<p>' . wp_kses(sprintf(
|
|
/* translators: %s: shortcode */
|
|
__('Put the status on any page with the "Service status" block or the %s shortcode. Options: title, show (api, hosting, vps or domain) and refresh (seconds).', 'mozout-status'),
|
|
'<code>[mozout_service_status]</code>'
|
|
), array('code' => array())) . '</p>';
|
|
self::form_open('display');
|
|
self::row(__('Read the state every', 'mozout-status'), self::input('cache_seconds', 'number', 'min="30" max="3600" style="width:7em"') . ' ' . esc_html__('seconds', 'mozout-status'), __('Visitors in between see the last reading, so the API is not asked on every visit.', 'mozout-status'), 'mozout-status-cache_seconds');
|
|
$link = MOZOUT_STATUS_Brands::status_link();
|
|
if ($link !== '') {
|
|
self::row(__('Status page', 'mozout-status'), self::checkbox('show_link', __('Show a link to the status page of the brand', 'mozout-status')), esc_html($link));
|
|
}
|
|
self::row(__('Dashboard', 'mozout-status'), self::checkbox('dashboard', __('Show a status box on the WordPress dashboard', 'mozout-status')));
|
|
self::row(__('Colour', 'mozout-status'), self::input('color', 'color', 'style="width:5em"'), '', 'mozout-status-color');
|
|
self::form_close();
|
|
if (MOZOUT_STATUS_Monitor::chosen() && MOZOUT_STATUS_Settings::api_key() !== '') {
|
|
echo '<h2>' . esc_html__('Preview', 'mozout-status') . '</h2>';
|
|
$lang = MOZOUT_STATUS_I18n::lang();
|
|
echo '<div class="mozout-status-box">' . MOZOUT_STATUS_Display::inner(MOZOUT_STATUS_Monitor::snapshot(array(), $lang), MOZOUT_STATUS_Display::labels($lang)) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
|
|
}
|
|
}
|
|
|
|
public static function tab_activity()
|
|
{
|
|
self::log_table(null, 50);
|
|
}
|
|
|
|
/* ---- Saving ---- */
|
|
|
|
public static function after_key_change()
|
|
{
|
|
MOZOUT_STATUS_Monitor::forget();
|
|
}
|
|
|
|
public static function save()
|
|
{
|
|
$tab = isset($_POST['tab']) ? sanitize_key(wp_unslash($_POST['tab'])) : '';
|
|
self::check('mozout_status_save_' . $tab);
|
|
$p = wp_unslash($_POST);
|
|
$ok = true;
|
|
$msg = __('Settings saved.', 'mozout-status');
|
|
$more = array();
|
|
switch ($tab) {
|
|
case 'connection':
|
|
list($ok, $msg, $more) = self::save_connection($p);
|
|
MOZOUT_STATUS_Monitor::forget();
|
|
break;
|
|
case 'items':
|
|
$items = array();
|
|
foreach (MOZOUT_STATUS_Monitor::TYPES as $type) {
|
|
$rows = isset($p['items'][$type]) && is_array($p['items'][$type]) ? $p['items'][$type] : array();
|
|
foreach ($rows as $r) {
|
|
if (empty($r['on']) || !isset($r['id'])) {
|
|
continue;
|
|
}
|
|
$id = substr(preg_replace('/[^A-Za-z0-9.\-_]/', '', (string) $r['id']), 0, 120);
|
|
$label = sanitize_text_field(isset($r['label']) ? $r['label'] : '');
|
|
if ($id === '') {
|
|
continue;
|
|
}
|
|
$items[] = array('type' => $type, 'id' => $id, 'label' => function_exists('mb_substr') ? mb_substr($label, 0, 60) : substr($label, 0, 60));
|
|
}
|
|
}
|
|
MOZOUT_STATUS_Settings::update(array('items' => array_slice($items, 0, 50)));
|
|
MOZOUT_STATUS_Monitor::forget();
|
|
$more = self::key_warnings();
|
|
if (!$items) {
|
|
$ok = 'warning';
|
|
$msg = __('Saved. Nothing is chosen, so the status box stays empty.', 'mozout-status');
|
|
}
|
|
break;
|
|
case 'display':
|
|
MOZOUT_STATUS_Settings::update(array(
|
|
'cache_seconds' => min(3600, max(30, (int) (isset($p['cache_seconds']) ? $p['cache_seconds'] : 60))),
|
|
'show_link' => empty($p['show_link']) ? 0 : 1,
|
|
'dashboard' => empty($p['dashboard']) ? 0 : 1,
|
|
'color' => sanitize_hex_color(isset($p['color']) ? $p['color'] : '') ? sanitize_hex_color($p['color']) : '#d60037',
|
|
));
|
|
MOZOUT_STATUS_Monitor::forget();
|
|
break;
|
|
default:
|
|
wp_die('bad tab', 400);
|
|
}
|
|
self::redirect($tab, $ok, $msg, $more);
|
|
}
|
|
|
|
public static function reload()
|
|
{
|
|
self::check('mozout_status_reload');
|
|
MOZOUT_STATUS_Monitor::forget();
|
|
$cat = MOZOUT_STATUS_Monitor::catalog(true);
|
|
if (!empty($cat['errors'])) {
|
|
self::redirect('items', 'warning', implode(' ', $cat['errors']));
|
|
}
|
|
self::redirect('items', true, __('Account read again.', 'mozout-status'));
|
|
}
|
|
}
|