Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/01/2012 11:36:53 PM (14 years ago)
Author:
johnjamesjacoby
Message:

Reply Position Improvements:

  • Add _update_ and _raw functions for reply positioning.
  • Use these functions through-out the codebase as needed.
  • Maintains the existing bbp_get_reply_position() behavior, so it's backwards compatible.
  • Fixes #1840.
File:
1 edited

Legend:

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

    r3919 r3934  
    209209
    210210        /** Reply Blacklist *******************************************************/
    211        
     211
    212212        if ( !bbp_check_for_blacklist( $anonymous_data, $reply_author, $reply_title, $reply_content ) )
    213213                bbp_add_error( 'bbp_reply_blacklist', __( '<strong>ERROR</strong>: Your reply cannot be created at this time.', 'bbpress' ) );
    214214
    215215        /** Reply Moderation ******************************************************/
    216        
     216
    217217        $post_status = bbp_get_public_status_id();
    218218        if ( !bbp_check_for_moderation( $anonymous_data, $reply_author, $reply_title, $reply_content ) )
     
    243243                        'post_status'    => $post_status,
    244244                        'post_type'      => bbp_get_reply_post_type(),
    245                         'comment_status' => 'closed'
     245                        'comment_status' => 'closed',
     246                        'menu_order'     => (int) ( bbp_get_topic_reply_count( $topic_id ) + 1 )
    246247                );
    247248
     
    468469
    469470        /** Reply Blacklist *******************************************************/
    470        
     471
    471472        if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_reply_author_id( $reply_id ), $reply_title, $reply_content ) )
    472473                bbp_add_error( 'bbp_reply_blacklist', __( '<strong>ERROR</strong>: Your reply cannot be edited at this time.', 'bbpress' ) );
    473474
    474475        /** Reply Moderation ******************************************************/
    475        
     476
    476477        $post_status = bbp_get_public_status_id();
    477478        if ( !bbp_check_for_moderation( $anonymous_data, bbp_get_reply_author_id( $reply_id ), $reply_title, $reply_content ) )
     
    11741175        // Get pre spam status
    11751176        $reply['post_status'] = get_post_meta( $reply_id, '_bbp_spam_meta_status', true );
    1176        
     1177
    11771178        // Delete pre spam meta
    11781179        delete_post_meta( $reply_id, '_bbp_spam_meta_status' );
     
    13551356
    13561357        if ( bbp_use_autoembed() && is_a( $wp_embed, 'WP_Embed' ) ) {
    1357                 add_filter( 'bbp_get_reply_content', array( $wp_embed, 'autoembed' ), 8 );             
     1358                add_filter( 'bbp_get_reply_content', array( $wp_embed, 'autoembed' ), 8 );
    13581359        }
    13591360}
     
    15001501/**
    15011502 * Redirect if unathorized user is attempting to edit a reply
    1502  * 
     1503 *
    15031504 * @since bbPress (r3605)
    15041505 *
     
    15221523}
    15231524
     1525/** Reply Position ************************************************************/
     1526
     1527/**
     1528 * Update the position of the reply.
     1529 *
     1530 * The reply position is stored in the menu_order column of the posts table.
     1531 * This is done to prevent using a meta_query to retrieve posts in the proper
     1532 * freshness order. By updating the menu_order accordingly, we're able to
     1533 * leverage core WordPress query ordering much more effectively.
     1534 *
     1535 * @since bbPress (r3933)
     1536 *
     1537 * @global type $wpdb
     1538 * @param type $reply_id
     1539 * @param type $reply_position
     1540 * @return mixed
     1541 */
     1542function bbp_update_reply_position( $reply_id = 0, $reply_position = 0 ) {
     1543
     1544        // Bail if reply_id is empty
     1545        $reply_id = bbp_get_reply_id( $reply_id );
     1546        if ( empty( $reply_id ) )
     1547                return false;
     1548
     1549        // If no position was passed, get it from the db and update the menu_order
     1550        if ( empty( $reply_position ) ) {
     1551                $reply_position = bbp_get_reply_position_raw( $reply_id, bbp_get_reply_topic_id( $reply_id ) );
     1552        }
     1553
     1554        // Update the replies' 'menp_order' with the reply position
     1555        global $wpdb;
     1556        $wpdb->update( $wpdb->posts, array( 'menu_order' => $reply_position ), array( 'ID' => $reply_id ) );
     1557
     1558        return (int) $reply_position;
     1559}
     1560
     1561/**
     1562 * Get the position of a reply by querying the DB directly for the replies
     1563 * of a given topic.
     1564 *
     1565 * @since bbPress (r3933)
     1566 *
     1567 * @param int $reply_id
     1568 * @param int $topic_id
     1569 */
     1570function bbp_get_reply_position_raw( $reply_id = 0, $topic_id = 0 ) {
     1571
     1572        // Get required data
     1573        $reply_id    = bbp_get_reply_id( $reply_id );
     1574        $topic_id    = !empty( $topic_id ) ? bbp_get_topic_id( $topic_id ) : bbp_get_reply_topic_id( $reply_id );
     1575
     1576        // If reply is actually the first post in a topic, return 0
     1577        if ( $reply_id != $topic_id ) {
     1578
     1579                // Make sure the topic has replies before running another query
     1580                $reply_count = bbp_get_topic_reply_count( $topic_id );
     1581                if ( !empty( $reply_count ) ) {
     1582
     1583                        // Get reply id's
     1584                        $topic_replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() );
     1585                        if ( !empty( $topic_replies ) ) {
     1586
     1587                                // Reverse replies array and search for current reply position
     1588                                $topic_replies  = array_reverse( $topic_replies );
     1589                                $reply_position = array_search( (string) $reply_id, $topic_replies );
     1590
     1591                                // Bump the position to compensate for the lead topic post
     1592                                $reply_position++;
     1593                        }
     1594                }
     1595        } else {
     1596                $reply_position = 0;
     1597        }
     1598
     1599        return $reply_position;
     1600}
     1601
    15241602?>
Note: See TracChangeset for help on using the changeset viewer.