Skip to:
Content

bbPress.org

Changeset 7236


Ignore:
Timestamp:
02/22/2022 08:50:11 PM (3 years ago)
Author:
johnjamesjacoby
Message:

Statistics: numerous code improvements:

  • Prevent PHP warnings/notices when passing specific arguments and when viewed as a user with specific capabilities
  • Rename "trashed" & "spammed" keys to "trash" & "spam" respectively, to better match their status IDs
  • Refactor bbp_get_statistics() to be simpler internally – includes fully updated docs
  • Add unofficial support for "hidden" topic & reply statuses in statistics
  • Add bbp-stats class to default template part to make it easier to target with custom styling

These changes together vastly improve the experience of using the bbp-stats shortcode.

Props createquest, hasanuzzamanshamim, robin-w.

In trunk for 2.7.0. Fixes #3428.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/common/functions.php

    r7235 r7236  
    305305 *
    306306 * @since 2.0.0 bbPress (r2769)
    307  * @since 2.6.0 bbPress (r6055) Introduced the `count_pending_topics` and
    308  *                               `count_pending_replies` arguments.
     307 * @since 2.6.0 bbPress (r6055)  Added:
     308 *                               `count_pending_topics`
     309 *                               `count_pending_replies`
     310 * @since 2.6.10 bbPress (r7235) Renamed:
     311 *                                `count_trashed_topics`  to `count_trash_topics`
     312 *                                `count_trashed_replies` to `count_trash_replies`
     313 *                                `count_spammed_topics`  to `count_spam_topics`
     314 *                                `count_spammed_replies` to `count_spam_replies`
     315 *                               Added:
     316 *                                `count_hidden_topics`
     317 *                                `count_hidden_replies`
    309318 *
    310319 * @param array $args Optional. The function supports these arguments (all
    311  *                     default to true):
    312  *  - count_users: Count users?
    313  *  - count_forums: Count forums?
    314  *  - count_topics: Count topics? If set to false, private, spammed and trashed
    315  *                   topics are also not counted.
    316  *  - count_pending_topics: Count pending topics? (only counted if the current
     320 *                    default to true):
     321 *
     322 *  - count_users:           Count users?
     323 *  - count_forums:          Count forums?
     324 *  - count_topics:          Count topics? If set to false, private, spam and
     325 *                           trash topics are also not counted.
     326 *  - count_pending_topics:  Count pending topics? (only counted if the current
    317327 *                           user has edit_others_topics cap)
    318  *  - count_private_topics: Count private topics? (only counted if the current
     328 *  - count_private_topics:  Count private topics? (only counted if the current
    319329 *                           user has read_private_topics cap)
    320  *  - count_spammed_topics: Count spammed topics? (only counted if the current
     330 *  - count_hidden_topics:   Count hidden topics? (only counted if the current
     331 *                           user has read_hidden_topics cap)
     332 *  - count_spam_topics:     Count spam topics? (only counted if the current
    321333 *                           user has edit_others_topics cap)
    322  *  - count_trashed_topics: Count trashed topics? (only counted if the current
     334 *  - count_trash_topics:    Count trash topics? (only counted if the current
    323335 *                           user has view_trash cap)
    324  *  - count_replies: Count replies? If set to false, private, spammed and
    325  *                   trashed replies are also not counted.
     336 *  - count_replies:         Count replies? If set to false, private, spam and
     337 *                           trash replies are also not counted.
    326338 *  - count_pending_replies: Count pending replies? (only counted if the current
    327339 *                           user has edit_others_replies cap)
    328340 *  - count_private_replies: Count private replies? (only counted if the current
    329341 *                           user has read_private_replies cap)
    330  *  - count_spammed_replies: Count spammed replies? (only counted if the current
     342 *  - count_hidden_replies:  Count hidden replies? (only counted if the current
     343 *                           user has read_hidden_replies cap)
     344 *  - count_spam_replies:    Count spam replies? (only counted if the current
    331345 *                           user has edit_others_replies cap)
    332  *  - count_trashed_replies: Count trashed replies? (only counted if the current
     346 *  - count_trash_replies:   Count trash replies? (only counted if the current
    333347 *                           user has view_trash cap)
    334  *  - count_tags: Count tags? If set to false, empty tags are also not counted
    335  *  - count_empty_tags: Count empty tags?
    336  * @return object Walked forum tree
     348 *  - count_tags:            Count tags? If set to false, empty tags are also
     349 *                           not counted
     350 *  - count_empty_tags:      Count empty tags?
     351 *
     352 * @return array Array of statistics
    337353 */
    338354function bbp_get_statistics( $args = array() ) {
     
    351367        'count_pending_topics'  => true,
    352368        'count_private_topics'  => true,
    353         'count_spammed_topics'  => true,
    354         'count_trashed_topics'  => true,
     369        'count_spam_topics'     => true,
     370        'count_trash_topics'    => true,
     371        'count_hidden_topics'   => true,
    355372
    356373        // Replies
     
    358375        'count_pending_replies' => true,
    359376        'count_private_replies' => true,
    360         'count_spammed_replies' => true,
    361         'count_trashed_replies' => true,
     377        'count_spam_replies'    => true,
     378        'count_trash_replies'   => true,
     379        'count_hidden_replies'  => true,
    362380
    363381        // Topic tags
     
    373391    $hidden_topic_title = $hidden_reply_title = '';
    374392
     393    // Post statuses
     394    $publish = bbp_get_public_status_id();
     395    $closed  = bbp_get_closed_status_id();
     396    $pending = bbp_get_pending_status_id();
     397    $private = bbp_get_private_status_id();
     398    $hidden  = bbp_get_hidden_status_id();
     399    $spam    = bbp_get_spam_status_id();
     400    $trash   = bbp_get_trash_status_id();
     401
    375402    // Users
    376403    $user_count = ! empty( $r['count_users'] )
     
    380407    // Forums
    381408    $forum_count = ! empty( $r['count_forums'] )
    382         ? wp_count_posts( bbp_get_forum_post_type() )->publish
     409        ? wp_count_posts( bbp_get_forum_post_type() )->{$publish}
    383410        : 0;
    384411
    385     // Post statuses
    386     $pending = bbp_get_pending_status_id();
    387     $private = bbp_get_private_status_id();
    388     $spam    = bbp_get_spam_status_id();
    389     $trash   = bbp_get_trash_status_id();
    390     $closed  = bbp_get_closed_status_id();
     412    // Default capabilities
     413    $caps = array(
     414        'view_trash'           => false,
     415        'read_private_topics'  => false,
     416        'edit_others_topics'   => false,
     417        'read_private_replies' => false,
     418        'edit_others_replies'  => false,
     419        'edit_topic_tags'      => false
     420    );
     421
     422    // Get capabilities
     423    foreach ( $caps as $key => $cap ) {
     424        $caps[ $key ] = current_user_can( $cap );
     425    }
    391426
    392427    // Topics
    393428    if ( ! empty( $r['count_topics'] ) ) {
     429
     430        // Count all topics
    394431        $all_topics  = wp_count_posts( bbp_get_topic_post_type() );
    395432
    396433        // Published (publish + closed)
    397         $topic_count = $all_topics->publish + $all_topics->{$closed};
    398 
    399         if ( current_user_can( 'read_private_topics' ) || current_user_can( 'edit_others_topics' ) || current_user_can( 'view_trash' ) ) {
    400 
    401             // Declare empty arrays
    402             $topics = $topic_titles = array();
    403 
    404             // Pending
    405             $topics['pending'] = ( ! empty( $r['count_pending_topics'] ) && current_user_can( 'edit_others_topics' ) )
    406                 ? (int) $all_topics->{$pending}
    407                 : 0;
    408 
    409             // Private
    410             $topics['private'] = ( ! empty( $r['count_private_topics'] ) && current_user_can( 'read_private_topics' ) )
    411                 ? (int) $all_topics->{$private}
    412                 : 0;
    413 
    414             // Spam
    415             $topics['spammed'] = ( ! empty( $r['count_spammed_topics'] ) && current_user_can( 'edit_others_topics'  ) )
    416                 ? (int) $all_topics->{$spam}
    417                 : 0;
    418 
    419             // Trash
    420             $topics['trashed'] = ( ! empty( $r['count_trashed_topics'] ) && current_user_can( 'view_trash' ) )
    421                 ? (int) $all_topics->{$trash}
    422                 : 0;
    423 
    424             // Total hidden (pending + private + spam + trash)
    425             $topic_count_hidden = $topics['pending'] + $topics['private'] + $topics['spammed'] + $topics['trashed'];
    426 
    427             // Generate the hidden topic count's title attribute
    428             $topic_titles[] = ! empty( $topics['pending'] )
    429                 ? sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $topics['pending'] ) )
    430                 : '';
    431 
    432             $topic_titles[] = ! empty( $topics['private'] )
    433                 ? sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $topics['private'] ) )
    434                 : '';
    435 
    436             $topic_titles[] = ! empty( $topics['spammed'] )
    437                 ? sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $topics['spammed'] ) )
    438                 : '';
    439 
    440             $topic_titles[] = ! empty( $topics['trashed'] )
    441                 ? sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $topics['trashed'] ) )
    442                 : '';
    443 
    444             // Compile the hidden topic title
    445             $hidden_topic_title = implode( ' | ', array_filter( $topic_titles ) );
    446         }
     434        $topic_count = $all_topics->{$publish} + $all_topics->{$closed};
     435
     436        // Declare empty arrays
     437        $topics = $topic_titles = array_fill_keys( bbp_get_non_public_topic_statuses(), '' );
     438
     439        // Pending
     440        if ( ! empty( $r['count_pending_topics'] ) && ! empty( $caps['edit_others_topics'] ) ) {
     441            $topics[ $pending ]       = bbp_number_not_negative( $all_topics->{$pending} );
     442            $topic_titles[ $pending ] = sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $pending ] ) );
     443        }
     444
     445        // Private
     446        if ( ! empty( $r['count_private_topics'] ) && ! empty( $caps['read_private_topics'] ) ) {
     447            $topics[ $private ]       = bbp_number_not_negative( $all_topics->{$private} );
     448            $topic_titles[ $private ] = sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $private ] ) );
     449        }
     450
     451        // Hidden
     452        if ( ! empty( $r['count_hidden_topics'] ) && ! empty( $caps['read_hidden_topics'] ) ) {
     453            $topics[ $hidden ]       = bbp_number_not_negative( $all_topics->{$hidden} );
     454            $topic_titles[ $hidden ] = sprintf( esc_html__( 'Hidden: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $hidden ] ) );
     455        }
     456
     457        // Spam
     458        if ( ! empty( $r['count_spam_topics'] ) && ! empty( $caps['edit_others_topics'] ) ) {
     459            $topics[ $spam ]       = bbp_number_not_negative( $all_topics->{$spam} );
     460            $topic_titles[ $spam ] = sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $spam ] ) );
     461        }
     462
     463        // Trash
     464        if ( ! empty( $r['count_trash_topics'] ) && ! empty( $caps['view_trash'] ) ) {
     465            $topics[ $trash ]       = bbp_number_not_negative( $all_topics->{$trash} );
     466            $topic_titles[ $trash ] = sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $trash ] ) );
     467        }
     468
     469        // Total hidden (pending, private, hidden, spam, trash)
     470        $topic_count_hidden = array_sum( array_filter( $topics ) );
     471
     472        // Compile the hidden topic title
     473        $hidden_topic_title = implode( ' | ', array_filter( $topic_titles ) );
    447474    }
    448475
     
    450477    if ( ! empty( $r['count_replies'] ) ) {
    451478
     479        // Count all replies
    452480        $all_replies = wp_count_posts( bbp_get_reply_post_type() );
    453481
    454482        // Published
    455         $reply_count = $all_replies->publish;
    456 
    457         if ( current_user_can( 'read_private_replies' ) || current_user_can( 'edit_others_replies' ) || current_user_can( 'view_trash' ) ) {
    458 
    459             // Declare empty arrays
    460             $replies = $reply_titles = array();
    461 
    462             // Pending
    463             $replies['pending'] = ( ! empty( $r['count_pending_replies'] ) && current_user_can( 'edit_others_replies' ) )
    464                 ? (int) $all_replies->{$pending}
    465                 : 0;
    466 
    467             // Private
    468             $replies['private'] = ( ! empty( $r['count_private_replies'] ) && current_user_can( 'read_private_replies' ) )
    469                 ? (int) $all_replies->{$private}
    470                 : 0;
    471 
    472             // Spam
    473             $replies['spammed'] = ( ! empty( $r['count_spammed_replies'] ) && current_user_can( 'edit_others_replies'  ) )
    474                 ? (int) $all_replies->{$spam}
    475                 : 0;
    476 
    477             // Trash
    478             $replies['trashed'] = ( ! empty( $r['count_trashed_replies'] ) && current_user_can( 'view_trash' ) )
    479                 ? (int) $all_replies->{$trash}
    480                 : 0;
    481 
    482             // Total hidden (pending + private + spam + trash)
    483             $reply_count_hidden = $replies['pending'] + $replies['private'] + $replies['spammed'] + $replies['trashed'];
    484 
    485             // Generate the hidden topic count's title attribute
    486             $reply_titles[] = ! empty( $replies['pending'] )
    487                 ? sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $replies['pending'] ) )
    488                 : '';
    489             $reply_titles[] = ! empty( $replies['private'] )
    490                 ? sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $replies['private'] ) )
    491                 : '';
    492 
    493             $reply_titles[] = ! empty( $replies['spammed'] )
    494                 ? sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $replies['spammed'] ) )
    495                 : '';
    496 
    497             $reply_titles[] = ! empty( $replies['trashed'] )
    498                 ? sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $replies['trashed'] ) )
    499                 : '';
    500 
    501             // Compile the hidden replies title
    502             $hidden_reply_title = implode( ' | ', array_filter( $reply_titles ) );
    503         }
     483        $reply_count = $all_replies->{$publish};
     484
     485        // Declare empty arrays
     486        $topics = $topic_titles = array_fill_keys( bbp_get_non_public_reply_statuses(), '' );
     487
     488        // Pending
     489        if ( ! empty( $r['count_pending_replies'] ) && ! empty( $caps['edit_others_replies'] ) ) {
     490            $replies[ $pending ]      = bbp_number_not_negative( $all_replies->{$pending} );
     491            $reply_titles[ $pending ] = sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $pending ] ) );
     492        }
     493
     494        // Private
     495        if ( ! empty( $r['count_private_replies'] ) && ! empty( $caps['read_private_replies'] ) ) {
     496            $replies[ $private ]      = bbp_number_not_negative( $all_replies->{$private} );
     497            $reply_titles[ $private ] = sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $private ] ) );
     498        }
     499
     500        // Hidden
     501        if ( ! empty( $r['count_hidden_replies'] ) && ! empty( $caps['read_hidden_replies'] ) ) {
     502            $replies[ $hidden ]      = bbp_number_not_negative( $all_replies->{$hidden} );
     503            $reply_titles[ $hidden ] = sprintf( esc_html__( 'Hidden: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $hidden ] ) );
     504        }
     505
     506        // Spam
     507        if ( ! empty( $r['count_spam_replies'] ) && ! empty( $caps['edit_others_replies'] ) ) {
     508            $replies[ $spam ]      = bbp_number_not_negative( $all_replies->{$spam} );
     509            $reply_titles[ $spam ] = sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $spam ] ) );
     510        }
     511
     512        // Trash
     513        if ( ! empty( $r['count_trash_replies'] ) && ! empty( $caps['view_trash'] ) ) {
     514            $replies[ $trash ]      = bbp_number_not_negative( $all_replies->{$trash} );
     515            $reply_titles[ $trash ] = sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $trash ] ) );
     516        }
     517
     518        // Total hidden (pending, private, hidden, spam, trash)
     519        $reply_count_hidden = array_sum( $replies );
     520
     521        // Compile the hidden replies title
     522        $hidden_reply_title = implode( ' | ', $reply_titles );
    504523    }
    505524
     
    507526    if ( ! empty( $r['count_tags'] ) && bbp_allow_topic_tags() ) {
    508527
     528        // Get the topic-tag taxonomy ID
     529        $tt_id = bbp_get_topic_tag_tax_id();
     530
    509531        // Get the count
    510         $topic_tag_count = wp_count_terms( bbp_get_topic_tag_tax_id(), array( 'hide_empty' => true ) );
     532        $topic_tag_count = wp_count_terms( $tt_id, array( 'hide_empty' => true ) );
    511533
    512534        // Empty tags
    513         if ( ! empty( $r['count_empty_tags'] ) && current_user_can( 'edit_topic_tags' ) ) {
    514             $empty_topic_tag_count = wp_count_terms( bbp_get_topic_tag_tax_id() ) - $topic_tag_count;
     535        if ( ! empty( $r['count_empty_tags'] ) && ! empty( 'edit_topic_tags' ) ) {
     536            $empty_topic_tag_count = wp_count_terms( $tt_id ) - $topic_tag_count;
    515537        }
    516538    }
    517539
    518540    // Tally the tallies
    519     $counts = array_filter( array_map( 'absint', compact(
     541    $counts = array_map( 'absint', compact(
    520542        'user_count',
    521543        'forum_count',
     
    526548        'topic_tag_count',
    527549        'empty_topic_tag_count'
    528     ) ) );
     550    ) );
    529551
    530552    // Define return value
    531553    $statistics = array();
    532554
    533     // Loop through and store the integer and i18n formatted counts.
     555    // Loop through and store the integer and i18n formatted counts
    534556    foreach ( $counts as $key => $count ) {
    535         $statistics[ $key ]         = bbp_number_format_i18n( $count );
    536         $statistics[ "{$key}_int" ] = $count;
    537     }
    538 
    539     // Add the hidden (topic/reply) count title attribute strings because we
    540     // don't need to run the math functions on these (see above)
     557        $not_negative               = bbp_number_not_negative( $count );
     558        $statistics[ $key ]         = bbp_number_format_i18n( $not_negative );
     559        $statistics[ "{$key}_int" ] = $not_negative;
     560    }
     561
     562    // Add the hidden (topic/reply) count title attribute strings
    541563    $statistics['hidden_topic_title'] = $hidden_topic_title;
    542564    $statistics['hidden_reply_title'] = $hidden_reply_title;
  • trunk/src/templates/default/bbpress/content-statistics.php

    r7006 r7236  
    1414$stats = bbp_get_statistics(); ?>
    1515
    16 <dl role="main">
     16<dl role="main" class="bbp-stats">
    1717
    1818    <?php do_action( 'bbp_before_statistics' ); ?>
Note: See TracChangeset for help on using the changeset viewer.