use single shortcode_handler and catch missing default URL

This commit is contained in:
Martin Schuette 2014-04-03 21:25:58 +00:00
parent 0e3aefd62a
commit 24b893f44b

View file

@ -22,69 +22,49 @@ function ff_meta_getmetadata ($url) {
} }
if ( ! shortcode_exists( 'ff_state' ) ) { 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: // Example:
// [ff_state] // [ff_state]
// [ff_state url="http://meta.hamburg.freifunk.net/ffhh.json"] // [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' ); $default_url = get_option( 'ff_meta_url' );
extract(shortcode_atts( array( extract(shortcode_atts( array(
'url' => $default_url, 'url' => $default_url,
), $atts)); ), $atts));
$metadata = ff_meta_getmetadata ($url); if (empty($url) || false === ($metadata = ff_meta_getmetadata ($url))) {
return '';
}
$outstr = "<div class=\"ff $name\">";
switch ($name) {
case 'ff_state':
$state = $metadata['state']; $state = $metadata['state'];
$outstr .= sprintf('%s', $state['nodes']);
break;
// Output case 'ff_services':
$outstr = sprintf('%s',
$state['nodes']);
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']; $services = $metadata['services'];
$outstr .= '<ul>';
// Output
$outstr = '<div class="ff ff_services"><ul>';
foreach ($services as $service) { foreach ($services as $service) {
$outstr .= sprintf('<li>%s (%s): <a href="%s">%s</a></li>', $outstr .= sprintf('<li>%s (%s): <a href="%s">%s</a></li>',
$service['serviceName'], $service['serviceDescription'], $service['serviceName'], $service['serviceDescription'],
$service['internalUri'], $service['internalUri']); $service['internalUri'], $service['internalUri']);
} }
$outstr .= '</ul></div>'; $outstr .= '</ul>';
return $outstr; break;
}
if ( ! shortcode_exists( 'ff_contact' ) ) { case 'ff_contact':
add_shortcode( 'ff_contact', 'ff_meta_shortcode_contact'); $outstr .= '<p>';
}
// 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']; $contact = $metadata['contact'];
// Output -- rather ugly but the data is not uniform, some fields are URIs, some are usernames, ... // Output -- rather ugly but the data is not uniform, some fields are URIs, some are usernames, ...
$outstr = '<div class="ff ff_contact"><p>';
if (!empty($contact['email'])) { if (!empty($contact['email'])) {
$outstr .= sprintf("E-Mail: <a href=\"mailto:%s\">%s</a><br />\n", $contact['email'], $contact['email']); $outstr .= sprintf("E-Mail: <a href=\"mailto:%s\">%s</a><br />\n", $contact['email'], $contact['email']);
} }
@ -114,14 +94,20 @@ function ff_meta_shortcode_contact( $atts ) {
if (!empty($contact['jabber'])) { if (!empty($contact['jabber'])) {
$outstr .= sprintf("XMPP: <a href=\"xmpp:%s\">%s</a><br />\n", $contact['jabber'], $contact['jabber']); $outstr .= sprintf("XMPP: <a href=\"xmpp:%s\">%s</a><br />\n", $contact['jabber'], $contact['jabber']);
} }
# maybe we do not want public phone numbers... $outstr .= '</p>';
#if (!empty($contact['phone'])) { break;
# $outstr .= sprintf("Telephon: %s<br />\n", $contact['phone']);
#} default:
$outstr .= '</p></div>'; return "";
break;
}
// Output
$outstr .= "</div>";
return $outstr; return $outstr;
} }
// Options Page: // Options Page:
add_action( 'admin_menu', 'ff_meta_admin_menu' ); add_action( 'admin_menu', 'ff_meta_admin_menu' );
function ff_meta_admin_menu() { function ff_meta_admin_menu() {