From 69d2fc2897f8a77e748116b84779b75570e3a844 Mon Sep 17 00:00:00 2001 From: Bernhard Kau Date: Sun, 5 Feb 2023 22:09:10 +0100 Subject: [PATCH] hit the API only once a day and store the final list in an option --- wpmeetup-widget.php | 64 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/wpmeetup-widget.php b/wpmeetup-widget.php index 2609345..48252ac 100644 --- a/wpmeetup-widget.php +++ b/wpmeetup-widget.php @@ -56,33 +56,69 @@ public function __construct() { * @return array */ public function get_meetups() { - $meetups = []; + // Get new API data, if the current data is empty or expired. + $api_data = $this->get_new_data_from_api(); + + // Create the meetups array from the new API data. + if ( false !== $api_data ) { + $meetups = array(); + foreach ( $api_data as $meetup ) { + if ( ! empty( $meetup['homepage'] ) ) { + $meetups[ $meetup['title']['rendered'] ] = array( + 'title' => $meetup['title']['rendered'], + 'url' => $meetup['homepage'], + ); + } + } + + // Save the new meetups list into the option. + if ( ! empty( $meetups ) ) { + // Store the final list of meetups in an option, as the transient is not persistent. + update_option( 'wpmg_wpmeetup_meetups_list', $meetups ); + + return $meetups; + } + } + + // If no new API data was requested, return the old data. + return get_option( 'wpmg_wpmeetup_meetups_list' ); + } + + /** + * Get meetups from the API. Returns `false` in case the API request was not successful or the last request was not older than one day. + * + * @return false|mixed|null + */ + public function get_new_data_from_api() { + // Set the date of the last API check. + $last_requests = get_transient( 'wpmg_wpmeetup_meetups_request_expiration' ); + // If there was a previous request, and it was less than a day ago, don't get new data from the API. + if ( false !== $last_requests && (int) $last_requests < strtotime( '-1 day' ) ) { + return false; + } - $api_data = get_transient( 'wpmg_wpmeetup_meetups' ); + // Store the current time for the new API request. + set_transient( 'wpmg_wpmeetup_meetups_request_expiration', time() ); + // Use a transient to cache API data. + $api_data = get_transient( 'wpmg_wpmeetup_meetups_api_response' ); if ( false === $api_data ) { $api_request = 'https://wpmeetups.de/wp-json/wp/v2/meetup/?per_page=100&orderby=title&order=asc'; $api_response = wp_remote_get( $api_request ); $api_code = wp_remote_retrieve_response_code( $api_response ); if ( 200 !== $api_code ) { - return; + // If the API responded with a different code as 200, set a shorter expiration time. + set_transient( 'wpmg_wpmeetup_meetups_request_expiration', strtotime( '-1 hour' ) ); + + return false; } $api_data = json_decode( wp_remote_retrieve_body( $api_response ), true ); if ( ! empty( $api_data ) ) { - set_transient( 'wpmg_wpmeetup_meetups', $api_data, DAY_IN_SECONDS ); - } - } - - foreach ( $api_data as $meetup ) { - if ( ! empty( $meetup['homepage'] ) ) { - $meetups[ $meetup['title']['rendered'] ] = array( - 'title' => $meetup['title']['rendered'], - 'url' => $meetup['homepage'], - ); + set_transient( 'wpmg_wpmeetup_meetups_api_response', $api_data, DAY_IN_SECONDS ); } } - return $meetups; + return $api_data; } /**