|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin Page Framework |
|
4
|
|
|
* |
|
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
|
6
|
|
|
* Copyright (c) 2013-2016 Michael Uno; Licensed MIT |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Provides utility methods which do not use WordPress functions. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 2.0.0 |
|
14
|
|
|
* @since 3.7.3 Became not abstract for the xdebug max nesting level fatal error workaround. |
|
15
|
|
|
* @extends AdminPageFramework_Utility_SystemInformation |
|
16
|
|
|
* @package AdminPageFramework |
|
17
|
|
|
* @subpackage Utility |
|
18
|
|
|
* @internal |
|
19
|
|
|
*/ |
|
20
|
|
|
class AdminPageFramework_Utility extends AdminPageFramework_Utility_HTMLAttribute { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Calls back a user defined function. |
|
24
|
|
|
* |
|
25
|
|
|
* This is meant to be used to filter a value using a callback. When a callback is not available, the first parameter element will be returned. |
|
26
|
|
|
* so set a default return value to the first element of the parameter array. |
|
27
|
|
|
* |
|
28
|
|
|
* @since 3.7.0 |
|
29
|
|
|
* @since 3.8.5 Moved from `AdminPageFramework_Form_Base`. Added the default value to the `$asParameters`second parameter. |
|
30
|
|
|
* @param callable $oCallable |
|
31
|
|
|
* @param string|array $asParameters Parameters to pass to the callback function. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function callBack( $oCallable, $asParameters=array() ) { |
|
34
|
|
|
$_aParameters = self::getAsArray( |
|
35
|
|
|
$asParameters, |
|
36
|
|
|
true // preserve empty |
|
37
|
|
|
); |
|
38
|
|
|
$_mDefaultValue = self::getElement( $_aParameters, 0 ); |
|
39
|
|
|
return is_callable( $oCallable ) |
|
40
|
|
|
? call_user_func_array( $oCallable, $_aParameters ) |
|
41
|
|
|
: $_mDefaultValue; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Checks if the given id (usually a function name) has been called throughout the page load. |
|
46
|
|
|
* |
|
47
|
|
|
* This is used to check if a function which needs to be done only once has been already called or not. |
|
48
|
|
|
* |
|
49
|
|
|
* @since 3.7.0 |
|
50
|
|
|
* @return boolean |
|
51
|
|
|
*/ |
|
52
|
|
|
static public function hasBeenCalled( $sID ) { |
|
|
|
|
|
|
53
|
|
|
if ( isset( self::$_aCallStack[ $sID ] ) ) { |
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
self::$_aCallStack[ $sID ] = true; |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* Stores calls. |
|
61
|
|
|
* @internal |
|
62
|
|
|
*/ |
|
63
|
|
|
static private $_aCallStack = array(); |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Captures the output buffer of the given function. |
|
67
|
|
|
* @since 3.6.3 |
|
68
|
|
|
* @return string The captured output buffer. |
|
69
|
|
|
*/ |
|
70
|
|
|
static public function getOutputBuffer( $oCallable, array $aParameters=array() ) { |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
ob_start(); |
|
73
|
|
|
echo call_user_func_array( $oCallable, $aParameters ); |
|
74
|
|
|
$_sContent = ob_get_contents(); |
|
75
|
|
|
ob_end_clean(); |
|
76
|
|
|
return $_sContent; |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Generates brief object information. |
|
82
|
|
|
* |
|
83
|
|
|
* @remark Meant to be used for the `__toString()` method. |
|
84
|
|
|
* @since 3.6.0 |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
static public function getObjectInfo( $oInstance ) { |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$_iCount = count( get_object_vars( $oInstance ) ); |
|
90
|
|
|
$_sClassName = get_class( $oInstance ); |
|
91
|
|
|
return '(object) ' . $_sClassName . ': ' . $_iCount . ' properties.'; |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns one or the other. |
|
98
|
|
|
* |
|
99
|
|
|
* Saves one conditional statement. |
|
100
|
|
|
* |
|
101
|
|
|
* @remark Use this only when the performance is not critical. |
|
102
|
|
|
* @since 3.5.3 |
|
103
|
|
|
* @param boolean|integer|double|string|array|object|resource|NULL $mValue The value to evaluate. |
|
104
|
|
|
* @param boolean|integer|double|string|array|object|resource|NULL $mTrue The value to return when the first parameter value yields true. |
|
105
|
|
|
* @param boolean|integer|double|string|array|object|resource|NULL $mTrue The value to return when the first parameter value yields false. |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
|
|
static public function getAOrB( $mValue, $mTrue=null, $mFalse=null ) { |
|
|
|
|
|
|
109
|
|
|
return $mValue ? $mTrue : $mFalse; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|