woocommerce-reseller-plugin/interweb-ao-reseller/includes/class-admin.php
2026-12-01 14:09:12 +02:00

325 lines
17 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* Settings page: Settings > Interweb Angola Reseller, with tabs.
*/
class INTERWEB_AO_RESELLER_Admin extends INTERWEB_AO_RESELLER_Admin_Base
{
const PAGE = 'interweb-ao-reseller';
public static function defaults()
{
return array(
'auto_run' => 1,
'complete_order' => 1,
'renewals' => 1,
'panel_link' => 1,
'endpoint' => 'services',
'register_product' => 0,
'transfer_product' => 0,
'email_ready' => 1,
'email_reminder' => 1,
'email_status' => 1,
'email_admin' => 1,
'admin_email' => '',
'remind_days' => '30, 7, 1',
);
}
public static function needed_scopes()
{
return array(
'catalog:read' => __('plans and prices', 'interweb-ao-reseller'),
'billing:read' => __('credit and renewal invoices', 'interweb-ao-reseller'),
'billing:purchase' => __('buy domains, plans and SMS credits', 'interweb-ao-reseller'),
'billing:pay' => __('pay renewal invoices with credit', 'interweb-ao-reseller'),
'domains:read' => __('check names and follow domains', 'interweb-ao-reseller'),
'domains:write' => __('register, transfer and renew domains', 'interweb-ao-reseller'),
'hosting:read' => __('follow hosting', 'interweb-ao-reseller'),
'hosting:write' => __('buy hosting', 'interweb-ao-reseller'),
'hosting:login' => __('control panel link for customers (optional)', 'interweb-ao-reseller'),
'vps:write' => __('buy servers', 'interweb-ao-reseller'),
'sms:read' => __('SMS credit prices', 'interweb-ao-reseller'),
'sms:write' => __('buy SMS credits', 'interweb-ao-reseller'),
);
}
/** Products that the API fulfils, by type. */
public static function products_in_use()
{
$ids = get_posts(array(
'post_type' => 'product',
'post_status' => array('publish', 'private', 'draft'),
'posts_per_page' => 200,
'fields' => 'ids',
'meta_query' => array(array('key' => '_interweb_ao_reseller_type', 'value' => INTERWEB_AO_RESELLER_Product::TYPES, 'compare' => 'IN')), // phpcs:ignore WordPress.DB.SlowDBQuery
));
return array_map('intval', $ids);
}
public static function scopes_in_use()
{
$all = self::needed_scopes();
$need = array('catalog:read', 'billing:read', 'billing:purchase');
$plans = null;
foreach (self::products_in_use() as $id) {
$type = (string) get_post_meta($id, '_interweb_ao_reseller_type', true);
if (in_array($type, array('domain_register', 'domain_transfer'), true)) {
$need = array_merge($need, array('domains:read', 'domains:write'));
} elseif ($type === 'sms') {
$need = array_merge($need, array('sms:read', 'sms:write'));
} elseif ($type === 'plan') {
$need = array_merge($need, array('hosting:read', 'billing:pay'));
if ($plans === null) {
$plans = INTERWEB_AO_RESELLER_Settings::api_key() !== '' ? INTERWEB_AO_RESELLER_Catalog::plans() : array();
}
$pid = (int) get_post_meta($id, '_interweb_ao_reseller_plan', true);
$kinds = INTERWEB_AO_RESELLER_Catalog::kinds();
if (is_array($plans) && isset($plans[$pid])) {
$need[] = $kinds[$plans[$pid]['kind']][1];
}
}
}
$use = array();
foreach (array_unique($need) as $s) {
$use[$s] = $all[$s];
}
return $use;
}
public static function mode_warnings(array $info)
{
if ($info['mode'] === 'sandbox') {
return array(array('warning', __('This is a Sandbox key: paid orders are simulated and nothing is registered, created or charged. Use a Production key to sell for real.', 'interweb-ao-reseller')));
}
if (!empty($info['simulated'])) {
return array(array('warning', __('This Production key still simulates purchases, so paid orders are not fulfilled for real yet.', 'interweb-ao-reseller')));
}
return array();
}
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_reseller_save', array(__CLASS__, 'save'));
add_action('admin_post_interweb_ao_reseller_test', array(__CLASS__, 'test_connection'));
add_action('admin_post_interweb_ao_reseller_refresh', array(__CLASS__, 'refresh'));
add_action('admin_enqueue_scripts', array(__CLASS__, 'assets'));
add_filter('plugin_action_links_' . plugin_basename(INTERWEB_AO_RESELLER_FILE), array(__CLASS__, 'action_links'));
}
public static function menu()
{
$title = __('Interweb Angola Reseller', 'interweb-ao-reseller');
add_options_page($title, self::menu_label($title), 'manage_woocommerce', self::PAGE, array(__CLASS__, 'page'));
}
public static function action_links($links)
{
array_unshift($links, '<a href="' . esc_url(self::url()) . '">' . esc_html__('Settings', 'interweb-ao-reseller') . '</a>');
return $links;
}
public static function assets($hook)
{
if ($hook !== 'settings_page_' . self::PAGE) {
return;
}
wp_enqueue_style('interweb-ao-reseller-admin', INTERWEB_AO_RESELLER_URL . 'assets/admin.css', array(), INTERWEB_AO_RESELLER_VERSION);
wp_enqueue_script('interweb-ao-reseller-select', INTERWEB_AO_RESELLER_URL . 'assets/select.js', array(), INTERWEB_AO_RESELLER_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-ao-reseller'),
'fulfilment' => __('Fulfilment', 'interweb-ao-reseller'),
'emails' => __('Emails', 'interweb-ao-reseller'),
'products' => __('Products', 'interweb-ao-reseller'),
'activity' => __('Activity', 'interweb-ao-reseller'),
);
}
public static function page()
{
if (!current_user_can('manage_woocommerce')) {
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-ao-reseller-wrap">';
self::header(__('Interweb Angola Reseller for WooCommerce', 'interweb-ao-reseller'));
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>';
}
public static function tab_connection()
{
$credit = INTERWEB_AO_RESELLER_Settings::api_key() !== '' ? INTERWEB_AO_RESELLER_Api::request('GET', '/v1/billing/credit', null, array('timeout' => 10)) : null;
if (is_array($credit)) {
echo '<div class="interweb-ao-reseller-card"><p><strong>' . esc_html(sprintf(
/* translators: %s: amount */
__('Credit in your account: %s', 'interweb-ao-reseller'),
self::money($credit['data']['balance'], $credit['data']['currency'])
)) . '</strong> ' . esc_html__('Purchases are paid with this credit. Keep enough for the orders you expect.', 'interweb-ao-reseller') . '</p></div>';
}
parent::tab_connection();
}
/* ---- Fulfilment ---- */
private static function product_options($type)
{
$opts = array('0' => __('None', 'interweb-ao-reseller'));
foreach (self::products_in_use() as $id) {
if ((string) get_post_meta($id, '_interweb_ao_reseller_type', true) === $type) {
$opts[(string) $id] = get_the_title($id);
}
}
return $opts;
}
public static function tab_fulfilment()
{
self::need_key_notice();
echo '<p>' . esc_html__('In each WooCommerce product, the Fulfilment tab says what the API does when the order is paid: register or transfer a domain, create a hosting or server plan, or buy SMS credits.', 'interweb-ao-reseller') . '</p>';
self::form_open('fulfilment');
self::row(__('Paid orders', 'interweb-ao-reseller'), self::checkbox('auto_run', __('Fulfil by themselves when the order is paid (Processing)', 'interweb-ao-reseller')), __('Off: you start each order with "Run now" in the order screen.', 'interweb-ao-reseller'));
self::row(__('Complete the order', 'interweb-ao-reseller'), self::checkbox('complete_order', __('Mark the order as Completed when everything is done', 'interweb-ao-reseller')));
self::row(__('Renewals', 'interweb-ao-reseller'), self::checkbox('renewals', __('Show a Renew button in My services', 'interweb-ao-reseller')));
self::row(__('Control panel', 'interweb-ao-reseller'), self::checkbox('panel_link', __('Let customers open the hosting control panel from My services', 'interweb-ao-reseller')), __('Needs the hosting:login permission. The link works once.', 'interweb-ao-reseller'));
self::row(__('Address of My services', 'interweb-ao-reseller'), self::input('endpoint', 'text', 'style="width:12em"'), __('Part of the address in the customer account, for example services.', 'interweb-ao-reseller'), 'interweb-ao-reseller-endpoint');
self::row(__('Domain search: register', 'interweb-ao-reseller'), self::select('register_product', self::product_options('domain_register'), (string) INTERWEB_AO_RESELLER_Settings::get('register_product')), __('With the Domains plugin of the same brand, its Register button puts the name in the cart with this product.', 'interweb-ao-reseller'), 'interweb-ao-reseller-register_product');
self::row(__('Domain search: transfer', 'interweb-ao-reseller'), self::select('transfer_product', self::product_options('domain_transfer'), (string) INTERWEB_AO_RESELLER_Settings::get('transfer_product')), __('Its Transfer button opens this product, where the customer writes the transfer code.', 'interweb-ao-reseller'), 'interweb-ao-reseller-transfer_product');
self::form_close();
}
public static function tab_emails()
{
echo '<p>' . esc_html__('Emails go to your customers with your site name and the WooCommerce email design, in Portuguese or English.', 'interweb-ao-reseller') . '</p>';
self::form_open('emails');
self::row(__('Service ready', 'interweb-ao-reseller'), self::checkbox('email_ready', __('When a domain, plan or renewal is done', 'interweb-ao-reseller')));
self::row(__('Renewal reminder', 'interweb-ao-reseller'), self::checkbox('email_reminder', __('Before the expiry or due date, with a Renew button', 'interweb-ao-reseller')) . '<br>' . self::input('remind_days', 'text', 'style="width:10em"') . ' ' . esc_html__('days before', 'interweb-ao-reseller'), '', 'interweb-ao-reseller-remind_days');
self::row(__('State changes', 'interweb-ao-reseller'), self::checkbox('email_status', __('When a service is suspended, expires or comes back', 'interweb-ao-reseller')));
self::row(__('Alerts to you', 'interweb-ao-reseller'), self::checkbox('email_admin', __('When an order waits for credit or cannot be fulfilled', 'interweb-ao-reseller')) . '<br>' . self::input('admin_email', 'email', 'placeholder="' . esc_attr(get_option('admin_email')) . '"'), '', 'interweb-ao-reseller-admin_email');
self::form_close();
}
/* ---- Products ---- */
public static function tab_products()
{
$ids = self::products_in_use();
if (!$ids) {
echo '<p>' . esc_html__('No product is fulfilled by the API yet. Open a WooCommerce product and choose what the API does in its Fulfilment tab.', 'interweb-ao-reseller') . '</p>';
echo '<p><a class="button button-primary" href="' . esc_url(admin_url('post-new.php?post_type=product')) . '">' . esc_html__('Add a product', 'interweb-ao-reseller') . '</a></p>';
return;
}
$types = INTERWEB_AO_RESELLER_Product::type_names();
echo '<table class="widefat striped" style="max-width:980px"><thead><tr><th>' . esc_html__('Product', 'interweb-ao-reseller') . '</th><th>' . esc_html__('The API fulfils it as', 'interweb-ao-reseller') . '</th><th>' . esc_html__('Price', 'interweb-ao-reseller') . '</th><th>' . esc_html__('Your API price', 'interweb-ao-reseller') . '</th></tr></thead><tbody>';
foreach ($ids as $id) {
$p = wc_get_product($id);
if (!$p) {
continue;
}
$cost = INTERWEB_AO_RESELLER_Product::cost_line($p);
echo '<tr><td><a href="' . esc_url(get_edit_post_link($id)) . '"><strong>' . esc_html($p->get_name()) . '</strong></a></td><td>' . esc_html($types[INTERWEB_AO_RESELLER_Product::type($p)]) . '</td><td>' . wp_kses_post($p->get_price_html()) . '</td><td>' . wp_kses($cost !== '' ? $cost : '-', array('strong' => array(), 'span' => array('class' => array()))) . '</td></tr>';
}
echo '</tbody></table>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin-top:10px"><input type="hidden" name="action" value="interweb_ao_reseller_refresh">';
wp_nonce_field('interweb_ao_reseller_refresh');
submit_button(__('Read the prices again', 'interweb-ao-reseller'), 'secondary', 'submit', false);
echo '</form>';
}
public static function tab_activity()
{
self::log_table(null, 50, array('order' => __('Orders', 'interweb-ao-reseller'), 'cart' => __('Cart', 'interweb-ao-reseller'), 'account' => __('My services', 'interweb-ao-reseller')));
}
/* ---- Saving ---- */
public static function after_key_change()
{
INTERWEB_AO_RESELLER_Catalog::forget();
}
public static function save()
{
$tab = isset($_POST['tab']) ? sanitize_key(wp_unslash($_POST['tab'])) : '';
self::check('interweb_ao_reseller_save_' . $tab, 'manage_woocommerce');
$p = wp_unslash($_POST);
$ok = true;
$msg = __('Settings saved.', 'interweb-ao-reseller');
$more = array();
switch ($tab) {
case 'connection':
list($ok, $msg, $more) = self::save_connection($p);
INTERWEB_AO_RESELLER_Catalog::forget();
break;
case 'fulfilment':
$ep = sanitize_title(isset($p['endpoint']) ? $p['endpoint'] : '');
INTERWEB_AO_RESELLER_Settings::update(array(
'auto_run' => empty($p['auto_run']) ? 0 : 1,
'complete_order' => empty($p['complete_order']) ? 0 : 1,
'renewals' => empty($p['renewals']) ? 0 : 1,
'panel_link' => empty($p['panel_link']) ? 0 : 1,
'endpoint' => $ep !== '' ? $ep : 'services',
'register_product' => (int) (isset($p['register_product']) ? $p['register_product'] : 0),
'transfer_product' => (int) (isset($p['transfer_product']) ? $p['transfer_product'] : 0),
));
$more = self::key_warnings();
break;
case 'emails':
$days = array();
foreach (preg_split('/[\s,;]+/', isset($p['remind_days']) ? (string) $p['remind_days'] : '') as $d) {
$d = (int) $d;
if ($d >= 0 && $d <= 120) {
$days[] = $d;
}
}
$days = array_unique($days);
rsort($days);
INTERWEB_AO_RESELLER_Settings::update(array(
'email_ready' => empty($p['email_ready']) ? 0 : 1,
'email_reminder' => empty($p['email_reminder']) ? 0 : 1,
'email_status' => empty($p['email_status']) ? 0 : 1,
'email_admin' => empty($p['email_admin']) ? 0 : 1,
'admin_email' => sanitize_email(isset($p['admin_email']) ? $p['admin_email'] : ''),
'remind_days' => $days ? implode(', ', array_slice($days, 0, 5)) : '30, 7, 1',
));
break;
default:
wp_die('bad tab', 400);
}
self::redirect($tab, $ok, $msg, $more);
}
public static function refresh()
{
self::check('interweb_ao_reseller_refresh', 'manage_woocommerce');
INTERWEB_AO_RESELLER_Catalog::forget();
self::redirect('products', true, __('Prices read again.', 'interweb-ao-reseller'));
}
}