wp/wp-admin/includes/class-custom-image-header.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    69 	 *
    69 	 *
    70 	 * @since 2.1.0
    70 	 * @since 2.1.0
    71 	 */
    71 	 */
    72 	public function init() {
    72 	public function init() {
    73 		$page = add_theme_page( __( 'Header' ), __( 'Header' ), 'edit_theme_options', 'custom-header', array( $this, 'admin_page' ) );
    73 		$page = add_theme_page( __( 'Header' ), __( 'Header' ), 'edit_theme_options', 'custom-header', array( $this, 'admin_page' ) );
       
    74 
    74 		if ( ! $page ) {
    75 		if ( ! $page ) {
    75 			return;
    76 			return;
    76 		}
    77 		}
    77 
    78 
    78 		add_action( "admin_print_scripts-{$page}", array( $this, 'js_includes' ) );
    79 		add_action( "admin_print_scripts-{$page}", array( $this, 'js_includes' ) );
    79 		add_action( "admin_print_styles-{$page}", array( $this, 'css_includes' ) );
    80 		add_action( "admin_print_styles-{$page}", array( $this, 'css_includes' ) );
    80 		add_action( "admin_head-{$page}", array( $this, 'help' ) );
    81 		add_action( "admin_head-{$page}", array( $this, 'help' ) );
    81 		add_action( "admin_head-{$page}", array( $this, 'take_action' ), 50 );
    82 		add_action( "admin_head-{$page}", array( $this, 'take_action' ), 50 );
    82 		add_action( "admin_head-{$page}", array( $this, 'js' ), 50 );
    83 		add_action( "admin_head-{$page}", array( $this, 'js' ), 50 );
       
    84 
    83 		if ( $this->admin_header_callback ) {
    85 		if ( $this->admin_header_callback ) {
    84 			add_action( "admin_head-{$page}", $this->admin_header_callback, 51 );
    86 			add_action( "admin_head-{$page}", $this->admin_header_callback, 51 );
    85 		}
    87 		}
    86 	}
    88 	}
    87 
    89 
   139 	/**
   141 	/**
   140 	 * Get the current step.
   142 	 * Get the current step.
   141 	 *
   143 	 *
   142 	 * @since 2.6.0
   144 	 * @since 2.6.0
   143 	 *
   145 	 *
   144 	 * @return int Current step
   146 	 * @return int Current step.
   145 	 */
   147 	 */
   146 	public function step() {
   148 	public function step() {
   147 		if ( ! isset( $_GET['step'] ) ) {
   149 		if ( ! isset( $_GET['step'] ) ) {
   148 			return 1;
   150 			return 1;
   149 		}
   151 		}
   150 
   152 
   151 		$step = (int) $_GET['step'];
   153 		$step = (int) $_GET['step'];
   152 		if ( $step < 1 || 3 < $step ||
   154 		if ( $step < 1 || 3 < $step ||
   153 			( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
   155 			( 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' ) )
   156 			( 3 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
   155 		) {
   157 		) {
   156 			return 1;
   158 			return 1;
   157 		}
   159 		}
   158 
   160 
   159 		return $step;
   161 		return $step;
   165 	 * @since 2.1.0
   167 	 * @since 2.1.0
   166 	 */
   168 	 */
   167 	public function js_includes() {
   169 	public function js_includes() {
   168 		$step = $this->step();
   170 		$step = $this->step();
   169 
   171 
   170 		if ( ( 1 == $step || 3 == $step ) ) {
   172 		if ( ( 1 === $step || 3 === $step ) ) {
   171 			wp_enqueue_media();
   173 			wp_enqueue_media();
   172 			wp_enqueue_script( 'custom-header' );
   174 			wp_enqueue_script( 'custom-header' );
   173 			if ( current_theme_supports( 'custom-header', 'header-text' ) ) {
   175 			if ( current_theme_supports( 'custom-header', 'header-text' ) ) {
   174 				wp_enqueue_script( 'wp-color-picker' );
   176 				wp_enqueue_script( 'wp-color-picker' );
   175 			}
   177 			}
   176 		} elseif ( 2 == $step ) {
   178 		} elseif ( 2 === $step ) {
   177 			wp_enqueue_script( 'imgareaselect' );
   179 			wp_enqueue_script( 'imgareaselect' );
   178 		}
   180 		}
   179 	}
   181 	}
   180 
   182 
   181 	/**
   183 	/**
   184 	 * @since 2.7.0
   186 	 * @since 2.7.0
   185 	 */
   187 	 */
   186 	public function css_includes() {
   188 	public function css_includes() {
   187 		$step = $this->step();
   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 			wp_enqueue_style( 'wp-color-picker' );
   192 			wp_enqueue_style( 'wp-color-picker' );
   191 		} elseif ( 2 == $step ) {
   193 		} elseif ( 2 === $step ) {
   192 			wp_enqueue_style( 'imgareaselect' );
   194 			wp_enqueue_style( 'imgareaselect' );
   193 		}
   195 		}
   194 	}
   196 	}
   195 
   197 
   196 	/**
   198 	/**
   209 
   211 
   210 		$this->updated = true;
   212 		$this->updated = true;
   211 
   213 
   212 		if ( isset( $_POST['resetheader'] ) ) {
   214 		if ( isset( $_POST['resetheader'] ) ) {
   213 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   215 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
       
   216 
   214 			$this->reset_header_image();
   217 			$this->reset_header_image();
       
   218 
   215 			return;
   219 			return;
   216 		}
   220 		}
   217 
   221 
   218 		if ( isset( $_POST['removeheader'] ) ) {
   222 		if ( isset( $_POST['removeheader'] ) ) {
   219 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   223 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
       
   224 
   220 			$this->remove_header_image();
   225 			$this->remove_header_image();
       
   226 
   221 			return;
   227 			return;
   222 		}
   228 		}
   223 
   229 
   224 		if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) {
   230 		if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) {
   225 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   231 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
       
   232 
   226 			set_theme_mod( 'header_textcolor', 'blank' );
   233 			set_theme_mod( 'header_textcolor', 'blank' );
   227 		} elseif ( isset( $_POST['text-color'] ) ) {
   234 		} elseif ( isset( $_POST['text-color'] ) ) {
   228 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   235 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
       
   236 
   229 			$_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
   237 			$_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
   230 			$color               = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
   238 
   231 			if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) {
   239 			$color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
       
   240 
       
   241 			if ( strlen( $color ) === 6 || strlen( $color ) === 3 ) {
   232 				set_theme_mod( 'header_textcolor', $color );
   242 				set_theme_mod( 'header_textcolor', $color );
   233 			} elseif ( ! $color ) {
   243 			} elseif ( ! $color ) {
   234 				set_theme_mod( 'header_textcolor', 'blank' );
   244 				set_theme_mod( 'header_textcolor', 'blank' );
   235 			}
   245 			}
   236 		}
   246 		}
   237 
   247 
   238 		if ( isset( $_POST['default-header'] ) ) {
   248 		if ( isset( $_POST['default-header'] ) ) {
   239 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   249 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
       
   250 
   240 			$this->set_header_image( $_POST['default-header'] );
   251 			$this->set_header_image( $_POST['default-header'] );
       
   252 
   241 			return;
   253 			return;
   242 		}
   254 		}
   243 	}
   255 	}
   244 
   256 
   245 	/**
   257 	/**
   261 		}
   273 		}
   262 
   274 
   263 		$this->default_headers    = $_wp_default_headers;
   275 		$this->default_headers    = $_wp_default_headers;
   264 		$template_directory_uri   = get_template_directory_uri();
   276 		$template_directory_uri   = get_template_directory_uri();
   265 		$stylesheet_directory_uri = get_stylesheet_directory_uri();
   277 		$stylesheet_directory_uri = get_stylesheet_directory_uri();
       
   278 
   266 		foreach ( array_keys( $this->default_headers ) as $header ) {
   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 );
   280 			$this->default_headers[ $header ]['url'] = sprintf(
   268 			$this->default_headers[ $header ]['thumbnail_url'] = sprintf( $this->default_headers[ $header ]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
   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 	}
   271 
   293 
   272 	/**
   294 	/**
   273 	 * Display UI for selecting one of several default headers.
   295 	 * Display UI for selecting one of several default headers.
   295 			echo '</label>';
   317 			echo '</label>';
   296 			echo '</div>';
   318 			echo '</div>';
   297 		}
   319 		}
   298 
   320 
   299 		echo '<div class="available-headers">';
   321 		echo '<div class="available-headers">';
       
   322 
   300 		foreach ( $headers as $header_key => $header ) {
   323 		foreach ( $headers as $header_key => $header ) {
   301 			$header_thumbnail = $header['thumbnail_url'];
   324 			$header_thumbnail = $header['thumbnail_url'];
   302 			$header_url       = $header['url'];
   325 			$header_url       = $header['url'];
   303 			$header_alt_text  = empty( $header['alt_text'] ) ? '' : $header['alt_text'];
   326 			$header_alt_text  = empty( $header['alt_text'] ) ? '' : $header['alt_text'];
       
   327 
   304 			echo '<div class="default-header">';
   328 			echo '<div class="default-header">';
   305 			echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
   329 			echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
   306 			$width = '';
   330 			$width = '';
   307 			if ( ! empty( $header['attachment_id'] ) ) {
   331 			if ( ! empty( $header['attachment_id'] ) ) {
   308 				$width = ' width="230"';
   332 				$width = ' width="230"';
   309 			}
   333 			}
   310 			echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) . '"' . $width . ' /></label>';
   334 			echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) . '"' . $width . ' /></label>';
   311 			echo '</div>';
   335 			echo '</div>';
   312 		}
   336 		}
       
   337 
   313 		echo '<div class="clear"></div></div>';
   338 		echo '<div class="clear"></div></div>';
   314 	}
   339 	}
   315 
   340 
   316 	/**
   341 	/**
   317 	 * Execute JavaScript depending on step.
   342 	 * Execute JavaScript depending on step.
   318 	 *
   343 	 *
   319 	 * @since 2.1.0
   344 	 * @since 2.1.0
   320 	 */
   345 	 */
   321 	public function js() {
   346 	public function js() {
   322 		$step = $this->step();
   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 			$this->js_1();
   350 			$this->js_1();
   325 		} elseif ( 2 == $step ) {
   351 		} elseif ( 2 === $step ) {
   326 			$this->js_2();
   352 			$this->js_2();
   327 		}
   353 		}
   328 	}
   354 	}
   329 
   355 
   330 	/**
   356 	/**
   341 			}
   367 			}
   342 		}
   368 		}
   343 		?>
   369 		?>
   344 <script type="text/javascript">
   370 <script type="text/javascript">
   345 (function($){
   371 (function($){
   346 	var default_color = '<?php echo $default_color; ?>',
   372 	var default_color = '<?php echo esc_js( $default_color ); ?>',
   347 		header_text_fields;
   373 		header_text_fields;
   348 
   374 
   349 	function pickColor(color) {
   375 	function pickColor(color) {
   350 		$('#name').css('color', color);
   376 		$('#name').css('color', color);
   351 		$('#desc').css('color', color);
   377 		$('#desc').css('color', color);
   428 			x1: 0,
   454 			x1: 0,
   429 			y1: 0,
   455 			y1: 0,
   430 			x2: xinit,
   456 			x2: xinit,
   431 			y2: yinit,
   457 			y2: yinit,
   432 			<?php
   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 			aspectRatio: xinit + ':' + yinit,
   463 			aspectRatio: xinit + ':' + yinit,
   436 				<?php
   464 				<?php
   437 			}
   465 			}
   438 			if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
   466 			if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
   548 <tr>
   576 <tr>
   549 <th scope="row"><?php _e( 'Select Image' ); ?></th>
   577 <th scope="row"><?php _e( 'Select Image' ); ?></th>
   550 <td>
   578 <td>
   551 	<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 />
   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 			<?php
   580 			<?php
   553 			if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   581 			if ( ! current_theme_supports( 'custom-header', 'flex-height' )
   554 				/* translators: 1: Image width in pixels, 2: Image height in pixels. */
   582 				&& ! current_theme_supports( 'custom-header', 'flex-width' )
   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' ) );
   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 				);
   556 			} elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
   590 			} elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
   557 				if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   591 				if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   558 					printf(
   592 					printf(
   559 						/* translators: %s: Size in pixels. */
   593 						/* translators: %s: Size in pixels. */
   560 						__( 'Images should be at least %s wide.' ) . ' ',
   594 						__( 'Images should be at least %s wide.' ) . ' ',
   576 							get_theme_support( 'custom-header', 'height' )
   610 							get_theme_support( 'custom-header', 'height' )
   577 						)
   611 						)
   578 					);
   612 					);
   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 				if ( current_theme_supports( 'custom-header', 'width' ) ) {
   619 				if ( current_theme_supports( 'custom-header', 'width' ) ) {
   583 					printf(
   620 					printf(
   584 						/* translators: %s: Size in pixels. */
   621 						/* translators: %s: Size in pixels. */
   585 						__( 'Suggested width is %s.' ) . ' ',
   622 						__( 'Suggested width is %s.' ) . ' ',
   586 						sprintf(
   623 						sprintf(
   588 							'<strong>' . __( '%d pixels' ) . '</strong>',
   625 							'<strong>' . __( '%d pixels' ) . '</strong>',
   589 							get_theme_support( 'custom-header', 'width' )
   626 							get_theme_support( 'custom-header', 'width' )
   590 						)
   627 						)
   591 					);
   628 					);
   592 				}
   629 				}
       
   630 
   593 				if ( current_theme_supports( 'custom-header', 'height' ) ) {
   631 				if ( current_theme_supports( 'custom-header', 'height' ) ) {
   594 					printf(
   632 					printf(
   595 						/* translators: %s: Size in pixels. */
   633 						/* translators: %s: Size in pixels. */
   596 						__( 'Suggested height is %s.' ) . ' ',
   634 						__( 'Suggested height is %s.' ) . ' ',
   597 						sprintf(
   635 						sprintf(
   681 </td>
   719 </td>
   682 </tr>
   720 </tr>
   683 			<?php
   721 			<?php
   684 	endif;
   722 	endif;
   685 
   723 
   686 		$default_image = sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() );
   724 		$default_image = sprintf(
   687 		if ( $default_image && get_header_image() != $default_image ) :
   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 <tr>
   732 <tr>
   690 <th scope="row"><?php _e( 'Reset Image' ); ?></th>
   733 <th scope="row"><?php _e( 'Reset Image' ); ?></th>
   691 <td>
   734 <td>
   692 	<p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ); ?></p>
   735 	<p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ); ?></p>
   768 	 *
   811 	 *
   769 	 * @since 2.1.0
   812 	 * @since 2.1.0
   770 	 */
   813 	 */
   771 	public function step_2() {
   814 	public function step_2() {
   772 		check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' );
   815 		check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' );
       
   816 
   773 		if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
   817 		if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
   774 			wp_die(
   818 			wp_die(
   775 				'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
   819 				'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
   776 				'<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>',
   820 				'<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>',
   777 				403
   821 				403
   789 			$file          = $data['file'];
   833 			$file          = $data['file'];
   790 			$url           = $data['url'];
   834 			$url           = $data['url'];
   791 		}
   835 		}
   792 
   836 
   793 		if ( file_exists( $file ) ) {
   837 		if ( file_exists( $file ) ) {
   794 			list( $width, $height, $type, $attr ) = @getimagesize( $file );
   838 			list( $width, $height, $type, $attr ) = wp_getimagesize( $file );
   795 		} else {
   839 		} else {
   796 			$data   = wp_get_attachment_metadata( $attachment_id );
   840 			$data   = wp_get_attachment_metadata( $attachment_id );
   797 			$height = isset( $data['height'] ) ? $data['height'] : 0;
   841 			$height = isset( $data['height'] ) ? (int) $data['height'] : 0;
   798 			$width  = isset( $data['width'] ) ? $data['width'] : 0;
   842 			$width  = isset( $data['width'] ) ? (int) $data['width'] : 0;
   799 			unset( $data );
   843 			unset( $data );
   800 		}
   844 		}
   801 
   845 
   802 		$max_width = 0;
   846 		$max_width = 0;
       
   847 
   803 		// For flex, limit size of image displayed to 1500px unless theme says otherwise.
   848 		// For flex, limit size of image displayed to 1500px unless theme says otherwise.
   804 		if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
   849 		if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
   805 			$max_width = 1500;
   850 			$max_width = 1500;
   806 		}
   851 		}
   807 
   852 
   808 		if ( current_theme_supports( 'custom-header', 'max-width' ) ) {
   853 		if ( current_theme_supports( 'custom-header', 'max-width' ) ) {
   809 			$max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
   854 			$max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
   810 		}
   855 		}
       
   856 
   811 		$max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
   857 		$max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
   812 
   858 
   813 		// If flexible height isn't supported and the image is the exact right size.
   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' )
   860 		if ( ! current_theme_supports( 'custom-header', 'flex-height' )
   815 			&& get_theme_support( 'custom-header', 'width' ) == $width && get_theme_support( 'custom-header', 'height' ) == $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 			// Add the metadata.
   865 			// Add the metadata.
   817 			if ( file_exists( $file ) ) {
   866 			if ( file_exists( $file ) ) {
   818 				wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
   867 				wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
   819 			}
   868 			}
   820 
   869 
   831 			do_action( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication.
   880 			do_action( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication.
   832 
   881 
   833 			return $this->finished();
   882 			return $this->finished();
   834 		} elseif ( $width > $max_width ) {
   883 		} elseif ( $width > $max_width ) {
   835 			$oitar = $width / $max_width;
   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 			if ( ! $image || is_wp_error( $image ) ) {
   898 			if ( ! $image || is_wp_error( $image ) ) {
   838 				wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
   899 				wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
   839 			}
   900 			}
   840 
   901 
   841 			/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
   902 			/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
   858 
   919 
   859 	<div id="crop_image" style="position: relative">
   920 	<div id="crop_image" style="position: relative">
   860 		<img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="" />
   921 		<img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="" />
   861 	</div>
   922 	</div>
   862 
   923 
   863 	<input type="hidden" name="x1" id="x1" value="0"/>
   924 	<input type="hidden" name="x1" id="x1" value="0" />
   864 	<input type="hidden" name="y1" id="y1" value="0"/>
   925 	<input type="hidden" name="y1" id="y1" value="0" />
   865 	<input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
   926 	<input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>" />
   866 	<input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
   927 	<input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>" />
   867 	<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
   928 	<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
   868 	<input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
   929 	<input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
   869 		<?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
   930 		<?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
   870 	<input type="hidden" name="create-new-attachment" value="true" />
   931 	<input type="hidden" name="create-new-attachment" value="true" />
   871 	<?php } ?>
   932 	<?php } ?>
   872 		<?php wp_nonce_field( 'custom-header-crop-image' ); ?>
   933 		<?php wp_nonce_field( 'custom-header-crop-image' ); ?>
   873 
   934 
   874 	<p class="submit">
   935 	<p class="submit">
   875 		<?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
   936 		<?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
   876 		<?php
   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 			submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false );
   942 			submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false );
   879 		}
   943 		}
   880 		?>
   944 		?>
   881 	</p>
   945 	</p>
   882 </form>
   946 </form>
   893 	public function step_2_manage_upload() {
   957 	public function step_2_manage_upload() {
   894 		$overrides = array( 'test_form' => false );
   958 		$overrides = array( 'test_form' => false );
   895 
   959 
   896 		$uploaded_file = $_FILES['import'];
   960 		$uploaded_file = $_FILES['import'];
   897 		$wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
   961 		$wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
       
   962 
   898 		if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
   963 		if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
   899 			wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
   964 			wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
   900 		}
   965 		}
   901 
   966 
   902 		$file = wp_handle_upload( $uploaded_file, $overrides );
   967 		$file = wp_handle_upload( $uploaded_file, $overrides );
   919 			'context'        => 'custom-header',
   984 			'context'        => 'custom-header',
   920 		);
   985 		);
   921 
   986 
   922 		// Save the data.
   987 		// Save the data.
   923 		$attachment_id = wp_insert_attachment( $object, $file );
   988 		$attachment_id = wp_insert_attachment( $object, $file );
       
   989 
   924 		return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
   990 		return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
   925 	}
   991 	}
   926 
   992 
   927 	/**
   993 	/**
   928 	 * Display third step of custom header image page.
   994 	 * Display third step of custom header image page.
   940 				'<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>',
  1006 				'<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>',
   941 				403
  1007 				403
   942 			);
  1008 			);
   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 			wp_die(
  1015 			wp_die(
   947 				'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
  1016 				'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
   948 				'<p>' . __( 'The current theme does not support a flexible sized header image.' ) . '</p>',
  1017 				'<p>' . __( 'The current theme does not support a flexible sized header image.' ) . '</p>',
   949 				403
  1018 				403
   950 			);
  1019 			);
   968 		);
  1037 		);
   969 		$height     = $dimensions['dst_height'];
  1038 		$height     = $dimensions['dst_height'];
   970 		$width      = $dimensions['dst_width'];
  1039 		$width      = $dimensions['dst_width'];
   971 
  1040 
   972 		if ( empty( $_POST['skip-cropping'] ) ) {
  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 		} elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
  1051 		} elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
   975 			$cropped = _copy_image_file( $attachment_id );
  1052 			$cropped = _copy_image_file( $attachment_id );
   976 		} else {
  1053 		} else {
   977 			$cropped = get_attached_file( $attachment_id );
  1054 			$cropped = get_attached_file( $attachment_id );
   978 		}
  1055 		}
  1026 	 */
  1103 	 */
  1027 	public function admin_page() {
  1104 	public function admin_page() {
  1028 		if ( ! current_user_can( 'edit_theme_options' ) ) {
  1105 		if ( ! current_user_can( 'edit_theme_options' ) ) {
  1029 			wp_die( __( 'Sorry, you are not allowed to customize headers.' ) );
  1106 			wp_die( __( 'Sorry, you are not allowed to customize headers.' ) );
  1030 		}
  1107 		}
       
  1108 
  1031 		$step = $this->step();
  1109 		$step = $this->step();
  1032 		if ( 2 == $step ) {
  1110 
       
  1111 		if ( 2 === $step ) {
  1033 			$this->step_2();
  1112 			$this->step_2();
  1034 		} elseif ( 3 == $step ) {
  1113 		} elseif ( 3 === $step ) {
  1035 			$this->step_3();
  1114 			$this->step_3();
  1036 		} else {
  1115 		} else {
  1037 			$this->step_1();
  1116 			$this->step_1();
  1038 		}
  1117 		}
  1039 	}
  1118 	}
  1075 	 *  Or an array of arguments: attachment_id, url, width, height. All are required.
  1154 	 *  Or an array of arguments: attachment_id, url, width, height. All are required.
  1076 	 */
  1155 	 */
  1077 	final public function set_header_image( $choice ) {
  1156 	final public function set_header_image( $choice ) {
  1078 		if ( is_array( $choice ) || is_object( $choice ) ) {
  1157 		if ( is_array( $choice ) || is_object( $choice ) ) {
  1079 			$choice = (array) $choice;
  1158 			$choice = (array) $choice;
       
  1159 
  1080 			if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) {
  1160 			if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) {
  1081 				return;
  1161 				return;
  1082 			}
  1162 			}
  1083 
  1163 
  1084 			$choice['url'] = esc_url_raw( $choice['url'] );
  1164 			$choice['url'] = esc_url_raw( $choice['url'] );
  1090 				'height'        => $choice['height'],
  1170 				'height'        => $choice['height'],
  1091 				'width'         => $choice['width'],
  1171 				'width'         => $choice['width'],
  1092 			);
  1172 			);
  1093 
  1173 
  1094 			update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
  1174 			update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
       
  1175 
  1095 			set_theme_mod( 'header_image', $choice['url'] );
  1176 			set_theme_mod( 'header_image', $choice['url'] );
  1096 			set_theme_mod( 'header_image_data', $header_image_data );
  1177 			set_theme_mod( 'header_image_data', $header_image_data );
       
  1178 
  1097 			return;
  1179 			return;
  1098 		}
  1180 		}
  1099 
  1181 
  1100 		if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ), true ) ) {
  1182 		if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ), true ) ) {
  1101 			set_theme_mod( 'header_image', $choice );
  1183 			set_theme_mod( 'header_image', $choice );
  1102 			remove_theme_mod( 'header_image_data' );
  1184 			remove_theme_mod( 'header_image_data' );
       
  1185 
  1103 			return;
  1186 			return;
  1104 		}
  1187 		}
  1105 
  1188 
  1106 		$uploaded = get_uploaded_header_images();
  1189 		$uploaded = get_uploaded_header_images();
       
  1190 
  1107 		if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
  1191 		if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
  1108 			$header_image_data = $uploaded[ $choice ];
  1192 			$header_image_data = $uploaded[ $choice ];
  1109 
       
  1110 		} else {
  1193 		} else {
  1111 			$this->process_default_headers();
  1194 			$this->process_default_headers();
  1112 			if ( isset( $this->default_headers[ $choice ] ) ) {
  1195 			if ( isset( $this->default_headers[ $choice ] ) ) {
  1113 				$header_image_data = $this->default_headers[ $choice ];
  1196 				$header_image_data = $this->default_headers[ $choice ];
  1114 			} else {
  1197 			} else {
  1142 
  1225 
  1143 		if ( ! $default ) {
  1226 		if ( ! $default ) {
  1144 			$this->remove_header_image();
  1227 			$this->remove_header_image();
  1145 			return;
  1228 			return;
  1146 		}
  1229 		}
       
  1230 
  1147 		$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  1231 		$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  1148 
  1232 
  1149 		$default_data = array();
  1233 		$default_data = array();
  1150 		foreach ( $this->default_headers as $header => $details ) {
  1234 		foreach ( $this->default_headers as $header => $details ) {
  1151 			if ( $details['url'] == $default ) {
  1235 			if ( $details['url'] === $default ) {
  1152 				$default_data = $details;
  1236 				$default_data = $details;
  1153 				break;
  1237 				break;
  1154 			}
  1238 			}
  1155 		}
  1239 		}
  1156 
  1240 
  1221 	final public function create_attachment_object( $cropped, $parent_attachment_id ) {
  1305 	final public function create_attachment_object( $cropped, $parent_attachment_id ) {
  1222 		$parent     = get_post( $parent_attachment_id );
  1306 		$parent     = get_post( $parent_attachment_id );
  1223 		$parent_url = wp_get_attachment_url( $parent->ID );
  1307 		$parent_url = wp_get_attachment_url( $parent->ID );
  1224 		$url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
  1308 		$url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
  1225 
  1309 
  1226 		$size       = @getimagesize( $cropped );
  1310 		$size       = wp_getimagesize( $cropped );
  1227 		$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  1311 		$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  1228 
  1312 
  1229 		$object = array(
  1313 		$object = array(
  1230 			'ID'             => $parent_attachment_id,
  1314 			'ID'             => $parent_attachment_id,
  1231 			'post_title'     => wp_basename( $cropped ),
  1315 			'post_title'     => wp_basename( $cropped ),
  1405 	 * @param WP_Customize_Manager $wp_customize Customize manager.
  1489 	 * @param WP_Customize_Manager $wp_customize Customize manager.
  1406 	 */
  1490 	 */
  1407 	public function customize_set_last_used( $wp_customize ) {
  1491 	public function customize_set_last_used( $wp_customize ) {
  1408 
  1492 
  1409 		$header_image_data_setting = $wp_customize->get_setting( 'header_image_data' );
  1493 		$header_image_data_setting = $wp_customize->get_setting( 'header_image_data' );
       
  1494 
  1410 		if ( ! $header_image_data_setting ) {
  1495 		if ( ! $header_image_data_setting ) {
  1411 			return;
  1496 			return;
  1412 		}
  1497 		}
       
  1498 
  1413 		$data = $header_image_data_setting->post_value();
  1499 		$data = $header_image_data_setting->post_value();
  1414 
  1500 
  1415 		if ( ! isset( $data['attachment_id'] ) ) {
  1501 		if ( ! isset( $data['attachment_id'] ) ) {
  1416 			return;
  1502 			return;
  1417 		}
  1503 		}
  1436 
  1522 
  1437 		if ( ! $default ) { // If not, easy peasy.
  1523 		if ( ! $default ) { // If not, easy peasy.
  1438 			return $this->default_headers;
  1524 			return $this->default_headers;
  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 		$already_has_default = false;
  1529 		$already_has_default = false;
  1443 
  1530 
  1444 		foreach ( $this->default_headers as $k => $h ) {
  1531 		foreach ( $this->default_headers as $k => $h ) {
  1445 			if ( $h['url'] === $default ) {
  1532 			if ( $h['url'] === $default ) {
  1446 				$already_has_default = true;
  1533 				$already_has_default = true;