2014-04-02 20:34:16 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
Plugin Name: Freifunk Metadata Shortcodes
|
|
|
|
Plugin URI: http://mschuette.name/
|
|
|
|
Description: Defines shortcodes to display Freifunk metadata
|
|
|
|
Version: 0.1
|
|
|
|
Author: Martin Schuette
|
|
|
|
Author URI: http://mschuette.name/
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* gets metadata from URL, handles caching */
|
|
|
|
function ff_meta_getmetadata ($url) {
|
|
|
|
$url_hash = hash('crc32', $url);
|
|
|
|
|
|
|
|
// Caching
|
|
|
|
if ( false === ( $metajson = get_transient( "ff_metadata_${url_hash}" ) ) ) {
|
2014-04-03 23:57:07 +02:00
|
|
|
$metajson = wp_remote_retrieve_body( wp_remote_get($url) );
|
|
|
|
$cachetime = get_option( 'ff_meta_cachetime' ) * MINUTE_IN_SECONDS;
|
|
|
|
set_transient( "ff_metadata_${url_hash}", $metajson, $cachetime );
|
2014-04-02 20:34:16 +02:00
|
|
|
}
|
|
|
|
$metadata = json_decode ( $metajson, $assoc = true );
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:51:56 +02:00
|
|
|
if ( ! shortcode_exists( 'ff_state' ) ) {
|
2014-04-03 23:25:58 +02:00
|
|
|
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');
|
2014-04-02 22:51:56 +02:00
|
|
|
}
|
|
|
|
// Example:
|
|
|
|
// [ff_state]
|
|
|
|
// [ff_state url="http://meta.hamburg.freifunk.net/ffhh.json"]
|
2014-04-03 23:25:58 +02:00
|
|
|
function ff_meta_shortcode_handler( $atts, $content, $name ) {
|
2014-04-02 22:51:56 +02:00
|
|
|
$default_url = get_option( 'ff_meta_url' );
|
|
|
|
extract(shortcode_atts( array(
|
|
|
|
'url' => $default_url,
|
|
|
|
), $atts));
|
|
|
|
|
2014-04-03 23:25:58 +02:00
|
|
|
if (empty($url) || false === ($metadata = ff_meta_getmetadata ($url))) {
|
|
|
|
return '';
|
|
|
|
}
|
2014-04-02 22:51:56 +02:00
|
|
|
|
2014-04-03 23:25:58 +02:00
|
|
|
$outstr = "<div class=\"ff $name\">";
|
|
|
|
switch ($name) {
|
|
|
|
case 'ff_state':
|
|
|
|
$state = $metadata['state'];
|
|
|
|
$outstr .= sprintf('%s', $state['nodes']);
|
|
|
|
break;
|
2014-04-02 22:51:56 +02:00
|
|
|
|
2014-04-03 23:25:58 +02:00
|
|
|
case 'ff_services':
|
|
|
|
$services = $metadata['services'];
|
|
|
|
$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>';
|
|
|
|
break;
|
2014-04-02 20:34:16 +02:00
|
|
|
|
2014-04-03 23:25:58 +02:00
|
|
|
case 'ff_contact':
|
|
|
|
$outstr .= '<p>';
|
|
|
|
$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: <a href=\"mailto:%s\">%s</a><br />\n", $contact['email'], $contact['email']);
|
|
|
|
}
|
|
|
|
if (!empty($contact['ml'])) {
|
|
|
|
$outstr .= sprintf("Mailingliste: <a href=\"mailto:%s\">%s</a><br />\n", $contact['ml'], $contact['ml']);
|
|
|
|
}
|
|
|
|
if (!empty($contact['irc'])) {
|
|
|
|
$outstr .= sprintf("IRC: <a href=\"%s\">%s</a><br />\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: <a href=\"%s\">%s</a><br />\n", $twitter_url, $twitter_handle);
|
|
|
|
}
|
|
|
|
if (!empty($contact['facebook'])) {
|
|
|
|
$outstr .= sprintf("Facebook: <a href=\"%s\">%s</a><br />\n", $contact['facebook'], $contact['facebook']);
|
|
|
|
}
|
|
|
|
if (!empty($contact['googleplus'])) {
|
|
|
|
$outstr .= sprintf("G+: <a href=\"%s\">%s</a><br />\n", $contact['googleplus'], $contact['googleplus']);
|
|
|
|
}
|
|
|
|
if (!empty($contact['jabber'])) {
|
|
|
|
$outstr .= sprintf("XMPP: <a href=\"xmpp:%s\">%s</a><br />\n", $contact['jabber'], $contact['jabber']);
|
|
|
|
}
|
|
|
|
$outstr .= '</p>';
|
|
|
|
break;
|
2014-04-02 20:34:16 +02:00
|
|
|
|
2014-04-03 23:25:58 +02:00
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
break;
|
2014-04-02 20:34:16 +02:00
|
|
|
}
|
|
|
|
|
2014-04-03 23:25:58 +02:00
|
|
|
// Output
|
|
|
|
$outstr .= "</div>";
|
2014-04-02 20:34:16 +02:00
|
|
|
return $outstr;
|
|
|
|
}
|
|
|
|
|
2014-04-03 23:25:58 +02:00
|
|
|
|
2014-04-02 20:34:16 +02:00
|
|
|
// Options Page:
|
|
|
|
add_action( 'admin_menu', 'ff_meta_admin_menu' );
|
|
|
|
function ff_meta_admin_menu() {
|
|
|
|
add_options_page(
|
|
|
|
'FF Meta Plugin', // page title
|
|
|
|
'FF Meta', // menu title
|
|
|
|
'manage_options', // req'd capability
|
|
|
|
'ff_meta_plugin', // menu slug
|
|
|
|
'ff_meta_options_page' // callback function
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_action( 'admin_init', 'ff_meta_admin_init' );
|
|
|
|
function ff_meta_admin_init() {
|
|
|
|
register_setting(
|
|
|
|
'ff_meta_settings-group', // group name
|
|
|
|
'ff_meta_url' // option name
|
|
|
|
);
|
|
|
|
add_settings_section(
|
|
|
|
'ff_meta_section-one', // ID
|
|
|
|
'Section One', // Title
|
|
|
|
'ff_meta_section_one_callback', // callback to fill
|
|
|
|
'ff_meta_plugin' // page to display on
|
|
|
|
);
|
2014-04-03 23:37:51 +02:00
|
|
|
add_settings_field(
|
2014-04-03 23:57:07 +02:00
|
|
|
'ff_meta_cachetime', // ID
|
|
|
|
'Cache time', // Title
|
|
|
|
'ff_meta_cachetime_callback', // callback to fill field
|
2014-04-03 23:37:51 +02:00
|
|
|
'ff_meta_plugin', // menu page=slug to display field on
|
|
|
|
'ff_meta_section-one' // section to display the field in
|
|
|
|
);
|
2014-04-02 20:34:16 +02:00
|
|
|
add_settings_field(
|
|
|
|
'ff_meta_url', // ID
|
|
|
|
'URL of meta.json', // Title
|
|
|
|
'ff_meta_url_callback', // callback to fill field
|
|
|
|
'ff_meta_plugin', // menu page=slug to display field on
|
|
|
|
'ff_meta_section-one' // section to display the field in
|
|
|
|
);
|
2014-04-03 23:57:07 +02:00
|
|
|
add_settings_field(
|
|
|
|
'ff_meta_dir', // ID
|
|
|
|
'URL of directory.json', // Title
|
|
|
|
'ff_meta_dir_callback', // callback to fill field
|
|
|
|
'ff_meta_plugin', // menu page=slug to display field on
|
|
|
|
'ff_meta_section-one' // section to display the field in
|
|
|
|
);
|
2014-04-02 20:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function ff_meta_section_one_callback() {
|
|
|
|
echo 'This Plugin provides shortcodes to display information from the Freifunk meta.json.';
|
|
|
|
}
|
|
|
|
|
2014-04-03 23:57:07 +02:00
|
|
|
function ff_meta_cachetime_callback() {
|
|
|
|
$time = get_option( 'ff_meta_cachetime' );
|
|
|
|
if (empty($time)) {
|
|
|
|
$time = "15";
|
|
|
|
}
|
|
|
|
echo "<input type='number' name='ff_meta_cachetime' class='small-text code' value='$time' /> minutes"
|
|
|
|
."<p class='description'>Data from external URLs is cached for this number of minutes.</p>";
|
|
|
|
}
|
|
|
|
|
2014-04-03 23:37:51 +02:00
|
|
|
function ff_meta_dir_callback() {
|
|
|
|
$url = get_option( 'ff_meta_dir' );
|
|
|
|
if (empty($url)) {
|
|
|
|
$url = "https://raw.githubusercontent.com/freifunk/directory.api.freifunk.net/master/directory.json";
|
|
|
|
}
|
2014-04-03 23:57:07 +02:00
|
|
|
echo "<input type='text' name='ff_meta_dir' class='large-text code' value='$url' />"
|
|
|
|
."<p class='description'>Please keep the default unless you really know what you are doing.</p>";
|
2014-04-03 23:37:51 +02:00
|
|
|
}
|
|
|
|
|
2014-04-02 20:34:16 +02:00
|
|
|
function ff_meta_url_callback() {
|
|
|
|
$url = get_option( 'ff_meta_url' );
|
2014-04-03 23:37:51 +02:00
|
|
|
if (empty($url)) {
|
|
|
|
$url = "http://meta.hamburg.freifunk.net/ffhh.json";
|
|
|
|
}
|
2014-04-03 23:57:07 +02:00
|
|
|
echo "<input type='text' name='ff_meta_url' class='large-text code' value='$url' />"
|
|
|
|
."<p class='description'>This will be the default for all tags without url=\"xyz\" parameter.</p>";
|
2014-04-02 20:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function ff_meta_options_page() {
|
|
|
|
?>
|
|
|
|
<div class="wrap">
|
|
|
|
<h2>Freifunk Meta Plugin Options</h2>
|
|
|
|
<form action="options.php" method="POST">
|
|
|
|
<?php settings_fields( 'ff_meta_settings-group' ); ?>
|
|
|
|
<?php do_settings_sections( 'ff_meta_plugin' ); ?>
|
|
|
|
<?php submit_button(); ?>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|