Skip to content

Commit

Permalink
hit the API only once a day and store the final list in an option
Browse files Browse the repository at this point in the history
  • Loading branch information
2ndkauboy committed Feb 5, 2023
1 parent ef008d6 commit 69d2fc2
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions wpmeetup-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 69d2fc2

Please sign in to comment.