$v) { if (in_array($k, $shared, true)) { continue; } if (is_scalar($v) && trim((string) $v) !== '') { return true; } if (is_array($v) && !empty($v)) { return true; } } return false; } public function in_use(array $cfg) { if (0 === strpos((string) $this->code(), 'custom__')) { return true; } if (self::has_own_settings($cfg)) { return true; } if (class_exists('AIEO_Fulfillment_Settings')) { $default = (string) AIEO_Fulfillment_Settings::get('default_carrier', ''); if ($default !== '' && $default === (string) $this->code()) { return true; } } return false; } public function default_returns_mode() { static $known = array( 'boxnow' => self::RETURNS_NONE, 'elta' => self::RETURNS_API, 'geniki' => self::RETURNS_MANUAL, 'acs' => self::RETURNS_API, 'speedex' => self::RETURNS_API, 'courrier_centre' => self::RETURNS_API, 'brt' => self::RETURNS_API, 'colissimo' => self::RETURNS_API, 'correos_express' => self::RETURNS_API, 'dhl' => self::RETURNS_API, 'dhl_paket' => self::RETURNS_API, 'fedex' => self::RETURNS_API, 'royalmail' => self::RETURNS_API, 'sendcloud' => self::RETURNS_API, 'ups' => self::RETURNS_API, 'usps' => self::RETURNS_API, ); $code = (string) $this->code(); return isset($known[$code]) ? $known[$code] : self::RETURNS_NONE; } public function returns_mode(array $cfg) { $mode = isset($cfg['returns_mode']) ? sanitize_key((string) $cfg['returns_mode']) : ''; if ($mode === '' || !in_array($mode, self::returns_modes(), true)) { $mode = !empty($cfg['supports_returns']) ? ($this->has_capability('returns') ? self::RETURNS_API : self::RETURNS_MANUAL) : $this->default_returns_mode(); } return $mode; } public static function charge_parties() { return array(self::CHARGE_SENDER, self::CHARGE_RECIPIENT, self::CHARGE_THIRD_PARTY); } abstract public function code(); abstract public function label(); abstract public function capabilities(); public function settings_schema() { return array(); } public function settings_schema_full() { $schema = (array) $this->settings_schema(); $schema['returns_mode'] = array( 'type' => 'select', 'label' => __('Collects returns for us', 'ai-eshop-optimizer'), 'options' => array( self::RETURNS_NONE => __('No, they only deliver', 'ai-eshop-optimizer'), self::RETURNS_MANUAL => __('Yes, booked with them directly (no API)', 'ai-eshop-optimizer'), self::RETURNS_API => __('Yes, and we book it through their API', 'ai-eshop-optimizer'), ), 'no_empty' => true, 'help' => __('Only couriers that collect are offered when arranging a withdrawal return. Choose the API option to issue the voucher and label from here with one press; choose the direct option if you book the collection on their portal and bring the number back.', 'ai-eshop-optimizer'), ); if ($this->has_capability('returns') && $this->has_capability('pickup_date')) { $schema['pickup_weekend'] = array( 'type' => 'select', 'label' => __('Weekend return collections', 'ai-eshop-optimizer'), 'options' => array( self::PICKUP_WEEKEND_SKIP => __('Move to the next working day', 'ai-eshop-optimizer'), self::PICKUP_WEEKEND_AS_ENTERED => __('Send the date exactly as entered', 'ai-eshop-optimizer'), ), 'no_empty' => true, 'help' => __('What to do when a customer return collection falls on a Saturday or Sunday. Most couriers do not collect at weekends and some refuse the booking outright — ELTA answers "Invalid Pick Date. Saturday and Sunday is not allowed" — so the date is moved forward by default. Choose the second option only if this courier really does collect for you at weekends. Outbound vouchers are not affected: their date is the handover date and is always sent exactly as entered.', 'ai-eshop-optimizer'), ); } if ($this->has_capability('charge_party')) { $schema['default_charge_party'] = array( 'type' => 'select', 'label' => __('Who the carrier bills', 'ai-eshop-optimizer'), 'options' => array( self::CHARGE_SENDER => __('Sender — us, on dispatch', 'ai-eshop-optimizer'), self::CHARGE_RECIPIENT => __('Recipient — whoever receives it', 'ai-eshop-optimizer'), ), 'help' => __('Fallback only. Each shipment states its own charge party: a dispatch bills the sender (us) and a withdrawal return bills the recipient (also us, because the parties are swapped). This is what gets used when a caller states nothing.', 'ai-eshop-optimizer'), ); } return $schema; } protected function pickup_datetime(array $payload, $is_return = false) { $tz = wp_timezone(); $raw = ''; foreach (array('received_date', 'pickup_date') as $k) { if (isset($payload[$k]) && trim((string) $payload[$k]) !== '') { $raw = trim((string) $payload[$k]); break; } } try { $day = ($raw !== '') ? new DateTimeImmutable($raw, $tz) : new DateTimeImmutable('now', $tz); } catch (Exception $e) { $day = new DateTimeImmutable('now', $tz); } $day = $day->setTimezone($tz); if ($is_return && $this->pickup_weekend_mode() === self::PICKUP_WEEKEND_SKIP) { $day = self::next_working_day($day); } return $day; } protected function pickup_date(array $payload, $format = 'Y-m-d', $is_return = false) { return $this->pickup_datetime($payload, $is_return)->format((string) $format); } protected function pickup_weekend_mode() { $mode = strtolower(trim((string) $this->get_setting('pickup_weekend', ''))); return ($mode === self::PICKUP_WEEKEND_AS_ENTERED) ? self::PICKUP_WEEKEND_AS_ENTERED : self::PICKUP_WEEKEND_SKIP; } public static function next_working_day(DateTimeImmutable $day) { while ((int) $day->format('N') >= 6) { $day = $day->modify('+1 day'); } return $day; } protected function charge_party(array $payload) { $valid = self::charge_parties(); $stated = isset($payload['charge_party']) ? strtolower(trim((string) $payload['charge_party'])) : ''; if (in_array($stated, $valid, true)) { return $stated; } $configured = strtolower(trim((string) $this->get_setting('default_charge_party', ''))); if (in_array($configured, $valid, true)) { return $configured; } $legacy = strtolower(trim((string) $this->legacy_charge_party())); if (in_array($legacy, $valid, true)) { return $legacy; } return self::CHARGE_SENDER; } protected function legacy_charge_party() { return ''; } public function map_status($raw_code) { $map = $this->code_map(); return isset($map[$raw_code]) ? $map[$raw_code] : 'unknown'; } protected function code_map() { return array(); } public function tracking_url($voucher_no) { $tpl = (string) $this->get_setting('tracking_url_template', ''); if ($tpl !== '') { return str_replace('{voucher_no}', rawurlencode((string) $voucher_no), $tpl); } return ''; } protected function get_setting($key, $default = '') { if (!class_exists('AIEO_Fulfillment_Settings')) { return $default; } $code = method_exists($this, 'code') ? (string) $this->code() : ''; if ($code === '') { return $default; } $cfg = (array) AIEO_Fulfillment_Settings::get('courier_' . $code, array()); return array_key_exists($key, $cfg) ? $cfg[$key] : $default; } public function authenticate(array $creds) { return array('ok' => true, 'auth_token' => null, 'expires_at' => null); } public function create_voucher(array $payload) { return $this->unsupported('create_voucher'); } public function create_return_voucher(array $payload) { return $this->unsupported('create_return_voucher'); } public function get_label_pdf($voucher_no, $format = 'A4') { return $this->unsupported('get_label_pdf'); } public function cancel($voucher_no) { return $this->unsupported('cancel'); } public function track($voucher_no) { return $this->unsupported('track'); } public function calculate_cost(array $payload) { return $this->unsupported('calculate_cost'); } public function track_simple($voucher_no) { $r = $this->track($voucher_no); if (!is_array($r) || empty($r['ok'])) { return $r; } unset($r['checkpoints']); return $r; } public function close_pending($voucher_nos = null) { return $this->unsupported('close_pending'); } public function close_pending_by_date($from_iso, $to_iso) { return $this->unsupported('close_pending_by_date'); } public function shop_list() { return $this->unsupported('shop_list'); } private function unsupported($method) { return array( 'ok' => false, 'error' => sprintf( __('Driver "%1$s" does not support %2$s.', 'ai-eshop-optimizer'), $this->code(), $method), ); } public function has_capability($cap) { return in_array($cap, $this->capabilities(), true); } public function create_mode() { return 'api'; } }