Make WordPress Core


Ignore:
Timestamp:
07/05/2018 02:50: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.7 branch.

File:
1 edited

Legend:

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

    r42058 r43395  
    49154915    do_action( 'deleted_post', $post_id );
    49164916
     4917    wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     4918
     4919    clean_post_cache( $post );
     4920
     4921    return $post;
     4922}
     4923
     4924/**
     4925 * Deletes all files that belong to the given attachment.
     4926 *
     4927 * @since 4.9.7
     4928 *
     4929 * @param int    $post_id      Attachment ID.
     4930 * @param array  $meta         The attachment's meta data.
     4931 * @param array  $backup_sizes The meta data for the attachment's backup images.
     4932 * @param string $file         Absolute path to the attachment's file.
     4933 * @return bool True on success, false on failure.
     4934 */
     4935function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     4936    global $wpdb;
     4937
    49174938    $uploadpath = wp_get_upload_dir();
    4918 
    4919     if ( ! empty($meta['thumb']) ) {
     4939    $deleted    = true;
     4940
     4941    if ( ! empty( $meta['thumb'] ) ) {
    49204942        // Don't delete the thumb if another attachment uses it.
    4921         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)) ) {
    4922             $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4923             /** This filter is documented in wp-includes/functions.php */
    4924             $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4925             @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4943        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 ) ) ) {
     4944            $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
     4945            if ( ! empty( $thumbfile ) ) {
     4946                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     4947                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     4948
     4949                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     4950                    $deleted = false;
     4951                }
     4952            }
    49264953        }
    49274954    }
     
    49294956    // Remove intermediate and backup images if there are any.
    49304957    if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4958        $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    49314959        foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    49324960            $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4933             /** This filter is documented in wp-includes/functions.php */
    4934             $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4935             @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
    4936         }
    4937     }
    4938 
    4939     if ( is_array($backup_sizes) ) {
     4961            if ( ! empty( $intermediate_file ) ) {
     4962                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     4963
     4964                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     4965                    $deleted = false;
     4966                }
     4967            }
     4968        }
     4969    }
     4970
     4971    if ( is_array( $backup_sizes ) ) {
     4972        $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    49404973        foreach ( $backup_sizes as $size ) {
    4941             $del_file = path_join( dirname($meta['file']), $size['file'] );
    4942             /** This filter is documented in wp-includes/functions.php */
    4943             $del_file = apply_filters( 'wp_delete_file', $del_file );
    4944             @ unlink( path_join($uploadpath['basedir'], $del_file) );
    4945         }
    4946     }
    4947 
    4948     wp_delete_file( $file );
    4949 
    4950     clean_post_cache( $post );
    4951 
    4952     return $post;
     4974            $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     4975            if ( ! empty( $del_file ) ) {
     4976                $del_file = path_join( $uploadpath['basedir'], $del_file );
     4977
     4978                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     4979                    $deleted = false;
     4980                }
     4981            }
     4982        }
     4983    }
     4984
     4985    if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     4986        $deleted = false;
     4987    }
     4988
     4989    return $deleted;
    49534990}
    49544991
Note: See TracChangeset for help on using the changeset viewer.