Custom_Image_Header::step_3()

最后更新于:2021-11-25 19:49:59

Custom_Image_Header::step_3()

Display third step of custom header image page.

源文件

文件: gc-admin/includes/class-custom-image-header.php

	public function step_3() {
		check_admin_referer( 'custom-header-crop-image' );

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

		if ( ! empty( $_POST['skip-cropping'] )
			&& ! current_theme_supports( 'custom-header', 'flex-height' )
			&& ! current_theme_supports( 'custom-header', 'flex-width' )
		) {
			gc_die(
				'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
				'<p>' . __( 'The current theme does not support a flexible sized header image.' ) . '</p>',
				403
			);
		}

		if ( $_POST['oitar'] > 1 ) {
			$_POST['x1']     = $_POST['x1'] * $_POST['oitar'];
			$_POST['y1']     = $_POST['y1'] * $_POST['oitar'];
			$_POST['width']  = $_POST['width'] * $_POST['oitar'];
			$_POST['height'] = $_POST['height'] * $_POST['oitar'];
		}

		$attachment_id = absint( $_POST['attachment_id'] );
		$original      = get_attached_file( $attachment_id );

		$dimensions = $this->get_header_dimensions(
			array(
				'height' => $_POST['height'],
				'width'  => $_POST['width'],
			)
		);
		$height     = $dimensions['dst_height'];
		$width      = $dimensions['dst_width'];

		if ( empty( $_POST['skip-cropping'] ) ) {
			$cropped = gc_crop_image(
				$attachment_id,
				(int) $_POST['x1'],
				(int) $_POST['y1'],
				(int) $_POST['width'],
				(int) $_POST['height'],
				$width,
				$height
			);
		} elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
			$cropped = _copy_image_file( $attachment_id );
		} else {
			$cropped = get_attached_file( $attachment_id );
		}

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

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

		$object = $this->create_attachment_object( $cropped, $attachment_id );

		if ( ! empty( $_POST['create-new-attachment'] ) ) {
			unset( $object['ID'] );
		}

		// Update the attachment.
		$attachment_id = $this->insert_attachment( $object, $cropped );

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

		// Cleanup.
		$medium = str_replace( gc_basename( $original ), 'midsize-' . gc_basename( $original ), $original );
		if ( file_exists( $medium ) ) {
			gc_delete_file( $medium );
		}

		if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
			gc_delete_file( $original );
		}

		return $this->finished();
	}