wordpress-domains-plugin/interweb-ao-domains/includes/class-pricing.php
2026-12-01 14:09:12 +02:00

130 lines
5.5 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* Prices shown on the site.
* - Public prices: the ones the WHOIS lookup brings (GET /v1/whois).
* - Reseller prices: the extension prices of the account (GET /v1/catalog/tlds). With a Reseller Program key they come
* with the Agency discount, and the plugin adds your margin. Kept for 6 hours.
* Every price is shown with 2 decimal places.
*/
class INTERWEB_AO_DOMAINS_Pricing
{
public static function reseller_mode()
{
return INTERWEB_AO_DOMAINS_Settings::get('price_mode') === 'reseller' && INTERWEB_AO_DOMAINS_Settings::api_key() !== '';
}
/** Applies the margin and the rounding of the settings. */
public static function sell_price($cost)
{
if ($cost === null || $cost === '') {
return null;
}
$m = max(0, min(500, (float) INTERWEB_AO_DOMAINS_Settings::get('margin')));
$v = (float) $cost * (1 + $m / 100);
if ((int) INTERWEB_AO_DOMAINS_Settings::get('round_up') === 1) {
$v = ceil($v);
}
return round($v, 2);
}
/**
* Prices of one extension from the account catalog: array(currency, register, renew, transfer, discount, list) or null.
* register/renew/transfer are the cost for 1 year (with the discount of the account, when it has one).
*/
public static function extension($ext)
{
$ext = '.' . ltrim(strtolower((string) $ext), '.');
$cur = (string) INTERWEB_AO_DOMAINS_Settings::get('currency');
$key = 'interweb_ao_domains_tld_' . md5(INTERWEB_AO_DOMAINS_Brands::current_id() . '|' . $cur . '|' . $ext);
$hit = get_transient($key);
if (is_array($hit)) {
return $hit ? $hit : null;
}
$query = array('extension' => $ext);
if ($cur !== '') {
$query['currency'] = $cur;
}
$res = INTERWEB_AO_DOMAINS_Api::request('GET', '/v1/catalog/tlds', null, array('query' => $query, 'timeout' => 15));
if (is_wp_error($res)) {
$st = (int) INTERWEB_AO_DOMAINS_Admin::error_status($res);
if ($st === 404) {
set_transient($key, array(), 6 * HOUR_IN_SECONDS);
} else {
INTERWEB_AO_DOMAINS_Log::add('prices', false, $res->get_error_message(), array('extension' => $ext));
}
return null;
}
$row = null;
foreach ((array) $res['data'] as $t) {
if (isset($t['extension']) && strtolower($t['extension']) === $ext) {
$one = function ($kind, $src) {
return (isset($src[$kind]) && is_array($src[$kind]) && isset($src[$kind]['1'])) ? (float) $src[$kind]['1'] : null;
};
$list = isset($t['list_prices']) && is_array($t['list_prices']) ? $t['list_prices'] : array();
$row = array(
'currency' => isset($t['currency']) ? (string) $t['currency'] : '',
'register' => $one('register', $t),
'renew' => $one('renew', $t),
'transfer' => $one('transfer', $t),
'discount' => isset($t['discount']['percent']) ? (float) $t['discount']['percent'] : 0,
'list' => array('register' => $one('register', $list), 'renew' => $one('renew', $list), 'transfer' => $one('transfer', $list)),
);
break;
}
}
set_transient($key, $row ? $row : array(), 6 * HOUR_IN_SECONDS);
return $row;
}
/** Selling prices of one extension, formatted: array(register, renew, transfer) or null. */
public static function selling($ext, $lang = null)
{
$row = self::extension($ext);
if (!$row) {
return null;
}
$out = array();
foreach (array('register', 'renew', 'transfer') as $k) {
$p = self::sell_price($row[$k]);
$out[$k] = $p === null ? '' : INTERWEB_AO_DOMAINS_Admin::money($p, $row['currency'], $lang);
$out[$k . '_value'] = $p;
}
$out['currency'] = $row['currency'];
return $out;
}
/** Table rows for the price list: one per extension. */
public static function table(array $exts, $lang = null)
{
$rows = array();
foreach ($exts as $e) {
$row = self::extension($e);
if (!$row) {
continue;
}
$use_list = !self::reseller_mode();
$cell = function ($kind) use ($row, $use_list, $lang) {
$v = $use_list ? ($row['list'][$kind] !== null ? $row['list'][$kind] : $row[$kind]) : self::sell_price($row[$kind]);
return $v === null ? '-' : INTERWEB_AO_DOMAINS_Admin::money($v, $row['currency'], $lang);
};
$rows[] = array('extension' => $e, 'register' => $cell('register'), 'renew' => $cell('renew'), 'transfer' => $cell('transfer'));
}
return $rows;
}
public static function forget()
{
global $wpdb;
$wpdb->query($wpdb->prepare(
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s",
$wpdb->esc_like('_transient_interweb_ao_domains_tld_') . '%',
$wpdb->esc_like('_transient_timeout_interweb_ao_domains_tld_') . '%',
$wpdb->esc_like('_transient_interweb_ao_domains_q_') . '%',
$wpdb->esc_like('_transient_timeout_interweb_ao_domains_q_') . '%'
));
}
}