296 lines
16 KiB
PHP
296 lines
16 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Settings page: Settings > Interweb Mozambique Domains, with tabs.
|
|
*/
|
|
class INTERWEB_MZ_DOMAINS_Admin extends INTERWEB_MZ_DOMAINS_Admin_Base
|
|
{
|
|
const PAGE = 'interweb-mz-domains';
|
|
|
|
public static function defaults()
|
|
{
|
|
return array(
|
|
'extensions' => '',
|
|
'currency' => '',
|
|
'price_mode' => 'public',
|
|
'margin' => 0,
|
|
'round_up' => 0,
|
|
'buy_target' => 'brand',
|
|
'buy_url' => '',
|
|
'show_whois' => 1,
|
|
'show_transfer' => 1,
|
|
'per_hour' => 30,
|
|
'color' => '#d60037',
|
|
);
|
|
}
|
|
|
|
public static function needed_scopes()
|
|
{
|
|
return array(
|
|
'catalog:read' => __('price list and your prices', 'interweb-mz-domains'),
|
|
'reseller:read' => __('your Agency in the Prices tab (optional)', 'interweb-mz-domains'),
|
|
);
|
|
}
|
|
|
|
public static function scopes_in_use()
|
|
{
|
|
$all = self::needed_scopes();
|
|
return array('catalog:read' => $all['catalog:read']);
|
|
}
|
|
|
|
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_domains_save', array(__CLASS__, 'save'));
|
|
add_action('admin_post_interweb_mz_domains_test', array(__CLASS__, 'test_connection'));
|
|
add_action('admin_post_interweb_mz_domains_refresh', array(__CLASS__, 'refresh'));
|
|
add_action('admin_enqueue_scripts', array(__CLASS__, 'assets'));
|
|
add_filter('plugin_action_links_' . plugin_basename(INTERWEB_MZ_DOMAINS_FILE), array(__CLASS__, 'action_links'));
|
|
}
|
|
|
|
public static function menu()
|
|
{
|
|
$title = __('Interweb Mozambique Domains', 'interweb-mz-domains');
|
|
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-domains') . '</a>');
|
|
return $links;
|
|
}
|
|
|
|
public static function assets($hook)
|
|
{
|
|
if ($hook !== 'settings_page_' . self::PAGE) {
|
|
return;
|
|
}
|
|
wp_enqueue_style('interweb-mz-domains-admin', INTERWEB_MZ_DOMAINS_URL . 'assets/admin.css', array(), INTERWEB_MZ_DOMAINS_VERSION);
|
|
wp_enqueue_script('interweb-mz-domains-select', INTERWEB_MZ_DOMAINS_URL . 'assets/select.js', array(), INTERWEB_MZ_DOMAINS_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-domains'),
|
|
'search' => __('Search box', 'interweb-mz-domains'),
|
|
'prices' => __('Prices', 'interweb-mz-domains'),
|
|
'activity' => __('Activity', 'interweb-mz-domains'),
|
|
);
|
|
}
|
|
|
|
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-domains-wrap">';
|
|
self::header(__('Interweb Mozambique Domains', 'interweb-mz-domains'));
|
|
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>';
|
|
}
|
|
|
|
/* ---- Search box ---- */
|
|
|
|
public static function tab_search()
|
|
{
|
|
self::need_key_notice();
|
|
echo '<p>' . wp_kses(sprintf(
|
|
/* translators: 1: search shortcode, 2: price list shortcode */
|
|
__('Put the search on any page with the "Domain search" block or the %1$s shortcode. The price list uses the "Domain price list" block or %2$s.', 'interweb-mz-domains'),
|
|
'<code>[interweb_mz_domain_finder]</code>',
|
|
'<code>[interweb_mz_tld_prices]</code>'
|
|
), array('code' => array())) . '</p>';
|
|
self::form_open('search');
|
|
self::row(__('Extensions', 'interweb-mz-domains'), self::input('extensions', 'text', 'placeholder="' . esc_attr(INTERWEB_MZ_DOMAINS_Search::default_extensions()) . '"'), __('Up to 10, separated by commas. The first ones appear first.', 'interweb-mz-domains'), 'interweb-mz-domains-extensions');
|
|
self::row(__('Currency', 'interweb-mz-domains'), self::input('currency', 'text', 'maxlength="3" style="width:6em" placeholder="' . esc_attr__('account', 'interweb-mz-domains') . '"'), __('Empty uses the currency of your account. Otherwise a code such as USD.', 'interweb-mz-domains'), 'interweb-mz-domains-currency');
|
|
$targets = array(
|
|
'brand' => sprintf(
|
|
/* translators: %s: brand name */
|
|
__('The %s site', 'interweb-mz-domains'),
|
|
INTERWEB_MZ_DOMAINS_Brands::name()
|
|
),
|
|
'custom' => __('My own address', 'interweb-mz-domains'),
|
|
'woocommerce' => __('WooCommerce cart (Reseller plugin)', 'interweb-mz-domains'),
|
|
);
|
|
self::row(__('Register button opens', 'interweb-mz-domains'), self::select('buy_target', $targets, INTERWEB_MZ_DOMAINS_Settings::get('buy_target')), __('The WooCommerce cart needs the Reseller for WooCommerce plugin of the same brand, active and with a domain product chosen.', 'interweb-mz-domains'), 'interweb-mz-domains-buy_target');
|
|
self::row(__('My own address', 'interweb-mz-domains'), self::input('buy_url', 'url', 'placeholder="https://example.com/order?domain={domain}"'), __('Used with "My own address". {domain} becomes the name and {action} becomes register or transfer.', 'interweb-mz-domains'), 'interweb-mz-domains-buy_url');
|
|
self::row(__('WHOIS', 'interweb-mz-domains'), self::checkbox('show_whois', __('Show "See WHOIS" for names already registered', 'interweb-mz-domains')));
|
|
self::row(__('Transfer', 'interweb-mz-domains'), self::checkbox('show_transfer', __('Show a Transfer button for names already registered', 'interweb-mz-domains')));
|
|
self::row(__('Searches per visitor', 'interweb-mz-domains'), self::input('per_hour', 'number', 'min="1" max="500" style="width:6em"') . ' ' . esc_html__('per hour', 'interweb-mz-domains'), __('Protects your API limits from robots.', 'interweb-mz-domains'), 'interweb-mz-domains-per_hour');
|
|
self::row(__('Colour', 'interweb-mz-domains'), self::input('color', 'color', 'style="width:5em"'), '', 'interweb-mz-domains-color');
|
|
self::form_close();
|
|
}
|
|
|
|
/* ---- Prices ---- */
|
|
|
|
public static function tab_prices()
|
|
{
|
|
if (self::need_key_notice()) {
|
|
return;
|
|
}
|
|
$agency = self::agency();
|
|
if (is_array($agency) && !empty($agency['active'])) {
|
|
echo '<div class="interweb-mz-domains-card"><p><strong>' . esc_html(sprintf(
|
|
/* translators: 1: Agency name, 2: discount percentage */
|
|
__('Reseller Program: %1$s, %2$s%% discount.', 'interweb-mz-domains'),
|
|
$agency['name'],
|
|
number_format_i18n((float) $agency['discount_percent'], 2)
|
|
)) . '</strong> ' . esc_html__('With "Your price plus margin", the site shows your price with the margin you choose.', 'interweb-mz-domains') . '</p></div>';
|
|
} elseif (is_array($agency)) {
|
|
echo '<div class="interweb-mz-domains-card"><p>' . esc_html__('This account is not in the Reseller Program, so your price is the public price.', 'interweb-mz-domains') . '</p></div>';
|
|
}
|
|
self::form_open('prices');
|
|
$modes = array('public' => __('Public prices', 'interweb-mz-domains'), 'reseller' => __('Your price plus margin', 'interweb-mz-domains'));
|
|
self::row(__('Prices shown', 'interweb-mz-domains'), self::select('price_mode', $modes, INTERWEB_MZ_DOMAINS_Settings::get('price_mode')), '', 'interweb-mz-domains-price_mode');
|
|
self::row(__('Margin', 'interweb-mz-domains'), self::input('margin', 'number', 'min="0" max="500" step="0.01" style="width:7em"') . ' %', __('Added to your price. 0 shows your price as it is.', 'interweb-mz-domains'), 'interweb-mz-domains-margin');
|
|
self::row(__('Rounding', 'interweb-mz-domains'), self::checkbox('round_up', __('Round up to a whole number', 'interweb-mz-domains')));
|
|
self::form_close();
|
|
|
|
$rows = array();
|
|
foreach (INTERWEB_MZ_DOMAINS_Search::extensions() as $e) {
|
|
$r = INTERWEB_MZ_DOMAINS_Pricing::extension($e);
|
|
if ($r) {
|
|
$rows[$e] = $r;
|
|
}
|
|
}
|
|
if (!$rows) {
|
|
echo '<p>' . esc_html__('No prices to show yet. Check the extensions and the connection.', 'interweb-mz-domains') . '</p>';
|
|
return;
|
|
}
|
|
echo '<h2>' . esc_html__('Register price for 1 year', 'interweb-mz-domains') . '</h2>';
|
|
echo '<table class="widefat striped" style="max-width:760px"><thead><tr><th>' . esc_html__('Extension', 'interweb-mz-domains') . '</th><th>' . esc_html__('Public price', 'interweb-mz-domains') . '</th><th>' . esc_html__('Your price', 'interweb-mz-domains') . '</th><th>' . esc_html__('Shown on the site', 'interweb-mz-domains') . '</th></tr></thead><tbody>';
|
|
foreach ($rows as $e => $r) {
|
|
$public = $r['list']['register'] !== null ? $r['list']['register'] : $r['register'];
|
|
$shown = INTERWEB_MZ_DOMAINS_Pricing::reseller_mode() ? INTERWEB_MZ_DOMAINS_Pricing::sell_price($r['register']) : $public;
|
|
echo '<tr><td><strong>' . esc_html($e) . '</strong></td><td>' . esc_html($public === null ? '-' : self::money($public, $r['currency'])) . '</td><td>' . esc_html($r['register'] === null ? '-' : self::money($r['register'], $r['currency'])) . '</td><td>' . esc_html($shown === null ? '-' : self::money($shown, $r['currency'])) . '</td></tr>';
|
|
}
|
|
echo '</tbody></table>';
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin-top:10px">';
|
|
echo '<input type="hidden" name="action" value="interweb_mz_domains_refresh">';
|
|
wp_nonce_field('interweb_mz_domains_refresh');
|
|
submit_button(__('Read the prices again', 'interweb-mz-domains'), 'secondary', 'submit', false);
|
|
echo ' <span class="description">' . esc_html__('Prices are kept for 6 hours.', 'interweb-mz-domains') . '</span></form>';
|
|
}
|
|
|
|
/** Reseller Program of the account (GET /v1/reseller), kept for an hour. @return array|null */
|
|
private static function agency()
|
|
{
|
|
$hit = get_transient('interweb_mz_domains_agency');
|
|
if (is_array($hit)) {
|
|
return $hit;
|
|
}
|
|
$res = INTERWEB_MZ_DOMAINS_Api::request('GET', '/v1/reseller', null, array('timeout' => 10));
|
|
if (is_wp_error($res)) {
|
|
return null;
|
|
}
|
|
$rp = isset($res['data']['programs']['reseller_program']) && is_array($res['data']['programs']['reseller_program']) ? $res['data']['programs']['reseller_program'] : array('active' => false);
|
|
$out = array(
|
|
'active' => !empty($rp['active']),
|
|
'name' => isset($rp['name']) ? (string) $rp['name'] : '',
|
|
'discount_percent' => isset($rp['discount_percent']) ? (float) $rp['discount_percent'] : 0,
|
|
);
|
|
set_transient('interweb_mz_domains_agency', $out, HOUR_IN_SECONDS);
|
|
return $out;
|
|
}
|
|
|
|
/* ---- Activity ---- */
|
|
|
|
public static function tab_activity()
|
|
{
|
|
self::log_table(null, 50, array('search' => __('Search', 'interweb-mz-domains'), 'prices' => __('Prices', 'interweb-mz-domains')));
|
|
}
|
|
|
|
/* ---- Saving ---- */
|
|
|
|
public static function after_key_change()
|
|
{
|
|
delete_transient('interweb_mz_domains_agency');
|
|
INTERWEB_MZ_DOMAINS_Pricing::forget();
|
|
}
|
|
|
|
public static function save()
|
|
{
|
|
$tab = isset($_POST['tab']) ? sanitize_key(wp_unslash($_POST['tab'])) : '';
|
|
self::check('interweb_mz_domains_save_' . $tab);
|
|
$p = wp_unslash($_POST);
|
|
$v = array();
|
|
$ok = true;
|
|
$msg = __('Settings saved.', 'interweb-mz-domains');
|
|
$more = array();
|
|
switch ($tab) {
|
|
case 'connection':
|
|
list($ok, $msg, $more) = self::save_connection($p);
|
|
INTERWEB_MZ_DOMAINS_Pricing::forget();
|
|
break;
|
|
case 'search':
|
|
$v['extensions'] = implode(', ', INTERWEB_MZ_DOMAINS_Search::extensions(isset($p['extensions']) && trim($p['extensions']) !== '' ? $p['extensions'] : INTERWEB_MZ_DOMAINS_Search::default_extensions()));
|
|
if (trim(isset($p['extensions']) ? $p['extensions'] : '') === '') {
|
|
$v['extensions'] = '';
|
|
}
|
|
$cur = strtoupper(preg_replace('/[^A-Za-z]/', '', isset($p['currency']) ? $p['currency'] : ''));
|
|
$v['currency'] = strlen($cur) === 3 ? $cur : '';
|
|
$v['buy_target'] = isset($p['buy_target']) && in_array($p['buy_target'], array('brand', 'custom', 'woocommerce'), true) ? $p['buy_target'] : 'brand';
|
|
$v['buy_url'] = esc_url_raw(isset($p['buy_url']) ? $p['buy_url'] : '');
|
|
$v['show_whois'] = empty($p['show_whois']) ? 0 : 1;
|
|
$v['show_transfer'] = empty($p['show_transfer']) ? 0 : 1;
|
|
$v['per_hour'] = min(500, max(1, (int) (isset($p['per_hour']) ? $p['per_hour'] : 30)));
|
|
$color = sanitize_hex_color(isset($p['color']) ? $p['color'] : '');
|
|
$v['color'] = $color ? $color : '#d60037';
|
|
if ($v['buy_target'] === 'custom' && $v['buy_url'] === '') {
|
|
$ok = 'warning';
|
|
$msg = __('Saved, but the button keeps opening the brand site until you write your own address.', 'interweb-mz-domains');
|
|
}
|
|
if ($v['buy_target'] === 'woocommerce' && !has_filter('interweb_mz_domain_buy_url')) {
|
|
$ok = 'warning';
|
|
$msg = __('Saved, but the Reseller for WooCommerce plugin is not active, so the button keeps opening the brand site.', 'interweb-mz-domains');
|
|
}
|
|
INTERWEB_MZ_DOMAINS_Settings::update($v);
|
|
INTERWEB_MZ_DOMAINS_Pricing::forget();
|
|
$more = self::key_warnings();
|
|
break;
|
|
case 'prices':
|
|
$v['price_mode'] = isset($p['price_mode']) && $p['price_mode'] === 'reseller' ? 'reseller' : 'public';
|
|
$v['margin'] = round(min(500, max(0, (float) str_replace(',', '.', isset($p['margin']) ? $p['margin'] : 0))), 2);
|
|
$v['round_up'] = empty($p['round_up']) ? 0 : 1;
|
|
INTERWEB_MZ_DOMAINS_Settings::update($v);
|
|
INTERWEB_MZ_DOMAINS_Pricing::forget();
|
|
$more = self::key_warnings();
|
|
break;
|
|
default:
|
|
wp_die('bad tab', 400);
|
|
}
|
|
self::redirect($tab, $ok, $msg, $more);
|
|
}
|
|
|
|
public static function refresh()
|
|
{
|
|
self::check('interweb_mz_domains_refresh');
|
|
delete_transient('interweb_mz_domains_agency');
|
|
INTERWEB_MZ_DOMAINS_Pricing::forget();
|
|
self::redirect('prices', true, __('Prices read again.', 'interweb-mz-domains'));
|
|
}
|
|
}
|