forked from envato/wp-image-size-limit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpisl-options.php
158 lines (128 loc) · 3.7 KB
/
wpisl-options.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
<?php
/**
* WP Image Size Limit - Options
*
* @since Version 1.0
*/
/**
* Register the form setting for our wpisl_options array.
*
* This function is attached to the admin_init action hook.
*
* @since Version 1.0
*/
function wpisl_options_init() {
// If we have no options in the database, let's add them now.
if ( false === wpisl_get_options() )
add_option( 'wpisl_options', wpisl_get_default_options() );
register_setting(
'media', // Options group
'wpisl_options', // Database option, see wpisl_get_options()
'wpisl_options_validate' // The sanitization callback, see wpisl_options_validate()
);
add_settings_field(
'img_upload_limit',
'Maximum File Size for Images',
'wpisl_settings_field_img_upload_limit',
'media',
'uploads'
);
add_settings_field(
'img_upload_limit_types_regex',
'MIME types regex pattern of media files where the upload limit is enforced',
'wpisl_settings_field_img_upload_limit_types_regex',
'media',
'uploads'
);
}
add_action( 'admin_init', 'wpisl_options_init' );
/**
* Returns the default options.
*
* @since Version 1.0
*/
function wpisl_get_default_options() {
$wpisl = new WP_Image_Size_Limit;
$limit = $wpisl->wp_limit();
$default_options = array(
'img_upload_limit' => $limit,
'img_upload_limit_types_regex' => '/^image\//',
);
return apply_filters( 'wpisl_default_options', $default_options );
}
/**
* Returns the options array.
*
* @since Version 1.0
*/
function wpisl_get_options() {
return get_option( 'wpisl_options', wpisl_get_default_options() );
}
/**
* Renders the Maximum Upload Size setting field.
*
* @since Version 1.0
*
*/
function wpisl_settings_field_img_upload_limit() {
$options = wpisl_get_options();
$wpisl = new WP_Image_Size_Limit;
$limit = $wpisl->wp_limit();
// Sanitize
$id = 'img_upload_limit';
if ( isset($options[$id]) && ($options[$id] < $limit) ) {
$value = $options[$id];
}
/*elseif ( empty($options[$id]) ) {
$value = '1000';
} */
else {
$value = $limit;
}
$field = '<p>
<input name="wpisl_options[' . $id . ']' . '" id="wpisl-limit" type="text" value="' . $value . '" size="4" maxlength="5" /> KB
<br>
<span class="description">Server maximum: '.$limit.' KB</span>
</p>';
echo $field;
}
function wpisl_settings_field_img_upload_limit_types_regex() {
$options = wpisl_get_options();
$id = 'img_upload_limit_types_regex';
if ( isset($options[$id]) ) {
$value = $options[$id];
}
else {
$value = '/^image\//';
}
$field = '<p>
<input name="wpisl_options[' . $id . ']' . '" id="wpisl-limit-type" type="text" value="' . $value . '" size="80" />
<br/>
<a href="http://php.net/manual/en/reference.pcre.pattern.syntax.php">Regex Pattern Reference</a>
<br/><span class="hint">Use <code>/^image\//</code> to match all image types</span>
</p>';
echo $field;
}
/**
* Sanitize and validate form input. Accepts an array, return a sanitized array.
*
* @see wpisl_options_init()
* @since Version 1.0
*/
function wpisl_options_validate( $input ) {
$output = $defaults = wpisl_get_default_options();
$wpisl = new WP_Image_Size_Limit;
$limit = $wpisl->wp_limit();
$output['img_upload_limit'] = str_replace(',','', $input['img_upload_limit']);
$output['img_upload_limit'] = absint( intval( $output['img_upload_limit'] ) );
if ( $output['img_upload_limit'] > $limit ) {
$output['img_upload_limit'] = $limit;
}
// TODO: verify regex, maybe?
$output['img_upload_limit_types_regex'] = $input['img_upload_limit_types_regex'];
return apply_filters( 'wpisl_options_validate', $output, $input, $defaults );
}
function unique_identifyer_admin_notices() {
settings_errors( 'img_upload_limit' );
}
add_action( 'admin_notices', 'unique_identifyer_admin_notices' );