LLMS_REST_Courses_Controller::update_additional_object_fields( LLMS_Course $course, WP_REST_Request $request, array $schema, array $prepared_item, bool $creating = true )

Updates a single llms course.


Parameters Parameters

$course

(LLMS_Course) (Required) LLMS_Course 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


Top ↑

Return Return

(bool|WP_Error) True on success or false if nothing to update, WP_Error object if something went wrong during the update.


Top ↑

Source Source

File: libraries/lifterlms-rest/includes/server/class-llms-rest-courses-controller.php

	 *                     if their values didn't really change, otherwise we'd get a WP_Error which the consumer cannot avoid having no direct control on those properties.
	 * @since 1.0.0-beta.8 Call `set_bulk()` llms post method passing `true` as second parameter,
	 *                     so to instruct it to return a WP_Error on failure.
	 * @since 1.0.0-beta.9 Use `WP_Error::$errors` in place of `WP_Error::has_errors()` to support WordPress version prior to 5.1.
	 *                     Also made sure course's `instructor` is at least set as the post author.
	 * @since 1.0.0-beta.25 Allow updating meta with the same value as the stored one.
	 *
	 * @param LLMS_Course     $course        LLMS_Course 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( $course, $request, $schema, $prepared_item, $creating = true ) {

		$error = new WP_Error();

		// Course catalog visibility.
		if ( ! empty( $schema['properties']['catalog_visibility'] ) && isset( $request['catalog_visibility'] ) ) {
			$course->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: `$course->set_instructor( $instructors )` when `$instructors` is empty
			// will set the course's author as course's instructor.
			if ( $creating || ( ! $creating && isset( $request['instructors'] ) ) ) {
				$course->set_instructors( $instructors );
			}
		}

		$to_set = array();

		// Access dates.
		if ( ! empty( $schema['properties']['access_opens_date'] ) && isset( $request['access_opens_date'] ) ) {
			$access_opens_date    = rest_parse_date( $request['access_opens_date'] );
			$to_set['start_date'] = empty( $access_opens_date ) ? '' : date_i18n( 'Y-m-d H:i:s', $access_opens_date );
		}

		if ( ! empty( $schema['properties']['access_closes_date'] ) && isset( $request['access_closes_date'] ) ) {
			$access_closes_date = rest_parse_date( $request['access_closes_date'] );
			$to_set['end_date'] = empty( $access_closes_date ) ? '' : date_i18n( 'Y-m-d H:i:s', $access_closes_date );
		}

		// Needed until the following will be implemented: https://github.com/gocodebox/lifterlms/issues/908.
		if ( ! empty( $to_set['start_date'] ) || ! empty( $to_set['end_date'] ) ) {
			$to_set['time_period'] = 'yes';
		} else {
			$to_set['time_period'] = 'no';
		}

		// Enrollment dates.
		if ( ! empty( $schema['properties']['enrollment_opens_date'] ) && isset( $request['enrollment_opens_date'] ) ) {
			$enrollment_opens_date           = rest_parse_date( $request['enrollment_opens_date'] );
			$to_set['enrollment_start_date'] = empty( $enrollment_opens_date ) ? '' : date_i18n( 'Y-m-d H:i:s', $enrollment_opens_date );
		}

		if ( ! empty( $schema['properties']['enrollment_closes_date'] ) && isset( $request['enrollment_closes_date'] ) ) {
			$enrollment_closes_date        = rest_parse_date( $request['enrollment_closes_date'] );
			$to_set['enrollment_end_date'] = empty( $enrollment_closes_date ) ? '' : date_i18n( 'Y-m-d H:i:s', $enrollment_closes_date );
		}

		// Needed until the following will be implemented: https://github.com/gocodebox/lifterlms/issues/908.
		if ( ! empty( $to_set['enrollment_start_date'] ) || ! empty( $to_set['enrollment_end_date'] ) ) {
			$to_set['enrollment_period'] = 'yes';
		} else {
			$to_set['enrollment_period'] = 'no';
		}

		// Prerequisite.
		if ( ! empty( $schema['properties']['prerequisite'] ) && isset( $request['prerequisite'] ) ) {
			// check if course exists.
			$prerequisite = llms_get_post( $request['prerequisite'] );
			if ( is_a( $prerequisite, 'LLMS_Course' ) ) {
				$to_set['prerequisite'] = $request['prerequisite'];
			} else {
				$to_set['prerequisite'] = 0;
			}
		}

		// Prerequisite track.
		if ( ! empty( $schema['properties']['prerequisite_track'] ) && isset( $request['prerequisite_track'] ) ) {
			// check if the track exists.
			$track = new LLMS_Track( $request['prerequisite_track'] );
			if ( $track->term ) {
				$to_set['prerequisite_track'] = $request['prerequisite_track'];
			} else {
				$to_set['prerequisite_track'] = 0;
			}
		}

		// Needed until the following will be implemented: https://github.com/gocodebox/lifterlms/issues/908.
		if ( ! empty( $to_set['prerequisite'] ) || ! empty( $to_set['prerequisite_track'] ) ) {
			$to_set['has_prerequisite'] = 'yes';
		} else {
			$to_set['has_prerequisite'] = 'no';
		}

		/**
		 * The following properties have a default value that contains a placeholder ({{course_id}}) that can be "expanded" only
		 * after the course has been created.
		 */
		// Access opens/closes messages.
		if ( ! empty( $schema['properties']['access_opens_message'] ) && isset( $request['access_opens_message'] ) ) {
			if ( is_string( $request['access_opens_message'] ) ) {
				$to_set['course_opens_message'] = $request['access_opens_message'];
			} elseif ( isset( $request['access_opens_message']['raw'] ) ) {
				$to_set['course_opens_message'] = $request['access_opens_message']['raw'];
			}
		}

		if ( ! empty( $schema['properties']['access_closes_message'] ) && isset( $request['access_closes_message'] ) ) {
			if ( is_string( $request['access_closes_message'] ) ) {
				$to_set['course_closed_message'] = $request['access_closes_message'];
			} elseif ( isset( $request['access_closes_message']['raw'] ) ) {
				$to_set['course_closed_message'] = $request['access_closes_message']['raw'];
			}
		}

		// Enrollments opens/closes messages.
		if ( ! empty( $schema['properties']['enrollment_opens_message'] ) && isset( $request['enrollment_opens_message'] ) ) {
			if ( is_string( $request['enrollment_opens_message'] ) ) {
				$to_set['enrollment_opens_message'] = $request['enrollment_opens_message'];
			} elseif ( isset( $request['enrollment_opens_message']['raw'] ) ) {
				$to_set['enrollment_opens_message'] = $request['enrollment_opens_message']['raw'];
			}
		}

		if ( ! empty( $schema['properties']['enrollment_closes_message'] ) && isset( $request['enrollment_closes_message'] ) ) {
			if ( is_string( $request['enrollment_closes_message'] ) ) {
				$to_set['enrollment_closed_message'] = $request['enrollment_closes_message'];
			} elseif ( isset( $request['enrollment_closes_message']['raw'] ) ) {
				$to_set['enrollment_closed_message'] = $request['enrollment_closes_message']['raw'];
			}
		}

		// Are we creating a course?
		// If so, replace the placeholder with the actual course id.
		if ( $creating ) {

			$_to_expand_props = array(
				'course_opens_message',
				'course_closed_message',
				'enrollment_opens_message',
				'enrollment_closed_message',
			);

			$course_id = $course->get( 'id' );

			foreach ( $_to_expand_props as $prop ) {
				if ( ! empty( $to_set[ $prop ] ) ) {
					$to_set[ $prop ] = str_replace( '{{course_id}}', $course_id, $to_set[ $prop ] );
				}
			}
		}

		// Set bulk.


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.9 Use WP_Error::$errors in place of WP_Error::has_errors() to support WordPress version prior to 5.1. Also made sure course's instructor is at least set as the post author.
1.0.0-beta.8 Call set_bulk() llms post method passing true as second parameter, so to instruct it to return a WP_Error on failure.
1.0.0-beta.7 Allow prerequisite and prerequisite_track to be cleared (set to 0). Also:
  • if prerequisite is not a valid course the course prerequisite will be set to 0;
  • if prerequisite_track is not a valid course track, the course prerequisite_track will be set to 0. Return false if nothing to update. Properties access_opens_date, access_closes_date, enrollment_opens_date, enrollment_closes_date handling moved here from prepare_item_for_database() method so to better handle the update of the course's properties time_period and enrollment_period. Made access_opens_date, access_closes_date, enrollment_opens_date, enrollment_closes_date properties nullable. Added logic to prevent trying to update "derived only" courses's properties (time_period, enrollment_period, has_prerequisite) if their values didn't really change, otherwise we'd get a WP_Error which the consumer cannot avoid having no direct control on those properties.
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.