Custom_Background::handle_upload()

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

Custom_Background::handle_upload()

Handle an Image upload for the background image.

源文件

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

	public function handle_upload() {
		if ( empty( $_FILES ) ) {
			return;
		}

		check_admin_referer( 'custom-background-upload', '_gcnonce-custom-background-upload' );

		$overrides = array( 'test_form' => false );

		$uploaded_file = $_FILES['import'];
		$gc_filetype   = gc_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
		if ( ! gc_match_mime_types( 'image', $gc_filetype['type'] ) ) {
			gc_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
		}

		$file = gc_handle_upload( $uploaded_file, $overrides );

		if ( isset( $file['error'] ) ) {
			gc_die( $file['error'] );
		}

		$url      = $file['url'];
		$type     = $file['type'];
		$file     = $file['file'];
		$filename = gc_basename( $file );

		// Construct the object array.
		$object = array(
			'post_title'     => $filename,
			'post_content'   => $url,
			'post_mime_type' => $type,
			'guid'           => $url,
			'context'        => 'custom-background',
		);

		// Save the data.
		$id = gc_insert_attachment( $object, $file );

		// Add the metadata.
		gc_update_attachment_metadata( $id, gc_generate_attachment_metadata( $id, $file ) );
		update_post_meta( $id, '_gc_attachment_is_custom_background', get_option( 'stylesheet' ) );

		set_theme_mod( 'background_image', esc_url_raw( $url ) );

		$thumbnail = gc_get_attachment_image_src( $id, 'thumbnail' );
		set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );

		/** This action is documented in gc-admin/includes/class-custom-image-header.php */
		do_action( 'gc_create_file_in_uploads', $file, $id ); // For replication.
		$this->updated = true;
	}