Complex classes like WordAds 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 WordAds, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class WordAds { |
||
| 15 | |||
| 16 | public $params = null; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The different supported ad types. |
||
| 20 | * v0.1 - mrec only for now |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | public static $ad_tag_ids = array( |
||
| 24 | 'mrec' => array( |
||
| 25 | 'tag' => '300x250_mediumrectangle', |
||
| 26 | 'height' => '250', |
||
| 27 | 'width' => '300', |
||
| 28 | ), |
||
| 29 | 'lrec' => array( |
||
| 30 | 'tag' => '336x280_largerectangle', |
||
| 31 | 'height' => '280', |
||
| 32 | 'width' => '336', |
||
| 33 | ), |
||
| 34 | 'leaderboard' => array( |
||
| 35 | 'tag' => '728x90_leaderboard', |
||
| 36 | 'height' => '90', |
||
| 37 | 'width' => '728', |
||
| 38 | ), |
||
| 39 | 'wideskyscraper' => array( |
||
| 40 | 'tag' => '160x600_wideskyscraper', |
||
| 41 | 'height' => '600', |
||
| 42 | 'width' => '160', |
||
| 43 | ), |
||
| 44 | ); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Convenience function for grabbing options from params->options |
||
| 48 | * @param string $option the option to grab |
||
| 49 | * @param mixed $default (optional) |
||
| 50 | * @return option or $default if not set |
||
| 51 | * |
||
| 52 | * @since 4.5.0 |
||
| 53 | */ |
||
| 54 | function option( $option, $default = false ) { |
||
| 55 | if ( ! isset( $this->params->options[ $option ] ) ) { |
||
| 56 | return $default; |
||
| 57 | } |
||
| 58 | |||
| 59 | return $this->params->options[ $option ]; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Instantiate the plugin |
||
| 64 | * |
||
| 65 | * @since 4.5.0 |
||
| 66 | */ |
||
| 67 | function __construct() { |
||
| 68 | add_action( 'init', array( $this, 'init' ) ); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Code to run on WordPress 'init' hook |
||
| 73 | * |
||
| 74 | * @since 4.5.0 |
||
| 75 | */ |
||
| 76 | function init() { |
||
| 77 | // bail on infinite scroll |
||
| 78 | if ( self::is_infinite_scroll() ) { |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | require_once( WORDADS_ROOT . '/php/params.php' ); |
||
| 83 | $this->params = new WordAds_Params(); |
||
| 84 | |||
| 85 | if ( is_admin() ) { |
||
| 86 | require_once( WORDADS_ROOT . '/php/admin.php' ); |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | if ( $this->should_bail() ) { |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->insert_adcode(); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Check for Jetpack's The_Neverending_Home_Page and use got_infinity |
||
| 99 | * @return boolean true if load came from infinite scroll |
||
| 100 | * |
||
| 101 | * @since 4.5.0 |
||
| 102 | */ |
||
| 103 | public static function is_infinite_scroll() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add the actions/filters to insert the ads. Checks for mobile or desktop. |
||
| 109 | * |
||
| 110 | * @since 4.5.0 |
||
| 111 | */ |
||
| 112 | private function insert_adcode() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Register desktop scripts and styles |
||
| 135 | * |
||
| 136 | * @since 4.5.0 |
||
| 137 | */ |
||
| 138 | function enqueue_scripts() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * IPONWEB metadata used by the various scripts |
||
| 149 | * @return [type] [description] |
||
|
|
|||
| 150 | */ |
||
| 151 | function insert_head_meta() { |
||
| 152 | $themename = esc_js( get_stylesheet() ); |
||
| 153 | $pagetype = intval( $this->params->get_page_type_ipw() ); |
||
| 154 | $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : ''; |
||
| 155 | echo <<<HTML |
||
| 156 | <script$data_tags type="text/javascript"> |
||
| 157 | var __ATA_PP = { pt: $pagetype, ht: 2, tn: '$themename', amp: false }; |
||
| 158 | var __ATA = __ATA || {}; |
||
| 159 | __ATA.cmd = __ATA.cmd || []; |
||
| 160 | __ATA.criteo = __ATA.criteo || {}; |
||
| 161 | __ATA.criteo.cmd = __ATA.criteo.cmd || []; |
||
| 162 | </script> |
||
| 163 | HTML; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * IPONWEB scripts in <head> |
||
| 168 | * |
||
| 169 | * @since 4.5.0 |
||
| 170 | */ |
||
| 171 | function insert_head_iponweb() { |
||
| 172 | $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : ''; |
||
| 173 | echo <<<HTML |
||
| 174 | <link rel='dns-prefetch' href='//s.pubmine.com' /> |
||
| 175 | <link rel='dns-prefetch' href='//x.bidswitch.net' /> |
||
| 176 | <link rel='dns-prefetch' href='//static.criteo.net' /> |
||
| 177 | <link rel='dns-prefetch' href='//ib.adnxs.com' /> |
||
| 178 | <link rel='dns-prefetch' href='//aax.amazon-adsystem.com' /> |
||
| 179 | <link rel='dns-prefetch' href='//bidder.criteo.com' /> |
||
| 180 | <link rel='dns-prefetch' href='//cas.criteo.com' /> |
||
| 181 | <link rel='dns-prefetch' href='//gum.criteo.com' /> |
||
| 182 | <link rel='dns-prefetch' href='//ads.pubmatic.com' /> |
||
| 183 | <link rel='dns-prefetch' href='//gads.pubmatic.com' /> |
||
| 184 | <link rel='dns-prefetch' href='//tpc.googlesyndication.com' /> |
||
| 185 | <link rel='dns-prefetch' href='//ad.doubleclick.net' /> |
||
| 186 | <link rel='dns-prefetch' href='//googleads.g.doubleclick.net' /> |
||
| 187 | <link rel='dns-prefetch' href='//www.googletagservices.com' /> |
||
| 188 | <link rel='dns-prefetch' href='//cdn.switchadhub.com' /> |
||
| 189 | <link rel='dns-prefetch' href='//delivery.g.switchadhub.com' /> |
||
| 190 | <link rel='dns-prefetch' href='//delivery.swid.switchadhub.com' /> |
||
| 191 | <script$data_tags async type="text/javascript" src="//s.pubmine.com/head.js"></script> |
||
| 192 | HTML; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Insert the ad onto the page |
||
| 197 | * |
||
| 198 | * @since 4.5.0 |
||
| 199 | */ |
||
| 200 | function insert_ad( $content ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Inserts ad into header |
||
| 225 | * |
||
| 226 | * @since 4.5.0 |
||
| 227 | */ |
||
| 228 | function insert_header_ad() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Special cases for inserting header unit via jQuery |
||
| 248 | * |
||
| 249 | * @since 4.5.0 |
||
| 250 | */ |
||
| 251 | function insert_header_ad_special() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get the ad for the spot and type. |
||
| 289 | * @param string $spot top, side, or belowpost |
||
| 290 | * @param string $type iponweb or adsense |
||
| 291 | */ |
||
| 292 | function get_ad( $spot, $type = 'iponweb' ) { |
||
| 293 | $snippet = ''; |
||
| 294 | $blocker_unit = 'mrec'; |
||
| 295 | if ( 'iponweb' == $type ) { |
||
| 296 | $section_id = WORDADS_API_TEST_ID; |
||
| 297 | $width = 300; |
||
| 298 | $height = 250; |
||
| 299 | $second_belowpost = ''; |
||
| 300 | $snippet = ''; |
||
| 301 | if ( 'top' == $spot ) { |
||
| 302 | // mrec for mobile, leaderboard for desktop |
||
| 303 | $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2'; |
||
| 304 | $width = $this->params->mobile_device ? 300 : 728; |
||
| 305 | $height = $this->params->mobile_device ? 250 : 90; |
||
| 306 | $blocker_unit = $this->params->mobile_device ? 'top_mrec' : 'top'; |
||
| 307 | $snippet = $this->get_ad_snippet( $section_id, $height, $width, $blocker_unit ); |
||
| 308 | } else if ( 'belowpost' == $spot ) { |
||
| 309 | $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1'; |
||
| 310 | $width = 300; |
||
| 311 | $height = 250; |
||
| 312 | |||
| 313 | $snippet = $this->get_ad_snippet( $section_id, $height, $width, 'mrec', 'float:left;margin-right:5px;margin-top:0px;' ); |
||
| 314 | if ( $this->option( 'wordads_second_belowpost', true ) ) { |
||
| 315 | $section_id2 = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID2 : $this->params->blog_id . '4'; |
||
| 316 | $snippet .= $this->get_ad_snippet( $section_id2, $height, $width, 'mrec2', 'float:left;margin-top:0px;' ); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } else if ( 'house' == $type ) { |
||
| 320 | $leaderboard = 'top' == $spot && ! $this->params->mobile_device; |
||
| 321 | $snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' ); |
||
| 322 | if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) { |
||
| 323 | $snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' ); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | $header = 'top' == $spot ? 'wpcnt-header' : ''; |
||
| 328 | $about = __( 'Advertisements', 'jetpack' ); |
||
| 329 | return <<<HTML |
||
| 330 | <div class="wpcnt $header"> |
||
| 331 | <div class="wpa"> |
||
| 332 | <span class="wpa-about">$about</span> |
||
| 333 | <div class="u $spot"> |
||
| 334 | $snippet |
||
| 335 | </div> |
||
| 336 | </div> |
||
| 337 | </div> |
||
| 338 | HTML; |
||
| 339 | } |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns the snippet to be inserted into the ad unit |
||
| 344 | * @param int $section_id |
||
| 345 | * @param int $height |
||
| 346 | * @param int $width |
||
| 347 | * @param string $css |
||
| 348 | * @return string |
||
| 349 | * |
||
| 350 | * @since 5.7 |
||
| 351 | */ |
||
| 352 | function get_ad_snippet( $section_id, $height, $width, $adblock_unit = 'mrec', $css = '' ) { |
||
| 353 | $this->ads[] = array( 'id' => $section_id, 'width' => $width, 'height' => $height ); |
||
| 354 | $data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : ''; |
||
| 355 | $adblock_ad = $this->get_adblocker_ad( $adblock_unit ); |
||
| 356 | |||
| 357 | return <<<HTML |
||
| 358 | <div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css"> |
||
| 359 | <div id="atatags-{$section_id}"> |
||
| 360 | <script$data_tags type="text/javascript"> |
||
| 361 | __ATA.cmd.push(function() { |
||
| 362 | __ATA.initSlot('atatags-{$section_id}', { |
||
| 363 | collapseEmpty: 'before', |
||
| 364 | sectionId: '{$section_id}', |
||
| 365 | width: {$width}, |
||
| 366 | height: {$height} |
||
| 367 | }); |
||
| 368 | }); |
||
| 369 | </script> |
||
| 370 | $adblock_ad |
||
| 371 | </div> |
||
| 372 | </div> |
||
| 373 | HTML; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get Criteo Acceptable Ad unit |
||
| 378 | * @param string $unit mrec, mrec2, widesky, top, top_mrec |
||
| 379 | * |
||
| 380 | * @since 5.3 |
||
| 381 | */ |
||
| 382 | public function get_adblocker_ad( $unit = 'mrec' ) { |
||
| 383 | $data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : ''; |
||
| 384 | $criteo_id = mt_rand(); |
||
| 385 | $height = 250; |
||
| 386 | $width = 300; |
||
| 387 | $zone_id = 388248; |
||
| 388 | if ( 'mrec2' == $unit ) { // 2nd belowpost |
||
| 389 | $zone_id = 837497; |
||
| 390 | } else if ( 'widesky' == $unit ) { // sidebar |
||
| 391 | $zone_id = 563902; |
||
| 392 | $width = 160; |
||
| 393 | $height= 600; |
||
| 394 | } else if ( 'top' == $unit ) { // top leaderboard |
||
| 395 | $zone_id = 563903; |
||
| 396 | $width = 728; |
||
| 397 | $height = 90; |
||
| 398 | } else if ( 'top_mrec' == $unit ) { // top mrec |
||
| 399 | $zone_id = 563903; |
||
| 400 | } |
||
| 401 | |||
| 402 | return <<<HTML |
||
| 403 | <div id="crt-$criteo_id" style="width:{$width}px;height:{$height}px;display:none !important;"></div> |
||
| 404 | <script$data_tags type="text/javascript"> |
||
| 405 | (function(){var c=function(){var a=document.getElementById("crt-{$criteo_id}");window.Criteo?(a.parentNode.style.setProperty("display","inline-block","important"),a.style.setProperty("display","block","important"),window.Criteo.DisplayAcceptableAdIfAdblocked({zoneid:{$zone_id},containerid:"crt-{$criteo_id}",collapseContainerIfNotAdblocked:!0,callifnotadblocked:function(){a.style.setProperty("display","none","important");a.style.setProperty("visbility","hidden","important")}})):(a.style.setProperty("display","none","important"),a.style.setProperty("visibility","hidden","important"))};if(window.Criteo)c();else{if(!__ATA.criteo.script){var b=document.createElement("script");b.src="//static.criteo.net/js/ld/publishertag.js";b.onload=function(){for(var a=0;a<__ATA.criteo.cmd.length;a++){var b=__ATA.criteo.cmd[a];"function"===typeof b&&b()}};(document.head||document.getElementsByTagName("head")[0]).appendChild(b);__ATA.criteo.script=b}__ATA.criteo.cmd.push(c)}})(); |
||
| 406 | </script> |
||
| 407 | HTML; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Check the reasons to bail before we attempt to insert ads. |
||
| 412 | * @return true if we should bail (don't insert ads) |
||
| 413 | * |
||
| 414 | * @since 4.5.0 |
||
| 415 | */ |
||
| 416 | public function should_bail() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns markup for HTML5 house ad base on unit |
||
| 422 | * @param string $unit mrec, widesky, or leaderboard |
||
| 423 | * @return string markup for HTML5 house ad |
||
| 424 | * |
||
| 425 | * @since 4.7.0 |
||
| 426 | */ |
||
| 427 | public function get_house_ad( $unit = 'mrec' ) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Activation hook actions |
||
| 457 | * |
||
| 458 | * @since 4.5.0 |
||
| 459 | */ |
||
| 460 | public static function activate() { |
||
| 463 | } |
||
| 464 | |||
| 465 | add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) ); |
||
| 466 | add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) ); |
||
| 467 | add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) ); |
||
| 468 | |||
| 471 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.