WP_REST_Posts_Controller::prepare_item_for_database( WP_REST_Request $request ): stdClass|WP_Error

In this article

Prepares a single post for create or update.

Parameters

$requestWP_REST_Requestrequired
Request object.

Return

stdClass|WP_Error Post object or WP_Error.

Source

protected function prepare_item_for_database( $request ) {
	$prepared_post  = new stdClass();
	$current_status = '';

	// Post ID.
	if ( isset( $request['id'] ) ) {
		$existing_post = $this->get_post( $request['id'] );
		if ( is_wp_error( $existing_post ) ) {
			return $existing_post;
		}

		$prepared_post->ID = $existing_post->ID;
		$current_status    = $existing_post->post_status;
	}

	$schema = $this->get_item_schema();

	// Post title.
	if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
		if ( is_string( $request['title'] ) ) {
			$prepared_post->post_title = $request['title'];
		} elseif ( ! empty( $request['title']['raw'] ) ) {
			$prepared_post->post_title = $request['title']['raw'];
		}
	}

	// Post content.
	if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {
		if ( is_string( $request['content'] ) ) {
			$prepared_post->post_content = $request['content'];
		} elseif ( isset( $request['content']['raw'] ) ) {
			$prepared_post->post_content = $request['content']['raw'];
		}
	}

	// Post excerpt.
	if ( ! empty( $schema['properties']['excerpt'] ) && isset( $request['excerpt'] ) ) {
		if ( is_string( $request['excerpt'] ) ) {
			$prepared_post->post_excerpt = $request['excerpt'];
		} elseif ( isset( $request['excerpt']['raw'] ) ) {
			$prepared_post->post_excerpt = $request['excerpt']['raw'];
		}
	}

	// Post type.
	if ( empty( $request['id'] ) ) {
		// Creating new post, use default type for the controller.
		$prepared_post->post_type = $this->post_type;
	} else {
		// Updating a post, use previous type.
		$prepared_post->post_type = get_post_type( $request['id'] );
	}

	$post_type = get_post_type_object( $prepared_post->post_type );

	// Post status.
	if (
		! empty( $schema['properties']['status'] ) &&
		isset( $request['status'] ) &&
		( ! $current_status || $current_status !== $request['status'] )
	) {
		$status = $this->handle_status_param( $request['status'], $post_type );

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

		$prepared_post->post_status = $status;
	}

	// Post date.
	if ( ! empty( $schema['properties']['date'] ) && ! empty( $request['date'] ) ) {
		$current_date = isset( $prepared_post->ID ) ? get_post( $prepared_post->ID )->post_date : false;
		$date_data    = rest_get_date_with_gmt( $request['date'] );

		if ( ! empty( $date_data ) && $current_date !== $date_data[0] ) {
			list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
			$prepared_post->edit_date                                        = true;
		}
	} elseif ( ! empty( $schema['properties']['date_gmt'] ) && ! empty( $request['date_gmt'] ) ) {
		$current_date = isset( $prepared_post->ID ) ? get_post( $prepared_post->ID )->post_date_gmt : false;
		$date_data    = rest_get_date_with_gmt( $request['date_gmt'], true );

		if ( ! empty( $date_data ) && $current_date !== $date_data[1] ) {
			list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
			$prepared_post->edit_date                                        = true;
		}
	}

	/*
	 * Sending a null date or date_gmt value resets date and date_gmt to their
	 * default values (`0000-00-00 00:00:00`).
	 */
	if (
		( ! empty( $schema['properties']['date_gmt'] ) && $request->has_param( 'date_gmt' ) && null === $request['date_gmt'] ) ||
		( ! empty( $schema['properties']['date'] ) && $request->has_param( 'date' ) && null === $request['date'] )
	) {
		$prepared_post->post_date_gmt = null;
		$prepared_post->post_date     = null;
	}

	// Post slug.
	if ( ! empty( $schema['properties']['slug'] ) && isset( $request['slug'] ) ) {
		$prepared_post->post_name = $request['slug'];
	}

	// Author.
	if ( ! empty( $schema['properties']['author'] ) && ! 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 )
				);
			}
		}

		$prepared_post->post_author = $post_author;
	}

	// Post password.
	if ( ! empty( $schema['properties']['password'] ) && isset( $request['password'] ) ) {
		$prepared_post->post_password = $request['password'];

		if ( '' !== $request['password'] ) {
			if ( ! empty( $schema['properties']['sticky'] ) && ! empty( $request['sticky'] ) ) {
				return new WP_Error(
					'rest_invalid_field',
					__( 'A post can not be sticky and have a password.' ),
					array( 'status' => 400 )
				);
			}

			if ( ! empty( $prepared_post->ID ) && is_sticky( $prepared_post->ID ) ) {
				return new WP_Error(
					'rest_invalid_field',
					__( 'A sticky post can not be password protected.' ),
					array( 'status' => 400 )
				);
			}
		}
	}

	if ( ! empty( $schema['properties']['sticky'] ) && ! empty( $request['sticky'] ) ) {
		if ( ! empty( $prepared_post->ID ) && post_password_required( $prepared_post->ID ) ) {
			return new WP_Error(
				'rest_invalid_field',
				__( 'A password protected post can not be set to sticky.' ),
				array( 'status' => 400 )
			);
		}
	}

	// Parent.
	if ( ! empty( $schema['properties']['parent'] ) && isset( $request['parent'] ) ) {
		if ( 0 === (int) $request['parent'] ) {
			$prepared_post->post_parent = 0;
		} else {
			$parent = get_post( (int) $request['parent'] );

			if ( empty( $parent ) ) {
				return new WP_Error(
					'rest_post_invalid_id',
					__( 'Invalid post parent ID.' ),
					array( 'status' => 400 )
				);
			}

			$prepared_post->post_parent = (int) $parent->ID;
		}
	}

	// Menu order.
	if ( ! empty( $schema['properties']['menu_order'] ) && isset( $request['menu_order'] ) ) {
		$prepared_post->menu_order = (int) $request['menu_order'];
	}

	// Comment status.
	if ( ! empty( $schema['properties']['comment_status'] ) && ! empty( $request['comment_status'] ) ) {
		$prepared_post->comment_status = $request['comment_status'];
	}

	// Ping status.
	if ( ! empty( $schema['properties']['ping_status'] ) && ! empty( $request['ping_status'] ) ) {
		$prepared_post->ping_status = $request['ping_status'];
	}

	if ( ! empty( $schema['properties']['template'] ) ) {
		// Force template to null so that it can be handled exclusively by the REST controller.
		$prepared_post->page_template = null;
	}

	/**
	 * Filters a post before it is inserted via the REST API.
	 *
	 * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
	 *
	 * Possible hook names include:
	 *
	 *  - `rest_pre_insert_post`
	 *  - `rest_pre_insert_page`
	 *  - `rest_pre_insert_attachment`
	 *
	 * @since 4.7.0
	 *
	 * @param stdClass        $prepared_post An object representing a single post prepared
	 *                                       for inserting or updating the database.
	 * @param WP_REST_Request $request       Request object.
	 */
	return apply_filters( "rest_pre_insert_{$this->post_type}", $prepared_post, $request );
}

Hooks

apply_filters( “rest_pre_insert_{$this->post_type}”, stdClass $prepared_post, WP_REST_Request $request )

Filters a post before it is inserted via the REST API.

Changelog

VersionDescription
4.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.