Completed
Branch master (7ff82b)
by
unknown
01:54
created

AdminPageFramework_Utility::getObjectCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Page Framework
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2020, 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/Utility
17
 * @internal
18
 */
19
class AdminPageFramework_Utility extends AdminPageFramework_Utility_HTMLAttribute {
20
21
    /**
22
     * @param  array   $aRequest
23
     * @param  boolean $bStripSlashes   Whether to fix magic quotes.
24
     * @return array
25
     * @since  3.8.24
26
     */
27
    static public function getHTTPRequestSanitized( array $aRequest, $bStripSlashes ) {
28
        foreach( $aRequest as $_isIndex => $_mValue ) {
29
            if ( is_array( $_mValue ) ) {
30
                $aRequest[ $_isIndex ] = self::getHTTPRequestSanitized( $_mValue, $bStripSlashes );
31
                continue;
32
            }
33
            if ( is_string( $_mValue ) ) {
34
                $aRequest[ $_isIndex ] = _sanitize_text_fields( $_mValue, true );
35
            }
36
        }
37
        return $bStripSlashes ? stripslashes_deep( $aRequest ) : $aRequest;
38
    }
39
40
    /**
41
     * @var   array
42
     * @since 3.8.24
43
     */
44
    static private $___aObjectCache = array();
45
    /**
46
     * @param string|array $asName  If array, it represents a multi-dimensional keys.
47
     * @param mixed        $mValue
48
     * @since  3.8.24
49
     */
50
    static public function setObjectCache( $asName, $mValue ) {
51
        self::setMultiDimensionalArray( self::$___aObjectCache, self::getAsArray( $asName ), $mValue );
52
    }
53
54
    /**
55
     * @param array|string $asName
56
     * @since  3.8.24
57
     */
58
    static public function unsetObjectCache( $asName ) {
59
        self::unsetDimensionalArrayElement( self::$___aObjectCache, self::getAsArray( $asName ) );
60
    }
61
62
    /**
63
     * Caches values in the class property.
64
     *
65
     * @remark The stored data will be gone after the page load.
66
     * @param  array|string $asName The key of the object cache array. If an array is given, it represents the multi-dimensional keys.
67
     * @param  mixed $mDefault
68
     * @return mixed
69
     * @since  3.8.24
70
     */
71
    static public function getObjectCache( $asName, $mDefault=null ) {
72
        return self::getArrayValueByArrayKeys( self::$___aObjectCache, self::getAsArray( $asName ), $mDefault );
73
    }
74
75
    /**
76
     * Shows a message for a deprecated item.
77
     *
78
     * Uses the `E_USER_NOTICE` error level so that the message won't be shown if `WP_DEBUG` is `false`.
79
     *
80
     * @remark  This method is overridden by the `AdminPageFramework_FrameworkUtility` class.
81
     * @param   string $sDeprecated
82
     * @param   string $sAlternative
83
     * @param   string $sProgramName
84
     * @since   3.8.8
85
     */
86
    static public function showDeprecationNotice( $sDeprecated, $sAlternative='', $sProgramName='Admin Page Framework' ) {
87
        trigger_error(
88
            $sProgramName . ': ' . sprintf(
89
                $sAlternative
90
                    ? '<code>%1$s</code> has been deprecated. Use <code>%2$s</code> instead.'
91
                    : '<code>%1$s</code> has been deprecated.',
92
                $sDeprecated, // %1$s
93
                $sAlternative // %2%s
94
            ),
95
            E_USER_NOTICE
96
        );
97
    }
98
99
    /**
100
     * Calls back a user defined function.
101
     *
102
     * 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.
103
     * so set a default return value to the first element of the parameter array.
104
     *
105
     * @since       3.7.0
106
     * @since       3.8.5               Moved from `AdminPageFramework_Form_Base`. Added the default value to the `$asParameters`second parameter.
107
     * @param       callable            $oCallable
108
     * @param       string|array        $asParameters       Parameters to pass to the callback function.
109
     * @return      mixed
110
     */
111
    public function callBack( $oCallable, $asParameters=array() ) {
112
        $_aParameters   = self::getAsArray(
113
            $asParameters,
114
            true // preserve empty
115
        );
116
        $_mDefaultValue = self::getElement( $_aParameters, 0 );
117
        return is_callable( $oCallable )
118
            ? call_user_func_array( $oCallable, $_aParameters )
119
            : $_mDefaultValue;
120
    }
121
122
    /**
123
     * Checks if the given id (usually a function name) has been called throughout the page load.
124
     *
125
     * This is used to check if a function which needs to be done only once has been already called or not.
126
     *
127
     * @since       3.7.0
128
     * @param       string  $sKey   The string identifier of the call.
129
     * @return      boolean
130
     */
131
    static public function hasBeenCalled( $sKey ) {
132
        if ( isset( self::$___aCallStack[ $sKey ] ) ) {
133
            return true;
134
        }
135
        self::$___aCallStack[ $sKey ] = true;
136
        return false;
137
    }
138
        /**
139
         * Stores calls.
140
         * @internal
141
         */
142
        static private $___aCallStack = array();
143
144
    /**
145
     * Captures the output buffer of the given function.
146
     * @since       3.6.3
147
     * @param       callable    $cCallable
148
     * @param       array       $aParameters
149
     * @return      string      The captured output buffer.
150
     */
151
    static public function getOutputBuffer( $cCallable, array $aParameters=array() ) {
152
153
        ob_start();
154
        echo call_user_func_array( $cCallable, $aParameters );
155
        $_sContent = ob_get_contents();
156
        ob_end_clean();
157
        return $_sContent;
158
159
    }
160
161
    /**
162
     * Generates brief object information.
163
     *
164
     * @remark      Meant to be used for the `__toString()` method.
165
     * @since       3.6.0
166
     * @param       object  $oInstance
167
     * @return      string
168
     */
169
    static public function getObjectInfo( $oInstance ) {
170
171
        $_iCount     = count( get_object_vars( $oInstance ) );
172
        $_sClassName = get_class( $oInstance );
173
        return '(object) ' . $_sClassName . ': ' . $_iCount . ' properties.';
174
175
    }
176
177
178
    /**
179
     * Returns one or the other.
180
     *
181
     * Saves one conditional statement.
182
     *
183
     * @remark      Use this only when the performance is not critical.
184
     * @since       3.5.3
185
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mValue     The value to evaluate.
186
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mTrue      The value to return when the first parameter value yields true.
187
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mFalse     The value to return when the first parameter value yields false.
188
     * @return      mixed
189
     */
190
    static public function getAOrB( $mValue, $mTrue=null, $mFalse=null ) {
191
        return $mValue ? $mTrue : $mFalse;
192
    }
193
194
}
195