Custom_Image_Header::step_2()publicWP 2.1.0

Displays second step of custom header image page.

Method of the class: Custom_Image_Header{}

Hooks from the method

Return

null. Nothing (null).

Usage

$Custom_Image_Header = new Custom_Image_Header();
$Custom_Image_Header->step_2();

Changelog

Since 2.1.0 Introduced.

Custom_Image_Header::step_2() code WP 6.5.2

<?php
public function step_2() {
	check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' );

	if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
		wp_die(
			'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
			'<p>' . __( 'The active theme does not support uploading a custom header image.' ) . '</p>',
			403
		);
	}

	if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
		$attachment_id = absint( $_GET['file'] );
		$file          = get_attached_file( $attachment_id, true );
		$url           = wp_get_attachment_image_src( $attachment_id, 'full' );
		$url           = $url[0];
	} elseif ( isset( $_POST ) ) {
		$data          = $this->step_2_manage_upload();
		$attachment_id = $data['attachment_id'];
		$file          = $data['file'];
		$url           = $data['url'];
	}

	if ( file_exists( $file ) ) {
		list( $width, $height, $type, $attr ) = wp_getimagesize( $file );
	} else {
		$data   = wp_get_attachment_metadata( $attachment_id );
		$height = isset( $data['height'] ) ? (int) $data['height'] : 0;
		$width  = isset( $data['width'] ) ? (int) $data['width'] : 0;
		unset( $data );
	}

	$max_width = 0;

	// For flex, limit size of image displayed to 1500px unless theme says otherwise.
	if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
		$max_width = 1500;
	}

	if ( current_theme_supports( 'custom-header', 'max-width' ) ) {
		$max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
	}

	$max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );

	// If flexible height isn't supported and the image is the exact right size.
	if ( ! current_theme_supports( 'custom-header', 'flex-height' )
		&& ! current_theme_supports( 'custom-header', 'flex-width' )
		&& (int) get_theme_support( 'custom-header', 'width' ) === $width
		&& (int) get_theme_support( 'custom-header', 'height' ) === $height
	) {
		// Add the metadata.
		if ( file_exists( $file ) ) {
			wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
		}

		$this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );

		/**
		 * Filters the attachment file path after the custom header or background image is set.
		 *
		 * Used for file replication.
		 *
		 * @since 2.1.0
		 *
		 * @param string $file          Path to the file.
		 * @param int    $attachment_id Attachment ID.
		 */
		$file = apply_filters( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication.

		return $this->finished();
	} elseif ( $width > $max_width ) {
		$oitar = $width / $max_width;

		$image = wp_crop_image(
			$attachment_id,
			0,
			0,
			$width,
			$height,
			$max_width,
			$height / $oitar,
			false,
			str_replace( wp_basename( $file ), 'midsize-' . wp_basename( $file ), $file )
		);

		if ( ! $image || is_wp_error( $image ) ) {
			wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
		}

		/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
		$image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication.

		$url    = str_replace( wp_basename( $url ), wp_basename( $image ), $url );
		$width  = $width / $oitar;
		$height = $height / $oitar;
	} else {
		$oitar = 1;
	}
	?>

<div class="wrap">
<h1><?php _e( 'Crop Header Image' ); ?></h1>

<form method="post" action="<?php echo esc_url( add_query_arg( 'step', 3 ) ); ?>">
<p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.' ); ?></p>
<p class="hide-if-js"><strong><?php _e( 'You need JavaScript to choose a part of the image.' ); ?></strong></p>

<div id="crop_image" style="position: relative">
	<img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo esc_attr( $width ); ?>" height="<?php echo esc_attr( $height ); ?>" alt="" />
</div>

<input type="hidden" name="x1" id="x1" value="0" />
<input type="hidden" name="y1" id="y1" value="0" />
<input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>" />
<input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>" />
<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
<input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
	<?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
<input type="hidden" name="create-new-attachment" value="true" />
<?php } ?>
	<?php wp_nonce_field( 'custom-header-crop-image' ); ?>

<p class="submit">
	<?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
	<?php
	if ( isset( $oitar ) && 1 === $oitar
		&& ( current_theme_supports( 'custom-header', 'flex-height' )
			|| current_theme_supports( 'custom-header', 'flex-width' ) )
	) {
		submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false );
	}
	?>
</p>
</form>
</div>
	<?php
}