wp/wp-admin/custom-header.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    50 	 *
    50 	 *
    51 	 * @since 2.1.0
    51 	 * @since 2.1.0
    52 	 * @param callable $admin_header_callback
    52 	 * @param callable $admin_header_callback
    53 	 * @param callable $admin_image_div_callback Optional custom image div output callback.
    53 	 * @param callable $admin_image_div_callback Optional custom image div output callback.
    54 	 */
    54 	 */
    55 	public function __construct($admin_header_callback, $admin_image_div_callback = '') {
    55 	public function __construct( $admin_header_callback, $admin_image_div_callback = '' ) {
    56 		$this->admin_header_callback = $admin_header_callback;
    56 		$this->admin_header_callback    = $admin_header_callback;
    57 		$this->admin_image_div_callback = $admin_image_div_callback;
    57 		$this->admin_image_div_callback = $admin_image_div_callback;
    58 
    58 
    59 		add_action( 'admin_menu', array( $this, 'init' ) );
    59 		add_action( 'admin_menu', array( $this, 'init' ) );
    60 
    60 
    61 		add_action( 'customize_save_after',         array( $this, 'customize_set_last_used' ) );
    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'        ) );
    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'         ) );
    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'      ) );
    64 		add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove' ) );
    65 	}
    65 	}
    66 
    66 
    67 	/**
    67 	/**
    68 	 * Set up the hooks for the Custom Header admin page.
    68 	 * Set up the hooks for the Custom Header admin page.
    69 	 *
    69 	 *
    89 	 * Adds contextual help.
    89 	 * Adds contextual help.
    90 	 *
    90 	 *
    91 	 * @since 3.0.0
    91 	 * @since 3.0.0
    92 	 */
    92 	 */
    93 	public function help() {
    93 	public function help() {
    94 		get_current_screen()->add_help_tab( array(
    94 		get_current_screen()->add_help_tab(
    95 			'id'      => 'overview',
    95 			array(
    96 			'title'   => __('Overview'),
    96 				'id'      => 'overview',
    97 			'content' =>
    97 				'title'   => __( 'Overview' ),
    98 				'<p>' . __( 'This screen is used to customize the header section of your theme.') . '</p>' .
    98 				'content' =>
    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>'
    99 					'<p>' . __( 'This screen is used to customize the header section of your theme.' ) . '</p>' .
   100 		) );
   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 
   101 			)
   102 		get_current_screen()->add_help_tab( array(
   102 		);
   103 			'id'      => 'set-header-image',
   103 
   104 			'title'   => __('Header Image'),
   104 		get_current_screen()->add_help_tab(
   105 			'content' =>
   105 			array(
   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>' .
   106 				'id'      => 'set-header-image',
   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>' .
   107 				'title'   => __( 'Header Image' ),
   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>' .
   108 				'content' =>
   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>'
   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 		) );
   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 
   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 		get_current_screen()->add_help_tab( array(
   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 			'id'      => 'set-header-text',
   113 			)
   114 			'title'   => __('Header Text'),
   114 		);
   115 			'content' =>
   115 
   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>' .
   116 		get_current_screen()->add_help_tab(
   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>' .
   117 			array(
   118 				'<p>' . __( 'Don&#8217;t forget to click &#8220;Save Changes&#8221; when you&#8217;re done!') . '</p>'
   118 				'id'      => 'set-header-text',
   119 		) );
   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 		);
   120 
   126 
   121 		get_current_screen()->set_help_sidebar(
   127 		get_current_screen()->set_help_sidebar(
   122 			'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
   128 			'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
   123 			'<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Header_Screen">Documentation on Custom Header</a>' ) . '</p>' .
   129 			'<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Header_Screen">Documentation on Custom Header</a>' ) . '</p>' .
   124 			'<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
   130 			'<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
   125 		);
   131 		);
   126 	}
   132 	}
   127 
   133 
   128 	/**
   134 	/**
   129 	 * Get the current step.
   135 	 * Get the current step.
   131 	 * @since 2.6.0
   137 	 * @since 2.6.0
   132 	 *
   138 	 *
   133 	 * @return int Current step
   139 	 * @return int Current step
   134 	 */
   140 	 */
   135 	public function step() {
   141 	public function step() {
   136 		if ( ! isset( $_GET['step'] ) )
   142 		if ( ! isset( $_GET['step'] ) ) {
   137 			return 1;
   143 			return 1;
       
   144 		}
   138 
   145 
   139 		$step = (int) $_GET['step'];
   146 		$step = (int) $_GET['step'];
   140 		if ( $step < 1 || 3 < $step ||
   147 		if ( $step < 1 || 3 < $step ||
   141 			( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
   148 			( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
   142 			( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
   149 			( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
   143 		)
   150 		) {
   144 			return 1;
   151 			return 1;
       
   152 		}
   145 
   153 
   146 		return $step;
   154 		return $step;
   147 	}
   155 	}
   148 
   156 
   149 	/**
   157 	/**
   155 		$step = $this->step();
   163 		$step = $this->step();
   156 
   164 
   157 		if ( ( 1 == $step || 3 == $step ) ) {
   165 		if ( ( 1 == $step || 3 == $step ) ) {
   158 			wp_enqueue_media();
   166 			wp_enqueue_media();
   159 			wp_enqueue_script( 'custom-header' );
   167 			wp_enqueue_script( 'custom-header' );
   160 			if ( current_theme_supports( 'custom-header', 'header-text' ) )
   168 			if ( current_theme_supports( 'custom-header', 'header-text' ) ) {
   161 				wp_enqueue_script( 'wp-color-picker' );
   169 				wp_enqueue_script( 'wp-color-picker' );
       
   170 			}
   162 		} elseif ( 2 == $step ) {
   171 		} elseif ( 2 == $step ) {
   163 			wp_enqueue_script('imgareaselect');
   172 			wp_enqueue_script( 'imgareaselect' );
   164 		}
   173 		}
   165 	}
   174 	}
   166 
   175 
   167 	/**
   176 	/**
   168 	 * Set up the enqueue for the CSS files
   177 	 * Set up the enqueue for the CSS files
   170 	 * @since 2.7.0
   179 	 * @since 2.7.0
   171 	 */
   180 	 */
   172 	public function css_includes() {
   181 	public function css_includes() {
   173 		$step = $this->step();
   182 		$step = $this->step();
   174 
   183 
   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' ) ) {
   176 			wp_enqueue_style( 'wp-color-picker' );
   185 			wp_enqueue_style( 'wp-color-picker' );
   177 		elseif ( 2 == $step )
   186 		} elseif ( 2 == $step ) {
   178 			wp_enqueue_style('imgareaselect');
   187 			wp_enqueue_style( 'imgareaselect' );
       
   188 		}
   179 	}
   189 	}
   180 
   190 
   181 	/**
   191 	/**
   182 	 * Execute custom header modification.
   192 	 * Execute custom header modification.
   183 	 *
   193 	 *
   184 	 * @since 2.6.0
   194 	 * @since 2.6.0
   185 	 */
   195 	 */
   186 	public function take_action() {
   196 	public function take_action() {
   187 		if ( ! current_user_can('edit_theme_options') )
   197 		if ( ! current_user_can( 'edit_theme_options' ) ) {
   188 			return;
   198 			return;
   189 
   199 		}
   190 		if ( empty( $_POST ) )
   200 
       
   201 		if ( empty( $_POST ) ) {
   191 			return;
   202 			return;
       
   203 		}
   192 
   204 
   193 		$this->updated = true;
   205 		$this->updated = true;
   194 
   206 
   195 		if ( isset( $_POST['resetheader'] ) ) {
   207 		if ( isset( $_POST['resetheader'] ) ) {
   196 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   208 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   208 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   220 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   209 			set_theme_mod( 'header_textcolor', 'blank' );
   221 			set_theme_mod( 'header_textcolor', 'blank' );
   210 		} elseif ( isset( $_POST['text-color'] ) ) {
   222 		} elseif ( isset( $_POST['text-color'] ) ) {
   211 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   223 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   212 			$_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
   224 			$_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
   213 			$color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);
   225 			$color               = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
   214 			if ( strlen($color) == 6 || strlen($color) == 3 )
   226 			if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) {
   215 				set_theme_mod('header_textcolor', $color);
   227 				set_theme_mod( 'header_textcolor', $color );
   216 			elseif ( ! $color )
   228 			} elseif ( ! $color ) {
   217 				set_theme_mod( 'header_textcolor', 'blank' );
   229 				set_theme_mod( 'header_textcolor', 'blank' );
       
   230 			}
   218 		}
   231 		}
   219 
   232 
   220 		if ( isset( $_POST['default-header'] ) ) {
   233 		if ( isset( $_POST['default-header'] ) ) {
   221 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   234 			check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
   222 			$this->set_header_image( $_POST['default-header'] );
   235 			$this->set_header_image( $_POST['default-header'] );
   232 	 * @global array $_wp_default_headers
   245 	 * @global array $_wp_default_headers
   233 	 */
   246 	 */
   234 	public function process_default_headers() {
   247 	public function process_default_headers() {
   235 		global $_wp_default_headers;
   248 		global $_wp_default_headers;
   236 
   249 
   237 		if ( !isset($_wp_default_headers) )
   250 		if ( ! isset( $_wp_default_headers ) ) {
   238 			return;
   251 			return;
       
   252 		}
   239 
   253 
   240 		if ( ! empty( $this->default_headers ) ) {
   254 		if ( ! empty( $this->default_headers ) ) {
   241 			return;
   255 			return;
   242 		}
   256 		}
   243 
   257 
   244 		$this->default_headers = $_wp_default_headers;
   258 		$this->default_headers    = $_wp_default_headers;
   245 		$template_directory_uri = get_template_directory_uri();
   259 		$template_directory_uri   = get_template_directory_uri();
   246 		$stylesheet_directory_uri = get_stylesheet_directory_uri();
   260 		$stylesheet_directory_uri = get_stylesheet_directory_uri();
   247 		foreach ( array_keys($this->default_headers) as $header ) {
   261 		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 );
   262 			$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 );
   263 			$this->default_headers[ $header ]['thumbnail_url'] = sprintf( $this->default_headers[ $header ]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
   250 		}
   264 		}
   251 	}
   265 	}
   252 
   266 
   253 	/**
   267 	/**
   254 	 * Display UI for selecting one of several default headers.
   268 	 * Display UI for selecting one of several default headers.
   264 	public function show_header_selector( $type = 'default' ) {
   278 	public function show_header_selector( $type = 'default' ) {
   265 		if ( 'default' == $type ) {
   279 		if ( 'default' == $type ) {
   266 			$headers = $this->default_headers;
   280 			$headers = $this->default_headers;
   267 		} else {
   281 		} else {
   268 			$headers = get_uploaded_header_images();
   282 			$headers = get_uploaded_header_images();
   269 			$type = 'uploaded';
   283 			$type    = 'uploaded';
   270 		}
   284 		}
   271 
   285 
   272 		if ( 1 < count( $headers ) ) {
   286 		if ( 1 < count( $headers ) ) {
   273 			echo '<div class="random-header">';
   287 			echo '<div class="random-header">';
   274 			echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
   288 			echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
   278 		}
   292 		}
   279 
   293 
   280 		echo '<div class="available-headers">';
   294 		echo '<div class="available-headers">';
   281 		foreach ( $headers as $header_key => $header ) {
   295 		foreach ( $headers as $header_key => $header ) {
   282 			$header_thumbnail = $header['thumbnail_url'];
   296 			$header_thumbnail = $header['thumbnail_url'];
   283 			$header_url = $header['url'];
   297 			$header_url       = $header['url'];
   284 			$header_alt_text = empty( $header['alt_text'] ) ? '' : $header['alt_text'];
   298 			$header_alt_text  = empty( $header['alt_text'] ) ? '' : $header['alt_text'];
   285 			echo '<div class="default-header">';
   299 			echo '<div class="default-header">';
   286 			echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
   300 			echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
   287 			$width = '';
   301 			$width = '';
   288 			if ( !empty( $header['attachment_id'] ) )
   302 			if ( ! empty( $header['attachment_id'] ) ) {
   289 				$width = ' width="230"';
   303 				$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>';
   291 			echo '</div>';
   306 			echo '</div>';
   292 		}
   307 		}
   293 		echo '<div class="clear"></div></div>';
   308 		echo '<div class="clear"></div></div>';
   294 	}
   309 	}
   295 
   310 
   298 	 *
   313 	 *
   299 	 * @since 2.1.0
   314 	 * @since 2.1.0
   300 	 */
   315 	 */
   301 	public function js() {
   316 	public function js() {
   302 		$step = $this->step();
   317 		$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' ) ) {
   304 			$this->js_1();
   319 			$this->js_1();
   305 		elseif ( 2 == $step )
   320 		} elseif ( 2 == $step ) {
   306 			$this->js_2();
   321 			$this->js_2();
       
   322 		}
   307 	}
   323 	}
   308 
   324 
   309 	/**
   325 	/**
   310 	 * Display JavaScript based on Step 1 and 3.
   326 	 * Display JavaScript based on Step 1 and 3.
   311 	 *
   327 	 *
   362 		toggle_text();
   378 		toggle_text();
   363 		<?php endif; ?>
   379 		<?php endif; ?>
   364 	});
   380 	});
   365 })(jQuery);
   381 })(jQuery);
   366 </script>
   382 </script>
   367 <?php
   383 		<?php
   368 	}
   384 	}
   369 
   385 
   370 	/**
   386 	/**
   371 	 * Display JavaScript based on Step 2.
   387 	 * Display JavaScript based on Step 2.
   372 	 *
   388 	 *
   373 	 * @since 2.6.0
   389 	 * @since 2.6.0
   374 	 */
   390 	 */
   375 	public function js_2() { ?>
   391 	public function js_2() {
       
   392 
       
   393 		?>
   376 <script type="text/javascript">
   394 <script type="text/javascript">
   377 	function onEndCrop( coords ) {
   395 	function onEndCrop( coords ) {
   378 		jQuery( '#x1' ).val(coords.x);
   396 		jQuery( '#x1' ).val(coords.x);
   379 		jQuery( '#y1' ).val(coords.y);
   397 		jQuery( '#y1' ).val(coords.y);
   380 		jQuery( '#width' ).val(coords.w);
   398 		jQuery( '#width' ).val(coords.w);
   406 			y1: 0,
   424 			y1: 0,
   407 			x2: xinit,
   425 			x2: xinit,
   408 			y2: yinit,
   426 			y2: yinit,
   409 			<?php
   427 			<?php
   410 			if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   428 			if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   411 			?>
   429 				?>
   412 			aspectRatio: xinit + ':' + yinit,
   430 			aspectRatio: xinit + ':' + yinit,
   413 			<?php
   431 				<?php
   414 			}
   432 			}
   415 			if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
   433 			if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
   416 			?>
   434 				?>
   417 			maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>,
   435 			maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>,
   418 			<?php
   436 				<?php
   419 			}
   437 			}
   420 			if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   438 			if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   421 			?>
   439 				?>
   422 			maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>,
   440 			maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>,
   423 			<?php
   441 				<?php
   424 			}
   442 			}
   425 			?>
   443 			?>
   426 			onInit: function () {
   444 			onInit: function () {
   427 				jQuery('#width').val(xinit);
   445 				jQuery('#width').val(xinit);
   428 				jQuery('#height').val(yinit);
   446 				jQuery('#height').val(yinit);
   434 				jQuery('#height').val(c.height);
   452 				jQuery('#height').val(c.height);
   435 			}
   453 			}
   436 		});
   454 		});
   437 	});
   455 	});
   438 </script>
   456 </script>
   439 <?php
   457 		<?php
   440 	}
   458 	}
   441 
   459 
   442 	/**
   460 	/**
   443 	 * Display first step of custom header image page.
   461 	 * Display first step of custom header image page.
   444 	 *
   462 	 *
   445 	 * @since 2.1.0
   463 	 * @since 2.1.0
   446 	 */
   464 	 */
   447 	public function step_1() {
   465 	public function step_1() {
   448 		$this->process_default_headers();
   466 		$this->process_default_headers();
   449 ?>
   467 		?>
   450 
   468 
   451 <div class="wrap">
   469 <div class="wrap">
   452 <h1><?php _e( 'Custom Header' ); ?></h1>
   470 <h1><?php _e( 'Custom Header' ); ?></h1>
   453 
   471 
   454 <?php if ( current_user_can( 'customize' ) ) { ?>
   472 		<?php if ( current_user_can( 'customize' ) ) { ?>
   455 <div class="notice notice-info hide-if-no-customize">
   473 <div class="notice notice-info hide-if-no-customize">
   456 	<p>
   474 	<p>
   457 		<?php
   475 			<?php
   458 		printf(
   476 				printf(
   459 			__( 'You can now manage and live-preview Custom Header in the <a href="%1$s">Customizer</a>.' ),
   477 					__( 'You can now manage and live-preview Custom Header in the <a href="%1$s">Customizer</a>.' ),
   460 			admin_url( 'customize.php?autofocus[control]=header_image' )
   478 					admin_url( 'customize.php?autofocus[control]=header_image' )
   461 		);
   479 				);
   462 		?>
   480 			?>
   463 	</p>
   481 	</p>
   464 </div>
   482 </div>
   465 <?php } ?>
   483 		<?php } ?>
   466 
   484 
   467 <?php if ( ! empty( $this->updated ) ) { ?>
   485 		<?php if ( ! empty( $this->updated ) ) { ?>
   468 <div id="message" class="updated">
   486 <div id="message" class="updated">
   469 <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
   487 <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
   470 </div>
   488 </div>
   471 <?php } ?>
   489 		<?php } ?>
   472 
   490 
   473 <h3><?php _e( 'Header Image' ); ?></h3>
   491 <h2><?php _e( 'Header Image' ); ?></h2>
   474 
   492 
   475 <table class="form-table">
   493 <table class="form-table" role="presentation">
   476 <tbody>
   494 <tbody>
   477 
   495 
   478 <?php if ( get_custom_header() || display_header_text() ) : ?>
   496 		<?php if ( get_custom_header() || display_header_text() ) : ?>
   479 <tr>
   497 <tr>
   480 <th scope="row"><?php _e( 'Preview' ); ?></th>
   498 <th scope="row"><?php _e( 'Preview' ); ?></th>
   481 <td>
   499 <td>
   482 	<?php
   500 			<?php
   483 	if ( $this->admin_image_div_callback ) {
   501 			if ( $this->admin_image_div_callback ) {
   484 		call_user_func( $this->admin_image_div_callback );
   502 				call_user_func( $this->admin_image_div_callback );
   485 	} else {
   503 			} else {
   486 		$custom_header = get_custom_header();
   504 				$custom_header = get_custom_header();
   487 		$header_image = get_header_image();
   505 				$header_image  = get_header_image();
   488 
   506 
   489 		if ( $header_image ) {
   507 				if ( $header_image ) {
   490 			$header_image_style = 'background-image:url(' . esc_url( $header_image ) . ');';
   508 					$header_image_style = 'background-image:url(' . esc_url( $header_image ) . ');';
   491 		}  else {
   509 				} else {
   492 			$header_image_style = '';
   510 					$header_image_style = '';
   493 		}
   511 				}
   494 
   512 
   495 		if ( $custom_header->width )
   513 				if ( $custom_header->width ) {
   496 			$header_image_style .= 'max-width:' . $custom_header->width . 'px;';
   514 					$header_image_style .= 'max-width:' . $custom_header->width . 'px;';
   497 		if ( $custom_header->height )
   515 				}
   498 			$header_image_style .= 'height:' . $custom_header->height . 'px;';
   516 				if ( $custom_header->height ) {
   499 	?>
   517 					$header_image_style .= 'height:' . $custom_header->height . 'px;';
       
   518 				}
       
   519 				?>
   500 	<div id="headimg" style="<?php echo $header_image_style; ?>">
   520 	<div id="headimg" style="<?php echo $header_image_style; ?>">
   501 		<?php
   521 				<?php
   502 		if ( display_header_text() )
   522 				if ( display_header_text() ) {
   503 			$style = ' style="color:#' . get_header_textcolor() . ';"';
   523 					$style = ' style="color:#' . get_header_textcolor() . ';"';
   504 		else
   524 				} else {
   505 			$style = ' style="display:none;"';
   525 					$style = ' style="display:none;"';
   506 		?>
   526 				}
   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>
   527 				?>
       
   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>
   508 		<div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
   529 		<div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
   509 	</div>
   530 	</div>
   510 	<?php } ?>
   531 			<?php } ?>
   511 </td>
   532 </td>
   512 </tr>
   533 </tr>
   513 <?php endif; ?>
   534 		<?php endif; ?>
   514 
   535 
   515 <?php if ( current_user_can( 'upload_files' ) && current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
   536 		<?php if ( current_user_can( 'upload_files' ) && current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
   516 <tr>
   537 <tr>
   517 <th scope="row"><?php _e( 'Select Image' ); ?></th>
   538 <th scope="row"><?php _e( 'Select Image' ); ?></th>
   518 <td>
   539 <td>
   519 	<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 />
   540 	<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 />
   520 	<?php
   541 			<?php
   521 	if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   542 			if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
   522 		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' ) );
   543 				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' ) );
   523 	} elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
   544 			} 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' ) ) {
   525 			printf(
   546 					printf(
   526 				/* translators: %s: size in pixels */
   547 						/* translators: %s: size in pixels */
   527 				__( 'Images should be at least %s wide.' ) . ' ',
   548 						__( 'Images should be at least %s wide.' ) . ' ',
   528 				sprintf(
   549 						sprintf(
   529 					/* translators: %d: custom header width */
   550 							/* translators: %d: custom header width */
   530 					'<strong>' . __( '%d pixels' ) . '</strong>',
   551 							'<strong>' . __( '%d pixels' ) . '</strong>',
   531 					get_theme_support( 'custom-header', 'width' )
   552 							get_theme_support( 'custom-header', 'width' )
   532 				)
   553 						)
   533 			);
   554 					);
   534 	} elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
   555 				}
   535 		if ( ! current_theme_supports( 'custom-header', 'flex-height' ) )
   556 			} elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
   536 			printf(
   557 				if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
   537 				/* translators: %s: size in pixels */
   558 					printf(
   538 				__( 'Images should be at least %s tall.' ) . ' ',
   559 						/* translators: %s: size in pixels */
   539 				sprintf(
   560 						__( 'Images should be at least %s tall.' ) . ' ',
   540 					/* translators: %d: custom header height */
   561 						sprintf(
   541 					'<strong>' . __( '%d pixels' ) . '</strong>',
   562 							/* translators: %d: custom header height */
   542 					get_theme_support( 'custom-header', 'height' )
   563 							'<strong>' . __( '%d pixels' ) . '</strong>',
   543 				)
   564 							get_theme_support( 'custom-header', 'height' )
   544 			);
   565 						)
   545 	}
   566 					);
   546 	if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
   567 				}
   547 		if ( current_theme_supports( 'custom-header', 'width' ) )
   568 			}
   548 			printf(
   569 			if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
   549 				/* translators: %s: size in pixels */
   570 				if ( current_theme_supports( 'custom-header', 'width' ) ) {
   550 				__( 'Suggested width is %s.' ) . ' ',
   571 					printf(
   551 				sprintf(
   572 						/* translators: %s: size in pixels */
   552 					/* translators: %d: custom header width */
   573 						__( 'Suggested width is %s.' ) . ' ',
   553 					'<strong>' . __( '%d pixels' ) . '</strong>',
   574 						sprintf(
   554 					get_theme_support( 'custom-header', 'width' )
   575 							/* translators: %d: custom header width */
   555 				)
   576 							'<strong>' . __( '%d pixels' ) . '</strong>',
   556 			);
   577 							get_theme_support( 'custom-header', 'width' )
   557 		if ( current_theme_supports( 'custom-header', 'height' ) )
   578 						)
   558 			printf(
   579 					);
   559 				/* translators: %s: size in pixels */
   580 				}
   560 				__( 'Suggested height is %s.' ) . ' ',
   581 				if ( current_theme_supports( 'custom-header', 'height' ) ) {
   561 				sprintf(
   582 					printf(
   562 					/* translators: %d: custom header height */
   583 						/* translators: %s: size in pixels */
   563 					'<strong>' . __( '%d pixels' ) . '</strong>',
   584 						__( 'Suggested height is %s.' ) . ' ',
   564 					get_theme_support( 'custom-header', 'height' )
   585 						sprintf(
   565 				)
   586 							/* translators: %d: custom header height */
   566 			);
   587 							'<strong>' . __( '%d pixels' ) . '</strong>',
   567 	}
   588 							get_theme_support( 'custom-header', 'height' )
   568 	?></p>
   589 						)
   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 ) ) ?>">
   590 					);
       
   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 ) ); ?>">
   570 	<p>
   596 	<p>
   571 		<label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
   597 		<label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
   572 		<input type="file" id="upload" name="import" />
   598 		<input type="file" id="upload" name="import" />
   573 		<input type="hidden" name="action" value="save" />
   599 		<input type="hidden" name="action" value="save" />
   574 		<?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?>
   600 			<?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?>
   575 		<?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
   601 			<?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
   576 	</p>
   602 	</p>
   577 	<?php
   603 			<?php
   578 		$modal_update_href = esc_url( add_query_arg( array(
   604 				$modal_update_href = esc_url(
   579 			'page' => 'custom-header',
   605 					add_query_arg(
   580 			'step' => 2,
   606 						array(
   581 			'_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'),
   607 							'page' => 'custom-header',
   582 		), admin_url('themes.php') ) );
   608 							'step' => 2,
   583 	?>
   609 							'_wpnonce-custom-header-upload' => wp_create_nonce( 'custom-header-upload' ),
       
   610 						),
       
   611 						admin_url( 'themes.php' )
       
   612 					)
       
   613 				);
       
   614 			?>
   584 	<p>
   615 	<p>
   585 		<label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
   616 		<label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
   586 		<button id="choose-from-library-link" class="button"
   617 		<button id="choose-from-library-link" class="button"
   587 			data-update-link="<?php echo esc_attr( $modal_update_href ); ?>"
   618 			data-update-link="<?php echo esc_attr( $modal_update_href ); ?>"
   588 			data-choose="<?php esc_attr_e( 'Choose a Custom Header' ); ?>"
   619 			data-choose="<?php esc_attr_e( 'Choose a Custom Header' ); ?>"
   589 			data-update="<?php esc_attr_e( 'Set as header' ); ?>"><?php _e( 'Choose Image' ); ?></button>
   620 			data-update="<?php esc_attr_e( 'Set as header' ); ?>"><?php _e( 'Choose Image' ); ?></button>
   590 	</p>
   621 	</p>
   591 	</form>
   622 	</form>
   592 </td>
   623 </td>
   593 </tr>
   624 </tr>
   594 <?php endif; ?>
   625 		<?php endif; ?>
   595 </tbody>
   626 </tbody>
   596 </table>
   627 </table>
   597 
   628 
   598 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ) ?>">
   629 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>">
   599 <?php submit_button( null, 'screen-reader-text', 'save-header-options', false ); ?>
   630 		<?php submit_button( null, 'screen-reader-text', 'save-header-options', false ); ?>
   600 <table class="form-table">
   631 <table class="form-table" role="presentation">
   601 <tbody>
   632 <tbody>
   602 	<?php if ( get_uploaded_header_images() ) : ?>
   633 		<?php if ( get_uploaded_header_images() ) : ?>
   603 <tr>
   634 <tr>
   604 <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
   635 <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
   605 <td>
   636 <td>
   606 	<p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
   637 	<p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ); ?></p>
   607 	<?php
   638 			<?php
   608 		$this->show_header_selector( 'uploaded' );
   639 			$this->show_header_selector( 'uploaded' );
   609 	?>
   640 			?>
   610 </td>
   641 </td>
   611 </tr>
   642 </tr>
   612 	<?php endif;
   643 			<?php
   613 	if ( ! empty( $this->default_headers ) ) : ?>
   644 	endif;
       
   645 		if ( ! empty( $this->default_headers ) ) :
       
   646 			?>
   614 <tr>
   647 <tr>
   615 <th scope="row"><?php _e( 'Default Images' ); ?></th>
   648 <th scope="row"><?php _e( 'Default Images' ); ?></th>
   616 <td>
   649 <td>
   617 <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
   650 			<?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>
   651 	<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: ?>
   652 	<?php else : ?>
   620 	<p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
   653 	<p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ); ?></p>
   621 <?php endif; ?>
   654 	<?php endif; ?>
   622 	<?php
   655 			<?php
   623 		$this->show_header_selector( 'default' );
   656 			$this->show_header_selector( 'default' );
   624 	?>
   657 			?>
   625 </td>
   658 </td>
   626 </tr>
   659 </tr>
   627 	<?php endif;
   660 			<?php
   628 	if ( get_header_image() ) : ?>
   661 	endif;
       
   662 		if ( get_header_image() ) :
       
   663 			?>
   629 <tr>
   664 <tr>
   630 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
   665 <th scope="row"><?php _e( 'Remove Image' ); ?></th>
   631 <td>
   666 <td>
   632 	<p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
   667 	<p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ); ?></p>
   633 	<?php submit_button( __( 'Remove Header Image' ), '', 'removeheader', false ); ?>
   668 			<?php submit_button( __( 'Remove Header Image' ), '', 'removeheader', false ); ?>
   634 </td>
   669 </td>
   635 </tr>
   670 </tr>
   636 	<?php endif;
   671 			<?php
   637 
   672 	endif;
   638 	$default_image = sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() );
   673 
   639 	if ( $default_image && get_header_image() != $default_image ) : ?>
   674 		$default_image = sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() );
       
   675 		if ( $default_image && get_header_image() != $default_image ) :
       
   676 			?>
   640 <tr>
   677 <tr>
   641 <th scope="row"><?php _e( 'Reset Image' ); ?></th>
   678 <th scope="row"><?php _e( 'Reset Image' ); ?></th>
   642 <td>
   679 <td>
   643 	<p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
   680 	<p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ); ?></p>
   644 	<?php submit_button( __( 'Restore Original Header Image' ), '', 'resetheader', false ); ?>
   681 			<?php submit_button( __( 'Restore Original Header Image' ), '', 'resetheader', false ); ?>
   645 </td>
   682 </td>
   646 </tr>
   683 </tr>
   647 	<?php endif; ?>
   684 	<?php endif; ?>
   648 </tbody>
   685 </tbody>
   649 </table>
   686 </table>
   650 
   687 
   651 <?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?>
   688 		<?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?>
   652 
   689 
   653 <h3><?php _e( 'Header Text' ); ?></h3>
   690 <h2><?php _e( 'Header Text' ); ?></h2>
   654 
   691 
   655 <table class="form-table">
   692 <table class="form-table" role="presentation">
   656 <tbody>
   693 <tbody>
   657 <tr>
   694 <tr>
   658 <th scope="row"><?php _e( 'Header Text' ); ?></th>
   695 <th scope="row"><?php _e( 'Header Text' ); ?></th>
   659 <td>
   696 <td>
   660 	<p>
   697 	<p>
   665 
   702 
   666 <tr class="displaying-header-text">
   703 <tr class="displaying-header-text">
   667 <th scope="row"><?php _e( 'Text Color' ); ?></th>
   704 <th scope="row"><?php _e( 'Text Color' ); ?></th>
   668 <td>
   705 <td>
   669 	<p>
   706 	<p>
   670 	<?php
   707 			<?php
   671 	$default_color = '';
   708 			$default_color = '';
   672 	if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
   709 			if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
   673 		$default_color = get_theme_support( 'custom-header', 'default-text-color' );
   710 				$default_color = get_theme_support( 'custom-header', 'default-text-color' );
   674 		if ( $default_color && false === strpos( $default_color, '#' ) ) {
   711 				if ( $default_color && false === strpos( $default_color, '#' ) ) {
   675 			$default_color = '#' . $default_color;
   712 					$default_color = '#' . $default_color;
   676 		}
   713 				}
   677 	}
   714 			}
   678 
   715 
   679 	$default_color_attr = $default_color ? ' data-default-color="' . esc_attr( $default_color ) . '"' : '';
   716 			$default_color_attr = $default_color ? ' data-default-color="' . esc_attr( $default_color ) . '"' : '';
   680 
   717 
   681 	$header_textcolor = display_header_text() ? get_header_textcolor() : get_theme_support( 'custom-header', 'default-text-color' );
   718 			$header_textcolor = display_header_text() ? get_header_textcolor() : get_theme_support( 'custom-header', 'default-text-color' );
   682 	if ( $header_textcolor && false === strpos( $header_textcolor, '#' ) ) {
   719 			if ( $header_textcolor && false === strpos( $header_textcolor, '#' ) ) {
   683 		$header_textcolor = '#' . $header_textcolor;
   720 				$header_textcolor = '#' . $header_textcolor;
   684 	}
   721 			}
   685 
   722 
   686 	echo '<input type="text" name="text-color" id="text-color" value="' . esc_attr( $header_textcolor ) . '"' . $default_color_attr . ' />';
   723 			echo '<input type="text" name="text-color" id="text-color" value="' . esc_attr( $header_textcolor ) . '"' . $default_color_attr . ' />';
   687 	if ( $default_color ) {
   724 			if ( $default_color ) {
   688 		echo ' <span class="description hide-if-js">' . sprintf( _x( 'Default: %s', 'color' ), esc_html( $default_color ) ) . '</span>';
   725 				echo ' <span class="description hide-if-js">' . sprintf( _x( 'Default: %s', 'color' ), esc_html( $default_color ) ) . '</span>';
   689 	}
   726 			}
   690 	?>
   727 			?>
   691 	</p>
   728 	</p>
   692 </td>
   729 </td>
   693 </tr>
   730 </tr>
   694 </tbody>
   731 </tbody>
   695 </table>
   732 </table>
   696 <?php endif;
   733 			<?php
   697 
   734 endif;
   698 /**
   735 
   699  * Fires just before the submit button in the custom header options form.
   736 		/**
   700  *
   737 		 * Fires just before the submit button in the custom header options form.
   701  * @since 3.1.0
   738 		 *
   702  */
   739 		 * @since 3.1.0
   703 do_action( 'custom_header_options' );
   740 		 */
   704 
   741 		do_action( 'custom_header_options' );
   705 wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
   742 
   706 
   743 		wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' );
   707 <?php submit_button( null, 'primary', 'save-header-options' ); ?>
   744 		?>
       
   745 
       
   746 		<?php submit_button( null, 'primary', 'save-header-options' ); ?>
   708 </form>
   747 </form>
   709 </div>
   748 </div>
   710 
   749 
   711 <?php }
   750 		<?php
       
   751 	}
   712 
   752 
   713 	/**
   753 	/**
   714 	 * Display second step of custom header image page.
   754 	 * Display second step of custom header image page.
   715 	 *
   755 	 *
   716 	 * @since 2.1.0
   756 	 * @since 2.1.0
   717 	 */
   757 	 */
   718 	public function step_2() {
   758 	public function step_2() {
   719 		check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
   759 		check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' );
   720 		if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
   760 		if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
   721 			wp_die(
   761 			wp_die(
   722 				'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
   762 				'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
   723 				'<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>',
   763 				'<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>',
   724 				403
   764 				403
   725 			);
   765 			);
   726 		}
   766 		}
   727 
   767 
   728 		if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
   768 		if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
   729 			$attachment_id = absint( $_GET['file'] );
   769 			$attachment_id = absint( $_GET['file'] );
   730 			$file = get_attached_file( $attachment_id, true );
   770 			$file          = get_attached_file( $attachment_id, true );
   731 			$url = wp_get_attachment_image_src( $attachment_id, 'full' );
   771 			$url           = wp_get_attachment_image_src( $attachment_id, 'full' );
   732 			$url = $url[0];
   772 			$url           = $url[0];
   733 		} elseif ( isset( $_POST ) ) {
   773 		} elseif ( isset( $_POST ) ) {
   734 			$data = $this->step_2_manage_upload();
   774 			$data          = $this->step_2_manage_upload();
   735 			$attachment_id = $data['attachment_id'];
   775 			$attachment_id = $data['attachment_id'];
   736 			$file = $data['file'];
   776 			$file          = $data['file'];
   737 			$url = $data['url'];
   777 			$url           = $data['url'];
   738 		}
   778 		}
   739 
   779 
   740 		if ( file_exists( $file ) ) {
   780 		if ( file_exists( $file ) ) {
   741 			list( $width, $height, $type, $attr ) = getimagesize( $file );
   781 			list( $width, $height, $type, $attr ) = getimagesize( $file );
   742 		} else {
   782 		} else {
   743 			$data = wp_get_attachment_metadata( $attachment_id );
   783 			$data   = wp_get_attachment_metadata( $attachment_id );
   744 			$height = isset( $data[ 'height' ] ) ? $data[ 'height' ] : 0;
   784 			$height = isset( $data['height'] ) ? $data['height'] : 0;
   745 			$width = isset( $data[ 'width' ] ) ? $data[ 'width' ] : 0;
   785 			$width  = isset( $data['width'] ) ? $data['width'] : 0;
   746 			unset( $data );
   786 			unset( $data );
   747 		}
   787 		}
   748 
   788 
   749 		$max_width = 0;
   789 		$max_width = 0;
   750 		// For flex, limit size of image displayed to 1500px unless theme says otherwise
   790 		// For flex, limit size of image displayed to 1500px unless theme says otherwise
   751 		if ( current_theme_supports( 'custom-header', 'flex-width' ) )
   791 		if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
   752 			$max_width = 1500;
   792 			$max_width = 1500;
   753 
   793 		}
   754 		if ( current_theme_supports( 'custom-header', 'max-width' ) )
   794 
       
   795 		if ( current_theme_supports( 'custom-header', 'max-width' ) ) {
   755 			$max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
   796 			$max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
       
   797 		}
   756 		$max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
   798 		$max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
   757 
   799 
   758 		// If flexible height isn't supported and the image is the exact right size
   800 		// If flexible height isn't supported and the image is the exact right size
   759 		if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' )
   801 		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' ) )
   802 			&& $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) ) {
   761 		{
       
   762 			// Add the meta-data
   803 			// Add the meta-data
   763 			if ( file_exists( $file ) )
   804 			if ( file_exists( $file ) ) {
   764 				wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
   805 				wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
       
   806 			}
   765 
   807 
   766 			$this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
   808 			$this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
   767 
   809 
   768 			/**
   810 			/**
   769 			 * Fires after the header image is set or an error is returned.
   811 			 * Fires after the header image is set or an error is returned.
   776 			do_action( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication
   818 			do_action( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication
   777 
   819 
   778 			return $this->finished();
   820 			return $this->finished();
   779 		} elseif ( $width > $max_width ) {
   821 		} elseif ( $width > $max_width ) {
   780 			$oitar = $width / $max_width;
   822 			$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));
   823 			$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 ) );
   782 			if ( ! $image || is_wp_error( $image ) )
   824 			if ( ! $image || is_wp_error( $image ) ) {
   783 				wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
   825 				wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
       
   826 			}
   784 
   827 
   785 			/** This filter is documented in wp-admin/custom-header.php */
   828 			/** This filter is documented in wp-admin/custom-header.php */
   786 			$image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication
   829 			$image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication
   787 
   830 
   788 			$url = str_replace(basename($url), basename($image), $url);
   831 			$url    = str_replace( wp_basename( $url ), wp_basename( $image ), $url );
   789 			$width = $width / $oitar;
   832 			$width  = $width / $oitar;
   790 			$height = $height / $oitar;
   833 			$height = $height / $oitar;
   791 		} else {
   834 		} else {
   792 			$oitar = 1;
   835 			$oitar = 1;
   793 		}
   836 		}
   794 		?>
   837 		?>
   795 
   838 
   796 <div class="wrap">
   839 <div class="wrap">
   797 <h1><?php _e( 'Crop Header Image' ); ?></h1>
   840 <h1><?php _e( 'Crop Header Image' ); ?></h1>
   798 
   841 
   799 <form method="post" action="<?php echo esc_url(add_query_arg('step', 3)); ?>">
   842 <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>
   843 	<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>
   844 	<p class="hide-if-js"><strong><?php _e( 'You need JavaScript to choose a part of the image.' ); ?></strong></p>
   802 
   845 
   803 	<div id="crop_image" style="position: relative">
   846 	<div id="crop_image" style="position: relative">
   804 		<img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="" />
   847 		<img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="" />
   805 	</div>
   848 	</div>
   806 
   849 
   808 	<input type="hidden" name="y1" id="y1" value="0"/>
   851 	<input type="hidden" name="y1" id="y1" value="0"/>
   809 	<input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
   852 	<input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
   810 	<input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
   853 	<input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
   811 	<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
   854 	<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
   812 	<input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
   855 	<input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
   813 	<?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
   856 		<?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
   814 	<input type="hidden" name="create-new-attachment" value="true" />
   857 	<input type="hidden" name="create-new-attachment" value="true" />
   815 	<?php } ?>
   858 	<?php } ?>
   816 	<?php wp_nonce_field( 'custom-header-crop-image' ) ?>
   859 		<?php wp_nonce_field( 'custom-header-crop-image' ); ?>
   817 
   860 
   818 	<p class="submit">
   861 	<p class="submit">
   819 	<?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
   862 		<?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
   820 	<?php
   863 		<?php
   821 	if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) )
   864 		if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) {
   822 		submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false );
   865 			submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false );
   823 	?>
   866 		}
       
   867 		?>
   824 	</p>
   868 	</p>
   825 </form>
   869 </form>
   826 </div>
   870 </div>
   827 		<?php
   871 		<?php
   828 	}
   872 	}
   832 	 * Upload the file to be cropped in the second step.
   876 	 * Upload the file to be cropped in the second step.
   833 	 *
   877 	 *
   834 	 * @since 3.4.0
   878 	 * @since 3.4.0
   835 	 */
   879 	 */
   836 	public function step_2_manage_upload() {
   880 	public function step_2_manage_upload() {
   837 		$overrides = array('test_form' => false);
   881 		$overrides = array( 'test_form' => false );
   838 
   882 
   839 		$uploaded_file = $_FILES['import'];
   883 		$uploaded_file = $_FILES['import'];
   840 		$wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
   884 		$wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
   841 		if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
   885 		if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
   842 			wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
   886 			wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
   843 
   887 		}
   844 		$file = wp_handle_upload($uploaded_file, $overrides);
   888 
   845 
   889 		$file = wp_handle_upload( $uploaded_file, $overrides );
   846 		if ( isset($file['error']) )
   890 
   847 			wp_die( $file['error'],  __( 'Image Upload Error' ) );
   891 		if ( isset( $file['error'] ) ) {
   848 
   892 			wp_die( $file['error'], __( 'Image Upload Error' ) );
   849 		$url = $file['url'];
   893 		}
   850 		$type = $file['type'];
   894 
   851 		$file = $file['file'];
   895 		$url      = $file['url'];
   852 		$filename = basename($file);
   896 		$type     = $file['type'];
       
   897 		$file     = $file['file'];
       
   898 		$filename = wp_basename( $file );
   853 
   899 
   854 		// Construct the object array
   900 		// Construct the object array
   855 		$object = array(
   901 		$object = array(
   856 			'post_title'     => $filename,
   902 			'post_title'     => $filename,
   857 			'post_content'   => $url,
   903 			'post_content'   => $url,
   858 			'post_mime_type' => $type,
   904 			'post_mime_type' => $type,
   859 			'guid'           => $url,
   905 			'guid'           => $url,
   860 			'context'        => 'custom-header'
   906 			'context'        => 'custom-header',
   861 		);
   907 		);
   862 
   908 
   863 		// Save the data
   909 		// Save the data
   864 		$attachment_id = wp_insert_attachment( $object, $file );
   910 		$attachment_id = wp_insert_attachment( $object, $file );
   865 		return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
   911 		return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
   890 				403
   936 				403
   891 			);
   937 			);
   892 		}
   938 		}
   893 
   939 
   894 		if ( $_POST['oitar'] > 1 ) {
   940 		if ( $_POST['oitar'] > 1 ) {
   895 			$_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
   941 			$_POST['x1']     = $_POST['x1'] * $_POST['oitar'];
   896 			$_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
   942 			$_POST['y1']     = $_POST['y1'] * $_POST['oitar'];
   897 			$_POST['width'] = $_POST['width'] * $_POST['oitar'];
   943 			$_POST['width']  = $_POST['width'] * $_POST['oitar'];
   898 			$_POST['height'] = $_POST['height'] * $_POST['oitar'];
   944 			$_POST['height'] = $_POST['height'] * $_POST['oitar'];
   899 		}
   945 		}
   900 
   946 
   901 		$attachment_id = absint( $_POST['attachment_id'] );
   947 		$attachment_id = absint( $_POST['attachment_id'] );
   902 		$original = get_attached_file($attachment_id);
   948 		$original      = get_attached_file( $attachment_id );
   903 
   949 
   904 		$dimensions = $this->get_header_dimensions( array(
   950 		$dimensions = $this->get_header_dimensions(
   905 			'height' => $_POST['height'],
   951 			array(
   906 			'width'  => $_POST['width'],
   952 				'height' => $_POST['height'],
   907 		) );
   953 				'width'  => $_POST['width'],
   908 		$height = $dimensions['dst_height'];
   954 			)
   909 		$width = $dimensions['dst_width'];
   955 		);
   910 
   956 		$height     = $dimensions['dst_height'];
   911 		if ( empty( $_POST['skip-cropping'] ) )
   957 		$width      = $dimensions['dst_width'];
       
   958 
       
   959 		if ( empty( $_POST['skip-cropping'] ) ) {
   912 			$cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height );
   960 			$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'] ) )
   961 		} elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
   914 			$cropped = _copy_image_file( $attachment_id );
   962 			$cropped = _copy_image_file( $attachment_id );
   915 		else
   963 		} else {
   916 			$cropped = get_attached_file( $attachment_id );
   964 			$cropped = get_attached_file( $attachment_id );
   917 
   965 		}
   918 		if ( ! $cropped || is_wp_error( $cropped ) )
   966 
       
   967 		if ( ! $cropped || is_wp_error( $cropped ) ) {
   919 			wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
   968 			wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
       
   969 		}
   920 
   970 
   921 		/** This filter is documented in wp-admin/custom-header.php */
   971 		/** This filter is documented in wp-admin/custom-header.php */
   922 		$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
   972 		$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
   923 
   973 
   924 		$object = $this->create_attachment_object( $cropped, $attachment_id );
   974 		$object = $this->create_attachment_object( $cropped, $attachment_id );
   925 
   975 
   926 		if ( ! empty( $_POST['create-new-attachment'] ) )
   976 		if ( ! empty( $_POST['create-new-attachment'] ) ) {
   927 			unset( $object['ID'] );
   977 			unset( $object['ID'] );
       
   978 		}
   928 
   979 
   929 		// Update the attachment
   980 		// Update the attachment
   930 		$attachment_id = $this->insert_attachment( $object, $cropped );
   981 		$attachment_id = $this->insert_attachment( $object, $cropped );
   931 
   982 
   932 		$url = wp_get_attachment_url( $attachment_id );
   983 		$url = wp_get_attachment_url( $attachment_id );
   933 		$this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
   984 		$this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
   934 
   985 
   935 		// Cleanup.
   986 		// Cleanup.
   936 		$medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
   987 		$medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original ), $original );
   937 		if ( file_exists( $medium ) ) {
   988 		if ( file_exists( $medium ) ) {
   938 			wp_delete_file( $medium );
   989 			wp_delete_file( $medium );
   939 		}
   990 		}
   940 
   991 
   941 		if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
   992 		if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
   959 	 * Display the page based on the current step.
  1010 	 * Display the page based on the current step.
   960 	 *
  1011 	 *
   961 	 * @since 2.1.0
  1012 	 * @since 2.1.0
   962 	 */
  1013 	 */
   963 	public function admin_page() {
  1014 	public function admin_page() {
   964 		if ( ! current_user_can('edit_theme_options') )
  1015 		if ( ! current_user_can( 'edit_theme_options' ) ) {
   965 			wp_die(__('Sorry, you are not allowed to customize headers.'));
  1016 			wp_die( __( 'Sorry, you are not allowed to customize headers.' ) );
       
  1017 		}
   966 		$step = $this->step();
  1018 		$step = $this->step();
   967 		if ( 2 == $step )
  1019 		if ( 2 == $step ) {
   968 			$this->step_2();
  1020 			$this->step_2();
   969 		elseif ( 3 == $step )
  1021 		} elseif ( 3 == $step ) {
   970 			$this->step_3();
  1022 			$this->step_3();
   971 		else
  1023 		} else {
   972 			$this->step_1();
  1024 			$this->step_1();
       
  1025 		}
   973 	}
  1026 	}
   974 
  1027 
   975 	/**
  1028 	/**
   976 	 * Unused since 3.5.0.
  1029 	 * Unused since 3.5.0.
   977 	 *
  1030 	 *
  1001 	 * or provide an array of uploaded header data (either new, or from media library).
  1054 	 * or provide an array of uploaded header data (either new, or from media library).
  1002 	 *
  1055 	 *
  1003 	 * @since 3.4.0
  1056 	 * @since 3.4.0
  1004 	 *
  1057 	 *
  1005 	 * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
  1058 	 * @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
  1059 	 *  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
  1060 	 *  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).
  1061 	 *  the key of an image uploaded for that theme (the attachment ID of the image).
  1009 	 *  Or an array of arguments: attachment_id, url, width, height. All are required.
  1062 	 *  Or an array of arguments: attachment_id, url, width, height. All are required.
  1010 	 */
  1063 	 */
  1011 	final public function set_header_image( $choice ) {
  1064 	final public function set_header_image( $choice ) {
  1012 		if ( is_array( $choice ) || is_object( $choice ) ) {
  1065 		if ( is_array( $choice ) || is_object( $choice ) ) {
  1013 			$choice = (array) $choice;
  1066 			$choice = (array) $choice;
  1014 			if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) )
  1067 			if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) {
  1015 				return;
  1068 				return;
       
  1069 			}
  1016 
  1070 
  1017 			$choice['url'] = esc_url_raw( $choice['url'] );
  1071 			$choice['url'] = esc_url_raw( $choice['url'] );
  1018 
  1072 
  1019 			$header_image_data = (object) array(
  1073 			$header_image_data = (object) array(
  1020 				'attachment_id' => $choice['attachment_id'],
  1074 				'attachment_id' => $choice['attachment_id'],
  1040 		if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
  1094 		if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
  1041 			$header_image_data = $uploaded[ $choice ];
  1095 			$header_image_data = $uploaded[ $choice ];
  1042 
  1096 
  1043 		} else {
  1097 		} else {
  1044 			$this->process_default_headers();
  1098 			$this->process_default_headers();
  1045 			if ( isset( $this->default_headers[ $choice ] ) )
  1099 			if ( isset( $this->default_headers[ $choice ] ) ) {
  1046 				$header_image_data = $this->default_headers[ $choice ];
  1100 				$header_image_data = $this->default_headers[ $choice ];
  1047 			else
  1101 			} else {
  1048 				return;
  1102 				return;
       
  1103 			}
  1049 		}
  1104 		}
  1050 
  1105 
  1051 		set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
  1106 		set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
  1052 		set_theme_mod( 'header_image_data', $header_image_data );
  1107 		set_theme_mod( 'header_image_data', $header_image_data );
  1053 	}
  1108 	}
  1097 	 *
  1152 	 *
  1098 	 * @param array $dimensions
  1153 	 * @param array $dimensions
  1099 	 * @return array dst_height and dst_width of header image.
  1154 	 * @return array dst_height and dst_width of header image.
  1100 	 */
  1155 	 */
  1101 	final public function get_header_dimensions( $dimensions ) {
  1156 	final public function get_header_dimensions( $dimensions ) {
  1102 		$max_width = 0;
  1157 		$max_width       = 0;
  1103 		$width = absint( $dimensions['width'] );
  1158 		$width           = absint( $dimensions['width'] );
  1104 		$height = absint( $dimensions['height'] );
  1159 		$height          = absint( $dimensions['height'] );
  1105 		$theme_height = get_theme_support( 'custom-header', 'height' );
  1160 		$theme_height    = get_theme_support( 'custom-header', 'height' );
  1106 		$theme_width = get_theme_support( 'custom-header', 'width' );
  1161 		$theme_width     = get_theme_support( 'custom-header', 'width' );
  1107 		$has_flex_width = current_theme_supports( 'custom-header', 'flex-width' );
  1162 		$has_flex_width  = current_theme_supports( 'custom-header', 'flex-width' );
  1108 		$has_flex_height = current_theme_supports( 'custom-header', 'flex-height' );
  1163 		$has_flex_height = current_theme_supports( 'custom-header', 'flex-height' );
  1109 		$has_max_width = current_theme_supports( 'custom-header', 'max-width' ) ;
  1164 		$has_max_width   = current_theme_supports( 'custom-header', 'max-width' );
  1110 		$dst = array( 'dst_height' => null, 'dst_width' => null );
  1165 		$dst             = array(
       
  1166 			'dst_height' => null,
       
  1167 			'dst_width'  => null,
       
  1168 		);
  1111 
  1169 
  1112 		// For flex, limit size of image displayed to 1500px unless theme says otherwise
  1170 		// For flex, limit size of image displayed to 1500px unless theme says otherwise
  1113 		if ( $has_flex_width ) {
  1171 		if ( $has_flex_width ) {
  1114 			$max_width = 1500;
  1172 			$max_width = 1500;
  1115 		}
  1173 		}
  1119 		}
  1177 		}
  1120 		$max_width = max( $max_width, $theme_width );
  1178 		$max_width = max( $max_width, $theme_width );
  1121 
  1179 
  1122 		if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
  1180 		if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
  1123 			$dst['dst_height'] = absint( $height * ( $max_width / $width ) );
  1181 			$dst['dst_height'] = absint( $height * ( $max_width / $width ) );
  1124 		}
  1182 		} elseif ( $has_flex_height && $has_flex_width ) {
  1125 		elseif ( $has_flex_height && $has_flex_width ) {
       
  1126 			$dst['dst_height'] = $height;
  1183 			$dst['dst_height'] = $height;
  1127 		}
  1184 		} else {
  1128 		else {
       
  1129 			$dst['dst_height'] = $theme_height;
  1185 			$dst['dst_height'] = $theme_height;
  1130 		}
  1186 		}
  1131 
  1187 
  1132 		if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
  1188 		if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
  1133 			$dst['dst_width'] = absint( $width * ( $max_width / $width ) );
  1189 			$dst['dst_width'] = absint( $width * ( $max_width / $width ) );
  1134 		}
  1190 		} elseif ( $has_flex_width && $has_flex_height ) {
  1135 		elseif ( $has_flex_width && $has_flex_height ) {
       
  1136 			$dst['dst_width'] = $width;
  1191 			$dst['dst_width'] = $width;
  1137 		}
  1192 		} else {
  1138 		else {
       
  1139 			$dst['dst_width'] = $theme_width;
  1193 			$dst['dst_width'] = $theme_width;
  1140 		}
  1194 		}
  1141 
  1195 
  1142 		return $dst;
  1196 		return $dst;
  1143 	}
  1197 	}
  1150 	 * @param string $cropped              Cropped image URL.
  1204 	 * @param string $cropped              Cropped image URL.
  1151 	 * @param int    $parent_attachment_id Attachment ID of parent image.
  1205 	 * @param int    $parent_attachment_id Attachment ID of parent image.
  1152 	 * @return array Attachment object.
  1206 	 * @return array Attachment object.
  1153 	 */
  1207 	 */
  1154 	final public function create_attachment_object( $cropped, $parent_attachment_id ) {
  1208 	final public function create_attachment_object( $cropped, $parent_attachment_id ) {
  1155 		$parent = get_post( $parent_attachment_id );
  1209 		$parent     = get_post( $parent_attachment_id );
  1156 		$parent_url = wp_get_attachment_url( $parent->ID );
  1210 		$parent_url = wp_get_attachment_url( $parent->ID );
  1157 		$url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
  1211 		$url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
  1158 
  1212 
  1159 		$size = @getimagesize( $cropped );
  1213 		$size       = @getimagesize( $cropped );
  1160 		$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  1214 		$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  1161 
  1215 
  1162 		$object = array(
  1216 		$object = array(
  1163 			'ID' => $parent_attachment_id,
  1217 			'ID'             => $parent_attachment_id,
  1164 			'post_title' => basename($cropped),
  1218 			'post_title'     => wp_basename( $cropped ),
  1165 			'post_mime_type' => $image_type,
  1219 			'post_mime_type' => $image_type,
  1166 			'guid' => $url,
  1220 			'guid'           => $url,
  1167 			'context' => 'custom-header',
  1221 			'context'        => 'custom-header',
  1168 			'post_parent' => $parent_attachment_id,
  1222 			'post_parent'    => $parent_attachment_id,
  1169 		);
  1223 		);
  1170 
  1224 
  1171 		return $object;
  1225 		return $object;
  1172 	}
  1226 	}
  1173 
  1227 
  1183 	final public function insert_attachment( $object, $cropped ) {
  1237 	final public function insert_attachment( $object, $cropped ) {
  1184 		$parent_id = isset( $object['post_parent'] ) ? $object['post_parent'] : null;
  1238 		$parent_id = isset( $object['post_parent'] ) ? $object['post_parent'] : null;
  1185 		unset( $object['post_parent'] );
  1239 		unset( $object['post_parent'] );
  1186 
  1240 
  1187 		$attachment_id = wp_insert_attachment( $object, $cropped );
  1241 		$attachment_id = wp_insert_attachment( $object, $cropped );
  1188 		$metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
  1242 		$metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
  1189 
  1243 
  1190 		// If this is a crop, save the original attachment ID as metadata.
  1244 		// If this is a crop, save the original attachment ID as metadata.
  1191 		if ( $parent_id ) {
  1245 		if ( $parent_id ) {
  1192 			$metadata['attachment_parent'] = $parent_id;
  1246 			$metadata['attachment_parent'] = $parent_id;
  1193 		}
  1247 		}
  1225 			wp_send_json_error();
  1279 			wp_send_json_error();
  1226 		}
  1280 		}
  1227 
  1281 
  1228 		$crop_details = $_POST['cropDetails'];
  1282 		$crop_details = $_POST['cropDetails'];
  1229 
  1283 
  1230 		$dimensions = $this->get_header_dimensions( array(
  1284 		$dimensions = $this->get_header_dimensions(
  1231 			'height' => $crop_details['height'],
  1285 			array(
  1232 			'width'  => $crop_details['width'],
  1286 				'height' => $crop_details['height'],
  1233 		) );
  1287 				'width'  => $crop_details['width'],
       
  1288 			)
       
  1289 		);
  1234 
  1290 
  1235 		$attachment_id = absint( $_POST['id'] );
  1291 		$attachment_id = absint( $_POST['id'] );
  1236 
  1292 
  1237 		$cropped = wp_crop_image(
  1293 		$cropped = wp_crop_image(
  1238 			$attachment_id,
  1294 			$attachment_id,
  1262 		}
  1318 		}
  1263 
  1319 
  1264 		$new_attachment_id = $this->insert_attachment( $object, $cropped );
  1320 		$new_attachment_id = $this->insert_attachment( $object, $cropped );
  1265 
  1321 
  1266 		$object['attachment_id'] = $new_attachment_id;
  1322 		$object['attachment_id'] = $new_attachment_id;
  1267 		$object['url']           = wp_get_attachment_url( $new_attachment_id );;
  1323 		$object['url']           = wp_get_attachment_url( $new_attachment_id );
  1268 		$object['width']         = $dimensions['dst_width'];
  1324 
  1269 		$object['height']        = $dimensions['dst_height'];
  1325 		$object['width']  = $dimensions['dst_width'];
       
  1326 		$object['height'] = $dimensions['dst_height'];
  1270 
  1327 
  1271 		wp_send_json_success( $object );
  1328 		wp_send_json_success( $object );
  1272 	}
  1329 	}
  1273 
  1330 
  1274 	/**
  1331 	/**
  1345 		if ( ! isset( $data['attachment_id'] ) ) {
  1402 		if ( ! isset( $data['attachment_id'] ) ) {
  1346 			return;
  1403 			return;
  1347 		}
  1404 		}
  1348 
  1405 
  1349 		$attachment_id = $data['attachment_id'];
  1406 		$attachment_id = $data['attachment_id'];
  1350 		$key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
  1407 		$key           = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
  1351 		update_post_meta( $attachment_id, $key, time() );
  1408 		update_post_meta( $attachment_id, $key, time() );
  1352 	}
  1409 	}
  1353 
  1410 
  1354 	/**
  1411 	/**
  1355 	 * Gets the details of default header images if defined.
  1412 	 * Gets the details of default header images if defined.
  1366 
  1423 
  1367 		if ( ! $default ) { // If not,
  1424 		if ( ! $default ) { // If not,
  1368 			return $this->default_headers; // easy peasy.
  1425 			return $this->default_headers; // easy peasy.
  1369 		}
  1426 		}
  1370 
  1427 
  1371 		$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  1428 		$default             = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  1372 		$already_has_default = false;
  1429 		$already_has_default = false;
  1373 
  1430 
  1374 		foreach ( $this->default_headers as $k => $h ) {
  1431 		foreach ( $this->default_headers as $k => $h ) {
  1375 			if ( $h['url'] === $default ) {
  1432 			if ( $h['url'] === $default ) {
  1376 				$already_has_default = true;
  1433 				$already_has_default = true;
  1381 		if ( $already_has_default ) {
  1438 		if ( $already_has_default ) {
  1382 			return $this->default_headers;
  1439 			return $this->default_headers;
  1383 		}
  1440 		}
  1384 
  1441 
  1385 		// If the one true image isn't included in the default set, prepend it.
  1442 		// If the one true image isn't included in the default set, prepend it.
  1386 		$header_images = array();
  1443 		$header_images            = array();
  1387 		$header_images['default'] = array(
  1444 		$header_images['default'] = array(
  1388 			'url'           => $default,
  1445 			'url'           => $default,
  1389 			'thumbnail_url' => $default,
  1446 			'thumbnail_url' => $default,
  1390 			'description'   => 'Default'
  1447 			'description'   => 'Default',
  1391 		);
  1448 		);
  1392 
  1449 
  1393 		// The rest of the set comes after.
  1450 		// The rest of the set comes after.
  1394 		return array_merge( $header_images, $this->default_headers );
  1451 		return array_merge( $header_images, $this->default_headers );
  1395 	}
  1452 	}
  1402 	 * @return array Uploaded header images.
  1459 	 * @return array Uploaded header images.
  1403 	 */
  1460 	 */
  1404 	public function get_uploaded_header_images() {
  1461 	public function get_uploaded_header_images() {
  1405 		$header_images = get_uploaded_header_images();
  1462 		$header_images = get_uploaded_header_images();
  1406 		$timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
  1463 		$timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
  1407 		$alt_text_key = '_wp_attachment_image_alt';
  1464 		$alt_text_key  = '_wp_attachment_image_alt';
  1408 
  1465 
  1409 		foreach ( $header_images as &$header_image ) {
  1466 		foreach ( $header_images as &$header_image ) {
  1410 			$header_meta = get_post_meta( $header_image['attachment_id'] );
  1467 			$header_meta               = get_post_meta( $header_image['attachment_id'] );
  1411 			$header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
  1468 			$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 ] : '';
  1469 			$header_image['alt_text']  = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
  1413 		}
  1470 		}
  1414 
  1471 
  1415 		return $header_images;
  1472 		return $header_images;
  1416 	}
  1473 	}
  1417 
  1474