Complex classes like WPCOM_JSON_API_Site_Settings_Endpoint 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 WPCOM_JSON_API_Site_Settings_Endpoint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class WPCOM_JSON_API_Site_Settings_Endpoint extends WPCOM_JSON_API_Endpoint { |
||
4 | |||
5 | public static $site_format = array( |
||
6 | 'ID' => '(int) Site ID', |
||
7 | 'name' => '(string) Title of site', |
||
8 | 'description' => '(string) Tagline or description of site', |
||
9 | 'URL' => '(string) Full URL to the site', |
||
10 | 'lang' => '(string) Primary language code of the site', |
||
11 | 'settings' => '(array) An array of options/settings for the blog. Only viewable by users with post editing rights to the site.', |
||
12 | ); |
||
13 | |||
14 | // GET /sites/%s/settings |
||
15 | // POST /sites/%s/settings |
||
16 | function callback( $path = '', $blog_id = 0 ) { |
||
17 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
||
18 | if ( is_wp_error( $blog_id ) ) { |
||
19 | return $blog_id; |
||
20 | } |
||
21 | |||
22 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
23 | $this->load_theme_functions(); |
||
24 | } |
||
25 | |||
26 | if ( ! is_user_logged_in() ) { |
||
27 | return new WP_Error( 'Unauthorized', 'You must be logged-in to manage settings.', 401 ); |
||
28 | } else if ( ! current_user_can( 'manage_options' ) ) { |
||
29 | return new WP_Error( 'Forbidden', 'You do not have the capability to manage settings for this site.', 403 ); |
||
30 | } |
||
31 | |||
32 | if ( 'GET' === $this->api->method ) { |
||
33 | /** |
||
34 | * Fires on each GET request to a specific endpoint. |
||
35 | * |
||
36 | * @module json-api |
||
37 | * |
||
38 | * @since 3.2.0 |
||
39 | * |
||
40 | * @param string sites. |
||
41 | */ |
||
42 | do_action( 'wpcom_json_api_objects', 'sites' ); |
||
43 | return $this->get_settings_response(); |
||
44 | } else if ( 'POST' === $this->api->method ) { |
||
45 | return $this->update_settings(); |
||
46 | } else { |
||
47 | return new WP_Error( 'bad_request', 'An unsupported request method was used.' ); |
||
48 | } |
||
49 | |||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Determines whether jetpack_relatedposts is supported |
||
54 | * |
||
55 | * @return (bool) |
||
56 | */ |
||
57 | public function jetpack_relatedposts_supported() { |
||
58 | $wpcom_related_posts_theme_blacklist = array( |
||
59 | 'Expound', |
||
60 | 'Traveler', |
||
61 | 'Opti', |
||
62 | 'Currents', |
||
63 | ); |
||
64 | return ( ! in_array( wp_get_theme()->get( 'Name' ), $wpcom_related_posts_theme_blacklist ) ); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Returns category details |
||
69 | * |
||
70 | * @return (array) |
||
71 | */ |
||
72 | public function get_category_details( $category ) { |
||
73 | return array( |
||
74 | 'value' => $category->term_id, |
||
75 | 'name' => $category->name |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Collects the necessary information to return for a get settings response. |
||
81 | * |
||
82 | * @return (array) |
||
83 | */ |
||
84 | public function get_settings_response() { |
||
85 | |||
86 | $response_format = self::$site_format; |
||
87 | $blog_id = (int) $this->api->get_blog_id_for_output(); |
||
88 | /** This filter is documented in class.json-api-endpoints.php */ |
||
89 | $is_jetpack = true === apply_filters( 'is_jetpack_site', false, $blog_id ); |
||
|
|||
90 | |||
91 | foreach ( array_keys( $response_format ) as $key ) { |
||
92 | switch ( $key ) { |
||
93 | case 'ID' : |
||
94 | $response[$key] = $blog_id; |
||
95 | break; |
||
96 | case 'name' : |
||
97 | $response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); |
||
98 | break; |
||
99 | case 'description' : |
||
100 | $response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES ); |
||
101 | break; |
||
102 | case 'URL' : |
||
103 | $response[$key] = (string) home_url(); |
||
104 | break; |
||
105 | case 'lang' : |
||
106 | $response[$key] = (string) get_bloginfo( 'language' ); |
||
107 | break; |
||
108 | case 'settings': |
||
109 | |||
110 | $jetpack_relatedposts_options = Jetpack_Options::get_option( 'relatedposts' ); |
||
111 | |||
112 | if ( method_exists( 'Jetpack', 'is_module_active' ) ) { |
||
113 | $jetpack_relatedposts_options[ 'enabled' ] = Jetpack::is_module_active( 'related-posts' ); |
||
114 | } |
||
115 | |||
116 | // array_values() is necessary to ensure the array starts at index 0. |
||
117 | $post_categories = array_values( |
||
118 | array_map( |
||
119 | array( $this, 'get_category_details' ), |
||
120 | get_categories( array( 'hide_empty' => false ) ) |
||
121 | ) |
||
122 | ); |
||
123 | |||
124 | $eventbrite_api_token = (int) get_option( 'eventbrite_api_token' ); |
||
125 | if ( 0 === $eventbrite_api_token ) { |
||
126 | $eventbrite_api_token = null; |
||
127 | } |
||
128 | |||
129 | $holiday_snow = false; |
||
130 | if ( function_exists( 'jetpack_holiday_snow_option_name' ) ) { |
||
131 | $holiday_snow = (bool) get_option( jetpack_holiday_snow_option_name() ); |
||
132 | } |
||
133 | |||
134 | $response[$key] = array( |
||
135 | |||
136 | // also exists as "options" |
||
137 | 'admin_url' => get_admin_url(), |
||
138 | 'default_ping_status' => (bool) ( 'closed' != get_option( 'default_ping_status' ) ), |
||
139 | 'default_comment_status' => (bool) ( 'closed' != get_option( 'default_comment_status' ) ), |
||
140 | |||
141 | // new stuff starts here |
||
142 | 'blog_public' => (int) get_option( 'blog_public' ), |
||
143 | 'jetpack_sync_non_public_post_stati' => (bool) Jetpack_Options::get_option( 'sync_non_public_post_stati' ), |
||
144 | 'jetpack_relatedposts_allowed' => (bool) $this->jetpack_relatedposts_supported(), |
||
145 | 'jetpack_relatedposts_enabled' => (bool) $jetpack_relatedposts_options[ 'enabled' ], |
||
146 | 'jetpack_relatedposts_show_headline' => (bool) $jetpack_relatedposts_options[ 'show_headline' ], |
||
147 | 'jetpack_relatedposts_show_thumbnails' => (bool) $jetpack_relatedposts_options[ 'show_thumbnails' ], |
||
148 | 'default_category' => (int) get_option('default_category'), |
||
149 | 'post_categories' => (array) $post_categories, |
||
150 | 'default_post_format' => get_option( 'default_post_format' ), |
||
151 | 'default_pingback_flag' => (bool) get_option( 'default_pingback_flag' ), |
||
152 | 'require_name_email' => (bool) get_option( 'require_name_email' ), |
||
153 | 'comment_registration' => (bool) get_option( 'comment_registration' ), |
||
154 | 'close_comments_for_old_posts' => (bool) get_option( 'close_comments_for_old_posts' ), |
||
155 | 'close_comments_days_old' => (int) get_option( 'close_comments_days_old' ), |
||
156 | 'thread_comments' => (bool) get_option( 'thread_comments' ), |
||
157 | 'thread_comments_depth' => (int) get_option( 'thread_comments_depth' ), |
||
158 | 'page_comments' => (bool) get_option( 'page_comments' ), |
||
159 | 'comments_per_page' => (int) get_option( 'comments_per_page' ), |
||
160 | 'default_comments_page' => get_option( 'default_comments_page' ), |
||
161 | 'comment_order' => get_option( 'comment_order' ), |
||
162 | 'comments_notify' => (bool) get_option( 'comments_notify' ), |
||
163 | 'moderation_notify' => (bool) get_option( 'moderation_notify' ), |
||
164 | 'social_notifications_like' => ( "on" == get_option( 'social_notifications_like' ) ), |
||
165 | 'social_notifications_reblog' => ( "on" == get_option( 'social_notifications_reblog' ) ), |
||
166 | 'social_notifications_subscribe' => ( "on" == get_option( 'social_notifications_subscribe' ) ), |
||
167 | 'comment_moderation' => (bool) get_option( 'comment_moderation' ), |
||
168 | 'comment_whitelist' => (bool) get_option( 'comment_whitelist' ), |
||
169 | 'comment_max_links' => (int) get_option( 'comment_max_links' ), |
||
170 | 'moderation_keys' => get_option( 'moderation_keys' ), |
||
171 | 'blacklist_keys' => get_option( 'blacklist_keys' ), |
||
172 | 'lang_id' => get_option( 'lang_id' ), |
||
173 | 'wga' => get_option( 'wga' ), |
||
174 | 'disabled_likes' => (bool) get_option( 'disabled_likes' ), |
||
175 | 'disabled_reblogs' => (bool) get_option( 'disabled_reblogs' ), |
||
176 | 'jetpack_comment_likes_enabled' => (bool) get_option( 'jetpack_comment_likes_enabled', false ), |
||
177 | 'twitter_via' => (string) get_option( 'twitter_via' ), |
||
178 | 'jetpack-twitter-cards-site-tag' => (string) get_option( 'jetpack-twitter-cards-site-tag' ), |
||
179 | 'eventbrite_api_token' => $eventbrite_api_token, |
||
180 | 'holidaysnow' => $holiday_snow |
||
181 | ); |
||
182 | |||
183 | if ( class_exists( 'Sharing_Service' ) ) { |
||
184 | $ss = new Sharing_Service(); |
||
185 | $sharing = $ss->get_global_options(); |
||
186 | $response[ $key ]['sharing_button_style'] = (string) $sharing['button_style']; |
||
187 | $response[ $key ]['sharing_label'] = (string) $sharing['sharing_label']; |
||
188 | $response[ $key ]['sharing_show'] = (array) $sharing['show']; |
||
189 | $response[ $key ]['sharing_open_links'] = (string) $sharing['open_links']; |
||
190 | } |
||
191 | |||
192 | if ( function_exists( 'jetpack_protect_format_whitelist' ) ) { |
||
193 | $response[ $key ]['jetpack_protect_whitelist'] = jetpack_protect_format_whitelist(); |
||
194 | } |
||
195 | |||
196 | if ( ! current_user_can( 'edit_posts' ) ) |
||
197 | unset( $response[$key] ); |
||
198 | break; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return $response; |
||
203 | |||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Updates site settings for authorized users |
||
208 | * |
||
209 | * @return (array) |
||
210 | */ |
||
211 | public function update_settings() { |
||
406 | } |
||
407 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.