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() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Functions to be implemented by the extended class (publicize-wpcom or publicize-jetpack) |
||
| 117 | */ |
||
| 118 | abstract function get_connection_id( $connection ); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Shared Functions |
||
| 131 | */ |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns an external URL to the connection's profile |
||
| 135 | */ |
||
| 136 | function get_profile_link( $service_name, $c ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Returns a display name for the connection |
||
| 179 | */ |
||
| 180 | function get_display_name( $service_name, $c ) { |
||
| 196 | |||
| 197 | public static function get_service_label( $service_name ) { |
||
| 213 | |||
| 214 | function show_options_popup( $service_name, $c ) { |
||
| 237 | |||
| 238 | function user_id() { |
||
| 242 | |||
| 243 | function blog_id() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns true if a user has a connection to a particular service, false otherwise |
||
| 249 | */ |
||
| 250 | function is_enabled( $service, $_blog_id = false, $_user_id = false ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
||
| 263 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
||
| 264 | */ |
||
| 265 | function save_meta( $post_id, $post ) { |
||
| 266 | $cron_user = null; |
||
| 267 | $submit_post = true; |
||
| 268 | |||
| 269 | if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) |
||
| 270 | return; |
||
| 271 | |||
| 272 | // Don't Publicize during certain contexts: |
||
| 273 | |||
| 274 | // - import |
||
| 275 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
||
| 276 | $submit_post = false; |
||
| 277 | } |
||
| 278 | |||
| 279 | // - on quick edit, autosave, etc but do fire on p2, quickpress, and instapost ajax |
||
| 280 | if ( |
||
| 281 | defined( 'DOING_AJAX' ) |
||
| 282 | && |
||
| 283 | DOING_AJAX |
||
| 284 | && |
||
| 285 | !did_action( 'p2_ajax' ) |
||
| 286 | && |
||
| 287 | !did_action( 'wp_ajax_json_quickpress_post' ) |
||
| 288 | && |
||
| 289 | !did_action( 'wp_ajax_instapost_publish' ) |
||
| 290 | && |
||
| 291 | !did_action( 'wp_ajax_post_reblog' ) |
||
| 292 | && |
||
| 293 | !did_action( 'wp_ajax_press-this-save-post' ) |
||
| 294 | ) { |
||
| 295 | $submit_post = false; |
||
| 296 | } |
||
| 297 | |||
| 298 | // - bulk edit |
||
| 299 | if ( isset( $_GET['bulk_edit'] ) ) { |
||
| 300 | $submit_post = false; |
||
| 301 | } |
||
| 302 | |||
| 303 | // - API/XML-RPC Test Posts |
||
| 304 | if ( |
||
| 305 | ( |
||
| 306 | defined( 'XMLRPC_REQUEST' ) |
||
| 307 | && |
||
| 308 | XMLRPC_REQUEST |
||
| 309 | || |
||
| 310 | defined( 'APP_REQUEST' ) |
||
| 311 | && |
||
| 312 | APP_REQUEST |
||
| 313 | ) |
||
| 314 | && |
||
| 315 | 0 === strpos( $post->post_title, 'Temporary Post Used For Theme Detection' ) |
||
| 316 | ) { |
||
| 317 | $submit_post = false; |
||
| 318 | } |
||
| 319 | |||
| 320 | // only work with certain statuses (avoids inherits, auto drafts etc) |
||
| 321 | if ( !in_array( $post->post_status, array( 'publish', 'draft', 'future' ) ) ) { |
||
| 322 | $submit_post = false; |
||
| 323 | } |
||
| 324 | |||
| 325 | // don't publish password protected posts |
||
| 326 | if ( '' !== $post->post_password ) { |
||
| 327 | $submit_post = false; |
||
| 328 | } |
||
| 329 | |||
| 330 | // Did this request happen via wp-admin? |
||
| 331 | $from_web = isset( $_SERVER['REQUEST_METHOD'] ) |
||
| 332 | && |
||
| 333 | 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) |
||
| 334 | && |
||
| 335 | isset( $_POST[$this->ADMIN_PAGE] ); |
||
| 336 | |||
| 337 | if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && isset( $_POST['wpas_title'] ) ) { |
||
| 338 | if ( empty( $_POST['wpas_title'] ) ) { |
||
| 339 | delete_post_meta( $post_id, $this->POST_MESS ); |
||
| 340 | } else { |
||
| 341 | update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $_POST['wpas_title'] ) ) ); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | // change current user to provide context for get_services() if we're running during cron |
||
| 346 | if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
||
| 347 | $cron_user = (int) $GLOBALS['user_ID']; |
||
| 348 | wp_set_current_user( $post->post_author ); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * In this phase, we mark connections that we want to SKIP. When Publicize is actually triggered, |
||
| 353 | * it will Publicize to everything *except* those marked for skipping. |
||
| 354 | */ |
||
| 355 | foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { |
||
| 356 | foreach ( $connections as $connection ) { |
||
| 357 | $connection_data = ''; |
||
| 358 | View Code Duplication | if ( method_exists( $connection, 'get_meta' ) ) |
|
| 359 | $connection_data = $connection->get_meta( 'connection_data' ); |
||
| 360 | elseif ( ! empty( $connection['connection_data'] ) ) |
||
| 361 | $connection_data = $connection['connection_data']; |
||
| 362 | |||
| 363 | /** This action is documented in modules/publicize/ui.php */ |
||
| 364 | if ( false == apply_filters( 'wpas_submit_post?', $submit_post, $post_id, $service_name, $connection_data ) ) { |
||
| 365 | delete_post_meta( $post_id, $this->PENDING ); |
||
| 366 | continue; |
||
| 367 | } |
||
| 368 | |||
| 369 | View Code Duplication | if ( !empty( $connection->unique_id ) ) |
|
| 370 | $unique_id = $connection->unique_id; |
||
| 371 | else if ( !empty( $connection['connection_data']['token_id'] ) ) |
||
| 372 | $unique_id = $connection['connection_data']['token_id']; |
||
| 373 | |||
| 374 | // This was a wp-admin request, so we need to check the state of checkboxes |
||
| 375 | if ( $from_web ) { |
||
| 376 | // delete stray service-based post meta |
||
| 377 | delete_post_meta( $post_id, $this->POST_SKIP . $service_name ); |
||
| 378 | |||
| 379 | // We *unchecked* this stream from the admin page, or it's set to readonly, or it's a new addition |
||
| 380 | if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$unique_id] ) ) { |
||
| 381 | // Also make sure that the service-specific input isn't there. |
||
| 382 | // If the user connected to a new service 'in-page' then a hidden field with the service |
||
| 383 | // name is added, so we just assume they wanted to Publicize to that service. |
||
| 384 | if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$service_name] ) ) { |
||
| 385 | // Nothing seems to be checked, so we're going to mark this one to be skipped |
||
| 386 | update_post_meta( $post_id, $this->POST_SKIP . $unique_id, 1 ); |
||
| 387 | continue; |
||
| 388 | } else { |
||
| 389 | // clean up any stray post meta |
||
| 390 | delete_post_meta( $post_id, $this->POST_SKIP . $unique_id ); |
||
| 391 | } |
||
| 392 | } else { |
||
| 393 | // The checkbox for this connection is explicitly checked -- make sure we DON'T skip it |
||
| 394 | delete_post_meta( $post_id, $this->POST_SKIP . $unique_id ); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Fires right before the post is processed for Publicize. |
||
| 400 | * Users may hook in here and do anything else they need to after meta is written, |
||
| 401 | * and before the post is processed for Publicize. |
||
| 402 | * |
||
| 403 | * @since 2.1.2 |
||
| 404 | * |
||
| 405 | * @param bool $submit_post Should the post be publicized. |
||
| 406 | * @param int $post->ID Post ID. |
||
| 407 | * @param string $service_name Service name. |
||
| 408 | * @param array $connection Array of connection details. |
||
| 409 | */ |
||
| 410 | do_action( 'publicize_save_meta', $submit_post, $post_id, $service_name, $connection ); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
||
| 415 | wp_set_current_user( $cron_user ); |
||
| 416 | } |
||
| 417 | |||
| 418 | // Next up will be ::publicize_post() |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Is a given post type Publicize-able? |
||
| 423 | * |
||
| 424 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
||
| 425 | * the publicize_post_types array filter. |
||
| 426 | * |
||
| 427 | * @param string $post_type The post type to check. |
||
| 428 | * $return bool True if the post type can be Publicized. |
||
| 429 | */ |
||
| 430 | function post_type_is_publicizeable( $post_type ) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Runs tests on all the connections and returns the results to the caller |
||
| 439 | */ |
||
| 440 | function test_publicize_conns() { |
||
| 483 | } |
||
| 484 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.