false, 'reason' => 'not_configured']; } $transient = 'pickupselect_status_' . md5(PickupSelect_Plugin::origin() . '|' . $apiKey . '|' . $widgetKey); $cached = get_transient($transient); if (is_array($cached)) { return $cached; } $url = add_query_arg(['apiKey' => rawurlencode($apiKey), 'widgetKey' => rawurlencode($widgetKey)], PickupSelect_Plugin::server_origin() . '/api/catalog/status'); $response = wp_remote_get($url, ['timeout' => 5, 'redirection' => 0, 'headers' => ['Accept' => 'application/json']]); $code = is_wp_error($response) ? 0 : (int) wp_remote_retrieve_response_code($response); $body = is_wp_error($response) ? null : json_decode(wp_remote_retrieve_body($response), true); if ($code === 200 && is_array($body) && isset($body['available'])) { $status = $body; $ttl = $body['available'] ? self::TTL_OK : self::TTL_UNAVAILABLE; } elseif ($code === 404) { // régebbi PickupSelect szerver, amelyen még nincs ilyen végpont: nem tudjuk, nem rejtünk el semmit $status = ['available' => true, 'reason' => null]; $ttl = self::TTL_OK; } elseif ($code === 422) { $status = ['available' => false, 'reason' => 'invalid_keys']; $ttl = self::TTL_UNAVAILABLE; } else { // nem érhető el (hálózat, 5xx): rövid időre rejtjük, utána újra próbáljuk $status = ['available' => false, 'reason' => 'unreachable']; $ttl = self::TTL_ERROR; } set_transient($transient, $status, $ttl); return $status; } public static function available(): bool { return (bool) self::get()['available']; } // az admin értesítés szövege; null, ha a térkép használható (vagy még nincs beállítva: arról a beállítás oldal szól) public static function message(array $status): ?string { switch ($status['reason'] ?? null) { case 'quota_exceeded': $resets = !empty($status['resets_at']) ? wp_date(get_option('time_format'), strtotime($status['resets_at'])) : ''; return sprintf( /* translators: 1: requests today, 2: daily limit, 3: time the limit resets */ __('PickupSelect: today\'s request limit is used up (%1$d of %2$d). The "Pickup point" shipping option is hidden at checkout until the limit resets at %3$s. Upgrade your plan in the PickupSelect dashboard to raise the limit.', 'pickupselect'), (int) ($status['requests_today'] ?? 0), (int) ($status['requests_per_day'] ?? 0), $resets ); case 'no_subscription': return __('PickupSelect: your account has no active plan, so the "Pickup point" shipping option is hidden at checkout. Choose a plan in the PickupSelect dashboard.', 'pickupselect'); case 'invalid_keys': return __('PickupSelect: the API key or widget key is not valid (or this shop\'s address is not allowed), so the "Pickup point" shipping option is hidden at checkout. Check the PickupSelect settings.', 'pickupselect'); case 'unreachable': return __('PickupSelect could not be reached, so the "Pickup point" shipping option is hidden at checkout for now. It comes back automatically.', 'pickupselect'); default: return null; } } // csak a WooCommerce oldalakon és a vezérlőpulton, a boltkezelőknek (a lekérés gyorsítótárazott) public static function admin_notice(): void { $screen = function_exists('get_current_screen') ? get_current_screen() : null; $relevant = $screen && ($screen->id === 'dashboard' || str_contains((string) $screen->id, 'woocommerce') || str_contains((string) $screen->id, 'wc-')); if (!$relevant || !current_user_can('manage_woocommerce') || !PickupSelect_Carriers::instance_ids()) { return; } $message = self::message(self::get()); if ($message !== null) { $link = PickupSelect_Connection::settings_url(); printf( '
%s %s