Make WordPress Core

Ticket #32264: 32264.5.patch

File 32264.5.patch, 3.6 KB (added by rhurling, 10 years ago)

Add deletion of transients in wp_delete_attachment

  • 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,
  • src/wp-includes/post.php

     
    34773477                         */
    34783478                        do_action( 'edit_attachment', $post_ID );
    34793479                } else {
     3480                        // Delete persistent cache transients set in wp_enqueue_media
     3481                        $mime_type_prefix = substr( $postarr['post_mime_type'], 0, 5 );
     3482                        if ( 'video' === $mime_type_prefix ) {
     3483                                delete_transient( 'has_video' );
     3484                        } else if ( 'audio' === $mime_type_prefix ) {
     3485                                delete_transient( 'has_audio' );
     3486                        }
    34803487
     3488                        delete_transient( 'media_months' );
     3489
    34813490                        /**
    34823491                         * Fires once an attachment has been added.
    34833492                         *
     
    48304839        $meta = wp_get_attachment_metadata( $post_id );
    48314840        $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
    48324841        $file = get_attached_file( $post_id );
     4842        $mime_type_prefix = substr( get_post_field( 'post_mime_type', $post_id ), 0, 5 );
    48334843
    48344844        if ( is_multisite() )
    48354845                delete_transient( 'dirsize_cache' );
     
    48664876        /** This action is documented in wp-includes/post.php */
    48674877        do_action( 'deleted_post', $post_id );
    48684878
     4879        // Delete persistent cache transients set in wp_enqueue_media
     4880        if ( 'video' === $mime_type_prefix ) {
     4881                delete_transient( 'has_video' );
     4882        } else if ( 'audio' === $mime_type_prefix ) {
     4883                delete_transient( 'has_audio' );
     4884        }
     4885
     4886        delete_transient( 'media_months' );
     4887
    48694888        $uploadpath = wp_upload_dir();
    48704889
    48714890        if ( ! empty($meta['thumb']) ) {