This repository has been archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-based.php
198 lines (179 loc) · 4.65 KB
/
class-based.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* An example of using the settings API with a class
*
* @since 1.0
* @author Christopher Davis <http://pmg.co/people/chris>
* @copyright Performance Media Group 2012
* @license GPLv2
*/
class PMG_Settings_Tutorial
{
/**
* Our setting name
*
* @since 1.0
*/
const SETTING = 'pmgtut_class_setting';
/**
* Page on which our options will reside
*
* @since 1.0
*/
const PAGE = 'sample_options_page';
/**
* Our section ID
*
* @since 1.0
*/
const SECTION = 'pmgtut_class_section';
/**
* Called at the bottom of this file to add actions.
*
* @since 1.0
* @access public
* @uses add_action
*/
public static function init()
{
add_action(
'admin_init',
array(__CLASS__, 'register')
);
add_action(
'admin_menu',
array(__CLASS__, 'menu_page')
);
}
/**
* Hooked into `admin_init` this is the central function that takes care of
* registering the setting, adding sections, and adding fields
*
* @since 1.0
* @access public
* @uses register_setting
* @uses add_settings_section
* @uses add_settings_field
*/
public static function register()
{
register_setting(
self::PAGE,
self::SETTING,
array(__CLASS__, 'validate')
);
// at this point WP knows about your setting.
add_settings_section(
self::SECTION,
__('Example Class Section', 'pmg'),
'__return_false',
self::PAGE
);
$fields = array(
'email' => __('Your Email', 'pmg'),
'normal' => __('Some Text', 'pmg')
);
foreach($fields as $key => $label)
{
add_settings_field(
$key,
$label,
array(__CLASS__, 'field_cb'),
self::PAGE,
self::SECTION,
array('key' => $key) // option args
);
}
}
/**
* settings validation callback
*
* @since 1.0
* @uses add_settings_error
* @uses esc_attr
* @uses is_email
* @return array The cleaned options
*/
public static function validate($dirty)
{
$clean = array();
if(isset($dirty['email']) && is_email($dirty['email']))
{
$clean['email'] = $dirty['email'];
}
else
{
add_settings_error(
'pmgtut_setting',
'pmg-invalid-email',
__('Please enter a valid email', 'pmg'),
'error'
);
}
$clean['normal'] = isset($dirty['normal']) ? esc_attr($dirty['normal']) : '';
return $clean;
}
/**
* Example of a unified callback
*
* @since 1.0
* @uses get_option
*/
public static function field_cb($args)
{
// get our options
$opts = get_option(self::SETTING, array());
// Set up the value with a ternary statment
$val = isset($opts[$args['key']]) ? $opts[$args['key']] : '';
// actually print the field
printf(
'<input type="text" class="regular-text" id="%1$s[%2$s]" name="%1$s[%2$s]" value="%3$s" />',
esc_attr(self::SETTING),
esc_attr($args['key']),
esc_attr($val)
);
}
/**
* Register a new admin page
*
* @since 1.0
* @access public
* @uses add_options_page
*/
public static function menu_page()
{
$p = add_options_page(
__('Sample Options Page', 'pmg'),
__('Sample Options', 'pmg'),
'manage_options',
'pmg-sample-options',
array(__CLASS__, 'menu_page_cb')
);
}
/**
* Menu page callback function
*
* @since 1.0
* @access public
* @uses settings_field
* @uses do_settings_section
* @uses screen_icon
*/
public static function menu_page_cb()
{
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php esc_html_e('Sample Options Page', 'pmg'); ?></h2>
<form action="<?php echo admin_url('options.php'); ?>" method="post">
<?php
settings_fields(self::PAGE);
do_settings_sections(self::PAGE);
submit_button(__('Save Settings', 'pmg'));
?>
</form>
</div>
<?php
}
} // end class
PMG_Settings_Tutorial::init();