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().


Top ↑

Parameters Parameters

$request

(WP_REST_Request) (Required) Full details about the request.


Top ↑

Return Return

(WP_REST_Response|WP_Error) Response object on success, or WP_Error object on failure.


Top ↑

Source Source

File: libraries/lifterlms-rest/includes/abstracts/class-llms-rest-posts-controller.php

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
* 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.


Top ↑

Changelog 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.

Top ↑

User Contributed Notes User Contributed Notes

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