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:
| 1 | <?php |
||
| 5 | class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { |
||
| 6 | |||
| 7 | public function name() { |
||
| 10 | |||
| 11 | public function get_object_by_id( $object_type, $id ) { |
||
| 18 | |||
| 19 | public function set_defaults() { |
||
| 20 | } |
||
| 21 | |||
| 22 | public function init_listeners( $callable ) { |
||
| 23 | add_action( 'wp_insert_post', $callable, 10, 3 ); |
||
| 24 | add_action( 'deleted_post', $callable, 10 ); |
||
| 25 | add_action( 'jetpack_publicize_post', $callable ); |
||
| 26 | add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) ); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function init_full_sync_listeners( $callable ) { |
||
| 30 | add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta |
||
| 31 | } |
||
| 32 | |||
| 33 | public function init_before_send() { |
||
| 39 | |||
| 40 | public function enqueue_full_sync_actions( $config ) { |
||
| 41 | global $wpdb; |
||
| 42 | |||
| 43 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ) ); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function estimate_full_sync_actions( $config ) { |
||
| 47 | global $wpdb; |
||
| 48 | |||
| 49 | $query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config ); |
||
| 50 | $count = $wpdb->get_var( $query ); |
||
| 51 | |||
| 52 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
||
| 53 | } |
||
| 54 | |||
| 55 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 56 | $where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql(); |
||
| 57 | |||
| 58 | // config is a list of post IDs to sync |
||
| 59 | if ( is_array( $config ) ) { |
||
| 60 | $where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
||
|
|
|||
| 61 | } |
||
| 62 | |||
| 63 | return $where_sql; |
||
| 64 | } |
||
| 65 | |||
| 66 | function get_full_sync_actions() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Process content before send |
||
| 72 | */ |
||
| 73 | |||
| 74 | function expand_wp_insert_post( $args ) { |
||
| 77 | |||
| 78 | function filter_blacklisted_post_types( $args ) { |
||
| 79 | $post = $args[1]; |
||
| 80 | |||
| 81 | if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | return $args; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Expands wp_insert_post to include filtered content |
||
| 89 | function filter_post_content_and_add_links( $post_object ) { |
||
| 90 | global $post; |
||
| 91 | $post = $post_object; |
||
| 92 | /** |
||
| 93 | * Filters whether to prevent sending post data to .com |
||
| 94 | * |
||
| 95 | * Passing true to the filter will prevent the post data from being sent |
||
| 96 | * to the WordPress.com. |
||
| 97 | * Instead we pass data that will still enable us to do a checksum against the |
||
| 98 | * Jetpacks data but will prevent us from displaying the data on in the API as well as |
||
| 99 | * other services. |
||
| 100 | * @since 4.2.0 |
||
| 101 | * |
||
| 102 | * @param boolean false prevent post data from being synced to WordPress.com |
||
| 103 | * @param mixed $post WP_POST object |
||
| 104 | */ |
||
| 105 | if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
||
| 106 | // We only send the bare necessary object to be able to create a checksum. |
||
| 107 | $blocked_post = new stdClass(); |
||
| 108 | $blocked_post->ID = $post->ID; |
||
| 109 | $blocked_post->post_modified = $post->post_modified; |
||
| 110 | $blocked_post->post_modified_gmt = $post->post_modified_gmt; |
||
| 111 | $blocked_post->post_status = 'jetpack_sync_blocked'; |
||
| 112 | |||
| 113 | return $blocked_post; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ( 0 < strlen( $post->post_password ) ) { |
||
| 117 | $post->post_password = 'auto-' . wp_generate_password( 10, false ); |
||
| 118 | } |
||
| 119 | /** This filter is already documented in core. wp-includes/post-template.php */ |
||
| 120 | $post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
||
| 121 | $post->post_excerpt_filtered = apply_filters( 'the_content', $post->post_excerpt ); |
||
| 122 | $post->permalink = get_permalink( $post->ID ); |
||
| 123 | $post->shortlink = wp_get_shortlink( $post->ID ); |
||
| 124 | $post->dont_email_post_to_subs = Jetpack::is_module_active( 'subscriptions' ) ? |
||
| 125 | get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) : |
||
| 126 | true; // Don't email subscription if the subscription module is not active. |
||
| 127 | |||
| 128 | return $post; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function expand_post_ids( $args ) { |
||
| 143 | } |
||
| 144 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.