LLMS_REST_Posts_Controller::create_item( WP_REST_Request $request )
Creates a single LLMS post.
Description Description
Extending classes can add additional object fields by overriding the method update_additional_object_fields().
Parameters Parameters
- $request
-
(WP_REST_Request) (Required) Full details about the request.
Return Return
(WP_REST_Response|WP_Error) Response object on success, or WP_Error object on failure.
Source Source
File: libraries/lifterlms-rest/includes/abstracts/class-llms-rest-posts-controller.php
* Creates a single LLMS post.
*
* Extending classes can add additional object fields by overriding the method `update_additional_object_fields()`.
*
* @since 1.0.0-beta.1
* @since 1.0.0-beta.7 Added `"llms_rest_insert_{$this->post_type}"` and `"llms_rest_insert_{$this->post_type}"` action hooks:
* fired after inserting/updating an llms post into the database.
* @since 1.0.0-beta.25 Allow updating meta with the same value as the stored one.
* @since 1.0.0-beta.27 Handle custom meta registered via `register_meta()` and custom rest fields registered via `register_rest_field()`.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function create_item( $request ) {
$schema = $this->get_item_schema();
$prepared_item = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_item ) ) {
return $prepared_item;
}
$prepared_item = array_diff_key( $prepared_item, $this->get_additional_fields() );
$object = $this->create_llms_post( $prepared_item );
if ( is_wp_error( $object ) ) {
if ( 'db_insert_error' === $object->get_error_code() ) {
$object->add_data( array( 'status' => 500 ) );
} else {
$object->add_data( array( 'status' => 400 ) );
}
return $object;
}
/**
* Fires after a single llms post is created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* @since 1.0.0-beta.7
*
* @param LLMS_Post $object Inserted or updated llms object.
* @param WP_REST_Request $request Request object.
* @param array $schema The item schema.
* @param bool $creating True when creating a post, false when updating.
*/
do_action( "llms_rest_insert_{$this->post_type}", $object, $request, $schema, true );
// Set all the other properties.
// TODO: maybe we want to filter the post properties that have already been inserted before.
$set_bulk_result = $object->set_bulk( $prepared_item, true, true );
if ( is_wp_error( $set_bulk_result ) ) {
if ( 'db_update_error' === $set_bulk_result->get_error_code() ) {
$set_bulk_result->add_data( array( 'status' => 500 ) );
} else {
$set_bulk_result->add_data( array( 'status' => 400 ) );
}
return $set_bulk_result;
}
$object_id = $object->get( 'id' );
$additional_fields = $this->update_additional_object_fields( $object, $request, $schema, $prepared_item );
if ( is_wp_error( $additional_fields ) ) {
return $additional_fields;
}
if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
$this->handle_featured_media( $request['featured_media'], $object_id );
}
$terms_update = $this->handle_terms( $object_id, $request );
if ( is_wp_error( $terms_update ) ) {
return $terms_update;
}
$meta_update = $this->update_meta( $object, $request, $schema );
if ( is_wp_error( $meta_update ) ) {
return $meta_update;
}
// Fields registered via `register_rest_field()`.
$fields_update = $this->update_additional_fields_for_object( $object, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$request->set_param( 'context', 'edit' );
/**
* Fires after a single llms post is completely created or updated via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 1.0.0-beta.7 | Added "llms_rest_insert_{$this->post_type}" and "llms_rest_insert_{$this->post_type}" action hooks: fired after inserting/uodateing an llms post into the database. |
| 1.0.0-beta.25 | Allow updating meta with the same value as the stored one. |
| 1.0.0-beta.1 | Introduced. |