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() { |
||
160 | |||
161 | /** |
||
162 | * Register desktop scripts and styles |
||
163 | * |
||
164 | * @since 4.5.0 |
||
165 | */ |
||
166 | function enqueue_scripts() { |
||
174 | |||
175 | /** |
||
176 | * IPONWEB metadata used by the various scripts |
||
177 | * @return [type] [description] |
||
|
|||
178 | */ |
||
179 | function insert_head_meta() { |
||
193 | |||
194 | /** |
||
195 | * IPONWEB scripts in <head> |
||
196 | * |
||
197 | * @since 4.5.0 |
||
198 | */ |
||
199 | function insert_head_iponweb() { |
||
222 | |||
223 | /** |
||
224 | * Insert the ad onto the page |
||
225 | * |
||
226 | * @since 4.5.0 |
||
227 | */ |
||
228 | function insert_ad( $content ) { |
||
250 | |||
251 | /** |
||
252 | * Inserts ad into header |
||
253 | * |
||
254 | * @since 4.5.0 |
||
255 | */ |
||
256 | function insert_header_ad() { |
||
273 | |||
274 | /** |
||
275 | * Special cases for inserting header unit via jQuery |
||
276 | * |
||
277 | * @since 4.5.0 |
||
278 | */ |
||
279 | function insert_header_ad_special() { |
||
314 | |||
315 | /** |
||
316 | * Get the ad for the spot and type. |
||
317 | * @param string $spot top, side, or belowpost |
||
318 | * @param string $type iponweb or adsense |
||
319 | */ |
||
320 | function get_ad( $spot, $type = 'iponweb' ) { |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Returns the snippet to be inserted into the ad unit |
||
372 | * @param int $section_id |
||
373 | * @param int $height |
||
374 | * @param int $width |
||
375 | * @param string $css |
||
376 | * @return string |
||
377 | * |
||
378 | * @since 5.7 |
||
379 | */ |
||
380 | function get_ad_snippet( $section_id, $height, $width, $adblock_unit = 'mrec', $css = '' ) { |
||
403 | |||
404 | /** |
||
405 | * Get Criteo Acceptable Ad unit |
||
406 | * @param string $unit mrec, mrec2, widesky, top, top_mrec |
||
407 | * |
||
408 | * @since 5.3 |
||
409 | */ |
||
410 | public function get_adblocker_ad( $unit = 'mrec' ) { |
||
437 | |||
438 | /** |
||
439 | * Check the reasons to bail before we attempt to insert ads. |
||
440 | * @return true if we should bail (don't insert ads) |
||
441 | * |
||
442 | * @since 4.5.0 |
||
443 | */ |
||
444 | public function should_bail() { |
||
447 | |||
448 | /** |
||
449 | * Returns markup for HTML5 house ad base on unit |
||
450 | * @param string $unit mrec, widesky, or leaderboard |
||
451 | * @return string markup for HTML5 house ad |
||
452 | * |
||
453 | * @since 4.7.0 |
||
454 | */ |
||
455 | public function get_house_ad( $unit = 'mrec' ) { |
||
482 | |||
483 | /** |
||
484 | * Activation hook actions |
||
485 | * |
||
486 | * @since 4.5.0 |
||
487 | */ |
||
488 | public static function activate() { |
||
491 | } |
||
492 | |||
499 |
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.