Skip to:
Content

BuddyPress.org

Changeset 9263


Ignore:
Timestamp:
12/23/2014 05:36:06 PM (10 years ago)
Author:
r-a-y
Message:

Add a PHP-agnostic version of array_replace_recursive() in BP_Activity_Activity

r9257 implemented the ability to query activity items by multiple scopes.
array_replace_recursive() is a function that is used in the above commit
to help override certain arguments in the activity loop by scopes. However, array_replace_recursive() is a PHP 5.3 function and BuddyPress (and
WordPress) currently supports a minimum version of PHP 5.2.

This commit adds a PHP-agnostic version of array_replace_recursive() for
internal use by the BP_Activity_Activity class. This helps to avoid
fatal errors for those using PHP 5.2 and to fix up our unit tests.

Anti-props r-a-y.

See #4988.

File:
1 edited

Legend:

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

    r9257 r9263  
    355355            // override some arguments if needed
    356356            if ( ! empty( $scope_query['override'] ) ) {
    357                 $r = array_replace_recursive( $r, $scope_query['override'] );
     357                $r = self::array_replace_recursive( $r, $scope_query['override'] );
    358358            }
    359359        // Advanced filtering
     
    16671667        return $wpdb->get_var( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET hide_sitewide = 1 WHERE user_id = %d", $user_id ) );
    16681668    }
     1669
     1670    /**
     1671     * PHP-agnostic version of {@link array_replace_recursive()}.
     1672     *
     1673     * array_replace_recursive() is a PHP 5.3 function.  BuddyPress (and
     1674     * WordPress) currently supports down to PHP 5.2, so this method is a workaround
     1675     * for PHP 5.2.
     1676     *
     1677     * Note: array_replace_recursive() supports infinite arguments, but for our use-
     1678     * case, we only need to support two arguments.
     1679     *
     1680     * Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement.
     1681     *
     1682     * @since BuddyPress (2.2.0)
     1683     *
     1684     * @see http://php.net/manual/en/function.array-replace-recursive.php#109390
     1685     *
     1686     * @param  array $base         Array with keys needing to be replaced
     1687     * @param  array $replacements Array with the replaced keys
     1688     * @return array
     1689     */
     1690    protected static function array_replace_recursive( $base = array(), $replacements = array() ) {
     1691        if ( function_exists( 'array_replace_recursive' ) ) {
     1692            return array_replace_recursive( $base, $replacements );
     1693        }
     1694
     1695        // PHP 5.2-compatible version
     1696        // http://php.net/manual/en/function.array-replace-recursive.php#109390
     1697        foreach ( array_slice( func_get_args(), 1 ) as $replacements ) {
     1698            $bref_stack = array( &$base );
     1699            $head_stack = array( $replacements );
     1700
     1701            do {
     1702                end( $bref_stack );
     1703
     1704                $bref = &$bref_stack[ key( $bref_stack ) ];
     1705                $head = array_pop( $head_stack );
     1706
     1707                unset( $bref_stack[ key($bref_stack) ] );
     1708
     1709                foreach ( array_keys( $head ) as $key ) {
     1710                    if ( isset( $key, $bref ) && is_array( $bref[$key] ) && is_array( $head[$key] ) ) {
     1711                        $bref_stack[] = &$bref[ $key ];
     1712                        $head_stack[] = $head[ $key ];
     1713                    } else {
     1714                        $bref[ $key ] = $head[ $key ];
     1715                    }
     1716                }
     1717            } while( count( $head_stack ) );
     1718        }
     1719
     1720        return $base;
     1721    }
    16691722}
    16701723
Note: See TracChangeset for help on using the changeset viewer.