From d3a41d172e2c35c0eac47590f7fc8b57632143a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sch=C3=BCtte?= Date: Tue, 10 Jun 2014 13:19:47 +0000 Subject: [PATCH 01/16] travis: try larger build matrix --- .travis.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.travis.yml b/.travis.yml index d494c8c..61ce1dc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,13 @@ php: - 5.3 - 5.4 - 5.5 + - 5.6 + - hhvm + - hhvm-nightly + +os: + - linux + - osx env: - WP_VERSION=latest WP_MULTISITE=0 From 7dc5f0d712bfa558a05b89d70c6622617aa5adf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sch=C3=BCtte?= Date: Tue, 10 Jun 2014 14:33:55 +0000 Subject: [PATCH 02/16] travis: more WP versions --- .travis.yml | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 61ce1dc..2f5018d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,16 +6,14 @@ php: - 5.5 - 5.6 - hhvm - - hhvm-nightly - -os: - - linux - - osx env: - - WP_VERSION=latest WP_MULTISITE=0 + - WP_VERSION=3.7 + - WP_VERSION=3.8 + - WP_VERSION=latest before_script: - - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION + - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION + - fgrep wp_version $WP_TESTS_DIR/wp-includes/version.php script: phpunit From 753b512f7e19d2c6865779252faf4b218bcf2559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sch=C3=BCtte?= Date: Tue, 10 Jun 2014 14:39:58 +0000 Subject: [PATCH 03/16] travis: fix version grep --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2f5018d..0576d02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,6 @@ env: before_script: - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - - fgrep wp_version $WP_TESTS_DIR/wp-includes/version.php + - fgrep wp_version ${WP_TESTS_DIR-/tmp/wordpress-tests-lib}/wp-includes/version.php script: phpunit From 2ee9fabd7fcec743905d1779781a2716e5dc73e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sch=C3=BCtte?= Date: Tue, 10 Jun 2014 14:51:22 +0000 Subject: [PATCH 04/16] travis: fix version grep --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0576d02..baa36f3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,6 @@ env: before_script: - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - - fgrep wp_version ${WP_TESTS_DIR-/tmp/wordpress-tests-lib}/wp-includes/version.php + - fgrep wp_version /tmp/wordpress/wp-includes/version.php script: phpunit From 66d9c966e708adc74593a169e4b2a4aff6520b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sch=C3=BCtte?= Date: Tue, 10 Jun 2014 15:00:36 +0000 Subject: [PATCH 05/16] travis: try some older WP versions --- .travis.yml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index baa36f3..f749fde 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,15 +2,12 @@ language: php php: - 5.3 - - 5.4 - 5.5 - - 5.6 - - hhvm env: - - WP_VERSION=3.7 - - WP_VERSION=3.8 - - WP_VERSION=latest + - WP_VERSION=3.0.6 + - WP_VERSION=3.5.2 + - WP_VERSION=3.6.1 before_script: - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION From 45a37680ee0e5cddb85f20c53ad310908c20cb82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sch=C3=BCtte?= Date: Tue, 10 Jun 2014 15:03:45 +0000 Subject: [PATCH 06/16] Revert "travis: try some older WP versions" This reverts commit 66d9c966e708adc74593a169e4b2a4aff6520b08. --- .travis.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index f749fde..baa36f3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,12 +2,15 @@ language: php php: - 5.3 + - 5.4 - 5.5 + - 5.6 + - hhvm env: - - WP_VERSION=3.0.6 - - WP_VERSION=3.5.2 - - WP_VERSION=3.6.1 + - WP_VERSION=3.7 + - WP_VERSION=3.8 + - WP_VERSION=latest before_script: - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION From d538e719db65374c53f6644f52780659a96d4f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sch=C3=BCtte?= Date: Tue, 10 Jun 2014 18:04:52 +0000 Subject: [PATCH 07/16] apply WP coding standard, as tested by phpcs --- freifunkmeta.php | 648 +++++++++++++++++++++++------------------------ 1 file changed, 324 insertions(+), 324 deletions(-) diff --git a/freifunkmeta.php b/freifunkmeta.php index 4eac73a..d064b74 100644 --- a/freifunkmeta.php +++ b/freifunkmeta.php @@ -3,34 +3,34 @@ Plugin Name: Freifunk Metadata Shortcodes Plugin URI: http://mschuette.name/ Description: Defines shortcodes to display Freifunk metadata -Version: 0.3 +Version: 0.4dev Author: Martin Schuette Author URI: http://mschuette.name/ */ -define('FF_META_DEFAULT_CACHETIME', 15); -define('FF_META_DEFAULT_DIR', 'https://raw.githubusercontent.com/freifunk/directory.api.freifunk.net/master/directory.json'); -define('FF_META_DEFAULT_CITY', 'hamburg'); +define( 'FF_META_DEFAULT_CACHETIME', 15 ); +define( 'FF_META_DEFAULT_DIR', 'https://raw.githubusercontent.com/freifunk/directory.api.freifunk.net/master/directory.json' ); +define( 'FF_META_DEFAULT_CITY', 'hamburg' ); /** * class to fetch and cache data from external URLs */ class FF_Meta_Externaldata { - public function get($url) { - /* gets metadata from URL, handles caching */ - $cachekey = 'ff_metadata_'.hash('crc32', $url); - $cachetime = get_option('ff_meta_cachetime', FF_META_DEFAULT_CACHETIME) * MINUTE_IN_SECONDS; + public function get( $url ) { + /* gets metadata from URL, handles caching */ + $cachekey = 'ff_metadata_'.hash( 'crc32', $url ); + $cachetime = get_option( 'ff_meta_cachetime', FF_META_DEFAULT_CACHETIME ) * MINUTE_IN_SECONDS; - // Caching - if ( 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 ); - } - return $data; - } + // Caching + if ( 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 ); + } + return $data; + } } /** @@ -38,22 +38,22 @@ class FF_Meta_Externaldata */ class FF_Directory { - private $directory; + private $directory; - function __construct() { - $ed = new FF_Meta_Externaldata(); - $this->directory = $ed->get(FF_META_DEFAULT_DIR); - } + function __construct() { + $ed = new FF_Meta_Externaldata(); + $this->directory = $ed->get( FF_META_DEFAULT_DIR ); + } - function get_url_by_city($city) { - $val = $this->directory[$city]; + function get_url_by_city( $city ) { + $val = $this->directory[$city]; - if (empty($val)) { - return false; - } else { - return $val; - } - } + if ( empty( $val ) ) { + return false; + } else { + return $val; + } + } } /** @@ -61,49 +61,49 @@ class FF_Directory */ class FF_Community { - public $name; - public $street; - public $zip; - public $city; - public $lon; - public $lat; + public $name; + public $street; + public $zip; + public $city; + public $lon; + public $lat; - /** - * Default constructor from metadata - */ - 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->city = isset($loc['city']) ? $loc['city'] : ''; - $this->lon = isset($loc['lon']) ? $loc['lon'] : ''; - $this->lat = isset($loc['lat']) ? $loc['lat'] : ''; - } + /** + * Default constructor from metadata + */ + 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->city = isset( $loc['city'] ) ? $loc['city'] : ''; + $this->lon = isset( $loc['lon'] ) ? $loc['lon'] : ''; + $this->lat = isset( $loc['lat'] ) ? $loc['lat'] : ''; + } - /** - * 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"; - } - if (false === ($metadata = FF_Meta_Externaldata::get($url))) { - return "\n"; - } - return new FF_Community($metadata); - } + /** + * 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"; + } + if ( false === ( $metadata = FF_Meta_Externaldata::get( $url ) ) ) { + return "\n"; + } + return new FF_Community( $metadata ); + } - function format_address() { - if (empty($this->name) || empty($this->street) || empty($this->zip)) { - 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: handle missing values (i.e. only name & city) - return '

' . sprintf('%s
%s
%s %s', $this->name, $this->street, $this->zip, $this->city) . '

'; - } + function format_address() { + if ( empty( $this->name ) || empty( $this->street ) || empty( $this->zip ) ) { + 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: handle missing values ( i.e. only name & city ) + return '

' . sprintf( '%s
%s
%s %s', $this->name, $this->street, $this->zip, $this->city ) . '

'; + } } /** @@ -111,297 +111,297 @@ class FF_Community */ class FF_Meta { - private $dir; + private $dir; - function __construct() { - $this->dir = new FF_Directory(); - } + function __construct() { + $this->dir = new FF_Directory(); + } - function register_stuff() { - if ( ! shortcode_exists('ff_state') ) { - add_shortcode('ff_state', array($this, 'shortcode_handler')); - } - if ( ! shortcode_exists('ff_services') ) { - add_shortcode('ff_services', array($this, 'shortcode_handler')); - } - if ( ! shortcode_exists('ff_contact') ) { - add_shortcode('ff_contact', array($this, 'shortcode_handler')); - } - if ( ! shortcode_exists('ff_location') ) { - add_shortcode('ff_location', array($this, 'shortcode_handler')); - } + function register_stuff() { + if ( ! shortcode_exists( 'ff_state' ) ) { + add_shortcode( 'ff_state', array( $this, 'shortcode_handler' ) ); + } + if ( ! shortcode_exists( 'ff_services' ) ) { + add_shortcode( 'ff_services', array( $this, 'shortcode_handler' ) ); + } + if ( ! shortcode_exists( 'ff_contact' ) ) { + add_shortcode( 'ff_contact', array( $this, 'shortcode_handler' ) ); + } + if ( ! shortcode_exists( 'ff_location' ) ) { + add_shortcode( 'ff_location', array( $this, 'shortcode_handler' ) ); + } - add_action('admin_menu', array($this, 'admin_menu')); - add_action('admin_init', array($this, 'admin_init')); - register_uninstall_hook( __FILE__, array('ff_meta', 'uninstall_hook')); - } + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); + add_action( 'admin_init', array( $this, 'admin_init' ) ); + register_uninstall_hook( __FILE__, array( 'ff_meta', 'uninstall_hook' ) ); + } - function output_ff_state($citydata) { - $state = $citydata['state']; - return sprintf('%s', $state['nodes']); - } + function output_ff_state( $citydata ) { + $state = $citydata['state']; + return sprintf( '%s', $state['nodes'] ); + } - function aux_get_all_locations() { - // gather all location data - if (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); - if (!empty($tmp_meta['location'])) { - $tmp_loc = array_intersect_key($tmp_meta['location'], $arr_select); - $all_locs[$tmp_city] = $tmp_loc; - } - } catch (Exception $e) { - // pass - } - } - $json_locs = json_encode($all_locs); - $cachetime = get_option('FF_meta_cachetime', FF_META_DEFAULT_CACHETIME) * MINUTE_IN_SECONDS; - set_transient("FF_metadata_json_locs", $json_locs, $cachetime); - } - return $json_locs; - } + function aux_get_all_locations() { + // gather all location data + if ( 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 ); + if ( ! empty( $tmp_meta['location'] ) ) { + $tmp_loc = array_intersect_key( $tmp_meta['location'], $arr_select ); + $all_locs[$tmp_city] = $tmp_loc; + } + } catch ( Exception $e ) { + // pass + } + } + $json_locs = json_encode( $all_locs ); + $cachetime = get_option( 'FF_meta_cachetime', FF_META_DEFAULT_CACHETIME ) * MINUTE_IN_SECONDS; + set_transient( 'FF_metadata_json_locs', $json_locs, $cachetime ); + } + return $json_locs; + } - function output_ff_location($citydata) { - // normal per-city code - $loc = new FF_Community($citydata); + function output_ff_location( $citydata ) { + // normal per-city code + $loc = new FF_Community( $citydata ); + $outstr = $loc->format_address(); + $json_locs = $this->aux_get_all_locations(); - $outstr = $loc->format_address(); - $json_locs = $this->aux_get_all_locations(); - - if (!empty($loc_name) && !empty($loc_name)) { - $icon_url = plugin_dir_url(__FILE__) . "freifunk_marker.png"; - $outstr .= << EOT; - } - return $outstr; - } + } + return $outstr; + } - function output_ff_services($citydata) { - $outstr = '
    '; - if (isset($citydata['services'])) { - $services = $citydata['services']; - foreach ($services as $service) { - $outstr .= sprintf('
  • %s (%s): %s
  • ', $service['serviceName'], $service['serviceDescription'], $service['internalUri'], $service['internalUri']); - } - } - $outstr .= '
'; - return $outstr; - } + function output_ff_services( $citydata ) { + $outstr = '
    '; + if ( isset( $citydata['services'] ) ) { + $services = $citydata['services']; + foreach ( $services as $service ) { + $outstr .= sprintf( '
  • %s (%s ): %s
  • ', $service['serviceName'], $service['serviceDescription'], $service['internalUri'], $service['internalUri'] ); + } + } + $outstr .= '
'; + return $outstr; + } - function output_ff_contact($citydata) { - $outstr = '

'; - $contact = $citydata['contact']; - // Output -- rather ugly but the data is not uniform, some fields are URIs, some are usernames, ... - if (!empty($contact['email'])) { - $outstr .= sprintf("E-Mail: %s
\n", $contact['email'], $contact['email']); - } - if (!empty($contact['ml'])) { - $outstr .= sprintf("Mailingliste: %s
\n", $contact['ml'], $contact['ml']); - } - if (!empty($contact['irc'])) { - $outstr .= sprintf("IRC: %s
\n", $contact['irc'], $contact['irc']); - } - if (!empty($contact['twitter'])) { - // catch username instead of URI - if ($contact['twitter'][0] === "@") { - $twitter_url = 'http://twitter.com/' . ltrim($contact['twitter'], "@"); - $twitter_handle = $contact['twitter']; - } else { - $twitter_url = $contact['twitter']; - $twitter_handle = '@' . substr($contact['twitter'], strrpos($contact['twitter'], '/') + 1); - } - $outstr .= sprintf("Twitter: %s
\n", $twitter_url, $twitter_handle); - } - if (!empty($contact['facebook'])) { - $outstr .= sprintf("Facebook: %s
\n", $contact['facebook'], $contact['facebook']); - } - if (!empty($contact['googleplus'])) { - $outstr .= sprintf("G+: %s
\n", $contact['googleplus'], $contact['googleplus']); - } - if (!empty($contact['jabber'])) { - $outstr .= sprintf("XMPP: %s
\n", $contact['jabber'], $contact['jabber']); - } - $outstr .= '

'; - return $outstr; - } + function output_ff_contact( $citydata ) { + $outstr = '

'; + $contact = $citydata['contact']; + // Output -- rather ugly but the data is not uniform, some fields are URIs, some are usernames, ... + if ( ! empty( $contact['email'] ) ) { + $outstr .= sprintf( "E-Mail: %s
\n", $contact['email'], $contact['email'] ); + } + if ( ! empty( $contact['ml'] ) ) { + $outstr .= sprintf( "Mailingliste: %s
\n", $contact['ml'], $contact['ml'] ); + } + if ( ! empty( $contact['irc'] ) ) { + $outstr .= sprintf( "IRC: %s
\n", $contact['irc'], $contact['irc'] ); + } + if ( ! empty( $contact['twitter'] ) ) { + // catch username instead of URI + if ( $contact['twitter'][0] === '@' ) { + $twitter_url = 'http://twitter.com/' . ltrim( $contact['twitter'], '@' ); + $twitter_handle = $contact['twitter']; + } else { + $twitter_url = $contact['twitter']; + $twitter_handle = '@' . substr( $contact['twitter'], strrpos( $contact['twitter'], '/' ) + 1 ); + } + $outstr .= sprintf( "Twitter: %s
\n", $twitter_url, $twitter_handle ); + } + if ( ! empty( $contact['facebook'] ) ) { + $outstr .= sprintf( "Facebook: %s
\n", $contact['facebook'], $contact['facebook'] ); + } + if ( ! empty( $contact['googleplus'] ) ) { + $outstr .= sprintf( "G+: %s
\n", $contact['googleplus'], $contact['googleplus'] ); + } + if ( ! empty( $contact['jabber'] ) ) { + $outstr .= sprintf( "XMPP: %s
\n", $contact['jabber'], $contact['jabber'] ); + } + $outstr .= '

'; + return $outstr; + } - function output_ff_list() { - return 'here be some ff_list'; - } - - function shortcode_handler($atts, $content, $shortcode) { - // $atts[0] holds the city name, if given - if (empty($atts[0])) { - $city = get_option('FF_meta_city', FF_META_DEFAULT_CITY); - } else { - $city = $atts[0]; - } + function output_ff_list() { + return 'here be some ff_list'; + } + + function shortcode_handler( $atts, $content, $shortcode ) { + // $atts[0] holds the city name, if given + if ( empty( $atts[0] ) ) { + $city = get_option( 'FF_meta_city', FF_META_DEFAULT_CITY ); + } else { + $city = $atts[0]; + } - if (false === ($cityurl = $this->dir->get_url_by_city($city))) { - return "\n"; - } + if ( false === ( $cityurl = $this->dir->get_url_by_city( $city ) ) ) { + return "\n"; + } - if (false === ($metadata = FF_Meta_Externaldata::get($cityurl))) { - return "\n"; - } + if ( false === ( $metadata = FF_Meta_Externaldata::get( $cityurl ) ) ) { + return "\n"; + } - $outstr = "
"; - switch ($shortcode) { - case 'ff_state': - $outstr .= $this->output_ff_state($metadata); - break; - case 'ff_location': - $outstr .= $this->output_ff_location($metadata); - break; - case 'ff_services': - $outstr .= $this->output_ff_services($metadata); - break; - case 'ff_contact': - $outstr .= $this->output_ff_contact($metadata); - break; - case 'ff_list': - $outstr .= $this->output_ff_list(); - break; - default: - $outstr .= ""; - break; - } - $outstr .= "
"; - return $outstr; - } + $outstr = "
"; + switch ( $shortcode ) { + case 'ff_state': + $outstr .= $this->output_ff_state( $metadata ); + break; + case 'ff_location': + $outstr .= $this->output_ff_location( $metadata ); + break; + case 'ff_services': + $outstr .= $this->output_ff_services( $metadata ); + break; + case 'ff_contact': + $outstr .= $this->output_ff_contact( $metadata ); + break; + case 'ff_list': + $outstr .= $this->output_ff_list(); + break; + default: + $outstr .= ''; + break; + } + $outstr .= '
'; + return $outstr; + } - function admin_menu() { - // Options Page: - add_options_page( - 'FF Meta Plugin', // page title - 'FF Meta', // menu title - 'manage_options', // req'd capability - 'ff_meta_plugin', // menu slug - array ('FF_meta', 'options_page') // callback function - ); - } + function admin_menu() { + // Options Page: + add_options_page( + 'FF Meta Plugin', // page title + 'FF Meta', // menu title + 'manage_options', // req'd capability + 'ff_meta_plugin', // menu slug + array( 'FF_meta', 'options_page' ) // callback function + ); + } - function admin_init() { - register_setting( - 'ff_meta_settings-group', // group name - 'ff_meta_cachetime' // option name - ); - register_setting( - 'ff_meta_settings-group', // group name - 'ff_meta_city' // option name - ); - add_settings_section( - 'ff_meta_section-one', // ID - 'Section One', // Title - array ('FF_Meta', 'section_one_callback'), // callback to fill - 'ff_meta_plugin' // page to display on - ); - add_settings_field( - 'ff_meta_city', // ID - 'Default community', // Title - array ('FF_Meta', 'city_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 - array('label_for' => 'ff_meta_city_id') // ID of input element - ); - add_settings_field( - 'ff_meta_cachetime', // ID - 'Cache time', // Title - array ('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 - array('label_for' => 'ff_meta_cachetime_id') // ID of input element - ); - } + function admin_init() { + register_setting( + 'ff_meta_settings-group', // group name + 'ff_meta_cachetime' // option name + ); + register_setting( + 'ff_meta_settings-group', // group name + 'ff_meta_city' // option name + ); + add_settings_section( + 'ff_meta_section-one', // ID + 'Section One', // Title + array( 'FF_Meta', 'section_one_callback' ), // callback to fill + 'ff_meta_plugin' // page to display on + ); + add_settings_field( + 'ff_meta_city', // ID + 'Default community', // Title + array( 'FF_Meta', 'city_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 + array( 'label_for' => 'ff_meta_city_id' ) // ID of input element + ); + add_settings_field( + 'ff_meta_cachetime', // ID + 'Cache time', // Title + array( '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 + array( 'label_for' => 'ff_meta_cachetime_id' ) // ID of input element + ); + } - function section_one_callback() { - echo 'This Plugin provides shortcodes to display information from the Freifunk meta.json.'; - } + function section_one_callback() { + echo 'This Plugin provides shortcodes to display information from the Freifunk meta.json.'; + } - function cachetime_callback() { - $time = get_option( 'ff_meta_cachetime', FF_META_DEFAULT_CACHETIME ); - echo " minutes" - ."

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

"; - } + function cachetime_callback() { + $time = get_option( 'ff_meta_cachetime', FF_META_DEFAULT_CACHETIME ); + echo " minutes'. + "

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

"; + } - function city_callback() { - if (false === ($directory = FF_Meta_Externaldata::get ( FF_META_DEFAULT_DIR ))) { - // TODO: error handling - return; - } - $default_city = get_option( 'ff_meta_city', FF_META_DEFAULT_CITY ); + function city_callback() { + if ( false === ( $directory = FF_Meta_Externaldata::get( FF_META_DEFAULT_DIR ) ) ) { + // TODO: error handling + return; + } + $default_city = get_option( 'ff_meta_city', FF_META_DEFAULT_CITY ); - echo ""; - echo "

This is the default city parameter.

"; - } + echo "'; + echo "

This is the default city parameter.

"; + } - function options_page() { - ?> -
-

Freifunk Meta Plugin Options

-
- - - -
-
- +
+

Freifunk Meta Plugin Options

+
+ + + +
+
+ Date: Mon, 16 Jun 2014 14:13:12 +0000 Subject: [PATCH 08/16] some more code formatting, also add FF_Meta_Externaldata as service to FF_Dir and FF_Meta --- freifunkmeta.php | 270 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 183 insertions(+), 87 deletions(-) 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 .= << +