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 |
||
7 | abstract class Publicize_Base { |
||
8 | |||
9 | /** |
||
10 | * Services that are currently connected to the given user |
||
11 | * through publicize. |
||
12 | */ |
||
13 | public $connected_services = array(); |
||
14 | |||
15 | /** |
||
16 | * Services that are supported by publicize. They don't |
||
17 | * necessarily need to be connected to the current user. |
||
18 | */ |
||
19 | public $services; |
||
20 | |||
21 | /** |
||
22 | * key names for post meta |
||
23 | */ |
||
24 | public $ADMIN_PAGE = 'wpas'; |
||
25 | public $POST_MESS = '_wpas_mess'; |
||
26 | |||
27 | /** |
||
28 | * Post meta key for flagging when the post is a tweetstorm. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | public $POST_TWEETSTORM = '_wpas_is_tweetstorm'; |
||
33 | |||
34 | public $POST_SKIP = '_wpas_skip_'; // connection id appended to indicate that a connection should NOT be publicized to |
||
35 | public $POST_DONE = '_wpas_done_'; // connection id appended to indicate a connection has already been publicized to |
||
36 | public $USER_AUTH = 'wpas_authorize'; |
||
37 | public $USER_OPT = 'wpas_'; |
||
38 | public $PENDING = '_publicize_pending'; // ready for Publicize to do its thing |
||
39 | public $POST_SERVICE_DONE = '_publicize_done_external'; // array of external ids where we've Publicized |
||
40 | |||
41 | /** |
||
42 | * default pieces of the message used in constructing the |
||
43 | * content pushed out to other social networks |
||
44 | */ |
||
45 | |||
46 | public $default_prefix = ''; |
||
47 | public $default_message = '%title%'; |
||
48 | public $default_suffix = ' '; |
||
49 | |||
50 | /** |
||
51 | * What WP capability is require to create/delete global connections? |
||
52 | * All users with this cap can un-globalize all other global connections, and globalize any of their own |
||
53 | * Globalized connections cannot be unselected by users without this capability when publishing |
||
54 | */ |
||
55 | public $GLOBAL_CAP = 'publish_posts'; |
||
56 | |||
57 | /** |
||
58 | * Sets up the basics of Publicize |
||
59 | */ |
||
60 | function __construct() { |
||
136 | |||
137 | /* |
||
138 | * Services: Facebook, Twitter, etc. |
||
139 | */ |
||
140 | |||
141 | /** |
||
142 | * Get services for the given blog and user. |
||
143 | * |
||
144 | * Can return all available services or just the ones with an active connection. |
||
145 | * |
||
146 | * @param string $filter |
||
147 | * 'all' (default) - Get all services available for connecting |
||
148 | * 'connected' - Get all services currently connected |
||
149 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
150 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
151 | * @return array |
||
152 | */ |
||
153 | abstract function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
||
154 | |||
155 | function can_connect_service( $service_name ) { |
||
158 | |||
159 | /** |
||
160 | * Does the given user have a connection to the service on the given blog? |
||
161 | * |
||
162 | * @param string $service_name 'facebook', 'twitter', etc. |
||
163 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
164 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
165 | * @return bool |
||
166 | */ |
||
167 | function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
||
177 | |||
178 | /** |
||
179 | * Generates a connection URL. |
||
180 | * |
||
181 | * This is the URL, which, when visited by the user, starts the authentication |
||
182 | * process required to forge a connection. |
||
183 | * |
||
184 | * @param string $service_name 'facebook', 'twitter', etc. |
||
185 | * @return string |
||
186 | */ |
||
187 | abstract function connect_url( $service_name ); |
||
188 | |||
189 | /** |
||
190 | * Generates a Connection refresh URL. |
||
191 | * |
||
192 | * This is the URL, which, when visited by the user, re-authenticates their |
||
193 | * connection to the service. |
||
194 | * |
||
195 | * @param string $service_name 'facebook', 'twitter', etc. |
||
196 | * @return string |
||
197 | */ |
||
198 | abstract function refresh_url( $service_name ); |
||
199 | |||
200 | /** |
||
201 | * Generates a disconnection URL. |
||
202 | * |
||
203 | * This is the URL, which, when visited by the user, breaks their connection |
||
204 | * with the service. |
||
205 | * |
||
206 | * @param string $service_name 'facebook', 'twitter', etc. |
||
207 | * @param string $connection_id Connection ID |
||
208 | * @return string |
||
209 | */ |
||
210 | abstract function disconnect_url( $service_name, $connection_id ); |
||
211 | |||
212 | /** |
||
213 | * Returns a display name for the Service |
||
214 | * |
||
215 | * @param string $service_name 'facebook', 'twitter', etc. |
||
216 | * @return string |
||
217 | */ |
||
218 | public static function get_service_label( $service_name ) { |
||
235 | |||
236 | /* |
||
237 | * Connections: For each Service, there can be multiple connections |
||
238 | * for a given user. For example, one user could be connected to Twitter |
||
239 | * as both @jetpack and as @wordpressdotcom |
||
240 | * |
||
241 | * For historical reasons, Connections are represented as an object |
||
242 | * on WordPress.com and as an array in Jetpack. |
||
243 | */ |
||
244 | |||
245 | /** |
||
246 | * Get the active Connections of a Service |
||
247 | * |
||
248 | * @param string $service_name 'facebook', 'twitter', etc. |
||
249 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
250 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
251 | * @return false|object[]|array[] false if no connections exist |
||
252 | */ |
||
253 | abstract function get_connections( $service_name, $_blog_id = false, $_user_id = false ); |
||
254 | |||
255 | /** |
||
256 | * Get a single Connection of a Service |
||
257 | * |
||
258 | * @param string $service_name 'facebook', 'twitter', etc. |
||
259 | * @param string $connection_id Connection ID |
||
260 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
261 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
262 | * @return false|object[]|array[] false if no connections exist |
||
263 | */ |
||
264 | abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
||
265 | |||
266 | /** |
||
267 | * Get the Connection ID. |
||
268 | * |
||
269 | * Note that this is different than the Connection's uniqueid. |
||
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_id( $connection ); |
||
278 | |||
279 | /** |
||
280 | * Get the Connection unique_id |
||
281 | * |
||
282 | * Note that this is different than the Connections ID. |
||
283 | * |
||
284 | * Via a quirk of history, ID is globally unique and unique_id |
||
285 | * is only unique per site. |
||
286 | * |
||
287 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
288 | * @return string |
||
289 | */ |
||
290 | abstract function get_connection_unique_id( $connection ); |
||
291 | |||
292 | /** |
||
293 | * Get the Connection's Meta data |
||
294 | * |
||
295 | * @param object|array Connection |
||
296 | * @return array Connection Meta |
||
297 | */ |
||
298 | abstract function get_connection_meta( $connection ); |
||
299 | |||
300 | /** |
||
301 | * Disconnect a Connection |
||
302 | * |
||
303 | * @param string $service_name 'facebook', 'twitter', etc. |
||
304 | * @param string $connection_id Connection ID |
||
305 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
306 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
307 | * @param bool $force_delete Whether to skip permissions checks |
||
308 | * @return false|void False on failure. Void on success. |
||
309 | */ |
||
310 | abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
||
311 | |||
312 | /** |
||
313 | * Globalizes a Connection |
||
314 | * |
||
315 | * @param string $connection_id Connection ID |
||
316 | * @return bool Falsey on failure. Truthy on success. |
||
317 | */ |
||
318 | abstract function globalize_connection( $connection_id ); |
||
319 | |||
320 | /** |
||
321 | * Unglobalizes a Connection |
||
322 | * |
||
323 | * @param string $connection_id Connection ID |
||
324 | * @return bool Falsey on failure. Truthy on success. |
||
325 | */ |
||
326 | abstract function unglobalize_connection( $connection_id ); |
||
327 | |||
328 | /** |
||
329 | * Returns an external URL to the Connection's profile |
||
330 | * |
||
331 | * @param string $service_name 'facebook', 'twitter', etc. |
||
332 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
333 | * @return false|string False on failure. URL on success. |
||
334 | */ |
||
335 | function get_profile_link( $service_name, $connection ) { |
||
371 | |||
372 | /** |
||
373 | * Returns a display name for the Connection |
||
374 | * |
||
375 | * @param string $service_name 'facebook', 'twitter', etc. |
||
376 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
377 | * @return string |
||
378 | */ |
||
379 | function get_display_name( $service_name, $connection ) { |
||
395 | |||
396 | /** |
||
397 | * Whether the user needs to select additional options after connecting |
||
398 | * |
||
399 | * @param string $service_name 'facebook', 'twitter', etc. |
||
400 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
401 | * @return bool |
||
402 | */ |
||
403 | function show_options_popup( $service_name, $connection ) { |
||
426 | |||
427 | /** |
||
428 | * Whether the Connection is "valid" wrt Facebook's requirements. |
||
429 | * |
||
430 | * Must be connected to a Page (not a Profile). |
||
431 | * (Also returns true if we're in the middle of the connection process) |
||
432 | * |
||
433 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
434 | * @return bool |
||
435 | */ |
||
436 | function is_valid_facebook_connection( $connection ) { |
||
444 | |||
445 | /** |
||
446 | * LinkedIn needs to be reauthenticated to use v2 of their API. |
||
447 | * If it's using LinkedIn old API, it's an 'invalid' connection |
||
448 | * |
||
449 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
450 | * @return bool |
||
451 | */ |
||
452 | function is_invalid_linkedin_connection( $connection ) { |
||
457 | |||
458 | /** |
||
459 | * Whether the Connection currently being connected |
||
460 | * |
||
461 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
462 | * @return bool |
||
463 | */ |
||
464 | function is_connecting_connection( $connection ) { |
||
469 | |||
470 | /** |
||
471 | * AJAX Handler to run connection tests on all Connections |
||
472 | * @return void |
||
473 | */ |
||
474 | function test_publicize_conns() { |
||
477 | |||
478 | /** |
||
479 | * Run connection tests on all Connections |
||
480 | * |
||
481 | * @return array { |
||
482 | * Array of connection test results. |
||
483 | * |
||
484 | * @type string 'connectionID' Connection identifier string that is unique for each connection |
||
485 | * @type string 'serviceName' Slug of the connection's service (facebook, twitter, ...) |
||
486 | * @type bool 'connectionTestPassed' Whether the connection test was successful |
||
487 | * @type string 'connectionTestMessage' Test success or error message |
||
488 | * @type bool 'userCanRefresh' Whether the user can re-authenticate their connection to the service |
||
489 | * @type string 'refreshText' Message instructing user to re-authenticate their connection to the service |
||
490 | * @type string 'refreshURL' URL, which, when visited by the user, re-authenticates their connection to the service. |
||
491 | * @type string 'unique_id' ID string representing connection |
||
492 | * } |
||
493 | */ |
||
494 | function get_publicize_conns_test_results() { |
||
560 | |||
561 | /** |
||
562 | * Run the connection test for the Connection |
||
563 | * |
||
564 | * @param string $service_name 'facebook', 'twitter', etc. |
||
565 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
566 | * @return WP_Error|true WP_Error on failure. True on success |
||
567 | */ |
||
568 | abstract function test_connection( $service_name, $connection ); |
||
569 | |||
570 | /** |
||
571 | * Retrieves current list of connections and applies filters. |
||
572 | * |
||
573 | * Retrieves current available connections and checks if the connections |
||
574 | * have already been used to share current post. Finally, the checkbox |
||
575 | * form UI fields are calculated. This function exposes connection form |
||
576 | * data directly as array so it can be retrieved for static HTML generation |
||
577 | * or JSON consumption. |
||
578 | * |
||
579 | * @since 6.7.0 |
||
580 | * |
||
581 | * @param integer $selected_post_id Optional. Post ID to query connection status for. |
||
582 | * |
||
583 | * @return array { |
||
584 | * Array of UI setup data for connection list form. |
||
585 | * |
||
586 | * @type string 'unique_id' ID string representing connection |
||
587 | * @type string 'service_name' Slug of the connection's service (facebook, twitter, ...) |
||
588 | * @type string 'service_label' Service Label (Facebook, Twitter, ...) |
||
589 | * @type string 'display_name' Connection's human-readable Username: "@jetpack" |
||
590 | * @type bool 'enabled' Default value for the connection (e.g., for a checkbox). |
||
591 | * @type bool 'done' Has this connection already been publicized to? |
||
592 | * @type bool 'toggleable' Is the user allowed to change the value for the connection? |
||
593 | * @type bool 'global' Is this connection a global one? |
||
594 | * } |
||
595 | */ |
||
596 | public function get_filtered_connection_data( $selected_post_id = null ) { |
||
731 | |||
732 | /** |
||
733 | * Checks if post has already been shared by Publicize in the past. |
||
734 | * |
||
735 | * @since 6.7.0 |
||
736 | * |
||
737 | * @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing. |
||
738 | * |
||
739 | * @return bool True if post has already been shared by Publicize, false otherwise. |
||
740 | */ |
||
741 | abstract public function post_is_done_sharing( $post_id = null ); |
||
742 | |||
743 | /** |
||
744 | * Retrieves full list of available Publicize connection services. |
||
745 | * |
||
746 | * Retrieves current available publicize service connections |
||
747 | * with associated labels and URLs. |
||
748 | * |
||
749 | * @since 6.7.0 |
||
750 | * |
||
751 | * @return array { |
||
752 | * Array of UI service connection data for all services |
||
753 | * |
||
754 | * @type string 'name' Name of service. |
||
755 | * @type string 'label' Display label for service. |
||
756 | * @type string 'url' URL for adding connection to service. |
||
757 | * } |
||
758 | */ |
||
759 | function get_available_service_data() { |
||
773 | |||
774 | /* |
||
775 | * Site Data |
||
776 | */ |
||
777 | |||
778 | function user_id() { |
||
781 | |||
782 | function blog_id() { |
||
785 | |||
786 | /* |
||
787 | * Posts |
||
788 | */ |
||
789 | |||
790 | /** |
||
791 | * Checks old and new status to see if the post should be flagged as |
||
792 | * ready to Publicize. |
||
793 | * |
||
794 | * Attached to the `transition_post_status` filter. |
||
795 | * |
||
796 | * @param string $new_status |
||
797 | * @param string $old_status |
||
798 | * @param WP_Post $post |
||
799 | * @return void |
||
800 | */ |
||
801 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
||
802 | |||
803 | /** |
||
804 | * Ensures the Post internal post-type supports `publicize` |
||
805 | * |
||
806 | * This feature support flag is used by the REST API. |
||
807 | */ |
||
808 | function add_post_type_support() { |
||
811 | |||
812 | /** |
||
813 | * Register the Publicize Gutenberg extension |
||
814 | */ |
||
815 | function register_gutenberg_extension() { |
||
826 | |||
827 | /** |
||
828 | * Can the current user access Publicize Data. |
||
829 | * |
||
830 | * @param int $post_id. 0 for general access. Post_ID for specific access. |
||
831 | * @return bool |
||
832 | */ |
||
833 | function current_user_can_access_publicize_data( $post_id = 0 ) { |
||
851 | |||
852 | /** |
||
853 | * Auth callback for the protected ->POST_MESS post_meta |
||
854 | * |
||
855 | * @param bool $allowed |
||
856 | * @param string $meta_key |
||
857 | * @param int $object_id Post ID |
||
858 | * @return bool |
||
859 | */ |
||
860 | function message_meta_auth_callback( $allowed, $meta_key, $object_id ) { |
||
863 | |||
864 | /** |
||
865 | * Registers the post_meta for use in the REST API. |
||
866 | * |
||
867 | * Registers for each post type that with `publicize` feature support. |
||
868 | */ |
||
869 | function register_post_meta() { |
||
904 | |||
905 | /** |
||
906 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
907 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
908 | * |
||
909 | * Attached to the `save_post` action. |
||
910 | * |
||
911 | * @param int $post_id |
||
912 | * @param WP_Post $post |
||
913 | * @return void |
||
914 | */ |
||
915 | function save_meta( $post_id, $post ) { |
||
1071 | |||
1072 | /** |
||
1073 | * Alters the "Post Published" message to include information about where the post |
||
1074 | * was Publicized to. |
||
1075 | * |
||
1076 | * Attached to the `post_updated_messages` filter |
||
1077 | * |
||
1078 | * @param string[] $messages |
||
1079 | * @return string[] |
||
1080 | */ |
||
1081 | public function update_published_message( $messages ) { |
||
1147 | |||
1148 | /** |
||
1149 | * Get the Connections the Post was just Publicized to. |
||
1150 | * |
||
1151 | * Only reliable just after the Post was published. |
||
1152 | * |
||
1153 | * @param int $post_id |
||
1154 | * @return string[] Array of Service display name => Connection display name |
||
1155 | */ |
||
1156 | function get_publicizing_services( $post_id ) { |
||
1178 | |||
1179 | /** |
||
1180 | * Is the post Publicize-able? |
||
1181 | * |
||
1182 | * Only valid prior to Publicizing a Post. |
||
1183 | * |
||
1184 | * @param WP_Post $post |
||
1185 | * @return bool |
||
1186 | */ |
||
1187 | function post_is_publicizeable( $post ) { |
||
1202 | |||
1203 | /** |
||
1204 | * Is a given post type Publicize-able? |
||
1205 | * |
||
1206 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
1207 | * the publicize_post_types array filter. |
||
1208 | * |
||
1209 | * @param string $post_type The post type to check. |
||
1210 | * @return bool True if the post type can be Publicized. |
||
1211 | */ |
||
1212 | function post_type_is_publicizeable( $post_type ) { |
||
1218 | |||
1219 | /** |
||
1220 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
1221 | * false if a post has already been published. |
||
1222 | * |
||
1223 | * Attached to the `publicize_checkbox_default` filter |
||
1224 | * |
||
1225 | * @param bool $checked |
||
1226 | * @param int $post_id |
||
1227 | * @param string $service_name 'facebook', 'twitter', etc |
||
1228 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
1229 | * @return bool |
||
1230 | */ |
||
1231 | function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
||
1238 | |||
1239 | /* |
||
1240 | * Util |
||
1241 | */ |
||
1242 | |||
1243 | /** |
||
1244 | * Converts a Publicize message template string into a sprintf format string |
||
1245 | * |
||
1246 | * @param string[] $args |
||
1247 | * 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
||
1248 | * ... - The template tags 'title', 'url', etc. |
||
1249 | * @return string |
||
1250 | */ |
||
1251 | protected static function build_sprintf( $args ) { |
||
1264 | } |
||
1265 | |||
1269 |
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: