forked from bruno-barros/wordpress-plugin-media-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv-ajax-actions.php
329 lines (246 loc) · 10.3 KB
/
mv-ajax-actions.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
<?php
/**
* Media Vault Admin Ajax Handling.
*
* @package WordPress_Plugin
* @package MediaVault
*
* @author Max G J Panas <[email protected]>
* @license GPL-3.0+
*/
// forbid direct calls to this file without wp ajax constants
if (!defined('DOING_AJAX') || !DOING_AJAX)
{
header('Status: 403 Forbidden');
header('HTTP/1.1 403 Forbidden');
exit();
}
/**
* Get the HTML image element of an attachment via AJAX
*
* @since 0.8
*
* @return string HTML image element of attachment file,
* if there is any, otherwise return 0
*/
function mgjp_mv_get_attachment_image()
{
$id = isset($_GET['id']) ? absint($_GET['id']) : '';
$size = isset($_GET['size']) ? $_GET['size'] : 'thumbnail';
$icon = isset($_GET['icon']) ? !!$_GET['icon'] : false;
$args = isset($_GET['args']) ? $_GET['args'] : null;
$html = wp_get_attachment_image($id, $size, $icon, $args);
if (empty($html))
{
wp_die(-1);
}
wp_die($html);
}
add_action('wp_ajax_mgjp_mv_get_attachment_image', 'mgjp_mv_get_attachment_image');
/**
* Attempt to restore the default placeholder image
*
* @since 0.8
*
* @return array [0]
* [1]
*/
function mgjp_mv_restore_default_placeholder_image()
{
if (!current_user_can('manage_options') || !current_user_can('upload_files'))
{
wp_die(-1);
}
check_ajax_referer('mgjp_mv_ir_restore_default', 'nonce');
$size = isset($_POST['size']) ? $_POST['size'] : 'thumbnail';
$args = isset($_GET['args']) ? $_GET['args'] : null;
$ir_id = mgjp_mv_load_placeholder_image(true);
if (!$ir_id)
{
wp_die(-1);
}
wp_die(json_encode(array(
'id' => $ir_id,
'img' => wp_get_attachment_image($ir_id, $size, false, $args),
)));
}
add_action('wp_ajax_mgjp_mv_restore_default_placeholder_image', 'mgjp_mv_restore_default_placeholder_image');
/**
* Render the Media Vault attachment edit fields in
* the Media Upload modal
*
* @since 0.8.9
*
* @uses mgjp_mv_get_the_permissions()
* @uses mgjp_mv_is_protected()
* @return array if called in the Media Modal, adds a Media Vault field
* to the attachment fields to edit array
*/
function mgjp_mv_add_attachment_edit_fields($form_fields, $post)
{
// only add the field to the Media Upload Modal, and not the attachment
// edit page, we have the Media Vault Protection Settings metabox for
// that job
if (get_current_screen() !== null)
{
return $form_fields;
}
$permission = get_post_meta($post->ID, '_mgjp_mv_permission', true);
$permissions = mgjp_mv_get_the_permissions();
if (empty($permission) || !isset($permissions[$permission]))
{
$permission = 'default';
}
$default = array(
'default' => array(
'select' => __('Use Default Setting', 'media-vault'),
),
);
$permissions = $default + $permissions;
ob_start(); ?>
<tr id="mgjp_mv_attachment_fields" class="mgjp_mv_attachment_fields">
<th><?php esc_html_e('Media Vault Protection Settings', 'media-vault'); ?></th>
<td>
<label for="attachments[<?php echo $post->ID; ?>][mgjp_mv_protection_toggle]" class="button">
<input type="hidden" name="attachments[<?php echo $post->ID ?>][mgjp_mv_protection_toggle]" value="off">
<input class="mgjp_mv_protection_toggle" type="checkbox"
id="attachments[<?php echo $post->ID; ?>][mgjp_mv_protection_toggle]"
name="attachments[<?php echo $post->ID; ?>][mgjp_mv_protection_toggle]" <?php checked(mgjp_mv_is_protected($post->ID)); ?>>
<?php esc_html_e('Protect files', 'media-vault'); ?>
</label>
<p id="mgjp_mv_attachment_permissions_field">
<label for="attachments[<?php echo $post->ID; ?>][mgjp_mv_permission_select]"><?php esc_html_e('File access permission:', 'media-vault'); ?></label>
<select class="mgjp_mv_permission_select"
id="attachments[<?php echo $post->ID; ?>][mgjp_mv_permission_select]"
name="attachments[<?php echo $post->ID; ?>][mgjp_mv_permission_select]">
<?php foreach ($permissions as $key => $data) : ?>
<option value="<?php echo esc_attr($key); ?>" <?php selected($permission, $key); ?>>
<?php echo esc_html($data['select']); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<script>
jQuery(function ($) {
$('#mgjp_mv_attachment_fields').trigger('mgjpMvLoaded', <?php echo $post->ID; ?>);
}(jQuery));
</script>
<td>
</tr>
<?php
$form_fields['mgjp_mv_permission_fields']['tr'] = ob_get_clean();
return $form_fields;
}
function mgjp_mv_add_attachment_edit_fields_2($form_fields, $post)
{
// only add the field to the Media Upload Modal, and not the attachment
// edit page, we have the Media Vault Protection Settings metabox for
// that job
if (get_current_screen() !== null)
{
return $form_fields;
}
$permission = get_post_meta($post->ID, '_mgjp_mv_permission', true);
$permissions = mgjp_mv_get_the_permissions();
if (empty($permission) || !isset($permissions[$permission]))
{
$permission = 'default';
}
$default = array(
'default' => array(
'select' => __('Use Default Setting', 'media-vault'),
),
);
$permissions = $default + $permissions;
ob_start();
?>
<div id="mgjp_mv_attachment_fields">
<label for="attachments[<?php echo $post->ID; ?>][mgjp_mv_protection_toggle]" class="button">
<input type="hidden" name="attachments[<?php echo $post->ID ?>][mgjp_mv_protection_toggle]" value="off">
<input class="mgjp_mv_protection_toggle" type="checkbox"
id="attachments[<?php echo $post->ID; ?>][mgjp_mv_protection_toggle]"
name="attachments[<?php echo $post->ID; ?>][mgjp_mv_protection_toggle]" <?php checked(mgjp_mv_is_protected($post->ID)); ?>>
<?php esc_html_e('Protect files', 'media-vault'); ?>
</label>
<p id="mgjp_mv_attachment_permissions_field">
<label for="attachments[<?php echo $post->ID; ?>][mgjp_mv_permission_select]"><?php esc_html_e('File access permission:', 'media-vault'); ?></label>
<select class="mgjp_mv_permission_select"
id="attachments[<?php echo $post->ID; ?>][mgjp_mv_permission_select]"
name="attachments[<?php echo $post->ID; ?>][mgjp_mv_permission_select]">
<?php foreach ($permissions as $key => $data) : ?>
<option value="<?php echo esc_attr($key); ?>" <?php selected($permission, $key); ?>>
<?php echo esc_html($data['select']); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<script>
jQuery(function ($) {
$('#mgjp_mv_attachment_fields').trigger('mgjpMvLoaded', <?php echo $post->ID; ?>);
}(jQuery));
</script>
</div>
<?php
$form_fields['mgjp_mv_permission_fields']['html'] = ob_get_clean();
$form_fields["mgjp_mv_permission_fields"]["label"] = esc_html('Media Vault Protection Settings', 'media-vault');
$form_fields["mgjp_mv_permission_fields"]["input"] = "html";
return $form_fields;
}
add_filter('attachment_fields_to_edit', 'mgjp_mv_add_attachment_edit_fields', 10, 2);
/**
* Saves changes to attachment fields in
* the Media Upload/Library modal
*
* @since 0.8.9
*
* @uses mgjp_mv_move_attachment_from_protected()
* @uses mgjp_mv_move_attachment_to_protected()
* @uses mgjp_mv_get_the_permissions()
*/
function mgjp_mv_save_attachment_edit_fields($post, $attachment)
{
if (!isset($attachment['mgjp_mv_protection_toggle']))
{
return $post;
}
$attachment_id = $post['ID'];
switch ($attachment['mgjp_mv_protection_toggle'])
{
case 'off' :
remove_action('edit_attachment', 'mgjp_mv_save_attachment_metabox_data');
$move = mgjp_mv_move_attachment_from_protected($attachment_id);
add_action('edit_attachment', 'mgjp_mv_save_attachment_metabox_data');
if (is_wp_error($move))
{
return $post;
}
delete_post_meta($attachment_id, '_mgjp_mv_permission');
return $post;
case 'on':
remove_action('edit_attachment', 'mgjp_mv_save_attachment_metabox_data');
$move = mgjp_mv_move_attachment_to_protected($attachment_id);
// $move = false;
add_action('edit_attachment', 'mgjp_mv_save_attachment_metabox_data');
if (is_wp_error($move))
{
return $post;
}
if (!isset($attachment['mgjp_mv_permission_select']) || empty($attachment['mgjp_mv_permission_select']))
{
return $post;
}
$permissions = mgjp_mv_get_the_permissions();
if ('default' == $attachment['mgjp_mv_permission_select'] || !isset($permissions[$attachment['mgjp_mv_permission_select']]))
{
delete_post_meta($attachment_id, '_mgjp_mv_permission');
}
else
{
update_post_meta($attachment_id, '_mgjp_mv_permission', $attachment['mgjp_mv_permission_select']);
}
return $post;
default:
return $post;
}
}
add_filter('attachment_fields_to_save', 'mgjp_mv_save_attachment_edit_fields', 10, 2);