From 66b035ab3ba2e80446b020071a6fa4d01eeb6106 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 13 Apr 2021 19:03:07 +0000 Subject: [PATCH] Coding Standards: Use strict comparison in `wp-admin/includes/class-custom-image-header.php`. Includes minor code layout fixes for better readability. See #52627. git-svn-id: https://develop.svn.wordpress.org/trunk@50707 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-custom-image-header.php | 153 ++++++++++++++---- 1 file changed, 120 insertions(+), 33 deletions(-) diff --git a/src/wp-admin/includes/class-custom-image-header.php b/src/wp-admin/includes/class-custom-image-header.php index 4702643c36..68fc78797d 100644 --- a/src/wp-admin/includes/class-custom-image-header.php +++ b/src/wp-admin/includes/class-custom-image-header.php @@ -71,6 +71,7 @@ class Custom_Image_Header { */ public function init() { $page = add_theme_page( __( 'Header' ), __( 'Header' ), 'edit_theme_options', 'custom-header', array( $this, 'admin_page' ) ); + if ( ! $page ) { return; } @@ -80,6 +81,7 @@ class Custom_Image_Header { add_action( "admin_head-{$page}", array( $this, 'help' ) ); add_action( "admin_head-{$page}", array( $this, 'take_action' ), 50 ); add_action( "admin_head-{$page}", array( $this, 'js' ), 50 ); + if ( $this->admin_header_callback ) { add_action( "admin_head-{$page}", $this->admin_header_callback, 51 ); } @@ -141,7 +143,7 @@ class Custom_Image_Header { * * @since 2.6.0 * - * @return int Current step + * @return int Current step. */ public function step() { if ( ! isset( $_GET['step'] ) ) { @@ -150,8 +152,8 @@ class Custom_Image_Header { $step = (int) $_GET['step']; if ( $step < 1 || 3 < $step || - ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) || - ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) ) + ( 2 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) || + ( 3 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) ) ) { return 1; } @@ -167,13 +169,13 @@ class Custom_Image_Header { public function js_includes() { $step = $this->step(); - if ( ( 1 == $step || 3 == $step ) ) { + if ( ( 1 === $step || 3 === $step ) ) { wp_enqueue_media(); wp_enqueue_script( 'custom-header' ); if ( current_theme_supports( 'custom-header', 'header-text' ) ) { wp_enqueue_script( 'wp-color-picker' ); } - } elseif ( 2 == $step ) { + } elseif ( 2 === $step ) { wp_enqueue_script( 'imgareaselect' ); } } @@ -186,9 +188,9 @@ class Custom_Image_Header { public function css_includes() { $step = $this->step(); - if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) { + if ( ( 1 === $step || 3 === $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) { wp_enqueue_style( 'wp-color-picker' ); - } elseif ( 2 == $step ) { + } elseif ( 2 === $step ) { wp_enqueue_style( 'imgareaselect' ); } } @@ -211,24 +213,32 @@ class Custom_Image_Header { if ( isset( $_POST['resetheader'] ) ) { check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); + $this->reset_header_image(); + return; } if ( isset( $_POST['removeheader'] ) ) { check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); + $this->remove_header_image(); + return; } if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) { check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); + set_theme_mod( 'header_textcolor', 'blank' ); } elseif ( isset( $_POST['text-color'] ) ) { check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); + $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] ); - $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] ); - if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) { + + $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] ); + + if ( strlen( $color ) === 6 || strlen( $color ) === 3 ) { set_theme_mod( 'header_textcolor', $color ); } elseif ( ! $color ) { set_theme_mod( 'header_textcolor', 'blank' ); @@ -237,7 +247,9 @@ class Custom_Image_Header { if ( isset( $_POST['default-header'] ) ) { check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); + $this->set_header_image( $_POST['default-header'] ); + return; } } @@ -263,9 +275,19 @@ class Custom_Image_Header { $this->default_headers = $_wp_default_headers; $template_directory_uri = get_template_directory_uri(); $stylesheet_directory_uri = get_stylesheet_directory_uri(); + foreach ( array_keys( $this->default_headers ) as $header ) { - $this->default_headers[ $header ]['url'] = sprintf( $this->default_headers[ $header ]['url'], $template_directory_uri, $stylesheet_directory_uri ); - $this->default_headers[ $header ]['thumbnail_url'] = sprintf( $this->default_headers[ $header ]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri ); + $this->default_headers[ $header ]['url'] = sprintf( + $this->default_headers[ $header ]['url'], + $template_directory_uri, + $stylesheet_directory_uri + ); + + $this->default_headers[ $header ]['thumbnail_url'] = sprintf( + $this->default_headers[ $header ]['thumbnail_url'], + $template_directory_uri, + $stylesheet_directory_uri + ); } } @@ -297,10 +319,12 @@ class Custom_Image_Header { } echo '
'; + foreach ( $headers as $header_key => $header ) { $header_thumbnail = $header['thumbnail_url']; $header_url = $header['url']; $header_alt_text = empty( $header['alt_text'] ) ? '' : $header['alt_text']; + echo '
'; echo ''; echo '
'; } + echo '
'; } @@ -320,9 +345,10 @@ class Custom_Image_Header { */ public function js() { $step = $this->step(); - if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) { + + if ( ( 1 === $step || 3 === $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) { $this->js_1(); - } elseif ( 2 == $step ) { + } elseif ( 2 === $step ) { $this->js_2(); } } @@ -430,7 +456,9 @@ class Custom_Image_Header { x2: xinit, y2: yinit, aspectRatio: xinit + ':' + yinit,


%1$d × %2$d pixels will be used as-is.' ) . '
', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ) ); + if ( ! current_theme_supports( 'custom-header', 'flex-height' ) + && ! current_theme_supports( 'custom-header', 'flex-width' ) + ) { + printf( + /* translators: 1: Image width in pixels, 2: Image height in pixels. */ + __( 'Images of exactly %1$d × %2$d pixels will be used as-is.' ) . '
', + get_theme_support( 'custom-header', 'width' ), + get_theme_support( 'custom-header', 'height' ) + ); } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) { if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) { printf( @@ -578,7 +612,10 @@ class Custom_Image_Header { ); } } - if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) { + + if ( current_theme_supports( 'custom-header', 'flex-height' ) + || current_theme_supports( 'custom-header', 'flex-width' ) + ) { if ( current_theme_supports( 'custom-header', 'width' ) ) { printf( /* translators: %s: Size in pixels. */ @@ -590,6 +627,7 @@ class Custom_Image_Header { ) ); } + if ( current_theme_supports( 'custom-header', 'height' ) ) { printf( /* translators: %s: Size in pixels. */ @@ -683,8 +721,13 @@ class Custom_Image_Header { @@ -770,6 +813,7 @@ endif; */ public function step_2() { check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' ); + if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) { wp_die( '

' . __( 'Something went wrong.' ) . '

' . @@ -794,12 +838,13 @@ endif; list( $width, $height, $type, $attr ) = wp_getimagesize( $file ); } else { $data = wp_get_attachment_metadata( $attachment_id ); - $height = isset( $data['height'] ) ? $data['height'] : 0; - $width = isset( $data['width'] ) ? $data['width'] : 0; + $height = isset( $data['height'] ) ? (int) $data['height'] : 0; + $width = isset( $data['width'] ) ? (int) $data['width'] : 0; unset( $data ); } $max_width = 0; + // For flex, limit size of image displayed to 1500px unless theme says otherwise. if ( current_theme_supports( 'custom-header', 'flex-width' ) ) { $max_width = 1500; @@ -808,11 +853,15 @@ endif; if ( current_theme_supports( 'custom-header', 'max-width' ) ) { $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) ); } + $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) ); // If flexible height isn't supported and the image is the exact right size. - if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) - && get_theme_support( 'custom-header', 'width' ) == $width && get_theme_support( 'custom-header', 'height' ) == $height ) { + if ( ! current_theme_supports( 'custom-header', 'flex-height' ) + && ! current_theme_supports( 'custom-header', 'flex-width' ) + && (int) get_theme_support( 'custom-header', 'width' ) === $width + && (int) get_theme_support( 'custom-header', 'height' ) === $height + ) { // Add the metadata. if ( file_exists( $file ) ) { wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); @@ -833,7 +882,19 @@ endif; return $this->finished(); } elseif ( $width > $max_width ) { $oitar = $width / $max_width; - $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 ) ); + + $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 ) + ); + if ( ! $image || is_wp_error( $image ) ) { wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); } @@ -874,7 +935,10 @@ endif;

@@ -895,6 +959,7 @@ endif; $uploaded_file = $_FILES['import']; $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] ); + if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) ); } @@ -921,6 +986,7 @@ endif; // Save the data. $attachment_id = wp_insert_attachment( $object, $file ); + return compact( 'attachment_id', 'file', 'filename', 'url', 'type' ); } @@ -942,7 +1008,10 @@ endif; ); } - if ( ! empty( $_POST['skip-cropping'] ) && ! ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) { + if ( ! empty( $_POST['skip-cropping'] ) + && ! current_theme_supports( 'custom-header', 'flex-height' ) + && ! current_theme_supports( 'custom-header', 'flex-width' ) + ) { wp_die( '

' . __( 'Something went wrong.' ) . '

' . '

' . __( 'The current theme does not support a flexible sized header image.' ) . '

', @@ -970,7 +1039,15 @@ endif; $width = $dimensions['dst_width']; if ( empty( $_POST['skip-cropping'] ) ) { - $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height ); + $cropped = wp_crop_image( + $attachment_id, + (int) $_POST['x1'], + (int) $_POST['y1'], + (int) $_POST['width'], + (int) $_POST['height'], + $width, + $height + ); } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { $cropped = _copy_image_file( $attachment_id ); } else { @@ -1028,10 +1105,12 @@ endif; if ( ! current_user_can( 'edit_theme_options' ) ) { wp_die( __( 'Sorry, you are not allowed to customize headers.' ) ); } + $step = $this->step(); - if ( 2 == $step ) { + + if ( 2 === $step ) { $this->step_2(); - } elseif ( 3 == $step ) { + } elseif ( 3 === $step ) { $this->step_3(); } else { $this->step_1(); @@ -1077,6 +1156,7 @@ endif; final public function set_header_image( $choice ) { if ( is_array( $choice ) || is_object( $choice ) ) { $choice = (array) $choice; + if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) { return; } @@ -1092,21 +1172,24 @@ endif; ); update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() ); + set_theme_mod( 'header_image', $choice['url'] ); set_theme_mod( 'header_image_data', $header_image_data ); + return; } if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ), true ) ) { set_theme_mod( 'header_image', $choice ); remove_theme_mod( 'header_image_data' ); + return; } $uploaded = get_uploaded_header_images(); + if ( $uploaded && isset( $uploaded[ $choice ] ) ) { $header_image_data = $uploaded[ $choice ]; - } else { $this->process_default_headers(); if ( isset( $this->default_headers[ $choice ] ) ) { @@ -1144,11 +1227,12 @@ endif; $this->remove_header_image(); return; } + $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); $default_data = array(); foreach ( $this->default_headers as $header => $details ) { - if ( $details['url'] == $default ) { + if ( $details['url'] === $default ) { $default_data = $details; break; } @@ -1407,9 +1491,11 @@ endif; public function customize_set_last_used( $wp_customize ) { $header_image_data_setting = $wp_customize->get_setting( 'header_image_data' ); + if ( ! $header_image_data_setting ) { return; } + $data = $header_image_data_setting->post_value(); if ( ! isset( $data['attachment_id'] ) ) { @@ -1438,7 +1524,8 @@ endif; return $this->default_headers; } - $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); + $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); + $already_has_default = false; foreach ( $this->default_headers as $k => $h ) {