Ticket #6864: 6864-class.diff
File 6864-class.diff, 3.7 KB (added by , 9 years ago) |
---|
-
src/bp-core/bp-core-classes.php
diff --git src/bp-core/bp-core-classes.php src/bp-core/bp-core-classes.php index 1566c85..bebf4ed 100644
require dirname( __FILE__ ) . '/classes/class-bp-walker-nav-menu-checklist.php'; 21 21 require dirname( __FILE__ ) . '/classes/class-bp-suggestions.php'; 22 22 require dirname( __FILE__ ) . '/classes/class-bp-members-suggestions.php'; 23 23 require dirname( __FILE__ ) . '/classes/class-bp-recursive-query.php'; 24 require dirname( __FILE__ ) . '/classes/class-bp-core-sort-by-key-callback.php'; 24 25 require dirname( __FILE__ ) . '/classes/class-bp-media-extractor.php'; 25 26 require dirname( __FILE__ ) . '/classes/class-bp-attachment.php'; 26 27 require dirname( __FILE__ ) . '/classes/class-bp-attachment-avatar.php'; -
src/bp-core/bp-core-functions.php
diff --git src/bp-core/bp-core-functions.php src/bp-core/bp-core-functions.php index e25389a..527438d 100644
function bp_core_get_table_prefix() { 116 116 * @return array $items The sorted array. 117 117 */ 118 118 function bp_sort_by_key( $items, $key, $type = 'alpha' ) { 119 usort( $items, create_function( '$a, $b', ' 120 $values = array( 0 => false, 1 => false, ); 121 $func_args = func_get_args(); 122 foreach ( $func_args as $indexi => $index ) { 123 if ( isset( $index->' . $key . ' ) ) { 124 $values[ $indexi ] = $index->' . $key . '; 125 } elseif ( isset( $index["' . $key . '"] ) ) { 126 $values[ $indexi ] = $index["' . $key . '"]; 127 } 128 } 129 130 if ( isset( $values[0], $values[1] ) ) { 131 if ( "num" === "' . $type . '" ) { 132 $cmp = $values[0] - $values[1]; 133 } else { 134 $cmp = strcmp( $values[0], $values[1] ); 135 } 136 137 if ( 0 > $cmp ) { 138 $retval = -1; 139 } elseif ( 0 < $cmp ) { 140 $retval = 1; 141 } else { 142 $retval = 0; 143 } 144 return $retval; 145 } else { 146 return 0; 147 } 148 ') ); 119 usort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) ); 149 120 150 121 return $items; 151 122 } -
new file src/bp-core/classes/class-bp-core-sort-by-key-callback.php
diff --git src/bp-core/classes/class-bp-core-sort-by-key-callback.php src/bp-core/classes/class-bp-core-sort-by-key-callback.php new file mode 100644 index 0000000..f2055a8
- + 1 <?php 2 3 /** 4 * Callback class for bp_sort_by_key(). 5 * 6 * Used in place of an anonymous closure. 7 * 8 * @since 2.5.0 9 */ 10 class BP_Core_Sort_By_Key_Callback { 11 /** 12 * Object/array index to use for sorting. 13 * 14 * @since 2.5.0 15 * @var mixed 16 */ 17 protected $key; 18 19 /** 20 * Sort type. 21 * 22 * @since 2.5.0 23 * @var string 24 */ 25 protected $type; 26 27 /** 28 * Constructor. 29 * 30 * @since 2.5.0 31 * 32 * @param mixed $key Object or array index to use for sorting. 33 * @param string $type Sort type. 34 */ 35 public function __construct( $key, $type ) { 36 $this->key = $key; 37 $this->type = $type; 38 } 39 40 /** 41 * Sort callback. 42 * 43 * @since 2.5.0 44 * 45 * @param $a object|array 46 * @param $b object|array 47 * @return int 48 */ 49 public function sort_callback( $a, $b ) { 50 $values = array( 0 => false, 1 => false, ); 51 $func_args = func_get_args(); 52 foreach ( $func_args as $indexi => $index ) { 53 if ( isset( $index->{$this->key} ) ) { 54 $values[ $indexi ] = $index->{$this->key}; 55 } elseif ( isset( $index[ $this->key ] ) ) { 56 $values[ $indexi ] = $index[ $this->key ]; 57 } 58 } 59 60 if ( isset( $values[0], $values[1] ) ) { 61 if ( 'num' === $this->type ) { 62 $cmp = $values[0] - $values[1]; 63 } else { 64 $cmp = strcmp( $values[0], $values[1] ); 65 } 66 67 if ( 0 > $cmp ) { 68 $retval = -1; 69 } elseif ( 0 < $cmp ) { 70 $retval = 1; 71 } else { 72 $retval = 0; 73 } 74 return $retval; 75 } else { 76 return 0; 77 } 78 } 79 }