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 WPCOM_JSON_API_Post_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_Post_Endpoint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint { |
||
4 | public $post_object_format = array( |
||
5 | // explicitly document and cast all output |
||
6 | 'ID' => '(int) The post ID.', |
||
7 | 'site_ID' => '(int) The site ID.', |
||
8 | 'author' => '(object>author) The author of the post.', |
||
9 | 'date' => "(ISO 8601 datetime) The post's creation time.", |
||
10 | 'modified' => "(ISO 8601 datetime) The post's most recent update time.", |
||
11 | 'title' => '(HTML) <code>context</code> dependent.', |
||
12 | 'URL' => '(URL) The full permalink URL to the post.', |
||
13 | 'short_URL' => '(URL) The wp.me short URL.', |
||
14 | 'content' => '(HTML) <code>context</code> dependent.', |
||
15 | 'excerpt' => '(HTML) <code>context</code> dependent.', |
||
16 | 'slug' => '(string) The name (slug) for the post, used in URLs.', |
||
17 | 'guid' => '(string) The GUID for the post.', |
||
18 | 'status' => array( |
||
19 | 'publish' => 'The post is published.', |
||
20 | 'draft' => 'The post is saved as a draft.', |
||
21 | 'pending' => 'The post is pending editorial approval.', |
||
22 | 'private' => 'The post is published privately', |
||
23 | 'future' => 'The post is scheduled for future publishing.', |
||
24 | 'trash' => 'The post is in the trash.', |
||
25 | 'auto-draft' => 'The post is a placeholder for a new post.', |
||
26 | ), |
||
27 | 'sticky' => '(bool) Is the post sticky?', |
||
28 | 'password' => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.', |
||
29 | 'parent' => "(object>post_reference|false) A reference to the post's parent, if it has one.", |
||
30 | 'type' => "(string) The post's post_type. Post types besides post, page and revision need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.", |
||
31 | 'comments_open' => '(bool) Is the post open for comments?', |
||
32 | 'pings_open' => '(bool) Is the post open for pingbacks, trackbacks?', |
||
33 | 'likes_enabled' => "(bool) Is the post open to likes?", |
||
34 | 'sharing_enabled' => "(bool) Should sharing buttons show on this post?", |
||
35 | 'comment_count' => '(int) The number of comments for this post.', |
||
36 | 'like_count' => '(int) The number of likes for this post.', |
||
37 | 'i_like' => '(bool) Does the current user like this post?', |
||
38 | 'is_reblogged' => '(bool) Did the current user reblog this post?', |
||
39 | 'is_following' => '(bool) Is the current user following this blog?', |
||
40 | 'global_ID' => '(string) A unique WordPress.com-wide representation of a post.', |
||
41 | 'featured_image' => '(URL) The URL to the featured image for this post if it has one.', |
||
42 | 'post_thumbnail' => '(object>attachment) The attachment object for the featured image if it has one.', |
||
43 | 'format' => array(), // see constructor |
||
44 | 'geo' => '(object>geo|false)', |
||
45 | 'menu_order' => '(int) (Pages Only) The order pages should appear in.', |
||
46 | 'publicize_URLs' => '(array:URL) Array of Twitter and Facebook URLs published by this post.', |
||
47 | 'tags' => '(object:tag) Hash of tags (keyed by tag name) applied to the post.', |
||
48 | 'categories' => '(object:category) Hash of categories (keyed by category name) applied to the post.', |
||
49 | 'attachments' => '(object:attachment) Hash of post attachments (keyed by attachment ID).', |
||
50 | 'metadata' => '(array) Array of post metadata keys and values. All unprotected meta keys are available by default for read requests. Both unprotected and protected meta keys are available for authenticated requests with access. Protected meta keys can be made available with the <code>rest_api_allowed_public_metadata</code> filter.', |
||
51 | 'meta' => '(object) API result meta data', |
||
52 | 'current_user_can' => '(object) List of permissions. Note, deprecated in favor of `capabilities`', |
||
53 | 'capabilities' => '(object) List of post-specific permissions for the user; publish_post, edit_post, delete_post', |
||
54 | ); |
||
55 | |||
56 | // public $response_format =& $this->post_object_format; |
||
57 | |||
58 | View Code Duplication | function __construct( $args ) { |
|
67 | |||
68 | View Code Duplication | function is_metadata_public( $key ) { |
|
69 | if ( empty( $key ) ) |
||
70 | return false; |
||
71 | |||
72 | // Default whitelisted meta keys. |
||
73 | $whitelisted_meta = array( '_thumbnail_id' ); |
||
74 | |||
75 | /** |
||
76 | * Filters the meta keys accessible by the REST API. |
||
77 | * @see https://developer.wordpress.com/2013/04/26/custom-post-type-and-metadata-support-in-the-rest-api/ |
||
78 | * |
||
79 | * @module json-api |
||
80 | * |
||
81 | * @since 2.2.3 |
||
82 | * |
||
83 | * @param array $whitelisted_meta Array of metadata that is accessible by the REST API. |
||
84 | */ |
||
85 | if ( in_array( $key, apply_filters( 'rest_api_allowed_public_metadata', $whitelisted_meta ) ) ) |
||
86 | return true; |
||
87 | |||
88 | if ( 0 === strpos( $key, 'geo_' ) ) |
||
89 | return true; |
||
90 | |||
91 | if ( 0 === strpos( $key, '_wpas_' ) ) |
||
92 | return true; |
||
93 | |||
94 | return false; |
||
95 | } |
||
96 | |||
97 | function the_password_form() { |
||
100 | |||
101 | /** |
||
102 | * Get a post by a specified field and value |
||
103 | * |
||
104 | * @param string $field |
||
105 | * @param string $field_value |
||
106 | * @param string $context Post use context (e.g. 'display') |
||
107 | * @return array Post |
||
108 | **/ |
||
109 | function get_post_by( $field, $field_value, $context = 'display' ) { |
||
496 | |||
497 | // No Blog ID parameter. No Post ID parameter. Depends on globals. |
||
498 | // Expects setup_postdata() to already have been run |
||
499 | View Code Duplication | function get_the_post_content_for_display() { |
|
500 | global $pages, $page; |
||
501 | |||
502 | $old_pages = $pages; |
||
503 | $old_page = $page; |
||
504 | |||
505 | $content = join( "\n\n", $pages ); |
||
506 | $content = preg_replace( '/<!--more(.*?)?-->/', '', $content ); |
||
507 | $pages = array( $content ); |
||
508 | $page = 1; |
||
509 | |||
510 | ob_start(); |
||
511 | the_content(); |
||
512 | $return = ob_get_clean(); |
||
513 | |||
514 | $pages = $old_pages; |
||
515 | $page = $old_page; |
||
516 | |||
517 | return $return; |
||
518 | } |
||
519 | |||
520 | View Code Duplication | function get_blog_post( $blog_id, $post_id, $context = 'display' ) { |
|
521 | $blog_id = $this->api->get_blog_id( $blog_id ); |
||
522 | if ( !$blog_id || is_wp_error( $blog_id ) ) { |
||
523 | return $blog_id; |
||
524 | } |
||
525 | switch_to_blog( $blog_id ); |
||
526 | $post = $this->get_post_by( 'ID', $post_id, $context ); |
||
527 | restore_current_blog(); |
||
528 | return $post; |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Supporting featured media in post endpoints. Currently on for wpcom blogs |
||
533 | * since it's calling WPCOM_JSON_API_Read_Endpoint methods which presently |
||
534 | * rely on wpcom specific functionality. |
||
535 | * |
||
536 | * @param WP_Post $post |
||
537 | * @return object list of featured media |
||
538 | */ |
||
539 | public static function find_featured_media( &$post ) { |
||
540 | |||
541 | if ( class_exists( 'WPCOM_JSON_API_Read_Endpoint' ) ) { |
||
542 | return WPCOM_JSON_API_Read_Endpoint::find_featured_worthy_media( (array) $post ); |
||
543 | } else { |
||
544 | return (object) array(); |
||
545 | } |
||
546 | |||
547 | } |
||
548 | |||
549 | |||
550 | |||
551 | View Code Duplication | function win8_gallery_shortcode( $attr ) { |
|
552 | global $post; |
||
553 | |||
554 | static $instance = 0; |
||
555 | $instance++; |
||
556 | |||
557 | $output = ''; |
||
558 | |||
559 | // We're trusting author input, so let's at least make sure it looks like a valid orderby statement |
||
560 | if ( isset( $attr['orderby'] ) ) { |
||
561 | $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); |
||
562 | if ( !$attr['orderby'] ) |
||
563 | unset( $attr['orderby'] ); |
||
564 | } |
||
565 | |||
566 | extract( shortcode_atts( array( |
||
567 | 'order' => 'ASC', |
||
568 | 'orderby' => 'menu_order ID', |
||
569 | 'id' => $post->ID, |
||
570 | 'include' => '', |
||
571 | 'exclude' => '', |
||
572 | 'slideshow' => false |
||
573 | ), $attr, 'gallery' ) ); |
||
574 | |||
575 | // Custom image size and always use it |
||
576 | add_image_size( 'win8app-column', 480 ); |
||
577 | $size = 'win8app-column'; |
||
578 | |||
579 | $id = intval( $id ); |
||
580 | if ( 'RAND' === $order ) |
||
581 | $orderby = 'none'; |
||
582 | |||
583 | if ( !empty( $include ) ) { |
||
584 | $include = preg_replace( '/[^0-9,]+/', '', $include ); |
||
585 | $_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); |
||
586 | $attachments = array(); |
||
587 | foreach ( $_attachments as $key => $val ) { |
||
588 | $attachments[$val->ID] = $_attachments[$key]; |
||
589 | } |
||
590 | } elseif ( !empty( $exclude ) ) { |
||
591 | $exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); |
||
592 | $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); |
||
593 | } else { |
||
594 | $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); |
||
595 | } |
||
596 | |||
597 | if ( ! empty( $attachments ) ) { |
||
598 | foreach ( $attachments as $id => $attachment ) { |
||
599 | $link = isset( $attr['link'] ) && 'file' === $attr['link'] ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false ); |
||
600 | |||
601 | if ( $captiontag && trim($attachment->post_excerpt) ) { |
||
602 | $output .= "<div class='wp-caption aligncenter'>$link |
||
603 | <p class='wp-caption-text'>" . wptexturize($attachment->post_excerpt) . "</p> |
||
604 | </div>"; |
||
605 | } else { |
||
606 | $output .= $link . ' '; |
||
607 | } |
||
608 | } |
||
609 | } |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * Returns attachment object. |
||
614 | * |
||
615 | * @param $attachment attachment row |
||
616 | * |
||
617 | * @return (object) |
||
618 | */ |
||
619 | View Code Duplication | function get_attachment( $attachment ) { |
|
638 | |||
639 | /** |
||
640 | * Get post-specific user capabilities |
||
641 | * @param WP_Post $post post object |
||
642 | * @return array array of post-level permissions; 'publish_post', 'delete_post', 'edit_post' |
||
643 | */ |
||
644 | View Code Duplication | function get_current_user_capabilities( $post ) { |
|
651 | |||
652 | /** |
||
653 | * Get post ID by name |
||
654 | * |
||
655 | * Attempts to match name on post title and page path |
||
656 | * |
||
657 | * @param string $name |
||
658 | * |
||
659 | * @return int|object Post ID on success, WP_Error object on failure |
||
660 | **/ |
||
661 | View Code Duplication | protected function get_post_id_by_name( $name ) { |
|
684 | } |
||
685 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.