Skip to content

Commit

Permalink
add a simple "ff_list" shortcode, refactor get_all_locations()
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuett committed Jun 16, 2014
1 parent c967ec7 commit 56e67a1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
83 changes: 56 additions & 27 deletions freifunkmeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function get( $url ) {

// 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',
Expand All @@ -53,14 +54,15 @@ public function get( $url ) {
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 {
Expand All @@ -75,6 +77,18 @@ function get_url_by_city( $city ) {
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;
}
}

/**
Expand Down Expand Up @@ -180,34 +194,28 @@ function register_stuff() {
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' ) );
}

function output_ff_state( $citydata ) {
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
private function aux_get_all_locations_json() {
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
$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 );
Expand All @@ -217,11 +225,19 @@ function aux_get_all_locations() {
return $json_locs;
}

function output_ff_state( $citydata ) {
if ( isset( $citydata['state'] ) && isset( $citydata['state']['nodes'] ) ) {
return sprintf( '%s', $citydata['state']['nodes'] );
} else {
return '';
}
}

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';
Expand Down Expand Up @@ -355,7 +371,20 @@ function output_ff_contact( $citydata ) {
}

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 ) {
Expand Down
6 changes: 0 additions & 6 deletions tests/test-LowLevelTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,4 @@ function test_output_ff_contact_filled() {
$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.'
// );
// }

}
14 changes: 14 additions & 0 deletions tests/test-WpIntegrationTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,18 @@ function test_post_ff_services() {
$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);
}
}

0 comments on commit 56e67a1

Please sign in to comment.