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() { |
||
| 125 | |||
| 126 | function message_meta_auth_callback( $allowed, $meta_key, $object_id ) { |
||
| 129 | |||
| 130 | function register_post_meta() { |
||
| 152 | |||
| 153 | /* |
||
| 154 | * Services: Facebook, Twitter, etc. |
||
| 155 | */ |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get services for the given blog and user. |
||
| 159 | * |
||
| 160 | * Can return all available services or just the ones with an active connection. |
||
| 161 | * |
||
| 162 | * @param string $filter |
||
| 163 | * 'all' (default) - Get all services available for connecting |
||
| 164 | * 'connected' - Get all services currently connected |
||
| 165 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 166 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | abstract function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Does the given user have a connection to the service on the given blog? |
||
| 173 | * |
||
| 174 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 175 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 176 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Generates a connection URL. |
||
| 192 | * |
||
| 193 | * This is the URL, which, when visited by the user, starts the authentication |
||
| 194 | * process required to forge a connection. |
||
| 195 | * |
||
| 196 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | abstract function connect_url( $service_name ); |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Generates a Connection refresh URL. |
||
| 203 | * |
||
| 204 | * This is the URL, which, when visited by the user, re-authenticates their |
||
| 205 | * connection to the service. |
||
| 206 | * |
||
| 207 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | abstract function refresh_url( $service_name ); |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Generates a disconnection URL. |
||
| 214 | * |
||
| 215 | * This is the URL, which, when visited by the user, breaks their connection |
||
| 216 | * with the service. |
||
| 217 | * |
||
| 218 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 219 | * @param string $connection_id Connection ID |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | abstract function disconnect_url( $service_name, $connection_id ); |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns a display name for the Service |
||
| 226 | * |
||
| 227 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public static function get_service_label( $service_name ) { |
||
| 246 | |||
| 247 | /* |
||
| 248 | * Connections: For each Service, there can be multiple connections |
||
| 249 | * for a given user. For example, one user could be connected to Twitter |
||
| 250 | * as both @jetpack and as @wordpressdotcom |
||
| 251 | * |
||
| 252 | * For historical reasons, Connections are represented as an object |
||
| 253 | * on WordPress.com and as an array in Jetpack. |
||
| 254 | */ |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get the active Connections of a Service |
||
| 258 | * |
||
| 259 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 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_connections( $service_name, $_blog_id = false, $_user_id = false ); |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get a single Connection of a Service |
||
| 268 | * |
||
| 269 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 270 | * @param string $connection_id Connection ID |
||
| 271 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 272 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 273 | * @return false|object[]|array[] false if no connections exist |
||
| 274 | */ |
||
| 275 | abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the Connection ID. |
||
| 279 | * |
||
| 280 | * Note that this is different than the Connection's uniqueid. |
||
| 281 | * |
||
| 282 | * Via a quirk of history, ID is globally unique and unique_id |
||
| 283 | * is only unique per site. |
||
| 284 | * |
||
| 285 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | abstract function get_connection_id( $connection ); |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the Connection unique_id |
||
| 292 | * |
||
| 293 | * Note that this is different than the Connections ID. |
||
| 294 | * |
||
| 295 | * Via a quirk of history, ID is globally unique and unique_id |
||
| 296 | * is only unique per site. |
||
| 297 | * |
||
| 298 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | abstract function get_connection_unique_id( $connection ); |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get the Connection's Meta data |
||
| 305 | * |
||
| 306 | * @param object|array Connection |
||
| 307 | * @return array Connection Meta |
||
| 308 | */ |
||
| 309 | abstract function get_connection_meta( $connection ); |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Disconnect a Connection |
||
| 313 | * |
||
| 314 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 315 | * @param string $connection_id Connection ID |
||
| 316 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 317 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 318 | * @param bool $force_delete Whether to skip permissions checks |
||
| 319 | * @return false|void False on failure. Void on success. |
||
| 320 | */ |
||
| 321 | abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Globalizes a Connection |
||
| 325 | * |
||
| 326 | * @param string $connection_id Connection ID |
||
| 327 | * @return bool Falsey on failure. Truthy on success. |
||
| 328 | */ |
||
| 329 | abstract function globalize_connection( $connection_id ); |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Unglobalizes a Connection |
||
| 333 | * |
||
| 334 | * @param string $connection_id Connection ID |
||
| 335 | * @return bool Falsey on failure. Truthy on success. |
||
| 336 | */ |
||
| 337 | abstract function unglobalize_connection( $connection_id ); |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns an external URL to the Connection's profile |
||
| 341 | * |
||
| 342 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 343 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 344 | * @return false|string False on failure. URL on success. |
||
| 345 | */ |
||
| 346 | function get_profile_link( $service_name, $connection ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Returns a display name for the Connection |
||
| 389 | * |
||
| 390 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 391 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | function get_display_name( $service_name, $connection ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Whether the user needs to select additional options after connecting |
||
| 413 | * |
||
| 414 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 415 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | function show_options_popup( $service_name, $connection ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Whether the Connection is "valid" wrt Facebook's requirements. |
||
| 444 | * |
||
| 445 | * Must be connected to a Page (not a Profile). |
||
| 446 | * (Also returns true if we're in the middle of the connection process) |
||
| 447 | * |
||
| 448 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | function is_valid_facebook_connection( $connection ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Whether the Connection currently being connected |
||
| 462 | * |
||
| 463 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | function is_connecting_connection( $connection ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * AJAX Handler to run connection tests on all Connections |
||
| 474 | * @return void |
||
| 475 | */ |
||
| 476 | function test_publicize_conns() { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Run connection tests on all Connections |
||
| 482 | * |
||
| 483 | * @return array { |
||
| 484 | * Array of connection test results. |
||
| 485 | * |
||
| 486 | * @type string 'connectionID' Connection identifier string that is unique for each connection |
||
| 487 | * @type string 'serviceName' Slug of the connection's service (facebook, twitter, ...) |
||
| 488 | * @type bool 'connectionTestPassed' Whether the connection test was successful |
||
| 489 | * @type string 'connectionTestMessage' Test success or error message |
||
| 490 | * @type bool 'userCanRefresh' Whether the user can re-authenticate their connection to the service |
||
| 491 | * @type string 'refreshText' Message instructing user to re-authenticate their connection to the service |
||
| 492 | * @type string 'refreshURL' URL, which, when visited by the user, re-authenticates their connection to the service. |
||
| 493 | * @type string 'unique_id' ID string representing connection |
||
| 494 | * } |
||
| 495 | */ |
||
| 496 | function get_publicize_conns_test_results() { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Run the connection test for the Connection |
||
| 558 | * |
||
| 559 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 560 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 561 | * @return WP_Error|true WP_Error on failure. True on success |
||
| 562 | */ |
||
| 563 | abstract function test_connection( $service_name, $connection ); |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Retrieves current list of connections and applies filters. |
||
| 567 | * |
||
| 568 | * Retrieves current available connections and checks if the connections |
||
| 569 | * have already been used to share current post. Finally, the checkbox |
||
| 570 | * form UI fields are calculated. This function exposes connection form |
||
| 571 | * data directly as array so it can be retrieved for static HTML generation |
||
| 572 | * or JSON consumption. |
||
| 573 | * |
||
| 574 | * @since 6.7.0 |
||
| 575 | * |
||
| 576 | * @param integer $selected_post_id Optional. Post ID to query connection status for. |
||
| 577 | * |
||
| 578 | * @return array { |
||
| 579 | * Array of UI setup data for connection list form. |
||
| 580 | * |
||
| 581 | * @type string 'unique_id' ID string representing connection |
||
| 582 | * @type string 'service_name' Slug of the connection's service (facebook, twitter, ...) |
||
| 583 | * @type string 'service_label' Service Label (Facebook, Twitter, ...) |
||
| 584 | * @type string 'display_name' Connection's human-readable Username: "@jetpack" |
||
| 585 | * @type bool 'enabled' Default value for the connection (e.g., for a checkbox). |
||
| 586 | * @type bool 'done' Has this connection already been publicized to? |
||
| 587 | * @type bool 'toggleable' Is the user allowed to change the value for the connection? |
||
| 588 | * @type bool 'global' Is this connection a global one? |
||
| 589 | * } |
||
| 590 | */ |
||
| 591 | public function get_filtered_connection_data( $selected_post_id = null ) { |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Checks if post has already been shared by Publicize in the past. |
||
| 729 | * |
||
| 730 | * @since 6.7.0 |
||
| 731 | * |
||
| 732 | * @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing. |
||
| 733 | * |
||
| 734 | * @return bool True if post has already been shared by Publicize, false otherwise. |
||
| 735 | */ |
||
| 736 | abstract public function post_is_done_sharing( $post_id = null ); |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Retrieves full list of available Publicize connection services. |
||
| 740 | * |
||
| 741 | * Retrieves current available publicize service connections |
||
| 742 | * with associated labels and URLs. |
||
| 743 | * |
||
| 744 | * @since 6.7.0 |
||
| 745 | * |
||
| 746 | * @return array { |
||
| 747 | * Array of UI service connection data for all services |
||
| 748 | * |
||
| 749 | * @type string 'name' Name of service. |
||
| 750 | * @type string 'label' Display label for service. |
||
| 751 | * @type string 'url' URL for adding connection to service. |
||
| 752 | * } |
||
| 753 | */ |
||
| 754 | function get_available_service_data() { |
||
| 768 | |||
| 769 | /* |
||
| 770 | * Site Data |
||
| 771 | */ |
||
| 772 | |||
| 773 | function user_id() { |
||
| 776 | |||
| 777 | function blog_id() { |
||
| 780 | |||
| 781 | /* |
||
| 782 | * Posts |
||
| 783 | */ |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Checks old and new status to see if the post should be flagged as |
||
| 787 | * ready to Publicize. |
||
| 788 | * |
||
| 789 | * Attached to the `transition_post_status` filter. |
||
| 790 | * |
||
| 791 | * @param string $new_status |
||
| 792 | * @param string $old_status |
||
| 793 | * @param WP_Post $post |
||
| 794 | * @return void |
||
| 795 | */ |
||
| 796 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
| 800 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
| 801 | * |
||
| 802 | * Attached to the `save_post` action. |
||
| 803 | * |
||
| 804 | * @param int $post_id |
||
| 805 | * @param WP_Post $post |
||
| 806 | * @return void |
||
| 807 | */ |
||
| 808 | function save_meta( $post_id, $post ) { |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Alters the "Post Published" message to include information about where the post |
||
| 966 | * was Publicized to. |
||
| 967 | * |
||
| 968 | * Attached to the `post_updated_messages` filter |
||
| 969 | * |
||
| 970 | * @param string[] $messages |
||
| 971 | * @return string[] |
||
| 972 | */ |
||
| 973 | public function update_published_message( $messages ) { |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Get the Connections the Post was just Publicized to. |
||
| 1036 | * |
||
| 1037 | * Only reliable just after the Post was published. |
||
| 1038 | * |
||
| 1039 | * @param int $post_id |
||
| 1040 | * @return string[] Array of Service display name => Connection display name |
||
| 1041 | */ |
||
| 1042 | function get_publicizing_services( $post_id ) { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Is the post Publicize-able? |
||
| 1067 | * |
||
| 1068 | * Only valid prior to Publicizing a Post. |
||
| 1069 | * |
||
| 1070 | * @param WP_Post $post |
||
| 1071 | * @return bool |
||
| 1072 | */ |
||
| 1073 | function post_is_publicizeable( $post ) { |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Is a given post type Publicize-able? |
||
| 1091 | * |
||
| 1092 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
| 1093 | * the publicize_post_types array filter. |
||
| 1094 | * |
||
| 1095 | * @param string $post_type The post type to check. |
||
| 1096 | * @return bool True if the post type can be Publicized. |
||
| 1097 | */ |
||
| 1098 | function post_type_is_publicizeable( $post_type ) { |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
| 1107 | * false if a post has already been published. |
||
| 1108 | * |
||
| 1109 | * Attached to the `publicize_checkbox_default` filter |
||
| 1110 | * |
||
| 1111 | * @param bool $checked |
||
| 1112 | * @param int $post_id |
||
| 1113 | * @param string $service_name 'facebook', 'twitter', etc |
||
| 1114 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 1115 | * @return bool |
||
| 1116 | */ |
||
| 1117 | function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
||
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Set up Publicize meta fields for publishing post. |
||
| 1127 | * |
||
| 1128 | * Process 'publicize' REST field to setup Publicize for publishing |
||
| 1129 | * post. Sets post meta keys to enable/disable each connection for |
||
| 1130 | * the post and sets publicize title meta key if a title message |
||
| 1131 | * is provided. |
||
| 1132 | * |
||
| 1133 | * @since 6.7.0 |
||
| 1134 | * |
||
| 1135 | * @param stdClass $new_post_obj Updated post object about to be inserted view REST endpoint. |
||
| 1136 | * @param WP_REST_Request $request Request object, possibly containing 'publicize' field {@see add_publicize_rest_fields()}. |
||
| 1137 | * |
||
| 1138 | * @return WP_Post Returns the original $new_post value unchanged. |
||
| 1139 | */ |
||
| 1140 | public function process_publicize_from_rest( $new_post_obj, $request ) { |
||
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Checks if a connection should be shared to. |
||
| 1185 | * |
||
| 1186 | * Checks $connection_id against $connections_array to see if the connection associated |
||
| 1187 | * with $connection_id should be shared to. Will return true if $connection_id is in the |
||
| 1188 | * array and 'should_share' property is set to true, and will default to false otherwise. |
||
| 1189 | * |
||
| 1190 | * @since 6.7.0 |
||
| 1191 | * |
||
| 1192 | * @param array $connections_array 'connections' from 'publicize' REST field {@see add_publicize_rest_fields()}. |
||
| 1193 | * @param string $connection_id Connection identifier string that is unique for each connection. |
||
| 1194 | * @return boolean True if connection should be shared to, false otherwise. |
||
| 1195 | */ |
||
| 1196 | private function connection_should_share( $connections_array, $connection_id ) { |
||
| 1206 | |||
| 1207 | /* |
||
| 1208 | * Util |
||
| 1209 | */ |
||
| 1210 | |||
| 1211 | /** |
||
| 1212 | * Converts a Publicize message template string into a sprintf format string |
||
| 1213 | * |
||
| 1214 | * @param string[] $args |
||
| 1215 | * 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
||
| 1216 | * ... - The template tags 'title', 'url', etc. |
||
| 1217 | * @return string |
||
| 1218 | */ |
||
| 1219 | protected static function build_sprintf( $args ) { |
||
| 1232 | } |
||
| 1233 | |||
| 1248 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: