| Conditions | 36 |
| Paths | 161 |
| Total Lines | 247 |
| Code Lines | 126 |
| Lines | 9 |
| Ratio | 3.64 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 304 | function post_page_metabox() { |
||
| 305 | global $post; |
||
| 306 | |||
| 307 | if ( ! $this->publicize->post_type_is_publicizeable( $post->post_type ) ) |
||
| 308 | return; |
||
| 309 | |||
| 310 | $user_id = empty( $post->post_author ) ? $GLOBALS['user_ID'] : $post->post_author; |
||
| 311 | $services = $this->publicize->get_services( 'connected' ); |
||
| 312 | $available_services = $this->publicize->get_services( 'all' ); |
||
| 313 | |||
| 314 | if ( ! is_array( $available_services ) ) |
||
| 315 | $available_services = array(); |
||
| 316 | |||
| 317 | if ( ! is_array( $services ) ) |
||
| 318 | $services = array(); |
||
| 319 | |||
| 320 | $active = array(); ?> |
||
| 321 | |||
| 322 | <div id="publicize" class="misc-pub-section misc-pub-section-last"> |
||
| 323 | <?php |
||
| 324 | _e( 'Publicize:', 'jetpack' ); |
||
| 325 | |||
| 326 | if ( 0 < count( $services ) ) : |
||
| 327 | ob_start(); |
||
| 328 | ?> |
||
| 329 | |||
| 330 | <div id="publicize-form" class="hide-if-js"> |
||
| 331 | <ul> |
||
| 332 | |||
| 333 | <?php |
||
| 334 | // We can set an _all flag to indicate that this post is completely done as |
||
| 335 | // far as Publicize is concerned. Jetpack uses this approach. All published posts in Jetpack |
||
| 336 | // have Publicize disabled. |
||
| 337 | $all_done = get_post_meta( $post->ID, $this->publicize->POST_DONE . 'all', true ) || ( $this->in_jetpack && 'publish' == $post->post_status ); |
||
| 338 | |||
| 339 | // We don't allow Publicizing to the same external id twice, to prevent spam |
||
| 340 | $service_id_done = (array) get_post_meta( $post->ID, $this->publicize->POST_SERVICE_DONE, true ); |
||
| 341 | |||
| 342 | foreach ( $services as $name => $connections ) { |
||
| 343 | foreach ( $connections as $connection ) { |
||
| 344 | $connection_data = ''; |
||
| 345 | View Code Duplication | if ( method_exists( $connection, 'get_meta' ) ) |
|
| 346 | $connection_data = $connection->get_meta( 'connection_data' ); |
||
| 347 | elseif ( ! empty( $connection['connection_data'] ) ) |
||
| 348 | $connection_data = $connection['connection_data']; |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Filter whether a post should be publicized to a given service. |
||
| 352 | * |
||
| 353 | * @module publicize |
||
| 354 | * |
||
| 355 | * @since 2.0.0 |
||
| 356 | * |
||
| 357 | * @param bool true Should the post be publicized to a given service? Default to true. |
||
| 358 | * @param int $post->ID Post ID. |
||
| 359 | * @param string $name Service name. |
||
| 360 | * @param array $connection_data Array of information about all Publicize details for the site. |
||
| 361 | */ |
||
| 362 | if ( ! $continue = apply_filters( 'wpas_submit_post?', true, $post->ID, $name, $connection_data ) ) { |
||
| 363 | continue; |
||
| 364 | } |
||
| 365 | |||
| 366 | View Code Duplication | if ( ! empty( $connection->unique_id ) ) { |
|
| 367 | $unique_id = $connection->unique_id; |
||
| 368 | } else if ( ! empty( $connection['connection_data']['token_id'] ) ) { |
||
| 369 | $unique_id = $connection['connection_data']['token_id']; |
||
| 370 | } |
||
| 371 | |||
| 372 | // Should we be skipping this one? |
||
| 373 | $skip = ( |
||
| 374 | ( |
||
| 375 | in_array( $post->post_status, array( 'publish', 'draft', 'future' ) ) |
||
| 376 | && |
||
| 377 | get_post_meta( $post->ID, $this->publicize->POST_SKIP . $unique_id, true ) |
||
| 378 | ) |
||
| 379 | || |
||
| 380 | ( |
||
| 381 | is_array( $connection ) |
||
| 382 | && |
||
| 383 | ( |
||
| 384 | ( isset( $connection['meta']['external_id'] ) && ! empty( $service_id_done[ $name ][ $connection['meta']['external_id'] ] ) ) |
||
| 385 | || |
||
| 386 | // Jetpack's connection data looks a little different. |
||
| 387 | ( isset( $connection['external_id'] ) && ! empty( $service_id_done[ $name ][ $connection['external_id'] ] ) ) |
||
| 388 | ) |
||
| 389 | ) |
||
| 390 | ); |
||
| 391 | |||
| 392 | // Was this connections (OR, old-format service) already Publicized to? |
||
| 393 | $done = ( 1 == get_post_meta( $post->ID, $this->publicize->POST_DONE . $unique_id, true ) || 1 == get_post_meta( $post->ID, $this->publicize->POST_DONE . $name, true ) ); // New and old style flags |
||
| 394 | |||
| 395 | // If this one has already been publicized to, don't let it happen again |
||
| 396 | $disabled = ''; |
||
| 397 | if ( $done ) |
||
| 398 | $disabled = ' disabled="disabled"'; |
||
| 399 | |||
| 400 | // If this is a global connection and this user doesn't have enough permissions to modify |
||
| 401 | // those connections, don't let them change it |
||
| 402 | $cmeta = $this->publicize->get_connection_meta( $connection ); |
||
| 403 | $hidden_checkbox = false; |
||
| 404 | if ( !$done && ( 0 == $cmeta['connection_data']['user_id'] && !current_user_can( $this->publicize->GLOBAL_CAP ) ) ) { |
||
| 405 | $disabled = ' disabled="disabled"'; |
||
| 406 | /** |
||
| 407 | * Filters the checkboxes for global connections with non-prilvedged users. |
||
| 408 | * |
||
| 409 | * @module publicize |
||
| 410 | * |
||
| 411 | * @since 3.7.0 |
||
| 412 | * |
||
| 413 | * @param bool $checked Indicates if this connection should be enabled. Default true. |
||
| 414 | * @param int $post->ID ID of the current post |
||
| 415 | * @param string $name Name of the connection (Facebook, Twitter, etc) |
||
| 416 | * @param array $connection Array of data about the connection. |
||
| 417 | */ |
||
| 418 | $hidden_checkbox = apply_filters( 'publicize_checkbox_global_default', true, $post->ID, $name, $connection ); |
||
| 419 | } |
||
| 420 | |||
| 421 | // Determine the state of the checkbox (on/off) and allow filtering |
||
| 422 | $checked = $skip != 1 || $done; |
||
| 423 | /** |
||
| 424 | * Filter the checkbox state of each Publicize connection appearing in the post editor. |
||
| 425 | * |
||
| 426 | * @module publicize |
||
| 427 | * |
||
| 428 | * @since 2.0.1 |
||
| 429 | * |
||
| 430 | * @param bool $checked Should the Publicize checkbox be enabled for a given service. |
||
| 431 | * @param int $post->ID Post ID. |
||
| 432 | * @param string $name Service name. |
||
| 433 | * @param array $connection Array of connection details. |
||
| 434 | */ |
||
| 435 | $checked = apply_filters( 'publicize_checkbox_default', $checked, $post->ID, $name, $connection ); |
||
| 436 | |||
| 437 | // Force the checkbox to be checked if the post was DONE, regardless of what the filter does |
||
| 438 | if ( $done ) { |
||
| 439 | $checked = true; |
||
| 440 | } |
||
| 441 | |||
| 442 | // This post has been handled, so disable everything |
||
| 443 | if ( $all_done ) { |
||
| 444 | $disabled = ' disabled="disabled"'; |
||
| 445 | } |
||
| 446 | |||
| 447 | $label = sprintf( |
||
| 448 | _x( '%1$s: %2$s', 'Service: Account connected as', 'jetpack' ), |
||
| 449 | esc_html( $this->publicize->get_service_label( $name ) ), |
||
| 450 | esc_html( $this->publicize->get_display_name( $name, $connection ) ) |
||
| 451 | ); |
||
| 452 | if ( !$skip || $done ) { |
||
| 453 | $active[] = $label; |
||
| 454 | } |
||
| 455 | ?> |
||
| 456 | <li> |
||
| 457 | <label for="wpas-submit-<?php echo esc_attr( $unique_id ); ?>"> |
||
| 458 | <input type="checkbox" name="wpas[submit][<?php echo $unique_id; ?>]" id="wpas-submit-<?php echo $unique_id; ?>" class="wpas-submit-<?php echo $name; ?>" value="1" <?php |
||
| 459 | checked( true, $checked ); |
||
| 460 | echo $disabled; |
||
| 461 | ?> /> |
||
| 462 | <?php |
||
| 463 | if ( $hidden_checkbox ) { |
||
| 464 | // Need to submit a value to force a global connection to post |
||
| 465 | echo '<input type="hidden" name="wpas[submit][' . $unique_id . ']" value="1" />'; |
||
| 466 | } |
||
| 467 | echo esc_html( $label ); |
||
| 468 | ?> |
||
| 469 | </label> |
||
| 470 | </li> |
||
| 471 | <?php |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | if ( $title = get_post_meta( $post->ID, $this->publicize->POST_MESS, true ) ) { |
||
| 476 | $title = esc_html( $title ); |
||
| 477 | } else { |
||
| 478 | $title = ''; |
||
| 479 | } |
||
| 480 | ?> |
||
| 481 | |||
| 482 | </ul> |
||
| 483 | |||
| 484 | <label for="wpas-title"><?php _e( 'Custom Message:', 'jetpack' ); ?></label> |
||
| 485 | <span id="wpas-title-counter" class="alignright hide-if-no-js">0</span> |
||
| 486 | |||
| 487 | <textarea name="wpas_title" id="wpas-title"<?php disabled( $all_done ); ?>><?php echo $title; ?></textarea> |
||
| 488 | |||
| 489 | <a href="#" class="hide-if-no-js" id="publicize-form-hide"><?php _e( 'Hide', 'jetpack' ); ?></a> |
||
| 490 | <input type="hidden" name="wpas[0]" value="1" /> |
||
| 491 | |||
| 492 | </div> |
||
| 493 | <div id="pub-connection-tests"></div> |
||
| 494 | <?php // #publicize-form |
||
| 495 | |||
| 496 | $publicize_form = ob_get_clean(); |
||
| 497 | else : |
||
| 498 | echo " " . __( 'Not Connected', 'jetpack' ); |
||
| 499 | ob_start(); |
||
| 500 | ?> |
||
| 501 | |||
| 502 | <div id="publicize-form" class="hide-if-js"> |
||
| 503 | <div id="add-publicize-check" style="display: none;"></div> |
||
| 504 | |||
| 505 | <strong><?php _e( 'Connect to', 'jetpack' ); ?>:</strong> |
||
| 506 | |||
| 507 | <ul class="not-connected"> |
||
| 508 | <?php foreach ( $available_services as $service_name => $service ) : ?> |
||
| 509 | <li> |
||
| 510 | <a class="pub-service" data-service="<?php echo esc_attr( $service_name ); ?>" title="<?php echo esc_attr( sprintf( __( 'Connect and share your posts on %s', 'jetpack' ), $this->publicize->get_service_label( $service_name ) ) ); ?>" target="_blank" href="<?php echo $this->publicize->connect_url( $service_name ); ?>"> |
||
| 511 | <?php echo esc_html( $this->publicize->get_service_label( $service_name ) ); ?> |
||
| 512 | </a> |
||
| 513 | </li> |
||
| 514 | <?php endforeach; ?> |
||
| 515 | </ul> |
||
| 516 | |||
| 517 | <?php if ( 0 < count( $services ) ) : ?> |
||
| 518 | <a href="#" class="hide-if-no-js" id="publicize-form-hide"><?php _e( 'Hide', 'jetpack' ); ?></a> |
||
| 519 | <?php else : ?> |
||
| 520 | <a href="#" class="hide-if-no-js" id="publicize-disconnected-form-hide"><?php _e( 'Hide', 'jetpack' ); ?></a> |
||
| 521 | <?php endif; ?> |
||
| 522 | </div> <?php // #publicize-form |
||
| 523 | |||
| 524 | $publicize_form = ob_get_clean(); |
||
| 525 | endif; |
||
| 526 | ?> |
||
| 527 | |||
| 528 | <span id="publicize-defaults"><strong><?php echo join( '</strong>, <strong>', array_map( 'esc_html', $active ) ); ?></strong></span><br /> |
||
| 529 | |||
| 530 | <?php if ( 0 < count( $services ) ) : ?> |
||
| 531 | <a href="#" id="publicize-form-edit"><?php _e( 'Edit Details', 'jetpack' ); ?></a> <a href="<?php echo admin_url( 'options-general.php?page=sharing' ); ?>" target="_blank"><?php _e( 'Settings', 'jetpack' ); ?></a><br /> |
||
| 532 | <?php else : ?> |
||
| 533 | <a href="#" id="publicize-disconnected-form-show"><?php _e( 'Show', 'jetpack' ); ?></a><br /> |
||
| 534 | <?php endif; ?> |
||
| 535 | |||
| 536 | <?php |
||
| 537 | /** |
||
| 538 | * Filter the Publicize details form. |
||
| 539 | * |
||
| 540 | * @module publicize |
||
| 541 | * |
||
| 542 | * @since 2.0.0 |
||
| 543 | * |
||
| 544 | * @param string $publicize_form Publicize Details form appearing above Publish button in the editor. |
||
| 545 | */ |
||
| 546 | echo apply_filters( 'publicize_form', $publicize_form ); |
||
| 547 | ?> |
||
| 548 | |||
| 549 | </div> <?php // #publicize |
||
| 550 | } |
||
| 551 | |||
| 553 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.