Complex classes like WordAds_Params 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_Params, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class WordAds_Params { |
||
6 | |||
7 | /** |
||
8 | * Setup parameters for serving the ads |
||
9 | * |
||
10 | * @since 4.5.0 |
||
11 | */ |
||
12 | public function __construct() { |
||
13 | // WordAds setting => default |
||
14 | $settings = array( |
||
15 | 'wordads_approved' => false, |
||
16 | 'wordads_active' => false, |
||
17 | 'wordads_house' => true, |
||
18 | 'wordads_unsafe' => false, |
||
19 | 'enable_header_ad' => true, |
||
20 | 'wordads_second_belowpost' => true, |
||
21 | 'wordads_display_front_page' => true, |
||
22 | 'wordads_display_post' => true, |
||
23 | 'wordads_display_page' => true, |
||
24 | 'wordads_display_archive' => true, |
||
25 | 'wordads_custom_adstxt' => '', |
||
26 | 'wordads_custom_adstxt_enabled' => false, |
||
27 | 'wordads_ccpa_enabled' => false, |
||
28 | 'wordads_ccpa_privacy_policy_url' => get_option( 'wp_page_for_privacy_policy' ) ? get_permalink( (int) get_option( 'wp_page_for_privacy_policy' ) ) : '', |
||
29 | ); |
||
30 | |||
31 | // grab settings, or set as default if it doesn't exist |
||
32 | $this->options = array(); |
||
|
|||
33 | foreach ( $settings as $setting => $default ) { |
||
34 | $option = get_option( $setting, null ); |
||
35 | |||
36 | if ( is_null( $option ) ) { |
||
37 | |||
38 | // Handle retroactively setting wordads_custom_adstxt_enabled to true if custom ads.txt content is already entered. |
||
39 | if ( 'wordads_custom_adstxt_enabled' === $setting ) { |
||
40 | $default = get_option( 'wordads_custom_adstxt' ) !== ''; |
||
41 | } |
||
42 | |||
43 | update_option( $setting, $default, true ); |
||
44 | $option = $default; |
||
45 | } |
||
46 | |||
47 | $this->options[ $setting ] = is_bool( $default ) ? (bool) $option : $option; |
||
48 | } |
||
49 | |||
50 | $host = 'localhost'; |
||
51 | if ( isset( $_SERVER['HTTP_HOST'] ) ) { |
||
52 | $host = $_SERVER['HTTP_HOST']; |
||
53 | } |
||
54 | |||
55 | $this->url = ( is_ssl() ? 'https' : 'http' ) . '://' . $host . $_SERVER['REQUEST_URI']; |
||
56 | if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) { |
||
57 | $this->url = substr( $this->url, 0, strpos( $this->url, '?' ) ); |
||
58 | } |
||
59 | |||
60 | $this->cloudflare = self::is_cloudflare(); |
||
61 | $this->blog_id = Jetpack::get_option( 'id', 0 ); |
||
62 | $this->mobile_device = jetpack_is_mobile( 'any', true ); |
||
63 | $this->targeting_tags = array( |
||
64 | 'WordAds' => 1, |
||
65 | 'BlogId' => ( new Status() )->is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ), |
||
66 | 'Domain' => esc_js( wp_parse_url( home_url(), PHP_URL_HOST ) ), |
||
67 | 'PageURL' => esc_js( $this->url ), |
||
68 | 'LangId' => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else? |
||
69 | 'AdSafe' => 1, // TODO |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return boolean true if the user is browsing on a mobile device (iPad not included) |
||
75 | * |
||
76 | * @since 4.5.0 |
||
77 | */ |
||
78 | public function is_mobile() { |
||
81 | |||
82 | /** |
||
83 | * @return boolean true if site is being served via CloudFlare |
||
84 | * |
||
85 | * @since 4.5.0 |
||
86 | */ |
||
87 | public static function is_cloudflare() { |
||
99 | |||
100 | /** |
||
101 | * @return boolean true if user is browsing in iOS device |
||
102 | * |
||
103 | * @since 4.5.0 |
||
104 | */ |
||
105 | public function is_ios() { |
||
108 | |||
109 | /** |
||
110 | * Returns the user's device (see user-agent.php) or 'desktop' |
||
111 | * |
||
112 | * @return string user device |
||
113 | * |
||
114 | * @since 4.5.0 |
||
115 | */ |
||
116 | public function get_device() { |
||
129 | |||
130 | /** |
||
131 | * @return string The type of page that is being loaded |
||
132 | * |
||
133 | * @since 4.5.0 |
||
134 | */ |
||
135 | public function get_page_type() { |
||
160 | |||
161 | /** |
||
162 | * @return int The page type code for ipw config |
||
163 | * |
||
164 | * @since 5.6.0 |
||
165 | */ |
||
166 | public function get_page_type_ipw() { |
||
189 | |||
190 | /** |
||
191 | * Returns true if page is static home |
||
192 | * |
||
193 | * @return boolean true if page is static home |
||
194 | * |
||
195 | * @since 4.5.0 |
||
196 | */ |
||
197 | public static function is_static_home() { |
||
202 | |||
203 | /** |
||
204 | * Logic for if we should show an ad |
||
205 | * |
||
206 | * @since 4.5.0 |
||
207 | */ |
||
208 | public function should_show() { |
||
237 | } |
||
238 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: