From f0d6031d9bb4f0e2c547be1031d386f7a332c005 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 21 May 2015 19:48:19 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@32524 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 10 +++++----- src/wp-includes/user.php | 4 ++-- tests/phpunit/tests/post/getPostsByAuthorSql.php | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 679b0dd706..479f2ab6ed 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -5326,9 +5326,9 @@ function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { * published posts, and all private posts to which the user has access. * * @since 2.2.0 + * @since 4.3.0 Added the ability to pass an array to `$post_type`. * - * @param string|array $post_type Array or comma-separated string of post types. - * Currently only supports 'post' or 'page'. + * @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'. * @return string SQL code that can be added to a where clause. */ function get_private_posts_cap_sql( $post_type ) { @@ -5339,11 +5339,11 @@ function get_private_posts_cap_sql( $post_type ) { * Retrieve the post SQL based on capability, author, and type. * * @since 3.0.0 - * @since 4.3.0 Introduced the ability to pass multiple post types to `$post_type`. + * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. * * @see get_private_posts_cap_sql() * - * @param array|string $post_type Array or comma-separated list of post type(s). + * @param array|string $post_type Single post type or an array of post types. * @param bool $full Optional. Returns a full WHERE statement instead of just * an 'andalso' term. Default true. * @param int $post_author Optional. Query posts having a single author ID. Default null. @@ -5357,7 +5357,7 @@ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, if ( is_array( $post_type ) ) { $post_types = $post_type; } else { - $post_types = preg_split( '/[\s,]+/', $post_type ); + $post_types = array( $post_type ); } $post_type_clauses = array(); diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 9e6ca0efa4..9971b96e62 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -251,7 +251,7 @@ function wp_validate_logged_in_cookie( $user_id ) { * * @since 3.0.0 * @since 4.1.0 Added `$post_type` argument. - * @since 4.3.0 Added `$public_only` argument. + * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array of post types to `$post_type`. * * @global wpdb $wpdb WordPress database object for queries. * @@ -288,7 +288,7 @@ function count_user_posts( $userid, $post_type = 'post', $public_only = false ) * @since 3.0.0 * * @param array $users Array of user IDs. - * @param string|array $post_type Optional. Array or comma-separated list of post types to check. Defaults to 'post'. + * @param string|array $post_type Optional. Single post type or array of post types to check. Defaults to 'post'. * @param bool $public_only Optional. Only return counts for public posts. Defaults to false. * @return array Amount of posts each user has written. */ diff --git a/tests/phpunit/tests/post/getPostsByAuthorSql.php b/tests/phpunit/tests/post/getPostsByAuthorSql.php index 70b2f4e878..7cd1aa65ef 100644 --- a/tests/phpunit/tests/post/getPostsByAuthorSql.php +++ b/tests/phpunit/tests/post/getPostsByAuthorSql.php @@ -24,7 +24,7 @@ class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase { register_post_type( 'foo' ); register_post_type( 'bar' ); - $maybe_string = get_posts_by_author_sql( 'foo,bar' ); + $maybe_string = get_posts_by_author_sql( array( 'foo', 'bar' ) ); $this->assertContains( "post_type = 'foo'", $maybe_string ); $this->assertContains( "post_type = 'bar'", $maybe_string ); @@ -135,7 +135,7 @@ class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase { $editor_role->add_cap( 'read_private_baz' ); wp_set_current_user( $u ); - $maybe_string = get_posts_by_author_sql( 'foo,bar,baz' ); + $maybe_string = get_posts_by_author_sql( array( 'foo', 'bar', 'baz' ) ); $this->assertNotContains( "post_type = 'foo' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string ); $this->assertNotContains( "post_type = 'bar' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string ); $this->assertContains( "post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );