wordpress-account-plugin/interweb-ao-account/includes/class-admin.php

792 lines
48 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* The account in the WordPress dashboard: a menu of its own with tabs for the overview, hosting, servers, domains,
* invoices and tickets, plus the connection settings. Only administrators see it.
*/
class INTERWEB_AO_ACCOUNT_Admin extends INTERWEB_AO_ACCOUNT_Admin_Base
{
const PAGE = 'interweb-ao-account';
public static function defaults()
{
return array(
'cache_seconds' => 300,
'dashboard' => 1,
'actions' => 1,
);
}
public static function needed_scopes()
{
return array(
'account:read' => __('account name', 'interweb-ao-account'),
'hosting:read' => __('hosting list and usage', 'interweb-ao-account'),
'hosting:login' => __('open the control panel (optional)', 'interweb-ao-account'),
'vps:read' => __('servers', 'interweb-ao-account'),
'vps:write' => __('start, stop and restart servers (optional)', 'interweb-ao-account'),
'domains:read' => __('domains and nameservers', 'interweb-ao-account'),
'domains:write' => __('auto-renewal and transfer lock (optional)', 'interweb-ao-account'),
'dns:read' => __('DNS records', 'interweb-ao-account'),
'billing:read' => __('invoices and credit', 'interweb-ao-account'),
'billing:pay' => __('pay invoices with credit (optional)', 'interweb-ao-account'),
'support:read' => __('tickets', 'interweb-ao-account'),
'support:write' => __('open and reply to tickets (optional)', 'interweb-ao-account'),
);
}
public static function scopes_in_use()
{
$all = self::needed_scopes();
$use = array();
foreach (array('hosting:read', 'vps:read', 'domains:read', 'dns:read', 'billing:read', 'support:read') as $s) {
$use[$s] = $all[$s];
}
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_ao_account_save', array(__CLASS__, 'save'));
add_action('admin_post_interweb_ao_account_test', array(__CLASS__, 'test_connection'));
add_action('admin_enqueue_scripts', array(__CLASS__, 'assets'));
add_action('wp_dashboard_setup', array(__CLASS__, 'dashboard'));
add_filter('plugin_action_links_' . plugin_basename(INTERWEB_AO_ACCOUNT_FILE), array(__CLASS__, 'action_links'));
}
public static function menu()
{
$title = __('Interweb Angola Account', 'interweb-ao-account');
add_menu_page($title, $title, 'manage_options', self::PAGE, array(__CLASS__, 'page'), 'dashicons-cloud', 81);
}
public static function action_links($links)
{
array_unshift($links, '<a href="' . esc_url(self::url()) . '">' . esc_html__('Open the account', 'interweb-ao-account') . '</a>');
return $links;
}
public static function assets($hook)
{
if ($hook !== 'toplevel_page_' . self::PAGE && $hook !== 'index.php') {
return;
}
wp_enqueue_style('interweb-ao-account-admin', INTERWEB_AO_ACCOUNT_URL . 'assets/admin.css', array(), INTERWEB_AO_ACCOUNT_VERSION);
if ($hook !== 'index.php') {
wp_enqueue_script('interweb-ao-account-select', INTERWEB_AO_ACCOUNT_URL . 'assets/select.js', array(), INTERWEB_AO_ACCOUNT_VERSION, true);
wp_enqueue_script('interweb-ao-account-confirm', INTERWEB_AO_ACCOUNT_URL . 'assets/confirm.js', array(), INTERWEB_AO_ACCOUNT_VERSION, true);
}
}
public static function url($tab = 'overview', $args = array())
{
return add_query_arg(array_merge(array('page' => self::PAGE, 'tab' => $tab), $args), admin_url('admin.php'));
}
private static function tabs()
{
return array(
'overview' => __('Overview', 'interweb-ao-account'),
'hosting' => __('Hosting', 'interweb-ao-account'),
'servers' => __('Servers', 'interweb-ao-account'),
'domains' => __('Domains', 'interweb-ao-account'),
'invoices' => __('Invoices', 'interweb-ao-account'),
'tickets' => __('Tickets', 'interweb-ao-account'),
'connection' => __('Connection', 'interweb-ao-account'),
'activity' => __('Activity', 'interweb-ao-account'),
);
}
public static function page()
{
if (!current_user_can('manage_options')) {
return;
}
$tabs = self::tabs();
$tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : 'overview';
if (!isset($tabs[$tab])) {
$tab = 'overview';
}
if (INTERWEB_AO_ACCOUNT_Settings::api_key() === '' && !in_array($tab, array('connection', 'activity'), true)) {
$tab = 'connection';
}
echo '<div class="wrap interweb-ao-account-wrap">';
self::header(__('Interweb Angola Account', 'interweb-ao-account'));
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>';
if (!in_array($tab, array('connection', 'activity'), true)) {
self::refresh_button($tab);
}
call_user_func(array(__CLASS__, 'tab_' . $tab));
echo '</div>';
}
private static function refresh_button($tab)
{
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" class="interweb-ao-account-refresh">';
echo '<input type="hidden" name="action" value="interweb_ao_account_refresh"><input type="hidden" name="back" value="' . esc_attr(self::current_url()) . '">';
wp_nonce_field('interweb_ao_account_refresh');
submit_button(__('Refresh', 'interweb-ao-account'), 'secondary small', 'submit', false);
echo ' <span class="interweb-ao-account-muted">' . esc_html(sprintf(
/* translators: %d: minutes */
__('Data is kept for %d minutes.', 'interweb-ao-account'),
max(1, (int) round(INTERWEB_AO_ACCOUNT_Settings::get('cache_seconds') / 60))
)) . '</span></form>';
}
public static function current_url()
{
$args = array();
foreach (array('tab', 'id', 'status', 'all') as $k) {
if (isset($_GET[$k])) {
$args[$k] = sanitize_text_field(wp_unslash($_GET[$k]));
}
}
return self::url(isset($args['tab']) ? $args['tab'] : 'overview', array_diff_key($args, array('tab' => 1)));
}
/* ---- Small helpers ---- */
private static function error_box($e)
{
echo '<div class="notice notice-error inline"><p>' . esc_html(is_wp_error($e) ? $e->get_error_message() : (string) $e) . '</p></div>';
}
public static function date($s)
{
if (!$s) {
return '-';
}
$t = strtotime((string) $s);
return $t ? wp_date(get_option('date_format'), $t) : (string) $s;
}
/** Coloured label for a status word of the API. */
public static function badge($status)
{
$s = strtolower((string) $status);
$good = array('active', 'paid', 'running', 'open', 'answered', 'completed');
$warn = array('pending', 'pending_registration', 'pending_transfer', 'unpaid', 'payment_pending', 'starting', 'stopping', 'restarting', 'reinstalling', 'backing_up', 'customer-reply', 'in-progress', 'grace');
$bad = array('suspended', 'terminated', 'cancelled', 'expired', 'redemption', 'stopped', 'fraud', 'collections', 'refunded', 'closed');
$cls = in_array($s, $good, true) ? 'is-good' : (in_array($s, $warn, true) ? 'is-warn' : (in_array($s, $bad, true) ? 'is-bad' : ''));
return '<span class="interweb-ao-account-badge ' . esc_attr($cls) . '">' . esc_html(self::status_name($s)) . '</span>';
}
public static function status_name($s)
{
$names = array(
'active' => __('Active', 'interweb-ao-account'), 'pending' => __('Pending', 'interweb-ao-account'), 'suspended' => __('Suspended', 'interweb-ao-account'),
'terminated' => __('Closed', 'interweb-ao-account'), 'cancelled' => __('Cancelled', 'interweb-ao-account'), 'completed' => __('Completed', 'interweb-ao-account'),
'paid' => __('Paid', 'interweb-ao-account'), 'unpaid' => __('Unpaid', 'interweb-ao-account'), 'refunded' => __('Refunded', 'interweb-ao-account'),
'payment_pending' => __('Payment pending', 'interweb-ao-account'), 'collections' => __('In collections', 'interweb-ao-account'),
'running' => __('Running', 'interweb-ao-account'), 'stopped' => __('Stopped', 'interweb-ao-account'), 'starting' => __('Starting', 'interweb-ao-account'),
'stopping' => __('Stopping', 'interweb-ao-account'), 'restarting' => __('Restarting', 'interweb-ao-account'), 'unknown' => __('Unknown', 'interweb-ao-account'),
'expired' => __('Expired', 'interweb-ao-account'), 'grace' => __('Grace period', 'interweb-ao-account'), 'redemption' => __('Redemption', 'interweb-ao-account'),
'pending_registration' => __('Being registered', 'interweb-ao-account'), 'pending_transfer' => __('Being transferred', 'interweb-ao-account'),
'open' => __('Open', 'interweb-ao-account'), 'answered' => __('Answered', 'interweb-ao-account'), 'customer-reply' => __('Your reply', 'interweb-ao-account'),
'closed' => __('Closed', 'interweb-ao-account'), 'in-progress' => __('In progress', 'interweb-ao-account'),
);
return isset($names[$s]) ? $names[$s] : ucfirst(str_replace(array('_', '-'), ' ', $s));
}
public static function bytes($b)
{
return $b === null ? '-' : size_format((int) $b, 1);
}
/** A small POST button with nonce; "confirm" asks before sending. */
public static function action_button($action, array $fields, $label, $confirm = '', $class = 'button')
{
$h = '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '"' . ($confirm !== '' ? ' data-interweb-ao-account-confirm="' . esc_attr($confirm) . '"' : '') . '>';
$h .= '<input type="hidden" name="action" value="' . esc_attr($action) . '">';
foreach ($fields as $k => $v) {
$h .= '<input type="hidden" name="' . esc_attr($k) . '" value="' . esc_attr($v) . '">';
}
$h .= '<input type="hidden" name="back" value="' . esc_attr(self::current_url()) . '">';
$h .= wp_nonce_field($action, '_wpnonce', true, false);
$h .= '<button type="submit" class="' . esc_attr($class) . '">' . esc_html($label) . '</button></form>';
return $h;
}
/** Hides closed items unless "Show all" was clicked. */
private static function only_current(array $rows, $tab, array $closed)
{
$all = !empty($_GET['all']);
$kept = $all ? $rows : array_values(array_filter($rows, function ($r) use ($closed) {
return !in_array(strtolower((string) $r['status']), $closed, true);
}));
$hidden = count($rows) - count($kept);
if ($hidden > 0) {
echo '<p class="interweb-ao-account-muted">' . esc_html(sprintf(
/* translators: %d: number of hidden items */
_n('%d closed item is hidden.', '%d closed items are hidden.', $hidden, 'interweb-ao-account'),
$hidden
)) . ' <a href="' . esc_url(self::url($tab, array('all' => 1))) . '">' . esc_html__('Show all', 'interweb-ao-account') . '</a></p>';
}
return $kept;
}
private static function actions_on()
{
return (int) INTERWEB_AO_ACCOUNT_Settings::get('actions') === 1;
}
/* ---- Overview ---- */
public static function tab_overview()
{
$tiles = array();
$credit = INTERWEB_AO_ACCOUNT_Data::get('/v1/billing/credit');
$tiles[] = array(__('Credit', 'interweb-ao-account'), is_wp_error($credit) ? '-' : self::money($credit['data']['balance'], $credit['data']['currency']));
$unpaid = INTERWEB_AO_ACCOUNT_Data::rows('/v1/invoices', array('status' => 'unpaid'), 100);
$due = 0;
$cur = '';
if (!is_wp_error($unpaid)) {
foreach ($unpaid as $i) {
$due += (float) $i['balance_due'];
$cur = $i['currency'];
}
}
$tiles[] = array(__('Unpaid invoices', 'interweb-ao-account'), is_wp_error($unpaid) ? '-' : count($unpaid) . ($due > 0 ? ' · ' . self::money($due, $cur) : ''));
$services = INTERWEB_AO_ACCOUNT_Data::rows('/v1/services', array('status' => 'active'));
$tiles[] = array(__('Active services', 'interweb-ao-account'), is_wp_error($services) ? '-' : count($services));
$servers = INTERWEB_AO_ACCOUNT_Data::rows('/v1/servers');
$tiles[] = array(__('Servers', 'interweb-ao-account'), is_wp_error($servers) ? '-' : count($servers));
$domains = INTERWEB_AO_ACCOUNT_Data::rows('/v1/domains');
$soon = array();
if (!is_wp_error($domains)) {
foreach ($domains as $d) {
$days = !empty($d['expires_at']) ? (int) floor((strtotime($d['expires_at']) - time()) / DAY_IN_SECONDS) : 9999;
if (in_array(strtolower($d['status']), array('active', 'grace'), true) && $days <= 30) {
$soon[] = array($d['domain'], $days, $d['expires_at']);
}
}
}
$tiles[] = array(__('Domains', 'interweb-ao-account'), is_wp_error($domains) ? '-' : count($domains));
$tickets = INTERWEB_AO_ACCOUNT_Data::rows('/v1/tickets', array(), 100);
$open = 0;
if (!is_wp_error($tickets)) {
foreach ($tickets as $t) {
if (strtolower($t['status']) !== 'closed') {
$open++;
}
}
}
$tiles[] = array(__('Open tickets', 'interweb-ao-account'), is_wp_error($tickets) ? '-' : $open);
echo '<div class="interweb-ao-account-tiles">';
foreach ($tiles as $t) {
echo '<div class="interweb-ao-account-tile"><span>' . esc_html($t[0]) . '</span><strong>' . esc_html((string) $t[1]) . '</strong></div>';
}
echo '</div>';
if (is_wp_error($credit)) {
self::error_box($credit);
}
if ($soon) {
echo '<div class="interweb-ao-account-card"><h2>' . esc_html__('Domains that expire in the next 30 days', 'interweb-ao-account') . '</h2><ul>';
foreach ($soon as $s) {
echo '<li><a href="' . esc_url(self::url('domains', array('id' => $s[0]))) . '">' . esc_html($s[0]) . '</a> · ' . esc_html(self::date($s[2])) . '</li>';
}
echo '</ul></div>';
}
if (!is_wp_error($unpaid) && $unpaid) {
echo '<div class="interweb-ao-account-card"><h2>' . esc_html__('Unpaid invoices', 'interweb-ao-account') . '</h2>';
self::invoice_table(array_slice($unpaid, 0, 5));
echo '</div>';
}
}
/* ---- Hosting ---- */
public static function tab_hosting()
{
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id) {
self::hosting_detail($id);
return;
}
$rows = INTERWEB_AO_ACCOUNT_Data::rows('/v1/services');
if (is_wp_error($rows)) {
self::error_box($rows);
return;
}
$servers = INTERWEB_AO_ACCOUNT_Data::rows('/v1/servers');
$vps = is_wp_error($servers) ? array() : wp_list_pluck($servers, 'id');
$rows = array_values(array_filter($rows, function ($s) use ($vps) {
return !in_array($s['id'], $vps, true);
}));
$rows = self::only_current($rows, 'hosting', array('terminated', 'cancelled', 'fraud'));
if (!$rows) {
echo '<p>' . esc_html__('No hosting in this account.', 'interweb-ao-account') . '</p>';
return;
}
echo '<table class="widefat striped"><thead><tr><th>' . esc_html__('Plan', 'interweb-ao-account') . '</th><th>' . esc_html__('Domain', 'interweb-ao-account') . '</th><th>' . esc_html__('Status', 'interweb-ao-account') . '</th><th>' . esc_html__('Next due date', 'interweb-ao-account') . '</th><th>' . esc_html__('Price', 'interweb-ao-account') . '</th></tr></thead><tbody>';
foreach ($rows as $s) {
echo '<tr><td><a href="' . esc_url(self::url('hosting', array('id' => $s['id']))) . '"><strong>' . esc_html(isset($s['product']['name']) ? $s['product']['name'] : '#' . $s['id']) . '</strong></a></td><td>' . esc_html($s['domain'] ? $s['domain'] : '-') . '</td><td>' . self::badge($s['status']) . '</td><td>' . esc_html(self::date($s['next_due_date'])) . '</td><td>' . esc_html(self::money($s['amount'], $s['currency'])) . '</td></tr>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
}
echo '</tbody></table>';
}
private static function hosting_detail($id)
{
echo '<p><a href="' . esc_url(self::url('hosting')) . '">&larr; ' . esc_html__('All hosting', 'interweb-ao-account') . '</a></p>';
$res = INTERWEB_AO_ACCOUNT_Data::get('/v1/services/' . $id);
if (is_wp_error($res)) {
self::error_box($res);
return;
}
$s = $res['data'];
echo '<div class="interweb-ao-account-card"><h2>' . esc_html(isset($s['product']['name']) ? $s['product']['name'] : '#' . $id) . ' ' . self::badge($s['status']) . '</h2><table class="form-table" role="presentation">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
$rows = array(
__('Domain', 'interweb-ao-account') => $s['domain'] ? $s['domain'] : '-',
__('User name', 'interweb-ao-account') => $s['username'] ? $s['username'] : '-',
__('Billing cycle', 'interweb-ao-account') => $s['billing_cycle'],
__('Next due date', 'interweb-ao-account') => self::date($s['next_due_date']),
__('Price', 'interweb-ao-account') => self::money($s['amount'], $s['currency']),
);
foreach ($rows as $k => $v) {
echo '<tr><th>' . esc_html($k) . '</th><td>' . esc_html((string) $v) . '</td></tr>';
}
echo '</table>';
if (strtolower($s['status']) === 'active' && self::actions_on()) {
echo '<div class="interweb-ao-account-actions">' . self::action_button('interweb_ao_account_cpanel', array('id' => $id), __('Open the control panel', 'interweb-ao-account'), '', 'button button-primary') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
echo '<p class="description">' . esc_html__('Opens a link that works once. Keep it private.', 'interweb-ao-account') . '</p>';
}
echo '</div>';
if (strtolower($s['status']) === 'active') {
$u = INTERWEB_AO_ACCOUNT_Data::get('/v1/services/' . $id . '/usage');
if (!is_wp_error($u)) {
$d = $u['data'];
echo '<div class="interweb-ao-account-tiles">';
echo '<div class="interweb-ao-account-tile"><span>' . esc_html__('Disk', 'interweb-ao-account') . '</span><strong>' . esc_html(self::bytes($d['disk']['used_bytes']) . ' / ' . ($d['disk']['limit_bytes'] === null ? __('no limit', 'interweb-ao-account') : self::bytes($d['disk']['limit_bytes']))) . '</strong></div>';
echo '<div class="interweb-ao-account-tile"><span>' . esc_html__('Traffic this month', 'interweb-ao-account') . '</span><strong>' . esc_html(self::bytes($d['bandwidth']['used_bytes']) . ' / ' . ($d['bandwidth']['limit_bytes'] === null ? __('no limit', 'interweb-ao-account') : self::bytes($d['bandwidth']['limit_bytes']))) . '</strong></div>';
echo '</div>';
}
}
}
/* ---- Servers ---- */
public static function tab_servers()
{
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id) {
self::server_detail($id);
return;
}
$rows = INTERWEB_AO_ACCOUNT_Data::rows('/v1/servers');
if (is_wp_error($rows)) {
self::error_box($rows);
return;
}
if (!$rows) {
echo '<p>' . esc_html__('No servers in this account.', 'interweb-ao-account') . '</p>';
return;
}
echo '<table class="widefat striped"><thead><tr><th>' . esc_html__('Name', 'interweb-ao-account') . '</th><th>' . esc_html__('Plan', 'interweb-ao-account') . '</th><th>IP</th><th>' . esc_html__('State', 'interweb-ao-account') . '</th><th>' . esc_html__('Next due date', 'interweb-ao-account') . '</th></tr></thead><tbody>';
foreach ($rows as $s) {
echo '<tr><td><a href="' . esc_url(self::url('servers', array('id' => $s['id']))) . '"><strong>' . esc_html($s['name'] !== '' ? $s['name'] : '#' . $s['id']) . '</strong></a></td><td>' . esc_html($s['product']) . '</td><td><code>' . esc_html($s['ip']) . '</code></td><td>' . self::badge(strtolower($s['status']) === 'suspended' ? 'suspended' : $s['power_state']) . '</td><td>' . esc_html(self::date($s['next_due_date'])) . '</td></tr>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
}
echo '</tbody></table>';
}
private static function server_detail($id)
{
echo '<p><a href="' . esc_url(self::url('servers')) . '">&larr; ' . esc_html__('All servers', 'interweb-ao-account') . '</a></p>';
$res = INTERWEB_AO_ACCOUNT_Data::get('/v1/servers/' . $id, array(), 30);
if (is_wp_error($res)) {
self::error_box($res);
return;
}
$s = $res['data'];
$os = isset($s['operating_system']['name']) ? trim($s['operating_system']['name'] . ' ' . (isset($s['operating_system']['version']) ? $s['operating_system']['version'] : '')) : '-';
$r = isset($s['resources']) && is_array($s['resources']) ? $s['resources'] : array();
echo '<div class="interweb-ao-account-card"><h2>' . esc_html($s['name'] !== '' ? $s['name'] : '#' . $id) . ' ' . self::badge(strtolower($s['status']) === 'suspended' ? 'suspended' : $s['power_state']) . '</h2><table class="form-table" role="presentation">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
$rows = array(
__('Plan', 'interweb-ao-account') => $s['product'],
'IP' => $s['ip'] . (!empty($s['additional_ips']) ? ' + ' . implode(', ', (array) $s['additional_ips']) : ''),
__('Operating system', 'interweb-ao-account') => $os,
__('Location', 'interweb-ao-account') => isset($s['location']) ? $s['location'] : '-',
__('Resources', 'interweb-ao-account') => $r ? sprintf(
/* translators: 1: vCPU, 2: RAM in GB, 3: disk in GB */
__('%1$d vCPU, %2$d GB RAM, %3$d GB disk', 'interweb-ao-account'),
isset($r['vcpu']) ? $r['vcpu'] : 0,
isset($r['ram_gb']) ? $r['ram_gb'] : 0,
isset($r['disk_gb']) ? $r['disk_gb'] : 0
) : '-',
__('Next due date', 'interweb-ao-account') => self::date($s['next_due_date']),
);
foreach ($rows as $k => $v) {
echo '<tr><th>' . esc_html($k) . '</th><td>' . esc_html((string) $v) . '</td></tr>';
}
echo '</table>';
if (!empty($s['state_stale'])) {
echo '<p class="description">' . esc_html__('The state could not be read now. This is the last known state.', 'interweb-ao-account') . '</p>';
}
if (strtolower($s['status']) !== 'suspended' && self::actions_on()) {
echo '<div class="interweb-ao-account-actions">';
if ($s['power_state'] === 'stopped') {
echo self::action_button('interweb_ao_account_power', array('id' => $id, 'op' => 'start'), __('Start', 'interweb-ao-account'), '', 'button button-primary'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
} else {
echo self::action_button('interweb_ao_account_power', array('id' => $id, 'op' => 'restart'), __('Restart', 'interweb-ao-account'), __('Restart this server now? Sites on it stop for a moment.', 'interweb-ao-account')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
echo self::action_button('interweb_ao_account_power', array('id' => $id, 'op' => 'stop'), __('Stop', 'interweb-ao-account'), __('Stop this server? Everything on it goes offline until you start it again.', 'interweb-ao-account')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
}
echo '</div>';
}
if (!empty($s['portal_url'])) {
echo '<p><a href="' . esc_url($s['portal_url']) . '" target="_blank" rel="noopener">' . esc_html(sprintf(
/* translators: %s: name of the customer area */
__('More options in %s', 'interweb-ao-account'),
INTERWEB_AO_ACCOUNT_Brands::portal_name()
)) . '</a></p>';
}
echo '</div>';
}
/* ---- Domains ---- */
public static function tab_domains()
{
$id = isset($_GET['id']) ? sanitize_text_field(wp_unslash($_GET['id'])) : '';
if ($id !== '') {
self::domain_detail(strtolower(preg_replace('/[^A-Za-z0-9.\-]/', '', $id)));
return;
}
$rows = INTERWEB_AO_ACCOUNT_Data::rows('/v1/domains');
if (is_wp_error($rows)) {
self::error_box($rows);
return;
}
$rows = self::only_current($rows, 'domains', array('cancelled', 'transferred_away', 'fraud'));
if (!$rows) {
echo '<p>' . esc_html__('No domains in this account.', 'interweb-ao-account') . '</p>';
return;
}
echo '<table class="widefat striped"><thead><tr><th>' . esc_html__('Domain', 'interweb-ao-account') . '</th><th>' . esc_html__('Status', 'interweb-ao-account') . '</th><th>' . esc_html__('Expires', 'interweb-ao-account') . '</th><th>' . esc_html__('Auto-renewal', 'interweb-ao-account') . '</th><th>' . esc_html__('Renewal', 'interweb-ao-account') . '</th></tr></thead><tbody>';
foreach ($rows as $d) {
echo '<tr><td><a href="' . esc_url(self::url('domains', array('id' => $d['domain']))) . '"><strong>' . esc_html($d['domain']) . '</strong></a></td><td>' . self::badge($d['status']) . '</td><td>' . esc_html(self::date($d['expires_at'])) . '</td><td>' . esc_html(!empty($d['auto_renew']) ? __('On', 'interweb-ao-account') : __('Off', 'interweb-ao-account')) . '</td><td>' . esc_html(self::money($d['renewal_amount'], $d['currency'])) . '</td></tr>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
}
echo '</tbody></table>';
}
private static function domain_detail($domain)
{
echo '<p><a href="' . esc_url(self::url('domains')) . '">&larr; ' . esc_html__('All domains', 'interweb-ao-account') . '</a></p>';
$res = INTERWEB_AO_ACCOUNT_Data::get('/v1/domains/' . rawurlencode($domain));
if (is_wp_error($res)) {
self::error_box($res);
return;
}
$d = $res['data'];
echo '<div class="interweb-ao-account-card"><h2>' . esc_html($d['domain']) . ' ' . self::badge($d['status']) . '</h2><table class="form-table" role="presentation">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
$lock = INTERWEB_AO_ACCOUNT_Data::get('/v1/domains/' . rawurlencode($domain) . '/lock', array(), 60);
$locked = is_wp_error($lock) ? null : (isset($lock['data']['locked']) ? $lock['data']['locked'] : null);
$rows = array(
__('Registered on', 'interweb-ao-account') => self::date($d['registered_at']),
__('Expires', 'interweb-ao-account') => self::date($d['expires_at']),
/* translators: %d: number of years */
__('Renewal', 'interweb-ao-account') => self::money($d['renewal_amount'], $d['currency']) . ' (' . sprintf(_n('%d year', '%d years', (int) $d['period_years'], 'interweb-ao-account'), (int) $d['period_years']) . ')',
__('Auto-renewal', 'interweb-ao-account') => !empty($d['auto_renew']) ? __('On', 'interweb-ao-account') : __('Off', 'interweb-ao-account'),
__('Transfer lock', 'interweb-ao-account') => $locked === null ? '-' : ($locked ? __('On', 'interweb-ao-account') : __('Off', 'interweb-ao-account')),
);
foreach ($rows as $k => $v) {
echo '<tr><th>' . esc_html($k) . '</th><td>' . esc_html((string) $v) . '</td></tr>';
}
echo '</table>';
if (self::actions_on() && in_array(strtolower($d['status']), array('active', 'grace'), true)) {
echo '<div class="interweb-ao-account-actions">';
echo self::action_button('interweb_ao_account_autorenew', array('domain' => $domain, 'on' => empty($d['auto_renew']) ? 1 : 0), empty($d['auto_renew']) ? __('Turn auto-renewal on', 'interweb-ao-account') : __('Turn auto-renewal off', 'interweb-ao-account')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
if ($locked !== null) {
echo self::action_button('interweb_ao_account_lock', array('domain' => $domain, 'on' => $locked ? 0 : 1), $locked ? __('Remove the transfer lock', 'interweb-ao-account') : __('Turn the transfer lock on', 'interweb-ao-account'), $locked ? __('Without the lock the domain can be transferred away. Continue?', 'interweb-ao-account') : ''); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
}
echo '</div>';
}
echo '</div>';
$ns = INTERWEB_AO_ACCOUNT_Data::get('/v1/domains/' . rawurlencode($domain) . '/nameservers', array(), 120);
echo '<div class="interweb-ao-account-card"><h2>' . esc_html__('Nameservers', 'interweb-ao-account') . '</h2>';
if (is_wp_error($ns)) {
echo '<p class="interweb-ao-account-muted">' . esc_html($ns->get_error_message()) . '</p>';
} else {
echo '<p>' . implode('<br>', array_map(function ($n) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
return '<code>' . esc_html($n) . '</code>';
}, (array) $ns['data']['nameservers'])) . '</p>';
}
echo '</div>';
$dns = INTERWEB_AO_ACCOUNT_Data::get('/v1/domains/' . rawurlencode($domain) . '/dns', array(), 120);
echo '<div class="interweb-ao-account-card"><h2>' . esc_html__('DNS records', 'interweb-ao-account') . '</h2>';
if (is_wp_error($dns)) {
echo '<p class="interweb-ao-account-muted">' . esc_html($dns->get_error_message()) . '</p>';
} elseif (!$dns['data']) {
echo '<p class="interweb-ao-account-muted">' . esc_html__('No records.', 'interweb-ao-account') . '</p>';
} else {
echo '<table class="widefat striped"><thead><tr><th>' . esc_html__('Name', 'interweb-ao-account') . '</th><th>' . esc_html__('Type', 'interweb-ao-account') . '</th><th>' . esc_html__('Value', 'interweb-ao-account') . '</th><th>TTL</th></tr></thead><tbody>';
foreach ($dns['data'] as $r) {
echo '<tr><td>' . esc_html($r['name']) . '</td><td>' . esc_html($r['type']) . '</td><td style="word-break:break-all">' . esc_html((!empty($r['priority']) ? $r['priority'] . ' ' : '') . $r['value']) . '</td><td>' . esc_html((string) $r['ttl']) . '</td></tr>';
}
echo '</tbody></table>';
echo '<p class="description">' . esc_html(sprintf(
/* translators: %s: name of the customer area */
__('To change records, use %s or the DNS tools of the API.', 'interweb-ao-account'),
INTERWEB_AO_ACCOUNT_Brands::portal_name()
)) . '</p>';
}
echo '</div>';
}
/* ---- Invoices ---- */
public static function tab_invoices()
{
$status = isset($_GET['status']) ? sanitize_key(wp_unslash($_GET['status'])) : '';
$filters = array('' => __('All', 'interweb-ao-account'), 'unpaid' => __('Unpaid', 'interweb-ao-account'), 'paid' => __('Paid', 'interweb-ao-account'));
echo '<p class="interweb-ao-account-filters">';
foreach ($filters as $k => $v) {
echo $k === $status ? '<strong>' . esc_html($v) . '</strong> ' : '<a href="' . esc_url(self::url('invoices', $k !== '' ? array('status' => $k) : array())) . '">' . esc_html($v) . '</a> ';
}
echo '</p>';
$credit = INTERWEB_AO_ACCOUNT_Data::get('/v1/billing/credit');
if (!is_wp_error($credit)) {
echo '<p>' . esc_html(sprintf(
/* translators: %s: amount */
__('Credit available: %s', 'interweb-ao-account'),
self::money($credit['data']['balance'], $credit['data']['currency'])
)) . '</p>';
}
$rows = INTERWEB_AO_ACCOUNT_Data::rows('/v1/invoices', $status !== '' ? array('status' => $status) : array(), 100);
if (is_wp_error($rows)) {
self::error_box($rows);
return;
}
if (!$rows) {
echo '<p>' . esc_html__('No invoices.', 'interweb-ao-account') . '</p>';
return;
}
self::invoice_table($rows);
}
private static function invoice_table(array $rows)
{
echo '<table class="widefat striped"><thead><tr><th>' . esc_html__('Invoice', 'interweb-ao-account') . '</th><th>' . esc_html__('Date', 'interweb-ao-account') . '</th><th>' . esc_html__('Due date', 'interweb-ao-account') . '</th><th>' . esc_html__('Total', 'interweb-ao-account') . '</th><th>' . esc_html__('To pay', 'interweb-ao-account') . '</th><th>' . esc_html__('Status', 'interweb-ao-account') . '</th><th></th></tr></thead><tbody>';
foreach ($rows as $i) {
$num = $i['number'] !== '' ? $i['number'] : '#' . $i['id'];
echo '<tr><td><strong>' . esc_html($num) . '</strong></td><td>' . esc_html(self::date($i['date'])) . '</td><td>' . esc_html(self::date($i['due_date'])) . '</td><td>' . esc_html(self::money($i['total'], $i['currency'])) . '</td><td>' . esc_html(self::money($i['balance_due'], $i['currency'])) . '</td><td>' . self::badge($i['status']) . '</td><td class="interweb-ao-account-actions">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
echo self::action_button('interweb_ao_account_pdf', array('id' => $i['id']), __('PDF', 'interweb-ao-account'), '', 'button button-small'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
if (strtolower($i['status']) === 'unpaid' && (float) $i['balance_due'] > 0 && self::actions_on()) {
echo self::action_button('interweb_ao_account_pay', array('id' => $i['id']), __('Pay with credit', 'interweb-ao-account'), sprintf( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
/* translators: 1: amount, 2: invoice number */
__('Pay %1$s of invoice %2$s with the account credit?', 'interweb-ao-account'), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
self::money($i['balance_due'], $i['currency']), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
$num // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
), 'button button-small button-primary');
}
echo '</td></tr>';
}
echo '</tbody></table>';
}
/* ---- Tickets ---- */
public static function tab_tickets()
{
$id = isset($_GET['id']) ? preg_replace('/[^A-Za-z0-9\-]/', '', (string) wp_unslash($_GET['id'])) : '';
if ($id === 'new') {
self::ticket_new();
return;
}
if ($id !== '') {
self::ticket_detail($id);
return;
}
echo '<p><a class="button button-primary" href="' . esc_url(self::url('tickets', array('id' => 'new'))) . '">' . esc_html__('Open a ticket', 'interweb-ao-account') . '</a></p>';
$rows = INTERWEB_AO_ACCOUNT_Data::rows('/v1/tickets', array(), 100);
if (is_wp_error($rows)) {
self::error_box($rows);
return;
}
if (!$rows) {
echo '<p>' . esc_html__('No tickets.', 'interweb-ao-account') . '</p>';
return;
}
echo '<table class="widefat striped"><thead><tr><th>' . esc_html__('Subject', 'interweb-ao-account') . '</th><th>' . esc_html__('Department', 'interweb-ao-account') . '</th><th>' . esc_html__('Status', 'interweb-ao-account') . '</th><th>' . esc_html__('Last reply', 'interweb-ao-account') . '</th></tr></thead><tbody>';
foreach ($rows as $t) {
echo '<tr><td><a href="' . esc_url(self::url('tickets', array('id' => $t['id']))) . '"><strong>' . esc_html($t['subject']) . '</strong></a> <span class="interweb-ao-account-muted">#' . esc_html($t['id']) . '</span>' . (!empty($t['unread']) ? ' <span class="interweb-ao-account-badge is-warn">' . esc_html__('New reply', 'interweb-ao-account') . '</span>' : '') . '</td><td>' . esc_html(isset($t['department']['name']) ? $t['department']['name'] : '') . '</td><td>' . self::badge($t['status']) . '</td><td>' . esc_html(self::date($t['last_reply_at'])) . '</td></tr>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
}
echo '</tbody></table>';
}
private static function ticket_detail($id)
{
echo '<p><a href="' . esc_url(self::url('tickets')) . '">&larr; ' . esc_html__('All tickets', 'interweb-ao-account') . '</a></p>';
$res = INTERWEB_AO_ACCOUNT_Data::get('/v1/tickets/' . rawurlencode($id), array(), 60);
if (is_wp_error($res)) {
self::error_box($res);
return;
}
$t = $res['data'];
echo '<h2>' . esc_html($t['subject']) . ' ' . self::badge($t['status']) . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
foreach ((array) $t['messages'] as $m) {
$from = isset($m['from']) && $m['from'] === 'staff' ? 'is-staff' : '';
echo '<div class="interweb-ao-account-card interweb-ao-account-msg ' . esc_attr($from) . '"><p class="interweb-ao-account-muted"><strong>' . esc_html($m['author']) . '</strong> · ' . esc_html(self::date($m['created_at'])) . '</p>' . wpautop(esc_html($m['message'])); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
if (!empty($m['attachments'])) {
echo '<p class="interweb-ao-account-muted">' . esc_html(sprintf(
/* translators: %s: file names */
__('Attachments: %s', 'interweb-ao-account'),
implode(', ', array_map('strval', (array) $m['attachments']))
)) . '</p>';
}
echo '</div>';
}
if (self::actions_on()) {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" class="interweb-ao-account-card">';
echo '<input type="hidden" name="action" value="interweb_ao_account_reply"><input type="hidden" name="id" value="' . esc_attr($id) . '"><input type="hidden" name="back" value="' . esc_attr(self::current_url()) . '">';
wp_nonce_field('interweb_ao_account_reply');
echo '<p><label for="interweb-ao-account-reply"><strong>' . esc_html__('Your reply', 'interweb-ao-account') . '</strong></label></p><textarea id="interweb-ao-account-reply" name="message" rows="6" class="large-text" required></textarea>';
if (strtolower($t['status']) === 'closed') {
echo '<p class="description">' . esc_html__('Replying opens the ticket again.', 'interweb-ao-account') . '</p>';
}
submit_button(__('Send reply', 'interweb-ao-account'));
echo '</form>';
}
}
private static function ticket_new()
{
echo '<p><a href="' . esc_url(self::url('tickets')) . '">&larr; ' . esc_html__('All tickets', 'interweb-ao-account') . '</a></p>';
$deps = INTERWEB_AO_ACCOUNT_Data::get('/v1/support/departments', array(), 3600);
if (is_wp_error($deps)) {
self::error_box($deps);
return;
}
$opts = array();
foreach ((array) $deps['data'] as $d) {
$opts[(string) $d['id']] = $d['name'];
}
$about = array('0' => __('Nothing in particular', 'interweb-ao-account'));
$services = INTERWEB_AO_ACCOUNT_Data::rows('/v1/services');
if (!is_wp_error($services)) {
foreach ($services as $s) {
$about['s' . $s['id']] = (isset($s['product']['name']) ? $s['product']['name'] : '#' . $s['id']) . ($s['domain'] ? ' · ' . $s['domain'] : '');
}
}
$domains = INTERWEB_AO_ACCOUNT_Data::rows('/v1/domains');
if (!is_wp_error($domains)) {
foreach ($domains as $d) {
$about['d' . $d['id']] = $d['domain'];
}
}
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="interweb_ao_account_ticket"><input type="hidden" name="back" value="' . esc_attr(self::url('tickets')) . '">';
wp_nonce_field('interweb_ao_account_ticket');
echo '<table class="form-table" role="presentation">';
self::row(__('Department', 'interweb-ao-account'), self::select('department_id', $opts, '', 'interweb-ao-account-dep'), '', 'interweb-ao-account-dep');
self::row(__('About', 'interweb-ao-account'), self::select('about', $about, '0', 'interweb-ao-account-about'), '', 'interweb-ao-account-about');
self::row(__('Priority', 'interweb-ao-account'), self::select('priority', array('low' => __('Low', 'interweb-ao-account'), 'medium' => __('Medium', 'interweb-ao-account'), 'high' => __('High', 'interweb-ao-account')), 'medium', 'interweb-ao-account-priority'), '', 'interweb-ao-account-priority');
self::row(__('Subject', 'interweb-ao-account'), '<input type="text" id="interweb-ao-account-subject" name="subject" class="regular-text" maxlength="200" required>', '', 'interweb-ao-account-subject');
self::row(__('Message', 'interweb-ao-account'), '<textarea id="interweb-ao-account-message" name="message" rows="8" class="large-text" required></textarea>', '', 'interweb-ao-account-message');
echo '</table>';
submit_button(__('Open the ticket', 'interweb-ao-account'));
echo '</form>';
}
/* ---- Connection and activity ---- */
public static function tab_connection()
{
parent::tab_connection();
echo '<h2>' . esc_html__('Options', 'interweb-ao-account') . '</h2>';
self::form_open('options');
self::row(__('Keep data for', 'interweb-ao-account'), self::input('cache_seconds', 'number', 'min="60" max="3600" step="60" style="width:7em"') . ' ' . esc_html__('seconds', 'interweb-ao-account'), '', 'interweb-ao-account-cache_seconds');
self::row(__('Actions', 'interweb-ao-account'), self::checkbox('actions', __('Show the action buttons (restart, pay, reply and others)', 'interweb-ao-account')), __('Turn it off for a view without changes.', 'interweb-ao-account'));
self::row(__('Dashboard', 'interweb-ao-account'), self::checkbox('dashboard', __('Show a summary box on the WordPress dashboard', 'interweb-ao-account')));
self::form_close();
}
public static function tab_activity()
{
self::log_table(null, 50);
}
public static function after_key_change()
{
INTERWEB_AO_ACCOUNT_Data::forget();
}
public static function save()
{
$tab = isset($_POST['tab']) ? sanitize_key(wp_unslash($_POST['tab'])) : '';
self::check('interweb_ao_account_save_' . $tab);
$p = wp_unslash($_POST);
$ok = true;
$msg = __('Settings saved.', 'interweb-ao-account');
$more = array();
if ($tab === 'connection') {
list($ok, $msg, $more) = self::save_connection($p);
INTERWEB_AO_ACCOUNT_Data::forget();
} elseif ($tab === 'options') {
INTERWEB_AO_ACCOUNT_Settings::update(array(
'cache_seconds' => min(3600, max(60, (int) (isset($p['cache_seconds']) ? $p['cache_seconds'] : 300))),
'actions' => empty($p['actions']) ? 0 : 1,
'dashboard' => empty($p['dashboard']) ? 0 : 1,
));
INTERWEB_AO_ACCOUNT_Data::forget();
$tab = 'connection';
} else {
wp_die('bad tab', 400);
}
self::redirect($tab, $ok, $msg, $more);
}
/* ---- Dashboard widget ---- */
public static function dashboard()
{
if (!current_user_can('manage_options') || (int) INTERWEB_AO_ACCOUNT_Settings::get('dashboard') !== 1 || INTERWEB_AO_ACCOUNT_Settings::api_key() === '') {
return;
}
wp_add_dashboard_widget('interweb-ao-account-dashboard', __('Interweb Angola Account', 'interweb-ao-account'), array(__CLASS__, 'dashboard_html'));
}
public static function dashboard_html()
{
echo '<div class="interweb-ao-account-wrap">';
$credit = INTERWEB_AO_ACCOUNT_Data::get('/v1/billing/credit');
if (is_wp_error($credit)) {
echo '<p>' . esc_html($credit->get_error_message()) . '</p></div>';
return;
}
$unpaid = INTERWEB_AO_ACCOUNT_Data::rows('/v1/invoices', array('status' => 'unpaid'), 100);
$due = 0;
foreach (is_wp_error($unpaid) ? array() : $unpaid as $i) {
$due += (float) $i['balance_due'];
}
echo '<p>' . esc_html(sprintf(
/* translators: %s: amount */
__('Credit available: %s', 'interweb-ao-account'),
self::money($credit['data']['balance'], $credit['data']['currency'])
)) . '</p>';
if (!is_wp_error($unpaid) && $unpaid) {
echo '<p><strong>' . esc_html(sprintf(
/* translators: 1: number of invoices, 2: amount */
_n('%1$d unpaid invoice, %2$s to pay.', '%1$d unpaid invoices, %2$s to pay.', count($unpaid), 'interweb-ao-account'),
count($unpaid),
self::money($due, $credit['data']['currency'])
)) . '</strong></p>';
} else {
echo '<p>' . esc_html__('No unpaid invoices.', 'interweb-ao-account') . '</p>';
}
echo '<p><a class="button" href="' . esc_url(self::url()) . '">' . esc_html__('Open the account', 'interweb-ao-account') . '</a></p></div>';
}
}