remove user-visible URLs, only select city from directory

This commit is contained in:
Martin Schuette 2014-04-03 23:14:19 +00:00
parent 15005fb702
commit e15a919bfa

View file

@ -10,7 +10,7 @@ Author URI: http://mschuette.name/
define('FF_META_DEFAULT_CACHETIME', 15); define('FF_META_DEFAULT_CACHETIME', 15);
define('FF_META_DEFAULT_DIR', 'https://raw.githubusercontent.com/freifunk/directory.api.freifunk.net/master/directory.json'); define('FF_META_DEFAULT_DIR', 'https://raw.githubusercontent.com/freifunk/directory.api.freifunk.net/master/directory.json');
define('FF_META_DEFAULT_URL', 'http://meta.hamburg.freifunk.net/ffhh.json'); define('FF_META_DEFAULT_CITY', 'hamburg');
/* gets metadata from URL, handles caching */ /* gets metadata from URL, handles caching */
function ff_meta_getmetadata ($url) { function ff_meta_getmetadata ($url) {
@ -38,25 +38,22 @@ if ( ! shortcode_exists( 'ff_contact' ) ) {
// Example: // Example:
// [ff_state] // [ff_state]
// [ff_state hamburg] // [ff_state hamburg]
// [ff_state url="http://meta.hamburg.freifunk.net/ffhh.json"]
function ff_meta_shortcode_handler( $atts, $content, $name ) { function ff_meta_shortcode_handler( $atts, $content, $name ) {
$default_url = get_option( 'ff_meta_url', FF_META_DEFAULT_URL ); // $atts[0] holds the city name, if given
extract(shortcode_atts( array( if (empty($atts[0])) {
'url' => $default_url, $city = get_option( 'ff_meta_city', FF_META_DEFAULT_CITY );
), $atts)); } else {
// check for city name
if (!empty($atts[0])) {
$city = $atts[0]; $city = $atts[0];
}
if (false === ($directory = ff_meta_getmetadata ( FF_META_DEFAULT_DIR )) if (false === ($directory = ff_meta_getmetadata ( FF_META_DEFAULT_DIR ))
|| empty($directory[$city])) { || empty($directory[$city])) {
return ''; return "<!-- FF Meta Error: cannot get directory.json, or no URL for '$city' -->\n";
} }
$url = $directory[$city]; $url = $directory[$city];
}
if (empty($url) || false === ($metadata = ff_meta_getmetadata ($url))) { if (false === ($metadata = ff_meta_getmetadata ($url))) {
return ''; return "<!-- FF Meta Error: cannot get metadata from $url -->\n";
} }
$outstr = "<div class=\"ff $name\">"; $outstr = "<div class=\"ff $name\">";
@ -67,13 +64,15 @@ function ff_meta_shortcode_handler( $atts, $content, $name ) {
break; break;
case 'ff_services': case 'ff_services':
$services = $metadata['services'];
$outstr .= '<ul>'; $outstr .= '<ul>';
if (isset($metadata['services'])) {
$services = $metadata['services'];
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>'; $outstr .= '</ul>';
break; break;
@ -144,7 +143,7 @@ function ff_meta_admin_init() {
); );
register_setting( register_setting(
'ff_meta_settings-group', // group name 'ff_meta_settings-group', // group name
'ff_meta_url' // option name 'ff_meta_city' // option name
); );
add_settings_section( add_settings_section(
'ff_meta_section-one', // ID 'ff_meta_section-one', // ID
@ -152,6 +151,14 @@ function ff_meta_admin_init() {
'ff_meta_section_one_callback', // callback to fill 'ff_meta_section_one_callback', // callback to fill
'ff_meta_plugin' // page to display on 'ff_meta_plugin' // page to display on
); );
add_settings_field(
'ff_meta_city', // ID
'Default community', // Title
'ff_meta_city_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
array('label_for' => 'ff_meta_city_id') // ID of input element
);
add_settings_field( add_settings_field(
'ff_meta_cachetime', // ID 'ff_meta_cachetime', // ID
'Cache time', // Title 'Cache time', // Title
@ -160,14 +167,6 @@ function ff_meta_admin_init() {
'ff_meta_section-one', // section to display the field in 'ff_meta_section-one', // section to display the field in
array('label_for' => 'ff_meta_cachetime_id') // ID of input element array('label_for' => 'ff_meta_cachetime_id') // ID of input element
); );
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
array('label_for' => 'ff_meta_url_id') // ID of input element
);
} }
function ff_meta_section_one_callback() { function ff_meta_section_one_callback() {
@ -180,10 +179,21 @@ function ff_meta_cachetime_callback() {
."<p class='description'>Data from external URLs is cached for this number of minutes.</p>"; ."<p class='description'>Data from external URLs is cached for this number of minutes.</p>";
} }
function ff_meta_url_callback() { function ff_meta_city_callback() {
$url = get_option( 'ff_meta_url', FF_META_DEFAULT_URL ); if (false === ($directory = ff_meta_getmetadata ( FF_META_DEFAULT_DIR ))) {
echo "<input type='text' name='ff_meta_url' id='ff_meta_url_id' class='large-text code' value='$url' />" // TODO: error handling
."<p class='description'>This will be the default for all tags without url=\"xyz\" or city parameter.</p>"; return;
}
$default_city = get_option( 'ff_meta_city', FF_META_DEFAULT_CITY );
echo "<select name='ff_meta_city' id='ff_meta_city_id' size='1'>";
foreach (array_keys($directory) as $city) {
$prettycity = ucwords(str_replace(array('_', '-'), ' ', $city));
$selected = selected( $default_city, $city );
echo "<option value='$city' $selected>$prettycity</option>";
}
echo "</select>";
echo "<p class='description'>This is the default city parameter.</p>";
} }
function ff_meta_options_page() { function ff_meta_options_page() {