From 58a9a4530b384abeacedd32fa9055d3346d8286b Mon Sep 17 00:00:00 2001 From: Evan Mullins Date: Thu, 16 Jun 2022 17:38:24 -0400 Subject: [PATCH] cache marketplace response for a day --- components/marketplace/index.js | 1 - includes/MarketplaceApi.php | 64 +++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/components/marketplace/index.js b/components/marketplace/index.js index 3c7b56b..eae808c 100644 --- a/components/marketplace/index.js +++ b/components/marketplace/index.js @@ -39,7 +39,6 @@ import { default as MarketplaceList } from '../marketplaceList/'; methods.apiFetch( { url: `${constants.resturl}/newfold-marketplace/v1/marketplace` }).then( ( response ) => { - console.log( response ); setIsLoading( false ); setMarketplaceItems( response ); setMarketplaceCategories( collectCategories( response ) ); diff --git a/includes/MarketplaceApi.php b/includes/MarketplaceApi.php index 4037534..4592a27 100644 --- a/includes/MarketplaceApi.php +++ b/includes/MarketplaceApi.php @@ -12,6 +12,11 @@ */ class MarketplaceApi { + /** + * Transient name where notifications are stored. + */ + const TRANSIENT = 'newfold_marketplace'; + /** * Register notification routes. */ @@ -25,28 +30,33 @@ public static function registerRoutes() { 'methods' => \WP_REST_Server::READABLE, 'callback' => function ( \WP_REST_Request $request ) { - $marketplace_endpoint = add_query_arg( array( - 'brand' => container()->plugin()->id, - ), NFD_HIIVE_URL . '/sites/v1/products' ); - - $response = wp_remote_get( - $marketplace_endpoint, - array( - 'headers' => array( - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - 'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(), - ), - ) - ); - - // return rest_ensure_response( $response ); - if ( ! is_wp_error( $response ) ) { - $body = wp_remote_retrieve_body( $response ); - $data = json_decode( $body, true ); - if ( $data && is_array( $data ) ) { - $marketplace = $data; - // set_transient( self::TRANSIENT, $marketplace, 7 * DAY_IN_SECONDS ); + $marketplace = get_transient( self::TRANSIENT ); + + if ( false === $marketplace ) { + + $marketplace_endpoint = add_query_arg( array( + 'brand' => container()->plugin()->id, + ), NFD_HIIVE_URL . '/sites/v1/products' ); + + $response = wp_remote_get( + $marketplace_endpoint, + array( + 'headers' => array( + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(), + ), + ) + ); + + // return rest_ensure_response( $response ); + if ( ! is_wp_error( $response ) ) { + $body = wp_remote_retrieve_body( $response ); + $data = json_decode( $body, true ); + if ( $data && is_array( $data ) ) { + $marketplace = $data; + self::setTransient( $marketplace ); + } } } return $marketplace; @@ -59,4 +69,14 @@ public static function registerRoutes() { } + /** + * Set the transient where marketplace is stored. + * + * @param string $data json of marketplace. + * @param float|int $expiration Transient expiration. + */ + public static function setTransient( $data, $expiration = DAY_IN_SECONDS ) { + set_transient( self::TRANSIENT, $data, $expiration ); + } + }