<?php


/**
* Validation callback for all of 'gv_settings' option.
*
* Referenced in register_setting() for gv_settings, runs whenever update_option('gv_settings') is used.
* Must account for every field added to gv_settings via add_settings_field
*
* @param array $input Full array of data passed into update_option()
* @return array Sanitized array of data
*/
function gv_settings_validate($input){
global $gv;

/**
* Validate an email address
*/
$email = sanitize_email( $input['email'] );
// If its valid add the email to the output array
if ( $email AND is_email( $email ) ) :
$output['email'] = $email;
// Otherwise add a message with a slug and text value to be output on the settings page
elseif ( $input['email'] ) :
$messages['email_error'] = "Email Error: The 'email address' given, <strong>" . $input['email'] . " </strong> is not a valid email";
endif;


/**
* Validate a page id
*/
if ($input['special_page_id']) :
$page_object = get_page( $input['special_page_id'] );
// Make sure a valid page object was returned.
if ( is_object( $page_object ) AND !is_wp_error($page_object) )
$output['special_page_id'] = $input['special_page_id'];
else
$messages['page_id_error'] = "Page ID Error: The page id given, <strong>" . $input['special_page_id'] . " </strong> is not a page on this site";
endif;

/**
* Add any messages to the array
*/
if (is_array($messages))
$output['messages'] = $messages;

/**
* Return the array of new values
*/
return $output;
}

/**
* Show any messages/errors saved to a setting during automated validation
*
* Needed because validation system has no error reporting.
* Uses a ['messages'] array inside the settings array
* Format should be ['messages'][$slug][$message_text], $slug is unique id, $message is just text.
*
* @param string $setting The setting name as used in get_option()
*/
function gv_settings_display_errors($setting) {
$option = get_option($setting);
if (is_array($option['messages'])) :
foreach ( (array) $option['messages'] as $slug => $message) :
echo "<div id='gv_messages' class='error fade $slug'><p>$message</p></div>";
unset($option['messages'][$slug]);
endforeach;
update_option('gv_settings', $option);
endif;
}

/**
* Page display function for GV Settings
*
* Defined during gv_add_settings_pages()
*/
function gv_settings_page() {
?>
<div>
<h2><?php _e('Global Voices Settings'); ?></h2>
<p>
These settings are related specifically to Global Voices sites and the gv-plugin plugin.
They cover various specific and general functionality added by gv-plugin.
</p>

<?php gv_settings_display_errors('gv_settings'); ?>

<form action="options.php" method="post">
<p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
<?php
// Output fundamental security etc. form fields
settings_fields('gv_settings');
// Output any sections defined for page gv_settings
do_settings_sections('gv_settings');
?>
<p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
</form>
</div>
<?php
}

?>