Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SAL_Site 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 SAL_Site, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class SAL_Site { |
||
| 23 | public $blog_id; |
||
| 24 | public $platform; |
||
| 25 | |||
| 26 | public function __construct( $blog_id, $platform ) { |
||
| 27 | $this->blog_id = $blog_id; |
||
| 28 | $this->platform = $platform; |
||
| 29 | } |
||
| 30 | |||
| 31 | public function get_id() { |
||
| 32 | return $this->blog_id; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function get_name() { |
||
| 36 | return (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function get_description() { |
||
| 40 | return (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES ); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function get_url() { |
||
| 44 | return (string) home_url(); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function get_post_count() { |
||
| 48 | return (int) wp_count_posts( 'post' )->publish; |
||
| 49 | } |
||
| 50 | |||
| 51 | abstract public function has_videopress(); |
||
| 52 | |||
| 53 | abstract public function upgraded_filetypes_enabled(); |
||
| 54 | |||
| 55 | abstract public function is_mapped_domain(); |
||
| 56 | |||
| 57 | abstract public function is_redirect(); |
||
| 58 | |||
| 59 | abstract public function featured_images_enabled(); |
||
| 60 | |||
| 61 | abstract public function has_wordads(); |
||
| 62 | |||
| 63 | abstract public function get_frame_nonce(); |
||
| 64 | |||
| 65 | abstract public function allowed_file_types(); |
||
| 66 | |||
| 67 | abstract public function get_post_formats(); |
||
| 68 | |||
| 69 | abstract public function is_private(); |
||
| 70 | |||
| 71 | abstract public function is_following(); |
||
| 72 | |||
| 73 | abstract public function get_subscribers_count(); |
||
| 74 | |||
| 75 | abstract public function get_locale(); |
||
| 76 | |||
| 77 | abstract public function is_jetpack(); |
||
| 78 | |||
| 79 | abstract public function get_jetpack_modules(); |
||
| 80 | |||
| 81 | abstract public function is_vip(); |
||
| 82 | |||
| 83 | abstract public function is_multisite(); |
||
| 84 | |||
| 85 | abstract public function is_single_user_site(); |
||
| 86 | |||
| 87 | abstract public function get_plan(); |
||
| 88 | |||
| 89 | abstract public function get_ak_vp_bundle_enabled(); |
||
| 90 | |||
| 91 | abstract public function before_render(); |
||
| 92 | |||
| 93 | abstract public function after_render( &$response ); |
||
| 94 | |||
| 95 | // TODO - factor this out? Seems an odd thing to have on a site |
||
|
|
|||
| 96 | abstract public function after_render_options( &$options ); |
||
| 97 | |||
| 98 | // wrap a WP_Post object with SAL methods |
||
| 99 | abstract public function wrap_post( $post, $context ); |
||
| 100 | |||
| 101 | |||
| 102 | public function get_post_by_id( $post_id, $context ) { |
||
| 103 | $post = get_post( $post_id, OBJECT, $context ); |
||
| 104 | |||
| 105 | if ( ! $post ) { |
||
| 106 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
||
| 107 | } |
||
| 108 | |||
| 109 | $wrapped_post = $this->wrap_post( $post, $context ); |
||
| 110 | |||
| 111 | // validate access |
||
| 112 | return $this->validate_access( $wrapped_post ); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Validate current user can access the post |
||
| 117 | * |
||
| 118 | * @return WP_Error or post |
||
| 119 | */ |
||
| 120 | private function validate_access( $post ) { |
||
| 121 | $context = $post->context; |
||
| 122 | |||
| 123 | View Code Duplication | if ( ! $this->is_post_type_allowed( $post->post_type ) |
|
| 124 | && |
||
| 125 | ( ! function_exists( 'is_post_freshly_pressed' ) || ! is_post_freshly_pressed( $post->ID ) ) ) { |
||
| 126 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
||
| 127 | } |
||
| 128 | |||
| 129 | switch ( $context ) { |
||
| 130 | case 'edit' : |
||
| 131 | if ( ! current_user_can( 'edit_post', $post ) ) { |
||
| 132 | return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); |
||
| 133 | } |
||
| 134 | break; |
||
| 135 | case 'display' : |
||
| 136 | $can_view = $this->user_can_view_post( $post ); |
||
| 137 | if ( is_wp_error( $can_view ) ) { |
||
| 138 | return $can_view; |
||
| 139 | } |
||
| 140 | break; |
||
| 141 | default : |
||
| 142 | return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 ); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $post; |
||
| 146 | } |
||
| 147 | |||
| 148 | // copied from class.json-api-endpoints.php |
||
| 149 | View Code Duplication | private function is_post_type_allowed( $post_type ) { |
|
| 150 | // if the post type is empty, that's fine, WordPress will default to post |
||
| 151 | if ( empty( $post_type ) ) |
||
| 152 | return true; |
||
| 153 | |||
| 154 | // allow special 'any' type |
||
| 155 | if ( 'any' == $post_type ) |
||
| 156 | return true; |
||
| 157 | |||
| 158 | // check for allowed types |
||
| 159 | if ( in_array( $post_type, $this->_get_whitelisted_post_types() ) ) |
||
| 160 | return true; |
||
| 161 | |||
| 162 | return false; |
||
| 163 | } |
||
| 164 | |||
| 165 | // copied from class.json-api-endpoints.php |
||
| 166 | /** |
||
| 167 | * Gets the whitelisted post types that JP should allow access to. |
||
| 168 | * |
||
| 169 | * @return array Whitelisted post types. |
||
| 170 | */ |
||
| 171 | View Code Duplication | private function _get_whitelisted_post_types() { |
|
| 172 | $allowed_types = array( 'post', 'page', 'revision' ); |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Filter the post types Jetpack has access to, and can synchronize with WordPress.com. |
||
| 176 | * |
||
| 177 | * @module json-api |
||
| 178 | * |
||
| 179 | * @since 2.2.3 |
||
| 180 | * |
||
| 181 | * @param array $allowed_types Array of whitelisted post types. Default to `array( 'post', 'page', 'revision' )`. |
||
| 182 | */ |
||
| 183 | $allowed_types = apply_filters( 'rest_api_allowed_post_types', $allowed_types ); |
||
| 184 | |||
| 185 | return array_unique( $allowed_types ); |
||
| 186 | } |
||
| 187 | |||
| 188 | // copied and modified a little from class.json-api-endpoints.php |
||
| 189 | private function user_can_view_post( $post ) { |
||
| 190 | if ( !$post || is_wp_error( $post ) ) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | |||
| 194 | View Code Duplication | if ( 'inherit' === $post->post_status ) { |
|
| 195 | $parent_post = get_post( $post->post_parent ); |
||
| 196 | $post_status_obj = get_post_status_object( $parent_post->post_status ); |
||
| 197 | } else { |
||
| 198 | $post_status_obj = get_post_status_object( $post->post_status ); |
||
| 199 | } |
||
| 200 | |||
| 201 | $authorized = ( |
||
| 202 | $post_status_obj->public || |
||
| 203 | ( is_user_logged_in() && |
||
| 204 | ( |
||
| 205 | ( $post_status_obj->protected && current_user_can( 'edit_post', $post->ID ) ) || |
||
| 206 | ( $post_status_obj->private && current_user_can( 'read_post', $post->ID ) ) || |
||
| 207 | ( 'trash' === $post->post_status && current_user_can( 'edit_post', $post->ID ) ) || |
||
| 208 | 'auto-draft' === $post->post_status |
||
| 209 | ) |
||
| 210 | ) |
||
| 211 | ); |
||
| 212 | |||
| 213 | if ( ! $authorized ) { |
||
| 214 | return new WP_Error( 'unauthorized', 'User cannot view post', 403 ); |
||
| 215 | } |
||
| 216 | |||
| 217 | View Code Duplication | if ( |
|
| 218 | -1 == get_option( 'blog_public' ) && |
||
| 219 | /** |
||
| 220 | * Filter access to a specific post. |
||
| 221 | * |
||
| 222 | * @module json-api |
||
| 223 | * |
||
| 224 | * @since 3.4.0 |
||
| 225 | * |
||
| 226 | * @param bool current_user_can( 'read_post', $post->ID ) Can the current user access the post. |
||
| 227 | * @param WP_Post $post Post data. |
||
| 228 | */ |
||
| 229 | ! apply_filters( |
||
| 230 | 'wpcom_json_api_user_can_view_post', |
||
| 231 | current_user_can( 'read_post', $post->ID ), |
||
| 232 | $post |
||
| 233 | ) |
||
| 234 | ) { |
||
| 235 | return new WP_Error( 'unauthorized', 'User cannot view post', array( 'status_code' => 403, 'error' => 'private_blog' ) ); |
||
| 236 | } |
||
| 237 | |||
| 238 | View Code Duplication | if ( strlen( $post->post_password ) && !current_user_can( 'edit_post', $post->ID ) ) { |
|
| 239 | return new WP_Error( 'unauthorized', 'User cannot view password protected post', array( 'status_code' => 403, 'error' => 'password_protected' ) ); |
||
| 240 | } |
||
| 241 | |||
| 242 | return true; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get post by name |
||
| 247 | * |
||
| 248 | * Attempts to match name on post title and page path |
||
| 249 | * |
||
| 250 | * @param string $name |
||
| 251 | * @param string $context (display or edit) |
||
| 252 | * |
||
| 253 | * @return int|object Post ID on success, WP_Error object on failure |
||
| 254 | **/ |
||
| 255 | View Code Duplication | public function get_post_by_name( $name, $context ) { |
|
| 256 | $name = sanitize_title( $name ); |
||
| 257 | |||
| 258 | if ( ! $name ) { |
||
| 259 | return new WP_Error( 'invalid_post', 'Invalid post', 400 ); |
||
| 260 | } |
||
| 261 | |||
| 262 | $posts = get_posts( array( 'name' => $name, 'numberposts' => 1 ) ); |
||
| 263 | |||
| 264 | if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) { |
||
| 265 | $page = get_page_by_path( $name ); |
||
| 266 | |||
| 267 | if ( ! $page ) { |
||
| 268 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
||
| 269 | } |
||
| 270 | |||
| 271 | $post_id = $page->ID; |
||
| 272 | } else { |
||
| 273 | $post_id = (int) $posts[0]->ID; |
||
| 274 | } |
||
| 275 | |||
| 276 | return $this->get_post_by_id( $post_id, $context ); |
||
| 277 | } |
||
| 278 | |||
| 279 | function user_can_manage() { |
||
| 280 | current_user_can( 'manage_options' ); |
||
| 281 | } |
||
| 282 | |||
| 283 | function get_xmlrpc_url() { |
||
| 284 | $xmlrpc_scheme = apply_filters( 'wpcom_json_api_xmlrpc_scheme', parse_url( get_option( 'home' ), PHP_URL_SCHEME ) ); |
||
| 285 | return site_url( 'xmlrpc.php', $xmlrpc_scheme ); |
||
| 286 | } |
||
| 287 | |||
| 288 | function get_registered_date() { |
||
| 289 | if ( function_exists( 'get_blog_details' ) ) { |
||
| 290 | $blog_details = get_blog_details(); |
||
| 291 | if ( ! empty( $blog_details->registered ) ) { |
||
| 292 | return WPCOM_JSON_API_Date::format_date( $blog_details->registered ); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | return '0000-00-00T00:00:00+00:00'; |
||
| 297 | } |
||
| 298 | |||
| 299 | function get_capabilities() { |
||
| 318 | |||
| 319 | function is_visible() { |
||
| 335 | |||
| 336 | function get_logo() { |
||
| 360 | |||
| 361 | function get_timezone() { |
||
| 362 | return (string) get_option( 'timezone_string' ); |
||
| 363 | } |
||
| 364 | |||
| 365 | function get_gmt_offset() { |
||
| 366 | return (float) get_option( 'gmt_offset' ); |
||
| 367 | } |
||
| 368 | |||
| 369 | function get_login_url() { |
||
| 370 | return wp_login_url(); |
||
| 371 | } |
||
| 372 | |||
| 373 | function get_admin_url() { |
||
| 374 | return get_admin_url(); |
||
| 375 | } |
||
| 376 | |||
| 377 | function get_unmapped_url() { |
||
| 378 | return get_site_url( $this->blog_id ); |
||
| 379 | } |
||
| 380 | |||
| 381 | function get_theme_slug() { |
||
| 382 | return get_option( 'stylesheet' ); |
||
| 384 | |||
| 385 | function get_header_image() { |
||
| 388 | |||
| 389 | function get_background_color() { |
||
| 392 | |||
| 393 | function get_image_default_link_type() { |
||
| 396 | |||
| 397 | function get_image_thumbnail_width() { |
||
| 400 | |||
| 401 | function get_image_thumbnail_height() { |
||
| 404 | |||
| 405 | function get_image_thumbnail_crop() { |
||
| 408 | |||
| 409 | function get_image_medium_width() { |
||
| 412 | |||
| 413 | function get_image_medium_height() { |
||
| 416 | |||
| 417 | function get_image_large_width() { |
||
| 420 | |||
| 421 | function get_image_large_height() { |
||
| 424 | |||
| 425 | function get_permalink_structure() { |
||
| 428 | |||
| 429 | function get_default_post_format() { |
||
| 432 | |||
| 433 | function get_default_category() { |
||
| 436 | |||
| 437 | function get_show_on_front() { |
||
| 440 | |||
| 441 | function is_custom_front_page() { |
||
| 444 | |||
| 445 | function get_default_likes_enabled() { |
||
| 448 | |||
| 449 | function get_default_sharing_status() { |
||
| 458 | |||
| 459 | function get_default_comment_status() { |
||
| 462 | |||
| 463 | function default_ping_status() { |
||
| 466 | |||
| 467 | function is_publicize_permanently_disabled() { |
||
| 474 | |||
| 475 | function get_page_on_front() { |
||
| 478 | |||
| 479 | function get_page_for_posts() { |
||
| 482 | |||
| 483 | function is_headstart() { |
||
| 486 | |||
| 487 | function get_wordpress_version() { |
||
| 491 | } |