Making WordPress.org


Ignore:
Timestamp:
07/10/2023 11:06:47 PM (19 months ago)
Author:
coffee2code
Message:

Photo Directory: User: Add count_photos_moderated() and count_photos_rejected_as_moderator() to count photos moderated by the user.

See #7130.

File:
1 edited

Legend:

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

    r12727 r12732  
    111111        }
    112112
     113        if ( ! $user_id ) {
     114            return 0;
     115        }
     116
    113117        return (int) $wpdb->get_var( $wpdb->prepare(
    114118            "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status = %s AND post_author = %d",
     
    135139        }
    136140
     141        if ( ! $user_id ) {
     142            return 0;
     143        }
     144
    137145        return (int) $wpdb->get_var( $wpdb->prepare(
    138146            "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status = %s AND post_author = %d",
     
    144152
    145153    /**
     154     * Returns the 'meta_query' value for use in finding posts moderated (and also
     155     * optionally rejected) by a user.
     156     *
     157     * @param int $user_id            The user ID.
     158     * @param int $include_rejections Optional. Should the count of photos
     159     *                                rejected by the user be included in the
     160     *                                count? Default false.
     161     * @return array
     162     */
     163    public static function get_moderator_meta_query( $user_id, $include_rejections = false ) {
     164        if ( ! $user_id ) {
     165            return [];
     166        }
     167
     168        $moderator_query = [
     169            'key'   => Registrations::get_meta_key( 'moderator' ),
     170            'value' => $user_id,
     171        ];
     172        $rejector_query = [
     173            'key'   => 'rejected_by',
     174            'value' => $user_id,
     175        ];
     176
     177        if ( $include_rejections ) {
     178            $meta_query = [
     179                'relation' => 'OR',
     180                $moderator_query,
     181                $rejector_query,
     182            ];
     183        } else {
     184            $meta_query = [ $moderator_query ];
     185        }
     186
     187        return $meta_query;
     188    }
     189
     190    /**
     191     * Returns the number of photos moderated by the user.
     192     *
     193     * By default, this does NOT include photo rejections unless the optional
     194     * argument is enabled.
     195     *
     196     * @param int $user_id            Optional. The user ID. If not defined,
     197     *                                assumes global author. Default false.
     198     * @param int $include_rejections Optional. Should the count of photos
     199     *                                rejected by the user be included in the
     200     *                                count? Default false.
     201     * @return int
     202     */
     203    public static function count_photos_moderated( $user_id = false, $include_rejections = false ) {
     204        if ( ! $user_id ) {
     205            global $authordata;
     206
     207            $user_id = $authordata->ID;
     208        }
     209
     210        if ( ! $user_id ) {
     211            return 0;
     212        }
     213
     214        $post_statuses = ['publish'];
     215        if ( $include_rejections ) {
     216            $post_statuses[] = Rejection::get_post_status();
     217        }
     218
     219        $args = [
     220            'post_type'      => Registrations::get_post_type(),
     221            'post_status'    => $post_statuses,
     222            'meta_query'     => self::get_moderator_meta_query( $user_id, $include_rejections ),
     223            'fields'         => 'ids',
     224            'posts_per_page' => -1,
     225        ];
     226
     227        $query = new \WP_Query( $args );
     228
     229        return $query->post_count;
     230    }
     231
     232    /**
     233     * Returns the number of photos rejected by the user as a moderator.
     234     *
     235     * @param int $user_id Optional. The user ID. If not defined, assumes global
     236     *                     author. Default false.
     237     * @return int
     238     */
     239    public static function count_photos_rejected_as_moderator( $user_id = false ) {
     240        if ( ! $user_id ) {
     241            global $authordata;
     242
     243            $user_id = $authordata->ID;
     244        }
     245
     246        if ( ! $user_id ) {
     247            return 0;
     248        }
     249
     250        $args = [
     251            'post_type'      => Registrations::get_post_type(),
     252            'post_status'    => Rejection::get_post_status(),
     253            'meta_query'     => [
     254                [
     255                    'key'   => 'rejected_by',
     256                    'value' => $user_id,
     257                ],
     258            ],
     259            'fields'         => 'ids',
     260            'posts_per_page' => -1,
     261        ];
     262
     263        $query = new \WP_Query( $args );
     264
     265        return $query->post_count;
     266    }
     267
     268    /**
    146269     * Determines if a user is eligible to toggle all confirmation checkboxes on
    147270     * the photo upload form.
Note: See TracChangeset for help on using the changeset viewer.