From 56e67a174524a9fb4fc1ed0de63a643ffb9521cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sch=C3=BCtte?= Date: Mon, 16 Jun 2014 18:59:51 +0200 Subject: [PATCH] add a simple "ff_list" shortcode, refactor get_all_locations() --- freifunkmeta.php | 87 ++++++++++++++++++++----------- tests/test-LowLevelTests.php | 6 --- tests/test-WpIntegrationTests.php | 14 +++++ 3 files changed, 72 insertions(+), 35 deletions(-) diff --git a/freifunkmeta.php b/freifunkmeta.php index c3cb3de..8818cb7 100644 --- a/freifunkmeta.php +++ b/freifunkmeta.php @@ -29,7 +29,8 @@ class FF_Meta_Externaldata // Caching if ( WP_DEBUG || ( false === ( $data = get_transient( $cachekey ) ) ) ) { - $http_response = wp_remote_get( $url ); + $args = array( 'sslverify' => false ); + $http_response = wp_remote_get( $url, $args ); if ( is_wp_error( $http_response ) ) { $error_msg = sprintf( 'Unable to retrieve URL %s, error: %s', @@ -53,14 +54,15 @@ class FF_Meta_Externaldata class FF_Directory { private $directory; + private $ed; function __construct( $ext_data_service = null ) { if ( is_null( $ext_data_service ) ) { - $ed = new FF_Meta_Externaldata(); + $this->ed = new FF_Meta_Externaldata(); } else { - $ed = $ext_data_service; + $this->ed = $ext_data_service; } - $data = $ed->get( FF_META_DEFAULT_DIR ); + $data = $this->ed->get( FF_META_DEFAULT_DIR ); if ( is_wp_error( $data ) ) { $this->directory = array(); } else { @@ -75,6 +77,18 @@ class FF_Directory return false; } } + + // get one big array of all known community data + function get_all_data() { + $all_locs = array(); + foreach ( $this->directory as $tmp_city => $url ) { + $tmp_meta = $this->ed->get( $url ); + if ( ! is_wp_error( $tmp_meta ) ) { + $all_locs[$tmp_city] = $tmp_meta; + } + } + return $all_locs; + } } /** @@ -180,12 +194,37 @@ class FF_Meta if ( ! shortcode_exists( 'ff_location' ) ) { add_shortcode( 'ff_location', array( $this, 'shortcode_handler' ) ); } + if ( ! shortcode_exists( 'ff_list' ) ) { + add_shortcode( 'ff_list', 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' ) ); } + private function aux_get_all_locations_json() { + if ( WP_DEBUG || ( false === ( $json_locs = get_transient( 'FF_metadata_json_locs' ) ) ) ) { + $all_locs = array(); + $comm_list = $this->dir->get_all_data(); + foreach ( $comm_list as $entry ) { + if ( isset( $entry['location'] ) + && isset( $entry['location']['lat'] ) + && isset( $entry['location']['lon'] ) + ) { + $all_locs[$entry['location']['city']] = array( + 'lat' => $entry['location']['lat'], + 'lon' => $entry['location']['lon'], + ); + } + } + $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_state( $citydata ) { if ( isset( $citydata['state'] ) && isset( $citydata['state']['nodes'] ) ) { return sprintf( '%s', $citydata['state']['nodes'] ); @@ -194,34 +233,11 @@ class FF_Meta } } - function aux_get_all_locations() { - // gather all location data - 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 = $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; - } - } 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 ); $outstr = $loc->format_address(); - $json_locs = $this->aux_get_all_locations(); + $json_locs = $this->aux_get_all_locations_json(); if ( ! empty( $loc->name ) && ! empty( $loc->name ) ) { $icon_url = plugin_dir_url( __FILE__ ) . 'freifunk_marker.png'; @@ -355,7 +371,20 @@ EOT; } function output_ff_list() { - return 'here be some ff_list'; + $comm_list = $this->dir->get_all_data(); + $outstr = ''; + $outstr .= ''; + foreach ( $comm_list as $handle => $entry ) { + $outstr .= sprintf( + '', + esc_url( $entry['url'] ), + isset( $entry['name'] ) ? esc_html( $entry['name'] ) : esc_html($handle), + isset( $entry['location']['city'] ) ? esc_html( $entry['location']['city'] ) : 'n/a', + isset( $entry['state']['nodes'] ) ? esc_html( $entry['state']['nodes'] ) : 'n/a' + ); + } + $outstr .= '
NameStadtKnoten
%s%s%s
'; + return $outstr; } function shortcode_handler( $atts, $content, $shortcode ) { diff --git a/tests/test-LowLevelTests.php b/tests/test-LowLevelTests.php index eb5cdf7..4651be0 100644 --- a/tests/test-LowLevelTests.php +++ b/tests/test-LowLevelTests.php @@ -171,10 +171,4 @@ class LowLevelTests extends PHPUnit_Framework_TestCase { $this->assertRegExp('/irc\.hackint\.net\/example/', $ret); $this->assertRegExp('/Facebook:/', $ret); } - // function test_aux_get_all_locations() { - // $this->markTestIncomplete( - // 'This test has not been implemented yet.' - // ); - // } - } diff --git a/tests/test-WpIntegrationTests.php b/tests/test-WpIntegrationTests.php index 125fc7c..d41dc20 100644 --- a/tests/test-WpIntegrationTests.php +++ b/tests/test-WpIntegrationTests.php @@ -53,4 +53,18 @@ class WpIntegrationTests extends WP_UnitTestCase { $output = apply_filters( 'the_content', $post->post_content ); $this->assertRegExp('/radio\.ffhh/', $output); } + + function test_post_ff_list() { + $post_content = '[ff_list]'; + $post_attribs = array( 'post_title' => 'Test', 'post_content' => $post_content ); + $post = $this->factory->post->create_and_get( $post_attribs ); + + // w/o filter: + $this->assertEquals($post_content, $post->post_content); + + // with filter: + $output = apply_filters( 'the_content', $post->post_content ); + $this->assertRegExp('/Hamburg/', $output); + $this->assertRegExp('/Frankfurt/', $output); + } } \ No newline at end of file