Custom_Image_Header::ajax_header_crop()

最后更新于:2021-11-25 19:42:00

Custom_Image_Header::ajax_header_crop()

Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. 响应s JSON-encoded object details.

源文件

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

	public function ajax_header_crop() {
		check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' );

		if ( ! current_user_can( 'edit_theme_options' ) ) {
			gc_send_json_error();
		}

		if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
			gc_send_json_error();
		}

		$crop_details = $_POST['cropDetails'];

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

		$attachment_id = absint( $_POST['id'] );

		$cropped = gc_crop_image(
			$attachment_id,
			(int) $crop_details['x1'],
			(int) $crop_details['y1'],
			(int) $crop_details['width'],
			(int) $crop_details['height'],
			(int) $dimensions['dst_width'],
			(int) $dimensions['dst_height']
		);

		if ( ! $cropped || is_gc_error( $cropped ) ) {
			gc_send_json_error( array( 'message' => __( 'Image could not be processed. Please go back and try again.' ) ) );
		}

		/** 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 );

		$previous = $this->get_previous_crop( $object );

		if ( $previous ) {
			$object['ID'] = $previous;
		} else {
			unset( $object['ID'] );
		}

		$new_attachment_id = $this->insert_attachment( $object, $cropped );

		$object['attachment_id'] = $new_attachment_id;
		$object['url']           = gc_get_attachment_url( $new_attachment_id );

		$object['width']  = $dimensions['dst_width'];
		$object['height'] = $dimensions['dst_height'];

		gc_send_json_success( $object );
	}