-
Notifications
You must be signed in to change notification settings - Fork 4
/
pinim-settings.php
275 lines (215 loc) · 8.41 KB
/
pinim-settings.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
<?php
class Pinim_Settings {
function __construct(){
add_action( 'admin_init', array( $this, 'settings_init' ) );
add_action( 'admin_menu',array( $this,'admin_menu' ),20,2);
}
function admin_menu(){
pinim()->page_settings = add_submenu_page(
sprintf('edit.php?post_type=%s',pinim()->pin_post_type),
__('Settings','pinim'),
__('Settings','pinim'),
'manage_options',
'settings',
array($this, 'page_settings')
);
}
function settings_sanitize( $input ){
$new_input = array();
if( isset( $input['reset_options'] ) ){
$new_input = pinim()->options_default;
}else{ //sanitize values
//delete boards settings
if ( isset($input['delete_boards_settings']) ){
delete_user_meta( get_current_user_id(), pinim()->meta_name_user_boards_options );
}
//boards per page
if ( isset ($input['boards_per_page']) && ctype_digit($input['boards_per_page']) ){
$new_input['boards_per_page'] = $input['boards_per_page'];
}
//pins per page
if ( isset ($input['pins_per_page']) && ctype_digit($input['pins_per_page']) ){
$new_input['pins_per_page'] = $input['pins_per_page'];
}
//default post status
if ( isset ($input['default_status']) ){
$stati = Pinim_Pin_Item::get_allowed_stati();
$stati_keys = array_keys($stati);
if (in_array($input['default_status'],$stati_keys)){
$new_input['default_status'] = $input['default_status'];
}
}
//followed
$new_input['enable_followed'] = isset ($input['enable_followed']) ? 'on' : 'off';
//auto private
$new_input['can_autoprivate'] = isset ($input['can_autoprivate']) ? 'on' : 'off';
}
//remove default values
foreach($input as $slug => $value){
$default = pinim()->get_default_option($slug);
if ($value == $default) unset ($input[$slug]);
}
$new_input = array_filter($new_input);
return $new_input;
}
function settings_init(){
register_setting(
'pinim_option_group', // Option group
pinim()->meta_name_options, // Option name
array( $this, 'settings_sanitize' ) // Sanitize
);
add_settings_section(
'settings_general', // ID
__('General','pinim'), // Title
array( $this, 'pinim_settings_general_desc' ), // Callback
'pinim-settings-page' // Page
);
add_settings_field(
'boards_per_page',
__('Boards per page','pinim'),
array( $this, 'boards_per_page_field_callback' ),
'pinim-settings-page', // Page
'settings_general' //section
);
add_settings_field(
'pins_per_page',
__('Pins per page','pinim'),
array( $this, 'pins_per_page_field_callback' ),
'pinim-settings-page', // Page
'settings_general' //section
);
add_settings_field(
'enable_followed',
__('Followed boards','pinim'),
array( $this, 'enable_followed_callback' ),
'pinim-settings-page', // Page
'settings_general'//section
);
add_settings_section(
'settings_import', // ID
__('Import','pinim'), // Title
array( $this, 'pinim_settings_import_desc' ), // Callback
'pinim-settings-page' // Page
);
add_settings_field(
'default_status',
__('Defaut post status','pinim'),
array( $this, 'default_status_callback' ),
'pinim-settings-page', // Page
'settings_import'//section
);
add_settings_field(
'can_autoprivate',
__('Auto private status','pinim'),
array( $this, 'can_autoprivate_callback' ),
'pinim-settings-page', // Page
'settings_import'//section
);
add_settings_section(
'settings_system', // ID
__('System','pinim'), // Title
array( $this, 'pinim_settings_system_desc' ), // Callback
'pinim-settings-page' // Page
);
add_settings_field(
'reset_options',
__('Reset Options','pinim'),
array( $this, 'reset_options_callback' ),
'pinim-settings-page', // Page
'settings_system'//section
);
if ( pinim_get_boards_options() ){
add_settings_field(
'delete_boards_settings',
__('Delete boards preferences','pinim'),
array( $this, 'delete_boards_settings_callback' ),
'pinim-settings-page', // Page
'settings_system'//section
);
}
}
function page_settings(){
?>
<div class="wrap">
<h2><?php _e('Pinterest Importer Settings','pinim');?></h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'pinim_option_group' );
do_settings_sections( 'pinim-settings-page' );
submit_button();
?>
</form>
</div>
<?php
}
function pinim_settings_general_desc(){
}
function enable_followed_callback(){
$option = pinim()->get_options('enable_followed');
printf(
'<input type="checkbox" name="%s[enable_followed]" value="on" %s/>',
pinim()->meta_name_options,
checked( $option, 'on', false )
);
}
function pinim_settings_import_desc(){
}
function default_status_callback(){
$option = pinim()->get_options('default_status');
$stati = Pinim_Pin_Item::get_allowed_stati();
$select_options = array();
foreach ((array)$stati as $slug=>$status){
$selected = selected( $option, $slug, false);
$select_options[] = sprintf('<option value="%1$s" %2$s>%3$s</option>',$slug,$selected,$status);
}
printf(
'<select name="%1$s[default_status]">%2$s</select>',
pinim()->meta_name_options,
implode('',$select_options)
);
}
function can_autoprivate_callback(){
$option = pinim()->get_options('can_autoprivate');
printf(
'<input type="checkbox" name="%1$s[can_autoprivate]" value="on" %2$s/> %3$s<br/>',
pinim()->meta_name_options,
checked( $option, "on", false ),
__("Set post status to private if the pin's board is secret.","pinim")
);
}
function pinim_settings_system_desc(){
}
function reset_options_callback(){
printf(
'<input type="checkbox" name="%1$s[reset_options]" value="on"/> %2$s',
pinim()->meta_name_options,
__("Reset options to their default values.","pinim")
);
}
function delete_boards_settings_callback(){
printf(
'<input type="checkbox" name="%1$s[delete_boards_settings]" value="on"/> %2$s',
pinim()->meta_name_options,
__("Delete the boards preferences for the current user","pinim")
);
}
function boards_per_page_field_callback(){
$option = (int)pinim()->get_options('boards_per_page');
printf(
'<input type="number" name="%1$s[boards_per_page]" size="3" value="%2$s" /> %3$s',
pinim()->meta_name_options,
$option,
'<small>'.__("0 = display all boards.","pinim").'</small>'
);
}
function pins_per_page_field_callback(){
$option = (int)pinim()->get_options('pins_per_page');
printf(
'<input type="number" name="%1$s[pins_per_page]" size="3" min="10" value="%2$s" /><br/>',
pinim()->meta_name_options,
$option
);
}
}
new Pinim_Settings;