Make WordPress Core


Ignore:
Timestamp:
07/05/2018 02:52:09 PM (7 years ago)
Author:
johnbillion
Message:

Media: Limit thumbnail file deletions to the same directory as the original file.

Merges [43393] to the 4.6 branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.6/src/wp-includes/post.php

    r42059 r43396  
    47984798    do_action( 'deleted_post', $post_id );
    47994799
     4800    wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     4801
     4802    clean_post_cache( $post );
     4803
     4804    return $post;
     4805}
     4806
     4807/**
     4808 * Deletes all files that belong to the given attachment.
     4809 *
     4810 * @since 4.9.7
     4811 *
     4812 * @param int    $post_id      Attachment ID.
     4813 * @param array  $meta         The attachment's meta data.
     4814 * @param array  $backup_sizes The meta data for the attachment's backup images.
     4815 * @param string $file         Absolute path to the attachment's file.
     4816 * @return bool True on success, false on failure.
     4817 */
     4818function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     4819    global $wpdb;
     4820
    48004821    $uploadpath = wp_get_upload_dir();
    4801 
    4802     if ( ! empty($meta['thumb']) ) {
     4822    $deleted    = true;
     4823
     4824    if ( ! empty( $meta['thumb'] ) ) {
    48034825        // Don't delete the thumb if another attachment uses it.
    4804         if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) {
    4805             $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4806             /** This filter is documented in wp-includes/functions.php */
    4807             $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4808             @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4826        if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) {
     4827            $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
     4828            if ( ! empty( $thumbfile ) ) {
     4829                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     4830                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     4831
     4832                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     4833                    $deleted = false;
     4834                }
     4835            }
    48094836        }
    48104837    }
     
    48124839    // Remove intermediate and backup images if there are any.
    48134840    if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4841        $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    48144842        foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    48154843            $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4816             /** This filter is documented in wp-includes/functions.php */
    4817             $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4818             @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
    4819         }
    4820     }
    4821 
    4822     if ( is_array($backup_sizes) ) {
     4844            if ( ! empty( $intermediate_file ) ) {
     4845                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     4846
     4847                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     4848                    $deleted = false;
     4849                }
     4850            }
     4851        }
     4852    }
     4853
     4854    if ( is_array( $backup_sizes ) ) {
     4855        $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    48234856        foreach ( $backup_sizes as $size ) {
    4824             $del_file = path_join( dirname($meta['file']), $size['file'] );
    4825             /** This filter is documented in wp-includes/functions.php */
    4826             $del_file = apply_filters( 'wp_delete_file', $del_file );
    4827             @ unlink( path_join($uploadpath['basedir'], $del_file) );
    4828         }
    4829     }
    4830 
    4831     wp_delete_file( $file );
    4832 
    4833     clean_post_cache( $post );
    4834 
    4835     return $post;
     4857            $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     4858            if ( ! empty( $del_file ) ) {
     4859                $del_file = path_join( $uploadpath['basedir'], $del_file );
     4860
     4861                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     4862                    $deleted = false;
     4863                }
     4864            }
     4865        }
     4866    }
     4867
     4868    if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     4869        $deleted = false;
     4870    }
     4871
     4872    return $deleted;
    48364873}
    48374874
Note: See TracChangeset for help on using the changeset viewer.