Make WordPress Core

Ticket #32264: 32264.8.patch

File 32264.8.patch, 6.1 KB (added by philipjohn, 9 years ago)

Adds media_month query caching

  • wp-includes/default-filters.php

     
    387387// Media
    388388add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' );
    389389add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' );
     390add_action( 'add_attachment', 'wp_check_has_media' );
     391add_action( 'delete_attachment', 'wp_check_has_media' );
     392add_action( 'add_attachment', 'wp_check_media_months' );
    390393
    391394// Nav menu
    392395add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );
  • wp-includes/media.php

     
    29442944                }
    29452945        }
    29462946
    2947         $has_audio = $wpdb->get_var( "
    2948                 SELECT ID
    2949                 FROM $wpdb->posts
    2950                 WHERE post_type = 'attachment'
    2951                 AND post_mime_type LIKE 'audio%'
    2952                 LIMIT 1
    2953         " );
    2954         $has_video = $wpdb->get_var( "
    2955                 SELECT ID
    2956                 FROM $wpdb->posts
    2957                 WHERE post_type = 'attachment'
    2958                 AND post_mime_type LIKE 'video%'
    2959                 LIMIT 1
    2960         " );
    2961         $months = $wpdb->get_results( $wpdb->prepare( "
    2962                 SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
    2963                 FROM $wpdb->posts
    2964                 WHERE post_type = %s
    2965                 ORDER BY post_date DESC
    2966         ", 'attachment' ) );
     2947        $has_audio = media_has_audio();
     2948        $has_video = media_has_video();
     2949        $months = media_months();
     2950
    29672951        foreach ( $months as $month_year ) {
    29682952                $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year );
    29692953        }
     
    29822966                ),
    29832967                'defaultProps' => $props,
    29842968                'attachmentCounts' => array(
    2985                         'audio' => ( $has_audio ) ? 1 : 0,
    2986                         'video' => ( $has_video ) ? 1 : 0
     2969                        'audio' => intval( $has_audio ),
     2970                        'video' => intval( $has_video )
    29872971                ),
    29882972                'embedExts'    => $exts,
    29892973                'embedMimes'   => $ext_mimes,
     
    34703454
    34713455        return array( $mediaelement, $wpmediaelement );
    34723456}
     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 */
     3466function 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 */
     3494function 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 */
     3521function 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 */
     3547function 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
     3580function 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