| Total Complexity | 50 | 
| Total Lines | 405 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like AdminPageFramework_Form_View__Resource 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_Form_View__Resource, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 20 | class AdminPageFramework_Form_View__Resource extends AdminPageFramework_FrameworkUtility { | ||
| 21 | |||
| 22 | /** | ||
| 23 | * Stores the form object to let this class access the resource array. | ||
| 24 | * @var AdminPageFramework_Form | ||
| 25 | */ | ||
| 26 | public $oForm; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Sets up hooks. | ||
| 30 | * @since 3.7.0 | ||
| 31 | */ | ||
| 32 |     public function __construct( $oForm ) { | ||
| 33 | |||
| 34 | $this->oForm = $oForm; | ||
| 35 | |||
| 36 | // If it is loaded in the background, no need to load scripts and styles. | ||
| 37 |         if ( $this->isDoingAjax() ) { | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | // Widgets can be called multiple times for the number of user-created widget instances for one class instance | ||
| 42 | // so make sure it is processed only once per page. | ||
| 43 |         if ( $this->hasBeenCalled( 'resource_' . $oForm->aArguments[ 'caller_id' ] ) ) { | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | $this->___setHooks(); | ||
| 48 | |||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * @since 3.7.0 | ||
| 53 | */ | ||
| 54 |         private function ___setHooks() { | ||
| 55 | |||
| 56 |             if ( is_admin() ) { | ||
| 57 | $this->___setAdminHooks(); | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | // Hook the admin header to insert custom admin stylesheets and scripts. | ||
| 62 | add_action( 'wp_enqueue_scripts', array( $this, '_replyToEnqueueScripts' ) ); | ||
| 63 | add_action( 'wp_enqueue_scripts', array( $this, '_replyToEnqueueStyles' ) ); | ||
| 64 | |||
| 65 | /// A low priority is required to let dependencies loaded fast especially in customizer.php. | ||
| 66 | add_action( did_action( 'wp_print_styles' ) ? 'wp_print_footer_scripts' : 'wp_print_styles', array( $this, '_replyToAddStyle' ), 999 ); | ||
| 67 | // add_action( did_action( 'wp_print_scripts' ) ? 'wp_print_footer_scripts' : 'wp_print_scripts', array( $this, '_replyToAddScript' ), 999 ); // @deprecated 3.8.11 All the added scripts should be loaded in the footer. | ||
| 68 | |||
| 69 | // Take care of items that could not be added in the head tag. | ||
| 70 | |||
| 71 | /// For admin pages other than wp-admin/customizer.php | ||
| 72 | add_action( 'wp_footer', array( $this, '_replyToEnqueueScripts' ) ); | ||
| 73 | add_action( 'wp_footer', array( $this, '_replyToEnqueueStyles' ) ); | ||
| 74 | |||
| 75 | /// For all admin pages. | ||
| 76 | add_action( 'wp_print_footer_scripts', array( $this, '_replyToAddStyle' ), 999 ); | ||
| 77 | add_action( 'wp_print_footer_scripts', array( $this, '_replyToAddScript' ), 999 ); | ||
| 78 | |||
| 79 | // Required scripts in the head tag. | ||
| 80 | new AdminPageFramework_Form_View__Resource__Head( $this->oForm, 'wp_head' ); | ||
| 81 | |||
| 82 | } | ||
| 83 |             private function ___setAdminHooks() { | ||
| 84 | |||
| 85 | // Hook the admin header to insert custom admin stylesheets and scripts. | ||
| 86 | add_action( 'admin_enqueue_scripts', array( $this, '_replyToEnqueueScripts' ) ); | ||
| 87 | add_action( 'admin_enqueue_scripts', array( $this, '_replyToEnqueueStyles' ) ); | ||
| 88 | |||
| 89 | add_action( did_action( 'admin_print_styles' ) ? 'admin_print_footer_scripts' : 'admin_print_styles', array( $this, '_replyToAddStyle' ), 999 ); | ||
| 90 | // add_action( did_action( 'admin_print_scripts' ) ? 'admin_print_footer_scripts' : 'admin_print_scripts', array( $this, '_replyToAddScript' ), 999 ); // @deprecated 3.8.11 All the added scripts should be loaded in the footer. | ||
| 91 | |||
| 92 | // Take care of items that could not be added in the head tag. | ||
| 93 | /// For wp-admin/customizer.php | ||
| 94 | add_action( 'customize_controls_print_footer_scripts', array( $this, '_replyToEnqueueScripts' ) ); | ||
| 95 | add_action( 'customize_controls_print_footer_scripts', array( $this, '_replyToEnqueueStyles' ) ); | ||
| 96 | |||
| 97 | /// For admin pages other than wp-admin/customizer.php | ||
| 98 | add_action( 'admin_footer', array( $this, '_replyToEnqueueScripts' ) ); | ||
| 99 | add_action( 'admin_footer', array( $this, '_replyToEnqueueStyles' ) ); | ||
| 100 | |||
| 101 | /// For all admin pages. | ||
| 102 | add_action( 'admin_print_footer_scripts', array( $this, '_replyToAddStyle' ), 999 ); | ||
| 103 | add_action( 'admin_print_footer_scripts', array( $this, '_replyToAddScript' ), 999 ); | ||
| 104 | |||
| 105 | // Required scripts in the head tag. | ||
| 106 | new AdminPageFramework_Form_View__Resource__Head( $this->oForm, 'admin_head' ); | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Enqueues page script resources. | ||
| 112 | * | ||
| 113 | * @since 3.7.0 | ||
| 114 | */ | ||
| 115 |     public function _replyToEnqueueScripts() { | ||
| 116 |         if ( ! $this->oForm->isInThePage() ) { | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | $_aRegister = $this->oForm->getResources( 'register' ); | ||
| 120 |         foreach( $this->getElementAsArray( $_aRegister, array( 'scripts' ) ) as $_iIndex => $_aRegister ) { | ||
| 121 | $this->___registerScript( $_aRegister ); | ||
| 122 | } | ||
| 123 |         foreach( $this->oForm->getResources( 'src_scripts' ) as $_isIndex => $_asEnqueue ) { | ||
| 124 | $this->___enqueueScript( $_asEnqueue ); | ||
| 125 | $this->oForm->unsetResources( array( 'src_scripts', $_isIndex ) ); // no longer needed | ||
| 126 | } | ||
| 127 | } | ||
| 128 | /** | ||
| 129 | * @param array $aRegister | ||
| 130 | * @since 3.9.0 | ||
| 131 | */ | ||
| 132 |         private function ___registerScript( array $aRegister ) { | ||
| 133 | |||
| 134 | $aRegister = $aRegister + array( | ||
| 135 | 'handle_id' => '', 'src' => '', | ||
| 136 | 'dependencies' => array(), 'version' => false, | ||
| 137 | 'in_footer' => false, | ||
| 138 | 'translation' => array(), 'translation_var' => '', | ||
| 139 | ); | ||
| 140 | $_bRegistered = wp_register_script( | ||
| 141 | $aRegister[ 'handle_id' ], | ||
| 142 | $this->___getSRCFormatted( $aRegister ), | ||
| 143 | $aRegister[ 'dependencies' ], | ||
| 144 | $aRegister[ 'version' ], | ||
| 145 | $aRegister[ 'in_footer' ] | ||
| 146 | ); | ||
| 147 |             if ( $_bRegistered && ! empty( $aRegister[ 'translation' ] ) ) { | ||
| 148 | wp_localize_script( | ||
| 149 | $aRegister[ 'handle_id' ], | ||
| 150 | $aRegister[ 'translation_var' ] ? $aRegister[ 'translation_var' ] : $aRegister[ 'translation_var' ], | ||
| 151 | $this->getAsArray( $aRegister[ 'translation' ] ) | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | |||
| 155 | } | ||
| 156 | /** | ||
| 157 | * Stores flags of enqueued items. | ||
| 158 | * @since 3.7.0 | ||
| 159 | */ | ||
| 160 | static private $_aEnqueued = array(); | ||
| 161 | /** | ||
| 162 | * @return void | ||
| 163 | * @since 3.7.0 | ||
| 164 | */ | ||
| 165 |         private function ___enqueueScript( $asEnqueue ) { | ||
| 166 | |||
| 167 | $_sSetHandleID = $this->getElement( $this->getAsArray( $asEnqueue ), 'handle_id', '' ); | ||
| 168 | $_aEnqueueItem = $this->___getFormattedEnqueueScript( $asEnqueue ); | ||
| 169 | $_sCacheID = $_sSetHandleID . $this->getElement( $_aEnqueueItem, 'src', '' ); | ||
| 170 | |||
| 171 | // Do not load the same items multiple times. | ||
| 172 | // Checking if src is not empty is because there is a case that src is empty to just use handle ID to enqueue an item | ||
| 173 | // Allow same SRCs as field types extends another field type and script can have the same resource but with a different handle ID and translations. | ||
| 174 |             if ( ! empty( $_sCacheID ) && isset( self::$_aEnqueued[ $_sCacheID ] ) ) { | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | self::$_aEnqueued[ $_sCacheID ] = $_aEnqueueItem; | ||
| 178 | |||
| 179 | wp_enqueue_script( | ||
| 180 | $_aEnqueueItem[ 'handle_id' ], | ||
| 181 | $_aEnqueueItem[ 'src' ], | ||
| 182 | $_aEnqueueItem[ 'dependencies' ], | ||
| 183 | $_aEnqueueItem[ 'version' ], | ||
| 184 | did_action( 'admin_body_class' ) || ( boolean ) $_aEnqueueItem[ 'in_footer' ] | ||
| 185 | ); | ||
| 186 |             if ( $_aEnqueueItem[ 'translation' ] ) { | ||
| 187 | wp_localize_script( | ||
| 188 | $_aEnqueueItem[ 'handle_id' ], | ||
| 189 | empty( $_aEnqueueItem[ 'translation_var' ] ) ? $_aEnqueueItem[ 'handle_id' ] : $_aEnqueueItem[ 'translation_var' ], | ||
| 190 | $_aEnqueueItem[ 'translation' ] | ||
| 191 | ); | ||
| 192 | } | ||
| 193 |             if ( $_aEnqueueItem[ 'conditional' ] ) { | ||
| 194 | wp_script_add_data( $_aEnqueueItem[ 'handle_id' ], 'conditional', $_aEnqueueItem[ 'conditional' ] ); | ||
| 195 | } | ||
| 196 | |||
| 197 | } | ||
| 198 | /** | ||
| 199 | * @return array | ||
| 200 | * @since 3.7.0 | ||
| 201 | */ | ||
| 202 |             private function ___getFormattedEnqueueScript( $asEnqueue ) { | ||
| 203 | static $_iCallCount = 1; | ||
| 204 | $_aEnqueueItem = $this->getAsArray( $asEnqueue ) + array( | ||
| 205 | 'handle_id' => 'admin-page-framework-script-form-' . $this->oForm->aArguments[ 'caller_id' ] . '_' . $_iCallCount, | ||
| 206 | 'src' => null, | ||
| 207 | 'dependencies' => null, | ||
| 208 | 'version' => null, | ||
| 209 | 'in_footer' => false, | ||
| 210 | 'translation' => null, | ||
| 211 | 'conditional' => null, | ||
| 212 | 'translation_var' => null, | ||
| 213 | ); | ||
| 214 |                 if ( is_string( $asEnqueue ) ) { | ||
| 215 | $_aEnqueueItem[ 'src' ] = $asEnqueue; | ||
| 216 | } | ||
| 217 | $_aEnqueueItem[ 'src' ] = $this->___getSRCFormatted( $_aEnqueueItem ); | ||
| 218 | $_iCallCount++; | ||
| 219 | return $_aEnqueueItem; | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | /** | ||
| 224 | * Enqueues page stylesheet resources. | ||
| 225 | * | ||
| 226 | * @since 3.7.0 | ||
| 227 | */ | ||
| 228 |     public function _replyToEnqueueStyles() { | ||
| 229 | |||
| 230 |         if ( ! $this->oForm->isInThePage() ) { | ||
| 231 | return; | ||
| 232 | } | ||
| 233 | $_aRegister = $this->oForm->getResources( 'register' ); | ||
| 234 |         foreach( $this->getElementAsArray( $_aRegister, array( 'styles' ) ) as $_iIndex => $_aRegister ) { | ||
| 235 | $this->___registerStyle( $_aRegister ); | ||
| 236 | } | ||
| 237 |         foreach( $this->oForm->getResources( 'src_styles' ) as $_isIndex => $_asEnqueueItem ) { | ||
| 238 | $this->___enqueueStyle( $_asEnqueueItem ); | ||
| 239 | $this->oForm->unsetResources( array( 'src_styles', $_isIndex ) ); // no longer needed | ||
| 240 | } | ||
| 241 | |||
| 242 | } | ||
| 243 | /** | ||
| 244 | * @param array $aRegister | ||
| 245 | * @since 3.9.0 | ||
| 246 | */ | ||
| 247 |         private function ___registerStyle( array $aRegister ) { | ||
| 259 | ); | ||
| 260 | } | ||
| 261 | /** | ||
| 262 | * @param array|string $asEnqueue | ||
| 263 | * @since 3.9.0 | ||
| 264 | */ | ||
| 265 |         private function ___enqueueStyle( $asEnqueue ) { | ||
| 280 | } | ||
| 281 | } | ||
| 282 | /** | ||
| 283 | * @return array | ||
| 284 | */ | ||
| 285 |             private function ___getFormattedEnqueueStyle( $asEnqueue ) { | ||
| 286 | static $_iCallCount = 1; | ||
| 287 | $_aEnqueueItem = $this->getAsArray( $asEnqueue ) + array( | ||
| 288 | 'handle_id' => 'admin-page-framework-style-form-' . $this->oForm->aArguments[ 'caller_id' ] . '_' . $_iCallCount, | ||
| 289 | 'src' => null, 'dependencies' => null, | ||
| 290 | 'version' => null, 'media' => null, | ||
| 291 | 'conditional' => null, | ||
| 292 | 'rtl' => null, 'suffix' => null, | ||
| 293 | 'alt' => null, 'title' => null, | ||
| 294 | ); | ||
| 295 |                 if ( is_string( $asEnqueue ) ) { | ||
| 296 | $_aEnqueueItem[ 'src' ] = $asEnqueue; | ||
| 297 | } | ||
| 298 | $_aEnqueueItem[ 'src' ] = $this->___getSRCFormatted( $_aEnqueueItem ); | ||
| 299 | $_iCallCount++; | ||
| 300 | return $_aEnqueueItem; | ||
| 301 | } | ||
| 302 | |||
| 303 | /** | ||
| 304 | * Formats the SRC value. | ||
| 305 | * | ||
| 306 | * Also, adds the ability to auto-load a .min file if exists when a path is given (not url). | ||
| 307 | * | ||
| 308 | * @param array $aEnqueueItem | ||
| 309 | * @return string | ||
| 310 | * @since 3.8.31 | ||
| 311 | * @remark This is identical to the AdminPageFramework_Resource_Base::___getSRCFormatted() method. | ||
| 312 | * @see AdminPageFramework_Resource_Base::___getSRCFormatted() | ||
| 313 | */ | ||
| 314 |             private function ___getSRCFormatted( array $aEnqueueItem ) { | ||
| 315 | $_sSRCRaw = wp_normalize_path( $aEnqueueItem[ 'src' ] ); | ||
| 316 | $_sSRC = $this->getResolvedSRC( $_sSRCRaw ); // at this point, it is a URL | ||
| 317 | |||
| 318 |                 if ( ! $this->oForm->aArguments[ 'autoload_min_resource' ] ) { | ||
| 319 | return $_sSRC; | ||
| 320 | } | ||
| 321 | |||
| 322 | // If the site debug mode is on, use the one that user gave. | ||
| 323 |                 if ( $this->isDebugMode() ) { | ||
| 324 | return $_sSRC; | ||
| 325 | } | ||
| 326 | |||
| 327 | // If the user gave a url, use it. | ||
| 328 |                 if ( $this->isURL( $_sSRCRaw ) ) { | ||
| 329 | return $_sSRC; | ||
| 330 | } | ||
| 331 | |||
| 332 | // At this point, the user gave a path. | ||
| 333 | $_sMinPrefix = '.min'; | ||
| 334 | |||
| 335 | // If the user already handles a min version, then use it. | ||
| 336 |                 if ( false !== stripos( $_sSRC, $_sMinPrefix ) ) { | ||
| 337 | return $_sSRC; | ||
| 338 | } | ||
| 339 | |||
| 340 | $_aPathParts = pathinfo( $_sSRCRaw ) | ||
| 341 | + array( 'dirname' => '', 'filename' => '', 'basename' => '', 'extension' => '' ); // avoid undefined index warnings | ||
| 342 | |||
| 343 | // If there is no extension, avoid using a minified version. | ||
| 344 |                 if ( ! $_aPathParts[ 'extension' ] ) { | ||
| 345 | return $_sSRC; | ||
| 346 | } | ||
| 347 | |||
| 348 | $_aPathPartsURL = pathinfo( $_sSRC ) | ||
| 349 | + array( 'dirname' => '', 'filename' => '', 'basename' => '', 'extension' => '' ); // avoid undefined index warnings | ||
| 350 | |||
| 351 | $_sPathMinifiedVersion = $_aPathParts[ 'dirname' ] . '/' . $_aPathParts[ 'filename' ] . $_sMinPrefix . '.' . $_aPathParts[ 'extension' ]; | ||
| 352 | return file_exists( $_sPathMinifiedVersion ) | ||
| 353 | ? $_aPathPartsURL[ 'dirname' ] . '/' . $_aPathPartsURL[ 'filename' ] . $_sMinPrefix . '.' . $_aPathPartsURL[ 'extension' ] | ||
| 354 | : $_sSRC; | ||
| 355 | |||
| 356 | } | ||
| 357 | |||
| 358 | /** | ||
| 359 | * Enqueues inline styles. | ||
| 360 | * | ||
| 361 | * @since 3.7.0 | ||
| 362 | * @callback action wp_print_footer_scripts | ||
| 363 | * @callback action wp_print_styles | ||
| 364 | * @return void | ||
| 365 | */ | ||
| 366 |     public function _replyToAddStyle() { | ||
| 367 | |||
| 368 |         if ( ! $this->oForm->isInThePage() ) { | ||
| 369 | return; | ||
| 370 | } | ||
| 371 | $_sCSSRules = $this->___getFormattedInternalStyles( | ||
| 372 | $this->oForm->getResources( 'internal_styles' ) | ||
| 373 | ); | ||
| 374 | |||
| 375 | $_sID = $this->sanitizeSlug( strtolower( $this->oForm->aArguments[ 'caller_id' ] ) ); | ||
| 376 |         if ( $_sCSSRules ) { | ||
| 377 |             echo "<style type='text/css' id='internal-style-{$_sID}' class='admin-page-framework-form-style'>" | ||
| 378 | . $_sCSSRules | ||
| 379 | . "</style>"; | ||
| 380 | } | ||
| 381 | $_sIECSSRules = $this->___getFormattedInternalStyles( | ||
| 382 | $this->oForm->getResources( 'internal_styles_ie' ) | ||
| 383 | ); | ||
| 384 |         if ( $_sIECSSRules ) { | ||
| 385 |             echo "<!--[if IE]><style type='text/css' id='internal-style-ie-{$_sID}' class='admin-page-framework-form-ie-style'>" | ||
| 386 | . $_sIECSSRules | ||
| 387 | . "</style><![endif]-->"; | ||
| 388 | } | ||
| 389 | |||
| 390 | // Empty the values as this method can be called multiple times, in the head tag and the footer. | ||
| 391 | $this->oForm->setResources( 'internal_styles', array() ); | ||
| 392 | $this->oForm->setResources( 'internal_styles_ie', array() ); | ||
| 393 | |||
| 394 | } | ||
| 395 | /** | ||
| 396 | * @since 3.7.0 | ||
| 397 | * @string | ||
| 398 | */ | ||
| 399 |         private function ___getFormattedInternalStyles( array $aInternalStyles ) { | ||
| 400 | return trim( implode( PHP_EOL, array_unique( $aInternalStyles ) ) ); | ||
| 401 | } | ||
| 402 | |||
| 403 | /** | ||
| 404 | * Enqueues page inline scripts. | ||
| 405 | * | ||
| 406 | * @since 3.7.0 | ||
| 407 | */ | ||
| 408 |     public function _replyToAddScript() { | ||
| 425 | |||
| 426 | } | ||
| 427 | |||
| 428 | } |