-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathari-settings.php
510 lines (406 loc) · 15.4 KB
/
ari-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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?php
class AriSettings{
/**
* Holds the values to be used in the fields callbacks
*/
public $db_version_name = '_ari_db_version';
public $db_version = '0104';
public $option_name = '_ari_options';
public $options = array();
/**
* Start up
*/
public function __construct(){
$this->options = self::get_settings();
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
add_action( 'plugins_loaded', array($this, 'upgrade'));//install and upgrade
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) );
}
public function get_settings(){
$options = get_option( $this->option_name, self::get_default_settings() );
return apply_filters('ari_get_settings',$options);
}
public function get_default_settings(){
$default = array(
'default_checked' => false,
'remember_status' => false,
'ignored_post_type' => array(),
'replace_parent_link' => true,
'time_limit' => ini_get('max_execution_time'),
'image_size' => "full",
'image_linked_size' => "medium",
'image_linked_target' => "file"
);
return $default;
}
public function get_default_setting($name){
$settings = self::get_default_settings();
if (!array_key_exists($name, $settings)) return false;
return $settings[$name];
}
function enqueue_scripts_styles($hook){
if ($hook!='settings_page_ari-admin') return;
wp_enqueue_script('ari-settings', ari()->plugin_url.'_inc/js/settings.js', array('jquery'),ari()->version);
}
public function get_allowed_post_types(){
$args = array(
'public' => true
);
$post_types = get_post_types($args);
$disabled = apply_filters('ari_option_post_type_disabled',array(
'attachment'
)
);
$post_types = array_diff($post_types,$disabled);
return $post_types;
}
function upgrade(){
global $wpdb;
$current_version = get_option($this->db_version_name);
if ( $current_version==$this->db_version ) return false;
//install
if(!$current_version){
//handle SQL
//require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
//dbDelta($sql);
add_option($this->option_name,$this->get_default_settings()); // add settings
}
//upgrade DB version
update_option($this->db_version_name, $this->db_version );//upgrade DB version
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
__('Archive Remote Images','ari'),
__('Archive Remote Images','ari'),
'manage_options',
'ari-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page(){
// Set class property
?>
<div class="wrap">
<h2><?php _e('Archive Remote Images','ari');?></h2>
<?php
$count = ari()->count_archived_attachments();
if ($count){
?>
<p>
<?php
printf(
_n(
'<strong>1</strong> media has been downloaded using Archive Remote Images !',
'Already <strong>%s</strong> medias have been downloaded using Archive Remote Images !',
$count,
'ari' ),
$count
);
?>
</p>
<?php
}
?>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'ari_option_group' );
do_settings_sections( 'ari-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'ari_option_group', // Option group
$this->option_name, // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'settings_general', // ID
__('General Options','ari'), // Title
array( $this, 'section_general_desc' ), // Callback
'ari-setting-admin' // Page
);
add_settings_field(
'ignored_post_type', // ID
__('Supported post types','ari'), // Title
array( $this, 'post_type_callback' ), // Callback
'ari-setting-admin', // Page
'settings_general' // Section
);
add_settings_field(
'default_checked',
__('Check archiving by default','ari'),
array( $this, 'default_checked_callback' ),
'ari-setting-admin',
'settings_general'
);
add_settings_field(
'remember_status',
__('Remember status','ari'),
array( $this, 'remember_status_callback' ),
'ari-setting-admin',
'settings_general'
);
add_settings_section(
'settings_image', // ID
__('Image Options','ari'), // Title
array( $this, 'section_image_desc' ), // Callback
'ari-setting-admin' // Page
);
add_settings_field(
'image_size',
__('Image size','ari'),
array( $this, 'image_size_callback' ),
'ari-setting-admin',
'settings_image'
);
add_settings_field(
'replace_parent_link',
__('Linked image','ari'),
array( $this, 'replace_parent_link_callback' ),
'ari-setting-admin',
'settings_image'
);
add_settings_field(
'image_linked_size',
__('Linked image size','ari'),
array( $this, 'image_linked_size_callback' ),
'ari-setting-admin',
'settings_image'
);
add_settings_field(
'image_linked_target',
__('Linked image target','ari'),
array( $this, 'image_linked_target_callback' ),
'ari-setting-admin',
'settings_image'
);
add_settings_section(
'settings_system', // ID
__('System Options','ari'), // Title
array( $this, 'section_system_desc' ), // Callback
'ari-setting-admin' // Page
);
add_settings_field(
'time_limit',
__('Time Limit','ari'),
array( $this, 'time_limit_callback' ),
'ari-setting-admin',
'settings_system'
);
add_settings_field(
'reset_options',
__('Reset Options','ari'),
array( $this, 'reset_options_callback' ),
'ari-setting-admin',
'settings_system'
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input ){
$new_input = array();
if( isset( $input['reset_options'] ) ){
$new_input = self::get_default_settings();
}else{
//post types
$post_types = self::get_allowed_post_types();
if( isset( $input['post_types'] ) ){
$new_input['ignored_post_type'] = array();
foreach ((array)$post_types as $post_type){
if (!array_key_exists($post_type, $input['post_types'])) $new_input['ignored_post_type'][] = $post_type;
}
}else{
$new_input['ignored_post_type'] = $post_types;
}
//default checked
if( isset( $input['default_checked'] ) )
$new_input['default_checked'] = (bool)( $input['default_checked'] );
//remember status
if( isset( $input['remember_status'] ) )
$new_input['remember_status'] = (bool)( $input['remember_status'] );
//image size
if( isset( $input['image_size'] ) )
$new_input['image_size'] = self::sanitize_image_size("image_size",$input['image_size']);
//linked image size
if( isset( $input['image_linked_size'] ) )
$new_input['image_linked_size'] = self::sanitize_image_size("image_linked_size",$input['image_linked_size']);
//parent link
if( isset( $input['replace_parent_link'] ) )
$new_input['replace_parent_link'] = (bool)( $input['replace_parent_link'] );
//linked image target
if ( isset( $input['image_linked_target'] )){
$new_input['image_linked_target'] = self::sanitize_linked_image_target($input['image_linked_target']);
};
//time limit
if( isset( $input['time_limit'] ) ){
$new_input['time_limit'] = absint($input['time_limit']);
}
}
$new_input = array_filter($new_input);
return $new_input;
}
function sanitize_image_size($option,$value){
$default = self::get_default_setting($option);
$available = self::available_image_size();
if (in_array($value,$available)){
return $value;
}else{
return $default;
}
}
function sanitize_linked_image_target($value){
$default = self::get_default_setting('image_linked_target');
$available = self::image_linked_available_target();
$available_slugs = array_keys($available);
if (in_array($value,$available_slugs)){
return $value;
}else{
return $default;
}
}
public function image_linked_available_target(){
$available = array(
'file' => __('Media File'),
'post' => __('Attachment Page'),
);
return $available;
}
public function available_image_size(){
$sizes[] = 'full';
$sizes = array_merge($sizes,get_intermediate_image_sizes());
return $sizes;
}
/**
* Print the Section text
*/
public function section_general_desc(){
}
/**
* Get the settings option array and print one of its values
*/
public function post_type_callback(){
$ignored = (array)ari()->get_setting('ignored_post_type');
$post_types = self::get_allowed_post_types();
foreach ((array)$post_types as $slug){
$post_type = get_post_type_object($slug);
$name = $post_type->name;
$checked = checked( in_array($slug,$ignored), false, false );
printf(
'<input type="checkbox" name="%1$s[post_types][%2$s]" value="on" %3$s/> %4$s<br/>',
$this->option_name,
$slug,
$checked,
$name
);
}
}
/**
* Get the settings option array and print one of its values
*/
public function default_checked_callback(){
$option = ari()->get_setting('default_checked');
$checked = checked( (bool)$option, true, false );
printf(
'<input type="checkbox" name="%1$s[default_checked]" value="on" %2$s/>',
$this->option_name,
$checked
);
}
public function remember_status_callback(){
$option = ari()->get_setting('remember_status');
$checked = checked( (bool)$option, true, false );
printf(
'<input type="checkbox" name="%1$s[remember_status]" value="on" %2$s/> %3$s',
$this->option_name,
$checked,
__("Remember archiving status for posts, so you don't need to click the checkbox each time.","ari")
);
}
public function section_image_desc(){
}
public function image_size_callback(){
$option = ari()->get_setting('image_size');
$box = '<select name="'.$this->option_name.'[image_size]">';
foreach (self::available_image_size() as $size){
$box .= '<option value="'.$size.'" '.selected( $option, $size, false ).'>'.$size.'</option>';
}
$box.='</select> ';
printf(
__('Display %1$s image size','ari'),
$box
);
}
public function image_linked_size_callback(){
$option = ari()->get_setting('image_linked_size');
$box = '<select name="'.$this->option_name.'[image_linked_size]">';
foreach (self::available_image_size() as $size){
$box .= '<option value="'.$size.'" '.selected( $option, $size, false ).'>'.$size.'</option>';
}
$box.='</select> ';
printf(
__('Display %1$s image size','ari'),
$box
);
}
public function replace_parent_link_callback(){
$option = ari()->get_setting('replace_parent_link');
printf(
'<input type="checkbox" name="%1$s[replace_parent_link]" value="on" %2$s/> %3$s',
$this->option_name,
checked( (bool)$option, true, false ),
__("If the remote image is wrapped into a link pointing to the same remote file, replace that link.","ari")
);
}
public function image_linked_target_callback(){
$option = ari()->get_setting('image_linked_target');
$box = '<select name="'.$this->option_name.'[image_linked_target]">';
foreach (self::image_linked_available_target() as $slug=>$name){
$box .= '<option value="'.$slug.'" '.selected( $option, $slug, false ).'>'.$name.'</option>';
}
$box.='</select> ';
echo $box;
}
public function section_system_desc(){
}
public function time_limit_callback(){
$option = absint(ari()->get_setting('time_limit'));
$min = 0;
$max = 600;
printf(
'<input type="number" name="%1$s[time_limit]" value="%2$d" class="small-text" min="%3$d" max="%4$d"/> %5$s',
$this->option_name,
$option,
$min,
$max,
__("Limits the maximum execution time for the script (seconds).","ari")
);
}
public function reset_options_callback(){
printf(
'<input type="checkbox" name="%1$s[reset_options]" value="on"/> %2$s',
$this->option_name,
__("Reset options to their default values.","ari")
);
}
}