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 = 'publish_posts'; |
||
44 | |||
45 | /** |
||
46 | * Sets up the basics of Publicize |
||
47 | */ |
||
48 | function __construct() { |
||
124 | |||
125 | /* |
||
126 | * Services: Facebook, Twitter, etc. |
||
127 | */ |
||
128 | |||
129 | /** |
||
130 | * Get services for the given blog and user. |
||
131 | * |
||
132 | * Can return all available services or just the ones with an active connection. |
||
133 | * |
||
134 | * @param string $filter |
||
135 | * 'all' (default) - Get all services available for connecting |
||
136 | * 'connected' - Get all services currently connected |
||
137 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
138 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
139 | * @return array |
||
140 | */ |
||
141 | abstract function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
||
142 | |||
143 | function can_connect_service( $service_name ) { |
||
146 | |||
147 | /** |
||
148 | * Does the given user have a connection to the service on the given blog? |
||
149 | * |
||
150 | * @param string $service_name 'facebook', 'twitter', etc. |
||
151 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
152 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
153 | * @return bool |
||
154 | */ |
||
155 | function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
||
165 | |||
166 | /** |
||
167 | * Generates a connection URL. |
||
168 | * |
||
169 | * This is the URL, which, when visited by the user, starts the authentication |
||
170 | * process required to forge a connection. |
||
171 | * |
||
172 | * @param string $service_name 'facebook', 'twitter', etc. |
||
173 | * @return string |
||
174 | */ |
||
175 | abstract function connect_url( $service_name ); |
||
176 | |||
177 | /** |
||
178 | * Generates a Connection refresh URL. |
||
179 | * |
||
180 | * This is the URL, which, when visited by the user, re-authenticates their |
||
181 | * connection to the service. |
||
182 | * |
||
183 | * @param string $service_name 'facebook', 'twitter', etc. |
||
184 | * @return string |
||
185 | */ |
||
186 | abstract function refresh_url( $service_name ); |
||
187 | |||
188 | /** |
||
189 | * Generates a disconnection URL. |
||
190 | * |
||
191 | * This is the URL, which, when visited by the user, breaks their connection |
||
192 | * with the service. |
||
193 | * |
||
194 | * @param string $service_name 'facebook', 'twitter', etc. |
||
195 | * @param string $connection_id Connection ID |
||
196 | * @return string |
||
197 | */ |
||
198 | abstract function disconnect_url( $service_name, $connection_id ); |
||
199 | |||
200 | /** |
||
201 | * Returns a display name for the Service |
||
202 | * |
||
203 | * @param string $service_name 'facebook', 'twitter', etc. |
||
204 | * @return string |
||
205 | */ |
||
206 | public static function get_service_label( $service_name ) { |
||
222 | |||
223 | /* |
||
224 | * Connections: For each Service, there can be multiple connections |
||
225 | * for a given user. For example, one user could be connected to Twitter |
||
226 | * as both @jetpack and as @wordpressdotcom |
||
227 | * |
||
228 | * For historical reasons, Connections are represented as an object |
||
229 | * on WordPress.com and as an array in Jetpack. |
||
230 | */ |
||
231 | |||
232 | /** |
||
233 | * Get the active Connections of a Service |
||
234 | * |
||
235 | * @param string $service_name 'facebook', 'twitter', etc. |
||
236 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
237 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
238 | * @return false|object[]|array[] false if no connections exist |
||
239 | */ |
||
240 | abstract function get_connections( $service_name, $_blog_id = false, $_user_id = false ); |
||
241 | |||
242 | /** |
||
243 | * Get a single Connection of a Service |
||
244 | * |
||
245 | * @param string $service_name 'facebook', 'twitter', etc. |
||
246 | * @param string $connection_id Connection ID |
||
247 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
248 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
249 | * @return false|object[]|array[] false if no connections exist |
||
250 | */ |
||
251 | abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
||
252 | |||
253 | /** |
||
254 | * Get the Connection ID. |
||
255 | * |
||
256 | * Note that this is different than the Connection's uniqueid. |
||
257 | * |
||
258 | * Via a quirk of history, ID is globally unique and unique_id |
||
259 | * is only unique per site. |
||
260 | * |
||
261 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
262 | * @return string |
||
263 | */ |
||
264 | abstract function get_connection_id( $connection ); |
||
265 | |||
266 | /** |
||
267 | * Get the Connection unique_id |
||
268 | * |
||
269 | * Note that this is different than the Connections ID. |
||
270 | * |
||
271 | * Via a quirk of history, ID is globally unique and unique_id |
||
272 | * is only unique per site. |
||
273 | * |
||
274 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
275 | * @return string |
||
276 | */ |
||
277 | abstract function get_connection_unique_id( $connection ); |
||
278 | |||
279 | /** |
||
280 | * Get the Connection's Meta data |
||
281 | * |
||
282 | * @param object|array Connection |
||
283 | * @return array Connection Meta |
||
284 | */ |
||
285 | abstract function get_connection_meta( $connection ); |
||
286 | |||
287 | /** |
||
288 | * Disconnect a Connection |
||
289 | * |
||
290 | * @param string $service_name 'facebook', 'twitter', etc. |
||
291 | * @param string $connection_id Connection ID |
||
292 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
293 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
294 | * @param bool $force_delete Whether to skip permissions checks |
||
295 | * @return false|void False on failure. Void on success. |
||
296 | */ |
||
297 | abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
||
298 | |||
299 | /** |
||
300 | * Globalizes a Connection |
||
301 | * |
||
302 | * @param string $connection_id Connection ID |
||
303 | * @return bool Falsey on failure. Truthy on success. |
||
304 | */ |
||
305 | abstract function globalize_connection( $connection_id ); |
||
306 | |||
307 | /** |
||
308 | * Unglobalizes a Connection |
||
309 | * |
||
310 | * @param string $connection_id Connection ID |
||
311 | * @return bool Falsey on failure. Truthy on success. |
||
312 | */ |
||
313 | abstract function unglobalize_connection( $connection_id ); |
||
314 | |||
315 | /** |
||
316 | * Returns an external URL to the Connection's profile |
||
317 | * |
||
318 | * @param string $service_name 'facebook', 'twitter', etc. |
||
319 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
320 | * @return false|string False on failure. URL on success. |
||
321 | */ |
||
322 | function get_profile_link( $service_name, $connection ) { |
||
362 | |||
363 | /** |
||
364 | * Returns a display name for the Connection |
||
365 | * |
||
366 | * @param string $service_name 'facebook', 'twitter', etc. |
||
367 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
368 | * @return string |
||
369 | */ |
||
370 | function get_display_name( $service_name, $connection ) { |
||
386 | |||
387 | /** |
||
388 | * Whether the user needs to select additional options after connecting |
||
389 | * |
||
390 | * @param string $service_name 'facebook', 'twitter', etc. |
||
391 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
392 | * @return bool |
||
393 | */ |
||
394 | function show_options_popup( $service_name, $connection ) { |
||
417 | |||
418 | /** |
||
419 | * Whether the Connection is "valid" wrt Facebook's requirements. |
||
420 | * |
||
421 | * Must be connected to a Page (not a Profile). |
||
422 | * (Also returns true if we're in the middle of the connection process) |
||
423 | * |
||
424 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
425 | * @return bool |
||
426 | */ |
||
427 | function is_valid_facebook_connection( $connection ) { |
||
435 | |||
436 | /** |
||
437 | * LinkedIn needs to be reauthenticated to use v2 of their API. |
||
438 | * If it's using LinkedIn old API, it's an 'invalid' connection |
||
439 | * |
||
440 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
441 | * @return bool |
||
442 | */ |
||
443 | function is_invalid_linkedin_connection( $connection ) { |
||
448 | |||
449 | /** |
||
450 | * Whether the Connection currently being connected |
||
451 | * |
||
452 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
453 | * @return bool |
||
454 | */ |
||
455 | function is_connecting_connection( $connection ) { |
||
460 | |||
461 | /** |
||
462 | * AJAX Handler to run connection tests on all Connections |
||
463 | * @return void |
||
464 | */ |
||
465 | function test_publicize_conns() { |
||
468 | |||
469 | /** |
||
470 | * Run connection tests on all Connections |
||
471 | * |
||
472 | * @return array { |
||
473 | * Array of connection test results. |
||
474 | * |
||
475 | * @type string 'connectionID' Connection identifier string that is unique for each connection |
||
476 | * @type string 'serviceName' Slug of the connection's service (facebook, twitter, ...) |
||
477 | * @type bool 'connectionTestPassed' Whether the connection test was successful |
||
478 | * @type string 'connectionTestMessage' Test success or error message |
||
479 | * @type bool 'userCanRefresh' Whether the user can re-authenticate their connection to the service |
||
480 | * @type string 'refreshText' Message instructing user to re-authenticate their connection to the service |
||
481 | * @type string 'refreshURL' URL, which, when visited by the user, re-authenticates their connection to the service. |
||
482 | * @type string 'unique_id' ID string representing connection |
||
483 | * } |
||
484 | */ |
||
485 | function get_publicize_conns_test_results() { |
||
551 | |||
552 | /** |
||
553 | * Run the connection test for the Connection |
||
554 | * |
||
555 | * @param string $service_name 'facebook', 'twitter', etc. |
||
556 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
557 | * @return WP_Error|true WP_Error on failure. True on success |
||
558 | */ |
||
559 | abstract function test_connection( $service_name, $connection ); |
||
560 | |||
561 | /** |
||
562 | * Retrieves current list of connections and applies filters. |
||
563 | * |
||
564 | * Retrieves current available connections and checks if the connections |
||
565 | * have already been used to share current post. Finally, the checkbox |
||
566 | * form UI fields are calculated. This function exposes connection form |
||
567 | * data directly as array so it can be retrieved for static HTML generation |
||
568 | * or JSON consumption. |
||
569 | * |
||
570 | * @since 6.7.0 |
||
571 | * |
||
572 | * @param integer $selected_post_id Optional. Post ID to query connection status for. |
||
573 | * |
||
574 | * @return array { |
||
575 | * Array of UI setup data for connection list form. |
||
576 | * |
||
577 | * @type string 'unique_id' ID string representing connection |
||
578 | * @type string 'service_name' Slug of the connection's service (facebook, twitter, ...) |
||
579 | * @type string 'service_label' Service Label (Facebook, Twitter, ...) |
||
580 | * @type string 'display_name' Connection's human-readable Username: "@jetpack" |
||
581 | * @type bool 'enabled' Default value for the connection (e.g., for a checkbox). |
||
582 | * @type bool 'done' Has this connection already been publicized to? |
||
583 | * @type bool 'toggleable' Is the user allowed to change the value for the connection? |
||
584 | * @type bool 'global' Is this connection a global one? |
||
585 | * } |
||
586 | */ |
||
587 | public function get_filtered_connection_data( $selected_post_id = null ) { |
||
722 | |||
723 | /** |
||
724 | * Checks if post has already been shared by Publicize in the past. |
||
725 | * |
||
726 | * @since 6.7.0 |
||
727 | * |
||
728 | * @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing. |
||
729 | * |
||
730 | * @return bool True if post has already been shared by Publicize, false otherwise. |
||
731 | */ |
||
732 | abstract public function post_is_done_sharing( $post_id = null ); |
||
733 | |||
734 | /** |
||
735 | * Retrieves full list of available Publicize connection services. |
||
736 | * |
||
737 | * Retrieves current available publicize service connections |
||
738 | * with associated labels and URLs. |
||
739 | * |
||
740 | * @since 6.7.0 |
||
741 | * |
||
742 | * @return array { |
||
743 | * Array of UI service connection data for all services |
||
744 | * |
||
745 | * @type string 'name' Name of service. |
||
746 | * @type string 'label' Display label for service. |
||
747 | * @type string 'url' URL for adding connection to service. |
||
748 | * } |
||
749 | */ |
||
750 | function get_available_service_data() { |
||
764 | |||
765 | /* |
||
766 | * Site Data |
||
767 | */ |
||
768 | |||
769 | function user_id() { |
||
772 | |||
773 | function blog_id() { |
||
776 | |||
777 | /* |
||
778 | * Posts |
||
779 | */ |
||
780 | |||
781 | /** |
||
782 | * Checks old and new status to see if the post should be flagged as |
||
783 | * ready to Publicize. |
||
784 | * |
||
785 | * Attached to the `transition_post_status` filter. |
||
786 | * |
||
787 | * @param string $new_status |
||
788 | * @param string $old_status |
||
789 | * @param WP_Post $post |
||
790 | * @return void |
||
791 | */ |
||
792 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
||
793 | |||
794 | /** |
||
795 | * Ensures the Post internal post-type supports `publicize` |
||
796 | * |
||
797 | * This feature support flag is used by the REST API. |
||
798 | */ |
||
799 | function add_post_type_support() { |
||
802 | |||
803 | /** |
||
804 | * Register the Publicize Gutenberg extension |
||
805 | */ |
||
806 | function register_gutenberg_extension() { |
||
817 | |||
818 | /** |
||
819 | * Can the current user access Publicize Data. |
||
820 | * |
||
821 | * @param int $post_id. 0 for general access. Post_ID for specific access. |
||
822 | * @return bool |
||
823 | */ |
||
824 | function current_user_can_access_publicize_data( $post_id = 0 ) { |
||
842 | |||
843 | /** |
||
844 | * Auth callback for the protected ->POST_MESS post_meta |
||
845 | * |
||
846 | * @param bool $allowed |
||
847 | * @param string $meta_key |
||
848 | * @param int $object_id Post ID |
||
849 | * @return bool |
||
850 | */ |
||
851 | function message_meta_auth_callback( $allowed, $meta_key, $object_id ) { |
||
854 | |||
855 | /** |
||
856 | * Registers the ->POST_MESS post_meta for use in the REST API. |
||
857 | * |
||
858 | * Registers for each post type that with `publicize` feature support. |
||
859 | */ |
||
860 | function register_post_meta() { |
||
882 | |||
883 | /** |
||
884 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
885 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
886 | * |
||
887 | * Attached to the `save_post` action. |
||
888 | * |
||
889 | * @param int $post_id |
||
890 | * @param WP_Post $post |
||
891 | * @return void |
||
892 | */ |
||
893 | function save_meta( $post_id, $post ) { |
||
1048 | |||
1049 | /** |
||
1050 | * Alters the "Post Published" message to include information about where the post |
||
1051 | * was Publicized to. |
||
1052 | * |
||
1053 | * Attached to the `post_updated_messages` filter |
||
1054 | * |
||
1055 | * @param string[] $messages |
||
1056 | * @return string[] |
||
1057 | */ |
||
1058 | public function update_published_message( $messages ) { |
||
1118 | |||
1119 | /** |
||
1120 | * Get the Connections the Post was just Publicized to. |
||
1121 | * |
||
1122 | * Only reliable just after the Post was published. |
||
1123 | * |
||
1124 | * @param int $post_id |
||
1125 | * @return string[] Array of Service display name => Connection display name |
||
1126 | */ |
||
1127 | function get_publicizing_services( $post_id ) { |
||
1149 | |||
1150 | /** |
||
1151 | * Is the post Publicize-able? |
||
1152 | * |
||
1153 | * Only valid prior to Publicizing a Post. |
||
1154 | * |
||
1155 | * @param WP_Post $post |
||
1156 | * @return bool |
||
1157 | */ |
||
1158 | function post_is_publicizeable( $post ) { |
||
1173 | |||
1174 | /** |
||
1175 | * Is a given post type Publicize-able? |
||
1176 | * |
||
1177 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
1178 | * the publicize_post_types array filter. |
||
1179 | * |
||
1180 | * @param string $post_type The post type to check. |
||
1181 | * @return bool True if the post type can be Publicized. |
||
1182 | */ |
||
1183 | function post_type_is_publicizeable( $post_type ) { |
||
1189 | |||
1190 | /** |
||
1191 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
1192 | * false if a post has already been published. |
||
1193 | * |
||
1194 | * Attached to the `publicize_checkbox_default` filter |
||
1195 | * |
||
1196 | * @param bool $checked |
||
1197 | * @param int $post_id |
||
1198 | * @param string $service_name 'facebook', 'twitter', etc |
||
1199 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
1200 | * @return bool |
||
1201 | */ |
||
1202 | function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
||
1209 | |||
1210 | /* |
||
1211 | * Util |
||
1212 | */ |
||
1213 | |||
1214 | /** |
||
1215 | * Converts a Publicize message template string into a sprintf format string |
||
1216 | * |
||
1217 | * @param string[] $args |
||
1218 | * 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
||
1219 | * ... - The template tags 'title', 'url', etc. |
||
1220 | * @return string |
||
1221 | */ |
||
1222 | protected static function build_sprintf( $args ) { |
||
1235 | } |
||
1236 | |||
1251 |
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: