Skip to:
Content

BuddyPress.org

Changeset 14149


Ignore:
Timestamp:
12/16/2025 04:24:03 PM (2 months ago)
Author:
espellcaste
Message:

Code Modernization: address the only, so far, two issues related to PHP 8.5.

  • Instances of using null as an array offset.
  • ArrayIterator::__construct(): Using an object as a backing array for ArrayIterator is deprecated.

Reference: https://wiki.php.net/rfc/deprecations_php_8_5\#deprecate_arrayobject_and_arrayiterator_with_objects

Developed in https://github.com/buddypress/buddypress/pull/425

Follow-up to [14147].

Props espellcaste.

See #9313 (trunk)

Location:
trunk
Files:
4 edited

Legend:

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

    r14148 r14149  
    26352635                $comments = BP_Activity_Activity::get_activity_comments( $comment->item_id, 1, constant( 'PHP_INT_MAX' ) );
    26362636
     2637                if ( ! is_array( $comments ) ) {
     2638                    $comments = [];
     2639                }
     2640
    26372641                // Recursively find our comment object from the comment tree.
    2638                 $iterator  = new RecursiveArrayIterator( $comments );
    2639                 $recursive = new RecursiveIteratorIterator( $iterator, RecursiveIteratorIterator::SELF_FIRST );
    2640                 foreach ( $recursive as $cid => $cobj ) {
    2641                     // Skip items that are not a comment object.
    2642                     if ( ! is_numeric( $cid ) || ! is_object( $cobj ) ) {
    2643                         continue;
    2644                     }
    2645 
    2646                     // We found the activity comment! Set the depth.
    2647                     if ( $cid === $comment->id && isset( $cobj->depth ) ) {
    2648                         $depth = $cobj->depth;
    2649                         break;
    2650                     }
     2642                $comment_in_tree = BP_Activity_Activity::find_comment_in_tree( $comments, $comment->id );
     2643
     2644                if ( is_object( $comment_in_tree ) && isset( $comment_in_tree->depth ) ) {
     2645                    $depth = $comment_in_tree->depth;
    26512646                }
    26522647            }
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r14148 r14149  
    16521652     * @param string $spam                Optional. 'ham_only' (default), 'spam_only' or 'all'.
    16531653     * @param int    $top_level_parent_id Optional. The id of the root-level parent activity item.
    1654      * @return array The updated activities with nested comments.
     1654     * @return array|false The updated activities with nested comments. False if no comments are found.
    16551655     */
    16561656    public static function get_activity_comments( $activity_id, $left, $right, $spam = 'ham_only', $top_level_parent_id = 0 ) {
     
    17671767                        if ( isset( $direct_parent->secondary_item_id ) ) {
    17681768                            // If the direct parent is not an activity update, that means we've reached
    1769                             // the parent activity item (eg. new_blog_post).
     1769                            // the parent activity item (e.g. new_blog_post).
    17701770                            if ( 'activity_update' !== $direct_parent->type ) {
    17711771                                $parent_id = $r->item_id;
     
    21182118        return $wpdb->get_var( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET hide_sitewide = 1 WHERE user_id = %d", $user_id ) );
    21192119    }
     2120
     2121    /**
     2122     * Recursively find a comment object from a nested comment tree.
     2123     *
     2124     * @since 14.5.0
     2125     *
     2126     * @param array $comments  Array of comment objects with nested children.
     2127     * @param int   $target_id The comment ID to find.
     2128     * @return object|false The comment object if found, false otherwise.
     2129     */
     2130    public static function find_comment_in_tree( $comments, $target_id ) {
     2131
     2132        if ( ! is_array( $comments ) ) {
     2133            return false;
     2134        }
     2135
     2136        foreach ( $comments as $comment_id => $comment ) {
     2137            // Skip items that are not a comment object.
     2138            if ( ! is_numeric( $comment_id ) || ! is_object( $comment ) ) {
     2139                continue;
     2140            }
     2141
     2142            if ( (int) $comment_id === (int) $target_id ) {
     2143                return $comment;
     2144            }
     2145
     2146            // Recurse into children if they exist.
     2147            if ( ! empty( $comment->children ) && is_array( $comment->children ) ) {
     2148                $found = self::find_comment_in_tree( $comment->children, $target_id );
     2149
     2150                if ( false !== $found ) {
     2151                    return $found;
     2152                }
     2153            }
     2154        }
     2155
     2156        return false;
     2157    }
    21202158}
  • trunk/src/bp-core/bp-core-template-loader.php

    r14013 r14149  
    594594            $url_query_chunks = bp_parse_args( $GLOBALS['wp']->query_string, array() );
    595595            $directory        = key( $url_query_chunks );
    596             if ( isset( $bp_directories[ $directory ] ) ) {
     596            if ( isset( $directory, $bp_directories[ $directory ] ) ) {
    597597                $url_query_chunks[ $directory ] = $bp_directories[ $directory ];
    598598            }
  • trunk/tests/phpunit/testcases/activity/functions/bpActivityGetCommentDepth.php

    r11737 r14149  
    4141        bp_has_activities( 'display_comments=threaded' );
    4242
    43         // Loop through activity comments generated in activity loop.
    44         $recursive = new RecursiveIteratorIterator( new RecursiveArrayIterator( $GLOBALS['activities_template']->activities[0]->children ), RecursiveIteratorIterator::SELF_FIRST );
    45         foreach ( $recursive as $aid => $a ) {
    46             if ( ! is_numeric( $aid ) || ! is_object( $a ) ) {
     43        $children = $GLOBALS['activities_template']->activities[0]->children;
     44
     45        foreach ( array( $comment_one, $comment_one_one, $comment_two ) as $aid ) {
     46            $a = BP_Activity_Activity::find_comment_in_tree( $children, $aid );
     47
     48            if ( false === $a ) {
    4749                continue;
    4850            }
     
    6668                    break;
    6769            }
    68 
    6970        }
    7071
Note: See TracChangeset for help on using the changeset viewer.