From 5dcfb617832a78e53a774a06f232a2b6fd547504 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 23 Oct 2024 16:06:25 +0300 Subject: [PATCH 1/2] [WIP] --- inc/Engine/Common/PerformanceHints/Admin/Subscriber.php | 3 +++ inc/Engine/Optimization/RUCSS/Admin/Subscriber.php | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php b/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php index 2c6449c9a3..6d675041f5 100644 --- a/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php +++ b/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php @@ -99,6 +99,9 @@ public function truncate_tables(): void { * @return void */ public function delete_post( int $post_id ): void { + if ( 'attachment' === get_post_type( $post_id ) ) { + return; + } $this->controller->delete_post( $post_id ); } diff --git a/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php b/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php index 08814e970a..385c526466 100644 --- a/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php +++ b/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php @@ -119,6 +119,10 @@ public function delete_used_css_on_update_or_delete( $post_id ) { return; } + if ( 'attachment' === get_post_type( $post_id ) ) { + return; + } + $url = get_permalink( $post_id ); if ( false === $url ) { From fe949986e0e56008b2e15fd90404d4aa10b04281 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Thu, 23 Jan 2025 16:02:41 +0100 Subject: [PATCH 2/2] :closes: #7073 Update test, move logic to controller form subscriber --- .../Common/PerformanceHints/Admin/Controller.php | 4 ++++ .../Common/PerformanceHints/Admin/Subscriber.php | 3 --- .../Admin/Controller/deletePost.php | 13 +++++++++++++ .../Subscriber/deleteUsedCssOnUpdateOrDelete.php | 16 ++++++++++++++++ .../Admin/Controller/deletePost.php | 2 ++ .../Subscriber/deleteUsedCssOnUpdateOrDelete.php | 3 +++ 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/inc/Engine/Common/PerformanceHints/Admin/Controller.php b/inc/Engine/Common/PerformanceHints/Admin/Controller.php index 5856d55633..fb3152570f 100644 --- a/inc/Engine/Common/PerformanceHints/Admin/Controller.php +++ b/inc/Engine/Common/PerformanceHints/Admin/Controller.php @@ -64,6 +64,10 @@ private function delete_rows() { * @return void */ public function delete_post( $post_id ) { + if ( 'attachment' === get_post_type( $post_id ) ) { + return; + } + $url = get_permalink( $post_id ); // get_permalink should return false or string, but some plugins return null. diff --git a/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php b/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php index 6d675041f5..2c6449c9a3 100644 --- a/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php +++ b/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php @@ -99,9 +99,6 @@ public function truncate_tables(): void { * @return void */ public function delete_post( int $post_id ): void { - if ( 'attachment' === get_post_type( $post_id ) ) { - return; - } $this->controller->delete_post( $post_id ); } diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Admin/Controller/deletePost.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Admin/Controller/deletePost.php index d7bb531977..10da29d203 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Admin/Controller/deletePost.php +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Admin/Controller/deletePost.php @@ -5,6 +5,7 @@ 'config' => [ 'filter' => false, 'post_id' => 1, + 'post_type' => 'post', 'url' => 'http://example.org', 'factories' => [ 'get_admin_controller' @@ -17,6 +18,7 @@ 'filter' => true, 'post_id' => 1, 'url' => null, + 'post_type' => 'post', ], 'expected' => false, ], @@ -25,6 +27,7 @@ 'filter' => true, 'post_id' => 1, 'url' => false, + 'post_type' => 'post', ], 'expected' => false, ], @@ -32,8 +35,18 @@ 'config' => [ 'filter' => true, 'post_id' => 1, + 'post_type' => 'post', 'url' => 'http://example.org', ], 'expected' => true, ], + 'testShoulNotDeletePostWithAttachmentPostType' => [ + 'config' => [ + 'filter' => true, + 'post_id' => 1, + 'post_type' => 'attachment', + 'url' => 'http://example.org', + ], + 'expected' => false, + ], ]; diff --git a/tests/Fixtures/inc/Engine/Optimization/RUCSS/Admin/Subscriber/deleteUsedCssOnUpdateOrDelete.php b/tests/Fixtures/inc/Engine/Optimization/RUCSS/Admin/Subscriber/deleteUsedCssOnUpdateOrDelete.php index 3eddb2cbd3..154b912ed8 100644 --- a/tests/Fixtures/inc/Engine/Optimization/RUCSS/Admin/Subscriber/deleteUsedCssOnUpdateOrDelete.php +++ b/tests/Fixtures/inc/Engine/Optimization/RUCSS/Admin/Subscriber/deleteUsedCssOnUpdateOrDelete.php @@ -68,6 +68,7 @@ 'url' => 'http://example.org/category/test/', 'files_deleted' => [], 'files_preserved' => array_merge( $files, $preserved ), + 'post_type' => 'post' ] ], 'shouldDeleteOnUpdate' => [ @@ -80,6 +81,7 @@ 'post_id' => 1, 'url' => 'http://example.org/category/test/', 'files_preserved' => array_merge( $files, $preserved ), + 'post_type' => 'post' ] ], 'shouldNotDeleteOnDisabledFilter' => [ @@ -92,6 +94,20 @@ 'items' => $items, 'files_deleted' => [], 'files_preserved' => array_merge( $files, $preserved ), + 'post_type' => 'post' + ] + ], + 'shouldNotDeleteOnAttachmentPostType' => [ + 'input' => [ + 'remove_unused_css' => true, + 'is_disabled' => false, + 'wp_error' => false, + 'post_id' => 1, + 'url' => 'http://example.org/category/test/', + 'items' => $items, + 'files_deleted' => [], + 'files_preserved' => array_merge( $files, $preserved ), + 'post_type' => 'attachment' ] ] ] diff --git a/tests/Unit/inc/Engine/Common/PerformanceHints/Admin/Controller/deletePost.php b/tests/Unit/inc/Engine/Common/PerformanceHints/Admin/Controller/deletePost.php index 1c244493ef..3facd62f45 100644 --- a/tests/Unit/inc/Engine/Common/PerformanceHints/Admin/Controller/deletePost.php +++ b/tests/Unit/inc/Engine/Common/PerformanceHints/Admin/Controller/deletePost.php @@ -37,6 +37,8 @@ public function testShouldDoExpected( $config, $expected ) { $controller = new Controller( ! $config['filter'] ? [] : $this->factories ); Functions\when( 'get_permalink' )->justReturn( $config['url'] ); + Functions\when( 'get_post_type' ) + ->justReturn( $config['post_type'] ); if ( $expected ) { $this->queries->expects( $this->once() ) diff --git a/tests/Unit/inc/Engine/Optimization/RUCSS/Admin/Subscriber/deleteUsedCssOnUpdateOrDelete.php b/tests/Unit/inc/Engine/Optimization/RUCSS/Admin/Subscriber/deleteUsedCssOnUpdateOrDelete.php index 35fb9e9727..614bb46348 100644 --- a/tests/Unit/inc/Engine/Optimization/RUCSS/Admin/Subscriber/deleteUsedCssOnUpdateOrDelete.php +++ b/tests/Unit/inc/Engine/Optimization/RUCSS/Admin/Subscriber/deleteUsedCssOnUpdateOrDelete.php @@ -42,6 +42,9 @@ public function testShouldDoExpected( $config ) { Functions\when( 'get_permalink' ) ->justReturn( $config['url'] ); + Functions\when( 'get_post_type' ) + ->justReturn( $config['post_type'] ); + $this->configureDeletion($config); $this->subscriber->delete_used_css_on_update_or_delete( $config['post_id'] );