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() { |
||
126 | |||
127 | /* |
||
128 | * Services: Facebook, Twitter, etc. |
||
129 | */ |
||
130 | |||
131 | /** |
||
132 | * Get services for the given blog and user. |
||
133 | * |
||
134 | * Can return all available services or just the ones with an active connection. |
||
135 | * |
||
136 | * @param string $filter |
||
137 | * 'all' (default) - Get all services available for connecting |
||
138 | * 'connected' - Get all services currently connected |
||
139 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
140 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
141 | * @return array |
||
142 | */ |
||
143 | abstract function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
||
144 | |||
145 | /** |
||
146 | * Does the given user have a connection to the service on the given blog? |
||
147 | * |
||
148 | * @param string $service_name 'facebook', 'twitter', etc. |
||
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 bool |
||
152 | */ |
||
153 | function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
||
163 | |||
164 | /** |
||
165 | * Generates a connection URL. |
||
166 | * |
||
167 | * This is the URL, which, when visited by the user, starts the authentication |
||
168 | * process required to forge a connection. |
||
169 | * |
||
170 | * @param string $service_name 'facebook', 'twitter', etc. |
||
171 | * @return string |
||
172 | */ |
||
173 | abstract function connect_url( $service_name ); |
||
174 | |||
175 | /** |
||
176 | * Generates a Connection refresh URL. |
||
177 | * |
||
178 | * This is the URL, which, when visited by the user, re-authenticates their |
||
179 | * connection to the service. |
||
180 | * |
||
181 | * @param string $service_name 'facebook', 'twitter', etc. |
||
182 | * @return string |
||
183 | */ |
||
184 | abstract function refresh_url( $service_name ); |
||
185 | |||
186 | /** |
||
187 | * Generates a disconnection URL. |
||
188 | * |
||
189 | * This is the URL, which, when visited by the user, breaks their connection |
||
190 | * with the service. |
||
191 | * |
||
192 | * @param string $service_name 'facebook', 'twitter', etc. |
||
193 | * @param string $connection_id Connection ID |
||
194 | * @return string |
||
195 | */ |
||
196 | abstract function disconnect_url( $service_name, $connection_id ); |
||
197 | |||
198 | /** |
||
199 | * Returns a display name for the Service |
||
200 | * |
||
201 | * @param string $service_name 'facebook', 'twitter', etc. |
||
202 | * @return string |
||
203 | */ |
||
204 | public static function get_service_label( $service_name ) { |
||
220 | |||
221 | /* |
||
222 | * Connections: For each Service, there can be multiple connections |
||
223 | * for a given user. For example, one user could be connected to Twitter |
||
224 | * as both @jetpack and as @wordpressdotcom |
||
225 | * |
||
226 | * For historical reasons, Connections are represented as an object |
||
227 | * on WordPress.com and as an array in Jetpack. |
||
228 | */ |
||
229 | |||
230 | /** |
||
231 | * Get the active Connections of a Service |
||
232 | * |
||
233 | * @param string $service_name 'facebook', 'twitter', etc. |
||
234 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
235 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
236 | * @return false|object[]|array[] false if no connections exist |
||
237 | */ |
||
238 | abstract function get_connections( $service_name, $_blog_id = false, $_user_id = false ); |
||
239 | |||
240 | /** |
||
241 | * Get a single Connection of a Service |
||
242 | * |
||
243 | * @param string $service_name 'facebook', 'twitter', etc. |
||
244 | * @param string $connection_id Connection ID |
||
245 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
246 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
247 | * @return false|object[]|array[] false if no connections exist |
||
248 | */ |
||
249 | abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
||
250 | |||
251 | /** |
||
252 | * Get the Connection ID. |
||
253 | * |
||
254 | * Note that this is different than the Connection's uniqueid. |
||
255 | * |
||
256 | * Via a quirk of history, ID is globally unique and unique_id |
||
257 | * is only unique per site. |
||
258 | * |
||
259 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
260 | * @return string |
||
261 | */ |
||
262 | abstract function get_connection_id( $connection ); |
||
263 | |||
264 | /** |
||
265 | * Get the Connection unique_id |
||
266 | * |
||
267 | * Note that this is different than the Connections ID. |
||
268 | * |
||
269 | * Via a quirk of history, ID is globally unique and unique_id |
||
270 | * is only unique per site. |
||
271 | * |
||
272 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
273 | * @return string |
||
274 | */ |
||
275 | abstract function get_connection_unique_id( $connection ); |
||
276 | |||
277 | /** |
||
278 | * Get the Connection's Meta data |
||
279 | * |
||
280 | * @param object|array Connection |
||
281 | * @return array Connection Meta |
||
282 | */ |
||
283 | abstract function get_connection_meta( $connection ); |
||
284 | |||
285 | /** |
||
286 | * Disconnect a Connection |
||
287 | * |
||
288 | * @param string $service_name 'facebook', 'twitter', etc. |
||
289 | * @param string $connection_id Connection ID |
||
290 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
291 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
292 | * @param bool $force_delete Whether to skip permissions checks |
||
293 | * @return false|void False on failure. Void on success. |
||
294 | */ |
||
295 | abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
||
296 | |||
297 | /** |
||
298 | * Globalizes a Connection |
||
299 | * |
||
300 | * @param string $connection_id Connection ID |
||
301 | * @return bool Falsey on failure. Truthy on success. |
||
302 | */ |
||
303 | abstract function globalize_connection( $connection_id ); |
||
304 | |||
305 | /** |
||
306 | * Unglobalizes a Connection |
||
307 | * |
||
308 | * @param string $connection_id Connection ID |
||
309 | * @return bool Falsey on failure. Truthy on success. |
||
310 | */ |
||
311 | abstract function unglobalize_connection( $connection_id ); |
||
312 | |||
313 | /** |
||
314 | * Returns an external URL to the Connection's profile |
||
315 | * |
||
316 | * @param string $service_name 'facebook', 'twitter', etc. |
||
317 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
318 | * @return false|string False on failure. URL on success. |
||
319 | */ |
||
320 | function get_profile_link( $service_name, $connection ) { |
||
360 | |||
361 | /** |
||
362 | * Returns a display name for the Connection |
||
363 | * |
||
364 | * @param string $service_name 'facebook', 'twitter', etc. |
||
365 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
366 | * @return string |
||
367 | */ |
||
368 | function get_display_name( $service_name, $connection ) { |
||
384 | |||
385 | /** |
||
386 | * Whether the user needs to select additional options after connecting |
||
387 | * |
||
388 | * @param string $service_name 'facebook', 'twitter', etc. |
||
389 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
390 | * @return bool |
||
391 | */ |
||
392 | function show_options_popup( $service_name, $connection ) { |
||
415 | |||
416 | /** |
||
417 | * Whether the Connection is "valid" wrt Facebook's requirements. |
||
418 | * |
||
419 | * Must be connected to a Page (not a Profile). |
||
420 | * (Also returns true if we're in the middle of the connection process) |
||
421 | * |
||
422 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
423 | * @return bool |
||
424 | */ |
||
425 | function is_valid_facebook_connection( $connection ) { |
||
433 | |||
434 | /** |
||
435 | * Whether the Connection currently being connected |
||
436 | * |
||
437 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
438 | * @return bool |
||
439 | */ |
||
440 | function is_connecting_connection( $connection ) { |
||
445 | |||
446 | /** |
||
447 | * AJAX Handler to run connection tests on all Connections |
||
448 | * @return void |
||
449 | */ |
||
450 | function test_publicize_conns() { |
||
453 | |||
454 | /** |
||
455 | * Run connection tests on all Connections |
||
456 | * |
||
457 | * @return array { |
||
458 | * Array of connection test results. |
||
459 | * |
||
460 | * @type string 'connectionID' Connection identifier string that is unique for each connection |
||
461 | * @type string 'serviceName' Slug of the connection's service (facebook, twitter, ...) |
||
462 | * @type bool 'connectionTestPassed' Whether the connection test was successful |
||
463 | * @type string 'connectionTestMessage' Test success or error message |
||
464 | * @type bool 'userCanRefresh' Whether the user can re-authenticate their connection to the service |
||
465 | * @type string 'refreshText' Message instructing user to re-authenticate their connection to the service |
||
466 | * @type string 'refreshURL' URL, which, when visited by the user, re-authenticates their connection to the service. |
||
467 | * @type string 'unique_id' ID string representing connection |
||
468 | * } |
||
469 | */ |
||
470 | function get_publicize_conns_test_results() { |
||
529 | |||
530 | /** |
||
531 | * Run the connection test for the Connection |
||
532 | * |
||
533 | * @param string $service_name 'facebook', 'twitter', etc. |
||
534 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
535 | * @return WP_Error|true WP_Error on failure. True on success |
||
536 | */ |
||
537 | abstract function test_connection( $service_name, $connection ); |
||
538 | |||
539 | /** |
||
540 | * Retrieves current list of connections and applies filters. |
||
541 | * |
||
542 | * Retrieves current available connections and checks if the connections |
||
543 | * have already been used to share current post. Finally, the checkbox |
||
544 | * form UI fields are calculated. This function exposes connection form |
||
545 | * data directly as array so it can be retrieved for static HTML generation |
||
546 | * or JSON consumption. |
||
547 | * |
||
548 | * @since 6.7.0 |
||
549 | * |
||
550 | * @param integer $selected_post_id Optional. Post ID to query connection status for. |
||
551 | * |
||
552 | * @return array { |
||
553 | * Array of UI setup data for connection list form. |
||
554 | * |
||
555 | * @type string 'unique_id' ID string representing connection |
||
556 | * @type string 'service_name' Slug of the connection's service (facebook, twitter, ...) |
||
557 | * @type string 'service_label' Service Label (Facebook, Twitter, ...) |
||
558 | * @type string 'display_name' Connection's human-readable Username: "@jetpack" |
||
559 | * @type bool 'enabled' Default value for the connection (e.g., for a checkbox). |
||
560 | * @type bool 'done' Has this connection already been publicized to? |
||
561 | * @type bool 'toggleable' Is the user allowed to change the value for the connection? |
||
562 | * @type bool 'global' Is this connection a global one? |
||
563 | * } |
||
564 | */ |
||
565 | public function get_filtered_connection_data( $selected_post_id = null ) { |
||
700 | |||
701 | /** |
||
702 | * Checks if post has already been shared by Publicize in the past. |
||
703 | * |
||
704 | * @since 6.7.0 |
||
705 | * |
||
706 | * @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing. |
||
707 | * |
||
708 | * @return bool True if post has already been shared by Publicize, false otherwise. |
||
709 | */ |
||
710 | abstract public function post_is_done_sharing( $post_id = null ); |
||
711 | |||
712 | /** |
||
713 | * Retrieves full list of available Publicize connection services. |
||
714 | * |
||
715 | * Retrieves current available publicize service connections |
||
716 | * with associated labels and URLs. |
||
717 | * |
||
718 | * @since 6.7.0 |
||
719 | * |
||
720 | * @return array { |
||
721 | * Array of UI service connection data for all services |
||
722 | * |
||
723 | * @type string 'name' Name of service. |
||
724 | * @type string 'label' Display label for service. |
||
725 | * @type string 'url' URL for adding connection to service. |
||
726 | * } |
||
727 | */ |
||
728 | function get_available_service_data() { |
||
742 | |||
743 | /* |
||
744 | * Site Data |
||
745 | */ |
||
746 | |||
747 | function user_id() { |
||
750 | |||
751 | function blog_id() { |
||
754 | |||
755 | /* |
||
756 | * Posts |
||
757 | */ |
||
758 | |||
759 | /** |
||
760 | * Checks old and new status to see if the post should be flagged as |
||
761 | * ready to Publicize. |
||
762 | * |
||
763 | * Attached to the `transition_post_status` filter. |
||
764 | * |
||
765 | * @param string $new_status |
||
766 | * @param string $old_status |
||
767 | * @param WP_Post $post |
||
768 | * @return void |
||
769 | */ |
||
770 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
||
771 | |||
772 | /** |
||
773 | * Ensures the Post internal post-type supports `publicize` |
||
774 | * |
||
775 | * This feature support flag is used by the REST API. |
||
776 | */ |
||
777 | function add_post_type_support() { |
||
780 | |||
781 | /** |
||
782 | * Can the current user access Publicize Data. |
||
783 | * |
||
784 | * @param int $post_id. 0 for general access. Post_ID for specific access. |
||
785 | * @return bool |
||
786 | */ |
||
787 | function current_user_can_access_publicize_data( $post_id = 0 ) { |
||
805 | |||
806 | /** |
||
807 | * Auth callback for the protected ->POST_MESS post_meta |
||
808 | * |
||
809 | * @param bool $allowed |
||
810 | * @param string $meta_key |
||
811 | * @param int $object_id Post ID |
||
812 | * @return bool |
||
813 | */ |
||
814 | function message_meta_auth_callback( $allowed, $meta_key, $object_id ) { |
||
817 | |||
818 | /** |
||
819 | * Registers the ->POST_MESS post_meta for use in the REST API. |
||
820 | * |
||
821 | * Registers for each post type that with `publicize` feature support. |
||
822 | */ |
||
823 | function register_post_meta() { |
||
845 | |||
846 | /** |
||
847 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
848 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
849 | * |
||
850 | * Attached to the `save_post` action. |
||
851 | * |
||
852 | * @param int $post_id |
||
853 | * @param WP_Post $post |
||
854 | * @return void |
||
855 | */ |
||
856 | function save_meta( $post_id, $post ) { |
||
1011 | |||
1012 | /** |
||
1013 | * Alters the "Post Published" message to include information about where the post |
||
1014 | * was Publicized to. |
||
1015 | * |
||
1016 | * Attached to the `post_updated_messages` filter |
||
1017 | * |
||
1018 | * @param string[] $messages |
||
1019 | * @return string[] |
||
1020 | */ |
||
1021 | public function update_published_message( $messages ) { |
||
1081 | |||
1082 | /** |
||
1083 | * Get the Connections the Post was just Publicized to. |
||
1084 | * |
||
1085 | * Only reliable just after the Post was published. |
||
1086 | * |
||
1087 | * @param int $post_id |
||
1088 | * @return string[] Array of Service display name => Connection display name |
||
1089 | */ |
||
1090 | function get_publicizing_services( $post_id ) { |
||
1112 | |||
1113 | /** |
||
1114 | * Is the post Publicize-able? |
||
1115 | * |
||
1116 | * Only valid prior to Publicizing a Post. |
||
1117 | * |
||
1118 | * @param WP_Post $post |
||
1119 | * @return bool |
||
1120 | */ |
||
1121 | function post_is_publicizeable( $post ) { |
||
1136 | |||
1137 | /** |
||
1138 | * Is a given post type Publicize-able? |
||
1139 | * |
||
1140 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
1141 | * the publicize_post_types array filter. |
||
1142 | * |
||
1143 | * @param string $post_type The post type to check. |
||
1144 | * @return bool True if the post type can be Publicized. |
||
1145 | */ |
||
1146 | function post_type_is_publicizeable( $post_type ) { |
||
1152 | |||
1153 | /** |
||
1154 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
1155 | * false if a post has already been published. |
||
1156 | * |
||
1157 | * Attached to the `publicize_checkbox_default` filter |
||
1158 | * |
||
1159 | * @param bool $checked |
||
1160 | * @param int $post_id |
||
1161 | * @param string $service_name 'facebook', 'twitter', etc |
||
1162 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
1163 | * @return bool |
||
1164 | */ |
||
1165 | function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
||
1172 | |||
1173 | /** |
||
1174 | * Set up Publicize meta fields for publishing post. |
||
1175 | * |
||
1176 | * Process 'publicize' REST field to setup Publicize for publishing |
||
1177 | * post. Sets post meta keys to enable/disable each connection for |
||
1178 | * the post and sets publicize title meta key if a title message |
||
1179 | * is provided. |
||
1180 | * |
||
1181 | * @since 6.7.0 |
||
1182 | * |
||
1183 | * @param stdClass $new_post_obj Updated post object about to be inserted view REST endpoint. |
||
1184 | * @param WP_REST_Request $request Request object, possibly containing 'publicize' field {@see add_publicize_rest_fields()}. |
||
1185 | * |
||
1186 | * @return WP_Post Returns the original $new_post value unchanged. |
||
1187 | */ |
||
1188 | public function process_publicize_from_rest( $new_post_obj, $request ) { |
||
1230 | |||
1231 | /** |
||
1232 | * Checks if a connection should be shared to. |
||
1233 | * |
||
1234 | * Checks $connection_id against $connections_array to see if the connection associated |
||
1235 | * with $connection_id should be shared to. Will return true if $connection_id is in the |
||
1236 | * array and 'should_share' property is set to true, and will default to false otherwise. |
||
1237 | * |
||
1238 | * @since 6.7.0 |
||
1239 | * |
||
1240 | * @param array $connections_array 'connections' from 'publicize' REST field {@see add_publicize_rest_fields()}. |
||
1241 | * @param string $connection_id Connection identifier string that is unique for each connection. |
||
1242 | * @return boolean True if connection should be shared to, false otherwise. |
||
1243 | */ |
||
1244 | private function connection_should_share( $connections_array, $connection_id ) { |
||
1254 | |||
1255 | /* |
||
1256 | * Util |
||
1257 | */ |
||
1258 | |||
1259 | /** |
||
1260 | * Converts a Publicize message template string into a sprintf format string |
||
1261 | * |
||
1262 | * @param string[] $args |
||
1263 | * 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
||
1264 | * ... - The template tags 'title', 'url', etc. |
||
1265 | * @return string |
||
1266 | */ |
||
1267 | protected static function build_sprintf( $args ) { |
||
1280 | } |
||
1281 | |||
1296 |
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: