WP_REST_Templates_Controller8b7c:f320:99b9:690f:4595:cd17:293a:c069prepare_item_for_database( WP_REST_Request $request ): stdClass  Prepares a single template for create or update.   Parameters   $request WP_REST_Request Required    Request object.      Return stdClass Changes to pass to wp_update_post.   Source  File: wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php. View all references  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' === $this->post_type && isset( $request['is_wp_suggestion'] ) ) {
		$changes->meta_input     = wp_parse_args(
			array(
				'is_wp_suggestion' => $request['is_wp_suggestion'],
			),
			$changes->meta_input = array()
		);
	}

	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;
}
   Related  Uses    Uses Description      _filter_block_template_part_area() wp-includes/block-template-utils.php   Checks whether the input ‘area’ is a supported value.     get_block_template() wp-includes/block-template-utils.php   Retrieves a single unified template object using its id.     WP_Them8b7c:f320:99b9:690f:4595:cd17:293a:c069get_stylesheet() wp-includes/class-wp-theme.php   Returns the directory name of the theme’s “stylesheet” files, inside the theme root.     wp_get_theme() wp-includes/theme.php   Gets a WP_Theme object for a theme.     __() wp-includes/l10n.php   Retrieves the translation of $text.     get_userdata() wp-includes/pluggable.php   Retrieves user info by user ID.     wp_parse_args() wp-includes/functions.php   Merges user defined arguments into defaults array.     get_current_user_id() wp-includes/user.php   Gets the current user’s ID.     WP_Error8b7c:f320:99b9:690f:4595:cd17:293a:c069__construct() wp-includes/class-wp-error.php   Initializes the error.   
   Used By    Used By Description      WP_REST_Templates_Controller8b7c:f320:99b9:690f:4595:cd17:293a:c069update_item() wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php   Updates a single template.     WP_REST_Templates_Controller8b7c:f320:99b9:690f:4595:cd17:293a:c069reate_item() wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php   Creates a single template.   
    Changelog    Version Description     5.8.0 Introduced.    

    
{'path': 'public/docs/wordpress/classes/wp_rest_templates_controller/prepare_item_for_database.html', 'source': 'DevDocs.IO'}