PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.3.3
Responsive Lightbox & Gallery v2.3.3
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / includes / class-folders.php
responsive-lightbox / includes Last commit date
providers 4 years ago class-fast-image.php 8 years ago class-folders-walker.php 7 years ago class-folders.php 4 years ago class-frontend.php 4 years ago class-galleries.php 4 years ago class-multilang.php 4 years ago class-remote-library-api.php 4 years ago class-remote-library.php 4 years ago class-settings.php 4 years ago class-tour.php 4 years ago class-welcome.php 4 years ago class-widgets.php 4 years ago functions.php 4 years ago
class-folders.php
1232 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Responsive Lightbox folders class.
8 *
9 * @class Responsive_Lightbox_Folders
10 */
11 class Responsive_Lightbox_Folders {
12
13 private $mode = '';
14 private $term_counters = array(
15 'keys' => array(),
16 'values' => array()
17 );
18
19 /**
20 * Class constructor.
21 *
22 * @param bool $read_only Whether run this in read only mode
23 * @return void
24 */
25 public function __construct( $read_only = false ) {
26 // set instance
27 Responsive_Lightbox()->folders = $this;
28
29 // allow to load old taxonomies even in read only mode
30 add_action( 'wp_ajax_rl-folders-load-old-taxonomies', array( $this, 'load_old_taxonomies' ) );
31
32 if ( $read_only )
33 return;
34
35 // actions
36 add_action( 'init', array( $this, 'detect_library_mode' ), 11 );
37 add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) );
38 add_action( 'wp_enqueue_media', array( $this, 'add_library_scripts' ) );
39 add_action( 'admin_enqueue_scripts', array( $this, 'add_library_scripts' ) );
40 add_action( 'pre-upload-ui', array( $this, 'pre_upload_ui' ) );
41 add_action( 'post-upload-ui', array( $this, 'post_upload_ui' ) );
42 add_action( 'add_attachment', array( $this, 'add_attachment' ) );
43 add_action( 'wp_ajax_save-attachment-compat', array( $this, 'ajax_save_attachment_compat' ), 0 );
44 add_action( 'wp_ajax_rl-folders-delete-term', array( $this, 'delete_term' ) );
45 add_action( 'wp_ajax_rl-folders-rename-term', array( $this, 'rename_term' ) );
46 add_action( 'wp_ajax_rl-folders-add-term', array( $this, 'add_term' ) );
47 add_action( 'wp_ajax_rl-folders-move-term', array( $this, 'move_term' ) );
48 add_action( 'wp_ajax_rl-folders-get-terms', array( $this, 'get_terms' ) );
49 add_action( 'wp_ajax_rl-folders-move-attachments', array( $this, 'move_attachments' ) );
50
51 // filters
52 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
53 add_filter( 'parse_query', array( $this, 'parse_query' ) );
54 add_filter( 'ajax_query_attachments_args', array( $this, 'ajax_query_attachments_args' ) );
55 add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
56 add_filter( 'rl_count_attachments', array( $this, 'count_attachments' ), 10 );
57 }
58
59 /**
60 * Load previously used media taxonomies via AJAX.
61 *
62 * @return void
63 */
64 public function load_old_taxonomies() {
65 if ( isset( $_POST['taxonomies'], $_POST['nonce'] ) && is_array( $_POST['taxonomies'] ) && wp_verify_nonce( $_POST['nonce'], 'rl-folders-ajax-taxonomies-nonce' ) ) {
66 $fields = $this->get_taxonomies();
67
68 // any results?
69 if ( ! empty( $fields ) ) {
70 // remove main taxonomy
71 if ( ( $key = array_search( 'rl_media_folder', $fields, true ) ) !== false )
72 unset( $fields[$key] );
73
74 // remove media tags
75 if ( ( $key = array_search( 'rl_media_tag', $fields, true ) ) !== false )
76 unset( $fields[$key] );
77
78 foreach ( $_POST['taxonomies'] as $taxonomy ) {
79 // remove available taxonomy
80 if ( ( $key = array_search( $taxonomy, $fields, true ) ) !== false )
81 unset( $fields[$key] );
82 }
83 }
84
85 // send taxonomies, reindex them to avoid casting to an object in js
86 wp_send_json_success( array( 'taxonomies' => array_values( $fields ) ) );
87 }
88
89 // send JSON error
90 wp_send_json_error();
91 }
92
93 /**
94 * Detect library mode (list or grid).
95 *
96 * @global string $pagenow Current page
97 * @return void
98 */
99 public function detect_library_mode() {
100 global $pagenow;
101
102 if ( $pagenow === 'upload.php' ) {
103 // available modes
104 $modes = array( 'grid', 'list' );
105
106 // check $_GET mode
107 if ( isset( $_GET['mode'] ) && in_array( $_GET['mode'], $modes, true ) ) {
108 $mode = $_GET['mode'];
109 } else {
110 // get user mode
111 $user_mode = get_user_option( 'media_library_mode' );
112
113 // valid user mode?
114 if ( in_array( $user_mode, $modes, true ) )
115 $mode = $user_mode;
116 // default wp mode
117 else
118 $mode = 'grid';
119 }
120
121 // store mode
122 $this->mode = $mode;
123 }
124
125 if ( $pagenow === 'upload.php' || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
126 $this->rl_media_tag_terms = get_terms(
127 array(
128 'taxonomy' => 'rl_media_tag',
129 'hide_empty' => false,
130 'orderby' => 'name',
131 'order' => 'asc',
132 'number' => 0,
133 'fields' => 'id=>name',
134 'hierarchical' => false
135 )
136 );
137 }
138 }
139
140 /**
141 * Admin body classes.
142 *
143 * @global string $pagenow Current page
144 * @param array $classes Admin body classes
145 * @return array Changed classes
146 */
147 public function admin_body_class( $classes ) {
148 global $pagenow;
149
150 if ( $pagenow === 'upload.php' ) {
151 // append class
152 $classes .= ' rl-folders-upload-' . $this->mode . '-mode';
153 }
154
155 return $classes;
156 }
157
158 /**
159 * Get folders dropdown HTML.
160 *
161 * @param string $taxonomy Folders taxonomy
162 * @param string $selected Folders taxonomy ID
163 * @return string
164 */
165 private function get_folders( $taxonomy, $selected = 0 ) {
166 // get only 1 term to check if taxonomy is empty
167 $any_terms = get_terms(
168 array(
169 'taxonomy' => $taxonomy,
170 'hide_empty' => false,
171 'fields' => 'ids',
172 'hierarchical' => false,
173 'number' => 1
174 )
175 );
176
177 // prepare dropdown categories parameters
178 $args = array(
179 'orderby' => 'name',
180 'order' => 'asc',
181 'show_option_all' => __( 'Root Folder', 'responsive-lightbox' ),
182 'show_count' => false,
183 'hide_empty' => false,
184 'hierarchical' => true,
185 'hide_if_empty' => false,
186 'echo' => false,
187 'selected' => (int) $selected,
188 'id' => 'rl_folders_upload_files',
189 'name' => 'rl_folders_upload_files_term_id',
190 'taxonomy' => $taxonomy
191 );
192
193 // no terms?
194 if ( ! is_wp_error( $any_terms ) && empty( $any_terms ) ) {
195 $args['show_option_none'] = __( 'Root Folder', 'responsive-lightbox' );
196 $args['option_none_value'] = 0;
197 }
198
199 return wp_dropdown_categories( $args );
200 }
201
202 /**
203 * Add filter to add media folder id to the uploader
204 *
205 * @return void
206 */
207 public function pre_upload_ui() {
208 add_filter( 'upload_post_params', array( $this, 'upload_post_params' ) );
209 }
210
211 /**
212 * Add media folder id param to the uploader
213 *
214 * @param array $params Plupload parameters
215 * @return array
216 */
217 public function upload_post_params( $params ) {
218 $params['rl_folders_upload_files_term_id'] = 0;
219
220 return $params;
221 }
222
223 /**
224 * Display dropdown at media upload UI screen.
225 *
226 * @return void
227 */
228 public function post_upload_ui() {
229 // get taxonomy
230 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
231
232 // get only 1 term to check if taxonomy is empty
233 $any_terms = get_terms(
234 array(
235 'taxonomy' => $taxonomy,
236 'hide_empty' => false,
237 'fields' => 'ids',
238 'hierarchical' => false,
239 'number' => 1
240 )
241 );
242
243 // prepare dropdown categories parameters
244 $args = array(
245 'orderby' => 'name',
246 'order' => 'asc',
247 'show_option_all' => __( 'Root Folder', 'responsive-lightbox' ),
248 'show_count' => false,
249 'hide_empty' => false,
250 'hierarchical' => true,
251 'hide_if_empty' => false,
252 'echo' => false,
253 'selected' => isset( $_GET[$taxonomy] ) ? (int) $_GET[$taxonomy] : 0,
254 'id' => 'rl_folders_upload_files',
255 'name' => 'rl_folders_upload_files_term_id',
256 'taxonomy' => $taxonomy
257 );
258
259 // no terms?
260 if ( ! is_wp_error( $any_terms ) && empty( $any_terms ) ) {
261 $args['show_option_none'] = __( 'Root Folder', 'responsive-lightbox' );
262 $args['option_none_value'] = 0;
263 }
264
265 // display select
266 echo '<p><label>' . __( 'Upload files to', 'responsive-lightbox' ) . ': ' . wp_dropdown_categories( $args ) . '</label></p>';
267 }
268
269 /**
270 * Assign attachment to given term.
271 *
272 * @param int $post_id Current attachment ID
273 * @return void
274 */
275 public function add_attachment( $post_id ) {
276 if ( isset( $_POST['rl_folders_upload_files_term_id'] ) ) {
277 // cast term id
278 $term_id = (int) $_POST['rl_folders_upload_files_term_id'];
279
280 // get taxonomy
281 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
282
283 // valid term?
284 if ( is_array( term_exists( $term_id, $taxonomy ) ) )
285 wp_set_object_terms( $post_id, $term_id, $taxonomy, false );
286 }
287 }
288
289 /**
290 * Add filterable dropdown to media library.
291 *
292 * @global string $pagenow Current page
293 * @return void
294 */
295 public function restrict_manage_posts() {
296 global $pagenow;
297
298 if ( $pagenow === 'upload.php' ) {
299 // get taxonomy
300 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
301
302 $html = wp_dropdown_categories(
303 array(
304 'orderby' => 'name',
305 'order' => 'asc',
306 'id' => 'media-attachment-rl-folders-filters',
307 'show_option_all' => __( 'All Files', 'responsive-lightbox' ),
308 'show_count' => false,
309 'hide_empty' => false,
310 'hierarchical' => true,
311 'selected' => ( isset( $_GET[$taxonomy] ) ? (int) $_GET[$taxonomy] : 0 ),
312 'name' => $taxonomy,
313 'taxonomy' => $taxonomy,
314 'hide_if_empty' => true,
315 'echo' => false
316 )
317 );
318
319 echo ( $html === '' ? '<select name="' . $taxonomy . '" id="media-attachment-rl-folders-filters" class="postform"><option>' . __( 'All Files', 'responsive-lightbox' ) . '</option></select> ' : $html );
320 }
321 }
322
323 /**
324 * Change query to adjust taxonomy if needed.
325 *
326 * @global string $pagenow Current page
327 * @param object $query WP Query
328 * @return object Modified query
329 */
330 public function parse_query( $query ) {
331 global $pagenow;
332
333 // get taxonomy
334 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
335
336 if ( $pagenow === 'upload.php' && isset( $_GET[$taxonomy] ) ) {
337 // get tax query
338 $tax_query = $query->get( 'tax_query' );
339
340 if ( empty( $tax_query ) || ! is_array( $tax_query ) )
341 $tax_query = array();
342
343 // -1 === root, 0 === all files, >0 === term_id
344 $term_id = (int) $_GET[$taxonomy];
345
346 if ( $term_id !== 0 && ( $query->is_main_query() || empty( $query->query['rl_folders_root'] ) ) ) {
347 $tax = array(
348 'taxonomy' => $taxonomy,
349 'field' => 'id'
350 );
351
352 // root folder?
353 if ( $term_id === -1 ) {
354 $tax['terms'] = 0;
355 $tax['operator'] = 'NOT EXISTS';
356 $tax['include_children'] = false;
357 // specified term id
358 } else {
359 $tax['terms'] = $term_id;
360 $tax['include_children'] = false;
361 }
362
363 // add new tax query
364 $tax_query[] = array( 'relation' => 'AND', $tax );
365
366 // set new tax query
367 $query->set( 'tax_query', $tax_query );
368 }
369 }
370
371 return $query;
372 }
373
374 /**
375 * Change AJAX query parameters to adjust taxonomy in the media library if needed.
376 *
377 * @param array $query Query arguments
378 * @return array Modified query arguments
379 */
380 public function ajax_query_attachments_args( $query ) {
381 // get taxonomy
382 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
383
384 if ( isset( $_POST['query'][$taxonomy] ) ) {
385 if ( $_POST['query'][$taxonomy] === 'all' )
386 return $query;
387
388 $term_id = (int) $_POST['query'][$taxonomy];
389
390 if ( $term_id < 0 )
391 return $query;
392
393 if ( empty( $query['tax_query'] ) || ! is_array( $query['tax_query'] ) )
394 $query['tax_query'] = array();
395
396 $query['tax_query'][] = array(
397 'relation' => 'AND',
398 array(
399 'taxonomy' => $taxonomy,
400 'field' => 'id',
401 'terms' => $term_id,
402 'include_children' => ( ! ( isset( $_POST['query']['include_children'] ) && $_POST['query']['include_children'] === 'false' ) ),
403 'operator' => ( $term_id === 0 ? 'NOT EXISTS' : 'IN' )
404 )
405 );
406 }
407
408 return $query;
409 }
410
411 /**
412 * Filter the array of attachment fields that are displayed when editing an attachment.
413 *
414 * @param array $fields Attachment fields
415 * @param object $post Post object
416 * @return array Modified attachment fields
417 */
418 function attachment_fields_to_edit( $fields, $post ) {
419 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
420 // get taxonomy option
421 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
422
423 // get taxonomy object
424 $tax = (array) get_taxonomy( $taxonomy );
425
426 if ( ! empty( $tax ) ) {
427 if ( ! $tax['public'] || ! $tax['show_ui'] )
428 return $fields;
429
430 if ( empty( $tax['args'] ) )
431 $tax['args'] = array();
432
433 $ids = wp_get_post_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
434
435 // get select HTML
436 $dropdown = wp_dropdown_categories(
437 array(
438 'orderby' => 'name',
439 'order' => 'asc',
440 'show_option_none' => __( 'Root Folder', 'responsive-lightbox' ),
441 'show_option_all' => false,
442 'show_count' => false,
443 'hide_empty' => false,
444 'hierarchical' => true,
445 'selected' => ( ! empty( $ids ) ? reset( $ids ) : 0 ),
446 'name' => $taxonomy . '_term',
447 'taxonomy' => $taxonomy,
448 'hide_if_empty' => false,
449 'echo' => false
450 )
451 );
452
453 $tax['input'] = 'html';
454 $tax['html'] = $dropdown;
455
456 $fields[$taxonomy] = $tax;
457 }
458
459 if ( Responsive_Lightbox()->options['folders']['media_tags'] && taxonomy_exists( 'rl_media_tag' ) ) {
460 // get taxonomy object
461 $tax = (array) get_taxonomy( 'rl_media_tag' );
462
463 if ( ! empty( $tax ) ) {
464 if ( ! $tax['public'] || ! $tax['show_ui'] )
465 return $fields;
466
467 if ( empty( $tax['args'] ) )
468 $tax['args'] = array();
469
470 $tags_html = '';
471
472 // get terms
473 $tags = wp_get_post_terms( $post->ID, 'rl_media_tag', array( 'fields' => 'id=>name' ) );
474
475 // valid terms?
476 if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) {
477 foreach ( $tags as $tag_name ) {
478 $tags_html .= '<option value="' . $tag_name . '" selected="selected">' . esc_html( $tag_name ) . '</li>';
479 }
480 } else
481 $tags = array();
482
483 // update input
484 $tax['input'] = 'html';
485 $tax['html'] = '
486 <select class="rl-media-tag-select2" multiple="multiple" name="attachments[' . $post->ID . '][rl_media_tag]">
487 ' . $tags_html . '
488 </select>';
489
490 // update taxonomy
491 $fields['rl_media_tag'] = $tax;
492 }
493 }
494 }
495
496 return $fields;
497 }
498
499 /**
500 * Assign new term IDs to given attachment ID via AJAX in modal attachment edit screen.
501 *
502 * @return void
503 */
504 function ajax_save_attachment_compat() {
505 if ( ! isset( $_REQUEST['id'] ) || ( $id = (int) $_REQUEST['id'] ) <= 0 || empty( $_REQUEST['attachments'] ) || empty( $_REQUEST['attachments'][$id] ) )
506 wp_send_json_error();
507
508 check_ajax_referer( 'update-post_' . $id, 'nonce' );
509
510 if ( ! current_user_can( 'edit_post', $id ) )
511 wp_send_json_error();
512
513 $post = get_post( $id, ARRAY_A );
514
515 if ( $post['post_type'] !== 'attachment' )
516 wp_send_json_error();
517
518 // update attachment data if needed
519 $post = apply_filters( 'attachment_fields_to_save', $post, $_REQUEST['attachments'][$id] );
520
521 if ( isset( $post['errors'] ) )
522 wp_send_json_error();
523
524 // update attachment
525 wp_update_post( $post );
526
527 // get taxonomy
528 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
529
530 // first if needed?
531 if ( isset( $_REQUEST['attachments'][$id][$taxonomy] ) )
532 wp_set_object_terms( $id, (int) reset( array_map( 'trim', $_REQUEST['attachments'][$id][$taxonomy] ) ), $taxonomy, false );
533 elseif ( isset( $_REQUEST[$taxonomy . '_term'] ) )
534 wp_set_object_terms( $id, (int) $_REQUEST[$taxonomy . '_term'], $taxonomy, false );
535 else
536 wp_set_object_terms( $id, '', $taxonomy, false );
537
538 // check media tags
539 if ( isset( $_REQUEST['attachments'][$id]['rl_media_tag'] ) ) {
540 $media_tags = explode( ',', $_REQUEST['attachments'][$id]['rl_media_tag'] );
541
542 if ( is_array( $media_tags ) )
543 $media_tags = array_filter( array_map( 'trim', $media_tags ) );
544
545 // any media tags?
546 if ( ! empty( $media_tags ) ) {
547 wp_set_object_terms( $id, $media_tags, 'rl_media_tag', false );
548 } else {
549 wp_set_object_terms( $id, '', 'rl_media_tag', false );
550 }
551 }
552
553 if ( ! ( $attachment = wp_prepare_attachment_for_js( $id ) ) )
554 wp_send_json_error();
555
556 // send JSON success
557 wp_send_json_success( $attachment );
558 }
559
560 /**
561 * Assign new term IDs to given attachment ID via AJAX in modal attachment edit screen.
562 *
563 * @return void
564 */
565 function get_terms() {
566 $tags_html = '';
567
568 $tags = wp_get_post_terms( $post->ID, 'rl_media_tag', array( 'fields' => 'id=>name' ) );
569
570 if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) {
571 foreach ( $tags as $tag_id => $tag_name ) {
572 $tags_html .= '<option value="' . $tag_id . '" selected="selected">' . esc_html( $tag_name ) . '</li>';
573 }
574 } else
575 $tags = array();
576
577 if ( ! empty( $this->rl_media_tag_terms ) ) {
578 foreach ( $this->rl_media_tag_terms as $tag_id => $tag_name ) {
579 if ( ! array_key_exists( $tag_id, $tags ) )
580 $tags_html .= '<option value="' . $tag_id . '">' . esc_html( $tag_name ) . '</li>';
581 }
582 }
583 }
584
585 /**
586 * AJAX action to delete term.
587 *
588 * @return void
589 */
590 public function delete_term() {
591 if ( isset( $_POST['term_id'], $_POST['nonce'], $_POST['children'] ) && wp_verify_nonce( $_POST['nonce'], 'rl-folders-ajax-library-nonce' ) && ( $term_id = (int) $_POST['term_id'] ) > 0 ) {
592 // get taxonomy
593 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
594
595 // delete children?
596 if ( $_POST['children'] === '1' ) {
597 // get term children
598 $children = get_term_children( $term_id, $taxonomy );
599
600 // found any children?
601 if ( ! empty( $children ) && ! is_wp_error( $children ) ) {
602 // reverse array to delete terms with no children first
603 foreach ( array_reverse( $children ) as $child_id ) {
604 // delete child
605 wp_delete_term( $child_id, $taxonomy );
606 }
607 }
608 }
609
610 // delete parent
611 if ( ! is_wp_error( wp_delete_term( $term_id, $taxonomy ) ) )
612 wp_send_json_success( $this->get_folders( $taxonomy ) );
613 }
614
615 // send JSON error
616 wp_send_json_error();
617 }
618
619 /**
620 * AJAX action to assign new parent of the term.
621 *
622 * @return void
623 */
624 public function move_term() {
625 // get taxonomy
626 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
627
628 if ( isset( $_POST['parent_id'], $_POST['term_id'], $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'rl-folders-ajax-library-nonce' ) && ! is_wp_error( wp_update_term( (int) $_POST['term_id'], $taxonomy, array( 'parent' => (int) $_POST['parent_id'] ) ) ) )
629 wp_send_json_success( $this->get_folders( $taxonomy ) );
630
631 // send JSON error
632 wp_send_json_error();
633 }
634
635 /**
636 * AJAX action to add new term.
637 *
638 * @return void
639 */
640 public function add_term() {
641 if ( isset( $_POST['parent_id'], $_POST['name'], $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'rl-folders-ajax-library-nonce' ) ) {
642 // get taxonomy
643 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
644
645 // prepare data
646 $original_slug = $slug = sanitize_title( $_POST['name'] );
647 $parent_id = (int) $_POST['parent_id'];
648
649 // get all term slugs
650 $terms = get_terms(
651 array(
652 'taxonomy' => $taxonomy,
653 'hide_empty' => false,
654 'number' => 0,
655 'fields' => 'id=>slug',
656 'hierarchical' => true
657 )
658 );
659
660 // any terms?
661 if ( ! is_wp_error( $terms ) && is_array( $terms ) && ! empty( $terms ) ) {
662 $i = 2;
663
664 // slug already exists? create unique one
665 while ( in_array( $slug, $terms, true ) ) {
666 $slug = $original_slug . '-' . $i ++;
667 }
668 }
669
670 // add new term
671 $term = wp_insert_term(
672 $_POST['name'],
673 $taxonomy,
674 array(
675 'parent' => $parent_id,
676 'slug' => $slug
677 )
678 );
679
680 // no errors?
681 if ( ! is_wp_error( $term ) ) {
682 $term = get_term( $term['term_id'], $taxonomy );
683
684 // no errors?
685 if ( ! is_wp_error( $term ) )
686 wp_send_json_success( array( 'name' => $term->name, 'term_id' => $term->term_id, 'url' => admin_url( 'upload.php?mode=' . $this->mode . '&' . $taxonomy . '=' . $term->term_id ), 'select' => $this->get_folders( $taxonomy, $term->term_id ) ) );
687 }
688 }
689
690 // send JSON error
691 wp_send_json_error();
692 }
693
694 /**
695 * AJAX action to rename term.
696 *
697 * @return void
698 */
699 public function rename_term() {
700 if ( isset( $_POST['term_id'], $_POST['name'], $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'rl-folders-ajax-library-nonce' ) && ( $term_id = (int) $_POST['term_id'] ) > 0 ) {
701 // get taxonomy
702 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
703
704 // update term
705 if ( ! is_wp_error( wp_update_term( $term_id, $taxonomy, array( 'name' => $_POST['name'] ) ) ) ) {
706 $term = get_term( $term_id, $taxonomy );
707
708 // no errors?
709 if ( ! is_wp_error( $term ) )
710 wp_send_json_success( array( 'name' => $term->name, 'select' => $this->get_folders( $taxonomy, $term_id ) ) );
711 }
712 }
713
714 // send JSON error
715 wp_send_json_error();
716 }
717
718 /**
719 * AJAX action to assign new term to an attachment(s).
720 *
721 * @return void
722 */
723 public function move_attachments() {
724 if ( isset( $_POST['attachment_ids'], $_POST['old_term_id'], $_POST['new_term_id'], $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'rl-folders-ajax-library-nonce' ) && is_array( $_POST['attachment_ids'] ) && ! empty( $_POST['attachment_ids'] ) ) {
725 // get taxonomy
726 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
727
728 // prepare data
729 $ids = $all_terms = array();
730 $attachments = array(
731 'success' => array(),
732 'failure' => array(),
733 'duplicated' => array()
734 );
735
736 // get only numeric values first
737 foreach ( $_POST['attachment_ids'] as $id ) {
738 if ( is_numeric( $id ) )
739 $ids[] = (int) $id;
740 }
741
742 // filter unwanted data
743 $ids = array_unique( array_filter( $ids ) );
744
745 // no ids?
746 if ( empty( $ids ) )
747 wp_send_json_error();
748
749 // prepare term ids
750 $old_term_id = (int) $_POST['old_term_id'];
751 $new_term_id = (int) $_POST['new_term_id'];
752
753 // moving to root folder?
754 if ( $new_term_id === 0 ) {
755 foreach ( $ids as $id ) {
756 // get attachment's term ids
757 $all_terms[$id] = wp_get_object_terms( $id, $taxonomy, array( 'fields' => 'ids' ) );
758
759 // remove all terms assigned to attachment
760 if ( ! is_wp_error( wp_set_object_terms( $id, null, $taxonomy, false ) ) )
761 $attachments['success'][] = $id;
762 else
763 $attachments['failure'][] = $id;
764 }
765 } else {
766 foreach ( $ids as $id ) {
767 // get attachment's term ids
768 $terms = wp_get_object_terms( $id, $taxonomy, array( 'fields' => 'ids' ) );
769
770 // got terms?
771 if ( ! is_wp_error( $terms ) ) {
772 // save existing term (attachment already assigned to this term)
773 if ( in_array( $new_term_id, $terms, true ) )
774 $attachments['duplicated'][] = $id;
775
776 // update attachment's term
777 if ( ! is_wp_error( wp_set_object_terms( $id, $new_term_id, $taxonomy, false ) ) )
778 $attachments['success'][] = $id;
779 else
780 $attachments['failure'][] = $id;
781 }
782 }
783 }
784
785 if ( ! empty( $attachments['success'] ) )
786 wp_send_json_success( array( 'attachments' => $attachments, 'terms' => $all_terms ) );
787 }
788
789 // send JSON error
790 wp_send_json_error();
791 }
792
793 /**
794 * Change wp_list_categories HTML link.
795 *
796 * @param array $matches Matched elements
797 * @return string Changed link with term ID
798 */
799 public function replace_folders_href( $matches ) {
800 // get taxonomy
801 $taxonomy = Responsive_Lightbox()->options['folders']['media_taxonomy'];
802
803 // set 'all files' folder
804 $term_id = -1;
805 $url_term_id = 0;
806
807 // any matches?
808 if ( ! empty( $matches[1] ) ) {
809 $params = parse_url( html_entity_decode( urldecode( $matches[1] ) ) );
810
811 if ( isset( $params['query'] ) ) {
812 // parse query string
813 parse_str( $params['query'], $atts );
814
815 if ( isset( $atts['term'] ) ) {
816 // get term
817 $term = get_term_by( 'slug', $atts['term'], $taxonomy );
818
819 // valid term?
820 if ( $term !== false ) {
821 $this->term_counters['keys'][] = $term->term_id;
822
823 // set term ID
824 $url_term_id = $term_id = $term->term_id;
825 }
826 }
827 }
828 }
829
830 return 'href="' . esc_url( apply_filters( 'rl_folders_media_folder_url', add_query_arg( array( 'mode' => $this->mode, $taxonomy => $url_term_id ) ), $matches, $this->mode, $url_term_id ) ) . '" data-term_id="' . $term_id . '"';
831 }
832
833 /**
834 * Change wp_list_categories HTML link by adding attachment counter.
835 *
836 * @param array $matches Matched elements
837 * @return string Changed link with counter
838 */
839 public function replace_folders_count( $matches ) {
840 if ( isset( $matches[1] ) ) {
841 $count = (int) str_replace( array( ' ', '&nbsp;' ), '', $matches[1] );
842 $this->term_counters['values'][] = $count;
843
844 return ' (' . $count . ')</a>';
845 }
846
847 return '</a>';
848 }
849
850 /**
851 * Change wp_list_categories HTML output by adding jsTree attributes if needed.
852 *
853 * @param array $matches Matched elements
854 * @return string Changed element
855 */
856 public function open_folders( $matches ) {
857 if ( isset( $matches[0] ) ) {
858 // open parent term
859 if ( isset( $matches[0] ) && strpos( $matches[0], 'current-cat-ancestor' ) !== false )
860 return $matches[0] . ' data-jstree=\'{ "opened": true }\'';
861
862 // select current term
863 if ( strpos( $matches[0], 'current-cat' ) !== false )
864 return $matches[0] . ' data-jstree=\'{ "selected": true }\'';
865 }
866 }
867
868 /**
869 * Enqueue all needed scripts and styles for media library and modal screens.
870 *
871 * @global string $pagenow Current page
872 * @global string $wp_list_table WP List Table instance
873 * @param string $page Current page similar to $pagenow depends on from which filter function was called
874 * @return void
875 */
876 public function add_library_scripts( $page ) {
877 // count how many times function was executed, allow this only once
878 static $run = 0;
879
880 // allow only wp media scripts (empty $page), upload.php or media-new.php
881 if ( ! ( ( $page === '' || $page === 'upload.php' || $page === 'media-new.php' ) && $run < 1 ) )
882 return;
883
884 global $pagenow;
885
886 $run++;
887
888 // change page for wp_enqueue_media
889 if ( $page === '' ) {
890 if ( $pagenow === 'upload.php' )
891 $page = 'upload.php';
892 else
893 $page = 'media';
894 }
895
896 // get main instance
897 $rl = Responsive_Lightbox();
898
899 // include select2 styles
900 wp_enqueue_style( 'responsive-lightbox-admin-select2', RESPONSIVE_LIGHTBOX_URL . '/assets/select2/select2' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css', array(), $rl->defaults['version'] );
901
902 // filterable media folders taxonomy
903 $taxonomy = get_taxonomy( $rl->options['folders']['media_taxonomy'] );
904
905 // main script dependencies
906 $dependencies = array( 'jquery', 'underscore', 'jquery-ui-draggable', 'jquery-ui-droppable', 'media-models', 'tags-suggest' );
907
908 // create folder counters
909 $counters = array();
910
911 if ( $page !== 'media' ) {
912 // prepare variables
913 $no_items = '';
914 $childless = false;
915
916 // include styles
917 wp_enqueue_style( 'responsive-lightbox-folders-admin-css', RESPONSIVE_LIGHTBOX_URL . '/css/admin-folders.css' );
918 wp_enqueue_style( 'responsive-lightbox-folders-perfect-scrollbar', RESPONSIVE_LIGHTBOX_URL . '/assets/perfect-scrollbar/perfect-scrollbar' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css' );
919 // wp_enqueue_style( 'responsive-lightbox-folders-jstree', RESPONSIVE_LIGHTBOX_URL . '/assets/jstree/themes/' . $rl->options['folders']['jstree_style'] . '/style' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css' );
920 wp_enqueue_style( 'responsive-lightbox-folders-jstree', RESPONSIVE_LIGHTBOX_URL . '/assets/jstree/themes/default/style' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css' );
921
922 // get color scheme global
923 global $_wp_admin_css_colors;
924
925 // set default color;
926 $color = '0,160,210';
927
928 if ( ! empty( $_wp_admin_css_colors ) ) {
929 // get current admin color scheme name
930 $current_color_scheme = get_user_option( 'admin_color' );
931
932 if ( empty( $current_color_scheme ) )
933 $current_color_scheme = 'fresh';
934
935 if ( isset( $_wp_admin_css_colors[$current_color_scheme] ) )
936 $color = implode( ',', $rl->hex2rgb( $_wp_admin_css_colors[$current_color_scheme]->colors[3] ) );
937 }
938
939 wp_add_inline_style(
940 'responsive-lightbox-folders-jstree',
941 '#rl-folders-tree-container .jstree .rl-folders-state-active.rl-folders-state-hover {
942 background: #fff !important;
943 }
944 #rl-folders-tree-container .jstree-container-ul .jstree-wholerow-clicked,
945 #rl-folders-tree-container .jstree-container-ul:not(.jstree-wholerow-ul) .jstree-clicked {
946 background: rgba(' . $color . ', 0.15);
947 }
948 #rl-folders-tree-container .jstree-container-ul .jstree-wholerow-hovered,
949 #rl-folders-tree-container .jstree-container-ul:not(.jstree-wholerow-ul) .jstree-hovered {
950 background: rgba(' . $color . ', 0.05);
951 }'
952 );
953
954 // list categories parameters
955 $categories = array(
956 'orderby' => 'name',
957 'order' => 'asc',
958 'show_count' => true,
959 'show_option_all' => '',
960 'show_option_none' => '',
961 'use_desc_for_title' => false,
962 'title_li' => '',
963 'hide_empty' => false,
964 'hierarchical' => true,
965 'taxonomy' => $taxonomy->name,
966 'hide_title_if_empty' => true,
967 'echo' => false
968 );
969
970 // get current term id
971 $term_id = isset( $_GET[$taxonomy->name] ) ? (int) $_GET[$taxonomy->name] : 0;
972
973 // list mode?
974 if ( $this->mode === 'list' ) {
975 // get global wp list table instance
976 global $wp_list_table;
977
978 // empty instance?
979 if ( is_null( $wp_list_table ) )
980 $wp_list_table = _get_list_table( 'WP_Media_List_Table' );
981
982 // start buffering
983 ob_start();
984
985 // display "no media" table row
986 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $wp_list_table->get_column_count() . '">';
987
988 $wp_list_table->no_items();
989
990 echo '</td></tr>';
991
992 // save "no media" table row
993 $no_items = ob_get_contents();
994
995 // clear the buffer
996 ob_end_clean();
997
998 // valid term?
999 if ( $term_id > 0 ) {
1000 $children = get_term_children( $term_id, $taxonomy->name );
1001
1002 // found any children?
1003 $childless = ! ( ! empty( $children ) && ! is_wp_error( $children ) );
1004 }
1005 }
1006
1007 // set current term id
1008 if ( $term_id > 0 )
1009 $categories['current_category'] = $term_id;
1010
1011 // hide filter for grid
1012 if ( $this->mode !== 'list' ) {
1013 wp_add_inline_style(
1014 'responsive-lightbox-folders-admin-css',
1015 '#media-attachment-rl-folders-filters { display: none; }
1016 .media-modal-content .media-frame select.attachment-filters {
1017 max-width: 100%;
1018 min-width: auto;
1019 }'
1020 );
1021 }
1022
1023 // get taxonomy html output
1024 $html = wp_list_categories( $categories );
1025
1026 if ( $html !== '' ) {
1027 // fix for urls
1028 $html = preg_replace_callback( '/href=(?:\'|")(.*?)(?:\'|")/', array( $this, 'replace_folders_href' ), $html );
1029
1030 // fix for counters
1031 $html = preg_replace_callback( '/<\/a> \(((?:\d+|&nbsp;)+)\)/', array( $this, 'replace_folders_count' ), $html );
1032
1033 // open all needed folders at start
1034 if ( $term_id > 0 )
1035 $html = preg_replace_callback( '/class="cat-item cat-item-(\d+)(?:[a-z\s0-9-]+)?"/', array( $this, 'open_folders' ), $html );
1036
1037 // check whether counters are valid
1038 if ( ! ( empty( $this->term_counters['keys'] ) || empty( $this->term_counters['values'] ) || count( $this->term_counters['keys'] ) !== count( $this->term_counters['values'] ) ) ) {
1039 //@TODO counters are supposed to be used in JS but not implemented yet
1040 // update folder counters
1041 $counters = array_combine( $this->term_counters['keys'], $this->term_counters['values'] );
1042 }
1043 }
1044
1045 // root folder query
1046 $root_query = new WP_Query(
1047 apply_filters(
1048 'rl_root_folder_query_args',
1049 array(
1050 'rl_folders_root' => true,
1051 'posts_per_page' => -1,
1052 'post_type' => 'attachment',
1053 'post_status' => 'inherit,private',
1054 'fields' => 'ids',
1055 'no_found_rows' => false,
1056 'tax_query' => array(
1057 array(
1058 'relation' => 'AND',
1059 array(
1060 'taxonomy' => $taxonomy->name,
1061 'field' => 'id',
1062 'terms' => 0,
1063 'include_children' => false,
1064 'operator' => 'NOT EXISTS'
1065 )
1066 )
1067 )
1068 )
1069 )
1070 );
1071
1072 // set number of all attachments
1073 $counters[-1] = (int) apply_filters( 'rl_count_attachments', 0 );
1074
1075 // set number of root attachments (not categorized)
1076 $counters[0] = (int) $root_query->post_count;
1077
1078
1079 $html = '
1080 <ul>
1081 <li class="cat-item cat-item-all"' . ( $term_id === 0 ? ' data-jstree=\'{ "selected": true }\'' : '' ) . '>
1082 <a href="' . apply_filters( 'rl_folders_media_folder_url', add_query_arg( array( 'mode' => $this->mode, $taxonomy->name => 0 ) ), null, $this->mode, 0 ) . '" data-term_id="all">' . __( 'All Files', 'responsive-lightbox' ) . ' (' . $counters[-1] . ')</a>
1083 </li>
1084 <li class="cat-item cat-item-0" data-jstree=\'{ "opened": true' . ( $term_id === -1 ? ', "selected": true ' : '' ) . ' }\'>
1085 <a href="' . apply_filters( 'rl_folders_media_folder_url', add_query_arg( array( 'mode' => $this->mode, $taxonomy->name => -1 ) ), null, $this->mode, -1 ) . '" data-term_id="0">' . __( 'Root Folder', 'responsive-lightbox' ) . ' (' . $counters[0] . ')</a>
1086 <ul>' . $html . '</ul>
1087 </li>
1088 </ul>';
1089
1090 // register scripts
1091 wp_register_script( 'responsive-lightbox-folders-jstree', RESPONSIVE_LIGHTBOX_URL . '/assets/jstree/jstree' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', array(), $rl->defaults['version'], false );
1092 wp_register_script( 'responsive-lightbox-folders-perfect-scrollbar', RESPONSIVE_LIGHTBOX_URL . '/assets/perfect-scrollbar/perfect-scrollbar' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', array(), $rl->defaults['version'], false );
1093
1094 $dependencies[] = 'responsive-lightbox-folders-jstree';
1095 $dependencies[] = 'responsive-lightbox-folders-perfect-scrollbar';
1096 }
1097
1098 wp_enqueue_script( 'responsive-lightbox-admin-select2', RESPONSIVE_LIGHTBOX_URL . '/assets/select2/select2.full' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', array( 'jquery' ), $rl->defaults['version'], false );
1099
1100 wp_enqueue_script( 'responsive-lightbox-folders-admin', RESPONSIVE_LIGHTBOX_URL . '/js/admin-folders.js', $dependencies, $rl->defaults['version'], false );
1101
1102 if ( $page === 'media' ) {
1103 wp_localize_script(
1104 'responsive-lightbox-folders-admin',
1105 'rlFoldersArgs',
1106 array(
1107 'taxonomy' => $taxonomy->name,
1108 'page' => $page,
1109 'root' => __( 'Root Folder', 'responsive-lightbox' ),
1110 'terms' => wp_dropdown_categories(
1111 array(
1112 'orderby' => 'name',
1113 'order' => 'asc',
1114 'show_option_all' => __( 'All Files', 'responsive-lightbox' ),
1115 'show_count' => false,
1116 'hide_empty' => false,
1117 'hierarchical' => true,
1118 'selected' => ( isset( $_GET[$taxonomy->name] ) ? (int) $_GET[$taxonomy->name] : 0 ),
1119 'name' => $taxonomy->name,
1120 'taxonomy' => $taxonomy->name,
1121 'hide_if_empty' => true,
1122 'echo' => false
1123 )
1124 )
1125 )
1126 );
1127 } else {
1128 wp_localize_script(
1129 'responsive-lightbox-folders-admin',
1130 'rlFoldersArgs',
1131 array(
1132 'remove_children' => (int) $rl->options['folders']['folders_removal'],
1133 'wholerow' => (int) $rl->options['folders']['jstree_wholerow'],
1134 // 'theme' => $rl->options['folders']['jstree_style'],
1135 'theme' => 'default',
1136 'counters' => $counters,
1137 'no_media_items' => $no_items,
1138 'taxonomy' => $taxonomy->name,
1139 'page' => $page,
1140 'root' => __( 'Root Folder', 'responsive-lightbox' ),
1141 'all_terms' => __( 'All Files', 'responsive-lightbox' ),
1142 'new_folder' => __( 'New Folder', 'responsive-lightbox' ),
1143 'delete_term' => __( 'Are you sure you want to delete this folder?', 'responsive-lightbox' ),
1144 'delete_terms' => __( 'Are you sure you want to delete this folder with all subfolders?', 'responsive-lightbox' ),
1145 'nonce' => wp_create_nonce( 'rl-folders-ajax-library-nonce' ),
1146 'terms' => wp_dropdown_categories(
1147 array(
1148 'orderby' => 'name',
1149 'order' => 'asc',
1150 'show_option_all' => __( 'All Files', 'responsive-lightbox' ),
1151 'show_count' => false,
1152 'hide_empty' => false,
1153 'hierarchical' => true,
1154 'selected' => ( isset( $_GET[$taxonomy->name] ) ? (int) $_GET[$taxonomy->name] : 0 ),
1155 'name' => $taxonomy->name,
1156 'taxonomy' => $taxonomy->name,
1157 'hide_if_empty' => true,
1158 'echo' => false
1159 )
1160 ),
1161 'template' => '
1162 <div id="rl-folders-tree-container">
1163 <div class="media-toolbar wp-filter">
1164 <div class="view-switch rl-folders-action-links">
1165 <a href="#" title="' . $taxonomy->labels->add_new_item . '" class="dashicons dashicons-plus rl-folders-add-new-folder' . ( $this->mode === 'list' && ( $term_id === -1 || $term_id > 0 ) ? '' : ' disabled-link' ) . '"></a>
1166 <a href="#" title="' . sprintf( __( 'Save new %s', 'responsive-lightbox' ), $taxonomy->labels->singular_name ) . '" class="dashicons dashicons-yes rl-folders-save-new-folder" style="display: none;"></a>
1167 <a href="#" title="' . sprintf( __( 'Cancel adding new %s', 'responsive-lightbox' ), $taxonomy->labels->singular_name ) . '" class="dashicons dashicons-no rl-folders-cancel-new-folder" style="display: none;"></a>
1168 <a href="#" title="' . $taxonomy->labels->edit_item . '" class="dashicons dashicons-edit rl-folders-rename-folder' . ( $this->mode === 'list' && $term_id > 0 ? '' : ' disabled-link' ) . '"></a>
1169 <a href="#" title="' . sprintf( __( 'Save %s', 'responsive-lightbox' ), $taxonomy->labels->singular_name ) . '" class="dashicons dashicons-yes rl-folders-save-folder" style="display: none;"></a>
1170 <a href="#" title="' . sprintf( __( 'Cancel renaming %s', 'responsive-lightbox' ), $taxonomy->labels->singular_name ) . '" class="dashicons dashicons-no rl-folders-cancel-folder" style="display: none;"></a>
1171 <a href="#" title="' . sprintf( __( 'Delete %s', 'responsive-lightbox' ), $taxonomy->labels->singular_name ) . '" class="dashicons dashicons-trash rl-folders-delete-folder' . ( $this->mode === 'list' && $term_id > 0 ? '' : ' disabled-link' ) . '"></a>
1172 <a href="#" title="' . sprintf( __( 'Expand %s', 'responsive-lightbox' ), $taxonomy->labels->singular_name ) . '" class="dashicons dashicons-arrow-down-alt2 rl-folders-expand-folder' . ( $this->mode === 'list' && ! $childless && ( $term_id === -1 || $term_id > 0 ) ? '' : ' disabled-link' ) . '"></a>
1173 <a href="#" title="' . sprintf( __( 'Collapse %s', 'responsive-lightbox' ), $taxonomy->labels->singular_name ) . '" class="dashicons dashicons-arrow-up-alt2 rl-folders-collapse-folder' . ( $this->mode === 'list' && ! $childless && ( $term_id === -1 || $term_id > 0 ) ? '' : ' disabled-link' ) . '"></a>
1174 </div>
1175 </div>
1176 <div id="rl-folders-tree">' . esc_html( $html ) . '</div>
1177 </div>'
1178 )
1179 );
1180 }
1181
1182 add_action( 'admin_print_styles', array( $this, 'admin_print_media_styles' ) );
1183 }
1184
1185 /**
1186 * CSS fix for media folders checklist.
1187 *
1188 * @return void
1189 */
1190 public function admin_print_media_styles() {
1191 echo '<style>.rl_media_folder li .selectit input[type="checkbox"] { margin: 0 3px; }</style>';
1192 }
1193
1194 /**
1195 * Count attachments.
1196 *
1197 * @return int
1198 */
1199 public function count_attachments() {
1200 $count = wp_count_posts( 'attachment' );
1201
1202 return (int) $count->inherit;
1203 }
1204
1205 /**
1206 * Get all previously used media taxonomies.
1207 *
1208 * @global object $wpdb Database handler
1209 * @return array Old unused taxonomies
1210 */
1211 public function get_taxonomies() {
1212 global $wpdb;
1213
1214 // query
1215 $fields = $wpdb->get_col( '
1216 SELECT DISTINCT tt.taxonomy
1217 FROM ' . $wpdb->prefix . 'term_taxonomy tt
1218 LEFT JOIN ' . $wpdb->prefix . 'term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
1219 LEFT JOIN ' . $wpdb->prefix . 'posts p ON p.ID = tr.object_id
1220 WHERE p.post_type = \'attachment\'
1221 ORDER BY tt.taxonomy ASC'
1222 );
1223
1224 if ( ! empty( $fields ) ) {
1225 // remove polylang taxonomy
1226 if ( ( $key = array_search( 'language', $fields, true ) ) !== false )
1227 unset( $fields[$key] );
1228 }
1229
1230 return $fields;
1231 }
1232 }