Skip to content

Commit

Permalink
Fix #7061 Exclude attachment post type from some features (#7073)
Browse files Browse the repository at this point in the history
Co-authored-by: Khadreal <[email protected]>
  • Loading branch information
wordpressfan and Khadreal authored Feb 17, 2025
1 parent 10a00a8 commit 951143f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions inc/Engine/Common/PerformanceHints/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions inc/Engine/Optimization/RUCSS/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'config' => [
'filter' => false,
'post_id' => 1,
'post_type' => 'post',
'url' => 'http://example.org',
'factories' => [
'get_admin_controller'
Expand All @@ -17,6 +18,7 @@
'filter' => true,
'post_id' => 1,
'url' => null,
'post_type' => 'post',
],
'expected' => false,
],
Expand All @@ -25,15 +27,26 @@
'filter' => true,
'post_id' => 1,
'url' => false,
'post_type' => 'post',
],
'expected' => false,
],
'testShoulDeletePost' => [
'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,
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'url' => 'http://example.org/category/test/',
'files_deleted' => [],
'files_preserved' => array_merge( $files, $preserved ),
'post_type' => 'post'
]
],
'shouldDeleteOnUpdate' => [
Expand All @@ -80,6 +81,7 @@
'post_id' => 1,
'url' => 'http://example.org/category/test/',
'files_preserved' => array_merge( $files, $preserved ),
'post_type' => 'post'
]
],
'shouldNotDeleteOnDisabledFilter' => [
Expand All @@ -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'
]
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
Expand Down

0 comments on commit 951143f

Please sign in to comment.