Complex classes like Jetpack_Geo_Location 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 Jetpack_Geo_Location, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Jetpack_Geo_Location { |
||
| 34 | private static $instance; |
||
| 35 | |||
| 36 | public static function init() { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * This is mostly just used for testing purposes. |
||
| 46 | */ |
||
| 47 | public static function reset_instance() { |
||
| 50 | |||
| 51 | public function __construct() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Register support for the geo-location feature on pages and posts. Register the meta |
||
| 62 | * fields managed by this plugin so that they are properly sanitized during save. |
||
| 63 | */ |
||
| 64 | public function wordpress_init() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Filter "public" input to always be either 1 or 0. |
||
| 116 | * |
||
| 117 | * @param mixed $public |
||
| 118 | * |
||
| 119 | * @return int |
||
| 120 | */ |
||
| 121 | public function sanitize_public( $public ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Filter geo coordinates and normalize them to floats with 7 digits of precision. |
||
| 127 | * |
||
| 128 | * @param mixed $coordinate |
||
| 129 | * |
||
| 130 | * @return float|null |
||
| 131 | */ |
||
| 132 | public function sanitize_coordinate( $coordinate ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Render geo.position and ICBM meta tags with public geo meta values when rendering |
||
| 142 | * a single post. |
||
| 143 | */ |
||
| 144 | public function wp_head() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Append public meta values in the Geo microformat (https://en.wikipedia.org/wiki/Geo_(microformat) |
||
| 181 | * to the supplied content. |
||
| 182 | * |
||
| 183 | * Note that we cannot render the microformat in the context of an excerpt because tags are stripped |
||
| 184 | * in that context, making our microformat data visible. |
||
| 185 | * |
||
| 186 | * @param string $content |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function the_content_microformat( $content ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Register a range of hooks for integrating geo data with various feeds. |
||
| 223 | */ |
||
| 224 | public function register_rss_hooks() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Add the georss namespace during RSS generation. |
||
| 236 | */ |
||
| 237 | public function rss_namespace() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Output georss data for RSS items, assuming we have data for the currently rendered post and |
||
| 243 | * that data as marked as public. |
||
| 244 | */ |
||
| 245 | public function rss_item() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Enqueue CSS for rendering post flair with geo-location. |
||
| 264 | */ |
||
| 265 | public function enqueue_scripts() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * If we're rendering a single post and public geo-location data is available for it, |
||
| 271 | * include the human-friendly location label in the output. |
||
| 272 | * |
||
| 273 | * @param string $content |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function the_content_location_display( $content ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get the HTML for displaying a label representing the location associated with the |
||
| 287 | * supplied post ID. If no post ID is given, we'll use the global $post variable, if |
||
| 288 | * it is available. |
||
| 289 | * |
||
| 290 | * @param integer|null $post_id |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function get_location_label( $post_id = null ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the ID of the current global post object, if available. Otherwise, return null. |
||
| 326 | * |
||
| 327 | * This isolates the access of the global scope to this single method, making it easier to |
||
| 328 | * safeguard against unexpected missing $post objects in other hook functions. |
||
| 329 | * |
||
| 330 | * @return int|null |
||
| 331 | */ |
||
| 332 | public function get_post_id() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * This method always returns an array with the following structure: |
||
| 344 | * |
||
| 345 | * array(is_public => bool, latitude => float, longitude => float, label => string, is_populated => bool) |
||
| 346 | * |
||
| 347 | * So, regardless of whether your post actually has values in postmeta for the geo-location fields, |
||
| 348 | * you can be sure that you can reference those array keys in calling code without having to juggle |
||
| 349 | * isset(), array_key_exists(), etc. |
||
| 350 | * |
||
| 351 | * Mocking this method during testing can also be useful for testing output and logic in various |
||
| 352 | * hook functions. |
||
| 353 | * |
||
| 354 | * @param integer $post_id |
||
| 355 | * |
||
| 356 | * @return array A predictably structured array representing the meta values for the supplied post ID. |
||
| 357 | */ |
||
| 358 | public function get_meta_values( $post_id ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * This function wraps get_post_meta() to enable us to keep the "geo_" prefix isolated to a single |
||
| 376 | * location in the code and to assist in mocking during testing. |
||
| 377 | * |
||
| 378 | * @param integer $post_id |
||
| 379 | * @param string $meta_field_name |
||
| 380 | * |
||
| 381 | * @return mixed |
||
| 382 | */ |
||
| 383 | public function get_meta_value( $post_id, $meta_field_name ) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Check to see if the current filter is the get_the_excerpt filter. |
||
| 393 | * |
||
| 394 | * Just checking current_filter() here is not adequate because current_filter() only looks |
||
| 395 | * at the last element in the $wp_current_filter array. In the context of rendering an |
||
| 396 | * excerpt, however, both get_the_excerpt and the_content are present in that array. |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | public function is_currently_excerpt_filter() { |
||
| 409 | } |
||
| 410 | |||
| 412 |