Skip to:
Content

bbPress.org

Changeset 2918


Ignore:
Timestamp:
02/20/2011 07:44:13 AM (15 years ago)
Author:
johnjamesjacoby
Message:

Add function for updating the anonymous reply count of a topic. Uncomment new topic redirection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-topic-functions.php

    r2914 r2918  
    173173
    174174                // Redirect back to new reply
    175                 //wp_redirect( bbp_get_topic_permalink( $topic_id ) . '#topic-' . $topic_id );
     175                wp_redirect( bbp_get_topic_permalink( $topic_id ) . '#topic-' . $topic_id );
    176176
    177177                // For good measure
    178                 //exit();
     178                exit();
    179179
    180180            // Errors to report
     
    15591559    if ( bbp_is_reply( $topic_id ) )
    15601560        $topic_id = bbp_get_reply_topic_id( $topic_id );
     1561    elseif ( bbp_is_topic( $topic_id ) )
     1562        $topic_id = bbp_get_topic_id( $topic_id );
    15611563    else
     1564        return;
     1565
     1566    // There should always be at least 1 voice
     1567    if ( !$voices = $wpdb->get_col( $wpdb->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = 'publish' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) ) )
     1568        $voices = 1;
     1569
     1570    update_post_meta( $topic_id, '_bbp_voice_count', (int) $voices );
     1571
     1572    return apply_filters( 'bbp_update_topic_voice_count', (int) $voices, $topic_id );
     1573}
     1574
     1575/**
     1576 * Adjust the total anonymous reply count of a topic
     1577 *
     1578 * @since bbPress (r2567)
     1579 *
     1580 * @param int $topic_id Optional. Topic id to update
     1581 * @uses bbp_get_topic_id() To get the topic id
     1582 * @uses get_post_field() To get the post type of the supplied id
     1583 * @uses bbp_get_reply_topic_id() To get the reply topic id
     1584 * @uses wpdb::prepare() To prepare our sql query
     1585 * @uses wpdb::get_col() To execute our query and get the column back
     1586 * @uses update_post_meta() To update the topic voice count meta
     1587 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
     1588 *                        count and topic id
     1589 * @return bool False on failure, voice count on success
     1590 */
     1591function bbp_update_topic_anonymous_reply_count( $topic_id = 0 ) {
     1592    global $wpdb;
     1593
     1594    // If it's a reply, then get the parent (topic id)
     1595    if ( bbp_is_reply( $topic_id ) )
     1596        $topic_id = bbp_get_reply_topic_id( $topic_id );
     1597    elseif ( bbp_is_topic( $topic_id ) )
    15621598        $topic_id = bbp_get_topic_id( $topic_id );
    1563 
    1564     // If it is not a topic or reply, then we don't need it
    1565     if ( !in_array( get_post_field( 'post_type', $topic_id ), array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) )
    1566         return false;
    1567 
    1568     // If it's a reply, then get the parent (topic id)
    1569     if ( bbp_get_reply_post_type() == get_post_field( 'post_type', $topic_id ) )
    1570         $topic_id = bbp_get_reply_topic_id( $topic_id );
    1571 
    1572     // There should always be at least 1 voice
    1573     if ( !$voices = count( $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = 'publish' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) ) ) )
    1574         $voices = 1;
    1575 
    1576     update_post_meta( $topic_id, '_bbp_voice_count', (int) $voices );
    1577 
    1578     return apply_filters( 'bbp_update_topic_voice_count', (int) $voices, $topic_id );
     1599    else
     1600        return;
     1601
     1602    $anonymous_replies = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( ID ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = 'publish' AND post_type = '%s' AND post_author = 0 ) OR ( ID = %d AND post_type = '%s' AND post_author = 0 );", $topic_id, bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
     1603
     1604    update_post_meta( $topic_id, '_bbp_anonymous_reply_count', (int) $anonymous_replies );
     1605
     1606    return apply_filters( 'bbp_update_topic_anonymous_reply_count', (int) $anonymous_replies, $topic_id );
    15791607}
    15801608
Note: See TracChangeset for help on using the changeset viewer.