some more code formatting, also add FF_Meta_Externaldata as service to FF_Dir and FF_Meta

This commit is contained in:
Martin Schütte 2014-06-16 14:13:12 +00:00
parent d538e719db
commit c6e663e9fa

View file

@ -14,21 +14,35 @@ define( 'FF_META_DEFAULT_CITY', 'hamburg' );
/** /**
* class to fetch and cache data from external URLs * class to fetch and cache data from external URLs
* returns either an array from decoded JSON data, or WP_Error
*/ */
class FF_Meta_Externaldata class FF_Meta_Externaldata
{ {
public function get( $url ) { 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 ); $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 // Caching
if ( false === ( $data = get_transient( $cachekey ) ) ) { if ( WP_DEBUG || ( false === ( $data = get_transient( $cachekey ) ) ) ) {
$http_response = wp_remote_get( $url ); $http_response = wp_remote_get( $url );
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 ); $json = wp_remote_retrieve_body( $http_response );
$data = json_decode( $json, $assoc = true ); $data = json_decode( $json, $assoc = true );
set_transient( $cachekey, $data, $cachetime ); set_transient( $cachekey, $data, $cachetime );
} }
}
return $data; return $data;
} }
} }
@ -40,18 +54,25 @@ class FF_Directory
{ {
private $directory; private $directory;
function __construct() { function __construct( $ext_data_service = null ) {
if ( is_null( $ext_data_service ) ) {
$ed = new FF_Meta_Externaldata(); $ed = new FF_Meta_Externaldata();
$this->directory = $ed->get( FF_META_DEFAULT_DIR ); } 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 ) { function get_url_by_city( $city ) {
$val = $this->directory[$city]; if ( array_key_exists( $city, $this->directory ) ) {
return $this->directory[$city];
if ( empty( $val ) ) {
return false;
} else { } else {
return $val; return false;
} }
} }
} }
@ -73,9 +94,12 @@ class FF_Community
*/ */
function __construct( $metadata ) { function __construct( $metadata ) {
$loc = $metadata['location']; $loc = $metadata['location'];
$this->name = ( isset( $loc['address'] ) && isset( $loc['address']['Name'] ) ) ? $loc['address']['Name'] : ''; $this->name = ( isset( $loc['address'] ) && isset( $loc['address']['Name'] ) )
$this->street = ( isset( $loc['address'] ) && isset( $loc['address']['Street'] ) ) ? $loc['address']['Street'] : ''; ? $loc['address']['Name'] : '';
$this->zip = ( isset( $loc['address'] ) && isset( $loc['address']['Zipcode'] ) ) ? $loc['address']['Zipcode'] : ''; $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->city = isset( $loc['city'] ) ? $loc['city'] : '';
$this->lon = isset( $loc['lon'] ) ? $loc['lon'] : ''; $this->lon = isset( $loc['lon'] ) ? $loc['lon'] : '';
$this->lat = isset( $loc['lat'] ) ? $loc['lat'] : ''; $this->lat = isset( $loc['lat'] ) ? $loc['lat'] : '';
@ -84,12 +108,19 @@ class FF_Community
/** /**
* Alternative constructor from city name * Alternative constructor from city name
*/ */
static function make_from_city( $city ) { static function make_from_city( $city, $ext_data_service = null ) {
// TODO: test if ( is_null( $ext_data_service ) ) {
if ( false === ( $url = $this->dir->get_url_by_city( $city ) ) ) { $ed = new FF_Meta_Externaldata();
return "<!-- FF Meta Error: cannot get directory.json, or no URL for '$city' -->\n"; } 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 '<!-- FF Meta Error: cannot get directory.json, '.
" or no URL for '$city' -->\n";
}
if ( false === ( $metadata = $ed->get( $url ) ) ) {
return "<!-- FF Meta Error: cannot get metadata from $url -->\n"; return "<!-- FF Meta Error: cannot get metadata from $url -->\n";
} }
return new FF_Community( $metadata ); return new FF_Community( $metadata );
@ -100,9 +131,13 @@ class FF_Community
return ''; return '';
} }
// TODO: style address + map as single box // 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 ) // TODO: handle missing values ( i.e. only name & city )
return '<p>' . sprintf( '%s<br/>%s<br/>%s %s', $this->name, $this->street, $this->zip, $this->city ) . '</p>'; return sprintf(
'<p>%s<br/>%s<br/>%s %s</p>',
$this->name, $this->street, $this->zip, $this->city
);
} }
} }
@ -112,9 +147,24 @@ class FF_Community
class FF_Meta class FF_Meta
{ {
private $dir; private $dir;
private $ed;
function __construct() { function reinit_external_data_service( $ext_data_service = null ) {
$this->dir = new FF_Directory(); 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() { function register_stuff() {
@ -137,18 +187,21 @@ class FF_Meta
} }
function output_ff_state( $citydata ) { function output_ff_state( $citydata ) {
$state = $citydata['state']; if ( isset( $citydata['state'] ) && isset( $citydata['state']['nodes'] ) ) {
return sprintf( '%s', $state['nodes'] ); return sprintf( '%s', $citydata['state']['nodes'] );
} else {
return '';
}
} }
function aux_get_all_locations() { function aux_get_all_locations() {
// gather all location data // 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(); $all_locs = array();
$arr_select = array( 'lat' => 1, 'lon' => 1 ); $arr_select = array( 'lat' => 1, 'lon' => 1 );
foreach ( $this->dir as $tmp_city => $url ) { foreach ( $this->dir as $tmp_city => $url ) {
try { try {
$tmp_meta = FF_Meta_Externaldata::get( $url ); $tmp_meta = $this->ed->get( $url );
if ( ! empty( $tmp_meta['location'] ) ) { if ( ! empty( $tmp_meta['location'] ) ) {
$tmp_loc = array_intersect_key( $tmp_meta['location'], $arr_select ); $tmp_loc = array_intersect_key( $tmp_meta['location'], $arr_select );
$all_locs[$tmp_city] = $tmp_loc; $all_locs[$tmp_city] = $tmp_loc;
@ -170,20 +223,22 @@ class FF_Meta
$outstr = $loc->format_address(); $outstr = $loc->format_address();
$json_locs = $this->aux_get_all_locations(); $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'; $icon_url = plugin_dir_url( __FILE__ ) . 'freifunk_marker.png';
$loccity = $loc->city;
$outstr .= <<<EOT $outstr .= <<<EOT
<div id="mapdiv_$loc_city" style="width: 75%; height: 15em;"></div> <div id="mapdiv_${loccity}" style="width: 75%; height: 15em;"></div>
<style type="text/css"> <!-- <style type="text/css"> <!--
/* There seems to be a bug in OpenLayers' style.css ( ? ). Original bottom:4.5em is far too high. */ /* There seems to be a bug in OpenLayers' style.css ( ? ).
* Original bottom:4.5em is far too high. */
#OpenLayers_Control_Attribution_7 { bottom: 3px; } #OpenLayers_Control_Attribution_7 { bottom: 3px; }
--></style> --></style>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script> <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script> <script>
map = new OpenLayers.Map( "mapdiv_$loc->city" ); map = new OpenLayers.Map( "mapdiv_${loccity}" );
map.addLayer( new OpenLayers.Layer.OSM( ) ); map.addLayer( new OpenLayers.Layer.OSM() );
var lonLat = new OpenLayers.LonLat( $loc->lon, $loc->lat ) var lonLat = new OpenLayers.LonLat( $loc->lon, $loc->lat )
.transform( .transform(
@ -194,18 +249,18 @@ class FF_Meta
var markers = new OpenLayers.Layer.Markers( "Markers" ); var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer( markers ); map.addLayer( markers );
markers.addMarker( new OpenLayers.Marker(lonLat ) ); markers.addMarker( new OpenLayers.Marker( lonLat ) );
var size = new OpenLayers.Size( 20,16 ); var size = new OpenLayers.Size( 20,16 );
var offset = new OpenLayers.Pixel( 0, -(size.h/2 ) ); var offset = new OpenLayers.Pixel( 0, -( size.h/2 ) );
var icon = new OpenLayers.Icon( '$icon_url',size,offset ); var icon = new OpenLayers.Icon( '$icon_url',size,offset );
var ff_loc = $json_locs; var ff_loc = $json_locs;
delete ff_loc["$city"]; delete ff_loc["$loccity"];
for ( key in ff_loc ) { for ( key in ff_loc ) {
markers.addMarker( new OpenLayers.Marker( markers.addMarker( new OpenLayers.Marker(
new OpenLayers.LonLat( ff_loc[key]['lon'], ff_loc[key]['lat'] ) new OpenLayers.LonLat( ff_loc[key]['lon'], ff_loc[key]['lat'] )
.transform( new OpenLayers.Projection("EPSG:4326" ),map.getProjectionObject() ), .transform( new OpenLayers.Projection( "EPSG:4326" ),map.getProjectionObject() ),
icon.clone() icon.clone()
) ); ) );
} }
@ -219,29 +274,47 @@ EOT;
} }
function output_ff_services( $citydata ) { function output_ff_services( $citydata ) {
$outstr = '<ul>'; if ( ! isset( $citydata['services'] ) ) {
if ( isset( $citydata['services'] ) ) { return '';
$services = $citydata['services'];
foreach ( $services as $service ) {
$outstr .= sprintf( '<li>%s (%s ): <a href="%s">%s</a></li>', $service['serviceName'], $service['serviceDescription'], $service['internalUri'], $service['internalUri'] );
} }
$services = $citydata['services'];
$outstr = '<ul>';
foreach ( $services as $service ) {
$outstr .= sprintf(
'<li>%s ( %s ): <a href="%s">%s</a></li>',
$service['serviceName'], $service['serviceDescription'],
$service['internalUri'], $service['internalUri']
);
} }
$outstr .= '</ul>'; $outstr .= '</ul>';
return $outstr; return $outstr;
} }
function output_ff_contact( $citydata ) { function output_ff_contact( $citydata ) {
$outstr = '<p>'; if ( ! isset( $citydata['contact'] ) ) {
return '';
}
$contact = $citydata['contact']; $contact = $citydata['contact'];
// Output -- rather ugly but the data is not uniform, some fields are URIs, some are usernames, ... $outstr = '<p>';
// Output -- rather ugly but the data is not uniform,
// some fields are URIs, some are usernames, ...
if ( ! empty( $contact['email'] ) ) { if ( ! empty( $contact['email'] ) ) {
$outstr .= sprintf( "E-Mail: <a href=\"mailto:%s\">%s</a><br />\n", $contact['email'], $contact['email'] ); $outstr .= sprintf(
'E-Mail: <a href=\"mailto:%s\">%s</a><br />',
$contact['email'], $contact['email']
);
} }
if ( ! empty( $contact['ml'] ) ) { if ( ! empty( $contact['ml'] ) ) {
$outstr .= sprintf( "Mailingliste: <a href=\"mailto:%s\">%s</a><br />\n", $contact['ml'], $contact['ml'] ); $outstr .= sprintf(
'Mailingliste: <a href=\"mailto:%s\">%s</a><br />',
$contact['ml'], $contact['ml']
);
} }
if ( ! empty( $contact['irc'] ) ) { if ( ! empty( $contact['irc'] ) ) {
$outstr .= sprintf( "IRC: <a href=\"%s\">%s</a><br />\n", $contact['irc'], $contact['irc'] ); $outstr .= sprintf(
'IRC: <a href=\"%s\">%s</a><br />',
$contact['irc'], $contact['irc']
);
} }
if ( ! empty( $contact['twitter'] ) ) { if ( ! empty( $contact['twitter'] ) ) {
// catch username instead of URI // catch username instead of URI
@ -250,18 +323,32 @@ EOT;
$twitter_handle = $contact['twitter']; $twitter_handle = $contact['twitter'];
} else { } else {
$twitter_url = $contact['twitter']; $twitter_url = $contact['twitter'];
$twitter_handle = '@' . substr( $contact['twitter'], strrpos( $contact['twitter'], '/' ) + 1 ); $twitter_handle = '@' . substr(
$contact['twitter'], strrpos( $contact['twitter'], '/' ) + 1
);
} }
$outstr .= sprintf( "Twitter: <a href=\"%s\">%s</a><br />\n", $twitter_url, $twitter_handle ); $outstr .= sprintf(
'Follow us: <a href=\"%s\">%s</a><br />',
$twitter_url, $twitter_handle
);
} }
if ( ! empty( $contact['facebook'] ) ) { if ( ! empty( $contact['facebook'] ) ) {
$outstr .= sprintf( "Facebook: <a href=\"%s\">%s</a><br />\n", $contact['facebook'], $contact['facebook'] ); $outstr .= sprintf(
'Facebook: <a href=\"%s\">%s</a><br />',
$contact['facebook'], $contact['facebook']
);
} }
if ( ! empty( $contact['googleplus'] ) ) { if ( ! empty( $contact['googleplus'] ) ) {
$outstr .= sprintf( "G+: <a href=\"%s\">%s</a><br />\n", $contact['googleplus'], $contact['googleplus'] ); $outstr .= sprintf(
'G+: <a href=\"%s\">%s</a><br />',
$contact['googleplus'], $contact['googleplus']
);
} }
if ( ! empty( $contact['jabber'] ) ) { if ( ! empty( $contact['jabber'] ) ) {
$outstr .= sprintf( "XMPP: <a href=\"xmpp:%s\">%s</a><br />\n", $contact['jabber'], $contact['jabber'] ); $outstr .= sprintf(
'XMPP: <a href=\"xmpp:%s\">%s</a><br />',
$contact['jabber'], $contact['jabber']
);
} }
$outstr .= '</p>'; $outstr .= '</p>';
return $outstr; return $outstr;
@ -283,7 +370,8 @@ EOT;
return "<!-- FF Meta Error: cannot get directory.json, or no URL for '$city' -->\n"; return "<!-- FF Meta Error: cannot get directory.json, or no URL for '$city' -->\n";
} }
if ( false === ( $metadata = FF_Meta_Externaldata::get( $cityurl ) ) ) { $ed = new FF_Meta_Externaldata();
if ( false === ( $metadata = $this->ed->get( $cityurl ) ) ) {
return "<!-- FF Meta Error: cannot get metadata from $cityurl -->\n"; return "<!-- FF Meta Error: cannot get metadata from $cityurl -->\n";
} }
@ -357,18 +445,22 @@ EOT;
} }
function section_one_callback() { function section_one_callback() {
echo 'This Plugin provides shortcodes to display information from the Freifunk meta.json.'; echo 'This Plugin provides shortcodes to display information'
.' from the Freifunk meta.json.';
} }
function cachetime_callback() { function cachetime_callback() {
$time = get_option( 'ff_meta_cachetime', FF_META_DEFAULT_CACHETIME ); $time = get_option( 'ff_meta_cachetime', FF_META_DEFAULT_CACHETIME );
echo "<input type='number' name='ff_meta_cachetime' id='ff_meta_cachetime_id'" echo '<input type="number" name="ff_meta_cachetime" '
." class='small-text code' value='". intval( $time ) . ' /> minutes'. .'id="ff_meta_cachetime_id" class="small-text code" value="'
"<p class='description'>Data from external URLs is cached for this number of minutes.</p>"; . esc_attr( $time ) . ' /> minutes'
.'<p class="description">Data from external URLs is cached'
.' for this number of minutes.</p>';
} }
function city_callback() { function city_callback() {
if ( false === ( $directory = FF_Meta_Externaldata::get( FF_META_DEFAULT_DIR ) ) ) { $ed = new FF_Meta_Externaldata();
if ( false === ( $directory = $this->ed->get( FF_META_DEFAULT_DIR ) ) ) {
// TODO: error handling // TODO: error handling
return; return;
} }
@ -378,11 +470,13 @@ EOT;
foreach ( array_keys( $directory ) as $city ) { foreach ( array_keys( $directory ) as $city ) {
$prettycity = ucwords( str_replace( array( '_', '-' ), ' ', $city ) ); $prettycity = ucwords( str_replace( array( '_', '-' ), ' ', $city ) );
$selected = selected( $default_city, $city ); $selected = selected( $default_city, $city );
echo "<option value='" . esc attr( $city ) . "' $selected>" printf(
. esc_html( $prettycity ) . '</option>'; '<option value="%s" %s>%s</option>',
esc_attr( $city ), $selected, esc_str( $prettycity )
);
} }
echo '</select>'; echo '</select>';
echo "<p class='description'>This is the default city parameter.</p>"; echo '<p class="description">This is the default city parameter.</p>';
} }
function options_page() { function options_page() {
@ -402,7 +496,9 @@ EOT;
delete_option( 'ff_meta_city' ); delete_option( 'ff_meta_city' );
delete_option( 'ff_meta_cachetime' ); delete_option( 'ff_meta_cachetime' );
} }
} }
$ffmeta = new FF_Meta; $ffmeta = new FF_Meta();
$ffmeta->register_stuff(); $ffmeta->register_stuff();
$GLOBALS['wp-plugin-ffmeta'] = $ffmeta;