WP_REST_Templates_Controller::prepare_item_for_database()protectedWP 5.8.0

Prepares a single template for create or update.

{} It's a method of the class: WP_REST_Templates_Controller{}

No Hooks.

Return

stdClass. Changes to pass to wp_update_post.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_item_for_database( $request );
$request(WP_REST_Request) (required)
Request object.

Changelog

Since 5.8.0 Introduced.

Code of WP_REST_Templates_Controller::prepare_item_for_database() WP 6.0

protected function prepare_item_for_database( $request ) {
	$template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null;
	$changes  = new stdClass();
	if ( null === $template ) {
		$changes->post_type   = $this->post_type;
		$changes->post_status = 'publish';
		$changes->tax_input   = array(
			'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : wp_get_theme()->get_stylesheet(),
		);
	} elseif ( 'custom' !== $template->source ) {
		$changes->post_name   = $template->slug;
		$changes->post_type   = $this->post_type;
		$changes->post_status = 'publish';
		$changes->tax_input   = array(
			'wp_theme' => $template->theme,
		);
		$changes->meta_input  = array(
			'origin' => $template->source,
		);
	} else {
		$changes->post_name   = $template->slug;
		$changes->ID          = $template->wp_id;
		$changes->post_status = 'publish';
	}
	if ( isset( $request['content'] ) ) {
		if ( is_string( $request['content'] ) ) {
			$changes->post_content = $request['content'];
		} elseif ( isset( $request['content']['raw'] ) ) {
			$changes->post_content = $request['content']['raw'];
		}
	} elseif ( null !== $template && 'custom' !== $template->source ) {
		$changes->post_content = $template->content;
	}
	if ( isset( $request['title'] ) ) {
		if ( is_string( $request['title'] ) ) {
			$changes->post_title = $request['title'];
		} elseif ( ! empty( $request['title']['raw'] ) ) {
			$changes->post_title = $request['title']['raw'];
		}
	} elseif ( null !== $template && 'custom' !== $template->source ) {
		$changes->post_title = $template->title;
	}
	if ( isset( $request['description'] ) ) {
		$changes->post_excerpt = $request['description'];
	} elseif ( null !== $template && 'custom' !== $template->source ) {
		$changes->post_excerpt = $template->description;
	}

	if ( 'wp_template_part' === $this->post_type ) {
		if ( isset( $request['area'] ) ) {
			$changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $request['area'] );
		} elseif ( null !== $template && 'custom' !== $template->source && $template->area ) {
			$changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area );
		} elseif ( ! $template->area ) {
			$changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
		}
	}

	if ( ! empty( $request['author'] ) ) {
		$post_author = (int) $request['author'];

		if ( get_current_user_id() !== $post_author ) {
			$user_obj = get_userdata( $post_author );

			if ( ! $user_obj ) {
				return new WP_Error(
					'rest_invalid_author',
					__( 'Invalid author ID.' ),
					array( 'status' => 400 )
				);
			}
		}

		$changes->post_author = $post_author;
	}

	return $changes;
}