148 lines
3.5 KiB
PHP
148 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Settings screen (Settings > Piazza in Festa).
|
|
*
|
|
* One field only: the optional service key. The plugin does everything it can
|
|
* do without it; the key just lets the service serve the widget without the
|
|
* small Piazza in Festa credit line at the bottom.
|
|
*
|
|
* @package PiazzaInFesta
|
|
*/
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
/**
|
|
* Reads one saved option.
|
|
*
|
|
* @param string $key Option key.
|
|
* @return string Empty string when unset.
|
|
*/
|
|
function pinfesta_get_option( $key ) {
|
|
$options = get_option( 'pinfesta_options', array() );
|
|
|
|
if ( ! is_array( $options ) || ! isset( $options[ $key ] ) ) {
|
|
return '';
|
|
}
|
|
|
|
return (string) $options[ $key ];
|
|
}
|
|
|
|
/**
|
|
* Registers the setting and its field.
|
|
*
|
|
* @return void
|
|
*/
|
|
function pinfesta_register_settings() {
|
|
register_setting(
|
|
'pinfesta_settings',
|
|
'pinfesta_options',
|
|
array(
|
|
'type' => 'array',
|
|
'sanitize_callback' => 'pinfesta_sanitize_options',
|
|
'default' => array(),
|
|
)
|
|
);
|
|
|
|
add_settings_section(
|
|
'pinfesta_section',
|
|
'',
|
|
'__return_false',
|
|
'piazza-in-festa'
|
|
);
|
|
|
|
add_settings_field(
|
|
'pinfesta_chiave',
|
|
esc_html__( 'Service key', 'piazza-in-festa' ),
|
|
'pinfesta_field_chiave',
|
|
'piazza-in-festa',
|
|
'pinfesta_section'
|
|
);
|
|
}
|
|
add_action( 'admin_init', 'pinfesta_register_settings' );
|
|
|
|
/**
|
|
* Sanitises the saved options.
|
|
*
|
|
* @param mixed $input Raw value coming from the form.
|
|
* @return array
|
|
*/
|
|
function pinfesta_sanitize_options( $input ) {
|
|
$out = array();
|
|
|
|
if ( is_array( $input ) && isset( $input['chiave'] ) ) {
|
|
// Keys are hexadecimal strings; anything else is not a key.
|
|
$chiave = preg_replace( '/[^a-zA-Z0-9]/', '', (string) $input['chiave'] );
|
|
|
|
$out['chiave'] = substr( (string) $chiave, 0, 128 );
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
/**
|
|
* Prints the key field.
|
|
*
|
|
* @return void
|
|
*/
|
|
function pinfesta_field_chiave() {
|
|
printf(
|
|
'<input type="text" class="regular-text" name="pinfesta_options[chiave]" value="%s" autocomplete="off" />',
|
|
esc_attr( pinfesta_get_option( 'chiave' ) )
|
|
);
|
|
|
|
echo '<p class="description">' . esc_html__( 'Optional. Leave it empty and the widget works anyway, with a small credit line at the bottom.', 'piazza-in-festa' ) . '</p>';
|
|
}
|
|
|
|
/**
|
|
* Adds the menu entry.
|
|
*
|
|
* @return void
|
|
*/
|
|
function pinfesta_admin_menu() {
|
|
add_options_page(
|
|
esc_html__( 'Piazza in Festa', 'piazza-in-festa' ),
|
|
esc_html__( 'Piazza in Festa', 'piazza-in-festa' ),
|
|
'manage_options',
|
|
'piazza-in-festa',
|
|
'pinfesta_settings_page'
|
|
);
|
|
}
|
|
add_action( 'admin_menu', 'pinfesta_admin_menu' );
|
|
|
|
/**
|
|
* Prints the settings page.
|
|
*
|
|
* @return void
|
|
*/
|
|
function pinfesta_settings_page() {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return;
|
|
}
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php echo esc_html__( 'Piazza in Festa', 'piazza-in-festa' ); ?></h1>
|
|
|
|
<p>
|
|
<?php echo esc_html__( 'Add the events with the block called Piazza in Festa, or with this shortcode:', 'piazza-in-festa' ); ?>
|
|
<code>[piazza-in-festa comuni="alba,barolo,la-morra" limite="5"]</code>
|
|
</p>
|
|
<p>
|
|
<?php
|
|
printf(
|
|
/* translators: %s: link to the online configurator. */
|
|
esc_html__( 'Not sure about the identifiers of your towns? Build the widget here and copy the values: %s', 'piazza-in-festa' ),
|
|
'<a href="' . esc_url( pinfesta_base_url() . '/widget' ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( pinfesta_base_url() . '/widget' ) . '</a>'
|
|
);
|
|
?>
|
|
</p>
|
|
|
|
<form action="options.php" method="post">
|
|
<?php
|
|
settings_fields( 'pinfesta_settings' );
|
|
do_settings_sections( 'piazza-in-festa' );
|
|
submit_button();
|
|
?>
|
|
</form>
|
|
</div>
|
|
<?php
|
|
}
|