From 24b893f44b4a263b4387df68c4ad55b13bb67d6b Mon Sep 17 00:00:00 2001 From: Martin Schuette Date: Thu, 3 Apr 2014 21:25:58 +0000 Subject: [PATCH] use single shortcode_handler and catch missing default URL --- freifunkmeta.php | 156 +++++++++++++++++++++-------------------------- 1 file changed, 71 insertions(+), 85 deletions(-) diff --git a/freifunkmeta.php b/freifunkmeta.php index 566fec8..0be2974 100644 --- a/freifunkmeta.php +++ b/freifunkmeta.php @@ -22,105 +22,91 @@ function ff_meta_getmetadata ($url) { } if ( ! shortcode_exists( 'ff_state' ) ) { - add_shortcode( 'ff_state', 'ff_meta_shortcode_state'); + add_shortcode( 'ff_state', 'ff_meta_shortcode_handler'); +} +if ( ! shortcode_exists( 'ff_services' ) ) { + add_shortcode( 'ff_services', 'ff_meta_shortcode_handler'); +} +if ( ! shortcode_exists( 'ff_contact' ) ) { + add_shortcode( 'ff_contact', 'ff_meta_shortcode_handler'); } // Example: // [ff_state] // [ff_state url="http://meta.hamburg.freifunk.net/ffhh.json"] -function ff_meta_shortcode_state( $atts ) { +function ff_meta_shortcode_handler( $atts, $content, $name ) { $default_url = get_option( 'ff_meta_url' ); extract(shortcode_atts( array( 'url' => $default_url, ), $atts)); - $metadata = ff_meta_getmetadata ($url); - $state = $metadata['state']; + if (empty($url) || false === ($metadata = ff_meta_getmetadata ($url))) { + return ''; + } + + $outstr = "
"; + switch ($name) { + case 'ff_state': + $state = $metadata['state']; + $outstr .= sprintf('%s', $state['nodes']); + break; + + case 'ff_services': + $services = $metadata['services']; + $outstr .= ''; + break; + + case 'ff_contact': + $outstr .= '

'; + $contact = $metadata['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 .= '

'; + break; + + default: + return ""; + break; + } // Output - $outstr = sprintf('%s', - $state['nodes']); + $outstr .= "
"; return $outstr; } -if ( ! shortcode_exists( 'ff_services' ) ) { - add_shortcode( 'ff_services', 'ff_meta_shortcode_services'); -} -// Example: -// [ff_services] -// [ff_services url="http://meta.hamburg.freifunk.net/ffhh.json"] -function ff_meta_shortcode_services( $atts ) { - $default_url = get_option( 'ff_meta_url' ); - extract(shortcode_atts( array( - 'url' => $default_url, - ), $atts)); - - $metadata = ff_meta_getmetadata ($url); - $services = $metadata['services']; - - // Output - $outstr = '
'; - return $outstr; -} - -if ( ! shortcode_exists( 'ff_contact' ) ) { - add_shortcode( 'ff_contact', 'ff_meta_shortcode_contact'); -} -// Example: -// [ff_contact] -// [ff_contact url="http://meta.hamburg.freifunk.net/ffhh.json"] -function ff_meta_shortcode_contact( $atts ) { - $default_url = get_option( 'ff_meta_url' ); - extract(shortcode_atts( array( - 'url' => $default_url, - ), $atts)); - - $metadata = ff_meta_getmetadata ($url); - $contact = $metadata['contact']; - - // Output -- rather ugly but the data is not uniform, some fields are URIs, some are usernames, ... - $outstr = '

'; - 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']); - } - # maybe we do not want public phone numbers... - #if (!empty($contact['phone'])) { - # $outstr .= sprintf("Telephon: %s
\n", $contact['phone']); - #} - $outstr .= '

'; - return $outstr; -} // Options Page: add_action( 'admin_menu', 'ff_meta_admin_menu' );