Make WordPress Core

Changeset 59900


Ignore:
Timestamp:
03/02/2025 11:04:23 PM (7 weeks ago)
Author:
peterwilsoncc
Message:

Users: Add pre-flight filter to count_many_users_posts().

Introduces the filter pre_count_many_users_posts() to allow developers to bypass the function in favour of either avoiding counts or their own counting functionality.

Props audrasjb, ethitter, jigar-bhanushali, jorbin.
Fixes #63004.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r59828 r59900  
    661661    global $wpdb;
    662662
    663     $count = array();
    664663    if ( empty( $users ) || ! is_array( $users ) ) {
    665         return $count;
     664        return array();
     665    }
     666
     667    /**
     668     * Filters whether to short-circuit performing the post counts.
     669     *
     670     * When filtering, return an array of posts counts as strings, keyed
     671     * by the user ID.
     672     *
     673     * @since 6.8.0
     674     *
     675     * @param string[]|null   $count       The post counts. Return a non-null value to short-circuit.
     676     * @param int[]           $users       Array of user IDs.
     677     * @param string|string[] $post_type   Single post type or array of post types to check.
     678     * @param bool            $public_only Whether to only return counts for public posts.
     679     */
     680    $pre = apply_filters( 'pre_count_many_users_posts', null, $users, $post_type, $public_only );
     681    if ( null !== $pre ) {
     682        return $pre;
    666683    }
    667684
     
    670687
    671688    $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
     689
     690    $count = array_fill_keys( $users, 0 );
    672691    foreach ( $result as $row ) {
    673692        $count[ $row[0] ] = $row[1];
    674     }
    675 
    676     foreach ( $users as $id ) {
    677         if ( ! isset( $count[ $id ] ) ) {
    678             $count[ $id ] = 0;
    679         }
    680693    }
    681694
Note: See TracChangeset for help on using the changeset viewer.