| 3425 | |
| 3426 | /** |
| 3427 | * Check if there are any audio items in the media library. |
| 3428 | * |
| 3429 | * Queries the DB to check whether the media library contains any items of type audio, |
| 3430 | * and caches that expensive query to avoid slowness when saving posts on large sites. |
| 3431 | * |
| 3432 | * @return int Returns 1 or 0 for 'yes' or 'no' |
| 3433 | */ |
| 3434 | function media_has_audio() { |
| 3435 | |
| 3436 | $has_audio = get_transient( 'has_audio' ); |
| 3437 | if ( false === $has_audio ) { |
| 3438 | global $wpdb; |
| 3439 | $has_audio = $wpdb->get_var( " |
| 3440 | SELECT ID |
| 3441 | FROM $wpdb->posts |
| 3442 | WHERE post_type = 'attachment' |
| 3443 | AND post_mime_type LIKE 'audio%' |
| 3444 | LIMIT 1 |
| 3445 | " ); |
| 3446 | $has_audio = $has_audio ? 1 : 0; |
| 3447 | set_transient( 'has_audio', $has_audio ); |
| 3448 | } |
| 3449 | |
| 3450 | return $has_audio; |
| 3451 | |
| 3452 | } |
| 3453 | |
| 3454 | /** |
| 3455 | * Check if there are any video items in the media library. |
| 3456 | * |
| 3457 | * Queries the DB to check whether the media library contains any items of type video, |
| 3458 | * and caches that expensive query to avoid slowness when saving posts on large sites. |
| 3459 | * |
| 3460 | * @return int Returns 1 or 0 for 'yes' or 'no' |
| 3461 | */ |
| 3462 | function media_has_video() { |
| 3463 | |
| 3464 | $has_video = get_transient( 'has_video' ); |
| 3465 | if ( false === $has_video ) { |
| 3466 | global $wpdb; |
| 3467 | $has_video = $wpdb->get_var( " |
| 3468 | SELECT ID |
| 3469 | FROM $wpdb->posts |
| 3470 | WHERE post_type = 'attachment' |
| 3471 | AND post_mime_type LIKE 'video%' |
| 3472 | LIMIT 1 |
| 3473 | " ); |
| 3474 | $has_video = $has_video ? 1 : 0; |
| 3475 | set_transient( 'has_video', $has_video ); |
| 3476 | } |
| 3477 | |
| 3478 | return $has_video; |
| 3479 | } |
| 3480 | |
| 3481 | /** |
| 3482 | * Gets a list of months in which media has been uploaded |
| 3483 | * |
| 3484 | * Queries the DB to check in which months media items have been uploaded, then |
| 3485 | * caches that query which can be expensive on larger sites |
| 3486 | * |
| 3487 | * @return array An array of objects representing rows from the DB query |
| 3488 | */ |
| 3489 | function media_months() { |
| 3490 | |
| 3491 | $months = get_transient( 'media_months' ); |
| 3492 | if ( false === $months ) { |
| 3493 | global $wpdb; |
| 3494 | $months = $wpdb->get_results( $wpdb->prepare( " |
| 3495 | SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month |
| 3496 | FROM $wpdb->posts |
| 3497 | WHERE post_type = %s |
| 3498 | ORDER BY post_date DESC |
| 3499 | ", 'attachment' ) ); |
| 3500 | set_transient( 'media_months', $months ); |
| 3501 | } |
| 3502 | |
| 3503 | return $months; |
| 3504 | |
| 3505 | } |
| 3506 | |
| 3507 | /** |
| 3508 | * Updates the has_audio and has_video transients as required |
| 3509 | * |
| 3510 | * Hooks into `add_attachment` and `delete_attachment` to determine whether or not |
| 3511 | * the has_audio and has_video transients needs to be refreshed. |
| 3512 | * |
| 3513 | * @param $post_id ID of the attachment post added/deleted |
| 3514 | */ |
| 3515 | function wp_check_has_media( $post_id ) { |
| 3516 | |
| 3517 | $mime_type = get_post_mime_type( $post_id ); |
| 3518 | |
| 3519 | $post_status = get_post_status( $post_id ); |
| 3520 | |
| 3521 | // The value of the transient we should clear, where relevant. |
| 3522 | $transient_to_clear = null; |
| 3523 | |
| 3524 | if ( 'trash' == $post_status ) { |
| 3525 | |
| 3526 | // We're deleting an attachment so we should only refresh the transient |
| 3527 | // if it indicates there are attachments of this type |
| 3528 | $transient_to_clear = 1; |
| 3529 | |
| 3530 | } else { |
| 3531 | |
| 3532 | // We're adding an attachment so we should only refresh the transient |
| 3533 | // if it indicates there are currently no attachments of this type |
| 3534 | $transient_to_clear = 0; |
| 3535 | |
| 3536 | } |
| 3537 | |
| 3538 | // Based on attachment type, clear the relevant transient where necessary |
| 3539 | if ( 'image' == $mime_type && $transient_to_clear == media_has_audio() ){ |
| 3540 | delete_transient( 'has_audio' ); |
| 3541 | } else if ( 'video' == $mime_type && $transient_to_clear === media_has_video() ) { |
| 3542 | delete_transient( 'has_video' ); |
| 3543 | } |
| 3544 | |
| 3545 | |
| 3546 | } |
| 3547 | No newline at end of file |