Skip to:
Content

bbPress.org


Ignore:
Timestamp:
05/23/2013 07:09:03 AM (11 years ago)
Author:
johnjamesjacoby
Message:

Hierarchical replies:

  • Introduce setting, option, functions, JS, CSS, and Walker class to support hierarchical replies.
  • Tweak functions where saving the additional reply_to meta data is necessary.
  • Add meta data field in dashboard to show the reply_to ID.
  • There will likely be more tweaking necessary, as we test this further and get more eyes on the code.
  • Fixes #2036.
  • Props jmdodd for this huge effort.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/common/classes.php

    r4501 r4944  
    260260}
    261261
     262/**
     263 * Create hierarchical list of bbPress replies.
     264 *
     265 * @package bbPress
     266 * @subpackage Classes
     267 *
     268 * @since bbPress (r4944)
     269 */
     270class BBP_Walker_Reply extends Walker {
     271
     272    /**
     273     * @see Walker::$tree_type
     274     *
     275     * @since bbPress (r4944)
     276     *
     277     * @var string
     278     */
     279    var $tree_type = 'reply';
     280
     281    /**
     282     * @see Walker::$db_fields
     283     *
     284     * @since bbPress (r4944)
     285     *
     286     * @var array
     287     */
     288    var $db_fields = array(
     289        'parent' => 'reply_to',
     290        'id'     => 'ID'
     291    );
     292
     293    /**
     294     * @see Walker::start_lvl()
     295     *
     296     * @since bbPress (r4944)
     297     *
     298     * @param string $output Passed by reference. Used to append additional content
     299     * @param int $depth Depth of reply
     300     * @param array $args Uses 'style' argument for type of HTML list
     301     */
     302    public function start_lvl( &$output = '', $depth = 0, $args = array() ) {
     303        bbpress()->reply_query->reply_depth = $depth + 1;
     304
     305        switch ( $args['style'] ) {
     306            case 'div':
     307                break;
     308            case 'ol':
     309                echo "<ol class='bbp-threaded-replies'>\n";
     310                break;
     311            case 'ul':
     312            default:
     313                echo "<ul class='bbp-threaded-replies'>\n";
     314                break;
     315        }
     316    }
     317
     318    /**
     319     * @see Walker::end_lvl()
     320     *
     321     * @since bbPress (r4944)
     322     *
     323     * @param string $output Passed by reference. Used to append additional content
     324     * @param int $depth Depth of reply
     325     * @param array $args Will only append content if style argument value is 'ol' or 'ul'
     326     */
     327    public function end_lvl( &$output = '', $depth = 0, $args = array() ) {
     328        bbpress()->reply_query->reply_depth = (int) $depth + 1;
     329
     330        switch ( $args['style'] ) {
     331            case 'div':
     332                break;
     333            case 'ol':
     334                echo "</ol>\n";
     335                break;
     336            case 'ul':
     337            default:
     338                echo "</ul>\n";
     339                break;
     340        }
     341    }
     342
     343    /**
     344     * @since bbPress (r4944)
     345     */
     346    public function display_element( $element = false, &$children_elements = array(), $max_depth = 0, $depth = 0, $args = array(), &$output = '' ) {
     347
     348        if ( empty( $element ) )
     349            return;
     350
     351        // Get element's id
     352        $id_field = $this->db_fields['id'];
     353        $id       = $element->$id_field;
     354
     355        // Display element
     356        parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
     357
     358        // If we're at the max depth and the current element still has children, loop over those
     359        // and display them at this level to prevent them being orphaned to the end of the list.
     360        if ( ( $max_depth <= (int) $depth + 1 ) && isset( $children_elements[$id] ) ) {
     361            foreach ( $children_elements[$id] as $child ) {
     362                $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
     363            }
     364            unset( $children_elements[$id] );
     365        }
     366    }
     367
     368    /**
     369     * @see Walker:start_el()
     370     *
     371     * @since bbPress (r4944)
     372     */
     373    public function start_el( &$output = '', $reply = false, $depth = 0, $args = array(), $id = 0 ) {
     374
     375        // Set up reply
     376        $depth++;
     377        bbpress()->reply_query->reply_depth = $depth;
     378        bbpress()->reply_query->post        = $reply;
     379        bbpress()->current_reply_id         = $reply->ID;
     380
     381        // Check for a callback and use it if specified
     382        if ( !empty( $args['callback'] ) ) {
     383            call_user_func( $args['callback'], $reply, $args, $depth );
     384            return;
     385        }
     386
     387        // Style for div or list element
     388        if ( 'div' === $args['style'] ) {
     389            $tag = 'div';
     390        } else {
     391            $tag = 'li';
     392        } ?>
     393
     394        <<?php echo $tag ?>>
     395
     396            <?php bbp_get_template_part( 'loop', 'single-reply' ); ?>
     397
     398        </<?php echo $tag ?>>
     399
     400        <?php
     401    }
     402
     403    /**
     404     * @since bbPress (r4944)
     405     */
     406    public function end_el( &$output = '', $reply = false, $depth = 0, $args = array() ) {
     407
     408        // Check for a callback and use it if specified
     409        if ( !empty( $args['end-callback'] ) ) {
     410            call_user_func( $args['end-callback'], $reply, $args, $depth );
     411            return;
     412        }
     413
     414        // Style for div or list element
     415        if ( !empty( $args['style'] ) && ( 'div' === $args['style'] ) ) {
     416            echo "</div>\n";
     417        } else {
     418            echo "</li>\n";
     419        }
     420    }
     421}
    262422endif; // class_exists check
Note: See TracChangeset for help on using the changeset viewer.