diff --git a/freifunkmeta.php b/freifunkmeta.php index d064b74..b7995bc 100644 --- a/freifunkmeta.php +++ b/freifunkmeta.php @@ -14,20 +14,34 @@ define( 'FF_META_DEFAULT_CITY', 'hamburg' ); /** * class to fetch and cache data from external URLs + * returns either an array from decoded JSON data, or WP_Error */ class FF_Meta_Externaldata { public function get( $url ) { - /* gets metadata from URL, handles caching */ + //error_log( "FF_Meta_Externaldata::get( $url )" ); + /* gets metadata from URL, handles caching, + * hashed because cache keys should be <= 40 chars */ $cachekey = 'ff_metadata_'.hash( 'crc32', $url ); - $cachetime = get_option( 'ff_meta_cachetime', FF_META_DEFAULT_CACHETIME ) * MINUTE_IN_SECONDS; + $cachetime = get_option( + 'ff_meta_cachetime', FF_META_DEFAULT_CACHETIME + ) * MINUTE_IN_SECONDS; // Caching - if ( false === ( $data = get_transient( $cachekey ) ) ) { + if ( WP_DEBUG || ( false === ( $data = get_transient( $cachekey ) ) ) ) { $http_response = wp_remote_get( $url ); - $json = wp_remote_retrieve_body( $http_response ); - $data = json_decode( $json, $assoc = true ); - set_transient( $cachekey, $data, $cachetime ); + if ( is_wp_error( $http_response ) ) { + $error_msg = sprintf( + 'Unable to retrieve URL %s, error: %s', + $url, $http_response->get_error_message() + ); + error_log( $error_msg, 4 ); + return $http_response; + } else { + $json = wp_remote_retrieve_body( $http_response ); + $data = json_decode( $json, $assoc = true ); + set_transient( $cachekey, $data, $cachetime ); + } } return $data; } @@ -40,18 +54,25 @@ class FF_Directory { private $directory; - function __construct() { - $ed = new FF_Meta_Externaldata(); - $this->directory = $ed->get( FF_META_DEFAULT_DIR ); + function __construct( $ext_data_service = null ) { + if ( is_null( $ext_data_service ) ) { + $ed = new FF_Meta_Externaldata(); + } else { + $ed = $ext_data_service; + } + $data = $ed->get( FF_META_DEFAULT_DIR ); + if ( is_wp_error( $data ) ) { + $this->directory = array(); + } else { + $this->directory = $data; + } } function get_url_by_city( $city ) { - $val = $this->directory[$city]; - - if ( empty( $val ) ) { - return false; + if ( array_key_exists( $city, $this->directory ) ) { + return $this->directory[$city]; } else { - return $val; + return false; } } } @@ -73,9 +94,12 @@ class FF_Community */ function __construct( $metadata ) { $loc = $metadata['location']; - $this->name = ( isset( $loc['address'] ) && isset( $loc['address']['Name'] ) ) ? $loc['address']['Name'] : ''; - $this->street = ( isset( $loc['address'] ) && isset( $loc['address']['Street'] ) ) ? $loc['address']['Street'] : ''; - $this->zip = ( isset( $loc['address'] ) && isset( $loc['address']['Zipcode'] ) ) ? $loc['address']['Zipcode'] : ''; + $this->name = ( isset( $loc['address'] ) && isset( $loc['address']['Name'] ) ) + ? $loc['address']['Name'] : ''; + $this->street = ( isset( $loc['address'] ) && isset( $loc['address']['Street'] ) ) + ? $loc['address']['Street'] : ''; + $this->zip = ( isset( $loc['address'] ) && isset( $loc['address']['Zipcode'] ) ) + ? $loc['address']['Zipcode'] : ''; $this->city = isset( $loc['city'] ) ? $loc['city'] : ''; $this->lon = isset( $loc['lon'] ) ? $loc['lon'] : ''; $this->lat = isset( $loc['lat'] ) ? $loc['lat'] : ''; @@ -84,12 +108,19 @@ class FF_Community /** * Alternative constructor from city name */ - static function make_from_city( $city ) { - // TODO: test - if ( false === ( $url = $this->dir->get_url_by_city( $city ) ) ) { - return "\n"; + static function make_from_city( $city, $ext_data_service = null ) { + if ( is_null( $ext_data_service ) ) { + $ed = new FF_Meta_Externaldata(); + } else { + $ed = $ext_data_service; } - if ( false === ( $metadata = FF_Meta_Externaldata::get( $url ) ) ) { + $directory = new FF_Directory( $ed ); + + if ( false === ( $url = $directory->get_url_by_city( $city ) ) ) { + return '\n"; + } + if ( false === ( $metadata = $ed->get( $url ) ) ) { return "\n"; } return new FF_Community( $metadata ); @@ -100,9 +131,13 @@ class FF_Community return ''; } // TODO: style address + map as single box - // TODO: once it is "ready" package openlayers.js into the plugin ( cf. http://docs.openlayers.org/library/deploying.html ) + // TODO: once it is "ready" package openlayers.js into the plugin + // ( cf. http://docs.openlayers.org/library/deploying.html ) // TODO: handle missing values ( i.e. only name & city ) - return '
' . sprintf( '%s
%s
%s %s', $this->name, $this->street, $this->zip, $this->city ) . '
%s
%s
%s %s