WordPress.org

Make WordPress Core

#49597 closed enhancement (fixed)

Introduce preflight filter to wp_delete_attachment().

Reported by: peterwilsoncc Owned by: peterwilsoncc
Milestone: 5.5 Priority: normal
Severity: normal Version:
Component: Media Keywords: commit
Focuses: Cc:

Description

Introducing pre_delete_attachment to be friends with pre_delete_post would be helpful for large sites.

The signature would be identical:

apply_filters( 'pre_delete_attachment', bool|null $delete, WP_Post $post, bool $force_delete )

Attachments (1)

49597.diff (832 bytes) - added by peterwilsoncc 11 months ago.

Download all attachments as: .zip

Change History (5)

#1 @joemcgill
11 months ago

@peterwilsoncc is the goal here just to avoid having to check the post type of the post getting passed to the already included filter, or is there some other benefit? Alternatively, should we consider a more robust pre_delete_post_{$post_type} filter rather than special casing attachments?

If you could shed more light on the use case you see here, that would be helpful.

#2 @peterwilsoncc
11 months ago

  • Keywords has-patch added; needs-patch removed

@joemcgill

The intent is to make preflighting available for deleting attachments, at the moment it is unavailable due to the order of operation in wp_delete_post().

wp_delete_post() checks the post type and calls wp_delete_attachment() prior to calling the preflight hook:

<?php
// From wp_delete_post():

if ( 'attachment' === $post->post_type ) {
        return wp_delete_attachment( $postid, $force_delete );
}

/**
 * Filters whether a post deletion should take place.
 *
 * @since 4.4.0
 *
 * @param bool|null $delete       Whether to go forward with deletion.
 * @param WP_Post   $post         Post object.
 * @param bool      $force_delete Whether to bypass the Trash.
 */
$check = apply_filters( 'pre_delete_post', null, $post, $force_delete );
if ( null !== $check ) {
        return $check;
}

In 49597.diff:

  • introduce preflight hook
  • For consistency with pre_delete_post and pre_trash_post, I've added the function below any relevant post type and status checking.

I've left the milestone as awaiting review to allow the media component team to consider.

#3 @joemcgill
11 months ago

  • Keywords commit added; has-patch removed
  • Milestone changed from Awaiting Review to 5.5
  • Owner set to peterwilsoncc
  • Status changed from new to assigned

@peterwilsoncc Ah, I missed that special treatment of attachments came before the preflight filter. Yes, this change makes perfect sense to me, given the state of things and also extends the preflight to any code calling wp_delete_attachment() directly. +1 from me.

#4 @peterwilsoncc
11 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 47448:

Media: Introduce preflight filter to wp_delete_attachment().

Introduces the filter pre_delete_attachment to allow developers to prevent or modify the deletion of attachments. This improves consistency with wp_delete_post() and wp_trash_post().

Props joemcgill, peterwilsoncc.
Fixes #49597.

Note: See TracTickets for help on using tickets.