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/Optimization/RUCSS/Admin/Subscriber.php b/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php index 2962c44b87..459e10337e 100644 --- a/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php +++ b/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php @@ -120,6 +120,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 ) { 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'] );