- Timestamp:
- 04/13/2021 07:03:07 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-custom-image-header.php
r50556 r50707 72 72 public function init() { 73 73 $page = add_theme_page( __( 'Header' ), __( 'Header' ), 'edit_theme_options', 'custom-header', array( $this, 'admin_page' ) ); 74 74 75 if ( ! $page ) { 75 76 return; … … 81 82 add_action( "admin_head-{$page}", array( $this, 'take_action' ), 50 ); 82 83 add_action( "admin_head-{$page}", array( $this, 'js' ), 50 ); 84 83 85 if ( $this->admin_header_callback ) { 84 86 add_action( "admin_head-{$page}", $this->admin_header_callback, 51 ); … … 142 144 * @since 2.6.0 143 145 * 144 * @return int Current step 146 * @return int Current step. 145 147 */ 146 148 public function step() { … … 151 153 $step = (int) $_GET['step']; 152 154 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' ) ) 155 157 ) { 156 158 return 1; … … 168 170 $step = $this->step(); 169 171 170 if ( ( 1 == $step || 3== $step ) ) {172 if ( ( 1 === $step || 3 === $step ) ) { 171 173 wp_enqueue_media(); 172 174 wp_enqueue_script( 'custom-header' ); … … 174 176 wp_enqueue_script( 'wp-color-picker' ); 175 177 } 176 } elseif ( 2 == $step ) {178 } elseif ( 2 === $step ) { 177 179 wp_enqueue_script( 'imgareaselect' ); 178 180 } … … 187 189 $step = $this->step(); 188 190 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' ) ) { 190 192 wp_enqueue_style( 'wp-color-picker' ); 191 } elseif ( 2 == $step ) {193 } elseif ( 2 === $step ) { 192 194 wp_enqueue_style( 'imgareaselect' ); 193 195 } … … 212 214 if ( isset( $_POST['resetheader'] ) ) { 213 215 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); 216 214 217 $this->reset_header_image(); 218 215 219 return; 216 220 } … … 218 222 if ( isset( $_POST['removeheader'] ) ) { 219 223 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); 224 220 225 $this->remove_header_image(); 226 221 227 return; 222 228 } … … 224 230 if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) { 225 231 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); 232 226 233 set_theme_mod( 'header_textcolor', 'blank' ); 227 234 } elseif ( isset( $_POST['text-color'] ) ) { 228 235 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); 236 229 237 $_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 ) { 232 242 set_theme_mod( 'header_textcolor', $color ); 233 243 } elseif ( ! $color ) { … … 238 248 if ( isset( $_POST['default-header'] ) ) { 239 249 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); 250 240 251 $this->set_header_image( $_POST['default-header'] ); 252 241 253 return; 242 254 } … … 264 276 $template_directory_uri = get_template_directory_uri(); 265 277 $stylesheet_directory_uri = get_stylesheet_directory_uri(); 278 266 279 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 ); 269 291 } 270 292 } … … 298 320 299 321 echo '<div class="available-headers">'; 322 300 323 foreach ( $headers as $header_key => $header ) { 301 324 $header_thumbnail = $header['thumbnail_url']; 302 325 $header_url = $header['url']; 303 326 $header_alt_text = empty( $header['alt_text'] ) ? '' : $header['alt_text']; 327 304 328 echo '<div class="default-header">'; 305 329 echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />'; … … 311 335 echo '</div>'; 312 336 } 337 313 338 echo '<div class="clear"></div></div>'; 314 339 } … … 321 346 public function js() { 322 347 $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' ) ) { 324 350 $this->js_1(); 325 } elseif ( 2 == $step ) {351 } elseif ( 2 === $step ) { 326 352 $this->js_2(); 327 353 } … … 431 457 y2: yinit, 432 458 <?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 ) { 434 462 ?> 435 463 aspectRatio: xinit + ':' + yinit, … … 551 579 <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 /> 552 580 <?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 × %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 × %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 ); 556 590 } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) { 557 591 if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) { … … 579 613 } 580 614 } 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 ) { 582 619 if ( current_theme_supports( 'custom-header', 'width' ) ) { 583 620 printf( … … 591 628 ); 592 629 } 630 593 631 if ( current_theme_supports( 'custom-header', 'height' ) ) { 594 632 printf( … … 684 722 endif; 685 723 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 ) : 688 731 ?> 689 732 <tr> … … 771 814 public function step_2() { 772 815 check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' ); 816 773 817 if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) { 774 818 wp_die( … … 795 839 } else { 796 840 $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; 799 843 unset( $data ); 800 844 } 801 845 802 846 $max_width = 0; 847 803 848 // For flex, limit size of image displayed to 1500px unless theme says otherwise. 804 849 if ( current_theme_supports( 'custom-header', 'flex-width' ) ) { … … 809 854 $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) ); 810 855 } 856 811 857 $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) ); 812 858 813 859 // 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 ) { 816 865 // Add the metadata. 817 866 if ( file_exists( $file ) ) { … … 834 883 } elseif ( $width > $max_width ) { 835 884 $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 837 898 if ( ! $image || is_wp_error( $image ) ) { 838 899 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); … … 875 936 <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?> 876 937 <?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 ) { 878 942 submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false ); 879 943 } … … 896 960 $uploaded_file = $_FILES['import']; 897 961 $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] ); 962 898 963 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { 899 964 wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) ); … … 922 987 // Save the data. 923 988 $attachment_id = wp_insert_attachment( $object, $file ); 989 924 990 return compact( 'attachment_id', 'file', 'filename', 'url', 'type' ); 925 991 } … … 943 1009 } 944 1010 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 ) { 946 1015 wp_die( 947 1016 '<h1>' . __( 'Something went wrong.' ) . '</h1>' . … … 971 1040 972 1041 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 ); 974 1051 } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { 975 1052 $cropped = _copy_image_file( $attachment_id ); … … 1029 1106 wp_die( __( 'Sorry, you are not allowed to customize headers.' ) ); 1030 1107 } 1108 1031 1109 $step = $this->step(); 1032 if ( 2 == $step ) { 1110 1111 if ( 2 === $step ) { 1033 1112 $this->step_2(); 1034 } elseif ( 3 == $step ) {1113 } elseif ( 3 === $step ) { 1035 1114 $this->step_3(); 1036 1115 } else { … … 1078 1157 if ( is_array( $choice ) || is_object( $choice ) ) { 1079 1158 $choice = (array) $choice; 1159 1080 1160 if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) { 1081 1161 return; … … 1093 1173 1094 1174 update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() ); 1175 1095 1176 set_theme_mod( 'header_image', $choice['url'] ); 1096 1177 set_theme_mod( 'header_image_data', $header_image_data ); 1178 1097 1179 return; 1098 1180 } … … 1101 1183 set_theme_mod( 'header_image', $choice ); 1102 1184 remove_theme_mod( 'header_image_data' ); 1185 1103 1186 return; 1104 1187 } 1105 1188 1106 1189 $uploaded = get_uploaded_header_images(); 1190 1107 1191 if ( $uploaded && isset( $uploaded[ $choice ] ) ) { 1108 1192 $header_image_data = $uploaded[ $choice ]; 1109 1110 1193 } else { 1111 1194 $this->process_default_headers(); … … 1145 1228 return; 1146 1229 } 1230 1147 1231 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); 1148 1232 1149 1233 $default_data = array(); 1150 1234 foreach ( $this->default_headers as $header => $details ) { 1151 if ( $details['url'] == $default ) {1235 if ( $details['url'] === $default ) { 1152 1236 $default_data = $details; 1153 1237 break; … … 1408 1492 1409 1493 $header_image_data_setting = $wp_customize->get_setting( 'header_image_data' ); 1494 1410 1495 if ( ! $header_image_data_setting ) { 1411 1496 return; 1412 1497 } 1498 1413 1499 $data = $header_image_data_setting->post_value(); 1414 1500 … … 1439 1525 } 1440 1526 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 1442 1529 $already_has_default = false; 1443 1530
Note: See TracChangeset
for help on using the changeset viewer.