This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optionspage.php
307 lines (273 loc) · 8.83 KB
/
optionspage.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* LikeCount Options page
*/
class LikeCountOptionsPage
{
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'facebook_page_init' ) );
add_action( 'admin_init', array( $this, 'twitter_page_init' ) );
add_action( 'admin_init', array( $this, 'instagram_page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'LikeCount Settings',
'LikeCount',
'manage_options',
'likecount-settings',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
?>
<div class="wrap">
<h1>LikeCount Settings</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'likecount_group' );
do_settings_sections( 'likecount-settings' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function facebook_page_init()
{
/**
* LikeCount Facebook
*/
add_settings_section(
'likecount_facebook_option', // ID
'Facebook', // Title
array( $this, 'likecount_facebook_callback' ), // Callback
'likecount-settings' // Page
);
add_settings_field(
'likecount_fb_client_id', // ID
'Facebook Client ID', // Title
array( $this, 'likecount_fb_client_id_callback' ), // Callback
'likecount-settings', // Page
'likecount_facebook_option' // Section
);
add_settings_field(
'likecount_fb_client_secret',
'Facebook Client Secret',
array( $this, 'likecount_fb_client_secret_callback' ),
'likecount-settings',
'likecount_facebook_option'
);
add_settings_field(
'likecount_fb_page_name',
'Facebook Page Name',
array( $this, 'likecount_fb_page_name_callback' ),
'likecount-settings',
'likecount_facebook_option'
);
/**
* Option Group
*/
register_setting(
'likecount_group', // Option group
'likecount-facebook-data', // Option name
array( $this, 'sanitize_facebook' ) // Sanitize
);
}
/**
* Register and add settings
*/
public function twitter_page_init()
{
/**
* LikeCount Twitter
*/
add_settings_section(
'likecount_twitter_option', // ID
'Twitter', // Title
array( $this, 'likecount_twitter_callback' ), // Callback
'likecount-settings' // Page
);
add_settings_field(
'likecount_tw_page_name',
'Twitter Page Name',
array( $this, 'likecount_tw_page_name_callback' ),
'likecount-settings',
'likecount_twitter_option'
);
/**
* Option Group
*/
register_setting(
'likecount_group', // Option group
'likecount-twitter-data', // Option name
array( $this, 'sanitize_twitter' ) // Sanitize
);
}
/**
* Register and add settings
*/
public function instagram_page_init()
{
/**
* LikeCount Instagram
*/
add_settings_section(
'likecount_instagram_option', // ID
'Instagram', // Title
array( $this, 'likecount_instagram_callback' ), // Callback
'likecount-settings' // Page
);
add_settings_field(
'likecount_ig_page_name',
'Instagram Page Name',
array( $this, 'likecount_ig_page_name_callback' ),
'likecount-settings',
'likecount_instagram_option'
);
/**
* Option Group
*/
register_setting(
'likecount_group', // Option group
'likecount-instagram-data', // Option name
array( $this, 'sanitize_instagram' ) // Sanitize
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize_facebook( $input )
{
$new_input = array();
// Facebook
if( isset( $input['likecount_fb_client_id'] ) )
$new_input['likecount_fb_client_id'] = absint( $input['likecount_fb_client_id'] );
if( isset( $input['likecount_fb_client_secret'] ) )
$new_input['likecount_fb_client_secret'] = sanitize_text_field( $input['likecount_fb_client_secret'] );
if( isset( $input['likecount_fb_page_name'] ) )
$new_input['likecount_fb_page_name'] = sanitize_text_field( $input['likecount_fb_page_name'] );
return $new_input;
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize_twitter( $input )
{
$new_input = array();
// Twitter
if( isset( $input['likecount_tw_page_name'] ) )
$new_input['likecount_tw_page_name'] = sanitize_text_field( $input['likecount_tw_page_name'] );
return $new_input;
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize_instagram( $input )
{
$new_input = array();
// Instagram
if( isset( $input['likecount_ig_page_name'] ) )
$new_input['likecount_ig_page_name'] = sanitize_text_field( $input['likecount_ig_page_name'] );
return $new_input;
}
/**
* Print the Section text
*/
public function likecount_facebook_callback()
{
print 'Enter your Facebook credentials:';
}
/**
* Get the settings option array and print one of its values
*/
public function likecount_fb_client_id_callback()
{
$options = get_option('likecount-facebook-data');
printf(
'<input type="text" id="likecount_fb_client_id" name="likecount-facebook-data[likecount_fb_client_id]" value="%s" />',
isset( $options['likecount_fb_client_id'] ) ? esc_attr( $options['likecount_fb_client_id']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function likecount_fb_client_secret_callback()
{
$options = get_option('likecount-facebook-data');
printf(
'<input type="text" id="likecount_fb_client_secret" name="likecount-facebook-data[likecount_fb_client_secret]" value="%s" />',
isset( $options['likecount_fb_client_secret'] ) ? esc_attr( $options['likecount_fb_client_secret']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function likecount_fb_page_name_callback()
{
$options = get_option('likecount-facebook-data');
printf(
'<input type="text" id="likecount_fb_page_name" name="likecount-facebook-data[likecount_fb_page_name]" value="%s" />',
isset( $options['likecount_fb_page_name'] ) ? esc_attr( $options['likecount_fb_page_name']) : ''
);
}
/**
* Print the Section text
*/
public function likecount_twitter_callback()
{
print 'Enter your Twitter credentials:';
}
/**
* Get the settings option array and print one of its values
*/
public function likecount_tw_page_name_callback()
{
$options = get_option('likecount-twitter-data');
printf(
'<input type="text" id="likecount_tw_page_name" name="likecount-twitter-data[likecount_tw_page_name]" value="%s" />',
isset( $options['likecount_tw_page_name'] ) ? esc_attr( $options['likecount_tw_page_name']) : ''
);
}
/**
* Print the Section text
*/
public function likecount_instagram_callback()
{
print 'Enter your Instagram credentials:';
}
/**
* Get the settings option array and print one of its values
*/
public function likecount_ig_page_name_callback()
{
$options = get_option('likecount-instagram-data');
printf(
'<input type="text" id="likecount_ig_page_name" name="likecount-instagram-data[likecount_ig_page_name]" value="%s" />',
isset( $options['likecount_ig_page_name'] ) ? esc_attr( $options['likecount_ig_page_name']) : ''
);
}
}