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_Base 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_Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | abstract class Publicize_Base { |
||
4 | |||
5 | /** |
||
6 | * Services that are currently connected to the given user |
||
7 | * through publicize. |
||
8 | */ |
||
9 | public $connected_services = array(); |
||
10 | |||
11 | /** |
||
12 | * Services that are supported by publicize. They don't |
||
13 | * necessarily need to be connected to the current user. |
||
14 | */ |
||
15 | public $services; |
||
16 | |||
17 | /** |
||
18 | * key names for post meta |
||
19 | */ |
||
20 | public $ADMIN_PAGE = 'wpas'; |
||
21 | public $POST_MESS = '_wpas_mess'; |
||
22 | public $POST_SKIP = '_wpas_skip_'; // connection id appended to indicate that a connection should NOT be publicized to |
||
23 | public $POST_DONE = '_wpas_done_'; // connection id appended to indicate a connection has already been publicized to |
||
24 | public $USER_AUTH = 'wpas_authorize'; |
||
25 | public $USER_OPT = 'wpas_'; |
||
26 | public $PENDING = '_publicize_pending'; // ready for Publicize to do its thing |
||
27 | public $POST_SERVICE_DONE = '_publicize_done_external'; // array of external ids where we've Publicized |
||
28 | |||
29 | /** |
||
30 | * default pieces of the message used in constructing the |
||
31 | * content pushed out to other social networks |
||
32 | */ |
||
33 | |||
34 | public $default_prefix = ''; |
||
35 | public $default_message = '%title%'; |
||
36 | public $default_suffix = ' '; |
||
37 | |||
38 | /** |
||
39 | * What WP capability is require to create/delete global connections? |
||
40 | * All users with this cap can un-globalize all other global connections, and globalize any of their own |
||
41 | * Globalized connections cannot be unselected by users without this capability when publishing |
||
42 | */ |
||
43 | public $GLOBAL_CAP = 'edit_others_posts'; |
||
44 | |||
45 | /** |
||
46 | * Sets up the basics of Publicize |
||
47 | */ |
||
48 | function __construct() { |
||
49 | $this->default_message = Publicize_Util::build_sprintf( array( |
||
50 | /** |
||
51 | * Filter the default Publicize message. |
||
52 | * |
||
53 | * @module publicize |
||
54 | * |
||
55 | * @since 2.0.0 |
||
56 | * |
||
57 | * @param string $this->default_message Publicize's default message. Default is the post title. |
||
58 | */ |
||
59 | apply_filters( 'wpas_default_message', $this->default_message ), |
||
60 | 'title', |
||
61 | 'url', |
||
62 | ) ); |
||
63 | |||
64 | $this->default_prefix = Publicize_Util::build_sprintf( array( |
||
65 | /** |
||
66 | * Filter the message prepended to the Publicize custom message. |
||
67 | * |
||
68 | * @module publicize |
||
69 | * |
||
70 | * @since 2.0.0 |
||
71 | * |
||
72 | * @param string $this->default_prefix String prepended to the Publicize custom message. |
||
73 | */ |
||
74 | apply_filters( 'wpas_default_prefix', $this->default_prefix ), |
||
75 | 'url', |
||
76 | ) ); |
||
77 | |||
78 | $this->default_suffix = Publicize_Util::build_sprintf( array( |
||
79 | /** |
||
80 | * Filter the message appended to the Publicize custom message. |
||
81 | * |
||
82 | * @module publicize |
||
83 | * |
||
84 | * @since 2.0.0 |
||
85 | * |
||
86 | * @param string $this->default_suffix String appended to the Publicize custom message. |
||
87 | */ |
||
88 | apply_filters( 'wpas_default_suffix', $this->default_suffix ), |
||
89 | 'url', |
||
90 | ) ); |
||
91 | |||
92 | /** |
||
93 | * Filter the capability to change global Publicize connection options. |
||
94 | * |
||
95 | * All users with this cap can un-globalize all other global connections, and globalize any of their own |
||
96 | * Globalized connections cannot be unselected by users without this capability when publishing. |
||
97 | * |
||
98 | * @module publicize |
||
99 | * |
||
100 | * @since 2.2.1 |
||
101 | * |
||
102 | * @param string $this->GLOBAL_CAP default capability in control of global Publicize connection options. Default to edit_others_posts. |
||
103 | */ |
||
104 | $this->GLOBAL_CAP = apply_filters( 'jetpack_publicize_global_connections_cap', $this->GLOBAL_CAP ); |
||
105 | |||
106 | // stage 1 and 2 of 3-stage Publicize. Flag for Publicize on creation, save meta, |
||
107 | // then check meta and publicize based on that. stage 3 implemented on wpcom |
||
108 | add_action( 'transition_post_status', array( $this, 'flag_post_for_publicize' ), 10, 3 ); |
||
109 | add_action( 'save_post', array( &$this, 'save_meta' ), 20, 2 ); |
||
110 | |||
111 | // Connection test callback |
||
112 | add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Functions to be implemented by the extended class (publicize-wpcom or publicize-jetpack) |
||
117 | */ |
||
118 | abstract function get_connection_id( $connection ); |
||
128 | |||
129 | /** |
||
130 | * Shared Functions |
||
131 | */ |
||
132 | |||
133 | /** |
||
134 | * Returns an external URL to the connection's profile |
||
135 | */ |
||
136 | function get_profile_link( $service_name, $c ) { |
||
137 | $cmeta = $this->get_connection_meta( $c ); |
||
138 | |||
139 | if ( isset( $cmeta['connection_data']['meta']['link'] ) ) { |
||
140 | if ( 'facebook' == $service_name && 0 === strpos( parse_url( $cmeta['connection_data']['meta']['link'], PHP_URL_PATH ), '/app_scoped_user_id/' ) ) { |
||
141 | // App-scoped Facebook user IDs are not usable profile links |
||
142 | return false; |
||
143 | } |
||
144 | |||
145 | return $cmeta['connection_data']['meta']['link']; |
||
146 | View Code Duplication | } elseif ( 'facebook' == $service_name && isset( $cmeta['connection_data']['meta']['facebook_page'] ) ) { |
|
147 | return 'https://facebook.com/' . $cmeta['connection_data']['meta']['facebook_page']; |
||
148 | } elseif ( 'tumblr' == $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) { |
||
149 | return 'http://' . $cmeta['connection_data']['meta']['tumblr_base_hostname']; |
||
150 | } elseif ( 'twitter' == $service_name ) { |
||
151 | return 'https://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@' |
||
152 | View Code Duplication | } elseif ( 'google_plus' == $service_name && isset( $cmeta['connection_data']['meta']['google_plus_page'] ) ) { |
|
153 | return 'https://plus.google.com/' . $cmeta['connection_data']['meta']['google_plus_page']; |
||
154 | } elseif ( 'google_plus' == $service_name ) { |
||
155 | return 'https://plus.google.com/' . $cmeta['external_id']; |
||
156 | } else if ( 'linkedin' == $service_name ) { |
||
157 | if ( !isset( $cmeta['connection_data']['meta']['profile_url'] ) ) { |
||
158 | return false; |
||
159 | } |
||
160 | |||
161 | $profile_url_query = parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY ); |
||
162 | wp_parse_str( $profile_url_query, $profile_url_query_args ); |
||
|
|||
163 | if ( isset( $profile_url_query_args['key'] ) ) { |
||
164 | $id = $profile_url_query_args['key']; |
||
165 | } elseif ( isset( $profile_url_query_args['id'] ) ) { |
||
166 | $id = $profile_url_query_args['id']; |
||
167 | } else { |
||
168 | return false; |
||
169 | } |
||
170 | |||
171 | return esc_url_raw( add_query_arg( 'id', urlencode( $id ), 'http://www.linkedin.com/profile/view' ) ); |
||
172 | } else { |
||
173 | return false; // no fallback. we just won't link it |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Returns a display name for the connection |
||
179 | */ |
||
180 | function get_display_name( $service_name, $c ) { |
||
181 | $cmeta = $this->get_connection_meta( $c ); |
||
182 | |||
183 | if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) { |
||
184 | return $cmeta['connection_data']['meta']['display_name']; |
||
185 | View Code Duplication | } elseif ( $service_name == 'tumblr' && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) { |
|
186 | return $cmeta['connection_data']['meta']['tumblr_base_hostname']; |
||
187 | } elseif ( $service_name == 'twitter' ) { |
||
188 | return $cmeta['external_display']; |
||
189 | } else { |
||
190 | $connection_display = $cmeta['external_display']; |
||
191 | if ( empty( $connection_display ) ) |
||
192 | $connection_display = $cmeta['external_name']; |
||
193 | return $connection_display; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | public static function get_service_label( $service_name ) { |
||
198 | switch ( $service_name ) { |
||
199 | case 'linkedin': |
||
200 | return 'LinkedIn'; |
||
201 | break; |
||
202 | case 'google_plus': |
||
203 | return 'Google+'; |
||
204 | break; |
||
205 | case 'twitter': |
||
206 | case 'facebook': |
||
207 | case 'tumblr': |
||
208 | default: |
||
209 | return ucfirst( $service_name ); |
||
210 | break; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | function show_options_popup( $service_name, $c ) { |
||
215 | $cmeta = $this->get_connection_meta( $c ); |
||
216 | |||
217 | // always show if no selection has been made for facebook |
||
218 | if ( 'facebook' == $service_name && empty( $cmeta['connection_data']['meta']['facebook_profile'] ) && empty( $cmeta['connection_data']['meta']['facebook_page'] ) ) |
||
219 | return true; |
||
220 | |||
221 | // always show if no selection has been made for tumblr |
||
222 | if ( 'tumblr' == $service_name && empty ( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) |
||
223 | return true; |
||
224 | |||
225 | // if we have the specific connection info.. |
||
226 | if ( isset( $_GET['id'] ) ) { |
||
227 | if ( $cmeta['connection_data']['id'] == $_GET['id'] ) |
||
228 | return true; |
||
229 | } else { |
||
230 | // otherwise, just show if this is the completed step / first load |
||
231 | if ( !empty( $_GET['action'] ) && 'completed' == $_GET['action'] && !empty( $_GET['service'] ) && $service_name == $_GET['service'] && ! in_array( $_GET['service'], array( 'facebook', 'tumblr' ) ) ) |
||
232 | return true; |
||
233 | } |
||
234 | |||
235 | return false; |
||
236 | } |
||
237 | |||
238 | function user_id() { |
||
242 | |||
243 | function blog_id() { |
||
246 | |||
247 | /** |
||
248 | * Returns true if a user has a connection to a particular service, false otherwise |
||
249 | */ |
||
250 | function is_enabled( $service, $_blog_id = false, $_user_id = false ) { |
||
251 | if ( !$_blog_id ) |
||
252 | $_blog_id = $this->blog_id(); |
||
253 | |||
254 | if ( !$_user_id ) |
||
255 | $_user_id = $this->user_id(); |
||
256 | |||
257 | $connections = $this->get_connections( $service, $_blog_id, $_user_id ); |
||
258 | return ( is_array( $connections ) && count( $connections ) > 0 ? true : false ); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
263 | * can be picked up later by @see ::publicize_post() |
||
264 | */ |
||
265 | function save_meta( $post_id, $post ) { |
||
414 | |||
415 | /** |
||
416 | * Is a given post type Publicize-able? |
||
417 | * |
||
418 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
419 | * the publicize_post_types array filter. |
||
420 | * |
||
421 | * @param string $post_type The post type to check. |
||
422 | * $return bool True if the post type can be Publicized. |
||
423 | */ |
||
424 | function post_type_is_publicizeable( $post_type ) { |
||
430 | |||
431 | /** |
||
432 | * Runs tests on all the connections and returns the results to the caller |
||
433 | */ |
||
434 | function test_publicize_conns() { |
||
477 | } |
||
478 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.