Make WordPress Core


Ignore:
Timestamp:
07/05/2018 02:56:26 PM (8 years ago)
Author:
johnbillion
Message:

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

Merges [43393] into the 4.4 branch.

File:
1 edited

Legend:

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

    r42061 r43398  
    47454745    do_action( 'deleted_post', $post_id );
    47464746
     4747    wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     4748
     4749    clean_post_cache( $post );
     4750
     4751    return $post;
     4752}
     4753
     4754/**
     4755 * Deletes all files that belong to the given attachment.
     4756 *
     4757 * @since 4.9.7
     4758 *
     4759 * @param int    $post_id      Attachment ID.
     4760 * @param array  $meta         The attachment's meta data.
     4761 * @param array  $backup_sizes The meta data for the attachment's backup images.
     4762 * @param string $file         Absolute path to the attachment's file.
     4763 * @return bool True on success, false on failure.
     4764 */
     4765function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     4766    global $wpdb;
     4767
    47474768    $uploadpath = wp_upload_dir();
    4748 
    4749     if ( ! empty($meta['thumb']) ) {
     4769    $deleted    = true;
     4770
     4771    if ( ! empty( $meta['thumb'] ) ) {
    47504772        // Don't delete the thumb if another attachment uses it.
    4751         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)) ) {
    4752             $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4753             /** This filter is documented in wp-includes/functions.php */
    4754             $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4755             @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4773        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 ) ) ) {
     4774            $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
     4775            if ( ! empty( $thumbfile ) ) {
     4776                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     4777                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     4778
     4779                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     4780                    $deleted = false;
     4781                }
     4782            }
    47564783        }
    47574784    }
     
    47594786    // Remove intermediate and backup images if there are any.
    47604787    if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4788        $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    47614789        foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    47624790            $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4763             /** This filter is documented in wp-includes/functions.php */
    4764             $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4765             @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
    4766         }
    4767     }
    4768 
    4769     if ( is_array($backup_sizes) ) {
     4791            if ( ! empty( $intermediate_file ) ) {
     4792                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     4793
     4794                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     4795                    $deleted = false;
     4796                }
     4797            }
     4798        }
     4799    }
     4800
     4801    if ( is_array( $backup_sizes ) ) {
     4802        $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    47704803        foreach ( $backup_sizes as $size ) {
    4771             $del_file = path_join( dirname($meta['file']), $size['file'] );
    4772             /** This filter is documented in wp-includes/functions.php */
    4773             $del_file = apply_filters( 'wp_delete_file', $del_file );
    4774             @ unlink( path_join($uploadpath['basedir'], $del_file) );
    4775         }
    4776     }
    4777 
    4778     wp_delete_file( $file );
    4779 
    4780     clean_post_cache( $post );
    4781 
    4782     return $post;
     4804            $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     4805            if ( ! empty( $del_file ) ) {
     4806                $del_file = path_join( $uploadpath['basedir'], $del_file );
     4807
     4808                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     4809                    $deleted = false;
     4810                }
     4811            }
     4812        }
     4813    }
     4814
     4815    if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     4816        $deleted = false;
     4817    }
     4818
     4819    return $deleted;
    47834820}
    47844821
Note: See TracChangeset for help on using the changeset viewer.