From 8b5e2c55aa8d567b708d092f9b1ff277871f77c8 Mon Sep 17 00:00:00 2001 From: Martin Schuette Date: Thu, 3 Apr 2014 21:57:07 +0000 Subject: [PATCH] add and use cache time option --- freifunkmeta.php | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/freifunkmeta.php b/freifunkmeta.php index 528d903..0d2f698 100644 --- a/freifunkmeta.php +++ b/freifunkmeta.php @@ -14,8 +14,9 @@ function ff_meta_getmetadata ($url) { // Caching if ( false === ( $metajson = get_transient( "ff_metadata_${url_hash}" ) ) ) { - $metajson = wp_remote_retrieve_body( wp_remote_get($url) ); - set_transient( "ff_metadata_${url_hash}", $metajson, 1 * HOUR_IN_SECONDS ); + $metajson = wp_remote_retrieve_body( wp_remote_get($url) ); + $cachetime = get_option( 'ff_meta_cachetime' ) * MINUTE_IN_SECONDS; + set_transient( "ff_metadata_${url_hash}", $metajson, $cachetime ); } $metadata = json_decode ( $metajson, $assoc = true ); return $metadata; @@ -133,9 +134,9 @@ function ff_meta_admin_init() { 'ff_meta_plugin' // page to display on ); add_settings_field( - 'ff_meta_dir', // ID - 'URL of directory.json', // Title - 'ff_meta_dir_callback', // callback to fill field + 'ff_meta_cachetime', // ID + 'Cache time', // Title + 'ff_meta_cachetime_callback', // callback to fill field 'ff_meta_plugin', // menu page=slug to display field on 'ff_meta_section-one' // section to display the field in ); @@ -146,18 +147,35 @@ function ff_meta_admin_init() { 'ff_meta_plugin', // menu page=slug to display field on 'ff_meta_section-one' // section to display the field in ); + add_settings_field( + 'ff_meta_dir', // ID + 'URL of directory.json', // Title + 'ff_meta_dir_callback', // callback to fill field + 'ff_meta_plugin', // menu page=slug to display field on + 'ff_meta_section-one' // section to display the field in + ); } function ff_meta_section_one_callback() { echo 'This Plugin provides shortcodes to display information from the Freifunk meta.json.'; } +function ff_meta_cachetime_callback() { + $time = get_option( 'ff_meta_cachetime' ); + if (empty($time)) { + $time = "15"; + } + echo " minutes" + ."

Data from external URLs is cached for this number of minutes.

"; +} + function ff_meta_dir_callback() { $url = get_option( 'ff_meta_dir' ); if (empty($url)) { $url = "https://raw.githubusercontent.com/freifunk/directory.api.freifunk.net/master/directory.json"; } - echo ""; + echo "" + ."

Please keep the default unless you really know what you are doing.

"; } function ff_meta_url_callback() { @@ -165,7 +183,8 @@ function ff_meta_url_callback() { if (empty($url)) { $url = "http://meta.hamburg.freifunk.net/ffhh.json"; } - echo ""; + echo "" + ."

This will be the default for all tags without url=\"xyz\" parameter.

"; } function ff_meta_options_page() {