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() { |
||
123 | |||
124 | /* |
||
125 | * Services: Facebook, Twitter, etc. |
||
126 | */ |
||
127 | |||
128 | /** |
||
129 | * Get services for the given blog and user. |
||
130 | * |
||
131 | * Can return all available services or just the ones with an active connection. |
||
132 | * |
||
133 | * @param string $filter |
||
134 | * 'all' (default) - Get all services available for connecting |
||
135 | * 'connected' - Get all services currently connected |
||
136 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
137 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
138 | * @return array |
||
139 | */ |
||
140 | abstract function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
||
141 | |||
142 | /** |
||
143 | * Does the given user have a connection to the service on the given blog? |
||
144 | * |
||
145 | * @param string $service_name 'facebook', 'twitter', etc. |
||
146 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
147 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
148 | * @return bool |
||
149 | */ |
||
150 | function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
||
160 | |||
161 | /** |
||
162 | * Generates a connection URL. |
||
163 | * |
||
164 | * This is the URL, which, when visited by the user, starts the authentication |
||
165 | * process required to forge a connection. |
||
166 | * |
||
167 | * @param string $service_name 'facebook', 'twitter', etc. |
||
168 | * @return string |
||
169 | */ |
||
170 | abstract function connect_url( $service_name ); |
||
171 | |||
172 | /** |
||
173 | * Generates a Connection refresh URL. |
||
174 | * |
||
175 | * This is the URL, which, when visited by the user, re-authenticates their |
||
176 | * connection to the service. |
||
177 | * |
||
178 | * @param string $service_name 'facebook', 'twitter', etc. |
||
179 | * @return string |
||
180 | */ |
||
181 | abstract function refresh_url( $service_name ); |
||
182 | |||
183 | /** |
||
184 | * Generates a disconnection URL. |
||
185 | * |
||
186 | * This is the URL, which, when visited by the user, breaks their connection |
||
187 | * with the service. |
||
188 | * |
||
189 | * @param string $service_name 'facebook', 'twitter', etc. |
||
190 | * @param string $connection_id Connection ID |
||
191 | * @return string |
||
192 | */ |
||
193 | abstract function disconnect_url( $service_name, $connection_id ); |
||
194 | |||
195 | /** |
||
196 | * Returns a display name for the Service |
||
197 | * |
||
198 | * @param string $service_name 'facebook', 'twitter', etc. |
||
199 | * @return string |
||
200 | */ |
||
201 | public static function get_service_label( $service_name ) { |
||
217 | |||
218 | /* |
||
219 | * Connections: For each Service, there can be multiple connections |
||
220 | * for a given user. For example, one user could be connected to Twitter |
||
221 | * as both @jetpack and as @wordpressdotcom |
||
222 | * |
||
223 | * For historical reasons, Connections are represented as an object |
||
224 | * on WordPress.com and as an array in Jetpack. |
||
225 | */ |
||
226 | |||
227 | /** |
||
228 | * Get the active Connections of a Service |
||
229 | * |
||
230 | * @param string $service_name 'facebook', 'twitter', etc. |
||
231 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
232 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
233 | * @return false|object[]|array[] false if no connections exist |
||
234 | */ |
||
235 | abstract function get_connections( $service_name, $_blog_id = false, $_user_id = false ); |
||
236 | |||
237 | /** |
||
238 | * Get a single Connection of a Service |
||
239 | * |
||
240 | * @param string $service_name 'facebook', 'twitter', etc. |
||
241 | * @param string $connection_id Connection ID |
||
242 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
243 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
244 | * @return false|object[]|array[] false if no connections exist |
||
245 | */ |
||
246 | abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
||
247 | |||
248 | /** |
||
249 | * Get the Connection ID. |
||
250 | * |
||
251 | * Note that this is different than the Connection's uniqueid. |
||
252 | * |
||
253 | * Via a quirk of history, ID is globally unique and unique_id |
||
254 | * is only unique per site. |
||
255 | * |
||
256 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
257 | * @return string |
||
258 | */ |
||
259 | abstract function get_connection_id( $connection ); |
||
260 | |||
261 | /** |
||
262 | * Get the Connection unique_id |
||
263 | * |
||
264 | * Note that this is different than the Connections ID. |
||
265 | * |
||
266 | * Via a quirk of history, ID is globally unique and unique_id |
||
267 | * is only unique per site. |
||
268 | * |
||
269 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
270 | * @return string |
||
271 | */ |
||
272 | abstract function get_connection_unique_id( $connection ); |
||
273 | |||
274 | /** |
||
275 | * Get the Connection's Meta data |
||
276 | * |
||
277 | * @param object|array Connection |
||
278 | * @return array Connection Meta |
||
279 | */ |
||
280 | abstract function get_connection_meta( $connection ); |
||
281 | |||
282 | /** |
||
283 | * Disconnect a Connection |
||
284 | * |
||
285 | * @param string $service_name 'facebook', 'twitter', etc. |
||
286 | * @param string $connection_id Connection ID |
||
287 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
288 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
289 | * @param bool $force_delete Whether to skip permissions checks |
||
290 | * @return false|void False on failure. Void on success. |
||
291 | */ |
||
292 | abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
||
293 | |||
294 | /** |
||
295 | * Globalizes a Connection |
||
296 | * |
||
297 | * @param string $connection_id Connection ID |
||
298 | * @return bool Falsey on failure. Truthy on success. |
||
299 | */ |
||
300 | abstract function globalize_connection( $connection_id ); |
||
301 | |||
302 | /** |
||
303 | * Unglobalizes a Connection |
||
304 | * |
||
305 | * @param string $connection_id Connection ID |
||
306 | * @return bool Falsey on failure. Truthy on success. |
||
307 | */ |
||
308 | abstract function unglobalize_connection( $connection_id ); |
||
309 | |||
310 | /** |
||
311 | * Returns an external URL to the Connection's profile |
||
312 | * |
||
313 | * @param string $service_name 'facebook', 'twitter', etc. |
||
314 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
315 | * @return false|string False on failure. URL on success. |
||
316 | */ |
||
317 | function get_profile_link( $service_name, $connection ) { |
||
357 | |||
358 | /** |
||
359 | * Returns a display name for the Connection |
||
360 | * |
||
361 | * @param string $service_name 'facebook', 'twitter', etc. |
||
362 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
363 | * @return string |
||
364 | */ |
||
365 | function get_display_name( $service_name, $connection ) { |
||
381 | |||
382 | /** |
||
383 | * Whether the user needs to select additional options after connecting |
||
384 | * |
||
385 | * @param string $service_name 'facebook', 'twitter', etc. |
||
386 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
387 | * @return bool |
||
388 | */ |
||
389 | function show_options_popup( $service_name, $connection ) { |
||
412 | |||
413 | /** |
||
414 | * Whether the Connection is "valid" wrt Facebook's requirements. |
||
415 | * |
||
416 | * Must be connected to a Page (not a Profile). |
||
417 | * (Also returns true if we're in the middle of the connection process) |
||
418 | * |
||
419 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
420 | * @return bool |
||
421 | */ |
||
422 | function is_valid_facebook_connection( $connection ) { |
||
430 | |||
431 | /** |
||
432 | * Whether the Connection currently being connected |
||
433 | * |
||
434 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
435 | * @return bool |
||
436 | */ |
||
437 | function is_connecting_connection( $connection ) { |
||
442 | |||
443 | /** |
||
444 | * AJAX Handler to run connection tests on all Connections |
||
445 | * @return void |
||
446 | */ |
||
447 | function test_publicize_conns() { |
||
450 | |||
451 | /** |
||
452 | * Run connection tests on all Connections |
||
453 | * |
||
454 | * @return array { |
||
455 | * Array of connection test results. |
||
456 | * |
||
457 | * @type string 'connectionID' Connection identifier string that is unique for each connection |
||
458 | * @type string 'serviceName' Slug of the connection's service (facebook, twitter, ...) |
||
459 | * @type bool 'connectionTestPassed' Whether the connection test was successful |
||
460 | * @type string 'connectionTestMessage' Test success or error message |
||
461 | * @type bool 'userCanRefresh' Whether the user can re-authenticate their connection to the service |
||
462 | * @type string 'refreshText' Message instructing user to re-authenticate their connection to the service |
||
463 | * @type string 'refreshURL' URL, which, when visited by the user, re-authenticates their connection to the service. |
||
464 | * @type string 'unique_id' ID string representing connection |
||
465 | * } |
||
466 | */ |
||
467 | function get_publicize_conns_test_results() { |
||
526 | |||
527 | /** |
||
528 | * Run the connection test for the Connection |
||
529 | * |
||
530 | * @param string $service_name 'facebook', 'twitter', etc. |
||
531 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
532 | * @return WP_Error|true WP_Error on failure. True on success |
||
533 | */ |
||
534 | abstract function test_connection( $service_name, $connection ); |
||
535 | |||
536 | /** |
||
537 | * Retrieves current list of connections and applies filters. |
||
538 | * |
||
539 | * Retrieves current available connections and checks if the connections |
||
540 | * have already been used to share current post. Finally, the checkbox |
||
541 | * form UI fields are calculated. This function exposes connection form |
||
542 | * data directly as array so it can be retrieved for static HTML generation |
||
543 | * or JSON consumption. |
||
544 | * |
||
545 | * @since 6.7.0 |
||
546 | * |
||
547 | * @param integer $selected_post_id Optional. Post ID to query connection status for. |
||
548 | * |
||
549 | * @return array { |
||
550 | * Array of UI setup data for connection list form. |
||
551 | * |
||
552 | * @type string 'unique_id' ID string representing connection |
||
553 | * @type string 'service_name' Slug of the connection's service (facebook, twitter, ...) |
||
554 | * @type string 'service_label' Service Label (Facebook, Twitter, ...) |
||
555 | * @type string 'display_name' Connection's human-readable Username: "@jetpack" |
||
556 | * @type bool 'enabled' Default value for the connection (e.g., for a checkbox). |
||
557 | * @type bool 'done' Has this connection already been publicized to? |
||
558 | * @type bool 'toggleable' Is the user allowed to change the value for the connection? |
||
559 | * @type bool 'global' Is this connection a global one? |
||
560 | * } |
||
561 | */ |
||
562 | public function get_filtered_connection_data( $selected_post_id = null ) { |
||
697 | |||
698 | /** |
||
699 | * Checks if post has already been shared by Publicize in the past. |
||
700 | * |
||
701 | * @since 6.7.0 |
||
702 | * |
||
703 | * @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing. |
||
704 | * |
||
705 | * @return bool True if post has already been shared by Publicize, false otherwise. |
||
706 | */ |
||
707 | abstract public function post_is_done_sharing( $post_id = null ); |
||
708 | |||
709 | /** |
||
710 | * Retrieves full list of available Publicize connection services. |
||
711 | * |
||
712 | * Retrieves current available publicize service connections |
||
713 | * with associated labels and URLs. |
||
714 | * |
||
715 | * @since 6.7.0 |
||
716 | * |
||
717 | * @return array { |
||
718 | * Array of UI service connection data for all services |
||
719 | * |
||
720 | * @type string 'name' Name of service. |
||
721 | * @type string 'label' Display label for service. |
||
722 | * @type string 'url' URL for adding connection to service. |
||
723 | * } |
||
724 | */ |
||
725 | function get_available_service_data() { |
||
739 | |||
740 | /* |
||
741 | * Site Data |
||
742 | */ |
||
743 | |||
744 | function user_id() { |
||
747 | |||
748 | function blog_id() { |
||
751 | |||
752 | /* |
||
753 | * Posts |
||
754 | */ |
||
755 | |||
756 | /** |
||
757 | * Checks old and new status to see if the post should be flagged as |
||
758 | * ready to Publicize. |
||
759 | * |
||
760 | * Attached to the `transition_post_status` filter. |
||
761 | * |
||
762 | * @param string $new_status |
||
763 | * @param string $old_status |
||
764 | * @param WP_Post $post |
||
765 | * @return void |
||
766 | */ |
||
767 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
||
768 | |||
769 | /** |
||
770 | * Ensures the Post internal post-type supports `publicize` |
||
771 | * |
||
772 | * This feature support flag is used by the REST API. |
||
773 | */ |
||
774 | function add_post_type_support() { |
||
777 | |||
778 | /** |
||
779 | * Can the current user access Publicize Data. |
||
780 | * |
||
781 | * @param int $post_id. 0 for general access. Post_ID for specific access. |
||
782 | * @return bool |
||
783 | */ |
||
784 | function current_user_can_access_publicize_data( $post_id = 0 ) { |
||
802 | |||
803 | /** |
||
804 | * Auth callback for the protected ->POST_MESS post_meta |
||
805 | * |
||
806 | * @param bool $allowed |
||
807 | * @param string $meta_key |
||
808 | * @param int $object_id Post ID |
||
809 | * @return bool |
||
810 | */ |
||
811 | function message_meta_auth_callback( $allowed, $meta_key, $object_id ) { |
||
814 | |||
815 | /** |
||
816 | * Registers the ->POST_MESS post_meta for use in the REST API. |
||
817 | * |
||
818 | * Registers for each post type that with `publicize` feature support. |
||
819 | */ |
||
820 | function register_post_meta() { |
||
842 | |||
843 | /** |
||
844 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
845 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
846 | * |
||
847 | * Attached to the `save_post` action. |
||
848 | * |
||
849 | * @param int $post_id |
||
850 | * @param WP_Post $post |
||
851 | * @return void |
||
852 | */ |
||
853 | function save_meta( $post_id, $post ) { |
||
1008 | |||
1009 | /** |
||
1010 | * Alters the "Post Published" message to include information about where the post |
||
1011 | * was Publicized to. |
||
1012 | * |
||
1013 | * Attached to the `post_updated_messages` filter |
||
1014 | * |
||
1015 | * @param string[] $messages |
||
1016 | * @return string[] |
||
1017 | */ |
||
1018 | public function update_published_message( $messages ) { |
||
1078 | |||
1079 | /** |
||
1080 | * Get the Connections the Post was just Publicized to. |
||
1081 | * |
||
1082 | * Only reliable just after the Post was published. |
||
1083 | * |
||
1084 | * @param int $post_id |
||
1085 | * @return string[] Array of Service display name => Connection display name |
||
1086 | */ |
||
1087 | function get_publicizing_services( $post_id ) { |
||
1109 | |||
1110 | /** |
||
1111 | * Is the post Publicize-able? |
||
1112 | * |
||
1113 | * Only valid prior to Publicizing a Post. |
||
1114 | * |
||
1115 | * @param WP_Post $post |
||
1116 | * @return bool |
||
1117 | */ |
||
1118 | function post_is_publicizeable( $post ) { |
||
1133 | |||
1134 | /** |
||
1135 | * Is a given post type Publicize-able? |
||
1136 | * |
||
1137 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
1138 | * the publicize_post_types array filter. |
||
1139 | * |
||
1140 | * @param string $post_type The post type to check. |
||
1141 | * @return bool True if the post type can be Publicized. |
||
1142 | */ |
||
1143 | function post_type_is_publicizeable( $post_type ) { |
||
1149 | |||
1150 | /** |
||
1151 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
1152 | * false if a post has already been published. |
||
1153 | * |
||
1154 | * Attached to the `publicize_checkbox_default` filter |
||
1155 | * |
||
1156 | * @param bool $checked |
||
1157 | * @param int $post_id |
||
1158 | * @param string $service_name 'facebook', 'twitter', etc |
||
1159 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
1160 | * @return bool |
||
1161 | */ |
||
1162 | function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
||
1169 | |||
1170 | /* |
||
1171 | * Util |
||
1172 | */ |
||
1173 | |||
1174 | /** |
||
1175 | * Converts a Publicize message template string into a sprintf format string |
||
1176 | * |
||
1177 | * @param string[] $args |
||
1178 | * 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
||
1179 | * ... - The template tags 'title', 'url', etc. |
||
1180 | * @return string |
||
1181 | */ |
||
1182 | protected static function build_sprintf( $args ) { |
||
1195 | } |
||
1196 | |||
1211 |
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: