Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#53443 closed defect (bug) (fixed)

The value of the `post_count` meta field is wrong because it doesn't get decremented when a post is deleted

Reported by: henrywright's profile henry.wright Owned by: audrasjb's profile audrasjb
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: multisite Cc:

Description

The update_posts_count() function is used to update a blog's post count. When a post is added to the database the post_count value is incremented as expected. However, when a post is deleted, the post_count value isn't decremented.

This is because the _update_posts_count_on_delete() function, which calls update_posts_count() is hooked to the delete_post action.

delete_post fires immediately before a post is deleted from the database.

Therefore, the query performed in update_posts_count() to get the post count is run at the wrong time.

The solution is to use the deleted_post hook instead of the delete_post hook.

Attachments (1)

53443.diff (738 bytes) - added by henry.wright 3 years ago.

Download all attachments as: .zip

Change History (35)

@henry.wright
3 years ago

#1 @henry.wright
3 years ago

  • Keywords has-patch added

53443.diff uses the deleted_post hook instead of the delete_post hook

#2 @SergeyBiryukov
3 years ago

  • Component changed from General to Posts, Post Types
  • Focuses multisite added
  • Milestone changed from Awaiting Review to 5.9

#3 @hellofromTonya
3 years ago

  • Keywords needs-testing added

Hmm the change makes sense to me. Marking as needs-testing to ensure reproducible and then fixed after the patch. A test report will be helpful.

This ticket was mentioned in Slack in #core-test by hellofromtonya. View the logs.


3 years ago

#5 @pbearne
3 years ago

  • Owner set to pbearne
  • Status changed from new to assigned

This ticket was mentioned in PR #1890 on WordPress/wordpress-develop by pbearne.


3 years ago
#6

  • Keywords has-unit-tests added

#7 @pbearne
3 years ago

the change the filter does fix this

#8 @pbearne
3 years ago

  • Keywords needs-testing has-unit-tests removed
  • Owner changed from pbearne to h

#9 @pbearne
3 years ago

I am confused why the tests are failing on github as the test is passing in my local

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


3 years ago

This ticket was mentioned in Slack in #core-test by pbearne. View the logs.


3 years ago

audrasjb commented on PR #1890:


3 years ago
#12

It looks like the last commit (moving the action from deleted_post to after_delete_post) fixes the issue. It also makes sense to me since now caches are refreshed before assuming the delete action is done.

(the multisite testing report look unrelated)

#13 @audrasjb
3 years ago

  • Keywords has-unit-tests added

#14 @henry.wright
3 years ago

For the failing tests, do we need to create a blog and switch to it before the assertions are run?

$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );

#15 @audrasjb
3 years ago

@henrywright it sounded like a good idea, but the tests are failing as well :)

#16 @henry.wright
3 years ago

In that case the problem could be the array argument passed to the create() method.

array(
    'post_type'   => 'post',
    'post_author' => '1',
    'post_date'   => '2012-10-23 19:34:42',
)

This will likely be creating a post in draft status because the default value for post_status is draft.

We could try explicitly setting the post_status value to publish. update_posts_count() gets a count of published posts only to set the post count.

array(
    'post_type'   => 'post',
    'post_author' => '1',
    'post_date'   => '2012-10-23 19:34:42',
    'post_status' => 'publish',
)

#17 @henry.wright
3 years ago

Also when a new WordPress blog is created, it is automatically populated with Hello world! post. Do we need to account for that post in the assertion?

#18 @henry.wright
3 years ago

Actually, I can see the automatically populated Hello world! post is already accounted for

henrywright commented on PR #1890:


3 years ago
#19

@audrasjb tests still failing now b17e1e7 is applied means the value of post_count isn't incremented when the Hello world! post is created during blog creation

The Hello world! post is inserted using $wpdb methods instead of going through the wp_insert_post() API. Because the API isn't used, update_posts_count() isn't called. This results in the assertion failing because the expected value is erroneously 1. The expected value should be, and would be, 2 if the Hello world! post incremented post_count.

I'll post the specific test failure below for easy reference:

There was 1 failure:

1) Tests_update_posts_count_on_delete::test_update_posts_count_on_delete
post added
Failed asserting that 2 matches expected 1.

/var/www/tests/phpunit/tests/multisite/ms-functions/update_posts_count.php:38

audrasjb commented on PR #1890:


3 years ago
#20

Alright, let's try to take into account the "Hello World" blog post automatically created with the blog then.

henrywright commented on PR #1890:


3 years ago
#21

Yay tests are passing

1 final amendment I'd suggest is add a call to restore_current_blog() at the end of the routine because we added a switch_to_blog() call at the start

#22 @audrasjb
3 years ago

  • Keywords commit added

Tests are passing now. Self-assigning for commit consideration.

#23 @audrasjb
3 years ago

  • Owner changed from h to audrasjb

#25 @audrasjb
3 years ago

In 52201:

Posts, Post Types: Increment post_count option value during blog creation.

Previously, the post_count option value was not incremented when the default "Hello world!" post is inserted during blog creation on a multisite installation.

Props henry.wright.
Fixes #54462.
See #53443.

#26 @audrasjb
3 years ago

  • Keywords needs-refresh added; commit removed

Now that #54462 is closed, let's refresh the unit tests.

#27 @henry.wright
3 years ago

Now that #54462 is closed, let's refresh the unit tests.

Cool

audrasjb commented on PR #1890:


3 years ago
#28

@henrywright the code is updated. Tests are passing.
I think we're good to go now, isn't it?

This ticket was mentioned in PR #1912 on WordPress/wordpress-develop by audrasjb.


3 years ago
#29

  • Keywords needs-refresh removed

#30 @audrasjb
3 years ago

@henrywright @pbearne I added a new PR to rename the new unit test file from update_posts_count.php to updatePostsCount.php for better consistency with the other test files in this folder.

#31 @henry.wright
3 years ago

The new PR 1912 is looking good

#32 @audrasjb
3 years ago

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

In 52207:

Posts, Post Types: Multisite: Decrement post_count option value when a post is deleted.

Previously, the post_count option value was not decremented when a post was deleted.

This change moves the _update_posts_count_on_delete action from delete_post hook to after_delete_post to ensure the deletion is taken into account.

Props henry.wright, pbearne, audrasjb.
Fixes #53443.

Note: See TracTickets for help on using tickets.