Make WordPress Core

Ticket #32264: 32264.3.patch

File 32264.3.patch, 2.2 KB (added by philipjohn, 9 years ago)
  • src/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        // Cache these expensive queries
     2922        if ( false === ( $has_audio = get_transient( 'has_audio' ) ) ) {
     2923                $has_audio = $wpdb->get_var( "
     2924                        SELECT ID
     2925                        FROM $wpdb->posts
     2926                        WHERE post_type = 'attachment'
     2927                        AND post_mime_type LIKE 'audio%'
     2928                        LIMIT 1
     2929                " );
     2930                $has_audio = $has_audio ? 1 : 0;
     2931                set_transient( 'has_audio', $has_audio );
     2932        }
     2933        if ( false === ( $has_video = get_transient( 'has_video' ) ) ) {
     2934                $has_video = $wpdb->get_var( "
     2935                        SELECT ID
     2936                        FROM $wpdb->posts
     2937                        WHERE post_type = 'attachment'
     2938                        AND post_mime_type LIKE 'video%'
     2939                        LIMIT 1
     2940                " );
     2941                $has_video = $has_video ? 1 : 0;
     2942                set_transient( 'has_video', $has_video );
     2943        }
     2944        if ( false === ( $months = get_transient( 'media_months' ) ) ) {
     2945                $months = $wpdb->get_results( $wpdb->prepare( "
     2946                        SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
     2947                        FROM $wpdb->posts
     2948                        WHERE post_type = %s
     2949                        ORDER BY post_date DESC
     2950                ", 'attachment' ) );
     2951                set_transient( 'media_months', $months );
     2952        }
     2953
    29412954        foreach ( $months as $month_year ) {
    29422955                $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year );
    29432956        }
     
    29562969                ),
    29572970                'defaultProps' => $props,
    29582971                'attachmentCounts' => array(
    2959                         'audio' => ( $has_audio ) ? 1 : 0,
    2960                         'video' => ( $has_video ) ? 1 : 0
     2972                        'audio' => intval( $has_audio ),
     2973                        'video' => intval( $has_video )
    29612974                ),
    29622975                'embedExts'    => $exts,
    29632976                'embedMimes'   => $ext_mimes,