Skip to:
Content

bbPress.org


Ignore:
Timestamp:
03/05/2014 06:41:02 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Introduce topic & reply counts for users:

  • New functions for updating user options.
  • New action hooks for bumping existing counts on CRUD actions.
  • Uses existing _raw functions when no counts are previously found to incrementally update missing meta values.
  • Tool for updating topic & reply counts already exists.
  • Props netweb, MZAWeb, anointed.
  • Fixes #1694.
File:
1 edited

Legend:

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

    r5187 r5309  
    156156
    157157    return apply_filters( 'bbp_current_author_ua', $retval );
    158 }
    159 
    160 /** Post Counts ***************************************************************/
    161 
    162 /**
    163  * Return the raw database count of topics by a user
    164  *
    165  * @since bbPress (r3633)
    166  * @global WPDB $wpdb
    167  * @uses bbp_get_user_id()
    168  * @uses get_posts_by_author_sql()
    169  * @uses bbp_get_topic_post_type()
    170  * @uses apply_filters()
    171  * @return int Raw DB count of topics
    172  */
    173 function bbp_get_user_topic_count_raw( $user_id = 0 ) {
    174     $user_id = bbp_get_user_id( $user_id );
    175     if ( empty( $user_id ) )
    176         return false;
    177 
    178     global $wpdb;
    179 
    180     $where = get_posts_by_author_sql( bbp_get_topic_post_type(), true, $user_id );
    181     $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" );
    182 
    183     return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id );
    184 }
    185 
    186 /**
    187  * Return the raw database count of replies by a user
    188  *
    189  * @since bbPress (r3633)
    190  * @global WPDB $wpdb
    191  * @uses bbp_get_user_id()
    192  * @uses get_posts_by_author_sql()
    193  * @uses bbp_get_reply_post_type()
    194  * @uses apply_filters()
    195  * @return int Raw DB count of replies
    196  */
    197 function bbp_get_user_reply_count_raw( $user_id = 0 ) {
    198     $user_id = bbp_get_user_id( $user_id );
    199     if ( empty( $user_id ) )
    200         return false;
    201 
    202     global $wpdb;
    203 
    204     $where = get_posts_by_author_sql( bbp_get_reply_post_type(), true, $user_id );
    205     $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" );
    206 
    207     return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id );
    208158}
    209159
     
    15331483}
    15341484
    1535 /** Premissions ***************************************************************/
     1485/** Post Counts ***************************************************************/
     1486
     1487/**
     1488 * Return the raw database count of topics by a user
     1489 *
     1490 * @since bbPress (r3633)
     1491 * @global WPDB $wpdb
     1492 * @uses bbp_get_user_id()
     1493 * @uses get_posts_by_author_sql()
     1494 * @uses bbp_get_topic_post_type()
     1495 * @uses apply_filters()
     1496 * @return int Raw DB count of topics
     1497 */
     1498function bbp_get_user_topic_count_raw( $user_id = 0 ) {
     1499    $user_id = bbp_get_user_id( $user_id );
     1500    if ( empty( $user_id ) ) {
     1501        return false;
     1502    }
     1503
     1504    global $wpdb;
     1505
     1506    $where = get_posts_by_author_sql( bbp_get_topic_post_type(), true, $user_id );
     1507    $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" );
     1508
     1509    return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id );
     1510}
     1511
     1512/**
     1513 * Return the raw database count of replies by a user
     1514 *
     1515 * @since bbPress (r3633)
     1516 * @global WPDB $wpdb
     1517 * @uses bbp_get_user_id()
     1518 * @uses get_posts_by_author_sql()
     1519 * @uses bbp_get_reply_post_type()
     1520 * @uses apply_filters()
     1521 * @return int Raw DB count of replies
     1522 */
     1523function bbp_get_user_reply_count_raw( $user_id = 0 ) {
     1524    $user_id = bbp_get_user_id( $user_id );
     1525    if ( empty( $user_id ) ) {
     1526        return false;
     1527    }
     1528
     1529    global $wpdb;
     1530
     1531    $where = get_posts_by_author_sql( bbp_get_reply_post_type(), true, $user_id );
     1532    $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" );
     1533
     1534    return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id );
     1535}
     1536
     1537/**
     1538 * Bump the topic count for a user by a certain amount.
     1539 *
     1540 * @since bbPress (r5309)
     1541 *
     1542 * @param int $user_id
     1543 * @param int $difference
     1544 * @uses bbp_get_user_topic_count() To get the users current topic count
     1545 * @uses bbp_set_user_topic_count() To set the users new topic count
     1546 */
     1547function bbp_bump_user_topic_count( $user_id = 0, $difference = 1 ) {
     1548
     1549    // Validate user ID
     1550    $user_id = bbp_get_user_id( $user_id );
     1551    if ( empty( $user_id ) ) {
     1552        return false;
     1553    }
     1554
     1555    // Check meta for count, or query directly if not found
     1556    $count = bbp_get_user_topic_count( $user_id, true );
     1557    if ( empty( $count ) ) {
     1558        $count = bbp_get_user_topic_count_raw( $user_id );
     1559    }
     1560
     1561    // Add them up and filter them
     1562    $new_count = apply_filters( 'bbp_bump_user_topic_count', ( (int) $count + (int) $difference ), $user_id, $difference, $count );
     1563
     1564    return bbp_update_user_topic_count( $user_id, $new_count );
     1565}
     1566
     1567/**
     1568 * Bump the reply count for a user by a certain amount.
     1569 *
     1570 * @since bbPress (r5309)
     1571 *
     1572 * @param int $user_id
     1573 * @param int $difference
     1574 * @uses bbp_get_user_reply_count() To get the users current reply count
     1575 * @uses bbp_set_user_reply_count() To set the users new reply count
     1576 */
     1577function bbp_bump_user_reply_count( $user_id = 0, $difference = 1 ) {
     1578
     1579    // Validate user ID
     1580    $user_id = bbp_get_user_id( $user_id );
     1581    if ( empty( $user_id ) ) {
     1582        return false;
     1583    }
     1584
     1585    // Check meta for count, or query directly if not found
     1586    $count = bbp_get_user_reply_count( $user_id, true );
     1587    if ( empty( $count ) ) {
     1588        $count = bbp_get_user_reply_count_raw( $user_id );
     1589    }
     1590
     1591    // Add them up and filter them
     1592    $new_count = apply_filters( 'bbp_bump_user_reply_count', ( (int) $count + (int) $difference ), $user_id, $difference, $count );
     1593
     1594    return bbp_update_user_reply_count( $user_id, $new_count );
     1595}
     1596
     1597/**
     1598 * Helper function used to increase (by one) the count of topics for a user when
     1599 * a topic is published.
     1600 *
     1601 * @since bbPress (r5309)
     1602 *
     1603 * @access
     1604 * @param $topic_id
     1605 * @param $forum_id
     1606 * @param $anonymous_data
     1607 * @param $topic_author
     1608 */
     1609function bbp_increase_user_topic_count( $topic_id = 0 ) {
     1610    $user_id = bbp_get_topic_author_id( $topic_id );
     1611    return bbp_bump_user_topic_count( $user_id, 1 );
     1612}
     1613
     1614/**
     1615 * Helper function used to increase (by one) the count of replies for a user when
     1616 * a reply is published.
     1617 *
     1618 * This is a helper function, hooked to `bbp_new_reply`
     1619 *
     1620 * @since bbPress (r5309)
     1621 *
     1622 * @param $topic_id
     1623 * @param $forum_id
     1624 * @param $anonymous_data
     1625 * @param $topic_author
     1626 */
     1627function bbp_increase_user_reply_count( $reply_id = 0 ) {
     1628    $user_id = bbp_get_reply_author_id( $reply_id );
     1629    return bbp_bump_user_reply_count( $user_id, 1 );
     1630}
     1631
     1632/**
     1633 * Helper function used to decrease (by one) the count of topics for a user when
     1634 * a topic is unpublished.
     1635 *
     1636 * @since bbPress (r5309)
     1637 *
     1638 * @param $topic_id
     1639 */
     1640function bbp_decrease_user_topic_count( $topic_id = 0 ) {
     1641    $user_id = bbp_get_topic_author_id( $topic_id );
     1642    return bbp_bump_user_topic_count( $user_id, -1 );
     1643}
     1644
     1645/**
     1646 * Helper function used to increase (by one) the count of replies for a user when
     1647 * a topic is unpublished.
     1648 *
     1649 * @since bbPress (r5309)
     1650 *
     1651 * @param $reply_id
     1652 */
     1653function bbp_decrease_user_reply_count( $reply_id = 0 ) {
     1654    $user_id = bbp_get_reply_author_id( $reply_id );
     1655    return bbp_bump_user_reply_count( $user_id, -1 );
     1656}
     1657
     1658/** Permissions ***************************************************************/
    15361659
    15371660/**
Note: See TracChangeset for help on using the changeset viewer.