Completed
Branch master (c1f990)
by
unknown
04:37 queued 02:56
created

___getHTTPRequestTextValueSanitized()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 2
dl 0
loc 20
rs 9.6
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 ] = self::___getHTTPRequestTextValueSanitized( $_mValue );
35
            }
36
        }
37
        return $bStripSlashes ? stripslashes_deep( $aRequest ) : $aRequest;
38
    }
39
        /**
40
         * A light version of _sanitize_text_fields().
41
         * 
42
         * This does not strip HTML tags.
43
         * 
44
         * @param  string  $sString
45
         * @param  boolean $bKeepLineFeeds
46
         * @return string
47
         * @sicne  3.8.25
48
         * @see    _sanitize_text_fields()
49
         */
50
        static private function ___getHTTPRequestTextValueSanitized( $sString, $bKeepLineFeeds=true ) {
51
            
52
            $_sFiltered = wp_check_invalid_utf8( $sString );
53
            if ( ! $bKeepLineFeeds ) {
54
                $_sFiltered = preg_replace( '/[\r\n\t ]+/', ' ', $_sFiltered );
55
            }
56
            $_sFiltered = trim( $_sFiltered );
57
        
58
            $_bFound = false;
59
            while ( preg_match( '/%[a-f0-9]{2}/i', $_sFiltered, $_aMatches ) ) {
60
                $_sFiltered = str_replace( $_aMatches[ 0 ], '', $_sFiltered );
61
                $_bFound    = true;
62
            }
63
            if ( $_bFound ) {
64
                // Strip out the whitespace that may now exist after removing the octets.
65
                $_sFiltered = trim( preg_replace( '/ +/', ' ', $_sFiltered ) );
66
            }
67
            return $_sFiltered;
68
69
        }
70
71
    /**
72
     * @var   array
73
     * @since 3.8.24
74
     */
75
    static private $___aObjectCache = array();
76
    /**
77
     * @param string|array $asName  If array, it represents a multi-dimensional keys.
78
     * @param mixed        $mValue
79
     * @since  3.8.24
80
     */
81
    static public function setObjectCache( $asName, $mValue ) {
82
        self::setMultiDimensionalArray( self::$___aObjectCache, self::getAsArray( $asName ), $mValue );
83
    }
84
85
    /**
86
     * @param array|string $asName
87
     * @since  3.8.24
88
     */
89
    static public function unsetObjectCache( $asName ) {
90
        self::unsetDimensionalArrayElement( self::$___aObjectCache, self::getAsArray( $asName ) );
91
    }
92
93
    /**
94
     * Caches values in the class property.
95
     *
96
     * @remark The stored data will be gone after the page load.
97
     * @param  array|string $asName The key of the object cache array. If an array is given, it represents the multi-dimensional keys.
98
     * @param  mixed $mDefault
99
     * @return mixed
100
     * @since  3.8.24
101
     */
102
    static public function getObjectCache( $asName, $mDefault=null ) {
103
        return self::getArrayValueByArrayKeys( self::$___aObjectCache, self::getAsArray( $asName ), $mDefault );
104
    }
105
106
    /**
107
     * Shows a message for a deprecated item.
108
     *
109
     * Uses the `E_USER_NOTICE` error level so that the message won't be shown if `WP_DEBUG` is `false`.
110
     *
111
     * @remark  This method is overridden by the `AdminPageFramework_FrameworkUtility` class.
112
     * @param   string $sDeprecated
113
     * @param   string $sAlternative
114
     * @param   string $sProgramName
115
     * @since   3.8.8
116
     */
117
    static public function showDeprecationNotice( $sDeprecated, $sAlternative='', $sProgramName='Admin Page Framework' ) {
118
        trigger_error(
119
            $sProgramName . ': ' . sprintf(
120
                $sAlternative
121
                    ? '<code>%1$s</code> has been deprecated. Use <code>%2$s</code> instead.'
122
                    : '<code>%1$s</code> has been deprecated.',
123
                $sDeprecated, // %1$s
124
                $sAlternative // %2%s
125
            ),
126
            E_USER_NOTICE
127
        );
128
    }
129
130
    /**
131
     * Calls back a user defined function.
132
     *
133
     * 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.
134
     * so set a default return value to the first element of the parameter array.
135
     *
136
     * @since       3.7.0
137
     * @since       3.8.5               Moved from `AdminPageFramework_Form_Base`. Added the default value to the `$asParameters`second parameter.
138
     * @param       callable            $oCallable
139
     * @param       string|array        $asParameters       Parameters to pass to the callback function.
140
     * @return      mixed
141
     */
142
    public function callBack( $oCallable, $asParameters=array() ) {
143
        $_aParameters   = self::getAsArray(
144
            $asParameters,
145
            true // preserve empty
146
        );
147
        $_mDefaultValue = self::getElement( $_aParameters, 0 );
148
        return is_callable( $oCallable )
149
            ? call_user_func_array( $oCallable, $_aParameters )
150
            : $_mDefaultValue;
151
    }
152
153
    /**
154
     * Checks if the given id (usually a function name) has been called throughout the page load.
155
     *
156
     * This is used to check if a function which needs to be done only once has been already called or not.
157
     *
158
     * @since       3.7.0
159
     * @param       string  $sKey   The string identifier of the call.
160
     * @return      boolean
161
     */
162
    static public function hasBeenCalled( $sKey ) {
163
        if ( isset( self::$___aCallStack[ $sKey ] ) ) {
164
            return true;
165
        }
166
        self::$___aCallStack[ $sKey ] = true;
167
        return false;
168
    }
169
        /**
170
         * Stores calls.
171
         * @internal
172
         */
173
        static private $___aCallStack = array();
174
175
    /**
176
     * Captures the output buffer of the given function.
177
     * @since       3.6.3
178
     * @param       callable    $cCallable
179
     * @param       array       $aParameters
180
     * @return      string      The captured output buffer.
181
     */
182
    static public function getOutputBuffer( $cCallable, array $aParameters=array() ) {
183
184
        ob_start();
185
        echo call_user_func_array( $cCallable, $aParameters );
186
        $_sContent = ob_get_contents();
187
        ob_end_clean();
188
        return $_sContent;
189
190
    }
191
192
    /**
193
     * Generates brief object information.
194
     *
195
     * @remark      Meant to be used for the `__toString()` method.
196
     * @since       3.6.0
197
     * @param       object  $oInstance
198
     * @return      string
199
     */
200
    static public function getObjectInfo( $oInstance ) {
201
202
        $_iCount     = count( get_object_vars( $oInstance ) );
203
        $_sClassName = get_class( $oInstance );
204
        return '(object) ' . $_sClassName . ': ' . $_iCount . ' properties.';
205
206
    }
207
208
209
    /**
210
     * Returns one or the other.
211
     *
212
     * Saves one conditional statement.
213
     *
214
     * @remark      Use this only when the performance is not critical.
215
     * @since       3.5.3
216
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mValue     The value to evaluate.
217
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mTrue      The value to return when the first parameter value yields true.
218
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mFalse     The value to return when the first parameter value yields false.
219
     * @return      mixed
220
     */
221
    static public function getAOrB( $mValue, $mTrue=null, $mFalse=null ) {
222
        return $mValue ? $mTrue : $mFalse;
223
    }
224
225
}
226