WordPress.org

Make WordPress Core

Ticket #32264: 32264.7.patch

File 32264.7.patch, 5.2 KB (added by philipjohn, 4 years ago)
  • wp-includes/default-filters.php

     
    378378// Media
    379379add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' );
    380380add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' );
     381add_action( 'add_attachment', 'wp_check_has_media' );
     382add_action( 'delete_attachment', 'wp_check_has_media' );
    381383
    382384// Nav menu
    383385add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );
  • wp-includes/media.php

     
    29182918                }
    29192919        }
    29202920
    2921         $has_audio = $wpdb->get_var( "
    2922                 SELECT ID
    2923                 FROM $wpdb->posts
    2924                 WHERE post_type = 'attachment'
    2925                 AND post_mime_type LIKE 'audio%'
    2926                 LIMIT 1
    2927         " );
    2928         $has_video = $wpdb->get_var( "
    2929                 SELECT ID
    2930                 FROM $wpdb->posts
    2931                 WHERE post_type = 'attachment'
    2932                 AND post_mime_type LIKE 'video%'
    2933                 LIMIT 1
    2934         " );
    2935         $months = $wpdb->get_results( $wpdb->prepare( "
    2936                 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
    2937                 FROM $wpdb->posts
    2938                 WHERE post_type = %s
    2939                 ORDER BY post_date DESC
    2940         ", 'attachment' ) );
     2921        $has_audio = media_has_audio();
     2922        $has_video = media_has_video();
     2923        $months = media_months();
     2924
    29412925        foreach ( $months as $month_year ) {
    29422926                $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year );
    29432927        }
     
    29562940                ),
    29572941                'defaultProps' => $props,
    29582942                'attachmentCounts' => array(
    2959                         'audio' => ( $has_audio ) ? 1 : 0,
    2960                         'video' => ( $has_video ) ? 1 : 0
     2943                        'audio' => intval( $has_audio ),
     2944                        'video' => intval( $has_video )
    29612945                ),
    29622946                'embedExts'    => $exts,
    29632947                'embedMimes'   => $ext_mimes,
     
    34383422
    34393423        return array( $mediaelement, $wpmediaelement );
    34403424}
     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 */
     3434function 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 */
     3462function 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 */
     3489function 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 */
     3515function 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