| | 3457 | |
| | 3458 | /** |
| | 3459 | * Check if there are any audio items in the media library. |
| | 3460 | * |
| | 3461 | * Queries the DB to check whether the media library contains any items of type audio, |
| | 3462 | * and caches that expensive query to avoid slowness when saving posts on large sites. |
| | 3463 | * |
| | 3464 | * @return int Returns 1 or 0 for 'yes' or 'no' |
| | 3465 | */ |
| | 3466 | function media_has_audio() { |
| | 3467 | |
| | 3468 | $has_audio = get_transient( 'has_audio' ); |
| | 3469 | if ( false === $has_audio ) { |
| | 3470 | global $wpdb; |
| | 3471 | $has_audio = $wpdb->get_var( " |
| | 3472 | SELECT ID |
| | 3473 | FROM $wpdb->posts |
| | 3474 | WHERE post_type = 'attachment' |
| | 3475 | AND post_mime_type LIKE 'audio%' |
| | 3476 | LIMIT 1 |
| | 3477 | " ); |
| | 3478 | $has_audio = $has_audio ? 1 : 0; |
| | 3479 | set_transient( 'has_audio', $has_audio ); |
| | 3480 | } |
| | 3481 | |
| | 3482 | return $has_audio; |
| | 3483 | |
| | 3484 | } |
| | 3485 | |
| | 3486 | /** |
| | 3487 | * Check if there are any video items in the media library. |
| | 3488 | * |
| | 3489 | * Queries the DB to check whether the media library contains any items of type video, |
| | 3490 | * and caches that expensive query to avoid slowness when saving posts on large sites. |
| | 3491 | * |
| | 3492 | * @return int Returns 1 or 0 for 'yes' or 'no' |
| | 3493 | */ |
| | 3494 | function media_has_video() { |
| | 3495 | |
| | 3496 | $has_video = get_transient( 'has_video' ); |
| | 3497 | if ( false === $has_video ) { |
| | 3498 | global $wpdb; |
| | 3499 | $has_video = $wpdb->get_var( " |
| | 3500 | SELECT ID |
| | 3501 | FROM $wpdb->posts |
| | 3502 | WHERE post_type = 'attachment' |
| | 3503 | AND post_mime_type LIKE 'video%' |
| | 3504 | LIMIT 1 |
| | 3505 | " ); |
| | 3506 | $has_video = $has_video ? 1 : 0; |
| | 3507 | set_transient( 'has_video', $has_video ); |
| | 3508 | } |
| | 3509 | |
| | 3510 | return $has_video; |
| | 3511 | } |
| | 3512 | |
| | 3513 | /** |
| | 3514 | * Gets a list of months in which media has been uploaded |
| | 3515 | * |
| | 3516 | * Queries the DB to check in which months media items have been uploaded, then |
| | 3517 | * caches that query which can be expensive on larger sites |
| | 3518 | * |
| | 3519 | * @return array An array of objects representing rows from the DB query |
| | 3520 | */ |
| | 3521 | function media_months() { |
| | 3522 | |
| | 3523 | $months = get_transient( 'media_months' ); |
| | 3524 | if ( false === $months ) { |
| | 3525 | global $wpdb; |
| | 3526 | $months = $wpdb->get_results( $wpdb->prepare( " |
| | 3527 | SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month |
| | 3528 | FROM $wpdb->posts |
| | 3529 | WHERE post_type = %s |
| | 3530 | ORDER BY post_date DESC |
| | 3531 | ", 'attachment' ) ); |
| | 3532 | set_transient( 'media_months', $months ); |
| | 3533 | } |
| | 3534 | |
| | 3535 | return $months; |
| | 3536 | |
| | 3537 | } |
| | 3538 | |
| | 3539 | /** |
| | 3540 | * Updates the has_audio and has_video transients as required |
| | 3541 | * |
| | 3542 | * Hooks into `add_attachment` and `delete_attachment` to determine whether or not |
| | 3543 | * the has_audio and has_video transients needs to be refreshed. |
| | 3544 | * |
| | 3545 | * @param $post_id ID of the attachment post added/deleted |
| | 3546 | */ |
| | 3547 | function wp_check_has_media( $post_id ) { |
| | 3548 | |
| | 3549 | $mime_type = get_post_mime_type( $post_id ); |
| | 3550 | |
| | 3551 | $post_status = get_post_status( $post_id ); |
| | 3552 | |
| | 3553 | // The value of the transient we should clear, where relevant. |
| | 3554 | $transient_to_clear = null; |
| | 3555 | |
| | 3556 | if ( 'trash' == $post_status ) { |
| | 3557 | |
| | 3558 | // We're deleting an attachment so we should only refresh the transient |
| | 3559 | // if it indicates there are attachments of this type |
| | 3560 | $transient_to_clear = 1; |
| | 3561 | |
| | 3562 | } else { |
| | 3563 | |
| | 3564 | // We're adding an attachment so we should only refresh the transient |
| | 3565 | // if it indicates there are currently no attachments of this type |
| | 3566 | $transient_to_clear = 0; |
| | 3567 | |
| | 3568 | } |
| | 3569 | |
| | 3570 | // Based on attachment type, clear the relevant transient where necessary |
| | 3571 | if ( 'image' == $mime_type && $transient_to_clear == media_has_audio() ){ |
| | 3572 | delete_transient( 'has_audio' ); |
| | 3573 | } else if ( 'video' == $mime_type && $transient_to_clear === media_has_video() ) { |
| | 3574 | delete_transient( 'has_video' ); |
| | 3575 | } |
| | 3576 | |
| | 3577 | |
| | 3578 | } |
| | 3579 | |
| | 3580 | function wp_check_media_months( $post_id ) { |
| | 3581 | |
| | 3582 | // What month/year is the most recent attachment? |
| | 3583 | global $wpdb; |
| | 3584 | $months = $wpdb->get_results( $wpdb->prepare( " |
| | 3585 | SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month |
| | 3586 | FROM $wpdb->posts |
| | 3587 | WHERE post_type = %s |
| | 3588 | ORDER BY post_date DESC |
| | 3589 | LIMIT 1 |
| | 3590 | ", 'attachment' ) ); |
| | 3591 | |
| | 3592 | // Simplify by assigning the object to $months |
| | 3593 | $months = array_shift( array_values( $months ) ); |
| | 3594 | |
| | 3595 | // Compare the dates of the new, and most recent, attachment |
| | 3596 | if ( |
| | 3597 | ! $months->year == get_the_time( 'Y', $post_id ) && |
| | 3598 | ! $months->month == get_the_time( 'm', $post_id ) |
| | 3599 | ) { |
| | 3600 | // the new attachment is not in the same month/year as the |
| | 3601 | // most recent attachment, so we need to refresh the transient |
| | 3602 | delete_transient('media_months'); |
| | 3603 | } |
| | 3604 | |
| | 3605 | } |
| | 3606 | No newline at end of file |