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 = self::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 = self::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 = self::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 | // Default checkbox state for each Connection |
||
112 | add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 4 ); |
||
113 | |||
114 | // Alter the "Post Publish" admin notice to mention the Connections we Publicized to. |
||
115 | add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 20, 1 ); |
||
116 | |||
117 | // Connection test callback |
||
118 | add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) ); |
||
119 | } |
||
120 | |||
121 | /* |
||
122 | * Services: Facebook, Twitter, etc. |
||
123 | */ |
||
124 | |||
125 | /** |
||
126 | * Get services for the given blog and user. |
||
127 | * |
||
128 | * Can return all available services or just the ones with an active connection. |
||
129 | * |
||
130 | * @param string $filter |
||
131 | * 'all' (default) - Get all services available for connecting |
||
132 | * 'connected' - Get all services currently connected |
||
133 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
134 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
135 | * @return array |
||
136 | */ |
||
137 | abstract function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
||
138 | |||
139 | /** |
||
140 | * Does the given user have a connection to the service on the given blog? |
||
141 | * |
||
142 | * @param string $service_name 'facebook', 'twitter', etc. |
||
143 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
144 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
145 | * @return bool |
||
146 | */ |
||
147 | function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
||
148 | if ( !$_blog_id ) |
||
|
|||
149 | $_blog_id = $this->blog_id(); |
||
150 | |||
151 | if ( !$_user_id ) |
||
152 | $_user_id = $this->user_id(); |
||
153 | |||
154 | $connections = $this->get_connections( $service_name, $_blog_id, $_user_id ); |
||
155 | return ( is_array( $connections ) && count( $connections ) > 0 ? true : false ); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Generates a connection URL. |
||
160 | * |
||
161 | * This is the URL, which, when visited by the user, starts the authentication |
||
162 | * process required to forge a connection. |
||
163 | * |
||
164 | * @param string $service_name 'facebook', 'twitter', etc. |
||
165 | * @return string |
||
166 | */ |
||
167 | abstract function connect_url( $service_name ); |
||
168 | |||
169 | /** |
||
170 | * Generates a Connection refresh URL. |
||
171 | * |
||
172 | * This is the URL, which, when visited by the user, re-authenticates their |
||
173 | * connection to the service. |
||
174 | * |
||
175 | * @param string $service_name 'facebook', 'twitter', etc. |
||
176 | * @return string |
||
177 | */ |
||
178 | abstract function refresh_url( $service_name ); |
||
179 | |||
180 | /** |
||
181 | * Generates a disconnection URL. |
||
182 | * |
||
183 | * This is the URL, which, when visited by the user, breaks their connection |
||
184 | * with the service. |
||
185 | * |
||
186 | * @param string $service_name 'facebook', 'twitter', etc. |
||
187 | * @param string $connection_id Connection ID |
||
188 | * @return string |
||
189 | */ |
||
190 | abstract function disconnect_url( $service_name, $connection_id ); |
||
191 | |||
192 | /** |
||
193 | * Returns a display name for the Service |
||
194 | * |
||
195 | * @param string $service_name 'facebook', 'twitter', etc. |
||
196 | * @return string |
||
197 | */ |
||
198 | public static function get_service_label( $service_name ) { |
||
214 | |||
215 | /* |
||
216 | * Connections: For each Service, there can be multiple connections |
||
217 | * for a given user. For example, one user could be connected to Twitter |
||
218 | * as both @jetpack and as @wordpressdotcom |
||
219 | * |
||
220 | * For historical reasons, Connections are represented as an object |
||
221 | * on WordPress.com and as an array in Jetpack. |
||
222 | */ |
||
223 | |||
224 | /** |
||
225 | * Get the active Connections of a Service |
||
226 | * |
||
227 | * @param string $service_name 'facebook', 'twitter', etc. |
||
228 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
229 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
230 | * @return false|object[]|array[] false if no connections exist |
||
231 | */ |
||
232 | abstract function get_connections( $service_name, $_blog_id = false, $_user_id = false ); |
||
233 | |||
234 | /** |
||
235 | * Get a single Connection of a Service |
||
236 | * |
||
237 | * @param string $service_name 'facebook', 'twitter', etc. |
||
238 | * @param string $connection_id Connection ID |
||
239 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
240 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
241 | * @return false|object[]|array[] false if no connections exist |
||
242 | */ |
||
243 | abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
||
244 | |||
245 | /** |
||
246 | * Get the Connection ID. |
||
247 | * |
||
248 | * Note that this is different than the Connection's uniqueid. |
||
249 | * |
||
250 | * Via a quirk of history, ID is globally unique and unique_id |
||
251 | * is only unique per site. |
||
252 | * |
||
253 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
254 | * @return string |
||
255 | */ |
||
256 | abstract function get_connection_id( $connection ); |
||
257 | |||
258 | /** |
||
259 | * Get the Connection unique_id |
||
260 | * |
||
261 | * Note that this is different than the Connections ID. |
||
262 | * |
||
263 | * Via a quirk of history, ID is globally unique and unique_id |
||
264 | * is only unique per site. |
||
265 | * |
||
266 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
267 | * @return string |
||
268 | */ |
||
269 | abstract function get_connection_unique_id( $connection ); |
||
270 | |||
271 | /** |
||
272 | * Get the Connection's Meta data |
||
273 | * |
||
274 | * @param object|array Connection |
||
275 | * @return array Connection Meta |
||
276 | */ |
||
277 | abstract function get_connection_meta( $connection ); |
||
278 | |||
279 | /** |
||
280 | * Disconnect a Connection |
||
281 | * |
||
282 | * @param string $service_name 'facebook', 'twitter', etc. |
||
283 | * @param string $connection_id Connection ID |
||
284 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
285 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
286 | * @param bool $force_delete Whether to skip permissions checks |
||
287 | * @return false|void False on failure. Void on success. |
||
288 | */ |
||
289 | abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
||
290 | |||
291 | /** |
||
292 | * Globalizes a Connection |
||
293 | * |
||
294 | * @param string $connection_id Connection ID |
||
295 | * @return bool Falsey on failure. Truthy on success. |
||
296 | */ |
||
297 | abstract function globalize_connection( $connection_id ); |
||
298 | |||
299 | /** |
||
300 | * Unglobalizes a Connection |
||
301 | * |
||
302 | * @param string $connection_id Connection ID |
||
303 | * @return bool Falsey on failure. Truthy on success. |
||
304 | */ |
||
305 | abstract function unglobalize_connection( $connection_id ); |
||
306 | |||
307 | /** |
||
308 | * Returns an external URL to the Connection's profile |
||
309 | * |
||
310 | * @param string $service_name 'facebook', 'twitter', etc. |
||
311 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
312 | * @return false|string False on failure. URL on success. |
||
313 | */ |
||
314 | function get_profile_link( $service_name, $connection ) { |
||
354 | |||
355 | /** |
||
356 | * Returns a display name for the Connection |
||
357 | * |
||
358 | * @param string $service_name 'facebook', 'twitter', etc. |
||
359 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
360 | * @return string |
||
361 | */ |
||
362 | function get_display_name( $service_name, $connection ) { |
||
378 | |||
379 | /** |
||
380 | * Whether the user needs to select additional options after connecting |
||
381 | * |
||
382 | * @param string $service_name 'facebook', 'twitter', etc. |
||
383 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
384 | * @return bool |
||
385 | */ |
||
386 | function show_options_popup( $service_name, $connection ) { |
||
409 | |||
410 | /** |
||
411 | * Whether the Connection is "valid" wrt Facebook's requirements. |
||
412 | * |
||
413 | * Must be connected to a Page (not a Profile). |
||
414 | * (Also returns true if we're in the middle of the connection process) |
||
415 | * |
||
416 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
417 | * @return bool |
||
418 | */ |
||
419 | function is_valid_facebook_connection( $connection ) { |
||
427 | |||
428 | /** |
||
429 | * Whether the Connection currently being connected |
||
430 | * |
||
431 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
432 | * @return bool |
||
433 | */ |
||
434 | function is_connecting_connection( $connection ) { |
||
439 | |||
440 | /** |
||
441 | * AJAX Handler to run connection tests on all Connections |
||
442 | * @return void |
||
443 | */ |
||
444 | function test_publicize_conns() { |
||
503 | |||
504 | /** |
||
505 | * Run the connection test for the Connection |
||
506 | * |
||
507 | * @param string $service_name 'facebook', 'twitter', etc. |
||
508 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
509 | * @return WP_Error|true WP_Error on failure. True on success |
||
510 | */ |
||
511 | abstract function test_connection( $service_name, $connection ); |
||
512 | |||
513 | /** |
||
514 | * Retrieves current list of connections and applies filters. |
||
515 | * |
||
516 | * Retrieves current available connections and checks if the connections |
||
517 | * have already been used to share current post. Finally, the checkbox |
||
518 | * form UI fields are calculated. This function exposes connection form |
||
519 | * data directly as array so it can be retrieved for static HTML generation |
||
520 | * or JSON consumption. |
||
521 | * |
||
522 | * @since 6.7.0 |
||
523 | * |
||
524 | * @param integer $selected_post_id Optional. Post ID to query connection status for. |
||
525 | * |
||
526 | * @return array { |
||
527 | * Array of UI setup data for connection list form. |
||
528 | * |
||
529 | * @type string 'unique_id' ID string representing connection |
||
530 | * @type string 'service_name' Slug of the connection's service (facebook, twitter, ...) |
||
531 | * @type string 'service_label' Service Label (Facebook, Twitter, ...) |
||
532 | * @type string 'display_name' Connection's human-readable Username: "@jetpack" |
||
533 | * @type bool 'enabled' Default value for the connection (e.g., for a checkbox). |
||
534 | * @type bool 'done' Has this connection already been publicized to? |
||
535 | * @type bool 'toggleable' Is the user allowed to change the value for the connection? |
||
536 | * @type bool 'global' Is this connection a global one? |
||
537 | * } |
||
538 | */ |
||
539 | public function get_filtered_connection_data( $selected_post_id = null ) { |
||
674 | |||
675 | /** |
||
676 | * Checks if post has already been shared by Publicize in the past. |
||
677 | * |
||
678 | * @since 6.7.0 |
||
679 | * |
||
680 | * @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing. |
||
681 | * |
||
682 | * @return bool True if post has already been shared by Publicize, false otherwise. |
||
683 | */ |
||
684 | abstract public function post_is_done_sharing( $post_id = null ); |
||
685 | |||
686 | /** |
||
687 | * Retrieves full list of available Publicize connection services. |
||
688 | * |
||
689 | * Retrieves current available publicize service connections |
||
690 | * with associated labels and URLs. |
||
691 | * |
||
692 | * @since 6.7.0 |
||
693 | * |
||
694 | * @return array { |
||
695 | * Array of UI service connection data for all services |
||
696 | * |
||
697 | * @type string 'name' Name of service. |
||
698 | * @type string 'label' Display label for service. |
||
699 | * @type string 'url' URL for adding connection to service. |
||
700 | * } |
||
701 | */ |
||
702 | function get_available_service_data() { |
||
716 | |||
717 | /* |
||
718 | * Site Data |
||
719 | */ |
||
720 | |||
721 | function user_id() { |
||
724 | |||
725 | function blog_id() { |
||
728 | |||
729 | /* |
||
730 | * Posts |
||
731 | */ |
||
732 | |||
733 | /** |
||
734 | * Checks old and new status to see if the post should be flagged as |
||
735 | * ready to Publicize. |
||
736 | * |
||
737 | * Attached to the `transition_post_status` filter. |
||
738 | * |
||
739 | * @param string $new_status |
||
740 | * @param string $old_status |
||
741 | * @param WP_Post $post |
||
742 | * @return void |
||
743 | */ |
||
744 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
||
745 | |||
746 | /** |
||
747 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
748 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
749 | * |
||
750 | * Attached to the `save_post` action. |
||
751 | * |
||
752 | * @param int $post_id |
||
753 | * @param WP_Post $post |
||
754 | * @return void |
||
755 | */ |
||
756 | function save_meta( $post_id, $post ) { |
||
911 | |||
912 | /** |
||
913 | * Alters the "Post Published" message to include information about where the post |
||
914 | * was Publicized to. |
||
915 | * |
||
916 | * Attached to the `post_updated_messages` filter |
||
917 | * |
||
918 | * @param string[] $messages |
||
919 | * @return string[] |
||
920 | */ |
||
921 | public function update_published_message( $messages ) { |
||
981 | |||
982 | /** |
||
983 | * Get the Connections the Post was just Publicized to. |
||
984 | * |
||
985 | * Only reliable just after the Post was published. |
||
986 | * |
||
987 | * @param int $post_id |
||
988 | * @return string[] Array of Service display name => Connection display name |
||
989 | */ |
||
990 | function get_publicizing_services( $post_id ) { |
||
1012 | |||
1013 | /** |
||
1014 | * Is the post Publicize-able? |
||
1015 | * |
||
1016 | * Only valid prior to Publicizing a Post. |
||
1017 | * |
||
1018 | * @param WP_Post $post |
||
1019 | * @return bool |
||
1020 | */ |
||
1021 | function post_is_publicizeable( $post ) { |
||
1036 | |||
1037 | /** |
||
1038 | * Is a given post type Publicize-able? |
||
1039 | * |
||
1040 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
1041 | * the publicize_post_types array filter. |
||
1042 | * |
||
1043 | * @param string $post_type The post type to check. |
||
1044 | * @return bool True if the post type can be Publicized. |
||
1045 | */ |
||
1046 | function post_type_is_publicizeable( $post_type ) { |
||
1052 | |||
1053 | /** |
||
1054 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
1055 | * false if a post has already been published. |
||
1056 | * |
||
1057 | * Attached to the `publicize_checkbox_default` filter |
||
1058 | * |
||
1059 | * @param bool $checked |
||
1060 | * @param int $post_id |
||
1061 | * @param string $service_name 'facebook', 'twitter', etc |
||
1062 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
1063 | * @return bool |
||
1064 | */ |
||
1065 | function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
||
1072 | |||
1073 | /* |
||
1074 | * Util |
||
1075 | */ |
||
1076 | |||
1077 | /** |
||
1078 | * Converts a Publicize message template string into a sprintf format string |
||
1079 | * |
||
1080 | * @param string[] $args |
||
1081 | * 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
||
1082 | * ... - The template tags 'title', 'url', etc. |
||
1083 | * @return string |
||
1084 | */ |
||
1085 | protected static function build_sprintf( $args ) { |
||
1098 | } |
||
1099 | |||
1114 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: