wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_delete_attachment_files › WordPress Function

Since4.9.7
Deprecatedn/a
wp_delete_attachment_files ( $post_id, $meta, $backup_sizes, $file )
Parameters: (4)
  • (int) $post_id Attachment ID.
    Required: Yes
  • (array) $meta The attachment's meta data.
    Required: Yes
  • (array) $backup_sizes The meta data for the attachment's backup images.
    Required: Yes
  • (string) $file Absolute path to the attachment's file.
    Required: Yes
Returns:
  • (bool) True on success, false on failure.
Defined at:
Codex:

Deletes all files that belong to the given attachment.



Source

function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
	global $wpdb;

	$uploadpath = wp_get_upload_dir();
	$deleted    = true;

	if ( ! empty( $meta['thumb'] ) ) {
		// Don't delete the thumb if another attachment uses it.
		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 ) ) ) {
			$thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file );

			if ( ! empty( $thumbfile ) ) {
				$thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
				$thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );

				if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
					$deleted = false;
				}
			}
		}
	}

	// Remove intermediate and backup images if there are any.
	if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
		$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );

		foreach ( $meta['sizes'] as $size => $sizeinfo ) {

			// Check for alternate size mime types in the sizeinfo['sources'] array to delete.
			if ( isset( $sizeinfo['sources'] ) && is_array( $sizeinfo['sources'] ) ) {
				foreach ( $sizeinfo['sources'] as $mime => $properties ) {
					$intermediate_file = str_replace( wp_basename( $file ), $properties['file'], $file );
					if ( ! empty( $intermediate_file ) ) {
						$intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
						if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
							$deleted = false;
						}
					}
				}
			} else {
				// Otherwise, delete files from the sizeinfo data.
				$intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );

				if ( ! empty( $intermediate_file ) ) {
					$intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );

					if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
						$deleted = false;
					}
				}
			}
		}
	}

	if ( ! empty( $meta['original_image'] ) ) {
		if ( empty( $intermediate_dir ) ) {
			$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
		}

		$original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file );

		if ( ! empty( $original_image ) ) {
			$original_image = path_join( $uploadpath['basedir'], $original_image );

			if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) {
				$deleted = false;
			}
		}
	}

	// Delete the full size images from 'sources' if available, or the root file.
	if ( isset( $meta['sources'] ) && is_array( $meta['sources'] ) ) {
		$sources          = $meta['sources'];
		$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
		foreach ( $sources as $mime => $properties ) {
			if ( ! is_array( $properties ) || empty( $properties['file'] ) ) {
				continue;
			}
			$intermediate_file = str_replace( wp_basename( $file ), $properties['file'], $file );
			if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
				$deleted = false;
			}
		}
	} else {
		if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
			$deleted = false;
		}
	}

	if ( is_array( $backup_sizes ) ) {

		$del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
		// Delete the root (edited) file which was not deleted above.
		if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
			$deleted = false;
		}
		foreach ( $backup_sizes as $size ) {
			// Delete files from 'sources' data if available, otherwise from 'sizes' data.
			if ( isset( $meta['sources'] ) && is_array( $meta['sources'] ) ) {
				// Delete any backup images stored in the 'sources' array.
				if ( isset( $size['sources'] ) && is_array( $size['sources'] ) ) {
					foreach ( $size['sources'] as $mime => $properties ) {
						$del_file = path_join( dirname( $meta['file'] ), $properties['file'] );
						if ( ! empty( $del_file ) ) {
							$del_file = path_join( $uploadpath['basedir'], $del_file );
							if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
								$deleted = false;
							}
						}
					}
				}
			} else {
				$del_file = path_join( dirname( $meta['file'] ), $size['file'] );

				if ( ! empty( $del_file ) ) {
					$del_file = path_join( $uploadpath['basedir'], $del_file );
					if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
						$deleted = false;
					}
				}
			}
		}
	}

	return $deleted;
}