rewrite finished using Settings API!

This commit is contained in:
Harald Milz 2019-03-28 14:38:28 +01:00
parent 153fb0981f
commit d5baabe079

View file

@ -10,6 +10,11 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*
*/
/*
* Quellen:
* https://codex.wordpress.org/Creating_Options_Pages
* http://ottopress.com/2009/wordpress-settings-api-tutorial/
*/
add_action('admin_menu', 'gcal_admin_add_page');
@ -19,8 +24,6 @@ function gcal_admin_add_page() {
function gcal_options_page() {
global $wpdb;
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
@ -44,33 +47,114 @@ add_action('admin_init', 'gcal_admin_init');
function gcal_admin_init(){
register_setting( 'gcal_options', 'gcal_options', 'gcal_options_validate' );
add_settings_section('gcal_main', 'Terminkategorien und ICS-Feeds', 'gcal_section_text', 'gcal');
add_settings_field('gcal_feeds_text_string', 'ICS Feeds', 'gcal_setting_string', 'gcal', 'gcal_main');
add_settings_section('gcal_feeds', 'Terminkategorien und ICS-Feeds', 'gcal_feeds_section_text', 'gcal');
// settings fields dynamisch pro Feed generieren, nur ein Callback mit Args nutzen.
$terms = get_terms( array(
'taxonomy' => 'termine_type',
'hide_empty' => false, )
);
foreach($terms as $term){
$unique_id = 'gcal_feed_' . $term->name;
$feed_name = 'ICS Feed für Terminkategorie ' . $term->name;
add_settings_field($unique_id, $feed_name, 'gcal_feeds_setting_string', 'gcal', 'gcal_feeds', array($unique_id));
}
add_settings_section('gcal_timer', 'Zeitintervall', 'gcal_timer_section_text', 'gcal');
add_settings_field('gcal_timer', 'Zeitintervall', 'gcal_timer_setting_string', 'gcal', 'gcal_timer');
add_settings_section('gcal_geocoding', 'Geocoding', 'gcal_geocoding_section_text', 'gcal');
add_settings_field('gcal_geocoding', 'Geocoding', 'gcal_geocoding_setting_string', 'gcal', 'gcal_geocoding');
}
function gcal_section_text() {
function gcal_feeds_section_text() {
?>
<p><b>Bitte hier die zu den Terminkategorien gehörigen Feeds eintragen (copy & paste!).</b></br>
<b>Wenn zu einer Kategorie kein Feed gehört, einfach leer lassen.</b></br>
<b>Feeds lassen sich jederzeit aktivieren oder deaktivieren, ohne andere Einstellungen zu ändern.</b></p>
<b>Wenn zu einer Terminkategorie kein Feed gehört, einfach leer lassen.</b></p>
<?php
}
function gcal_setting_string() {
function gcal_feeds_setting_string($args) {
$options = get_option('gcal_options');
echo "<input id='gcal_feeds_text_string' name='gcal_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
$placeholder = "z.B. https://calendar.google.com/calendar/ical/.../public/basic.ics";
// die id entspricht dem unique_id in add_settings_field.
// der name wird options.php als Name der zu setzenden Option übergeben
// der Value ist der inhalt von der $option[unique_id].
echo '<input type="text" id="' . $args[0] . '" name="gcal_options[' . $args[0] . ']" value="' . $options[$args[0]] . '" size="80" maxlength="256" placeholder="' . $placeholder . '" > </br>';
}
function gcal_timer_section_text() {
?>
<p><b>Zeitintervall in Minuten, in dem die Feeds synchronisiert werden sollen.</b></br>
<b>Neu setzen erfordert einen Neustart des Plugins (Deaktivieren / Aktivieren).</b></p>
<?php
}
function gcal_timer_setting_string() {
$options = get_option('gcal_options');
$placeholder = "default 60";
echo '<input type="text" id="gcal_timer" name="gcal_options[gcal_timer]" value="' . $options['gcal_timer'] . '" size="6" maxlength="6" placeholder="' . $placeholder . '" > Minuten </br>';
}
function gcal_geocoding_section_text() {
?>
<p><b>Um Termine auf der Karte zu sehen, ist es nötig, die Orte zu geocoden, d.h. </br>
deren geografische Länge und Breite herauszufinden. Dafür sind mehrere </br>
Verfahren wählbar:</b>
</p>
<?php
}
function gcal_geocoding_setting_string() {
$options = get_option('gcal_options');
$current = ( isset ($options['gcal_geocoding']) ? $options['gcal_geocoding'] : 'off' ); // default off
$coders = array(
array(
'option' => 'off',
'name' => 'deaktiviert',
),
array(
'option' => 'official',
'name' => 'Google official (in Entwicklung; erfordert einen API Key)',
),
array(
'option' => 'inofficial',
'name' => 'Google inofficial',
),
array(
'option' => 'osm',
'name' => 'OpenStreetMap (in Entwicklung)',
),
);
foreach ( $coders as $coder ) {
$checked = ( $current == $coder['option'] ? 'checked' : '' );
echo '<input type="radio" id="gcal_geocoding" name="gcal_options[gcal_geocoding]" value ="' . $coder['option'] . '" ' . $checked . '> ' . $coder['name'] . '</br>' ;
}
}
function gcal_options_validate($input) {
return $input;
/*
$newinput['text_string'] = trim($input['text_string']);
if(!preg_match('/^[a-z0-9]{32}$/i', $newinput['text_string'])) {
$newinput['text_string'] = '';
}
return $newinput;
*/
}
/*