Making WordPress.org

Changeset 14538


Ignore:
Timestamp:
09/24/2025 08:26:16 PM (8 weeks ago)
Author:
coffee2code
Message:

Photo Directory, Moderation: Handle assignment of caps to moderators who are allowed to manage photo_tags.

File:
1 edited

Legend:

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

    r14356 r14538  
    4949     */
    5050    const FLAG_REJECTION_CRITICAL_THRESHOLD_PERCENTAGE = 0.50;
     51
     52    /**
     53     * Name of user meta key that acts as a flag for whether the user can manage photo_tags.
     54     *
     55     * Note: There are additional checks, such as the user also being a moderator or admin,
     56     * that are also considered.
     57     *
     58     * @var string
     59     */
     60    const USER_META_CAN_MANAGE_PHOTO_TAGS = 'can_manage_photo_tags';
    5161
    5262    /**
     
    6777        // Disable moderating own posts.
    6878        add_filter( 'user_has_cap',                       [ __CLASS__, 'disable_own_post_editing' ], 10, 4 );
     79
     80        // Assign caps to moderators who can manage photo_tags.
     81        add_filter( 'map_meta_cap',                       [ __CLASS__, 'assign_cap_manage_photo_tags' ], 10, 3 );
    6982
    7083        // Add column to users table with count of photos moderated.
     
    207220
    208221        return $caps;
     222    }
     223
     224    /**
     225     * Allows management of photo tags to admins and to moderators who can manage photo_tags.
     226     *
     227     * By default, photo moderators are not permitted to edit photo_tags. However,
     228     * if the user has the 'can_manage_photo_tags' user meta set to 1, they can.
     229     *
     230     * @param string[] $caps    Primitive capabilities required of the user.
     231     * @param string   $cap     Capability being checked.
     232     * @param int      $user_id The user ID.
     233     * @return string[]
     234     */
     235    public static function assign_cap_manage_photo_tags( $caps, $cap, $user_id ) {
     236        // Bail early if this is not for the 'manage_photo_tags' cap.
     237        if ( 'manage_photo_tags' !== $cap ) {
     238            return $caps;
     239        }
     240
     241        $is_caped = function_exists( 'is_caped' ) && is_caped( $user_id );
     242
     243        if ( ! is_user_member_of_blog() && ! $is_caped ) {
     244            return $caps;
     245        }
     246
     247        $allowed = (
     248            // User is caped.
     249            $is_caped
     250        ||
     251            // User is admin.
     252            user_can( $user_id, 'manage_options' )
     253        ||
     254            // User can moderate photos and has associated user meta key set.
     255            (
     256                user_can( $user_id, 'edit_photos' )
     257            &&
     258                get_user_meta( $user_id, self::USER_META_CAN_MANAGE_PHOTO_TAGS, true )
     259            )
     260        );
     261
     262        return $allowed ? [ 'exist' ] : [ 'do_not_allow' ];
    209263    }
    210264
Note: See TracChangeset for help on using the changeset viewer.