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
'permission_callback' => array( $this->enrollments_controller, 'get_items_permissions_check' ),
'args' => $this->enrollments_controller->get_collection_params(),
),
'schema' => array( $this->enrollments_controller, 'get_public_item_schema' ),
)
);
}
/**
* Updates an existing single LLMS_Membership in the database.
*
* This method should be used for membership properties that require the membership id in order to be saved in the database.
*
* @since 1.0.0-beta.9
* @since 1.0.0-beta.25 Allow updating meta with the same value as the stored one.
*
* @param LLMS_Membership $membership LLMS_Membership instance.
* @param WP_REST_Request $request Full details about the request.
* @param array $schema The item schema.
* @param array $prepared_item Array.
* @param bool $creating Optional. Whether we're in creation or update phase. Default true (create).
* @return bool|WP_Error True on success or false if nothing to update, WP_Error object if something went wrong during the update.
*/
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(
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. |