WP_REST_Font_Faces_Controller::create_item()publicWP 6.5.0

Creates a font face for the parent font family.

Method of the class: WP_REST_Font_Faces_Controller{}

No Hooks.

Return

WP_REST_Response|WP_Error. Response object on success, or WP_Error object on failure.

Usage

$WP_REST_Font_Faces_Controller = new WP_REST_Font_Faces_Controller();
$WP_REST_Font_Faces_Controller->create_item( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 6.5.0 Introduced.

WP_REST_Font_Faces_Controller::create_item() code WP 6.6.2

public function create_item( $request ) {
	$font_family = $this->get_parent_font_family_post( $request['font_family_id'] );
	if ( is_wp_error( $font_family ) ) {
		return $font_family;
	}

	// Settings have already been decoded by ::sanitize_font_face_settings().
	$settings    = $request->get_param( 'font_face_settings' );
	$file_params = $request->get_file_params();

	// Check that the necessary font face properties are unique.
	$query = new WP_Query(
		array(
			'post_type'              => $this->post_type,
			'posts_per_page'         => 1,
			'title'                  => WP_Font_Utils::get_font_face_slug( $settings ),
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
		)
	);
	if ( ! empty( $query->posts ) ) {
		return new WP_Error(
			'rest_duplicate_font_face',
			__( 'A font face matching those settings already exists.' ),
			array( 'status' => 400 )
		);
	}

	// Move the uploaded font asset from the temp folder to the fonts directory.
	if ( ! function_exists( 'wp_handle_upload' ) ) {
		require_once ABSPATH . 'wp-admin/includes/file.php';
	}

	$srcs           = is_string( $settings['src'] ) ? array( $settings['src'] ) : $settings['src'];
	$processed_srcs = array();
	$font_file_meta = array();

	foreach ( $srcs as $src ) {
		// If src not a file reference, use it as is.
		if ( ! isset( $file_params[ $src ] ) ) {
			$processed_srcs[] = $src;
			continue;
		}

		$file      = $file_params[ $src ];
		$font_file = $this->handle_font_file_upload( $file );
		if ( is_wp_error( $font_file ) ) {
			return $font_file;
		}

		$processed_srcs[] = $font_file['url'];
		$font_file_meta[] = $this->relative_fonts_path( $font_file['file'] );
	}

	// Store the updated settings for prepare_item_for_database to use.
	$settings['src'] = count( $processed_srcs ) === 1 ? $processed_srcs[0] : $processed_srcs;
	$request->set_param( 'font_face_settings', $settings );

	// Ensure that $settings data is slashed, so values with quotes are escaped.
	// WP_REST_Posts_Controller::create_item uses wp_slash() on the post_content.
	$font_face_post = parent::create_item( $request );

	if ( is_wp_error( $font_face_post ) ) {
		return $font_face_post;
	}

	$font_face_id = $font_face_post->data['id'];

	foreach ( $font_file_meta as $font_file_path ) {
		add_post_meta( $font_face_id, '_wp_font_face_file', $font_file_path );
	}

	return $font_face_post;
}