Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#32243 closed enhancement (fixed)

count_user_posts Should Accept Multiple Post Types

Reported by: nikonratm's profile nikonratm Owned by: boonebgorges's profile boonebgorges
Milestone: 4.3 Priority: normal
Severity: normal Version: 4.3
Component: Users Keywords: has-patch needs-testing needs-docs
Focuses: template Cc:

Description

It's great that the count_user_posts function now accepts a post_type parameter as of 4.1 but I think it would be much more useful if it accepted multiple post types, I think I will go ahead and implement this =)

Attachments (8)

user.diff (1.4 KB) - added by nikonratm 10 years ago.
Updated count_user_posts to accept multiple post types
post.diff (2.1 KB) - added by nikonratm 10 years ago.
Updated get_posts_by_author_sql to accept multiple post types
post.2.diff (2.4 KB) - added by nikonratm 10 years ago.
fixed get_posts_by_author_sql respects capabilities for all included post types
32243.diff (3.9 KB) - added by nikonratm 10 years ago.
patch file that includes changes to both post.php and user.php
32243_r2.diff (5.6 KB) - added by nikonratm 10 years ago.
updated to respect capabilities per post type
32243_3.diff (7.7 KB) - added by nikonratm 10 years ago.
Added unit testing
32243_4.diff (8.3 KB) - added by nikonratm 10 years ago.
updated to allow public_only as part of main function
32243_5.diff (8.2 KB) - added by nikonratm 10 years ago.
removed silly echo line used for testing

Download all attachments as: .zip

Change History (19)

@nikonratm
10 years ago

Updated count_user_posts to accept multiple post types

@nikonratm
10 years ago

Updated get_posts_by_author_sql to accept multiple post types

@nikonratm
10 years ago

fixed get_posts_by_author_sql respects capabilities for all included post types

#1 @nikonratm
10 years ago

  • Keywords has-patch needs-testing added

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


10 years ago

@nikonratm
10 years ago

patch file that includes changes to both post.php and user.php

#3 @boonebgorges
10 years ago

  • Keywords needs-patch added; has-patch needs-testing removed

Thanks for the patch, nikonratm!

Your $cap_final logic doesn't look like it will work properly. Say I'm querying for three post types: array( 'foo', 'bar', 'baz' ). And let's say that I have the read_private_posts cap for 'baz' but not for 'foo' and 'bar'. In this situation, it looks like the last post type cap will overwrite the previous two, and private posts for all three post types will be returned. If we're going to add the improvement suggested in this ticket, the SQL will need to look like this:

... WHERE (
  ( post_type = 'foo' AND post_status = 'publish' )
  OR
  ( post_type = 'bar' AND post_status = 'publish' )
  OR
  ( post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' ) ) 
)

(You'll have to make sure the "users can view their own private posts" stuff works too.)

It'll help enormously to write some unit tests describing scenarios like the above. See https://core.trac.wordpress.org/browser/trunk/tests/phpunit/tests/post/getPostsByAuthorSql.php for some examples. (You could also write indirect tests for count_user_posts().)

Small additional point: Make sure you don't break backward compatibility for singular $post_type. If a string has been passed, cast it to array( $post_type ).

Last edited 10 years ago by boonebgorges (previous) (diff)

@nikonratm
10 years ago

updated to respect capabilities per post type

#4 @nikonratm
10 years ago

  • Keywords has-patch needs-testing needs-unit-tests added; needs-patch removed

Ah, good point. I've updated the function to do as you suggested, as well as allow for string (i.e. single)
$post_types arguments.

I don't have the time to write tests right now but I would be happy to do that.

@nikonratm
10 years ago

Added unit testing

#5 @nikonratm
10 years ago

  • Keywords needs-unit-tests removed

Cool, added a unit test for multiple post types and for testing when a user has access to private posts for only certain post types ( test_user_has_access_only_to_private_posts_for_certain_post_types )

@nikonratm
10 years ago

updated to allow public_only as part of main function

@nikonratm
10 years ago

removed silly echo line used for testing

#6 @boonebgorges
10 years ago

  • Milestone changed from Awaiting Review to 4.3

This looks pretty good.

#7 @boonebgorges
10 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 32523:

Support multiple post types in count_user_posts() and other functions that use get_posts_by_author_sql().

Props nikonratm.
Fixes #32243.

#8 @DrewAPicture
10 years ago

  • Keywords needs-docs added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Since we added an additional type for $post_type, we need to note that in the changelogs as well.

#9 follow-up: @boonebgorges
10 years ago

DrewAPicture - Can you be more specific? I added a changelog item to get_posts_by_author_sql(). I didn't add the same thing to all functions that call get_posts_by_author_sql(), though I did update the param definitions. Please give guidance on how you want to handle this.

#10 in reply to: ↑ 9 @DrewAPicture
10 years ago

Replying to boonebgorges:

DrewAPicture - Can you be more specific? I added a changelog item to get_posts_by_author_sql(). I didn't add the same thing to all functions that call get_posts_by_author_sql(), though I did update the param definitions. Please give guidance on how you want to handle this.

$post_type only accepted a string before, and now accepts either a string or an array in three places. We need to document that it now accepts an array in the changelog.

Last edited 10 years ago by DrewAPicture (previous) (diff)

#11 @boonebgorges
10 years ago

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

In 32524:

Streamline support for multiple post types in get_posts_by_author_sql().

  • Don't accept a comma-separated list, only a single post type or an array of post types. This is easier to document.
  • Add changelog entries to all calling functions.

Props DrewAPicture.
Fixes #32243.

Note: See TracTickets for help on using tickets.