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' ) ) {
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);
if (empty($url) || false === ($metadata = ff_meta_getmetadata ($url))) {
return '';
}
$outstr = "<div class=\"ff $name\">";
switch ($name) {
case 'ff_state':
$state = $metadata['state'];
$outstr .= sprintf('%s', $state['nodes']);
break;
// Output
$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);
case 'ff_services':
$services = $metadata['services'];
// Output
$outstr = '<div class="ff ff_services"><ul>';
$outstr .= '<ul>';
foreach ($services as $service) {
$outstr .= sprintf('<li>%s (%s): <a href="%s">%s</a></li>',
$service['serviceName'], $service['serviceDescription'],
$service['internalUri'], $service['internalUri']);
}
$outstr .= '</ul></div>';
return $outstr;
}
$outstr .= '</ul>';
break;
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);
case 'ff_contact':
$outstr .= '<p>';
$contact = $metadata['contact'];
// 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'])) {
$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'])) {
$outstr .= sprintf("XMPP: <a href=\"xmpp:%s\">%s</a><br />\n", $contact['jabber'], $contact['jabber']);
}
# maybe we do not want public phone numbers...
#if (!empty($contact['phone'])) {
# $outstr .= sprintf("Telephon: %s<br />\n", $contact['phone']);
#}
$outstr .= '</p></div>';
$outstr .= '</p>';
break;
default:
return "";
break;
}
// Output
$outstr .= "</div>";
return $outstr;
}
// Options Page:
add_action( 'admin_menu', 'ff_meta_admin_menu' );
function ff_meta_admin_menu() {