transport = $transport ?? 'wp_remote_get'; } /** * The static fallback prices, shown whenever the live fetch fails or * hasn't completed yet. Mirrors ABDS_LS_Plan_Catalog::default_prices() * for 'abds-seo' on the license server as of 19Aug26 — these are the * plugin's own shipped defaults, not fetched, so they need updating by * hand if the server's shipped defaults ever change. * * @return array */ public static function fallback_tiers(): array { return array( 'pro' => array( 'usd' => array( 'monthly' => 7.0, 'yearly' => 69.0, ), 'uah' => array( 'monthly' => 299.0, 'yearly' => 2990.0, ), ), 'business' => array( 'usd' => array( 'monthly' => 18.0, 'yearly' => 179.0, ), 'uah' => array( 'monthly' => 799.0, 'yearly' => 7990.0, ), ), 'agency' => array( 'usd' => array( 'monthly' => 35.0, 'yearly' => 349.0, ), 'uah' => array( 'monthly' => 1590.0, 'yearly' => 15990.0, ), ), ); } /** * The pricing data for the UI: tier => currency => cycle => amount, which * currencies are currently sellable, and whether this came from a live * fetch or the static fallback (purely informational — the UI treats both * shapes identically, this is for anyone reading the cached value while * debugging). * * @param bool $force Bypass the cache and re-check now. * @return array{tiers:array,currencies:string[],source:string} */ public function get_prices( bool $force = false ): array { if ( ! $force ) { $cached = get_transient( self::TRANSIENT_KEY ); if ( is_array( $cached ) ) { return $cached; } } $live = $this->fetch_live(); $result = null !== $live ? array( 'tiers' => $live['tiers'], 'currencies' => $live['currencies'], 'source' => 'live', ) : array( 'tiers' => self::fallback_tiers(), 'currencies' => array( 'USD', 'UAH' ), 'source' => 'fallback', ); // Cache the fallback result too (same TTL) — otherwise an unreachable // server would be retried on every single Settings page load instead // of once per cache window. set_transient( self::TRANSIENT_KEY, $result, self::CACHE_TTL ); return $result; } /** * The license server base URL — same constant/filter ABDS_SEO_License_Client * uses, so a local wp-env stand override applies here too. * * @return string */ protected function server_url(): string { $url = defined( 'ABDS_SEO_LICENSE_SERVER' ) ? (string) ABDS_SEO_LICENSE_SERVER : ABDS_SEO_License_Client::DEFAULT_SERVER; return untrailingslashit( (string) apply_filters( 'abds_seo_license_server_url', $url ) ); } /** * Fetches /payment-config + /plans (once per currency it reports) and * merges them. Returns null on ANY failure — a missing/malformed * response for even one currency degrades to the full static fallback * rather than showing partial/inconsistent prices. * * @return array{tiers:array,currencies:string[]}|null */ protected function fetch_live(): ?array { $config = $this->get_json( '/payment-config', array() ); if ( null === $config || empty( $config['currencies'] ) || ! is_array( $config['currencies'] ) ) { return null; } $currencies = array_values( array_intersect( $config['currencies'], array( 'USD', 'UAH' ) ) ); $currency_responses = array(); foreach ( $currencies as $currency ) { $plans = $this->get_json( '/plans', array( 'product' => self::PRODUCT_SKU, 'currency' => $currency, ) ); if ( null === $plans || empty( $plans['plans'] ) || ! is_array( $plans['plans'] ) ) { return null; } $currency_responses[ $currency ] = $plans; } if ( empty( $currency_responses ) ) { return null; } return array( 'tiers' => self::merge_plans_responses( $currency_responses ), 'currencies' => $currencies, ); } /** * Pure merge of one or more /plans responses (one per currency) into a * single tier => currency => cycle => amount shape. No WordPress calls — * unit-tested standalone. * * @param array}>}> $currency_responses Currency (e.g. 'USD') => decoded /plans response body. * @return array>> Tier => lowercase currency => cycle => amount. */ public static function merge_plans_responses( array $currency_responses ): array { $tiers = array(); foreach ( $currency_responses as $currency => $response ) { $currency_key = strtolower( (string) $currency ); foreach ( (array) ( $response['plans'] ?? array() ) as $plan ) { if ( empty( $plan['tier'] ) ) { continue; } $tier = (string) $plan['tier']; if ( ! isset( $tiers[ $tier ] ) ) { $tiers[ $tier ] = array(); } $cycles = array(); foreach ( (array) ( $plan['cycles'] ?? array() ) as $cycle => $entry ) { $cycles[ $cycle ] = (float) ( $entry['amount'] ?? 0 ); } $tiers[ $tier ][ $currency_key ] = $cycles; } } return $tiers; } /** * GETs a license-server REST route (the `?rest_route=` form * ABDS_SEO_License_Client already uses) and JSON-decodes the body. Never * throws — any transport error, non-200, or unparsable body returns * null, which fetch_live() treats as "fall back to static prices". * * @param string $route Route, e.g. '/payment-config'. * @param array $args Extra query args. * @return array|null */ protected function get_json( string $route, array $args ) { $url = add_query_arg( array_merge( array( 'rest_route' => '/abds-ls/v1' . $route ), $args ), $this->server_url() . '/' ); $response = call_user_func( $this->transport, $url, array( 'timeout' => 5 ) ); if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { return null; } $data = json_decode( (string) wp_remote_retrieve_body( $response ), true ); return is_array( $data ) ? $data : null; } }