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() { |
||
| 49 | $this->default_message = self::build_sprintf( array( |
||
| 50 | /** |
||
| 51 | * Filter the default Publicize message. |
||
| 52 | * |
||
| 53 | * @module publicize |
||
| 54 | * |
||
| 55 | * @since 2.0.0 |
||
| 56 | * |
||
| 57 | * @param string $this->default_message Publicize's default message. Default is the post title. |
||
| 58 | */ |
||
| 59 | apply_filters( 'wpas_default_message', $this->default_message ), |
||
| 60 | 'title', |
||
| 61 | 'url', |
||
| 62 | ) ); |
||
| 63 | |||
| 64 | $this->default_prefix = self::build_sprintf( array( |
||
| 65 | /** |
||
| 66 | * Filter the message prepended to the Publicize custom message. |
||
| 67 | * |
||
| 68 | * @module publicize |
||
| 69 | * |
||
| 70 | * @since 2.0.0 |
||
| 71 | * |
||
| 72 | * @param string $this->default_prefix String prepended to the Publicize custom message. |
||
| 73 | */ |
||
| 74 | apply_filters( 'wpas_default_prefix', $this->default_prefix ), |
||
| 75 | 'url', |
||
| 76 | ) ); |
||
| 77 | |||
| 78 | $this->default_suffix = self::build_sprintf( array( |
||
| 79 | /** |
||
| 80 | * Filter the message appended to the Publicize custom message. |
||
| 81 | * |
||
| 82 | * @module publicize |
||
| 83 | * |
||
| 84 | * @since 2.0.0 |
||
| 85 | * |
||
| 86 | * @param string $this->default_suffix String appended to the Publicize custom message. |
||
| 87 | */ |
||
| 88 | apply_filters( 'wpas_default_suffix', $this->default_suffix ), |
||
| 89 | 'url', |
||
| 90 | ) ); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Filter the capability to change global Publicize connection options. |
||
| 94 | * |
||
| 95 | * All users with this cap can un-globalize all other global connections, and globalize any of their own |
||
| 96 | * Globalized connections cannot be unselected by users without this capability when publishing. |
||
| 97 | * |
||
| 98 | * @module publicize |
||
| 99 | * |
||
| 100 | * @since 2.2.1 |
||
| 101 | * |
||
| 102 | * @param string $this->GLOBAL_CAP default capability in control of global Publicize connection options. Default to edit_others_posts. |
||
| 103 | */ |
||
| 104 | $this->GLOBAL_CAP = apply_filters( 'jetpack_publicize_global_connections_cap', $this->GLOBAL_CAP ); |
||
| 105 | |||
| 106 | // stage 1 and 2 of 3-stage Publicize. Flag for Publicize on creation, save meta, |
||
| 107 | // then check meta and publicize based on that. stage 3 implemented on wpcom |
||
| 108 | add_action( 'transition_post_status', array( $this, 'flag_post_for_publicize' ), 10, 3 ); |
||
| 109 | add_action( 'save_post', array( &$this, 'save_meta' ), 20, 2 ); |
||
| 110 | |||
| 111 | // Default checkbox state for each Connection |
||
| 112 | add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 4 ); |
||
| 113 | |||
| 114 | // Alter the "Post Publish" admin notice to mention the Connections we Publicized to. |
||
| 115 | add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 20, 1 ); |
||
| 116 | |||
| 117 | // Connection test callback |
||
| 118 | add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) ); |
||
| 119 | } |
||
| 120 | |||
| 121 | /* |
||
| 122 | * Services: Facebook, Twitter, etc. |
||
| 123 | */ |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get services for the given blog and user. |
||
| 127 | * |
||
| 128 | * Can return all available services or just the ones with an active connection. |
||
| 129 | * |
||
| 130 | * @param string $filter |
||
| 131 | * 'all' (default) - Get all services available for connecting |
||
| 132 | * 'connected' - Get all services currently connected |
||
| 133 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 134 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | abstract function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Does the given user have a connection to the service on the given blog? |
||
| 141 | * |
||
| 142 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 143 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 144 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
||
| 148 | if ( !$_blog_id ) |
||
|
|
|||
| 149 | $_blog_id = $this->blog_id(); |
||
| 150 | |||
| 151 | if ( !$_user_id ) |
||
| 152 | $_user_id = $this->user_id(); |
||
| 153 | |||
| 154 | $connections = $this->get_connections( $service_name, $_blog_id, $_user_id ); |
||
| 155 | return ( is_array( $connections ) && count( $connections ) > 0 ? true : false ); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Generates a connection URL. |
||
| 160 | * |
||
| 161 | * This is the URL, which, when visited by the user, starts the authentication |
||
| 162 | * process required to forge a connection. |
||
| 163 | * |
||
| 164 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | abstract function connect_url( $service_name ); |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Generates a Connection refresh URL. |
||
| 171 | * |
||
| 172 | * This is the URL, which, when visited by the user, re-authenticates their |
||
| 173 | * connection to the service. |
||
| 174 | * |
||
| 175 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | abstract function refresh_url( $service_name ); |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Generates a disconnection URL. |
||
| 182 | * |
||
| 183 | * This is the URL, which, when visited by the user, breaks their connection |
||
| 184 | * with the service. |
||
| 185 | * |
||
| 186 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 187 | * @param string $connection_id Connection ID |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | abstract function disconnect_url( $service_name, $connection_id ); |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns a display name for the Service |
||
| 194 | * |
||
| 195 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public static function get_service_label( $service_name ) { |
||
| 214 | |||
| 215 | /* |
||
| 216 | * Connections: For each Service, there can be multiple connections |
||
| 217 | * for a given user. For example, one user could be connected to Twitter |
||
| 218 | * as both @jetpack and as @wordpressdotcom |
||
| 219 | * |
||
| 220 | * For historical reasons, Connections are represented as an object |
||
| 221 | * on WordPress.com and as an array in Jetpack. |
||
| 222 | */ |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the active Connections of a Service |
||
| 226 | * |
||
| 227 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 228 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 229 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 230 | * @return false|object[]|array[] false if no connections exist |
||
| 231 | */ |
||
| 232 | abstract function get_connections( $service_name, $_blog_id = false, $_user_id = false ); |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get a single Connection of a Service |
||
| 236 | * |
||
| 237 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 238 | * @param string $connection_id Connection ID |
||
| 239 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 240 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 241 | * @return false|object[]|array[] false if no connections exist |
||
| 242 | */ |
||
| 243 | abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the Connection ID. |
||
| 247 | * |
||
| 248 | * Note that this is different than the Connection's uniqueid. |
||
| 249 | * |
||
| 250 | * Via a quirk of history, ID is globally unique and unique_id |
||
| 251 | * is only unique per site. |
||
| 252 | * |
||
| 253 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | abstract function get_connection_id( $connection ); |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the Connection unique_id |
||
| 260 | * |
||
| 261 | * Note that this is different than the Connections ID. |
||
| 262 | * |
||
| 263 | * Via a quirk of history, ID is globally unique and unique_id |
||
| 264 | * is only unique per site. |
||
| 265 | * |
||
| 266 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | abstract function get_connection_unique_id( $connection ); |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the Connection's Meta data |
||
| 273 | * |
||
| 274 | * @param object|array Connection |
||
| 275 | * @return array Connection Meta |
||
| 276 | */ |
||
| 277 | abstract function get_connection_meta( $connection ); |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Disconnect a Connection |
||
| 281 | * |
||
| 282 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 283 | * @param string $connection_id Connection ID |
||
| 284 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
||
| 285 | * @param false|int $_user_id The user ID. Use false (default) for the current user |
||
| 286 | * @param bool $force_delete Whether to skip permissions checks |
||
| 287 | * @return false|void False on failure. Void on success. |
||
| 288 | */ |
||
| 289 | abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Globalizes a Connection |
||
| 293 | * |
||
| 294 | * @param string $connection_id Connection ID |
||
| 295 | * @return bool Falsey on failure. Truthy on success. |
||
| 296 | */ |
||
| 297 | abstract function globalize_connection( $connection_id ); |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Unglobalizes a Connection |
||
| 301 | * |
||
| 302 | * @param string $connection_id Connection ID |
||
| 303 | * @return bool Falsey on failure. Truthy on success. |
||
| 304 | */ |
||
| 305 | abstract function unglobalize_connection( $connection_id ); |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns an external URL to the Connection's profile |
||
| 309 | * |
||
| 310 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 311 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 312 | * @return false|string False on failure. URL on success. |
||
| 313 | */ |
||
| 314 | function get_profile_link( $service_name, $connection ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns a display name for the Connection |
||
| 357 | * |
||
| 358 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 359 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | function get_display_name( $service_name, $connection ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Whether the user needs to select additional options after connecting |
||
| 381 | * |
||
| 382 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 383 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | function show_options_popup( $service_name, $connection ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Whether the Connection is "valid" wrt Facebook's requirements. |
||
| 412 | * |
||
| 413 | * Must be connected to a Page (not a Profile). |
||
| 414 | * (Also returns true if we're in the middle of the connection process) |
||
| 415 | * |
||
| 416 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | function is_valid_facebook_connection( $connection ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Whether the Connection currently being connected |
||
| 430 | * |
||
| 431 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | function is_connecting_connection( $connection ) { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * AJAX Handler to run connection tests on all Connections |
||
| 442 | * @return void |
||
| 443 | */ |
||
| 444 | function test_publicize_conns() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Run the connection test for the Connection |
||
| 506 | * |
||
| 507 | * @param string $service_name 'facebook', 'twitter', etc. |
||
| 508 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 509 | * @return WP_Error|true WP_Error on failure. True on success |
||
| 510 | */ |
||
| 511 | abstract function test_connection( $service_name, $connection ); |
||
| 512 | |||
| 513 | /* |
||
| 514 | * Site Data |
||
| 515 | */ |
||
| 516 | |||
| 517 | function user_id() { |
||
| 520 | |||
| 521 | function blog_id() { |
||
| 524 | |||
| 525 | /* |
||
| 526 | * Posts |
||
| 527 | */ |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Checks old and new status to see if the post should be flagged as |
||
| 531 | * ready to Publicize. |
||
| 532 | * |
||
| 533 | * Attached to the `transition_post_status` filter. |
||
| 534 | * |
||
| 535 | * @param string $new_status |
||
| 536 | * @param string $old_status |
||
| 537 | * @param WP_Post $post |
||
| 538 | * @return void |
||
| 539 | */ |
||
| 540 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
| 544 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
| 545 | * |
||
| 546 | * Attached to the `save_post` action. |
||
| 547 | * |
||
| 548 | * @param int $post_id |
||
| 549 | * @param WP_Post $post |
||
| 550 | * @return void |
||
| 551 | */ |
||
| 552 | function save_meta( $post_id, $post ) { |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Alters the "Post Published" message to include information about where the post |
||
| 710 | * was Publicized to. |
||
| 711 | * |
||
| 712 | * Attached to the `post_updated_messages` filter |
||
| 713 | * |
||
| 714 | * @param string[] $messages |
||
| 715 | * @return string[] |
||
| 716 | */ |
||
| 717 | public function update_published_message( $messages ) { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Get the Connections the Post was just Publicized to. |
||
| 780 | * |
||
| 781 | * Only reliable just after the Post was published. |
||
| 782 | * |
||
| 783 | * @param int $post_id |
||
| 784 | * @return string[] Array of Service display name => Connection display name |
||
| 785 | */ |
||
| 786 | function get_publicizing_services( $post_id ) { |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Is the post Publicize-able? |
||
| 811 | * |
||
| 812 | * Only valid prior to Publicizing a Post. |
||
| 813 | * |
||
| 814 | * @param WP_Post $post |
||
| 815 | * @return bool |
||
| 816 | */ |
||
| 817 | function post_is_publicizeable( $post ) { |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Is a given post type Publicize-able? |
||
| 835 | * |
||
| 836 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
| 837 | * the publicize_post_types array filter. |
||
| 838 | * |
||
| 839 | * @param string $post_type The post type to check. |
||
| 840 | * @return bool True if the post type can be Publicized. |
||
| 841 | */ |
||
| 842 | function post_type_is_publicizeable( $post_type ) { |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Already-published posts should not be Publicized by default. This filter sets checked to |
||
| 851 | * false if a post has already been published. |
||
| 852 | * |
||
| 853 | * Attached to the `publicize_checkbox_default` filter |
||
| 854 | * |
||
| 855 | * @param bool $checked |
||
| 856 | * @param int $post_id |
||
| 857 | * @param string $service_name 'facebook', 'twitter', etc |
||
| 858 | * @param object|array The Connection object (WordPress.com) or array (Jetpack) |
||
| 859 | * @return bool |
||
| 860 | */ |
||
| 861 | function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
||
| 868 | |||
| 869 | /* |
||
| 870 | * Util |
||
| 871 | */ |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Converts a Publicize message template string into a sprintf format string |
||
| 875 | * |
||
| 876 | * @param string[] $args |
||
| 877 | * 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
||
| 878 | * ... - The template tags 'title', 'url', etc. |
||
| 879 | * @return string |
||
| 880 | */ |
||
| 881 | protected static function build_sprintf( $args ) { |
||
| 894 | } |
||
| 895 | |||
| 910 |
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: