Skip to:
Content

BuddyPress.org

Changeset 11413


Ignore:
Timestamp:
02/06/2017 07:26:44 PM (8 years ago)
Author:
r-a-y
Message:

Activity: Add parameter to bp_activity_get_comment_depth().

Previously, bp_activity_get_comment_depth() only supported looking for an
activity comment's depth during a queried activity comment loop.

This commit allows a developer to pass either a comment object or an
activity ID to the function so the activity comment depth can be fetched
outside the activity comment loop.

This fixes an issue with a notice displaying in the BuddyPress Activity
admin dashboard.

See #7329.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-template.php

    r11360 r11413  
    22282228 *
    22292229 * @since 2.0.0
    2230  */
    2231 function bp_activity_comment_depth() {
    2232     echo bp_activity_get_comment_depth();
    2233 }
     2230 * @since 2.8.0 Added $comment as a parameter.
     2231 *
     2232 * @param object|int $comment Object of the activity comment or activity comment ID. Usually unnecessary
     2233 *                            when used in activity comment loop.
     2234 */
     2235function bp_activity_comment_depth( $comment = 0 ) {
     2236    echo bp_activity_get_comment_depth( $comment );
     2237}
     2238
    22342239    /**
    22352240     * Return the current activity comment depth.
    22362241     *
    22372242     * @since 2.0.0
    2238      *
    2239      * @return int $depth Depth for the current activity comment.
    2240      */
    2241     function bp_activity_get_comment_depth() {
    2242         global $activities_template;
     2243     * @since 2.8.0 Added $comment as a parameter.
     2244     *
     2245     * @param  object|int $comment Object of the activity comment or activity comment ID. Usually unnecessary
     2246     *                             when used in activity comment loop.
     2247     * @return int
     2248     */
     2249    function bp_activity_get_comment_depth( $comment = 0 ) {
     2250        $depth = 0;
     2251
     2252        // Activity comment loop takes precedence.
     2253        if ( isset( $GLOBALS['activities_template']->activity->current_comment->depth ) ) {
     2254            $depth = $GLOBALS['activities_template']->activity->current_comment->depth;
     2255
     2256        // Get depth for activity comment manually.
     2257        } elseif ( ! empty( $comment ) ) {
     2258            // We passed an activity ID, so fetch the activity object.
     2259            if ( is_int( $comment ) ) {
     2260                $comment = new BP_Activity_Activity( $comment );
     2261            }
     2262
     2263            // Recurse through activity tree to find the depth.
     2264            if ( is_object( $comment ) && isset( $comment->type ) && 'activity_comment' === $comment->type ) {
     2265                // Fetch the entire root comment tree... ugh.
     2266                $comments = BP_Activity_Activity::get_activity_comments( $comment->item_id, 1, constant( 'PHP_INT_MAX' ) );
     2267
     2268                // Recursively find our comment object from the comment tree.
     2269                $iterator  = new RecursiveArrayIterator( $comments );
     2270                $recursive = new RecursiveIteratorIterator( $iterator, RecursiveIteratorIterator::SELF_FIRST );
     2271                foreach ( $recursive as $cid => $cobj ) {
     2272                    // Skip items that are not a comment object.
     2273                    if ( ! is_numeric( $cid ) || ! is_object( $cobj ) ) {
     2274                        continue;
     2275                    }
     2276
     2277                    // We found the activity comment! Set the depth.
     2278                    if ( $cid === $comment->id && isset( $cobj->depth ) ) {
     2279                        $depth = $cobj->depth;
     2280                        break;
     2281                    }
     2282                }
     2283            }
     2284        }
    22432285
    22442286        /**
     
    22492291         * @param int $depth Depth for the current activity comment.
    22502292         */
    2251         return apply_filters( 'bp_activity_get_comment_depth', $activities_template->activity->current_comment->depth );
     2293        return apply_filters( 'bp_activity_get_comment_depth', $depth );
    22522294    }
    22532295
     
    28852927        $comment_depth = isset( $comment->depth )
    28862928            ? intval( $comment->depth )
    2887             : bp_activity_get_comment_depth();
     2929            : bp_activity_get_comment_depth( $comment );
    28882930
    28892931        // Threading is turned on, so check the depth.
Note: See TracChangeset for help on using the changeset viewer.