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

143 lines
4.2 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* The brand of this plugin, fixed by brand.php.
*/
class INTERWEB_MZ_RESELLER_Brands
{
private static $fixed = null;
/** Id of the brand fixed by brand.php, or '' when the brand is chosen in the settings. */
public static function fixed_id()
{
if (self::$fixed === null) {
self::$fixed = '';
$file = INTERWEB_MZ_RESELLER_DIR . 'brand.php';
if (is_readable($file)) {
$id = include $file;
if (is_string($id)) {
self::$fixed = sanitize_key($id);
}
}
}
return self::$fixed;
}
public static function is_fixed()
{
return self::fixed_id() !== '';
}
public static function all()
{
$fixed = self::fixed_id();
$brands = array(
'interweb-mz' => array(
'name' => array('pt' => 'Interweb Moçambique', 'en' => 'Interweb Mozambique'),
'api' => 'https://api.interweb.co.mz',
'portal' => 'https://www.meu.interweb.co.mz',
'portal_name' => array('pt' => 'Meu Interweb', 'en' => 'Meu Interweb'),
'docs' => 'https://docs.interweb.co.mz',
'status' => 'https://www.status.interweb.co.mz',
'qs' => array('pt' => 'language=portuguese&currency=1', 'en' => 'language=english&currency=2'),
),
);
$brands = apply_filters('interweb_mz_reseller_brands', $brands);
// Only HTTPS API addresses are accepted: the key travels in every request.
foreach ($brands as $id => $b) {
if (empty($b['api']) || strpos($b['api'], 'https://') !== 0) {
unset($brands[$id]);
}
}
if ($fixed !== '') {
return isset($brands[$fixed]) ? array($fixed => $brands[$fixed]) : array();
}
return $brands;
}
public static function current_id()
{
$id = self::is_fixed() ? self::fixed_id() : INTERWEB_MZ_RESELLER_Settings::get('brand');
$all = self::all();
if (!isset($all[$id])) {
$keys = array_keys($all);
$id = $keys ? $keys[0] : '';
}
return $id;
}
public static function current()
{
$all = self::all();
$id = self::current_id();
return isset($all[$id]) ? $all[$id] : null;
}
public static function label($brand, $field = 'name')
{
$lang = INTERWEB_MZ_RESELLER_I18n::lang();
if (!$brand || !isset($brand[$field])) {
return '';
}
$v = $brand[$field];
if (is_array($v)) {
return isset($v[$lang]) ? $v[$lang] : reset($v);
}
return $v;
}
public static function name()
{
return self::label(self::current(), 'name');
}
public static function portal_name()
{
return self::label(self::current(), 'portal_name');
}
public static function api_base()
{
$b = self::current();
return $b ? untrailingslashit($b['api']) : '';
}
/** Link to a page of the brand's customer area, in the interface language. */
public static function portal_link($page)
{
$b = self::current();
if (!$b) {
return '';
}
$lang = INTERWEB_MZ_RESELLER_I18n::lang();
$qs = isset($b['qs'][$lang]) ? $b['qs'][$lang] : '';
return untrailingslashit($b['portal']) . '/' . ltrim($page, '/') . ($qs ? '?' . $qs : '');
}
/** Public status page of the brand. */
public static function status_link()
{
$b = self::current();
return ($b && !empty($b['status'])) ? untrailingslashit($b['status']) : '';
}
public static function docs_link($path = '')
{
$b = self::current();
if (!$b || empty($b['docs'])) {
return '';
}
$base = untrailingslashit($b['docs']);
$root = isset($b['docs_root']) ? $b['docs_root'] : 'pt';
$lang = INTERWEB_MZ_RESELLER_I18n::lang();
if ($lang !== $root) {
$base .= '/' . $lang;
}
return $base . '/' . ltrim($path, '/');
}
}