-
Notifications
You must be signed in to change notification settings - Fork 16
/
class-php-54-improvements.php
executable file
·124 lines (104 loc) · 2.98 KB
/
class-php-54-improvements.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Adding PHP 5.4.x specific improvements for the Debug Objects capabilities
*
* PHP Version 5.4
*
* @package Debug_Objects
* @subpackage Debug_Objects_Php54
* @since 09/13/2013 2.1.16
* @author nofearinc, [email protected]
*/
if ( ! function_exists( 'add_filter' ) ) {
echo "Hi there! I'm just a part of plugin, not much I can do when called directly.";
exit;
}
/**
* Class Debug_Objects_Php54
*
* PHP Version 5.4
*/
class Debug_Objects_Php54 {
/**
* Add all required filters/actions
*/
public function __construct() {
add_filter( 'debug_objects_sort_queries', array( $this, 'sort_queries' ), 10, 2 );
}
/**
* Sort queries for the class-query listing if PHP 5.4 is active
* (using shorthand for arrays)
*
* @param array $queries WP_Query array
* @param int|false $sorting
*
* @return array $queries sorted queries list
*/
public function sort_queries( $queries, $sorting ) {
if ( ! empty( $sorting ) || ! $sorting )
usort( $queries, self::make_comparer( [1, $sorting] ) );
return $queries;
}
/**
* Sorting of Multidimensional arrays
* Only >= PHP 5.4
*
* @since 08/18/2013
* @see http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php
* @return Boolean
*/
public static function make_comparer() {
// Normalize criteria up front so that the comparer finds everything tidy
$criteria = func_get_args();
foreach ($criteria as $index => $criterion) {
$criteria[$index] = is_array($criterion)
? array_pad($criterion, 3, null)
: array($criterion, SORT_ASC, null);
}
return function( $first, $second ) use ( $criteria ) {
foreach ($criteria as $criterion) {
// How will we compare this round?
list($column, $sortOrder, $projection) = $criterion;
$sortOrder = $sortOrder === SORT_DESC ? -1 : 1;
// If a projection was defined project the values now
if ($projection) {
$lhs = call_user_func($projection, $first[$column]);
$rhs = call_user_func($projection, $second[$column]);
} else {
$lhs = $first[$column];
$rhs = $second[$column];
}
// Do the actual comparison; do not return if equal
if ($lhs < $rhs) {
return -1 * $sortOrder;
} else if ($lhs > $rhs) {
return 1 * $sortOrder;
}
}
return 0; // tiebreakers exhausted, so $first == $second
};
}
/**
* Sorting of Multidimensional arrays
* Hint: Slow and inefficient which to much foreach, usort is a better way
*
* @since 08/18/2013
* @see http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2
* @param Array, Input array
* @param String, key in array
*/
public function aasort( &$array, $key ) {
$sorter = array();
$ret = array();
reset( $array );
foreach( $array as $ii => $va ) {
$sorter[$ii] = $va[$key];
}
asort( $sorter );
foreach( $sorter as $ii => $va ) {
$ret[$ii] = $array[$ii];
}
$array = $ret;
}
}
new Debug_Objects_Php54();