protected function upload_from_data( $data, $headers ) {
		if ( empty( $data ) ) {
			return new WP_Error( 'rest_upload_no_data', __( 'No data supplied.' ), array( 'status' => 400 ) );
		}

		if ( empty( $headers['content_type'] ) ) {
			return new WP_Error( 'rest_upload_no_content_type', __( 'No Content-Type supplied.' ), array( 'status' => 400 ) );
		}

		if ( empty( $headers['content_disposition'] ) ) {
			return new WP_Error( 'rest_upload_no_content_disposition', __( 'No Content-Disposition supplied.' ), array( 'status' => 400 ) );
		}

		$filename = self::get_filename_from_disposition( $headers['content_disposition'] );

		if ( empty( $filename ) ) {
			return new WP_Error( 'rest_upload_invalid_disposition', __( 'Invalid Content-Disposition supplied. Content-Disposition needs to be formatted as `attachment; filename="image.png"` or similar.' ), array( 'status' => 400 ) );
		}

		if ( ! empty( $headers['content_md5'] ) ) {
			$content_md5 = array_shift( $headers['content_md5'] );
			$expected = trim( $content_md5 );
			$actual   = md5( $data );

			if ( $expected !== $actual ) {
				return new WP_Error( 'rest_upload_hash_mismatch', __( 'Content hash did not match expected.' ), array( 'status' => 412 ) );
			}
		}

		// Get the content-type.
		$type = array_shift( $headers['content_type'] );

		/** Include admin functions to get access to wp_tempnam() and wp_handle_sideload() */
		require_once ABSPATH . 'wp-admin/includes/admin.php';

		// Save the file.
		$tmpfname = wp_tempnam( $filename );

		$fp = fopen( $tmpfname, 'w+' );

		if ( ! $fp ) {
			return new WP_Error( 'rest_upload_file_error', __( 'Could not open file handle.' ), array( 'status' => 500 ) );
		}

		fwrite( $fp, $data );
		fclose( $fp );

		// Now, sideload it in.
		$file_data = array(
			'error'    => null,
			'tmp_name' => $tmpfname,
			'name'     => $filename,
			'type'     => $type,
		);
		$overrides = array(
			'test_form' => false,
		);
		$sideloaded = wp_handle_sideload( $file_data, $overrides );

		if ( isset( $sideloaded['error'] ) ) {
			// @codingStandardsIgnoreStart
			@unlink( $tmpfname );
			// @codingStandardsIgnoreEnd
			return new WP_Error( 'rest_upload_sideload_error', $sideloaded['error'], array( 'status' => 500 ) );
		}

		return $sideloaded;
	}
protected function upload_from_data( $data, $headers ) {
		if ( empty( $data ) ) {
			return new WP_Error( 'rest_upload_no_data', __( 'No data supplied.' ), array( 'status' => 400 ) );
		}

		if ( empty( $headers['content_type'] ) ) {
			return new WP_Error( 'rest_upload_no_content_type', __( 'No Content-Type supplied.' ), array( 'status' => 400 ) );
		}

		if ( empty( $headers['content_disposition'] ) ) {
			return new WP_Error( 'rest_upload_no_content_disposition', __( 'No Content-Disposition supplied.' ), array( 'status' => 400 ) );
		}

		$filename = self::get_filename_from_disposition( $headers['content_disposition'] );

		if ( empty( $filename ) ) {
			return new WP_Error( 'rest_upload_invalid_disposition', __( 'Invalid Content-Disposition supplied. Content-Disposition needs to be formatted as `attachment; filename="image.png"` or similar.' ), array( 'status' => 400 ) );
		}

		if ( ! empty( $headers['content_md5'] ) ) {
			$content_md5 = array_shift( $headers['content_md5'] );
			$expected = trim( $content_md5 );
			$actual   = md5( $data );

			if ( $expected !== $actual ) {
				return new WP_Error( 'rest_upload_hash_mismatch', __( 'Content hash did not match expected.' ), array( 'status' => 412 ) );
			}
		}

		// Get the content-type.
		$type = array_shift( $headers['content_type'] );

		/** Include admin functions to get access to wp_tempnam() and wp_handle_sideload() */
		require_once ABSPATH . 'wp-admin/includes/admin.php';

		// Save the file.
		$tmpfname = wp_tempnam( $filename );

		$fp = fopen( $tmpfname, 'w+' );

		if ( ! $fp ) {
			return new WP_Error( 'rest_upload_file_error', __( 'Could not open file handle.' ), array( 'status' => 500 ) );
		}

		fwrite( $fp, $data );
		fclose( $fp );

		// Now, sideload it in.
		$file_data = array(
			'error'    => null,
			'tmp_name' => $tmpfname,
			'name'     => $filename,
			'type'     => $type,
		);
		$overrides = array(
			'test_form' => false,
		);
		$sideloaded = wp_handle_sideload( $file_data, $overrides );

		if ( isset( $sideloaded['error'] ) ) {
			// @codingStandardsIgnoreStart
			@unlink( $tmpfname );
			// @codingStandardsIgnoreEnd
			return new WP_Error( 'rest_upload_sideload_error', $sideloaded['error'], array( 'status' => 500 ) );
		}

		return $sideloaded;
	}
protected function upload_from_data ( $ data , $ headers ) { if ( empty ( $ data ) ) { return new WP_Error ( 'rest_upload_no_data' , __ ( 'No data supplied.' ) , array ( 'status' => 400 ) ) ; } if ( empty ( $ headers [ 'content_type' ] ) ) { return new WP_Error ( 'rest_upload_no_content_type' , __ ( 'No Content-Type supplied.' ) , array ( 'status' => 400 ) ) ; } if ( empty ( $ headers [ 'content_disposition' ] ) ) { return new WP_Error ( 'rest_upload_no_content_disposition' , __ ( 'No Content-Disposition supplied.' ) , array ( 'status' => 400 ) ) ; } $ filename = self :: get_filename_from_disposition ( $ headers [ 'content_disposition' ] ) ; if ( empty ( $ filename ) ) { return new WP_Error ( 'rest_upload_invalid_disposition' , __ ( 'Invalid Content-Disposition supplied. Content-Disposition needs to be formatted as `attachment; filename="image.png"` or similar.' ) , array ( 'status' => 400 ) ) ; } if ( ! empty ( $ headers [ 'content_md5' ] ) ) { $ content_md5 = array_shift ( $ headers [ 'content_md5' ] ) ; $ expected = trim ( $ content_md5 ) ; $ actual = md5 ( $ data ) ; if ( $ expected !== $ actual ) { return new WP_Error ( 'rest_upload_hash_mismatch' , __ ( 'Content hash did not match expected.' ) , array ( 'status' => 412 ) ) ; } } // Get the content-type. $ type = array_shift ( $ headers [ 'content_type' ] ) ; /** Include admin functions to get access to wp_tempnam() and wp_handle_sideload() */ require_once ABSPATH . 'wp-admin/includes/admin.php' ; // Save the file. $ tmpfname = wp_tempnam ( $ filename ) ; $ fp = fopen ( $ tmpfname , 'w+' ) ; if ( ! $ fp ) { return new WP_Error ( 'rest_upload_file_error' , __ ( 'Could not open file handle.' ) , array ( 'status' => 500 ) ) ; } fwrite ( $ fp , $ data ) ; fclose ( $ fp ) ; // Now, sideload it in. $ file_data = array ( 'error' => null , 'tmp_name' => $ tmpfname , 'name' => $ filename , 'type' => $ type , ) ; $ overrides = array ( 'test_form' => false , ) ; $ sideloaded = wp_handle_sideload ( $ file_data , $ overrides ) ; if ( isset ( $ sideloaded [ 'error' ] ) ) { // @codingStandardsIgnoreStart @ unlink ( $ tmpfname ) ; // @codingStandardsIgnoreEnd return new WP_Error ( 'rest_upload_sideload_error' , $ sideloaded [ 'error' ] , array ( 'status' => 500 ) ) ; } return $ sideloaded ; }
Handle an upload via raw POST data.

@param array $data    Supplied file data.
@param array $headers HTTP headers from the request.
@return array|WP_Error Data from {@see wp_handle_sideload()}.
Handle an upload via raw POST data .