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 Publicize 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 Publicize, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Publicize extends Publicize_Base { |
||
6 | |||
7 | function __construct() { |
||
8 | parent::__construct(); |
||
9 | |||
10 | add_filter( 'jetpack_xmlrpc_methods', array( $this, 'register_update_publicize_connections_xmlrpc_method' ) ); |
||
11 | |||
12 | // moved to Keyring module |
||
13 | //add_action( 'load-settings_page_sharing', array( $this, 'admin_page_load' ), 9 ); |
||
14 | |||
15 | add_action( 'wp_ajax_publicize_tumblr_options_page', array( $this, 'options_page_tumblr' ) ); |
||
16 | add_action( 'wp_ajax_publicize_facebook_options_page', array( $this, 'options_page_facebook' ) ); |
||
17 | add_action( 'wp_ajax_publicize_twitter_options_page', array( $this, 'options_page_twitter' ) ); |
||
18 | add_action( 'wp_ajax_publicize_linkedin_options_page', array( $this, 'options_page_linkedin' ) ); |
||
19 | add_action( 'wp_ajax_publicize_path_options_page', array( $this, 'options_page_path' ) ); |
||
20 | add_action( 'wp_ajax_publicize_google_plus_options_page', array( $this, 'options_page_google_plus' ) ); |
||
21 | |||
22 | add_action( 'wp_ajax_publicize_tumblr_options_save', array( $this, 'options_save_tumblr' ) ); |
||
23 | add_action( 'wp_ajax_publicize_facebook_options_save', array( $this, 'options_save_facebook' ) ); |
||
24 | add_action( 'wp_ajax_publicize_twitter_options_save', array( $this, 'options_save_twitter' ) ); |
||
25 | add_action( 'wp_ajax_publicize_linkedin_options_save', array( $this, 'options_save_linkedin' ) ); |
||
26 | add_action( 'wp_ajax_publicize_path_options_save', array( $this, 'options_save_path' ) ); |
||
27 | add_action( 'wp_ajax_publicize_google_plus_options_save', array( $this, 'options_save_google_plus' ) ); |
||
28 | |||
29 | add_action( 'load-settings_page_sharing', array( $this, 'force_user_connection' ) ); |
||
30 | |||
31 | add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 4 ); |
||
32 | |||
33 | add_filter( 'jetpack_published_post_flags', array( $this, 'set_post_flags' ), 10, 2 ); |
||
34 | |||
35 | add_action( 'wp_insert_post', array( $this, 'save_publicized' ), 11, 3 ); |
||
36 | |||
37 | add_filter( 'jetpack_twitter_cards_site_tag', array( $this, 'enhaced_twitter_cards_site_tag' ) ); |
||
38 | |||
39 | add_action( 'publicize_save_meta', array( $this, 'save_publicized_twitter_account' ), 10, 4 ); |
||
40 | add_action( 'publicize_save_meta', array( $this, 'save_publicized_facebook_account' ), 10, 4 ); |
||
41 | |||
42 | add_filter( 'jetpack_sharing_twitter_via', array( $this, 'get_publicized_twitter_account' ), 10, 2 ); |
||
43 | |||
44 | include_once( JETPACK__PLUGIN_DIR . 'modules/publicize/enhanced-open-graph.php' ); |
||
45 | } |
||
46 | |||
47 | function force_user_connection() { |
||
48 | global $current_user; |
||
49 | $user_token = Jetpack_Data::get_access_token( $current_user->ID ); |
||
50 | $is_user_connected = $user_token && ! is_wp_error( $user_token ); |
||
51 | |||
52 | // If the user is already connected via Jetpack, then we're good |
||
53 | if ( $is_user_connected ) { |
||
54 | return; |
||
55 | } |
||
56 | |||
57 | // If they're not connected, then remove the Publicize UI and tell them they need to connect first |
||
58 | global $publicize_ui; |
||
59 | remove_action( 'pre_admin_screen_sharing', array( $publicize_ui, 'admin_page' ) ); |
||
60 | |||
61 | // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. |
||
62 | // Jetpack::init()->admin_styles(); |
||
63 | add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page_warning' ), 1 ); |
||
64 | } |
||
65 | |||
66 | function admin_page_warning() { |
||
67 | $jetpack = Jetpack::init(); |
||
68 | $blog_name = get_bloginfo( 'blogname' ); |
||
69 | if ( empty( $blog_name ) ) { |
||
70 | $blog_name = home_url( '/' ); |
||
71 | } |
||
72 | |||
73 | ?> |
||
74 | <div id="message" class="updated jetpack-message jp-connect"> |
||
75 | <div class="jetpack-wrap-container"> |
||
76 | <div class="jetpack-text-container"> |
||
77 | <p><?php printf( |
||
78 | /* translators: %s is the name of the blog */ |
||
79 | esc_html( wptexturize( __( "To use Publicize, you'll need to link your %s account to your WordPress.com account using the link below.", 'jetpack' ) ) ), |
||
80 | '<strong>' . esc_html( $blog_name ) . '</strong>' |
||
81 | ); ?></p> |
||
82 | <p><?php echo esc_html( wptexturize( __( "If you don't have a WordPress.com account yet, you can sign up for free in just a few seconds.", 'jetpack' ) ) ); ?></p> |
||
83 | </div> |
||
84 | <div class="jetpack-install-container"> |
||
85 | <p class="submit"><a |
||
86 | href="<?php echo $jetpack->build_connect_url( false, menu_page_url( 'sharing', false ) ); ?>" |
||
87 | class="button-connector" |
||
88 | id="wpcom-connect"><?php esc_html_e( 'Link account with WordPress.com', 'jetpack' ); ?></a> |
||
89 | </p> |
||
90 | <p class="jetpack-install-blurb"> |
||
91 | <?php jetpack_render_tos_blurb(); ?> |
||
92 | </p> |
||
93 | </div> |
||
94 | </div> |
||
95 | </div> |
||
96 | <?php |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Remove a Publicize connection |
||
101 | */ |
||
102 | View Code Duplication | function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ) { |
|
103 | Jetpack::load_xml_rpc_client(); |
||
104 | $xml = new Jetpack_IXR_Client(); |
||
105 | $xml->query( 'jetpack.deletePublicizeConnection', $connection_id ); |
||
106 | |||
107 | if ( ! $xml->isError() ) { |
||
108 | Jetpack_Options::update_option( 'publicize_connections', $xml->getResponse() ); |
||
109 | } else { |
||
110 | return false; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | function receive_updated_publicize_connections( $publicize_connections ) { |
||
115 | Jetpack_Options::update_option( 'publicize_connections', $publicize_connections ); |
||
116 | |||
117 | return true; |
||
118 | } |
||
119 | |||
120 | function register_update_publicize_connections_xmlrpc_method( $methods ) { |
||
121 | return array_merge( $methods, array( |
||
122 | 'jetpack.updatePublicizeConnections' => array( $this, 'receive_updated_publicize_connections' ), |
||
123 | ) ); |
||
124 | } |
||
125 | |||
126 | function get_all_connections() { |
||
127 | return Jetpack_Options::get_option( 'publicize_connections' ); |
||
128 | } |
||
129 | |||
130 | function get_connections( $service_name, $_blog_id = false, $_user_id = false ) { |
||
131 | $connections = $this->get_all_connections(); |
||
132 | $connections_to_return = array(); |
||
133 | if ( ! empty( $connections ) && is_array( $connections ) ) { |
||
134 | if ( ! empty( $connections[ $service_name ] ) ) { |
||
135 | foreach ( $connections[ $service_name ] as $id => $connection ) { |
||
136 | if ( 0 == $connection['connection_data']['user_id'] || $this->user_id() == $connection['connection_data']['user_id'] ) { |
||
137 | $connections_to_return[ $id ] = $connection; |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | return $connections_to_return; |
||
143 | } |
||
144 | |||
145 | return false; |
||
146 | } |
||
147 | |||
148 | function get_all_connections_for_user() { |
||
168 | |||
169 | function get_connection_id( $connection ) { |
||
172 | |||
173 | function get_connection_meta( $connection ) { |
||
174 | $connection['user_id'] = $connection['connection_data']['user_id']; // Allows for shared connections |
||
175 | return $connection; |
||
176 | } |
||
177 | |||
178 | // moved to Keyring module |
||
179 | // function admin_age_load() { |
||
180 | |||
181 | // moved to Keyring module |
||
182 | // function display_connection_error() { |
||
183 | |||
184 | |||
185 | function display_disconnected() { |
||
190 | |||
191 | function globalization() { |
||
209 | |||
210 | |||
211 | // moved to Keyring module |
||
212 | //function api_url( $service = false, $params = array() ) { |
||
213 | |||
214 | function connect_url( $service_name ) { |
||
217 | |||
218 | function refresh_url( $service_name ) { |
||
221 | |||
222 | function disconnect_url( $service_name, $id ) { |
||
225 | |||
226 | /** |
||
227 | * Get social networks, either all available or only those that the site is connected to. |
||
228 | * |
||
229 | * @since 2.0 |
||
230 | * |
||
231 | * @param string $filter Select the list of services that will be returned. Defaults to 'all', accepts 'connected'. |
||
232 | * |
||
233 | * @return array List of social networks. |
||
234 | */ |
||
235 | function get_services( $filter = 'all' ) { |
||
258 | |||
259 | function get_connection( $service, $id, $_blog_id = false, $_user_id = false ) { |
||
262 | |||
263 | function flag_post_for_publicize( $new_status, $old_status, $post ) { |
||
288 | |||
289 | function test_connection( $service_name, $connection ) { |
||
321 | |||
322 | /** |
||
323 | * Save a flag locally to indicate that this post has already been Publicized via the selected |
||
324 | * connections. |
||
325 | */ |
||
326 | function save_publicized( $post_ID, $post = null, $update = null ) { |
||
347 | |||
348 | function set_post_flags( $flags, $post ) { |
||
368 | |||
369 | /** |
||
370 | * Options Code |
||
371 | */ |
||
372 | |||
373 | function options_page_facebook() { |
||
464 | |||
465 | function options_save_facebook() { |
||
487 | |||
488 | function options_page_tumblr() { |
||
561 | |||
562 | function get_basehostname( $url ) { |
||
565 | |||
566 | function options_save_tumblr() { |
||
574 | |||
575 | function set_remote_publicize_options( $id, $options ) { |
||
586 | |||
587 | function options_page_twitter() { |
||
590 | |||
591 | function options_page_linkedin() { |
||
594 | |||
595 | function options_page_path() { |
||
598 | |||
599 | function options_page_google_plus() { |
||
602 | |||
603 | function options_save_twitter() { |
||
606 | |||
607 | function options_save_linkedin() { |
||
610 | |||
611 | function options_save_path() { |
||
614 | |||
615 | function options_save_google_plus() { |
||
618 | |||
619 | function options_save_other( $service_name ) { |
||
624 | |||
625 | /** |
||
626 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
627 | * false if a post has already been published. |
||
628 | */ |
||
629 | function publicize_checkbox_default( $checked, $post_id, $name, $connection ) { |
||
636 | |||
637 | /** |
||
638 | * If there's only one shared connection to Twitter set it as twitter:site tag. |
||
639 | */ |
||
640 | function enhaced_twitter_cards_site_tag( $tag ) { |
||
659 | |||
660 | function save_publicized_twitter_account( $submit_post, $post_id, $service_name, $connection ) { |
||
669 | |||
670 | function get_publicized_twitter_account( $account, $post_id ) { |
||
681 | |||
682 | /** |
||
683 | * Save the Publicized Facebook account when publishing a post |
||
684 | * Use only Personal accounts, not Facebook Pages |
||
685 | */ |
||
686 | function save_publicized_facebook_account( $submit_post, $post_id, $service_name, $connection ) { |
||
699 | } |
||
700 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.