AdminPageFramework_WPUtility_Page   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 330
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 47
eloc 98
dl 0
loc 330
rs 8.64
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getPostTypeByTypeNow() 0 3 3
A getPostTypeByPostObject() 0 6 3
A getCurrentPostType() 0 7 2
A getPostTypeByScreenObject() 0 6 3
A _getPageNowAdminURLBasePath() 0 13 3
A _getPageNow_BackEnd() 0 13 3
A isPostListingPage() 0 11 3
A getPostTypeByREQUEST() 0 7 4
A isCustomTaxonomyPage() 0 6 2
A isCurrentPostTypeIn() 0 11 2
A isPostDefinitionPage() 0 7 2
A doesMetaBoxExist() 0 11 2
A getNumberOfScreenColumns() 0 2 1
A _getCurrentPostType() 0 17 3
A getCurrentScreenID() 0 17 5
A _isInAdminIndex() 0 4 1
A _getPageNow_FrontEnd() 0 5 2
A getPageNow() 0 20 3

How to fix   Complexity   

Complex Class

Complex classes like AdminPageFramework_WPUtility_Page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AdminPageFramework_WPUtility_Page, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Admin Page Framework
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2022, Michael Uno; Licensed MIT
7
 *
8
 */
9
10
/**
11
 * Provides utility methods which use WordPress functions and classes.
12
 *
13
 * The methods in this class mainly deal with determining the type of loading page such as a post type, url base name etc.
14
 *
15
 * @since       2.0.0
16
 * @extends     AdminPageFramework_Utility
17
 * @package     AdminPageFramework/Utility
18
 * @internal
19
 */
20
class AdminPageFramework_WPUtility_Page extends AdminPageFramework_WPUtility_HTML {
21
22
    /**
23
     * Attempts to retrieve the current admin post type
24
     *
25
     * @remark      Caches the result.
26
     * @since       3.0.0
27
     * @return      string|null     The found post type slug.
28
     */
29
    static public function getCurrentPostType() {
30
31
        if ( isset( self::$_sCurrentPostType ) ) {
32
            return self::$_sCurrentPostType;
33
        }
34
        self::$_sCurrentPostType = self::_getCurrentPostType();
35
        return self::$_sCurrentPostType;
36
37
    }
38
        /**
39
         * Since the current page will be the same throughout the execution of the script,
40
         * once it's found, there is no need to find it again.
41
         */
42
        static private $_sCurrentPostType;
43
44
        /**
45
         * Attempts to retrieve the current admin post type
46
         *
47
         * @remark      Does not cache the result.
48
         * @since       3.5.3
49
         * @return      string|null     The found post type or null if not found.
50
         */
51
        static private function _getCurrentPostType() {
52
53
            // the array element order is important,
54
            // the one listed fist will be tried first.
55
            $_aMethodsToTry = array(
56
                'getPostTypeByTypeNow',
57
                'getPostTypeByScreenObject',
58
                'getPostTypeByREQUEST',
59
                'getPostTypeByPostObject',  // 3.6.0+ Moved to the last as it is not reliable.
60
            );
61
            foreach ( $_aMethodsToTry as $_sMethodName ) {
62
                $_sPostType = call_user_func( array( __CLASS__, $_sMethodName ) );
63
                if ( $_sPostType ) {
64
                    return $_sPostType;
65
                }
66
            }
67
            return null;
68
69
        }
70
            /**#@+
71
             * Attempts to find a current post type.
72
             * @internal
73
             * @return      null|string
74
             * @callback    function        call_user_func
75
             * @since       3.5.3
76
             */
77
            static public function getPostTypeByTypeNow() {
78
                if ( isset( $GLOBALS[ 'typenow' ] ) && $GLOBALS[ 'typenow' ] ) {
79
                    return $GLOBALS[ 'typenow' ];
80
                }
81
            }
82
            static public function getPostTypeByScreenObject() {
83
                if (
84
                    isset( $GLOBALS[ 'current_screen' ]->post_type )
85
                    && $GLOBALS[ 'current_screen' ]->post_type
86
                ) {
87
                    return $GLOBALS[ 'current_screen' ]->post_type;
88
                }
89
            }
90
            /**
91
             * Tries to find the post type from the URL query for type.
92
             */
93
            static public function getPostTypeByREQUEST() {
94
                if ( isset( $_REQUEST[ 'post_type' ] ) ) {  // sanitization unnecessary
95
                    return sanitize_key( sanitize_text_field( $_REQUEST[ 'post_type' ] ) ); // sanitization done
96
                }
97
                if ( isset( $_GET[ 'post' ] ) && $_GET[ 'post' ] ) {    // sanitization unnecessary
98
                    // It will perform a database query.
99
                    return get_post_type( absint( self::getHTTPQueryGET( 'post', 0 ) ) );  // sanitization done
100
                }
101
            }
102
103
            /**
104
             * @remark      Checking with the global post object is not reliable because it gets modified when the `WP_Query::the_post()` method is performed.
105
             */
106
            static public function getPostTypeByPostObject() {
107
                if (
108
                    isset( $GLOBALS[ 'post' ]->post_type )
109
                    && $GLOBALS[ 'post' ]->post_type
110
                ) {
111
                    return $GLOBALS[ 'post' ]->post_type;
112
                }
113
            }
114
            /**#@-*/
115
116
    /**
117
     * Checks if the current page is a custom taxonomy of the give post types.
118
     *
119
     * @since       3.1.3
120
     * @param       array|string        The post type slug(s) to check. If this is empty, the method just checks the current page is a taxonomy page.
121
     * @return      boolean
122
     */
123
    static public function isCustomTaxonomyPage( $asPostTypes=array() ) {
124
125
        if ( ! in_array( self::getPageNow(), array( 'tags.php', 'edit-tags.php', 'term.php' ) ) ) {
126
            return false;
127
        }
128
        return self::isCurrentPostTypeIn( $asPostTypes );
129
130
    }
131
132
    /**
133
     * Checks if the current page is a post editing page that belongs to the given post type(s).
134
     *
135
     * @since       3.0.0
136
     * @param       array|string        The post type slug(s) to check. If this is empty, the method just checks the current page is a post definition page.
137
     * Otherwise, it will check if the page belongs to the given post type(s).
138
     * @return      boolean
139
     */
140
    static public function isPostDefinitionPage( $asPostTypes=array() ) {
141
142
        // If it's not the post definition page,
143
        if ( ! in_array( self::getPageNow(), array( 'post.php', 'post-new.php', ) ) ) {
144
            return false;
145
        }
146
        return self::isCurrentPostTypeIn( $asPostTypes );
147
148
    }
149
150
    /**
151
     * Checks if the curently loading page's post type is of the given post types.
152
     *
153
     * @param       array|string        $asPostTypes        The post type slugs that the current post type belongs to.
154
     * @return      boolean             True if the current post type belongs to the given post types. If an empty value is passed to the first parameter, returns always true.
155
     * @since       3.5.3
156
     */
157
    static public function isCurrentPostTypeIn( $asPostTypes ) {
158
159
        $_aPostTypes = self::getAsArray( $asPostTypes );
160
161
        // If the parameter is empty,
162
        if ( empty( $_aPostTypes ) ) {
163
            return true;
164
        }
165
166
        // If the parameter of the post type is set and it's in the given post types,
167
        return in_array( self::getCurrentPostType(), $_aPostTypes );
168
169
    }
170
171
    /**
172
     * Checks if the current page is in the post listing page of the given page slug(s).
173
     *
174
     * @since 3.0.0
175
     */
176
    static public function isPostListingPage( $asPostTypes=array() ) {
177
178
        if ( 'edit.php' != self::getPageNow() ) {
179
            return false;
180
        }
181
182
        $_aPostTypes = self::getAsArray( $asPostTypes );
183
        if ( ! isset( $_GET[ 'post_type' ] )  ) {   // sanitization unnecessary
184
            return in_array( 'post', $_aPostTypes, true );
185
        }
186
        return in_array( $_GET[ 'post_type' ], $_aPostTypes, true );    // sanitization unnecessary
187
188
    }
189
190
    /**
191
     * Stores the cache of the pagenow value.
192
     */
193
    static private $_sPageNow;
194
195
    /**
196
     * Returns the base name of the current url.
197
     *
198
     * When a plugin is network activated, the global $pagenow variable sometimes is not set. Some framework's objects rely on the value of it.
199
     * So this method will provide an alternative mean when it is not set.
200
     *
201
     * @since       3.0.5
202
     */
203
    static public function getPageNow() {
204
205
        // Use the cache,
206
        if ( isset( self::$_sPageNow ) ) {
207
            return self::$_sPageNow;
208
        }
209
210
        // If already set, use that.
211
        if ( isset( $GLOBALS[ 'pagenow' ] ) ) {
212
            self::$_sPageNow = $GLOBALS[ 'pagenow' ];
213
            return self::$_sPageNow;
214
        }
215
216
        $_aMethodNames = array(
217
            0 => '_getPageNow_FrontEnd',
218
            1 => '_getPageNow_BackEnd',
219
        );
220
        $_sMethodName  = $_aMethodNames[ ( integer ) is_admin() ];
221
        self::$_sPageNow = self::$_sMethodName();
222
        return self::$_sPageNow;
223
224
    }
225
        /**
226
         * Returns the current page url base name.
227
         *
228
         * Assumes the current page is not in the admin area.
229
         *
230
         * @since       3.5.3
231
         * @return      string      The current page url base name.
232
         */
233
        static private function _getPageNow_FrontEnd() {
234
            if ( preg_match( '#([^/]+\.php)([?/].*?)?$#i', $_SERVER[ 'PHP_SELF' ], $_aMatches ) ) {
235
                return strtolower( $_aMatches[ 1 ] );
236
            }
237
            return 'index.php';
238
        }
239
240
        /**
241
         * Returns the current page url base name of the admin page.
242
         *
243
         * Assumes the current page is in the admin area.
244
         * @remark      In admin area, it is checked carefully than in the fron-end.
245
         * @since       3.5.3
246
         * @return      string      The current page url base name of the admin page.
247
         */
248
        static private function _getPageNow_BackEnd() {
249
250
            $_sPageNow = self::_getPageNowAdminURLBasePath();
251
            if ( self::_isInAdminIndex( $_sPageNow ) ) {
252
                return 'index.php';
253
            }
254
255
            preg_match( '#(.*?)(/|$)#', $_sPageNow, $_aMatches );
256
            $_sPageNow = strtolower( $_aMatches[ 1 ] );
257
            if ( '.php' !== substr( $_sPageNow, -4, 4 ) ) {
258
                $_sPageNow .= '.php'; // for Options +Multiviews: /wp-admin/themes/index.php (themes.php is queried)
259
            }
260
            return $_sPageNow;
261
262
        }
263
            /**
264
             * Return the base part of the currently loading admin url.
265
             * @since       3.5.3
266
             * @internal
267
             * return       string      The base part of the currently loading admin url.
268
             */
269
            static private function _getPageNowAdminURLBasePath() {
270
271
                if ( is_network_admin() ) {
272
                    $_sNeedle = '#/wp-admin/network/?(.*?)$#i';
273
                }
274
                else if ( is_user_admin() ) {
275
                    $_sNeedle = '#/wp-admin/user/?(.*?)$#i';
276
                }
277
                else {
278
                    $_sNeedle = '#/wp-admin/?(.*?)$#i';
279
                }
280
                preg_match( $_sNeedle, $_SERVER[ 'PHP_SELF' ], $_aMatches );
281
                return preg_replace( '#\?.*?$#', '', trim( $_aMatches[ 1 ], '/' ) );
282
283
            }
284
            /**
285
             * Checkes whether the passed base url name is of the admin index page.
286
             * @since       3.5.3
287
             * return       boolean      Whether the passed base url name is of the admin index page.
288
             */
289
            static private function _isInAdminIndex( $sPageNow ) {
290
                return in_array(
291
                    $sPageNow,
292
                    array( '', 'index', 'index.php' )
293
                );
294
            }
295
296
297
    /**
298
     * Returns the current loading screen id.
299
     *
300
     * @since       3.1.0
301
     * @return      string      The found screen ID.
302
     */
303
    static public function getCurrentScreenID() {
304
305
        $_oScreen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $_oScreen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
306
        if ( is_string( $_oScreen ) ) {
0 ignored issues
show
introduced by
The condition is_string($_oScreen) is always false.
Loading history...
307
            $_oScreen = convert_to_screen( $_oScreen );
308
        }
309
        if ( isset( $_oScreen->id ) ) {
310
            return $_oScreen->id;
311
        }
312
313
        if ( isset( $GLBOALS[ 'page_hook' ] ) ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $GLBOALS seems to never exist and therefore isset should always be false.
Loading history...
314
            return is_network_admin()
315
                ? $GLBOALS[ 'page_hook' ] . '-network'
316
                : $GLBOALS[ 'page_hook' ];
317
        }
318
319
        return '';
320
321
    }
322
323
    /**
324
     * Checks if a meta box exists in the current page.
325
     *
326
     * @since       3.7.0
327
     * @return      boolean
328
     */
329
    static public function doesMetaBoxExist( $sContext='' ) {
330
331
        $_aDimensions = array( 'wp_meta_boxes', $GLOBALS[ 'page_hook' ] );
332
        if ( $sContext ) {
333
            $_aDimensions[] = $sContext;
334
        }
335
        $_aSideMetaBoxes = self::getElementAsArray(
336
            $GLOBALS,
337
            $_aDimensions
338
        );
339
        return count( $_aSideMetaBoxes ) > 0;
340
341
    }
342
343
    /**
344
     * Returns the number of columns in the currently loading page.
345
     * @return      integer     The number of columns that the current screen displays.
346
     * @since       3.6.3
347
     */
348
    static public function getNumberOfScreenColumns() {
349
        return get_current_screen()->get_columns();
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
350
    }
351
352
}
353