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 | /** |
||
| 144 | * Does the given user have a connection to the service on the given blog? |
||
| 145 | * |
||
| 146 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 147 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 148 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Generates a connection URL. |
||
| 164 | * |
||
| 165 | * This is the URL, which, when visited by the user, starts the authentication |
||
| 166 | * process required to forge a connection. |
||
| 167 | * |
||
| 168 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | abstract function connect_url( $service_name ); |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Generates a Connection refresh URL. |
||
| 175 | * |
||
| 176 | * This is the URL, which, when visited by the user, re-authenticates their |
||
| 177 | * connection to the service. |
||
| 178 | * |
||
| 179 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | abstract function refresh_url( $service_name ); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Generates a disconnection URL. |
||
| 186 | * |
||
| 187 | * This is the URL, which, when visited by the user, breaks their connection |
||
| 188 | * with the service. |
||
| 189 | * |
||
| 190 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 191 | * @param string $connection_id Connection ID |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | abstract function disconnect_url( $service_name, $connection_id ); |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns a display name for the Service |
||
| 198 | * |
||
| 199 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public static function get_service_label( $service_name ) { |
||
| 218 | |||
| 219 | /* |
||
| 220 | * Connections: For each Service, there can be multiple connections |
||
| 221 | * for a given user. For example, one user could be connected to Twitter |
||
| 222 | * as both @jetpack and as @wordpressdotcom |
||
| 223 | * |
||
| 224 | * For historical reasons, Connections are represented as an object |
||
| 225 | * on WordPress.com and as an array in Jetpack. |
||
| 226 | */ |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the active Connections of a Service |
||
| 230 | * |
||
| 231 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 232 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 233 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 234 | * @return false|object[]|array[] false if no connections exist |
||
| 235 | */ |
||
| 236 | abstract function get_connections( $service_name, $_blog_id = false, $_user_id = false ); |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get a single Connection of a Service |
||
| 240 | * |
||
| 241 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 242 | * @param string $connection_id Connection ID |
||
| 243 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 244 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 245 | * @return false|object[]|array[] false if no connections exist |
||
| 246 | */ |
||
| 247 | abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get the Connection ID. |
||
| 251 | * |
||
| 252 | * Note that this is different than the Connection's uniqueid. |
||
| 253 | * |
||
| 254 | * Via a quirk of history, ID is globally unique and unique_id |
||
| 255 | * is only unique per site. |
||
| 256 | * |
||
| 257 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | abstract function get_connection_id( $connection ); |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the Connection unique_id |
||
| 264 | * |
||
| 265 | * Note that this is different than the Connections ID. |
||
| 266 | * |
||
| 267 | * Via a quirk of history, ID is globally unique and unique_id |
||
| 268 | * is only unique per site. |
||
| 269 | * |
||
| 270 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | abstract function get_connection_unique_id( $connection ); |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the Connection's Meta data |
||
| 277 | * |
||
| 278 | * @param object|array Connection |
||
| 279 | * @return array Connection Meta |
||
| 280 | */ |
||
| 281 | abstract function get_connection_meta( $connection ); |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Disconnect a Connection |
||
| 285 | * |
||
| 286 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 287 | * @param string $connection_id Connection ID |
||
| 288 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 289 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 290 | * @param bool $force_delete Whether to skip permissions checks |
||
| 291 | * @return false|void False on failure. Void on success. |
||
| 292 | */ |
||
| 293 | abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Globalizes a Connection |
||
| 297 | * |
||
| 298 | * @param string $connection_id Connection ID |
||
| 299 | * @return bool Falsey on failure. Truthy on success. |
||
| 300 | */ |
||
| 301 | abstract function globalize_connection( $connection_id ); |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Unglobalizes a Connection |
||
| 305 | * |
||
| 306 | * @param string $connection_id Connection ID |
||
| 307 | * @return bool Falsey on failure. Truthy on success. |
||
| 308 | */ |
||
| 309 | abstract function unglobalize_connection( $connection_id ); |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Returns an external URL to the Connection's profile |
||
| 313 | * |
||
| 314 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 315 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 316 | * @return false|string False on failure. URL on success. |
||
| 317 | */ |
||
| 318 | function get_profile_link( $service_name, $connection ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns a display name for the Connection |
||
| 361 | * |
||
| 362 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 363 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | function get_display_name( $service_name, $connection ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Whether the user needs to select additional options after connecting |
||
| 385 | * |
||
| 386 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 387 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | function show_options_popup( $service_name, $connection ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Whether the Connection is "valid" wrt Facebook's requirements. |
||
| 416 | * |
||
| 417 | * Must be connected to a Page (not a Profile). |
||
| 418 | * (Also returns true if we're in the middle of the connection process) |
||
| 419 | * |
||
| 420 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | function is_valid_facebook_connection( $connection ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Whether the Connection currently being connected |
||
| 434 | * |
||
| 435 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | function is_connecting_connection( $connection ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * AJAX Handler to run connection tests on all Connections |
||
| 446 | * @return void |
||
| 447 | */ |
||
| 448 | function test_publicize_conns() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Run connection tests on all Connections |
||
| 454 | * |
||
| 455 | * @return array { |
||
| 456 | * Array of connection test results. |
||
| 457 | * |
||
| 458 | * @type string 'connectionID' Connection identifier string that is unique for each connection |
||
| 459 | * @type string 'serviceName' Slug of the connection's service (facebook, twitter, ...) |
||
| 460 | * @type bool 'connectionTestPassed' Whether the connection test was successful |
||
| 461 | * @type string 'connectionTestMessage' Test success or error message |
||
| 462 | * @type bool 'userCanRefresh' Whether the user can re-authenticate their connection to the service |
||
| 463 | * @type string 'refreshText' Message instructing user to re-authenticate their connection to the service |
||
| 464 | * @type string 'refreshURL' URL, which, when visited by the user, re-authenticates their connection to the service. |
||
| 465 | * @type string 'unique_id' ID string representing connection |
||
| 466 | * } |
||
| 467 | */ |
||
| 468 | function get_publicize_conns_test_results() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Run the connection test for the Connection |
||
| 530 | * |
||
| 531 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 532 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 533 | * @return WP_Error|true WP_Error on failure. True on success |
||
| 534 | */ |
||
| 535 | abstract function test_connection( $service_name, $connection ); |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Retrieves current list of connections and applies filters. |
||
| 539 | * |
||
| 540 | * Retrieves current available connections and checks if the connections |
||
| 541 | * have already been used to share current post. Finally, the checkbox |
||
| 542 | * form UI fields are calculated. This function exposes connection form |
||
| 543 | * data directly as array so it can be retrieved for static HTML generation |
||
| 544 | * or JSON consumption. |
||
| 545 | * |
||
| 546 | * @since 6.7.0 |
||
| 547 | * |
||
| 548 | * @param integer $selected_post_id Optional. Post ID to query connection status for. |
||
| 549 | * |
||
| 550 | * @return array { |
||
| 551 | * Array of UI setup data for connection list form. |
||
| 552 | * |
||
| 553 | * @type string 'unique_id' ID string representing connection |
||
| 554 | * @type string 'service_name' Slug of the connection's service (facebook, twitter, ...) |
||
| 555 | * @type string 'service_label' Service Label (Facebook, Twitter, ...) |
||
| 556 | * @type string 'display_name' Connection's human-readable Username: "@jetpack" |
||
| 557 | * @type bool 'enabled' Default value for the connection (e.g., for a checkbox). |
||
| 558 | * @type bool 'done' Has this connection already been publicized to? |
||
| 559 | * @type bool 'toggleable' Is the user allowed to change the value for the connection? |
||
| 560 | * @type bool 'global' Is this connection a global one? |
||
| 561 | * } |
||
| 562 | */ |
||
| 563 | public function get_filtered_connection_data( $selected_post_id = null ) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Checks if post has already been shared by Publicize in the past. |
||
| 701 | * |
||
| 702 | * @since 6.7.0 |
||
| 703 | * |
||
| 704 | * @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing. |
||
| 705 | * |
||
| 706 | * @return bool True if post has already been shared by Publicize, false otherwise. |
||
| 707 | */ |
||
| 708 | abstract public function post_is_done_sharing( $post_id = null ); |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Retrieves full list of available Publicize connection services. |
||
| 712 | * |
||
| 713 | * Retrieves current available publicize service connections |
||
| 714 | * with associated labels and URLs. |
||
| 715 | * |
||
| 716 | * @since 6.7.0 |
||
| 717 | * |
||
| 718 | * @return array { |
||
| 719 | * Array of UI service connection data for all services |
||
| 720 | * |
||
| 721 | * @type string 'name' Name of service. |
||
| 722 | * @type string 'label' Display label for service. |
||
| 723 | * @type string 'url' URL for adding connection to service. |
||
| 724 | * } |
||
| 725 | */ |
||
| 726 | function get_available_service_data() { |
||
| 740 | |||
| 741 | /* |
||
| 742 | * Site Data |
||
| 743 | */ |
||
| 744 | |||
| 745 | function user_id() { |
||
| 748 | |||
| 749 | function blog_id() { |
||
| 752 | |||
| 753 | /* |
||
| 754 | * Posts |
||
| 755 | */ |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Checks old and new status to see if the post should be flagged as |
||
| 759 | * ready to Publicize. |
||
| 760 | * |
||
| 761 | * Attached to the `transition_post_status` filter. |
||
| 762 | * |
||
| 763 | * @param string $new_status |
||
| 764 | * @param string $old_status |
||
| 765 | * @param WP_Post $post |
||
| 766 | * @return void |
||
| 767 | */ |
||
| 768 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Ensures the Post internal post-type supports `publicize` |
||
| 772 | * |
||
| 773 | * This feature support flag is used by the REST API. |
||
| 774 | */ |
||
| 775 | function add_post_type_support() { |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Register the Publicize Gutenberg extension |
||
| 781 | */ |
||
| 782 | function register_gutenberg_extension() { |
||
| 783 | jetpack_register_plugin( 'publicize', array( 'callback' => array( $this, 'get_extension_availability' ) ) ); |
||
| 784 | } |
||
| 785 | |||
| 786 | function get_extension_availability() { |
||
| 787 | $object_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; |
||
| 788 | |||
| 789 | if ( ! $this->current_user_can_access_publicize_data( $object_id ) ) { |
||
| 790 | return array( 'available' => false, 'unavailable_reason' => 'unauthorized' ); |
||
| 791 | } |
||
| 792 | |||
| 793 | return array( 'available' => true ); |
||
| 794 | } |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Can the current user access Publicize Data. |
||
| 798 | * |
||
| 799 | * @param int $post_id. 0 for general access. Post_ID for specific access. |
||
| 800 | * @return bool |
||
| 801 | */ |
||
| 802 | function current_user_can_access_publicize_data( $post_id = 0 ) { |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Auth callback for the protected ->POST_MESS post_meta |
||
| 823 | * |
||
| 824 | * @param bool $allowed |
||
| 825 | * @param string $meta_key |
||
| 826 | * @param int $object_id Post ID |
||
| 827 | * @return bool |
||
| 828 | */ |
||
| 829 | function message_meta_auth_callback( $allowed, $meta_key, $object_id ) { |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Registers the ->POST_MESS post_meta for use in the REST API. |
||
| 835 | * |
||
| 836 | * Registers for each post type that with `publicize` feature support. |
||
| 837 | */ |
||
| 838 | function register_post_meta() { |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
| 863 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
| 864 | * |
||
| 865 | * Attached to the `save_post` action. |
||
| 866 | * |
||
| 867 | * @param int $post_id |
||
| 868 | * @param WP_Post $post |
||
| 869 | * @return void |
||
| 870 | */ |
||
| 871 | function save_meta( $post_id, $post ) { |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Alters the "Post Published" message to include information about where the post |
||
| 1029 | * was Publicized to. |
||
| 1030 | * |
||
| 1031 | * Attached to the `post_updated_messages` filter |
||
| 1032 | * |
||
| 1033 | * @param string[] $messages |
||
| 1034 | * @return string[] |
||
| 1035 | */ |
||
| 1036 | public function update_published_message( $messages ) { |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Get the Connections the Post was just Publicized to. |
||
| 1099 | * |
||
| 1100 | * Only reliable just after the Post was published. |
||
| 1101 | * |
||
| 1102 | * @param int $post_id |
||
| 1103 | * @return string[] Array of Service display name => Connection display name |
||
| 1104 | */ |
||
| 1105 | function get_publicizing_services( $post_id ) { |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * Is the post Publicize-able? |
||
| 1130 | * |
||
| 1131 | * Only valid prior to Publicizing a Post. |
||
| 1132 | * |
||
| 1133 | * @param WP_Post $post |
||
| 1134 | * @return bool |
||
| 1135 | */ |
||
| 1136 | function post_is_publicizeable( $post ) { |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Is a given post type Publicize-able? |
||
| 1154 | * |
||
| 1155 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
| 1156 | * the publicize_post_types array filter. |
||
| 1157 | * |
||
| 1158 | * @param string $post_type The post type to check. |
||
| 1159 | * @return bool True if the post type can be Publicized. |
||
| 1160 | */ |
||
| 1161 | function post_type_is_publicizeable( $post_type ) { |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
| 1170 | * false if a post has already been published. |
||
| 1171 | * |
||
| 1172 | * Attached to the `publicize_checkbox_default` filter |
||
| 1173 | * |
||
| 1174 | * @param bool $checked |
||
| 1175 | * @param int $post_id |
||
| 1176 | * @param string $service_name 'facebook', 'twitter', etc |
||
| 1177 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 1178 | * @return bool |
||
| 1179 | */ |
||
| 1180 | function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
||
| 1187 | |||
| 1188 | /* |
||
| 1189 | * Util |
||
| 1190 | */ |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Converts a Publicize message template string into a sprintf format string |
||
| 1194 | * |
||
| 1195 | * @param string[] $args |
||
| 1196 | * 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
||
| 1197 | * ... - The template tags 'title', 'url', etc. |
||
| 1198 | * @return string |
||
| 1199 | */ |
||
| 1200 | protected static function build_sprintf( $args ) { |
||
| 1213 | } |
||
| 1214 | |||
| 1215 | function publicize_calypso_url() { |
||
| 1229 |
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: