Skip to content

Commit

Permalink
Cache single theme response for 1 day
Browse files Browse the repository at this point in the history
  • Loading branch information
roccotrip committed Jan 7, 2025
1 parent affa4f0 commit 8e9f088
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/ThemeGarden.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,22 @@ public function enqueue_admin_styles( $version ): void {
* @return \WP_Error | object
*/
public function get_theme( string $theme_id ) {
$response = wp_remote_get( self::THEME_GARDEN_ENDPOINT . '/theme/' . esc_attr( $theme_id ) . '?time=' . time() );
$status = wp_remote_retrieve_response_code( $response );
$cached_response = get_transient( 'ttgarden_tumblr_theme_response_' . $theme_id );

if ( 200 !== $status ) {
return new \WP_Error();
if ( false === $cached_response ) {
$response = wp_remote_get( self::THEME_GARDEN_ENDPOINT . '/theme/' . esc_attr( $theme_id ) . '?time=' . time() );
$status = wp_remote_retrieve_response_code( $response );
if ( 200 !== $status ) {
return new \WP_Error();
}

$cached_response = wp_remote_retrieve_body( $response );
set_transient( 'ttgarden_tumblr_theme_response_' . $theme_id, $cached_response, DAY_IN_SECONDS );
}

$body = json_decode( wp_remote_retrieve_body( $response ) );


$body = json_decode( wp_remote_retrieve_body( $cached_response ) );

if ( ! isset( $body->response->theme ) ) {
return new \WP_Error();
Expand Down Expand Up @@ -288,12 +296,13 @@ public function maybe_activate_theme(): void {
* @return array
*/
public function get_themes_and_categories(): array {
$cached_response = get_transient( 'ttgarden_tumblr_themes_response_' . $this->get_api_query_string() );
$query_string = $this->get_api_query_string();
$cached_response = get_transient( 'ttgarden_tumblr_themes_response_' . $query_string );

if ( false === $cached_response ) {
$response = wp_remote_get( self::THEME_GARDEN_ENDPOINT . $this->get_api_query_string() );
$cached_response = wp_remote_retrieve_body( $response );
set_transient( 'ttgarden_tumblr_themes_response', $cached_response, DAY_IN_SECONDS );
set_transient( 'ttgarden_tumblr_themes_response_' . $query_string, $cached_response, DAY_IN_SECONDS );
}

$body = json_decode( $cached_response, true );
Expand Down

0 comments on commit 8e9f088

Please sign in to comment.