Make WordPress Core


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

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

Merges [43393] into the 4.5 branch.

File:
1 edited

Legend:

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

    r42060 r43397  
    48964896    do_action( 'deleted_post', $post_id );
    48974897
     4898    wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     4899
     4900    clean_post_cache( $post );
     4901
     4902    return $post;
     4903}
     4904
     4905/**
     4906 * Deletes all files that belong to the given attachment.
     4907 *
     4908 * @since 4.9.7
     4909 *
     4910 * @param int    $post_id      Attachment ID.
     4911 * @param array  $meta         The attachment's meta data.
     4912 * @param array  $backup_sizes The meta data for the attachment's backup images.
     4913 * @param string $file         Absolute path to the attachment's file.
     4914 * @return bool True on success, false on failure.
     4915 */
     4916function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     4917    global $wpdb;
     4918
    48984919    $uploadpath = wp_get_upload_dir();
    4899 
    4900     if ( ! empty($meta['thumb']) ) {
     4920    $deleted    = true;
     4921
     4922    if ( ! empty( $meta['thumb'] ) ) {
    49014923        // Don't delete the thumb if another attachment uses it.
    4902         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)) ) {
    4903             $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4904             /** This filter is documented in wp-includes/functions.php */
    4905             $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4906             @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4924        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 ) ) ) {
     4925            $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
     4926            if ( ! empty( $thumbfile ) ) {
     4927                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     4928                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     4929
     4930                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     4931                    $deleted = false;
     4932                }
     4933            }
    49074934        }
    49084935    }
     
    49104937    // Remove intermediate and backup images if there are any.
    49114938    if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4939        $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    49124940        foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    49134941            $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4914             /** This filter is documented in wp-includes/functions.php */
    4915             $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4916             @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
    4917         }
    4918     }
    4919 
    4920     if ( is_array($backup_sizes) ) {
     4942            if ( ! empty( $intermediate_file ) ) {
     4943                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     4944
     4945                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     4946                    $deleted = false;
     4947                }
     4948            }
     4949        }
     4950    }
     4951
     4952    if ( is_array( $backup_sizes ) ) {
     4953        $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    49214954        foreach ( $backup_sizes as $size ) {
    4922             $del_file = path_join( dirname($meta['file']), $size['file'] );
    4923             /** This filter is documented in wp-includes/functions.php */
    4924             $del_file = apply_filters( 'wp_delete_file', $del_file );
    4925             @ unlink( path_join($uploadpath['basedir'], $del_file) );
    4926         }
    4927     }
    4928 
    4929     wp_delete_file( $file );
    4930 
    4931     clean_post_cache( $post );
    4932 
    4933     return $post;
     4955            $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     4956            if ( ! empty( $del_file ) ) {
     4957                $del_file = path_join( $uploadpath['basedir'], $del_file );
     4958
     4959                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     4960                    $deleted = false;
     4961                }
     4962            }
     4963        }
     4964    }
     4965
     4966    if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     4967        $deleted = false;
     4968    }
     4969
     4970    return $deleted;
    49344971}
    49354972
Note: See TracChangeset for help on using the changeset viewer.