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 ) . '

'; + return sprintf( + '

%s
%s
%s %s

', + $this->name, $this->street, $this->zip, $this->city + ); } } @@ -112,9 +147,24 @@ class FF_Community class FF_Meta { private $dir; + private $ed; - function __construct() { - $this->dir = new FF_Directory(); + function reinit_external_data_service( $ext_data_service = null ) { + if ( is_null( $ext_data_service ) ) { + $this->ed = new FF_Meta_Externaldata(); + } else { + $this->ed = $ext_data_service; + } + $this->dir = new FF_Directory( $this->ed ); + } + + function __construct( $ext_data_service = null ) { + if ( is_null( $ext_data_service ) ) { + $this->ed = new FF_Meta_Externaldata(); + } else { + $this->ed = $ext_data_service; + } + $this->dir = new FF_Directory( $this->ed ); } function register_stuff() { @@ -137,18 +187,21 @@ class FF_Meta } function output_ff_state( $citydata ) { - $state = $citydata['state']; - return sprintf( '%s', $state['nodes'] ); + if ( isset( $citydata['state'] ) && isset( $citydata['state']['nodes'] ) ) { + return sprintf( '%s', $citydata['state']['nodes'] ); + } else { + return ''; + } } function aux_get_all_locations() { // gather all location data - if ( false === ( $json_locs = get_transient( 'FF_metadata_json_locs' ) ) ) { + if ( WP_DEBUG || ( false === ( $json_locs = get_transient( 'FF_metadata_json_locs' ) ) ) ) { $all_locs = array(); $arr_select = array( 'lat' => 1, 'lon' => 1 ); foreach ( $this->dir as $tmp_city => $url ) { try { - $tmp_meta = FF_Meta_Externaldata::get( $url ); + $tmp_meta = $this->ed->get( $url ); if ( ! empty( $tmp_meta['location'] ) ) { $tmp_loc = array_intersect_key( $tmp_meta['location'], $arr_select ); $all_locs[$tmp_city] = $tmp_loc; @@ -170,42 +223,44 @@ class FF_Meta $outstr = $loc->format_address(); $json_locs = $this->aux_get_all_locations(); - if ( ! empty( $loc_name ) && ! empty( $loc_name ) ) { + if ( ! empty( $loc->name ) && ! empty( $loc->name ) ) { $icon_url = plugin_dir_url( __FILE__ ) . 'freifunk_marker.png'; + $loccity = $loc->city; $outstr .= << +