Only delete attachment if not used as featured image by any post.
All checks were successful
PHP Code Checker / PHP Code Checker (pull_request) Successful in 48s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m7s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m5s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 1m7s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 1m5s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Successful in 1m8s
All checks were successful
PHP Code Checker / PHP Code Checker (pull_request) Successful in 48s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m7s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m5s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 1m7s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 1m5s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Successful in 1m8s
This commit is contained in:
parent
d603cea442
commit
f683893284
2 changed files with 38 additions and 5 deletions
|
@ -208,6 +208,36 @@ class Event_Sources {
|
||||||
delete_transient( 'event_bridge_for_activitypub_event_sources_ids' );
|
delete_transient( 'event_bridge_for_activitypub_event_sources_ids' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether an attachment is set as a featured image of any post.
|
||||||
|
*
|
||||||
|
* @param string|int $attachment_id The numeric post ID of the attachment.
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function is_attachment_featured_image( $attachment_id ) {
|
||||||
|
if ( ! is_numeric( $attachment_id ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query posts with the given attachment ID as their featured image.
|
||||||
|
$args = array(
|
||||||
|
'post_type' => 'any',
|
||||||
|
'meta_query' => array(
|
||||||
|
array(
|
||||||
|
'key' => '_thumbnail_id',
|
||||||
|
'value' => $attachment_id,
|
||||||
|
'compare' => '=',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'fields' => 'ids', // Only retrieve post IDs for performance.
|
||||||
|
'numberposts' => 1, // We only need one match to confirm.
|
||||||
|
);
|
||||||
|
|
||||||
|
$posts = \get_posts( $args );
|
||||||
|
|
||||||
|
return ! empty( $posts );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete all posts of an event source.
|
* Delete all posts of an event source.
|
||||||
*
|
*
|
||||||
|
@ -242,10 +272,12 @@ class Event_Sources {
|
||||||
|
|
||||||
if ( $thumbnail_id ) {
|
if ( $thumbnail_id ) {
|
||||||
// Remove the thumbnail from the post.
|
// Remove the thumbnail from the post.
|
||||||
delete_post_thumbnail( $post->ID );
|
\delete_post_thumbnail( $post->ID );
|
||||||
|
|
||||||
// Delete the attachment (and its files) from the media library.
|
// Delete the attachment (and its files) from the media library.
|
||||||
wp_delete_attachment( $thumbnail_id, true );
|
if ( self::is_attachment_featured_image( $thumbnail_id ) ) {
|
||||||
|
\wp_delete_attachment( $thumbnail_id, true );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
\wp_delete_post( $post->ID, true );
|
\wp_delete_post( $post->ID, true );
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace Event_Bridge_For_ActivityPub\ActivityPub\Transmogrifier;
|
||||||
|
|
||||||
use Activitypub\Activity\Extended_Object\Event;
|
use Activitypub\Activity\Extended_Object\Event;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources;
|
||||||
use Exception;
|
use Exception;
|
||||||
use WP_Error;
|
use WP_Error;
|
||||||
|
|
||||||
|
@ -293,7 +294,7 @@ abstract class Base {
|
||||||
|
|
||||||
$thumbnail_id = get_post_thumbnail_id( $post_id );
|
$thumbnail_id = get_post_thumbnail_id( $post_id );
|
||||||
|
|
||||||
if ( $thumbnail_id ) {
|
if ( $thumbnail_id && ! Event_Sources::is_attachment_featured_image( $thumbnail_id ) ) {
|
||||||
wp_delete_attachment( $thumbnail_id, true );
|
wp_delete_attachment( $thumbnail_id, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue