Making WordPress.org

Changeset 4269


Ignore:
Timestamp:
10/19/2016 07:09:38 PM (8 years ago)
Author:
coffee2code
Message:

Support Forums, User Badges: Allow author badges to display for topics as well.

Also adds some missing docblocks and explicitly return false rather than null in a few places.

Fixes #2146.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges/inc/class-plugin.php

    r4205 r4269  
    5252    public function bbp_loaded() {
    5353        // Add class to div containing reply.
     54        add_filter( 'bbp_get_topic_class', array( $this, 'bbp_get_topic_class' ), 10, 2 );
    5455        add_filter( 'bbp_get_reply_class', array( $this, 'bbp_get_reply_class' ), 10, 2 );
    5556
    5657        // Add badge before reply author info.
    57         add_action( 'bbp_theme_before_reply_author_details', array( $this, 'add_user_badges' ) );
    58     }
    59 
    60     protected function get_author_badge_info() {
     58        add_action( 'bbp_theme_before_topic_author_details', array( $this, 'show_topic_author_badge' ) );
     59        add_action( 'bbp_theme_before_reply_author_details', array( $this, 'show_reply_author_badge' ) );
     60    }
     61
     62    /**
     63     * Return information about the resource item author if they merit a badge.
     64     *
     65     * Author badge is only applicable in support or reviews forums for a plugin
     66     * or theme to which the author is listed as a committer or a contributor.
     67     *
     68     * @access protected
     69     *
     70     * @param string $item_type The type of thing whose author is being checked
     71     *                          for badge info. One of 'topic' or 'reply'.
     72     * @param int    $item_id   The ID of the item getting badge assigned.
     73     * @return array|false      Associative array with keys 'type', 'slug', and
     74     *                          'user_login' if author merits a badge, else false.
     75     */
     76    protected function get_author_badge_info( $item_type, $item_id ) {
    6177        if ( ! class_exists( '\WordPressdotorg\Forums\Plugin' ) ) {
    62             return;
     78            return false;
    6379        }
    6480
     
    6985        );
    7086
    71         $forum_id = bbp_get_reply_forum_id();
    72         $topic_id = bbp_get_reply_topic_id();
     87        if ( 'topic' === $item_type ) {
     88            $forum_id = bbp_get_topic_forum_id();
     89            $topic_id = $item_id;
     90            $user_id  = bbp_get_topic_author_id();
     91        } else {
     92            $forum_id = bbp_get_reply_forum_id();
     93            $topic_id = bbp_get_reply_topic_id();
     94            $user_id  = bbp_get_reply_author_id();
     95        }
    7396
    7497        if ( ! in_array( $forum_id, $badgeable_forums ) ) {
    75             return;
     98            return false;
     99        }
     100
     101        if ( ! $user_id ) {
     102            return false;
    76103        }
    77104
    78105        $slugs = $types = array();
    79 
    80         $user_id = bbp_get_reply_author_id();
    81         if ( ! $user_id ) {
    82             return;
    83         }
    84106
    85107        $user_login = get_user_by( 'id', $user_id )->user_login;
     
    100122        // Else not a type of concern.
    101123        else {
    102             return;
     124            return false;
    103125        }
    104126
     
    111133
    112134        if ( ! $slugs ) {
    113             return;
     135            return false;
    114136        }
    115137
     
    121143    }
    122144
     145    /**
     146     * Amends the provided classes for a given topic with badge-related classes.
     147     *
     148     * @param array $classes  Array of existing classes.
     149     * @param int   $topic_id The ID of the topic.
     150     * @return array
     151     */
     152    public function bbp_get_topic_class( $classes, $topic_id ) {
     153        return $this->get_badge_class( $classes, 'topic', $topic_id );
     154    }
     155
     156    /**
     157     * Amends the provided classes for a given reply with badge-related classes.
     158     *
     159     * @param array $classes  Array of existing classes.
     160     * @param int   $reply_id The ID of the reply.
     161     * @return array
     162     */
    123163    public function bbp_get_reply_class( $classes, $reply_id ) {
     164        return $this->get_badge_class( $classes, 'reply', $reply_id );
     165    }
     166
     167    /**
     168     * Amends the provided classes with badge-related classes.
     169     *
     170     * Possible badge classes:
     171     * - by-moderator (Note: will always be added if author is a moderator)
     172     * - by-plugin-author
     173     * - by-plugin-contributor
     174     * - by-theme-author
     175     * - by-theme-contributor
     176     *
     177     * @access protected
     178     *
     179     * @param array  $classes   Array of existing classes.
     180     * @param string $item_type The type of thing getting badge assigned. One of 'topic' or 'reply'.
     181     * @param int    $item_id   The ID of the item getting badge assigned.
     182     * @return array
     183     */
     184    protected function get_badge_class( $classes, $item_type, $item_id ) {
    124185        // Class related to moderators.
    125186        if ( $this->is_user_moderator() ) {
     
    128189
    129190        // Class related to plugin and theme authors/contributors.
    130         if ( $info = $this->get_author_badge_info() ) {
     191        if ( $info = $this->get_author_badge_info( $item_type, $item_id ) ) {
    131192            if ( $this->is_user_author( $info['user_login'], $info['type'], $info['slug'] ) ) {
    132193                $contrib_type = 'author';
     
    146207
    147208    /**
    148      * Display author badge if reply author is in support or reviews forum for
    149      * the plugin/theme they contribute to.
    150      */
    151     public function add_user_badges() {
    152         $output = $this->get_author_badge();
     209     * Display badge for topic author if they merit a badge.
     210     */
     211    public function show_topic_author_badge() {
     212        $this->show_user_badge( 'topic', bbp_get_topic_id() );
     213    }
     214
     215    /**
     216     * Display badge for reply author if they merit a badge.
     217     */
     218    public function show_reply_author_badge() {
     219        $this->show_user_badge( 'reply', bbp_get_reply_id() );
     220    }
     221
     222    /**
     223     * Display badge if the author merits a badge.
     224     *
     225     * @access protected
     226     *
     227     * @param string $item_type The type of thing getting badge assigned. One of 'topic' or 'reply'.
     228     * @param int    $item_id   The ID of the item getting badge assigned.
     229     */
     230    protected function show_user_badge( $item_type, $item_id ) {
     231        $output = $this->get_author_badge( $item_type, $item_id );
    153232
    154233        // Don't assign moderator badge if already assigning author badge.
     
    162241    }
    163242
     243    /**
     244     * Returns the HTML formatted badge.
     245     *
     246     * @param $type  string The type of badge.
     247     * @param $label string The label for the badge.
     248     * @param $help  string Optional. Help/descriptive text for the badge.
     249     * @return string
     250     */
    164251    protected function format_badge( $type, $label, $help = '' ) {
    165252        $output = '';
     
    178265    }
    179266
    180     protected function get_author_badge() {
    181         if ( ! $info = $this->get_author_badge_info() ) {
    182             return;
     267    /**
     268     * Get badge if the author merits a badge for being a plugin/theme author or
     269     * contributor.
     270     *
     271     * @access protected
     272     *
     273     * @param string $item_type The type of thing getting badge assigned. One of
     274     *                          'topic' or 'reply'.
     275     * @param int    $item_id   The ID of the item getting badge assigned.
     276     * @return array|false      Associative array with keys 'type', 'slug', and
     277     *                          'user_login' if author merits a badge, else null.
     278     */
     279    protected function get_author_badge( $item_type, $item_id ) {
     280        if ( ! $info = $this->get_author_badge_info( $item_type, $item_id ) ) {
     281            return false;
    183282        }
    184283
     
    208307    }
    209308
     309    /**
     310     * Get badge if the author merits a badge for being a moderator.
     311     *
     312     * @access protected
     313     *
     314     * @return array|false Associative array with keys 'type', 'slug', and
     315     *                     'user_login' if author merits a badge, else false.
     316     */
    210317    protected function get_moderator_badge() {
    211318        $label = $help = null;
Note: See TracChangeset for help on using the changeset viewer.