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 |
||
| 3 | class Jetpack_Sync_Module_Comments extends Jetpack_Sync_Module { |
||
| 4 | |||
| 5 | public function name() { |
||
| 8 | |||
| 9 | public function init_listeners( $callable ) { |
||
| 28 | |||
| 29 | public function init_before_send() { |
||
| 30 | add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) ); |
||
| 31 | |||
| 32 | View Code Duplication | foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { |
|
| 33 | foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
||
| 34 | $comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
||
| 35 | add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array( |
||
| 36 | $this, |
||
| 37 | 'expand_wp_insert_comment', |
||
| 38 | ) ); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | // full sync |
||
| 43 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function enqueue_full_sync_actions() { |
||
| 47 | global $wpdb; |
||
| 48 | |||
| 49 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', null ); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function get_full_sync_actions() { |
||
| 53 | return array( 'jetpack_full_sync_comments' ); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function count_full_sync_actions( $action_names ) { |
||
| 57 | return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | function expand_wp_comment_status_change( $args ) { |
||
| 63 | |||
| 64 | function expand_wp_insert_comment( $args ) { |
||
| 67 | |||
| 68 | function filter_comment( $comment ) { |
||
| 69 | /** |
||
| 70 | * Filters whether to prevent sending comment data to .com |
||
| 71 | * |
||
| 72 | * Passing true to the filter will prevent the comment data from being sent |
||
| 73 | * to the WordPress.com. |
||
| 74 | * Instead we pass data that will still enable us to do a checksum against the |
||
| 75 | * Jetpacks data but will prevent us from displaying the data on in the API as well as |
||
| 76 | * other services. |
||
| 77 | * @since 4.2.0 |
||
| 78 | * |
||
| 79 | * @param boolean false prevent post data from bing synced to WordPress.com |
||
| 80 | * @param mixed $comment WP_COMMENT object |
||
| 81 | */ |
||
| 82 | if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) { |
||
| 83 | $blocked_comment = new stdClass(); |
||
| 84 | $blocked_comment->comment_ID = $comment->comment_ID; |
||
| 85 | $blocked_comment->comment_date = $comment->comment_date; |
||
| 86 | $blocked_comment->comment_date_gmt = $comment->comment_date_gmt; |
||
| 87 | $blocked_comment->comment_approved = 'jetpack_sync_blocked'; |
||
| 88 | |||
| 89 | return $blocked_comment; |
||
| 90 | } |
||
| 91 | |||
| 92 | return $comment; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function expand_comment_ids( $args ) { |
||
| 107 | } |
||
| 108 |