LLMS_REST_Memberships_Controller::update_additional_object_fields( LLMS_Membership $membership, WP_REST_Request $request, array $schema, array $prepared_item, bool $creating = true )
Updates an existing single LLMS_Membership in the database.
Description Description
This method should be used for membership properties that require the membership id in order to be saved in the database.
Parameters Parameters
- $membership
-
(LLMS_Membership) (Required) LLMS_Membership instance.
- $request
-
(WP_REST_Request) (Required) Full details about the request.
- $schema
-
(array) (Required) The item schema.
- $prepared_item
-
(array) (Required) Array.
- $creating
-
(bool) (Optional) Whether we're in creation or update phase. Default true (create).
Default value: true
Return Return
(bool|WP_Error) True on success or false if nothing to update, WP_Error object if something went wrong during the update.
Source Source
File: libraries/lifterlms-rest/includes/server/class-llms-rest-memberships-controller.php
protected function update_additional_object_fields( $membership, $request, $schema, $prepared_item, $creating = true ) { $error = new WP_Error(); // Auto enroll. if ( ! empty( $schema['properties']['auto_enroll'] ) && isset( $request['auto_enroll'] ) ) { $membership->add_auto_enroll_courses( $request['auto_enroll'], true ); } // Catalog visibility. if ( ! empty( $schema['properties']['catalog_visibility'] ) && isset( $request['catalog_visibility'] ) ) { $membership->get_product()->set_catalog_visibility( $request['catalog_visibility'] ); } // Instructors. if ( ! empty( $schema['properties']['instructors'] ) ) { $instructors = array(); if ( isset( $request['instructors'] ) ) { foreach ( $request['instructors'] as $instructor_id ) { $user_data = get_userdata( $instructor_id ); if ( ! empty( $user_data ) ) { $instructors[] = array( 'id' => $instructor_id, 'name' => $user_data->display_name, ); } } } // When creating, always make sure the instructors are set. // Note: `$membership->set_instructor( $instructors )` when `$instructors` is empty // will set the membership's author as membership's instructor. if ( $membership || ( ! $creating && isset( $request['instructors'] ) ) ) { $membership->set_instructors( $instructors ); } } $to_set = array(); /** * The following properties have a default value that contains a placeholder, e.g. `{{membership_id}}`, * that can be "expanded" only after the membership has been created. */ // Restriction message. if ( ! empty( $schema['properties']['restriction_message'] ) ) { if ( isset( $request['restriction_message'] ) ) { if ( is_string( $request['restriction_message'] ) ) { $to_set['restriction_notice'] = $request['restriction_message']; } elseif ( isset( $request['restriction_message']['raw'] ) ) { $to_set['restriction_notice'] = $request['restriction_message']['raw']; } } elseif ( $creating ) { $to_set['restriction_notice'] = $schema['properties']['restriction_message']['properties']['raw']['default']; } } // Needed until the following will be implemented: https://github.com/gocodebox/lifterlms/issues/908. // On creation, since the restriction message has a non empty default, the restriction_add_notice, // will be set to 'yes'. $to_set['restriction_add_notice'] = empty( $to_set['restriction_notice'] ) ? 'no' : 'yes'; // Are we creating a membership? TODO what about updating when message is empty? // If so, replace the placeholder with the actual membership id. if ( $creating ) { $_to_expand_props = array( 'restriction_notice', ); $membership_id = $membership->get( 'id' ); foreach ( $_to_expand_props as $prop ) { if ( ! empty( $to_set[ $prop ] ) ) { $to_set[ $prop ] = str_replace( '{{membership_id}}', $membership_id, $to_set[ $prop ] ); } } } // Set bulk. if ( ! empty( $to_set ) ) { $update = $membership->set_bulk( $to_set, true, true ); if ( is_wp_error( $update ) ) { $error = $update; } } if ( $error->errors ) { return $error; } return ! empty( $to_set ); }
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
1.0.0-beta.9 | |
1.0.0-beta.25 | Introduced. |