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 Custom_Image_Header 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 Custom_Image_Header, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Custom_Image_Header { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Callback for administration header. |
||
| 20 | * |
||
| 21 | * @var callable |
||
| 22 | * @since 2.1.0 |
||
| 23 | */ |
||
| 24 | public $admin_header_callback; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Callback for header div. |
||
| 28 | * |
||
| 29 | * @var callable |
||
| 30 | * @since 3.0.0 |
||
| 31 | */ |
||
| 32 | public $admin_image_div_callback; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Holds default headers. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | * @since 3.0.0 |
||
| 39 | * @access private |
||
| 40 | */ |
||
| 41 | public $default_headers = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Used to trigger a success message when settings updated and set to true. |
||
| 45 | * |
||
| 46 | * @since 3.0.0 |
||
| 47 | * @access private |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | private $updated; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor - Register administration header callback. |
||
| 54 | * |
||
| 55 | * @since 2.1.0 |
||
| 56 | * @param callable $admin_header_callback |
||
| 57 | * @param callable $admin_image_div_callback Optional custom image div output callback. |
||
| 58 | */ |
||
| 59 | public function __construct($admin_header_callback, $admin_image_div_callback = '') { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set up the hooks for the Custom Header admin page. |
||
| 73 | * |
||
| 74 | * @since 2.1.0 |
||
| 75 | */ |
||
| 76 | public function init() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Adds contextual help. |
||
| 94 | * |
||
| 95 | * @since 3.0.0 |
||
| 96 | */ |
||
| 97 | public function help() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the current step. |
||
| 134 | * |
||
| 135 | * @since 2.6.0 |
||
| 136 | * |
||
| 137 | * @return int Current step |
||
| 138 | */ |
||
| 139 | public function step() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set up the enqueue for the JavaScript files. |
||
| 155 | * |
||
| 156 | * @since 2.1.0 |
||
| 157 | */ |
||
| 158 | public function js_includes() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set up the enqueue for the CSS files |
||
| 173 | * |
||
| 174 | * @since 2.7.0 |
||
| 175 | */ |
||
| 176 | public function css_includes() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Execute custom header modification. |
||
| 187 | * |
||
| 188 | * @since 2.6.0 |
||
| 189 | */ |
||
| 190 | public function take_action() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Process the default headers |
||
| 233 | * |
||
| 234 | * @since 3.0.0 |
||
| 235 | * |
||
| 236 | * @global array $_wp_default_headers |
||
| 237 | */ |
||
| 238 | public function process_default_headers() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Display UI for selecting one of several default headers. |
||
| 259 | * |
||
| 260 | * Show the random image option if this theme has multiple header images. |
||
| 261 | * Random image option is on by default if no header has been set. |
||
| 262 | * |
||
| 263 | * @since 3.0.0 |
||
| 264 | * |
||
| 265 | * @param string $type The header type. One of 'default' (for the Uploaded Images control) |
||
| 266 | * or 'uploaded' (for the Uploaded Images control). |
||
| 267 | */ |
||
| 268 | public function show_header_selector( $type = 'default' ) { |
||
| 269 | if ( 'default' == $type ) { |
||
| 270 | $headers = $this->default_headers; |
||
| 271 | } else { |
||
| 272 | $headers = get_uploaded_header_images(); |
||
| 273 | $type = 'uploaded'; |
||
| 274 | } |
||
| 275 | |||
| 276 | if ( 1 < count( $headers ) ) { |
||
| 277 | echo '<div class="random-header">'; |
||
| 278 | echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />'; |
||
| 279 | _e( '<strong>Random:</strong> Show a different image on each page.' ); |
||
| 280 | echo '</label>'; |
||
| 281 | echo '</div>'; |
||
| 282 | } |
||
| 283 | |||
| 284 | echo '<div class="available-headers">'; |
||
| 285 | foreach ( $headers as $header_key => $header ) { |
||
| 286 | $header_thumbnail = $header['thumbnail_url']; |
||
| 287 | $header_url = $header['url']; |
||
| 288 | $header_alt_text = empty( $header['alt_text'] ) ? '' : $header['alt_text']; |
||
| 289 | echo '<div class="default-header">'; |
||
| 290 | echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />'; |
||
| 291 | $width = ''; |
||
| 292 | if ( !empty( $header['attachment_id'] ) ) |
||
| 293 | $width = ' width="230"'; |
||
| 294 | echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) .'"' . $width . ' /></label>'; |
||
| 295 | echo '</div>'; |
||
| 296 | } |
||
| 297 | echo '<div class="clear"></div></div>'; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Execute JavaScript depending on step. |
||
| 302 | * |
||
| 303 | * @since 2.1.0 |
||
| 304 | */ |
||
| 305 | public function js() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Display JavaScript based on Step 1 and 3. |
||
| 315 | * |
||
| 316 | * @since 2.6.0 |
||
| 317 | */ |
||
| 318 | public function js_1() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Display JavaScript based on Step 2. |
||
| 376 | * |
||
| 377 | * @since 2.6.0 |
||
| 378 | */ |
||
| 379 | public function js_2() { ?> |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Display first step of custom header image page. |
||
| 448 | * |
||
| 449 | * @since 2.1.0 |
||
| 450 | */ |
||
| 451 | public function step_1() { |
||
| 452 | $this->process_default_headers(); |
||
| 453 | ?> |
||
| 454 | |||
| 455 | <div class="wrap"> |
||
| 456 | <h1><?php _e( 'Custom Header' ); ?></h1> |
||
| 457 | |||
| 458 | <?php if ( current_user_can( 'customize' ) ) { ?> |
||
| 459 | <div class="notice notice-info hide-if-no-customize"> |
||
| 460 | <p> |
||
| 461 | <?php |
||
| 462 | printf( |
||
| 463 | __( 'You can now manage and live-preview Custom Header in the <a href="%1$s">Customizer</a>.' ), |
||
| 464 | admin_url( 'customize.php?autofocus[control]=header_image' ) |
||
| 465 | ); |
||
| 466 | ?> |
||
| 467 | </p> |
||
| 468 | </div> |
||
| 469 | <?php } ?> |
||
| 470 | |||
| 471 | <?php if ( ! empty( $this->updated ) ) { ?> |
||
| 472 | <div id="message" class="updated"> |
||
| 473 | <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p> |
||
| 474 | </div> |
||
| 475 | <?php } ?> |
||
| 476 | |||
| 477 | <h3><?php _e( 'Header Image' ); ?></h3> |
||
| 478 | |||
| 479 | <table class="form-table"> |
||
| 480 | <tbody> |
||
| 481 | |||
| 482 | <?php if ( get_custom_header() || display_header_text() ) : ?> |
||
| 483 | <tr> |
||
| 484 | <th scope="row"><?php _e( 'Preview' ); ?></th> |
||
| 485 | <td> |
||
| 486 | <?php |
||
| 487 | if ( $this->admin_image_div_callback ) { |
||
| 488 | call_user_func( $this->admin_image_div_callback ); |
||
| 489 | } else { |
||
| 490 | $custom_header = get_custom_header(); |
||
| 491 | $header_image = get_header_image(); |
||
| 492 | |||
| 493 | if ( $header_image ) { |
||
| 494 | $header_image_style = 'background-image:url(' . esc_url( $header_image ) . ');'; |
||
| 495 | } else { |
||
| 496 | $header_image_style = ''; |
||
| 497 | } |
||
| 498 | |||
| 499 | if ( $custom_header->width ) |
||
| 500 | $header_image_style .= 'max-width:' . $custom_header->width . 'px;'; |
||
| 501 | if ( $custom_header->height ) |
||
| 502 | $header_image_style .= 'height:' . $custom_header->height . 'px;'; |
||
| 503 | ?> |
||
| 504 | <div id="headimg" style="<?php echo $header_image_style; ?>"> |
||
| 505 | <?php |
||
| 506 | if ( display_header_text() ) |
||
| 507 | $style = ' style="color:#' . get_header_textcolor() . ';"'; |
||
| 508 | else |
||
| 509 | $style = ' style="display:none;"'; |
||
| 510 | ?> |
||
| 511 | <h1><a id="name" class="displaying-header-text" <?php echo $style; ?> onclick="return false;" href="<?php bloginfo('url'); ?>" tabindex="-1"><?php bloginfo( 'name' ); ?></a></h1> |
||
| 512 | <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div> |
||
| 513 | </div> |
||
| 514 | <?php } ?> |
||
| 515 | </td> |
||
| 516 | </tr> |
||
| 517 | <?php endif; ?> |
||
| 518 | |||
| 519 | <?php if ( current_user_can( 'upload_files' ) && current_theme_supports( 'custom-header', 'uploads' ) ) : ?> |
||
| 520 | <tr> |
||
| 521 | <th scope="row"><?php _e( 'Select Image' ); ?></th> |
||
| 522 | <td> |
||
| 523 | <p><?php _e( 'You can select an image to be shown at the top of your site by uploading from your computer or choosing from your media library. After selecting an image you will be able to crop it.' ); ?><br /> |
||
| 524 | <?php |
||
| 525 | if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) { |
||
| 526 | printf( __( 'Images of exactly <strong>%1$d × %2$d pixels</strong> will be used as-is.' ) . '<br />', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ) ); |
||
| 527 | View Code Duplication | } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) { |
|
| 528 | if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) |
||
| 529 | printf( |
||
| 530 | /* translators: %s: size in pixels */ |
||
| 531 | __( 'Images should be at least %s wide.' ) . ' ', |
||
| 532 | sprintf( |
||
| 533 | /* translators: %d: custom header width */ |
||
| 534 | '<strong>' . __( '%d pixels' ) . '</strong>', |
||
| 535 | get_theme_support( 'custom-header', 'width' ) |
||
| 536 | ) |
||
| 537 | ); |
||
| 538 | } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) { |
||
| 539 | if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) |
||
| 540 | printf( |
||
| 541 | /* translators: %s: size in pixels */ |
||
| 542 | __( 'Images should be at least %s tall.' ) . ' ', |
||
| 543 | sprintf( |
||
| 544 | /* translators: %d: custom header height */ |
||
| 545 | '<strong>' . __( '%d pixels' ) . '</strong>', |
||
| 546 | get_theme_support( 'custom-header', 'height' ) |
||
| 547 | ) |
||
| 548 | ); |
||
| 549 | } |
||
| 550 | if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) { |
||
| 551 | View Code Duplication | if ( current_theme_supports( 'custom-header', 'width' ) ) |
|
| 552 | printf( |
||
| 553 | /* translators: %s: size in pixels */ |
||
| 554 | __( 'Suggested width is %s.' ) . ' ', |
||
| 555 | sprintf( |
||
| 556 | /* translators: %d: custom header width */ |
||
| 557 | '<strong>' . __( '%d pixels' ) . '</strong>', |
||
| 558 | get_theme_support( 'custom-header', 'width' ) |
||
| 559 | ) |
||
| 560 | ); |
||
| 561 | View Code Duplication | if ( current_theme_supports( 'custom-header', 'height' ) ) |
|
| 562 | printf( |
||
| 563 | /* translators: %s: size in pixels */ |
||
| 564 | __( 'Suggested height is %s.' ) . ' ', |
||
| 565 | sprintf( |
||
| 566 | /* translators: %d: custom header height */ |
||
| 567 | '<strong>' . __( '%d pixels' ) . '</strong>', |
||
| 568 | get_theme_support( 'custom-header', 'height' ) |
||
| 569 | ) |
||
| 570 | ); |
||
| 571 | } |
||
| 572 | ?></p> |
||
| 573 | <form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="<?php echo esc_url( add_query_arg( 'step', 2 ) ) ?>"> |
||
| 574 | <p> |
||
| 575 | <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br /> |
||
| 576 | <input type="file" id="upload" name="import" /> |
||
| 577 | <input type="hidden" name="action" value="save" /> |
||
| 578 | <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?> |
||
| 579 | <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?> |
||
| 580 | </p> |
||
| 581 | <?php |
||
| 582 | $modal_update_href = esc_url( add_query_arg( array( |
||
| 583 | 'page' => 'custom-header', |
||
| 584 | 'step' => 2, |
||
| 585 | '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'), |
||
| 586 | ), admin_url('themes.php') ) ); |
||
| 587 | ?> |
||
| 588 | <p> |
||
| 589 | <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br /> |
||
| 590 | <button id="choose-from-library-link" class="button" |
||
| 591 | data-update-link="<?php echo esc_attr( $modal_update_href ); ?>" |
||
| 592 | data-choose="<?php esc_attr_e( 'Choose a Custom Header' ); ?>" |
||
| 593 | data-update="<?php esc_attr_e( 'Set as header' ); ?>"><?php _e( 'Choose Image' ); ?></button> |
||
| 594 | </p> |
||
| 595 | </form> |
||
| 596 | </td> |
||
| 597 | </tr> |
||
| 598 | <?php endif; ?> |
||
| 599 | </tbody> |
||
| 600 | </table> |
||
| 601 | |||
| 602 | <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ) ?>"> |
||
| 603 | <?php submit_button( null, 'screen-reader-text', 'save-header-options', false ); ?> |
||
| 604 | <table class="form-table"> |
||
| 605 | <tbody> |
||
| 606 | <?php if ( get_uploaded_header_images() ) : ?> |
||
| 607 | <tr> |
||
| 608 | <th scope="row"><?php _e( 'Uploaded Images' ); ?></th> |
||
| 609 | <td> |
||
| 610 | <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p> |
||
| 611 | <?php |
||
| 612 | $this->show_header_selector( 'uploaded' ); |
||
| 613 | ?> |
||
| 614 | </td> |
||
| 615 | </tr> |
||
| 616 | <?php endif; |
||
| 617 | if ( ! empty( $this->default_headers ) ) : ?> |
||
| 618 | <tr> |
||
| 619 | <th scope="row"><?php _e( 'Default Images' ); ?></th> |
||
| 620 | <td> |
||
| 621 | <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?> |
||
| 622 | <p><?php _e( 'If you don‘t want to upload your own image, you can use one of these cool headers, or show a random one.' ) ?></p> |
||
| 623 | <?php else: ?> |
||
| 624 | <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p> |
||
| 625 | <?php endif; ?> |
||
| 626 | <?php |
||
| 627 | $this->show_header_selector( 'default' ); |
||
| 628 | ?> |
||
| 629 | </td> |
||
| 630 | </tr> |
||
| 631 | <?php endif; |
||
| 632 | View Code Duplication | if ( get_header_image() ) : ?> |
|
| 633 | <tr> |
||
| 634 | <th scope="row"><?php _e( 'Remove Image' ); ?></th> |
||
| 635 | <td> |
||
| 636 | <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p> |
||
| 637 | <?php submit_button( __( 'Remove Header Image' ), 'button', 'removeheader', false ); ?> |
||
| 638 | </td> |
||
| 639 | </tr> |
||
| 640 | <?php endif; |
||
| 641 | |||
| 642 | $default_image = get_theme_support( 'custom-header', 'default-image' ); |
||
| 643 | View Code Duplication | if ( $default_image && get_header_image() != $default_image ) : ?> |
|
| 644 | <tr> |
||
| 645 | <th scope="row"><?php _e( 'Reset Image' ); ?></th> |
||
| 646 | <td> |
||
| 647 | <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p> |
||
| 648 | <?php submit_button( __( 'Restore Original Header Image' ), 'button', 'resetheader', false ); ?> |
||
| 649 | </td> |
||
| 650 | </tr> |
||
| 651 | <?php endif; ?> |
||
| 652 | </tbody> |
||
| 653 | </table> |
||
| 654 | |||
| 655 | <?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?> |
||
| 656 | |||
| 657 | <h3><?php _e( 'Header Text' ); ?></h3> |
||
| 658 | |||
| 659 | <table class="form-table"> |
||
| 660 | <tbody> |
||
| 661 | <tr> |
||
| 662 | <th scope="row"><?php _e( 'Header Text' ); ?></th> |
||
| 663 | <td> |
||
| 664 | <p> |
||
| 665 | <label><input type="checkbox" name="display-header-text" id="display-header-text"<?php checked( display_header_text() ); ?> /> <?php _e( 'Show header text with your image.' ); ?></label> |
||
| 666 | </p> |
||
| 667 | </td> |
||
| 668 | </tr> |
||
| 669 | |||
| 670 | <tr class="displaying-header-text"> |
||
| 671 | <th scope="row"><?php _e( 'Text Color' ); ?></th> |
||
| 672 | <td> |
||
| 673 | <p> |
||
| 674 | <?php |
||
| 675 | $default_color = ''; |
||
| 676 | View Code Duplication | if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) { |
|
| 677 | $default_color = get_theme_support( 'custom-header', 'default-text-color' ); |
||
| 678 | if ( $default_color && false === strpos( $default_color, '#' ) ) { |
||
| 679 | $default_color = '#' . $default_color; |
||
| 680 | } |
||
| 681 | } |
||
| 682 | |||
| 683 | $default_color_attr = $default_color ? ' data-default-color="' . esc_attr( $default_color ) . '"' : ''; |
||
| 684 | |||
| 685 | $header_textcolor = display_header_text() ? get_header_textcolor() : get_theme_support( 'custom-header', 'default-text-color' ); |
||
| 686 | if ( $header_textcolor && false === strpos( $header_textcolor, '#' ) ) { |
||
| 687 | $header_textcolor = '#' . $header_textcolor; |
||
| 688 | } |
||
| 689 | |||
| 690 | echo '<input type="text" name="text-color" id="text-color" value="' . esc_attr( $header_textcolor ) . '"' . $default_color_attr . ' />'; |
||
| 691 | if ( $default_color ) { |
||
| 692 | echo ' <span class="description hide-if-js">' . sprintf( _x( 'Default: %s', 'color' ), esc_html( $default_color ) ) . '</span>'; |
||
| 693 | } |
||
| 694 | ?> |
||
| 695 | </p> |
||
| 696 | </td> |
||
| 697 | </tr> |
||
| 698 | </tbody> |
||
| 699 | </table> |
||
| 700 | <?php endif; |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Fires just before the submit button in the custom header options form. |
||
| 704 | * |
||
| 705 | * @since 3.1.0 |
||
| 706 | */ |
||
| 707 | do_action( 'custom_header_options' ); |
||
| 708 | |||
| 709 | wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> |
||
| 710 | |||
| 711 | <?php submit_button( null, 'primary', 'save-header-options' ); ?> |
||
| 712 | </form> |
||
| 713 | </div> |
||
| 714 | |||
| 715 | <?php } |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Display second step of custom header image page. |
||
| 719 | * |
||
| 720 | * @since 2.1.0 |
||
| 721 | */ |
||
| 722 | public function step_2() { |
||
| 833 | |||
| 834 | |||
| 835 | /** |
||
| 836 | * Upload the file to be cropped in the second step. |
||
| 837 | * |
||
| 838 | * @since 3.4.0 |
||
| 839 | */ |
||
| 840 | public function step_2_manage_upload() { |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Display third step of custom header image page. |
||
| 874 | * |
||
| 875 | * @since 2.1.0 |
||
| 876 | * @since 4.4.0 Switched to using wp_get_attachment_url() instead of the guid |
||
| 877 | * for retrieving the header image URL. |
||
| 878 | */ |
||
| 879 | public function step_3() { |
||
| 880 | check_admin_referer( 'custom-header-crop-image' ); |
||
| 881 | |||
| 882 | View Code Duplication | if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) { |
|
| 883 | wp_die( |
||
| 884 | '<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' . |
||
| 885 | '<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>', |
||
| 886 | 403 |
||
| 887 | ); |
||
| 888 | } |
||
| 889 | |||
| 890 | View Code Duplication | if ( ! empty( $_POST['skip-cropping'] ) && ! ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) { |
|
| 891 | wp_die( |
||
| 892 | '<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' . |
||
| 893 | '<p>' . __( 'The current theme does not support a flexible sized header image.' ) . '</p>', |
||
| 894 | 403 |
||
| 895 | ); |
||
| 896 | } |
||
| 897 | |||
| 898 | if ( $_POST['oitar'] > 1 ) { |
||
| 899 | $_POST['x1'] = $_POST['x1'] * $_POST['oitar']; |
||
| 900 | $_POST['y1'] = $_POST['y1'] * $_POST['oitar']; |
||
| 901 | $_POST['width'] = $_POST['width'] * $_POST['oitar']; |
||
| 902 | $_POST['height'] = $_POST['height'] * $_POST['oitar']; |
||
| 903 | } |
||
| 904 | |||
| 905 | $attachment_id = absint( $_POST['attachment_id'] ); |
||
| 906 | $original = get_attached_file($attachment_id); |
||
| 907 | |||
| 908 | $dimensions = $this->get_header_dimensions( array( |
||
| 909 | 'height' => $_POST['height'], |
||
| 910 | 'width' => $_POST['width'], |
||
| 911 | ) ); |
||
| 912 | $height = $dimensions['dst_height']; |
||
| 913 | $width = $dimensions['dst_width']; |
||
| 914 | |||
| 915 | if ( empty( $_POST['skip-cropping'] ) ) |
||
| 916 | $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height ); |
||
| 917 | elseif ( ! empty( $_POST['create-new-attachment'] ) ) |
||
| 918 | $cropped = _copy_image_file( $attachment_id ); |
||
| 919 | else |
||
| 920 | $cropped = get_attached_file( $attachment_id ); |
||
| 921 | |||
| 922 | if ( ! $cropped || is_wp_error( $cropped ) ) |
||
| 923 | wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); |
||
| 924 | |||
| 925 | /** This filter is documented in wp-admin/custom-header.php */ |
||
| 926 | $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication |
||
| 927 | |||
| 928 | $object = $this->create_attachment_object( $cropped, $attachment_id ); |
||
| 929 | |||
| 930 | if ( ! empty( $_POST['create-new-attachment'] ) ) |
||
| 931 | unset( $object['ID'] ); |
||
| 932 | |||
| 933 | // Update the attachment |
||
| 934 | $attachment_id = $this->insert_attachment( $object, $cropped ); |
||
| 935 | |||
| 936 | $url = wp_get_attachment_url( $attachment_id ); |
||
| 937 | $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) ); |
||
| 938 | |||
| 939 | // Cleanup. |
||
| 940 | $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original ); |
||
| 941 | if ( file_exists( $medium ) ) { |
||
| 942 | wp_delete_file( $medium ); |
||
| 943 | } |
||
| 944 | |||
| 945 | if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) { |
||
| 946 | wp_delete_file( $original ); |
||
| 947 | } |
||
| 948 | |||
| 949 | return $this->finished(); |
||
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Display last step of custom header image page. |
||
| 954 | * |
||
| 955 | * @since 2.1.0 |
||
| 956 | */ |
||
| 957 | public function finished() { |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Display the page based on the current step. |
||
| 964 | * |
||
| 965 | * @since 2.1.0 |
||
| 966 | */ |
||
| 967 | public function admin_page() { |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Unused since 3.5.0. |
||
| 981 | * |
||
| 982 | * @since 3.4.0 |
||
| 983 | * |
||
| 984 | * @param array $form_fields |
||
| 985 | * @return array $form_fields |
||
| 986 | */ |
||
| 987 | public function attachment_fields_to_edit( $form_fields ) { |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Unused since 3.5.0. |
||
| 993 | * |
||
| 994 | * @since 3.4.0 |
||
| 995 | * |
||
| 996 | * @param array $tabs |
||
| 997 | * @return array $tabs |
||
| 998 | */ |
||
| 999 | public function filter_upload_tabs( $tabs ) { |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Choose a header image, selected from existing uploaded and default headers, |
||
| 1005 | * or provide an array of uploaded header data (either new, or from media library). |
||
| 1006 | * |
||
| 1007 | * @since 3.4.0 |
||
| 1008 | * |
||
| 1009 | * @param mixed $choice Which header image to select. Allows for values of 'random-default-image', |
||
| 1010 | * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling |
||
| 1011 | * among the uploaded images; the key of a default image registered for that theme; and |
||
| 1012 | * the key of an image uploaded for that theme (the attachment ID of the image). |
||
| 1013 | * Or an array of arguments: attachment_id, url, width, height. All are required. |
||
| 1014 | */ |
||
| 1015 | final public function set_header_image( $choice ) { |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Remove a header image. |
||
| 1061 | * |
||
| 1062 | * @since 3.4.0 |
||
| 1063 | */ |
||
| 1064 | final public function remove_header_image() { |
||
| 1067 | |||
| 1068 | /** |
||
| 1069 | * Reset a header image to the default image for the theme. |
||
| 1070 | * |
||
| 1071 | * This method does not do anything if the theme does not have a default header image. |
||
| 1072 | * |
||
| 1073 | * @since 3.4.0 |
||
| 1074 | */ |
||
| 1075 | final public function reset_header_image() { |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Calculate width and height based on what the currently selected theme supports. |
||
| 1099 | * |
||
| 1100 | * @param array $dimensions |
||
| 1101 | * @return array dst_height and dst_width of header image. |
||
| 1102 | */ |
||
| 1103 | final public function get_header_dimensions( $dimensions ) { |
||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Create an attachment 'object'. |
||
| 1149 | * |
||
| 1150 | * @param string $cropped Cropped image URL. |
||
| 1151 | * @param int $parent_attachment_id Attachment ID of parent image. |
||
| 1152 | * |
||
| 1153 | * @return array Attachment object. |
||
| 1154 | */ |
||
| 1155 | View Code Duplication | final public function create_attachment_object( $cropped, $parent_attachment_id ) { |
|
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Insert an attachment and its metadata. |
||
| 1176 | * |
||
| 1177 | * @param array $object Attachment object. |
||
| 1178 | * @param string $cropped Cropped image URL. |
||
| 1179 | * |
||
| 1180 | * @return int Attachment ID. |
||
| 1181 | */ |
||
| 1182 | View Code Duplication | final public function insert_attachment( $object, $cropped ) { |
|
| 1183 | $attachment_id = wp_insert_attachment( $object, $cropped ); |
||
| 1184 | $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); |
||
| 1185 | /** |
||
| 1186 | * Filters the header image attachment metadata. |
||
| 1187 | * |
||
| 1188 | * @since 3.9.0 |
||
| 1189 | * |
||
| 1190 | * @see wp_generate_attachment_metadata() |
||
| 1191 | * |
||
| 1192 | * @param array $metadata Attachment metadata. |
||
| 1193 | */ |
||
| 1194 | $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata ); |
||
| 1195 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
||
| 1196 | return $attachment_id; |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Gets attachment uploaded by Media Manager, crops it, then saves it as a |
||
| 1201 | * new object. Returns JSON-encoded object details. |
||
| 1202 | */ |
||
| 1203 | public function ajax_header_crop() { |
||
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Given an attachment ID for a header image, updates its "last used" |
||
| 1256 | * timestamp to now. |
||
| 1257 | * |
||
| 1258 | * Triggered when the user tries adds a new header image from the |
||
| 1259 | * Media Manager, even if s/he doesn't save that change. |
||
| 1260 | */ |
||
| 1261 | View Code Duplication | public function ajax_header_add() { |
|
| 1279 | |||
| 1280 | /** |
||
| 1281 | * Given an attachment ID for a header image, unsets it as a user-uploaded |
||
| 1282 | * header image for the current theme. |
||
| 1283 | * |
||
| 1284 | * Triggered when the user clicks the overlay "X" button next to each image |
||
| 1285 | * choice in the Customizer's Header tool. |
||
| 1286 | */ |
||
| 1287 | View Code Duplication | public function ajax_header_remove() { |
|
| 1305 | |||
| 1306 | /** |
||
| 1307 | * |
||
| 1308 | * @param WP_Customize_Manager $wp_customize |
||
| 1309 | */ |
||
| 1310 | public function customize_set_last_used( $wp_customize ) { |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * |
||
| 1324 | * @return array |
||
| 1325 | */ |
||
| 1326 | public function get_default_header_images() { |
||
| 1361 | |||
| 1362 | /** |
||
| 1363 | * |
||
| 1364 | * @return array |
||
| 1365 | */ |
||
| 1366 | public function get_uploaded_header_images() { |
||
| 1379 | } |
||
| 1380 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: