Making WordPress.org

Changeset 14056


Ignore:
Timestamp:
09/16/2024 10:15:28 PM (4 months ago)
Author:
coffee2code
Message:

Photo Directory, Rejection: Add function to return all the reasons (and respective counts) for all of the rejections for a user.

See #7774.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/rejection.php

    r14055 r14056  
    417417     * @return WP_Post[] Array of rejected photo posts.
    418418     */
    419     public static function get_user_rejections( $user_id ) {
    420         return get_posts( [
    421             'posts_per_page' => 99,
     419    public static function get_user_rejections( $user_id, $args = [] ) {
     420        $args = wp_parse_args(
     421            $args,
     422            [
     423                'fields'         => 'all',
     424                'posts_per_page' => 99,
     425            ]
     426        );
     427
     428        return get_posts( array_merge( $args, [
    422429            'author'         => (int) $user_id,
    423430            'post_status'    => Rejection::get_post_status(),
    424431            'post_type'      => Registrations::get_post_type(),
    425         ] );
     432        ] ) );
     433    }
     434
     435    /**
     436     * Returns an array of the reasons and respective counts for all of the user's rejections.
     437     *
     438     * @param int $user_id The user ID.
     439     * @return int Associative array of rejection reasons and the counts for how many rejections
     440     *             the user has for each reason. This does not include rejection reasons for which
     441     *             the user does not have any rejections.
     442     */
     443    public static function get_user_rejection_reasons( $user_id ) {
     444        global $wpdb;
     445        $reasons = [];
     446        $rejection_ids = self::get_user_rejections( $user_id, [ 'fields' => 'ids', 'posts_per_page' => -1 ] );
     447
     448        if ( $rejection_ids ) {
     449            $rejection_ids = implode( ',', array_map( 'absint', $rejection_ids ) );
     450            $results = $wpdb->get_results( $n = $wpdb->prepare(
     451                "SELECT pm.meta_value as rejection_reason, COUNT(*) as count FROM {$wpdb->postmeta} pm WHERE pm.post_id IN ($rejection_ids) AND meta_key = %s GROUP BY pm.meta_value",
     452                'rejected_reason'
     453            ), ARRAY_A );
     454
     455            foreach ( $results as $item ) {
     456                $reasons[ $item['rejection_reason'] ] = (int) $item['count'];
     457            }
     458        }
     459
     460        return $reasons;
    426461    }
    427462
Note: See TracChangeset for help on using the changeset viewer.