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
public function create_item( $request ) { $prepared_item = $this->prepare_item_for_database( $request ); if ( is_wp_error( $prepared_item ) ) { return $prepared_item; } $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; } $schema = $this->get_item_schema(); /** * 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; } /** * TODO: understand how to treat possible conflicting properties => instructors are registered as additional rest field by llms_blocks */ // $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. * * @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_after_insert_{$this->post_type}", $object, $request, $schema, true ); $response = $this->prepare_item_for_response( $object, $request ); $response->set_status( 201 ); $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $object_id ) ) ); return $response; }
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. |