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 |
||
| 10 | class WPCOM_REST_API_V2_Endpoint_Mailchimp extends WP_REST_Controller { |
||
| 11 | View Code Duplication | public function __construct() { |
|
| 18 | |||
| 19 | /** |
||
| 20 | * Called automatically on `rest_api_init()`. |
||
| 21 | */ |
||
| 22 | View Code Duplication | public function register_routes() { |
|
| 23 | register_rest_route( |
||
| 24 | $this->namespace, |
||
| 25 | $this->rest_base, |
||
| 26 | array( |
||
| 27 | array( |
||
| 28 | 'methods' => WP_REST_Server::READABLE, |
||
| 29 | 'callback' => array( $this, 'get_mailchimp_status' ), |
||
| 30 | ), |
||
| 31 | ) |
||
| 32 | ); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Check if MailChimp is set up properly. |
||
| 37 | * |
||
| 38 | * @return bool |
||
| 39 | */ |
||
| 40 | View Code Duplication | private function is_connected() { |
|
| 41 | $option = get_option( 'jetpack_mailchimp' ); |
||
| 42 | if ( ! $option ) { |
||
| 43 | return false; |
||
| 44 | } |
||
| 45 | $data = json_decode( $option, true ); |
||
| 46 | if ( ! $data ) { |
||
| 47 | return false; |
||
| 48 | } |
||
| 49 | return isset( $data['follower_list_id'], $data['keyring_id'] ); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the status of current blog's Mailchimp connection |
||
| 54 | * |
||
| 55 | * @return mixed |
||
| 56 | * code:string (connected|unconnected), |
||
| 57 | * connect_url:string |
||
| 58 | * site_id:int |
||
| 59 | */ |
||
| 60 | public function get_mailchimp_status() { |
||
| 77 | } |
||
| 78 | |||
| 79 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Mailchimp' ); |
||
| 80 |