Created
November 1, 2018 20:15
-
-
Save mediaformat/4f1863bd8434b42d91d48722b237dba6 to your computer and use it in GitHub Desktop.
RSS feed cache with redundancy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* RSS Cache | |
* Cache: 1 hour | |
* Cache fallback: no expiry (not infaillible) | |
*/ | |
// PROD | |
$newsURLall = 'https://news.example.com/'; | |
$mynews_url = 'https://news.example.com/rss'; | |
//DEV | |
//error_log( "News: delete cache."); | |
//delete_transient('mynews'); | |
//delete_transient('mynews_cache_fallback'); | |
if ( !get_transient( 'mynews' ) ) { | |
//error_log( "News: Expired cache."); | |
$response = wp_remote_get($mynews_url); | |
if ( !is_array( $response ) && is_wp_error( $response ) ){ | |
error_log( "News: The news service is not available. ".$response->get_error_message() ); | |
if ( false === ( get_transient( 'mynews_cache_fallback' ) ) ) { | |
if ( current_user_can( 'manage_options' ) ) {// ?> | |
<div class="notice notice-error"> | |
<p><?php _e( "News: The news service is not available." ); ?></p> | |
</div><?php | |
error_log( "News: The news service is not available, and the redundant cache has expired. ".$response->get_error_message() ); | |
} | |
return; | |
} else { | |
//error_log( "News: We have a cache fallback. ".$response->get_error_message() ); | |
$mynews = get_transient('mynews_cache_fallback' ); | |
} | |
} else { | |
//error_log('News: Cache refresh'); | |
$mynews = $response['body']; | |
delete_transient('mynews'); | |
delete_transient('mynews_cache_fallback'); | |
set_transient('mynews', $mynews, 1 * HOUR_IN_SECONDS ); | |
set_transient('mynews_cache_fallback', $mynews ); | |
} | |
} else { | |
//error_log( "News: Lets use the cache."); | |
$mynews = get_transient('mynews_cache_fallback' ); | |
} | |
?> | |
<div id="news" class="news_section"> | |
<h2><?php _e('News', ''); ?></h2> | |
<div class="all_links hidden-xs"> | |
<a href="<?php echo $newURLall; ?>"><?php _e('All news stories', ''); ?></a> | |
</div> | |
<div class="news" style="clear: both"> | |
<?php | |
$news_json = json_decode($mynews, true); | |
if (empty($news_json['data'])){ | |
echo 'No news.'; | |
} | |
foreach ($news_json['data'] as $news_story) { ?> | |
<div class="news__item"> | |
<div class="news__date"><?php | |
$news_sdate = DateTime::createFromFormat('Y-m-d H:i:s', $news_story['next_date']); | |
$news_ndate = DateTime::createFromFormat('Y-m-d H:i:s', $news_story['end_date']); | |
echo ucfirst( date_i18n( 'l j F', $news_sdate->getTimestamp() ) ); ?></div> | |
<div class="news__time clear"><?php | |
echo date_format($news_sdate, 'G\hi'); | |
echo ' ' . date_format($news_ndate, 'G\hi'); | |
?></div> | |
<a class="news__title" href="<?php echo $news_story['url']; ?>"><?php | |
echo $news_story['title']; | |
?></a> | |
</div> | |
<?php } //end foreach | |
?> | |
</div> | |
<div class="clear"></div> | |
</div><!-- #/news--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment