Skip to:
Content

bbPress.org

Changeset 5390


Ignore:
Timestamp:
06/12/2014 02:16:50 AM (5 years ago)
Author:
johnjamesjacoby
Message:

Introduce bbp_get_reply_ancestors() function to help with reply hierarchy dropdown. Also conveniently wraps logic incase we ever move to a real hierarchical post type. See #2617.

File:
1 edited

Legend:

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

    r5378 r5390  
    11141114    // Validation
    11151115    $reply_id = bbp_get_reply_id( $reply_id );
    1116     $reply_to = bbp_validate_reply_to( $reply_to );
     1116    $reply_to = bbp_validate_reply_to( $reply_to, $reply_id );
    11171117
    11181118    // Update or delete the `reply_to` postmeta
     
    11301130
    11311131    return (int) apply_filters( 'bbp_update_reply_to', (int) $reply_to, $reply_id );
     1132}
     1133
     1134/**
     1135 * Get all ancestors to a reply
     1136 *
     1137 * Because settings can be changed, this function does not care if hierarchical
     1138 * replies are active or to what depth.
     1139 *
     1140 * @since bbPress (r5390)
     1141 *
     1142 * @param int $reply_id
     1143 * @return array
     1144 */
     1145function bbp_get_reply_ancestors( $reply_id = 0 ) {
     1146   
     1147    // Validation
     1148    $reply_id  = bbp_get_reply_id( $reply_id );
     1149    $ancestors = array();
     1150
     1151    // Reply id is valid
     1152    if ( ! empty( $reply_id ) ) {
     1153
     1154        // Try to get reply parent
     1155        $reply_to = bbp_get_reply_to( $reply_id );
     1156
     1157        // Reply has a hierarchical parent
     1158        if ( ! empty( $reply_to ) ) {
     1159
     1160            // Setup the current ID and current post as an ancestor
     1161            $id = $ancestors[] = $reply_to;
     1162
     1163            // Get parent reply
     1164            while ( $ancestor = bbp_get_reply( $id ) ) {
     1165
     1166                // Does parent have a parent?
     1167                $grampy_id = bbp_get_reply_to( $ancestor->ID );
     1168
     1169                // Loop detection: If the ancestor has been seen before, break.
     1170                if ( empty( $ancestor->post_parent ) || ( $grampy_id === $reply_id ) || in_array( $grampy_id, $ancestors ) ) {
     1171                    break;
     1172                }
     1173
     1174                $id = $ancestors[] = $grampy_id;
     1175            }
     1176        }
     1177    }
     1178
     1179    return apply_filters( 'bbp_get_reply_ancestors', $ancestors, $reply_id );
    11321180}
    11331181
Note: See TracChangeset for help on using the changeset viewer.