Making WordPress.org

Changeset 5629


Ignore:
Timestamp:
07/07/2017 08:12:06 PM (7 years ago)
Author:
SergeyBiryukov
Message:

Support Theme: Introduce a function to count user's topics, excluding reviews.

The previous method of substracting the number of reviews from the total number of topics in [5617] was not really accurate and could sometimes result in a negative number.

See #2772.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-support
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-support/bbpress/user-profile.php

    r5617 r5629  
    116116
    117117        <p class="bbp-user-topic-count"><?php
    118             $review_count = wporg_support_get_user_review_count();
    119 
    120118            /* translators: %s: number of user's topics */
    121             printf( esc_html__( 'Topics Started: %s', 'wporg-forums' ), number_format_i18n( bbp_get_user_topic_count_raw() - $review_count ) );
     119            printf( esc_html__( 'Topics Started: %s', 'wporg-forums' ), number_format_i18n( wporg_support_get_user_topics_count() ) );
    122120        ?></p>
    123121
     
    129127        <p class="bbp-user-review-count"><?php
    130128            /* translators: %s: number of user's reviews */
    131             printf( esc_html__( 'Reviews Written: %s', 'wporg-forums' ), number_format_i18n( $review_count ) );
     129            printf( esc_html__( 'Reviews Written: %s', 'wporg-forums' ), number_format_i18n( wporg_support_get_user_reviews_count() ) );
    132130        ?></p>
    133131    </div>
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-support/functions.php

    r5626 r5629  
    215215
    216216/**
     217 * Return the raw database count of topics by a user, excluding reviews.
     218 *
     219 * @global wpdb $wpdb WordPress database abstraction object.
     220 *
     221 * @param int $user_id User ID to get count for.
     222 * @return int Raw DB count of topics.
     223 */
     224function wporg_support_get_user_topics_count( $user_id = 0 ) {
     225    global $wpdb;
     226
     227    $user_id = bbp_get_user_id( $user_id );
     228    if ( empty( $user_id ) ) {
     229        return 0;
     230    }
     231
     232    if ( ! class_exists( 'WordPressdotorg\Forums\Plugin' ) ) {
     233        return 0;
     234    }
     235
     236    $count = (int) $wpdb->get_var( $wpdb->prepare(
     237        "SELECT COUNT(*)
     238            FROM {$wpdb->posts}
     239            WHERE post_type = 'topic'
     240                AND post_status IN ( 'publish', 'closed' )
     241                AND post_parent <> %d
     242                AND post_author = %d",
     243        WordPressdotorg\Forums\Plugin::REVIEWS_FORUM_ID,
     244        $user_id
     245    ) );
     246
     247    return $count;
     248}
     249
     250/**
    217251 * Return the raw database count of reviews by a user.
    218252 *
     
    222256 * @return int Raw DB count of reviews.
    223257 */
    224 function wporg_support_get_user_review_count( $user_id = 0 ) {
     258function wporg_support_get_user_reviews_count( $user_id = 0 ) {
    225259    global $wpdb;
    226260
Note: See TracChangeset for help on using the changeset viewer.