add a simple "ff_list" shortcode, refactor get_all_locations()

This commit is contained in:
Martin Schütte 2014-06-16 18:59:51 +02:00
parent c967ec7188
commit 56e67a1745
3 changed files with 72 additions and 35 deletions

View file

@ -29,7 +29,8 @@ class FF_Meta_Externaldata
// Caching // Caching
if ( WP_DEBUG || ( false === ( $data = get_transient( $cachekey ) ) ) ) { 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 ) ) { if ( is_wp_error( $http_response ) ) {
$error_msg = sprintf( $error_msg = sprintf(
'Unable to retrieve URL %s, error: %s', 'Unable to retrieve URL %s, error: %s',
@ -53,14 +54,15 @@ class FF_Meta_Externaldata
class FF_Directory class FF_Directory
{ {
private $directory; private $directory;
private $ed;
function __construct( $ext_data_service = null ) { function __construct( $ext_data_service = null ) {
if ( is_null( $ext_data_service ) ) { if ( is_null( $ext_data_service ) ) {
$ed = new FF_Meta_Externaldata(); $this->ed = new FF_Meta_Externaldata();
} else { } 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 ) ) { if ( is_wp_error( $data ) ) {
$this->directory = array(); $this->directory = array();
} else { } else {
@ -75,6 +77,18 @@ class FF_Directory
return false; 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' ) ) { if ( ! shortcode_exists( 'ff_location' ) ) {
add_shortcode( 'ff_location', array( $this, 'shortcode_handler' ) ); 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_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) );
register_uninstall_hook( __FILE__, array( 'ff_meta', 'uninstall_hook' ) ); 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 ) { function output_ff_state( $citydata ) {
if ( isset( $citydata['state'] ) && isset( $citydata['state']['nodes'] ) ) { if ( isset( $citydata['state'] ) && isset( $citydata['state']['nodes'] ) ) {
return sprintf( '%s', $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 ) { function output_ff_location( $citydata ) {
// normal per-city code // normal per-city code
$loc = new FF_Community( $citydata ); $loc = new FF_Community( $citydata );
$outstr = $loc->format_address(); $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 ) ) { 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';
@ -355,7 +371,20 @@ EOT;
} }
function output_ff_list() { function output_ff_list() {
return 'here be some ff_list'; $comm_list = $this->dir->get_all_data();
$outstr = '<table>';
$outstr .= '<tr><th>Name</th><th>Stadt</th><th>Knoten</th></tr>';
foreach ( $comm_list as $handle => $entry ) {
$outstr .= sprintf(
'<tr><td><a href="%s">%s</a></td><td>%s</td><td>%s</td></tr>',
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 .= '</table>';
return $outstr;
} }
function shortcode_handler( $atts, $content, $shortcode ) { function shortcode_handler( $atts, $content, $shortcode ) {

View file

@ -171,10 +171,4 @@ class LowLevelTests extends PHPUnit_Framework_TestCase {
$this->assertRegExp('/irc\.hackint\.net\/example/', $ret); $this->assertRegExp('/irc\.hackint\.net\/example/', $ret);
$this->assertRegExp('/Facebook:/', $ret); $this->assertRegExp('/Facebook:/', $ret);
} }
// function test_aux_get_all_locations() {
// $this->markTestIncomplete(
// 'This test has not been implemented yet.'
// );
// }
} }

View file

@ -53,4 +53,18 @@ class WpIntegrationTests extends WP_UnitTestCase {
$output = apply_filters( 'the_content', $post->post_content ); $output = apply_filters( 'the_content', $post->post_content );
$this->assertRegExp('/radio\.ffhh/', $output); $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);
}
} }