$apiKey, 'widgetKey' => $widgetKey], PickupSelect_Plugin::server_origin() . '/api/catalog'); // Accept nélkül hibás kulcsnál a szerver átirányít (HTML) a 422-es JSON hiba helyett $response = wp_remote_get($url, ['timeout' => 10, 'redirection' => 0, 'headers' => ['Accept' => 'application/json']]); $result = self::parse($response); set_transient($transient, is_wp_error($result) ? $result->get_error_message() : $result, is_wp_error($result) ? self::ERROR_TTL : self::TTL); self::remember($transient); return $result; } // "Refresh from PickupSelect": az összes eltárolt katalógus törlése public static function forget(): void { foreach ((array) get_option(self::KEYS_OPTION, []) as $transient) { delete_transient($transient); } delete_option(self::KEYS_OPTION); } /** * Választási lehetőségek a szállítási mód beállításaihoz: érték => felirat. * Az értékek az override-ok nevei "|" jellel elválasztva (ország, ország|szállító, ország|szállító|csoport). */ public static function options(array $catalog): array { $options = ['countries' => [], 'carriers' => [], 'groups' => []]; foreach ($catalog['countries'] ?? [] as $country) { $options['countries'][$country['name']] = $country['name']; foreach ($country['carriers'] ?? [] as $carrier) { $carrierValue = $country['name'] . '|' . $carrier['name']; $options['carriers'][$carrierValue] = $country['name'] . ' – ' . $carrier['name']; foreach ($carrier['groups'] ?? [] as $group) { $options['groups'][$carrierValue . '|' . $group['name']] = $country['name'] . ' – ' . $carrier['name'] . ' – ' . $group['name']; } } } return $options; } // országnév => ISO kód, a pénztár ország előválasztásához public static function country_codes(array $catalog): array { $codes = []; foreach ($catalog['countries'] ?? [] as $country) { $codes[$country['name']] = $country['code']; } return $codes; } private static function parse($response) { if (is_wp_error($response)) { /* translators: %s: error message */ return new WP_Error('pickupselect_catalog', sprintf(__('Could not reach PickupSelect: %s', 'pickupselect'), $response->get_error_message())); } $code = wp_remote_retrieve_response_code($response); $body = json_decode(wp_remote_retrieve_body($response), true); if ($code !== 200 || !is_array($body) || !isset($body['countries'])) { $message = $body['error'] ?? $body['message'] ?? ''; if ($code === 422) { $message = __('The API key or widget key is wrong, they don\'t belong together, or this shop\'s address is not an allowed origin of the API key.', 'pickupselect'); } /* translators: 1: HTTP status code, 2: error message */ return new WP_Error('pickupselect_catalog', trim(sprintf(__('PickupSelect returned an error (%1$d). %2$s', 'pickupselect'), $code, $message))); } return $body; } private static function transient_key(string $apiKey, string $widgetKey): string { return 'pickupselect_catalog_' . md5(PickupSelect_Plugin::origin() . '|' . $apiKey . '|' . $widgetKey); } private static function remember(string $transient): void { $keys = (array) get_option(self::KEYS_OPTION, []); if (!in_array($transient, $keys, true)) { $keys[] = $transient; update_option(self::KEYS_OPTION, $keys, false); } } }