Vigiar
1
0
Derivar
Você já tinha feito uma derivação do repositório wordpress-status-plugin, anteriormente
0
wordpress-status-plugin/interweb-mz-status/includes/class-admin.php

271 linhas
13 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* Settings page: Settings > Interweb Mozambique Status, with tabs.
*/
class INTERWEB_MZ_STATUS_Admin extends INTERWEB_MZ_STATUS_Admin_Base
{
const PAGE = 'interweb-mz-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', 'interweb-mz-status'),
'vps:read' => __('server status', 'interweb-mz-status'),
'domains:read' => __('domain status', 'interweb-mz-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 (INTERWEB_MZ_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_interweb_mz_status_save', array(__CLASS__, 'save'));
add_action('admin_post_interweb_mz_status_test', array(__CLASS__, 'test_connection'));
add_action('admin_post_interweb_mz_status_reload', array(__CLASS__, 'reload'));
add_action('admin_enqueue_scripts', array(__CLASS__, 'assets'));
add_filter('plugin_action_links_' . plugin_basename(INTERWEB_MZ_STATUS_FILE), array(__CLASS__, 'action_links'));
}
public static function menu()
{
$title = __('Interweb Mozambique Status', 'interweb-mz-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', 'interweb-mz-status') . '</a>');
return $links;
}
public static function assets($hook)
{
if ($hook !== 'settings_page_' . self::PAGE) {
return;
}
wp_enqueue_style('interweb-mz-status-admin', INTERWEB_MZ_STATUS_URL . 'assets/admin.css', array(), INTERWEB_MZ_STATUS_VERSION);
wp_enqueue_style('interweb-mz-status-status', INTERWEB_MZ_STATUS_URL . 'assets/status.css', array(), INTERWEB_MZ_STATUS_VERSION);
wp_enqueue_script('interweb-mz-status-select', INTERWEB_MZ_STATUS_URL . 'assets/select.js', array(), INTERWEB_MZ_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', 'interweb-mz-status'),
'items' => __('Items', 'interweb-mz-status'),
'display' => __('Display', 'interweb-mz-status'),
'activity' => __('Activity', 'interweb-mz-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 interweb-mz-status-wrap">';
self::header(__('Interweb Mozambique Status', 'interweb-mz-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.', 'interweb-mz-status') . '</p>';
$cat = INTERWEB_MZ_STATUS_Monitor::catalog();
$chosen = array();
foreach (INTERWEB_MZ_STATUS_Monitor::chosen() as $it) {
$chosen[$it['type'] . '|' . $it['id']] = $it['label'];
}
$groups = array(
'api' => array(__('API', 'interweb-mz-status'), array(array('id' => 'api', 'name' => __('API', 'interweb-mz-status'), 'detail' => ''))),
'hosting' => array(__('Hosting', 'interweb-mz-status'), $cat['hosting']),
'vps' => array(__('Servers', 'interweb-mz-status'), $cat['vps']),
'domain' => array(__('Domains', 'interweb-mz-status'), $cat['domain']),
);
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="interweb_mz_status_save"><input type="hidden" name="tab" value="items">';
wp_nonce_field('interweb_mz_status_save_items');
foreach ($groups as $type => $g) {
echo '<h2>' . esc_html($g[0]) . '</h2>';
if (!empty($cat['errors'][$type])) {
echo '<p class="interweb-mz-status-error">' . esc_html($cat['errors'][$type]) . '</p>';
continue;
}
if (!$g[1]) {
echo '<p class="interweb-mz-status-muted">' . esc_html__('Nothing of this kind in the account.', 'interweb-mz-status') . '</p>';
continue;
}
echo '<table class="widefat striped" style="max-width:900px"><thead><tr><th style="width:70px">' . esc_html__('Show', 'interweb-mz-status') . '</th><th>' . esc_html__('In the account', 'interweb-mz-status') . '</th><th>' . esc_html__('Name on the site', 'interweb-mz-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', 'interweb-mz-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="interweb-mz-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', 'interweb-mz-status') . '"></td></tr>';
}
echo '</tbody></table>';
}
submit_button(__('Save changes', 'interweb-mz-status'));
echo '</form>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="interweb_mz_status_reload">';
wp_nonce_field('interweb_mz_status_reload');
submit_button(__('Read the account again', 'interweb-mz-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).', 'interweb-mz-status'),
'<code>[interweb_mz_service_status]</code>'
), array('code' => array())) . '</p>';
self::form_open('display');
self::row(__('Read the state every', 'interweb-mz-status'), self::input('cache_seconds', 'number', 'min="30" max="3600" style="width:7em"') . ' ' . esc_html__('seconds', 'interweb-mz-status'), __('Visitors in between see the last reading, so the API is not asked on every visit.', 'interweb-mz-status'), 'interweb-mz-status-cache_seconds');
$link = INTERWEB_MZ_STATUS_Brands::status_link();
if ($link !== '') {
self::row(__('Status page', 'interweb-mz-status'), self::checkbox('show_link', __('Show a link to the status page of the brand', 'interweb-mz-status')), esc_html($link));
}
self::row(__('Dashboard', 'interweb-mz-status'), self::checkbox('dashboard', __('Show a status box on the WordPress dashboard', 'interweb-mz-status')));
self::row(__('Colour', 'interweb-mz-status'), self::input('color', 'color', 'style="width:5em"'), '', 'interweb-mz-status-color');
self::form_close();
if (INTERWEB_MZ_STATUS_Monitor::chosen() && INTERWEB_MZ_STATUS_Settings::api_key() !== '') {
echo '<h2>' . esc_html__('Preview', 'interweb-mz-status') . '</h2>';
$lang = INTERWEB_MZ_STATUS_I18n::lang();
echo '<div class="interweb-mz-status-box">' . INTERWEB_MZ_STATUS_Display::inner(INTERWEB_MZ_STATUS_Monitor::snapshot(array(), $lang), INTERWEB_MZ_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()
{
INTERWEB_MZ_STATUS_Monitor::forget();
}
public static function save()
{
$tab = isset($_POST['tab']) ? sanitize_key(wp_unslash($_POST['tab'])) : '';
self::check('interweb_mz_status_save_' . $tab);
$p = wp_unslash($_POST);
$ok = true;
$msg = __('Settings saved.', 'interweb-mz-status');
$more = array();
switch ($tab) {
case 'connection':
list($ok, $msg, $more) = self::save_connection($p);
INTERWEB_MZ_STATUS_Monitor::forget();
break;
case 'items':
$items = array();
foreach (INTERWEB_MZ_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));
}
}
INTERWEB_MZ_STATUS_Settings::update(array('items' => array_slice($items, 0, 50)));
INTERWEB_MZ_STATUS_Monitor::forget();
$more = self::key_warnings();
if (!$items) {
$ok = 'warning';
$msg = __('Saved. Nothing is chosen, so the status box stays empty.', 'interweb-mz-status');
}
break;
case 'display':
INTERWEB_MZ_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',
));
INTERWEB_MZ_STATUS_Monitor::forget();
break;
default:
wp_die('bad tab', 400);
}
self::redirect($tab, $ok, $msg, $more);
}
public static function reload()
{
self::check('interweb_mz_status_reload');
INTERWEB_MZ_STATUS_Monitor::forget();
$cat = INTERWEB_MZ_STATUS_Monitor::catalog(true);
if (!empty($cat['errors'])) {
self::redirect('items', 'warning', implode(' ', $cat['errors']));
}
self::redirect('items', true, __('Account read again.', 'interweb-mz-status'));
}
}