Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve publication fetching routine #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ wp-admin/
wp-content/*.php
wp-content/advanced-cache.php
wp-content/aiowps_backups/
wp-content/ai1wm-backups/
wp-content/backup-db/
wp-content/backups/
wp-content/blogs.dir/
wp-content/cache/
wp-content/fv-flowplayer-custom/
wp-content/jetpack-waf/
wp-content/languages/
wp-content/mu-plugins/
wp-content/upgrade/
Expand All @@ -34,6 +36,7 @@ wp-content/wp-cache-config.php
wp-includes/
wp-snapshots/
/.htaccess
/.user.ini
/license.txt
/readme.html
/sitemap.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ function ngisweden_pubs_shortcode($atts_raw){

// Fetch the cached publications data
$pubs_json = @file_get_contents(get_template_directory().'/cache/publications_cache.json');
$pubs_data = @json_decode($pubs_json, true);
if($pubs_json){
try {
$pubs_data = json_decode($pubs_json, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
$warnings[] = 'JSON decode error while fetching cached NGI publications: ' . $exception->getMessage();
continue;
}
}

// Refresh cache if it doesn't exist or is more than a week old
if(!$pubs_data or $pubs_data['downloaded'] < (time()-(60*60*24*7)) or @count($pubs_data['publications']) == 0 or isset($_GET['refresh'])){
Expand All @@ -41,7 +48,12 @@ function ngisweden_pubs_shortcode($atts_raw){
$pubs_url = 'https://publications.scilifelab.se/label/'.rawurlencode($fac).'.json?limit='.$download_limit;
$pubs_json = file_get_contents($pubs_url);
if($pubs_json){
$pubs_raw_data = json_decode($pubs_json, true);
try {
$pubs_raw_data = json_decode($pubs_json, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
$warnings[] = 'JSON decode error while fetching NGI publications: ' . $exception->getMessage();
continue;
}
$new_pubs_data['publications'] = array_merge($new_pubs_data['publications'], $pubs_raw_data['publications']);
} else {
$warnings[] = 'Could not fetch URL: '.$pubs_url;
Expand Down