Skip to content

Instantly share code, notes, and snippets.

@pagelab
Forked from msaari/wp_remote_get_with_cache.php
Created October 19, 2021 00:50
Show Gist options
  • Select an option

  • Save pagelab/97a2de69fd061975ded7f5a23b36489f to your computer and use it in GitHub Desktop.

Select an option

Save pagelab/97a2de69fd061975ded7f5a23b36489f to your computer and use it in GitHub Desktop.

Revisions

  1. @msaari msaari revised this gist Nov 27, 2019. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions wp_remote_get_with_cache.php
    Original file line number Diff line number Diff line change
    @@ -10,21 +10,25 @@
    *
    * @param string $url The URL of the remote resource.
    * @param string $key The name of the option and the transient.
    * @param array $args Argument array that is passed on to wp_remote_get().
    * Default empty array.
    * @param int $expiry The expiration length of the transient, default
    * DAY_IN_SECONDS.
    * @param string $callback A callback function that is applied to the data
    * before it's saved in the transient and option. Default null.
    *
    * @return string The remote data set.
    * @see wp_remote_get()
    *
    * @return string The remote data set, JSON encoded.
    */
    function wp_remote_get_with_cache( $url, $key, $expiry = DAY_IN_SECONDS, $callback = null ) {
    function wp_remote_get_with_cache( $url, $key, $args = array(), $expiry = DAY_IN_SECONDS, $callback = null ) {
    $data = get_transient( $key );
    if ( $data ) {
    // Data found in cache, return that.
    return json_decode( $data );
    }

    $response = wp_remote_get( $url );
    $response = wp_remote_get( $url, $args );
    if ( ! is_array( $response ) ) {
    // Couldn't get the remote data, set the transient for an hour to avoid
    // bothering the server and return backup data.
    @@ -43,4 +47,4 @@ function wp_remote_get_with_cache( $url, $key, $expiry = DAY_IN_SECONDS, $callba
    update_option( $key, $json_encoded_data );

    return $fetched_data;
    }
    }
  2. @msaari msaari revised this gist Nov 27, 2019. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions wp_remote_get_with_cache.php
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    * @param string $callback A callback function that is applied to the data
    * before it's saved in the transient and option. Default null.
    *
    * @return string The remote data.
    * @return string The remote data set.
    */
    function wp_remote_get_with_cache( $url, $key, $expiry = DAY_IN_SECONDS, $callback = null ) {
    $data = get_transient( $key );
    @@ -26,8 +26,11 @@ function wp_remote_get_with_cache( $url, $key, $expiry = DAY_IN_SECONDS, $callba

    $response = wp_remote_get( $url );
    if ( ! is_array( $response ) ) {
    // Couldn't get the remote data, return backup data.
    return json_decode( get_option( $key ) );
    // Couldn't get the remote data, set the transient for an hour to avoid
    // bothering the server and return backup data.
    $json_encoded_data = json_decode( get_option( $key ) );
    set_transient( $key, $json_encoded_data, HOUR_IN_SECONDS );
    return $json_encoded_data;
    }

    $fetched_data = $response['body'];
    @@ -40,4 +43,4 @@ function wp_remote_get_with_cache( $url, $key, $expiry = DAY_IN_SECONDS, $callba
    update_option( $key, $json_encoded_data );

    return $fetched_data;
    }
    }
  3. @msaari msaari renamed this gist Nov 27, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @msaari msaari created this gist Nov 27, 2019.
    43 changes: 43 additions & 0 deletions wp_remote_get_with_cache
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    <?php

    /**
    * Fetches remote data with dual-layer caching.
    *
    * Uses wp_remote_get to fetch remote data. The data is cached twice: it's
    * stored in a transient and an option. The transient is used as the main cache
    * but if the transient has expired and the remote site doesn't respond,
    * backup data from the option is returned.
    *
    * @param string $url The URL of the remote resource.
    * @param string $key The name of the option and the transient.
    * @param int $expiry The expiration length of the transient, default
    * DAY_IN_SECONDS.
    * @param string $callback A callback function that is applied to the data
    * before it's saved in the transient and option. Default null.
    *
    * @return string The remote data.
    */
    function wp_remote_get_with_cache( $url, $key, $expiry = DAY_IN_SECONDS, $callback = null ) {
    $data = get_transient( $key );
    if ( $data ) {
    // Data found in cache, return that.
    return json_decode( $data );
    }

    $response = wp_remote_get( $url );
    if ( ! is_array( $response ) ) {
    // Couldn't get the remote data, return backup data.
    return json_decode( get_option( $key ) );
    }

    $fetched_data = $response['body'];
    if ( is_callable( $callback ) ) {
    $fetched_data = call_user_func( $callback, $fetched_data );
    }

    $json_encoded_data = wp_json_encode( $fetched_data );
    set_transient( $key, $json_encoded_data, $expiry );
    update_option( $key, $json_encoded_data );

    return $fetched_data;
    }