| Total Complexity | 47 |
| Total Lines | 330 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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(); |
||
|
|
|||
| 306 | if ( is_string( $_oScreen ) ) { |
||
| 307 | $_oScreen = convert_to_screen( $_oScreen ); |
||
| 308 | } |
||
| 309 | if ( isset( $_oScreen->id ) ) { |
||
| 310 | return $_oScreen->id; |
||
| 311 | } |
||
| 312 | |||
| 313 | if ( isset( $GLBOALS[ 'page_hook' ] ) ) { |
||
| 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='' ) { |
||
| 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() { |
||
| 350 | } |
||
| 351 | |||
| 352 | } |
||
| 353 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
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.