diff --git a/src/wp-includes/author-template.php b/src/wp-includes/author-template.php index b422e4d884edc..19cfe0b8b3426 100644 --- a/src/wp-includes/author-template.php +++ b/src/wp-includes/author-template.php @@ -623,37 +623,104 @@ function wp_list_authors( $args = '' ) { * Conditional Tags} article in the Theme Developer Handbook. * * @since 3.2.0 + * @since 7.3.0 Added the `$args` parameter to query specific post types and post statuses. * * @global wpdb $wpdb WordPress database abstraction object. * + * @param array $args { + * Optional. Arguments to determine which posts count towards the author total. + * + * @type string|string[] $post_type Post type or array of post types to check. + * Default 'post'. + * @type string|string[] $post_status Post status or array of post statuses to check. + * Default 'publish'. + * } * @return bool Whether or not we have more than one author */ -function is_multi_author() { +function is_multi_author( $args = array() ) { global $wpdb; - $is_multi_author = get_transient( 'is_multi_author' ); + $defaults = array( + 'post_type' => 'post', + 'post_status' => 'publish', + ); + $parsed_args = wp_parse_args( $args, $defaults ); + + $post_types = array_values( array_unique( array_filter( array_map( 'sanitize_key', (array) $parsed_args['post_type'] ) ) ) ); + $post_statuses = array_values( array_unique( array_filter( array_map( 'sanitize_key', (array) $parsed_args['post_status'] ) ) ) ); + sort( $post_types ); + sort( $post_statuses ); + + if ( empty( $post_types ) ) { + $post_types = (array) $defaults['post_type']; + } + if ( empty( $post_statuses ) ) { + $post_statuses = (array) $defaults['post_status']; + } + + // The default query keeps the original transient name for backward compatibility. + $is_default = array( 'post' ) === $post_types && array( 'publish' ) === $post_statuses; + $transient_key = $is_default ? 'is_multi_author' : 'is_multi_author_' . md5( wp_json_encode( array( $post_types, $post_statuses ) ) ); + + $is_multi_author = get_transient( $transient_key ); if ( false === $is_multi_author ) { - $rows = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" ); + $type_placeholders = implode( ', ', array_fill( 0, count( $post_types ), '%s' ) ); + $status_placeholders = implode( ', ', array_fill( 0, count( $post_statuses ), '%s' ) ); + + $rows = (array) $wpdb->get_col( + $wpdb->prepare( + "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type IN ( $type_placeholders ) AND post_status IN ( $status_placeholders ) LIMIT 2", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare + array_merge( $post_types, $post_statuses ) + ) + ); + $is_multi_author = 1 < count( $rows ) ? 1 : 0; - set_transient( 'is_multi_author', $is_multi_author ); + set_transient( $transient_key, $is_multi_author ); + + if ( ! $is_default ) { + $keys = get_transient( 'is_multi_author_keys' ); + $keys = is_array( $keys ) ? $keys : array(); + if ( ! in_array( $transient_key, $keys, true ) ) { + $keys[] = $transient_key; + set_transient( 'is_multi_author_keys', $keys ); + } + } } /** * Filters whether the site has more than one author with published posts. * * @since 3.2.0 + * @since 7.3.0 Added the `$args` parameter. * - * @param bool $is_multi_author Whether $is_multi_author should evaluate as true. + * @param bool $is_multi_author Whether $is_multi_author should evaluate as true. + * @param array $args Parsed arguments with `post_type` and `post_status` arrays. */ - return apply_filters( 'is_multi_author', (bool) $is_multi_author ); + return apply_filters( + 'is_multi_author', + (bool) $is_multi_author, + array( + 'post_type' => $post_types, + 'post_status' => $post_statuses, + ) + ); } /** * Helper function to clear the cache for number of authors. * * @since 3.2.0 + * @since 7.3.0 Also clears the caches created for custom post type and post status arguments. * @access private */ function __clear_multi_author_cache() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore delete_transient( 'is_multi_author' ); + + $keys = get_transient( 'is_multi_author_keys' ); + if ( is_array( $keys ) ) { + foreach ( $keys as $key ) { + delete_transient( $key ); + } + delete_transient( 'is_multi_author_keys' ); + } } diff --git a/tests/phpunit/tests/user/isMultiAuthor.php b/tests/phpunit/tests/user/isMultiAuthor.php index 3e6052b030efa..6144470d573dc 100644 --- a/tests/phpunit/tests/user/isMultiAuthor.php +++ b/tests/phpunit/tests/user/isMultiAuthor.php @@ -143,4 +143,97 @@ public function test_applies_is_multi_author_filter() { $this->assertSame( 1, $filter->get_call_count() ); $this->assertSame( array( false ), $filter->get_args()[0] ); } + + /** + * Tests that is_multi_author() can check a specific post type. + * + * @ticket 24284 + */ + public function test_custom_post_type_argument() { + self::factory()->post->create( + array( + 'post_author' => self::$author_1, + 'post_type' => 'page', + ) + ); + self::factory()->post->create( + array( + 'post_author' => self::$author_2, + 'post_type' => 'page', + ) + ); + + $this->assertFalse( is_multi_author() ); + $this->assertTrue( is_multi_author( array( 'post_type' => 'page' ) ) ); + $this->assertTrue( is_multi_author( array( 'post_type' => array( 'post', 'page' ) ) ) ); + } + + /** + * Tests that is_multi_author() can check a specific post status. + * + * @ticket 24284 + */ + public function test_custom_post_status_argument() { + self::factory()->post->create( + array( + 'post_author' => self::$author_1, + 'post_status' => 'publish', + ) + ); + self::factory()->post->create( + array( + 'post_author' => self::$author_2, + 'post_status' => 'draft', + ) + ); + + $this->assertFalse( is_multi_author() ); + $this->assertTrue( is_multi_author( array( 'post_status' => array( 'publish', 'draft' ) ) ) ); + } + + /** + * Tests that caches for custom arguments are cleared when a post status changes. + * + * @ticket 24284 + */ + public function test_custom_argument_cache_is_cleared_on_status_transition() { + $args = array( 'post_type' => 'page' ); + + self::factory()->post->create( + array( + 'post_author' => self::$author_1, + 'post_type' => 'page', + ) + ); + $this->assertFalse( is_multi_author( $args ) ); + + self::factory()->post->create( + array( + 'post_author' => self::$author_2, + 'post_type' => 'page', + ) + ); + + $this->assertTrue( is_multi_author( $args ) ); + } + + /** + * Tests that empty arguments fall back to the defaults. + * + * @ticket 24284 + */ + public function test_empty_arguments_fall_back_to_defaults() { + self::factory()->post->create( array( 'post_author' => self::$author_1 ) ); + self::factory()->post->create( array( 'post_author' => self::$author_2 ) ); + + $this->assertTrue( + is_multi_author( + array( + 'post_type' => '', + 'post_status' => array(), + ) + ) + ); + $this->assertSame( 1, get_transient( 'is_multi_author' ) ); + } }