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