-
Notifications
You must be signed in to change notification settings - Fork 6
/
mv-options-media-vault.php
309 lines (227 loc) · 8.62 KB
/
mv-options-media-vault.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
<?php
/**
* Media Vault Settings Functions.
*
* @package WordPress_Plugin
* @package MediaVault
*
* @author Max G J Panas <http://maxpanas.com/>
* @license GPL-3.0+
*/
/** Register plugin settings **/
register_setting( 'media', 'mgjp_mv_default_permission' );
register_setting( 'media', 'mgjp_mv_options', 'mgjp_mv_options_sanitize' );
register_setting( 'media', 'mgjp_mv_ir', 'mgjp_mv_ir_sanitize' );
add_settings_section(
'mgjp_mv_general_settings',
null,
'mgjp_mv_render_general_settings_info_txt',
'media'
);
add_settings_field(
'default_permission',
__( 'Default Protected File Permissions', 'media-vault' ),
'mgjp_mv_render_default_permission_field',
'media',
'mgjp_mv_general_settings',
array( 'label_for' => 'mgjp_mv_default_permission' )
);
add_settings_field(
'default_upload_protection',
__( 'Default Upload Protection', 'media-vault' ),
'mgjp_mv_render_checkbox_field',
'media',
'mgjp_mv_general_settings',
array(
'label_for' => 'mgjp_mv_default_upload_protection',
'option' => 'mgjp_mv_options',
'option_id' => 'default_upload_protection',
'value' => 'on',
'desc' => __( 'Set media file upload protection to be enabled by default when uploading new files through the Add New Media page in the WordPress Admin.', 'media-vault' )
)
);
add_settings_field(
'place_holder_img',
__( 'Image Placeholder', 'media-vault' ),
'mgjp_mv_render_place_holder_img_field',
'media',
'mgjp_mv_general_settings'
);
/**
* Sanitization function for mgjp_mv_option settings
*
* @since 0.8
*
* @param $input array of options from settings page
* @return array sanitized array of mgjp_mv_options
*/
function mgjp_mv_options_sanitize( $input ) {
$options = get_option( 'mgjp_mv_options' );
$options['default_upload_protection'] = isset( $input['default_upload_protection'] ) ? 'on' : 'off';
return $options;
}
/**
* Sanitization function for mgjp_mv_ir settings
*
* @since 0.8
*
* @param $input array of options from settings page
* @return array sanitized array of mgjp_mv_ir options
*/
function mgjp_mv_ir_sanitize( $input ) {
$options = get_option( 'mgjp_mv_ir' );
$options['is_on'] = isset( $input['is_on'] ) ? ! ! $input['is_on'] : false;
if ( isset( $input['id'] ) && wp_attachment_is_image( absint( $input['id'] ) ) )
$options['id'] = absint( $input['id'] );
else
add_settings_error(
'mgjp_mv_ir',
'invalid-attachment-id',
__( 'The Media Vault placeholder image ID must be the ID of an existing image attachment in your media library. Please select a different Media Vault placeholder image.', 'media-vault' )
);
if ( isset( $input['default'] ) && wp_attachment_is_image( absint( $input['default'] ) ) )
$options['default'] = absint( $input['default'] );
return $options;
}
/**
* Render the General Settings info txt
*
* @since 0.4
*/
function mgjp_mv_render_general_settings_info_txt() {
echo '<h3 class="title" id="mgjp_mv_settings_section">Media Vault</h3>';
echo '<p>';
esc_html_e( 'Media Vault is a plugin that allows you to protect media files in your uploads folder.', 'media-vault' );
echo ' ';
esc_html_e( 'Here you can set options for:', 'media-vault' );
echo '</p>';
}
/**
* Render a generic single checkbox field, supports options
* saved in an array *for options saved in an array remember
* to always manually handle sanitizing the saved settings
*
* @since 0.8
*
* @param array Array of arguments passed the specific settings field
*/
function mgjp_mv_render_checkbox_field( $args ) {
$id = isset( $args['label_for'] ) ? $args['label_for'] : ( isset( $args['id'] ) ? $args['id'] : '' );
$id = esc_attr( $id );
$option = get_option( $args['option'] );
$name = $args['option'];
if ( isset( $args['option_id'] ) ) {
$option = isset( $option[$args['option_id']] ) ? $option[$args['option_id']] : null;
$name = $args['option'] . '[' . $args['option_id'] . ']';
}
$value = isset( $args['value'] ) ? $args['value'] : true;
?>
<label for="<?php echo $id; ?>">
<input type="checkbox" id="<?php echo $id; ?>" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>" <?php checked( $option, $value ); ?>>
<?php if ( isset( $args['desc'] ) ) : ?>
<span class="description">
<?php echo esc_html( $args['desc'] ); ?>
</span>
<?php endif; ?>
</label>
<?php
}
/**
* Render the default file access permission field
*
* @since 0.4
*
* @param array Array of arguments passed the specific settings field
*/
function mgjp_mv_render_default_permission_field( $args ) {
$default_permission = get_option( 'mgjp_mv_default_permission', 'logged-in' );
?>
<select id="mgjp_mv_default_permission" name="mgjp_mv_default_permission">
<?php foreach( mgjp_mv_get_the_permissions() as $permission => $data ) : ?>
<option value="<?php echo esc_attr( $permission ); ?>" <?php selected( $default_permission, $permission ); ?>>
<?php echo esc_html( $data['select'] ); ?>
</option>
<?php endforeach; ?>
</select>
<span class="description">
<?php esc_html_e( 'Select the default permissions required for accessing protected media uploads.', 'media-vault' ); ?>
</span>
<?php
}
/**
* Render the placeholder image field
*
* @since 0.8
*
* @param array Array of arguments passed the specific settings field
*/
function mgjp_mv_render_place_holder_img_field( $args ) {
$ir = get_option( 'mgjp_mv_ir' );
$image_args = array(
'alt' => __( 'Media Vault selected placeholder image', 'media-vault' ),
'title' => __( 'Selected placeholder', 'media-vault' )
);
wp_localize_script( 'mgjp-image-selector', 'mgjp_mv_options_media', array(
'ir_select_btn' => __( 'Select Placeholder', 'media-vault' ),
'ir_select_btn2' => __( 'Change Placeholder', 'media-vault' ),
'ir_restore_btn' => __( 'Restore the Default', 'media-vault' ),
'ir_modal_title' => __( 'Select image placeholder for images that have been restricted.', 'media-vault' ),
'ir_modal_btn' => __( 'Use image as placeholder', 'media-vault' ),
'ir_image_args' => $image_args,
'ir_default' => isset( $ir['default'] ) ? $ir['default'] : -1,
'ir_size' => array( 100, 80 ),
'ir_restore_nonce' => wp_create_nonce( 'mgjp_mv_ir_restore_default' )
) );
?>
<style>
.mgjp-mv-ir-container {
margin-top: 4px;
}
.mgjp-mv-ir-container p {
margin-bottom: 0;
}
.mgjp-mv-img-select-preview {
float: left;
margin-right: 8px;
text-align: center;
}
</style>
<p>
<label for="mgjp_mv_ir">
<input type="checkbox" id="mgjp_mv_ir" name="mgjp_mv_ir[is_on]" value="true" <?php if ( isset( $ir['is_on'] ) ) checked( $ir['is_on'] ); ?>>
<span class="description">
<?php esc_html_e( 'Enable showing a replacement image for protected images when the user does not have sufficient permissions to access them.', 'media-vault' ); ?>
</span>
</label>
</p>
<div class="mgjp-mv-ir-container">
<div class="mgjp-mv-img-select-preview" id="mgjp_mv_ir_preview">
<?php if ( isset( $ir['id'] ) ) : ?>
<?php echo wp_get_attachment_image( $ir['id'], array( 100, 80 ), false, $image_args ); ?>
<?php endif; ?>
</div>
<div id="mgjp_mv_ir_wrap">
<label class="hide-if-js" for="mgjp_mv_ir_id">
<input class="small-text" type="number" step="1" min="0" id="mgjp_mv_ir_id" name="mgjp_mv_ir[id]" value="<?php if ( isset( $ir['id'] ) && wp_attachment_is_image( $ir['id'] ) ) echo absint( $ir['id'] ); ?>" size="5">
<span class="description">
<?php esc_html_e( 'The attachment ID of the image you would like to use as a placeholder.', 'media-vault' ); ?>
</span>
</label>
</div>
</div>
<?php
}
/**
* Add necessary scripts and styles to the WP admin media settings page
*
* @since 0.8
*/
function mgjp_mv_options_media_enqueue_scripts() {
$screen = get_current_screen();
if ( 'options-media' !== $screen->base )
return;
wp_enqueue_media();
wp_enqueue_script( 'mgjp-image-selector', plugins_url( 'js/min/mv-image-selector.min.js', __FILE__ ), array( 'jquery', 'json2' ), null, true );
}
add_action( 'admin_enqueue_scripts', 'mgjp_mv_options_media_enqueue_scripts' );
?>