Make WordPress Core

Changeset 50707


Ignore:
Timestamp:
04/13/2021 07:03:07 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use strict comparison in wp-admin/includes/class-custom-image-header.php.

Includes minor code layout fixes for better readability.

See #52627.

File:
1 edited

Legend:

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

    r50556 r50707  
    7272        public function init() {
    7373                $page = add_theme_page( __( 'Header' ), __( 'Header' ), 'edit_theme_options', 'custom-header', array( $this, 'admin_page' ) );
     74
    7475                if ( ! $page ) {
    7576                        return;
    … …  
    8182                add_action( "admin_head-{$page}", array( $this, 'take_action' ), 50 );
    8283                add_action( "admin_head-{$page}", array( $this, 'js' ), 50 );
     84
    8385                if ( $this->admin_header_callback ) {
    8486                        add_action( "admin_head-{$page}", $this->admin_header_callback, 51 );
    … …  
    142144         * @since 2.6.0
    143145         *
    144          * @return int Current step
     146         * @return int Current step.
    145147         */
    146148        public function step() {
    … …  
    151153                $step = (int) $_GET['step'];
    152154                if ( $step < 1 || 3 < $step ||
    153                         ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
    154                         ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
     155                        ( 2 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
     156                        ( 3 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
    155157                ) {
    156158                        return 1;
    … …  
    168170                $step = $this->step();
    169171
    170                 if ( ( 1 == $step || 3 == $step ) ) {
     172                if ( ( 1 === $step || 3 === $step ) ) {
    171173                        wp_enqueue_media();
    172174                        wp_enqueue_script( 'custom-header' );
    … …  
    174176                                wp_enqueue_script( 'wp-color-picker' );
    175177                        }
    176                 } elseif ( 2 == $step ) {
     178                } elseif ( 2 === $step ) {
    177179                        wp_enqueue_script( 'imgareaselect' );
    178180                }
    … …  
    187189                $step = $this->step();
    188190
    189                 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
     191                if ( ( 1 === $step || 3 === $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
    190192                        wp_enqueue_style( 'wp-color-picker' );
    191                 } elseif ( 2 == $step ) {
     193                } elseif ( 2 === $step ) {
    192194                        wp_enqueue_style( 'imgareaselect' );
    193195                }
    … …  
    212214                if ( isset( $_POST['resetheader'] ) ) {
    213215                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     216
    214217                        $this->reset_header_image();
     218
    215219                        return;
    216220                }
    … …  
    218222                if ( isset( $_POST['removeheader'] ) ) {
    219223                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     224
    220225                        $this->remove_header_image();
     226
    221227                        return;
    222228                }
    … …  
    224230                if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) {
    225231                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     232
    226233                        set_theme_mod( 'header_textcolor', 'blank' );
    227234                } elseif ( isset( $_POST['text-color'] ) ) {
    228235                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     236
    229237                        $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
    230                         $color               = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
    231                         if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) {
     238
     239                        $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
     240
     241                        if ( strlen( $color ) === 6 || strlen( $color ) === 3 ) {
    232242                                set_theme_mod( 'header_textcolor', $color );
    233243                        } elseif ( ! $color ) {
    … …  
    238248                if ( isset( $_POST['default-header'] ) ) {
    239249                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
     250
    240251                        $this->set_header_image( $_POST['default-header'] );
     252
    241253                        return;
    242254                }
    … …  
    264276                $template_directory_uri   = get_template_directory_uri();
    265277                $stylesheet_directory_uri = get_stylesheet_directory_uri();
     278
    266279                foreach ( array_keys( $this->default_headers ) as $header ) {
    267                         $this->default_headers[ $header ]['url']           = sprintf( $this->default_headers[ $header ]['url'], $template_directory_uri, $stylesheet_directory_uri );
    268                         $this->default_headers[ $header ]['thumbnail_url'] = sprintf( $this->default_headers[ $header ]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
     280                        $this->default_headers[ $header ]['url'] = sprintf(
     281                                $this->default_headers[ $header ]['url'],
     282                                $template_directory_uri,
     283                                $stylesheet_directory_uri
     284                        );
     285
     286                        $this->default_headers[ $header ]['thumbnail_url'] = sprintf(
     287                                $this->default_headers[ $header ]['thumbnail_url'],
     288                                $template_directory_uri,
     289                                $stylesheet_directory_uri
     290                        );
    269291                }
    270292        }
    … …  
    298320
    299321                echo '<div class="available-headers">';
     322
    300323                foreach ( $headers as $header_key => $header ) {
    301324                        $header_thumbnail = $header['thumbnail_url'];
    302325                        $header_url       = $header['url'];
    303326                        $header_alt_text  = empty( $header['alt_text'] ) ? '' : $header['alt_text'];
     327
    304328                        echo '<div class="default-header">';
    305329                        echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
    … …  
    311335                        echo '</div>';
    312336                }
     337
    313338                echo '<div class="clear"></div></div>';
    314339        }
    … …  
    321346        public function js() {
    322347                $step = $this->step();
    323                 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
     348
     349                if ( ( 1 === $step || 3 === $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
    324350                        $this->js_1();
    325                 } elseif ( 2 == $step ) {
     351                } elseif ( 2 === $step ) {
    326352                        $this->js_2();
    327353                }
    … …  
    431457                        y2: yinit,
    432458                        <?php
    433                         if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
     459                        if ( ! current_theme_supports( 'custom-header', 'flex-height' )
     460                                && ! current_theme_supports( 'custom-header', 'flex-width' )
     461                        ) {
    434462                                ?>
    435463                        aspectRatio: xinit + ':' + yinit,
    … …  
    551579        <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 />
    552580                        <?php
    553                         if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
    554                                 /* translators: 1: Image width in pixels, 2: Image height in pixels. */
    555                                 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' ) );
     581                        if ( ! current_theme_supports( 'custom-header', 'flex-height' )
     582                                && ! current_theme_supports( 'custom-header', 'flex-width' )
     583                        ) {
     584                                printf(
     585                                        /* translators: 1: Image width in pixels, 2: Image height in pixels. */
     586                                        __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />',
     587                                        get_theme_support( 'custom-header', 'width' ),
     588                                        get_theme_support( 'custom-header', 'height' )
     589                                );
    556590                        } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
    557591                                if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
    … …  
    579613                                }
    580614                        }
    581                         if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
     615
     616                        if ( current_theme_supports( 'custom-header', 'flex-height' )
     617                                || current_theme_supports( 'custom-header', 'flex-width' )
     618                        ) {
    582619                                if ( current_theme_supports( 'custom-header', 'width' ) ) {
    583620                                        printf(
    … …  
    591628                                        );
    592629                                }
     630
    593631                                if ( current_theme_supports( 'custom-header', 'height' ) ) {
    594632                                        printf(
    … …  
    684722        endif;
    685723
    686                 $default_image = sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() );
    687                 if ( $default_image && get_header_image() != $default_image ) :
     724                $default_image = sprintf(
     725                        get_theme_support( 'custom-header', 'default-image' ),
     726                        get_template_directory_uri(),
     727                        get_stylesheet_directory_uri()
     728                );
     729
     730                if ( $default_image && get_header_image() !== $default_image ) :
    688731                        ?>
    689732<tr>
    … …  
    771814        public function step_2() {
    772815                check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' );
     816
    773817                if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
    774818                        wp_die(
    … …  
    795839                } else {
    796840                        $data   = wp_get_attachment_metadata( $attachment_id );
    797                         $height = isset( $data['height'] ) ? $data['height'] : 0;
    798                         $width  = isset( $data['width'] ) ? $data['width'] : 0;
     841                        $height = isset( $data['height'] ) ? (int) $data['height'] : 0;
     842                        $width  = isset( $data['width'] ) ? (int) $data['width'] : 0;
    799843                        unset( $data );
    800844                }
    801845
    802846                $max_width = 0;
     847
    803848                // For flex, limit size of image displayed to 1500px unless theme says otherwise.
    804849                if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
    … …  
    809854                        $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
    810855                }
     856
    811857                $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
    812858
    813859                // If flexible height isn't supported and the image is the exact right size.
    814                 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' )
    815                         && get_theme_support( 'custom-header', 'width' ) == $width && get_theme_support( 'custom-header', 'height' ) == $height ) {
     860                if ( ! current_theme_supports( 'custom-header', 'flex-height' )
     861                        && ! current_theme_supports( 'custom-header', 'flex-width' )
     862                        && (int) get_theme_support( 'custom-header', 'width' ) === $width
     863                        && (int) get_theme_support( 'custom-header', 'height' ) === $height
     864                ) {
    816865                        // Add the metadata.
    817866                        if ( file_exists( $file ) ) {
    … …  
    834883                } elseif ( $width > $max_width ) {
    835884                        $oitar = $width / $max_width;
    836                         $image = wp_crop_image( $attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace( wp_basename( $file ), 'midsize-' . wp_basename( $file ), $file ) );
     885
     886                        $image = wp_crop_image(
     887                                $attachment_id,
     888                                0,
     889                                0,
     890                                $width,
     891                                $height,
     892                                $max_width,
     893                                $height / $oitar,
     894                                false,
     895                                str_replace( wp_basename( $file ), 'midsize-' . wp_basename( $file ), $file )
     896                        );
     897
    837898                        if ( ! $image || is_wp_error( $image ) ) {
    838899                                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    … …  
    875936                <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
    876937                <?php
    877                 if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) {
     938                if ( isset( $oitar ) && 1 === $oitar
     939                        && ( current_theme_supports( 'custom-header', 'flex-height' )
     940                                || current_theme_supports( 'custom-header', 'flex-width' ) )
     941                ) {
    878942                        submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false );
    879943                }
    … …  
    896960                $uploaded_file = $_FILES['import'];
    897961                $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
     962
    898963                if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
    899964                        wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
    … …  
    922987                // Save the data.
    923988                $attachment_id = wp_insert_attachment( $object, $file );
     989
    924990                return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
    925991        }
    … …  
    9431009                }
    9441010
    945                 if ( ! empty( $_POST['skip-cropping'] ) && ! ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) {
     1011                if ( ! empty( $_POST['skip-cropping'] )
     1012                        && ! current_theme_supports( 'custom-header', 'flex-height' )
     1013                        && ! current_theme_supports( 'custom-header', 'flex-width' )
     1014                ) {
    9461015                        wp_die(
    9471016                                '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
    … …  
    9711040
    9721041                if ( empty( $_POST['skip-cropping'] ) ) {
    973                         $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height );
     1042                        $cropped = wp_crop_image(
     1043                                $attachment_id,
     1044                                (int) $_POST['x1'],
     1045                                (int) $_POST['y1'],
     1046                                (int) $_POST['width'],
     1047                                (int) $_POST['height'],
     1048                                $width,
     1049                                $height
     1050                        );
    9741051                } elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
    9751052                        $cropped = _copy_image_file( $attachment_id );
    … …  
    10291106                        wp_die( __( 'Sorry, you are not allowed to customize headers.' ) );
    10301107                }
     1108
    10311109                $step = $this->step();
    1032                 if ( 2 == $step ) {
     1110
     1111                if ( 2 === $step ) {
    10331112                        $this->step_2();
    1034                 } elseif ( 3 == $step ) {
     1113                } elseif ( 3 === $step ) {
    10351114                        $this->step_3();
    10361115                } else {
    … …  
    10781157                if ( is_array( $choice ) || is_object( $choice ) ) {
    10791158                        $choice = (array) $choice;
     1159
    10801160                        if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) {
    10811161                                return;
    … …  
    10931173
    10941174                        update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
     1175
    10951176                        set_theme_mod( 'header_image', $choice['url'] );
    10961177                        set_theme_mod( 'header_image_data', $header_image_data );
     1178
    10971179                        return;
    10981180                }
    … …  
    11011183                        set_theme_mod( 'header_image', $choice );
    11021184                        remove_theme_mod( 'header_image_data' );
     1185
    11031186                        return;
    11041187                }
    11051188
    11061189                $uploaded = get_uploaded_header_images();
     1190
    11071191                if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
    11081192                        $header_image_data = $uploaded[ $choice ];
    1109 
    11101193                } else {
    11111194                        $this->process_default_headers();
    … …  
    11451228                        return;
    11461229                }
     1230
    11471231                $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
    11481232
    11491233                $default_data = array();
    11501234                foreach ( $this->default_headers as $header => $details ) {
    1151                         if ( $details['url'] == $default ) {
     1235                        if ( $details['url'] === $default ) {
    11521236                                $default_data = $details;
    11531237                                break;
    … …  
    14081492
    14091493                $header_image_data_setting = $wp_customize->get_setting( 'header_image_data' );
     1494
    14101495                if ( ! $header_image_data_setting ) {
    14111496                        return;
    14121497                }
     1498
    14131499                $data = $header_image_data_setting->post_value();
    14141500
    … …  
    14391525                }
    14401526
    1441                 $default             = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1527                $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1528
    14421529                $already_has_default = false;
    14431530
Note: See TracChangeset for help on using the changeset viewer.