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