1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?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
}

?>