WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Платформа для конвертации и монетизации трафика
функция не описана

WP_REST_Posts_Controller::prepare_item_for_database() protected WP 4.7.0

Prepares a single post for create or update.

{} Это метод класса: WP_REST_Posts_Controller{}

Хуки из метода
Возвращает

stdClass/WP_Error. Post object or WP_Error.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->prepare_item_for_database( $request );
$request(WP_REST_Request) (обязательный)
Request object.

Список изменений

С версии 4.7.0 Введена.

Код WP_REST_Posts_Controller::prepare_item_for_database() WP 5.5.3

<?php
protected function prepare_item_for_database( $request ) {
	$prepared_post = new stdClass();

	// 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;
	}

	$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'] ) ) {
		$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.
	 *
	 * @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 );

}