Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/custom-header.php

    r41937 r42343  
    5353     * @param callable $admin_image_div_callback Optional custom image div output callback.
    5454     */
    55     public function __construct($admin_header_callback, $admin_image_div_callback = '') {
    56         $this->admin_header_callback = $admin_header_callback;
     55    public function __construct( $admin_header_callback, $admin_image_div_callback = '' ) {
     56        $this->admin_header_callback    = $admin_header_callback;
    5757        $this->admin_image_div_callback = $admin_image_div_callback;
    5858
    5959        add_action( 'admin_menu', array( $this, 'init' ) );
    6060
    61         add_action( 'customize_save_after',         array( $this, 'customize_set_last_used' ) );
    62         add_action( 'wp_ajax_custom-header-crop',   array( $this, 'ajax_header_crop'        ) );
    63         add_action( 'wp_ajax_custom-header-add',    array( $this, 'ajax_header_add'        ) );
    64         add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove'      ) );
     61        add_action( 'customize_save_after', array( $this, 'customize_set_last_used' ) );
     62        add_action( 'wp_ajax_custom-header-crop', array( $this, 'ajax_header_crop' ) );
     63        add_action( 'wp_ajax_custom-header-add', array( $this, 'ajax_header_add' ) );
     64        add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove' ) );
    6565    }
    6666
     
    9292     */
    9393    public function help() {
    94         get_current_screen()->add_help_tab( array(
    95             'id'      => 'overview',
    96             'title'   => __('Overview'),
    97             'content' =>
    98                 '<p>' . __( 'This screen is used to customize the header section of your theme.') . '</p>' .
    99                 '<p>' . __( 'You can choose from the theme&#8217;s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.') . '<p>'
    100         ) );
    101 
    102         get_current_screen()->add_help_tab( array(
    103             'id'      => 'set-header-image',
    104             'title'   => __('Header Image'),
    105             'content' =>
    106                 '<p>' . __( 'You can set a custom image header for your site. Simply upload the image and crop it, and the new header will go live immediately. Alternatively, you can use an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button.' ) . '</p>' .
    107                 '<p>' . __( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you&#8217;d like and click the &#8220;Save Changes&#8221; button.' ) . '</p>' .
    108                 '<p>' . __( 'If your theme has more than one default header image, or you have uploaded more than one custom header image, you have the option of having WordPress display a randomly different image on each page of your site. Click the &#8220;Random&#8221; radio button next to the Uploaded Images or Default Images section to enable this feature.') . '</p>' .
    109                 '<p>' . __( 'If you don&#8217;t want a header image to be displayed on your site at all, click the &#8220;Remove Header Image&#8221; button at the bottom of the Header Image section of this page. If you want to re-enable the header image later, you just have to select one of the other image options and click &#8220;Save Changes&#8221;.') . '</p>'
    110         ) );
    111 
    112         get_current_screen()->add_help_tab( array(
    113             'id'      => 'set-header-text',
    114             'title'   => __('Header Text'),
    115             'content' =>
    116                 '<p>' . sprintf( __( 'For most themes, the header text is your Site Title and Tagline, as defined in the <a href="%1$s">General Settings</a> section.' ), admin_url( 'options-general.php' ) ) . '<p>' .
    117                 '<p>' . __( 'In the Header Text section of this page, you can choose whether to display this text or hide it. You can also choose a color for the text by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. &#8220;#ff0000&#8221; for red, or by choosing a color using the color picker.' ) . '</p>' .
    118                 '<p>' . __( 'Don&#8217;t forget to click &#8220;Save Changes&#8221; when you&#8217;re done!') . '</p>'
    119         ) );
     94        get_current_screen()->add_help_tab(
     95            array(
     96                'id'      => 'overview',
     97                'title'   => __( 'Overview' ),
     98                'content' =>
     99                    '<p>' . __( 'This screen is used to customize the header section of your theme.' ) . '</p>' .
     100                    '<p>' . __( 'You can choose from the theme&#8217;s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.' ) . '<p>',
     101            )
     102        );
     103
     104        get_current_screen()->add_help_tab(
     105            array(
     106                'id'      => 'set-header-image',
     107                'title'   => __( 'Header Image' ),
     108                'content' =>
     109                    '<p>' . __( 'You can set a custom image header for your site. Simply upload the image and crop it, and the new header will go live immediately. Alternatively, you can use an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button.' ) . '</p>' .
     110                    '<p>' . __( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you&#8217;d like and click the &#8220;Save Changes&#8221; button.' ) . '</p>' .
     111                    '<p>' . __( 'If your theme has more than one default header image, or you have uploaded more than one custom header image, you have the option of having WordPress display a randomly different image on each page of your site. Click the &#8220;Random&#8221; radio button next to the Uploaded Images or Default Images section to enable this feature.' ) . '</p>' .
     112                    '<p>' . __( 'If you don&#8217;t want a header image to be displayed on your site at all, click the &#8220;Remove Header Image&#8221; button at the bottom of the Header Image section of this page. If you want to re-enable the header image later, you just have to select one of the other image options and click &#8220;Save Changes&#8221;.' ) . '</p>',
     113            )
     114        );
     115
     116        get_current_screen()->add_help_tab(
     117            array(
     118                'id'      => 'set-header-text',
     119                'title'   => __( 'Header Text' ),
     120                'content' =>
     121                    '<p>' . sprintf( __( 'For most themes, the header text is your Site Title and Tagline, as defined in the <a href="%1$s">General Settings</a> section.' ), admin_url( 'options-general.php' ) ) . '<p>' .
     122                    '<p>' . __( 'In the Header Text section of this page, you can choose whether to display this text or hide it. You can also choose a color for the text by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. &#8220;#ff0000&#8221; for red, or by choosing a color using the color picker.' ) . '</p>' .
     123                    '<p>' . __( 'Don&#8217;t forget to click &#8220;Save Changes&#8221; when you&#8217;re done!' ) . '</p>',
     124            )
     125        );
    120126
    121127        get_current_screen()->set_help_sidebar(
     
    134140     */
    135141    public function step() {
    136         if ( ! isset( $_GET['step'] ) )
     142        if ( ! isset( $_GET['step'] ) ) {
    137143            return 1;
     144        }
    138145
    139146        $step = (int) $_GET['step'];
     
    141148            ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
    142149            ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
    143         )
     150        ) {
    144151            return 1;
     152        }
    145153
    146154        return $step;
     
    158166            wp_enqueue_media();
    159167            wp_enqueue_script( 'custom-header' );
    160             if ( current_theme_supports( 'custom-header', 'header-text' ) )
     168            if ( current_theme_supports( 'custom-header', 'header-text' ) ) {
    161169                wp_enqueue_script( 'wp-color-picker' );
     170            }
    162171        } elseif ( 2 == $step ) {
    163             wp_enqueue_script('imgareaselect');
     172            wp_enqueue_script( 'imgareaselect' );
    164173        }
    165174    }
     
    173182        $step = $this->step();
    174183
    175         if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
     184        if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
    176185            wp_enqueue_style( 'wp-color-picker' );
    177         elseif ( 2 == $step )
    178             wp_enqueue_style('imgareaselect');
     186        } elseif ( 2 == $step ) {
     187            wp_enqueue_style( 'imgareaselect' );
     188        }
    179189    }
    180190
     
    185195     */
    186196    public function take_action() {
    187         if ( ! current_user_can('edit_theme_options') )
     197        if ( ! current_user_can( 'edit_theme_options' ) ) {
    188198            return;
    189 
    190         if ( empty( $_POST ) )
     199        }
     200
     201        if ( empty( $_POST ) ) {
    191202            return;
     203        }
    192204
    193205        $this->updated = true;
     
    211223            check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
    212224            $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
    213             $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);
    214             if ( strlen($color) == 6 || strlen($color) == 3 )
    215                 set_theme_mod('header_textcolor', $color);
    216             elseif ( ! $color )
     225            $color               = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
     226            if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) {
     227                set_theme_mod( 'header_textcolor', $color );
     228            } elseif ( ! $color ) {
    217229                set_theme_mod( 'header_textcolor', 'blank' );
     230            }
    218231        }
    219232
     
    235248        global $_wp_default_headers;
    236249
    237         if ( !isset($_wp_default_headers) )
     250        if ( ! isset( $_wp_default_headers ) ) {
    238251            return;
     252        }
    239253
    240254        if ( ! empty( $this->default_headers ) ) {
     
    242256        }
    243257
    244         $this->default_headers = $_wp_default_headers;
    245         $template_directory_uri = get_template_directory_uri();
     258        $this->default_headers    = $_wp_default_headers;
     259        $template_directory_uri   = get_template_directory_uri();
    246260        $stylesheet_directory_uri = get_stylesheet_directory_uri();
    247         foreach ( array_keys($this->default_headers) as $header ) {
    248             $this->default_headers[$header]['url'] =  sprintf( $this->default_headers[$header]['url'], $template_directory_uri, $stylesheet_directory_uri );
    249             $this->default_headers[$header]['thumbnail_url'] =  sprintf( $this->default_headers[$header]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
     261        foreach ( array_keys( $this->default_headers ) as $header ) {
     262            $this->default_headers[ $header ]['url']           = sprintf( $this->default_headers[ $header ]['url'], $template_directory_uri, $stylesheet_directory_uri );
     263            $this->default_headers[ $header ]['thumbnail_url'] = sprintf( $this->default_headers[ $header ]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
    250264        }
    251265    }
     
    267281        } else {
    268282            $headers = get_uploaded_header_images();
    269             $type = 'uploaded';
     283            $type    = 'uploaded';
    270284        }
    271285
     
    281295        foreach ( $headers as $header_key => $header ) {
    282296            $header_thumbnail = $header['thumbnail_url'];
    283             $header_url = $header['url'];
    284             $header_alt_text = empty( $header['alt_text'] ) ? '' : $header['alt_text'];
     297            $header_url       = $header['url'];
     298            $header_alt_text  = empty( $header['alt_text'] ) ? '' : $header['alt_text'];
    285299            echo '<div class="default-header">';
    286300            echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
    287301            $width = '';
    288             if ( !empty( $header['attachment_id'] ) )
     302            if ( ! empty( $header['attachment_id'] ) ) {
    289303                $width = ' width="230"';
    290             echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) .'"' . $width . ' /></label>';
     304            }
     305            echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) . '"' . $width . ' /></label>';
    291306            echo '</div>';
    292307        }
     
    301316    public function js() {
    302317        $step = $this->step();
    303         if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
     318        if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
    304319            $this->js_1();
    305         elseif ( 2 == $step )
     320        } elseif ( 2 == $step ) {
    306321            $this->js_2();
     322        }
    307323    }
    308324
     
    373389     * @since 2.6.0
    374390     */
    375     public function js_2() { ?>
     391    public function js_2() {
     392
     393    ?>
    376394<script type="text/javascript">
    377395    function onEndCrop( coords ) {
     
    485503    } else {
    486504        $custom_header = get_custom_header();
    487         $header_image = get_header_image();
     505        $header_image  = get_header_image();
    488506
    489507        if ( $header_image ) {
    490508            $header_image_style = 'background-image:url(' . esc_url( $header_image ) . ');';
    491         }  else {
     509        } else {
    492510            $header_image_style = '';
    493511        }
    494512
    495         if ( $custom_header->width )
     513        if ( $custom_header->width ) {
    496514            $header_image_style .= 'max-width:' . $custom_header->width . 'px;';
    497         if ( $custom_header->height )
     515        }
     516        if ( $custom_header->height ) {
    498517            $header_image_style .= 'height:' . $custom_header->height . 'px;';
     518        }
    499519    ?>
    500520    <div id="headimg" style="<?php echo $header_image_style; ?>">
    501521        <?php
    502         if ( display_header_text() )
     522        if ( display_header_text() ) {
    503523            $style = ' style="color:#' . get_header_textcolor() . ';"';
    504         else
     524        } else {
    505525            $style = ' style="display:none;"';
     526        }
    506527        ?>
    507         <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>
     528        <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>
    508529        <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
    509530    </div>
     
    522543        printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ) );
    523544    } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
    524         if ( ! current_theme_supports( 'custom-header', 'flex-width' ) )
     545        if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
    525546            printf(
    526547                /* translators: %s: size in pixels */
     
    532553                )
    533554            );
     555        }
    534556    } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
    535         if ( ! current_theme_supports( 'custom-header', 'flex-height' ) )
     557        if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
    536558            printf(
    537559                /* translators: %s: size in pixels */
     
    543565                )
    544566            );
     567        }
    545568    }
    546569    if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
    547         if ( current_theme_supports( 'custom-header', 'width' ) )
     570        if ( current_theme_supports( 'custom-header', 'width' ) ) {
    548571            printf(
    549572                /* translators: %s: size in pixels */
     
    555578                )
    556579            );
    557         if ( current_theme_supports( 'custom-header', 'height' ) )
     580        }
     581        if ( current_theme_supports( 'custom-header', 'height' ) ) {
    558582            printf(
    559583                /* translators: %s: size in pixels */
     
    565589                )
    566590            );
    567     }
    568     ?></p>
    569     <form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="<?php echo esc_url( add_query_arg( 'step', 2 ) ) ?>">
     591        }
     592    }
     593    ?>
     594    </p>
     595    <form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="<?php echo esc_url( add_query_arg( 'step', 2 ) ); ?>">
    570596    <p>
    571597        <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
     
    576602    </p>
    577603    <?php
    578         $modal_update_href = esc_url( add_query_arg( array(
    579             'page' => 'custom-header',
    580             'step' => 2,
    581             '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'),
    582         ), admin_url('themes.php') ) );
     604        $modal_update_href = esc_url(
     605            add_query_arg(
     606                array(
     607                    'page'                          => 'custom-header',
     608                    'step'                          => 2,
     609                    '_wpnonce-custom-header-upload' => wp_create_nonce( 'custom-header-upload' ),
     610                ), admin_url( 'themes.php' )
     611            )
     612        );
    583613    ?>
    584614    <p>
     
    596626</table>
    597627
    598 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ) ?>">
     628<form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>">
    599629<?php submit_button( null, 'screen-reader-text', 'save-header-options', false ); ?>
    600630<table class="form-table">
     
    604634<th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
    605635<td>
    606     <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
     636    <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ); ?></p>
    607637    <?php
    608638        $this->show_header_selector( 'uploaded' );
     
    610640</td>
    611641</tr>
    612     <?php endif;
    613     if ( ! empty( $this->default_headers ) ) : ?>
     642    <?php
     643    endif;
     644if ( ! empty( $this->default_headers ) ) :
     645    ?>
    614646<tr>
    615647<th scope="row"><?php _e( 'Default Images' ); ?></th>
    616648<td>
    617649<?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
    618     <p><?php _e( 'If you don&lsquo;t want to upload your own image, you can use one of these cool headers, or show a random one.' ) ?></p>
    619 <?php else: ?>
    620     <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
     650    <p><?php _e( 'If you don&lsquo;t want to upload your own image, you can use one of these cool headers, or show a random one.' ); ?></p>
     651<?php else : ?>
     652    <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ); ?></p>
    621653<?php endif; ?>
    622654    <?php
    623         $this->show_header_selector( 'default' );
     655    $this->show_header_selector( 'default' );
    624656    ?>
    625657</td>
    626658</tr>
    627     <?php endif;
    628     if ( get_header_image() ) : ?>
     659    <?php
     660    endif;
     661if ( get_header_image() ) :
     662    ?>
    629663<tr>
    630664<th scope="row"><?php _e( 'Remove Image' ); ?></th>
    631665<td>
    632     <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
     666    <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ); ?></p>
    633667    <?php submit_button( __( 'Remove Header Image' ), '', 'removeheader', false ); ?>
    634668</td>
    635669</tr>
    636     <?php endif;
     670    <?php
     671    endif;
    637672
    638673    $default_image = sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() );
    639     if ( $default_image && get_header_image() != $default_image ) : ?>
     674if ( $default_image && get_header_image() != $default_image ) :
     675    ?>
    640676<tr>
    641677<th scope="row"><?php _e( 'Reset Image' ); ?></th>
    642678<td>
    643     <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
     679    <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ); ?></p>
    644680    <?php submit_button( __( 'Restore Original Header Image' ), '', 'resetheader', false ); ?>
    645681</td>
     
    694730</tbody>
    695731</table>
    696 <?php endif;
     732<?php
     733endif;
    697734
    698735/**
     
    703740do_action( 'custom_header_options' );
    704741
    705 wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
     742wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' );
     743?>
    706744
    707745<?php submit_button( null, 'primary', 'save-header-options' ); ?>
     
    709747</div>
    710748
    711 <?php }
     749<?php
     750    }
    712751
    713752    /**
     
    717756     */
    718757    public function step_2() {
    719         check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
     758        check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' );
    720759        if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
    721760            wp_die(
     
    728767        if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
    729768            $attachment_id = absint( $_GET['file'] );
    730             $file = get_attached_file( $attachment_id, true );
    731             $url = wp_get_attachment_image_src( $attachment_id, 'full' );
    732             $url = $url[0];
     769            $file          = get_attached_file( $attachment_id, true );
     770            $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
     771            $url           = $url[0];
    733772        } elseif ( isset( $_POST ) ) {
    734             $data = $this->step_2_manage_upload();
     773            $data          = $this->step_2_manage_upload();
    735774            $attachment_id = $data['attachment_id'];
    736             $file = $data['file'];
    737             $url = $data['url'];
     775            $file          = $data['file'];
     776            $url           = $data['url'];
    738777        }
    739778
     
    741780            list( $width, $height, $type, $attr ) = getimagesize( $file );
    742781        } else {
    743             $data = wp_get_attachment_metadata( $attachment_id );
    744             $height = isset( $data[ 'height' ] ) ? $data[ 'height' ] : 0;
    745             $width = isset( $data[ 'width' ] ) ? $data[ 'width' ] : 0;
     782            $data   = wp_get_attachment_metadata( $attachment_id );
     783            $height = isset( $data['height'] ) ? $data['height'] : 0;
     784            $width  = isset( $data['width'] ) ? $data['width'] : 0;
    746785            unset( $data );
    747786        }
     
    749788        $max_width = 0;
    750789        // For flex, limit size of image displayed to 1500px unless theme says otherwise
    751         if ( current_theme_supports( 'custom-header', 'flex-width' ) )
     790        if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
    752791            $max_width = 1500;
    753 
    754         if ( current_theme_supports( 'custom-header', 'max-width' ) )
     792        }
     793
     794        if ( current_theme_supports( 'custom-header', 'max-width' ) ) {
    755795            $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
     796        }
    756797        $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
    757798
    758799        // If flexible height isn't supported and the image is the exact right size
    759800        if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' )
    760             && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) )
    761         {
     801            && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) ) {
    762802            // Add the meta-data
    763             if ( file_exists( $file ) )
     803            if ( file_exists( $file ) ) {
    764804                wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
     805            }
    765806
    766807            $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
     
    779820        } elseif ( $width > $max_width ) {
    780821            $oitar = $width / $max_width;
    781             $image = wp_crop_image($attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
    782             if ( ! $image || is_wp_error( $image ) )
     822            $image = wp_crop_image( $attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace( basename( $file ), 'midsize-' . basename( $file ), $file ) );
     823            if ( ! $image || is_wp_error( $image ) ) {
    783824                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
     825            }
    784826
    785827            /** This filter is documented in wp-admin/custom-header.php */
    786828            $image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication
    787829
    788             $url = str_replace(basename($url), basename($image), $url);
    789             $width = $width / $oitar;
     830            $url    = str_replace( basename( $url ), basename( $image ), $url );
     831            $width  = $width / $oitar;
    790832            $height = $height / $oitar;
    791833        } else {
     
    797839<h1><?php _e( 'Crop Header Image' ); ?></h1>
    798840
    799 <form method="post" action="<?php echo esc_url(add_query_arg('step', 3)); ?>">
    800     <p class="hide-if-no-js"><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
    801     <p class="hide-if-js"><strong><?php _e( 'You need JavaScript to choose a part of the image.'); ?></strong></p>
     841<form method="post" action="<?php echo esc_url( add_query_arg( 'step', 3 ) ); ?>">
     842    <p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.' ); ?></p>
     843    <p class="hide-if-js"><strong><?php _e( 'You need JavaScript to choose a part of the image.' ); ?></strong></p>
    802844
    803845    <div id="crop_image" style="position: relative">
     
    814856    <input type="hidden" name="create-new-attachment" value="true" />
    815857    <?php } ?>
    816     <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
     858    <?php wp_nonce_field( 'custom-header-crop-image' ); ?>
    817859
    818860    <p class="submit">
    819861    <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
    820862    <?php
    821     if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) )
     863    if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) {
    822864        submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false );
     865    }
    823866    ?>
    824867    </p>
     
    835878     */
    836879    public function step_2_manage_upload() {
    837         $overrides = array('test_form' => false);
     880        $overrides = array( 'test_form' => false );
    838881
    839882        $uploaded_file = $_FILES['import'];
    840         $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
    841         if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
     883        $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
     884        if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
    842885            wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
    843 
    844         $file = wp_handle_upload($uploaded_file, $overrides);
    845 
    846         if ( isset($file['error']) )
    847             wp_die( $file['error'],  __( 'Image Upload Error' ) );
    848 
    849         $url = $file['url'];
    850         $type = $file['type'];
    851         $file = $file['file'];
    852         $filename = basename($file);
     886        }
     887
     888        $file = wp_handle_upload( $uploaded_file, $overrides );
     889
     890        if ( isset( $file['error'] ) ) {
     891            wp_die( $file['error'], __( 'Image Upload Error' ) );
     892        }
     893
     894        $url      = $file['url'];
     895        $type     = $file['type'];
     896        $file     = $file['file'];
     897        $filename = basename( $file );
    853898
    854899        // Construct the object array
     
    858903            'post_mime_type' => $type,
    859904            'guid'           => $url,
    860             'context'        => 'custom-header'
     905            'context'        => 'custom-header',
    861906        );
    862907
     
    893938
    894939        if ( $_POST['oitar'] > 1 ) {
    895             $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
    896             $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
    897             $_POST['width'] = $_POST['width'] * $_POST['oitar'];
     940            $_POST['x1']     = $_POST['x1'] * $_POST['oitar'];
     941            $_POST['y1']     = $_POST['y1'] * $_POST['oitar'];
     942            $_POST['width']  = $_POST['width'] * $_POST['oitar'];
    898943            $_POST['height'] = $_POST['height'] * $_POST['oitar'];
    899944        }
    900945
    901946        $attachment_id = absint( $_POST['attachment_id'] );
    902         $original = get_attached_file($attachment_id);
    903 
    904         $dimensions = $this->get_header_dimensions( array(
    905             'height' => $_POST['height'],
    906             'width'  => $_POST['width'],
    907         ) );
    908         $height = $dimensions['dst_height'];
    909         $width = $dimensions['dst_width'];
    910 
    911         if ( empty( $_POST['skip-cropping'] ) )
     947        $original      = get_attached_file( $attachment_id );
     948
     949        $dimensions = $this->get_header_dimensions(
     950            array(
     951                'height' => $_POST['height'],
     952                'width'  => $_POST['width'],
     953            )
     954        );
     955        $height     = $dimensions['dst_height'];
     956        $width      = $dimensions['dst_width'];
     957
     958        if ( empty( $_POST['skip-cropping'] ) ) {
    912959            $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height );
    913         elseif ( ! empty( $_POST['create-new-attachment'] ) )
     960        } elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
    914961            $cropped = _copy_image_file( $attachment_id );
    915         else
     962        } else {
    916963            $cropped = get_attached_file( $attachment_id );
    917 
    918         if ( ! $cropped || is_wp_error( $cropped ) )
     964        }
     965
     966        if ( ! $cropped || is_wp_error( $cropped ) ) {
    919967            wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
     968        }
    920969
    921970        /** This filter is documented in wp-admin/custom-header.php */
     
    924973        $object = $this->create_attachment_object( $cropped, $attachment_id );
    925974
    926         if ( ! empty( $_POST['create-new-attachment'] ) )
     975        if ( ! empty( $_POST['create-new-attachment'] ) ) {
    927976            unset( $object['ID'] );
     977        }
    928978
    929979        // Update the attachment
     
    9621012     */
    9631013    public function admin_page() {
    964         if ( ! current_user_can('edit_theme_options') )
    965             wp_die(__('Sorry, you are not allowed to customize headers.'));
     1014        if ( ! current_user_can( 'edit_theme_options' ) ) {
     1015            wp_die( __( 'Sorry, you are not allowed to customize headers.' ) );
     1016        }
    9661017        $step = $this->step();
    967         if ( 2 == $step )
     1018        if ( 2 == $step ) {
    9681019            $this->step_2();
    969         elseif ( 3 == $step )
     1020        } elseif ( 3 == $step ) {
    9701021            $this->step_3();
    971         else
     1022        } else {
    9721023            $this->step_1();
     1024        }
    9731025    }
    9741026
     
    10041056     *
    10051057     * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
    1006      *  for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
    1007      *  among the uploaded images; the key of a default image registered for that theme; and
    1008      *  the key of an image uploaded for that theme (the attachment ID of the image).
     1058     *  for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
     1059     *  among the uploaded images; the key of a default image registered for that theme; and
     1060     *  the key of an image uploaded for that theme (the attachment ID of the image).
    10091061     *  Or an array of arguments: attachment_id, url, width, height. All are required.
    10101062     */
     
    10121064        if ( is_array( $choice ) || is_object( $choice ) ) {
    10131065            $choice = (array) $choice;
    1014             if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) )
     1066            if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) {
    10151067                return;
     1068            }
    10161069
    10171070            $choice['url'] = esc_url_raw( $choice['url'] );
     
    10431096        } else {
    10441097            $this->process_default_headers();
    1045             if ( isset( $this->default_headers[ $choice ] ) )
     1098            if ( isset( $this->default_headers[ $choice ] ) ) {
    10461099                $header_image_data = $this->default_headers[ $choice ];
    1047             else
     1100            } else {
    10481101                return;
     1102            }
    10491103        }
    10501104
     
    11001154     */
    11011155    final public function get_header_dimensions( $dimensions ) {
    1102         $max_width = 0;
    1103         $width = absint( $dimensions['width'] );
    1104         $height = absint( $dimensions['height'] );
    1105         $theme_height = get_theme_support( 'custom-header', 'height' );
    1106         $theme_width = get_theme_support( 'custom-header', 'width' );
    1107         $has_flex_width = current_theme_supports( 'custom-header', 'flex-width' );
     1156        $max_width       = 0;
     1157        $width           = absint( $dimensions['width'] );
     1158        $height          = absint( $dimensions['height'] );
     1159        $theme_height    = get_theme_support( 'custom-header', 'height' );
     1160        $theme_width     = get_theme_support( 'custom-header', 'width' );
     1161        $has_flex_width  = current_theme_supports( 'custom-header', 'flex-width' );
    11081162        $has_flex_height = current_theme_supports( 'custom-header', 'flex-height' );
    1109         $has_max_width = current_theme_supports( 'custom-header', 'max-width' ) ;
    1110         $dst = array( 'dst_height' => null, 'dst_width' => null );
     1163        $has_max_width   = current_theme_supports( 'custom-header', 'max-width' );
     1164        $dst             = array(
     1165            'dst_height' => null,
     1166            'dst_width'  => null,
     1167        );
    11111168
    11121169        // For flex, limit size of image displayed to 1500px unless theme says otherwise
     
    11221179        if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
    11231180            $dst['dst_height'] = absint( $height * ( $max_width / $width ) );
    1124         }
    1125         elseif ( $has_flex_height && $has_flex_width ) {
     1181        } elseif ( $has_flex_height && $has_flex_width ) {
    11261182            $dst['dst_height'] = $height;
    1127         }
    1128         else {
     1183        } else {
    11291184            $dst['dst_height'] = $theme_height;
    11301185        }
     
    11321187        if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
    11331188            $dst['dst_width'] = absint( $width * ( $max_width / $width ) );
    1134         }
    1135         elseif ( $has_flex_width && $has_flex_height ) {
     1189        } elseif ( $has_flex_width && $has_flex_height ) {
    11361190            $dst['dst_width'] = $width;
    1137         }
    1138         else {
     1191        } else {
    11391192            $dst['dst_width'] = $theme_width;
    11401193        }
     
    11531206     */
    11541207    final public function create_attachment_object( $cropped, $parent_attachment_id ) {
    1155         $parent = get_post( $parent_attachment_id );
     1208        $parent     = get_post( $parent_attachment_id );
    11561209        $parent_url = wp_get_attachment_url( $parent->ID );
    1157         $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
    1158 
    1159         $size = @getimagesize( $cropped );
     1210        $url        = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
     1211
     1212        $size       = @getimagesize( $cropped );
    11601213        $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
    11611214
    11621215        $object = array(
    1163             'ID' => $parent_attachment_id,
    1164             'post_title' => basename($cropped),
     1216            'ID'             => $parent_attachment_id,
     1217            'post_title'     => basename( $cropped ),
    11651218            'post_mime_type' => $image_type,
    1166             'guid' => $url,
    1167             'context' => 'custom-header',
    1168             'post_parent' => $parent_attachment_id,
     1219            'guid'           => $url,
     1220            'context'        => 'custom-header',
     1221            'post_parent'    => $parent_attachment_id,
    11691222        );
    11701223
     
    11861239
    11871240        $attachment_id = wp_insert_attachment( $object, $cropped );
    1188         $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
     1241        $metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
    11891242
    11901243        // If this is a crop, save the original attachment ID as metadata.
     
    12281281        $crop_details = $_POST['cropDetails'];
    12291282
    1230         $dimensions = $this->get_header_dimensions( array(
    1231             'height' => $crop_details['height'],
    1232             'width'  => $crop_details['width'],
    1233         ) );
     1283        $dimensions = $this->get_header_dimensions(
     1284            array(
     1285                'height' => $crop_details['height'],
     1286                'width'  => $crop_details['width'],
     1287            )
     1288        );
    12341289
    12351290        $attachment_id = absint( $_POST['id'] );
     
    12651320
    12661321        $object['attachment_id'] = $new_attachment_id;
    1267         $object['url']           = wp_get_attachment_url( $new_attachment_id );;
    1268         $object['width']         = $dimensions['dst_width'];
    1269         $object['height']        = $dimensions['dst_height'];
     1322        $object['url']           = wp_get_attachment_url( $new_attachment_id );
     1323
     1324        $object['width']  = $dimensions['dst_width'];
     1325        $object['height'] = $dimensions['dst_height'];
    12701326
    12711327        wp_send_json_success( $object );
     
    13481404
    13491405        $attachment_id = $data['attachment_id'];
    1350         $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
     1406        $key           = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
    13511407        update_post_meta( $attachment_id, $key, time() );
    13521408    }
     
    13691425        }
    13701426
    1371         $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1427        $default             = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
    13721428        $already_has_default = false;
    13731429
     
    13841440
    13851441        // If the one true image isn't included in the default set, prepend it.
    1386         $header_images = array();
     1442        $header_images            = array();
    13871443        $header_images['default'] = array(
    13881444            'url'           => $default,
    13891445            'thumbnail_url' => $default,
    1390             'description'   => 'Default'
     1446            'description'   => 'Default',
    13911447        );
    13921448
     
    14051461        $header_images = get_uploaded_header_images();
    14061462        $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
    1407         $alt_text_key = '_wp_attachment_image_alt';
     1463        $alt_text_key  = '_wp_attachment_image_alt';
    14081464
    14091465        foreach ( $header_images as &$header_image ) {
    1410             $header_meta = get_post_meta( $header_image['attachment_id'] );
     1466            $header_meta               = get_post_meta( $header_image['attachment_id'] );
    14111467            $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
    1412             $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
     1468            $header_image['alt_text']  = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
    14131469        }
    14141470
Note: See TracChangeset for help on using the changeset viewer.