Skip to:
Content

BuddyPress.org

Changeset 11362


Ignore:
Timestamp:
12/29/2016 09:13:41 PM (7 years ago)
Author:
boonebgorges
Message:

Use a closure for bp_sort_by_key() callback.

Previously, bp_sort_by_key() was refactored to avoid the use
of create_function(). See #6864. This refactoring needed to be
PHP 5.2-compatible, which required a workaround for the
unavailabilty of closures. This workaround can now be removed.

See #7299.

Location:
trunk/src/bp-core
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r11285 r11362  
    121121 */
    122122function bp_sort_by_key( $items, $key, $type = 'alpha', $preserve_keys = false ) {
     123    $callback = function( $a, $b ) use ( $key, $type ) {
     124        $values = array( 0 => false, 1 => false );
     125        $func_args = func_get_args();
     126        foreach ( $func_args as $indexi => $index ) {
     127            if ( isset( $index->{$key} ) ) {
     128                $values[ $indexi ] = $index->{$key};
     129            } elseif ( isset( $index[ $key ] ) ) {
     130                $values[ $indexi ] = $index[ $key ];
     131            }
     132        }
     133
     134        if ( isset( $values[0], $values[1] ) ) {
     135            if ( 'num' === $type ) {
     136                $cmp = $values[0] - $values[1];
     137            } else {
     138                $cmp = strcmp( $values[0], $values[1] );
     139            }
     140
     141            if ( 0 > $cmp ) {
     142                $retval = -1;
     143            } elseif ( 0 < $cmp ) {
     144                $retval = 1;
     145            } else {
     146                $retval = 0;
     147            }
     148            return $retval;
     149        } else {
     150            return 0;
     151        }
     152    };
     153
    123154    if ( true === $preserve_keys ) {
    124         uasort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) );
     155        uasort( $items, $callback );
    125156    } else {
    126         usort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) );
     157        usort( $items, $callback );
    127158    }
    128159
Note: See TracChangeset for help on using the changeset viewer.