400 ) ); } $response = wp_remote_get( coverground_api_origin() . '/api/site' . $path, array( 'timeout' => 10, 'headers' => array( 'Authorization' => 'Bearer ' . $token, 'Accept' => 'application/json', ), ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'unavailable', __( 'Coverground is temporarily unavailable. Try again in a moment.', 'coverground' ), array( 'status' => 502 ) ); } $status = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( 401 === $status || 403 === $status ) { return new WP_Error( 'unauthorized', __( 'The Coverground connection is no longer valid. Reconnect from the Coverground settings page.', 'coverground' ), array( 'status' => 401 ) ); } if ( 404 === $status ) { return new WP_Error( 'not_found', __( 'Route not found — it may no longer be published.', 'coverground' ), array( 'status' => 404 ) ); } if ( $status < 200 || $status >= 300 || ! is_array( $body ) ) { return new WP_Error( 'unavailable', __( 'Coverground is temporarily unavailable. Try again in a moment.', 'coverground' ), array( 'status' => 502 ) ); } return $body; } /** * The published-routes list, transient-cached briefly — it feeds every * editor's picker and the staleness checks. * * @return array|WP_Error */ function coverground_get_routes( bool $bypass_cache = false ) { if ( ! $bypass_cache ) { $cached = get_transient( 'coverground_routes' ); if ( is_array( $cached ) ) { return $cached; } } $body = coverground_api_get( '/routes' ); if ( is_wp_error( $body ) ) { return $body; } $routes = isset( $body['data'] ) && is_array( $body['data'] ) ? $body['data'] : array(); set_transient( 'coverground_routes', $routes, 5 * MINUTE_IN_SECONDS ); return $routes; }