LLMS_REST_Access_Plans_Controller::update_additional_object_fields( LLMS_Access_Plan $access_plan, WP_REST_Request $request, array $schema, array $prepared_item, bool $creating = true )

Updates an existing single LLMS_Access_Plan in the database.


Description Description

This method should be used for access plan properties that require the access plan id in order to be saved in the database.


Top ↑

Parameters Parameters

$access_plan

(LLMS_Access_Plan) (Required) LLMS Access Plan 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-access-plans-controller.php

		$to_set = array();

		// Access expiration.
		if ( ! empty( $schema['properties']['access_expiration'] ) && isset( $request['access_expiration'] ) ) {
			$to_set['access_expiration'] = $request['access_expiration'];
		}

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

		// Access length.
		if ( ! empty( $schema['properties']['access_length'] ) && isset( $request['access_length'] ) ) {
			$to_set['access_length'] = $request['access_length'];
		}

		// Access period.
		if ( ! empty( $schema['properties']['access_period'] ) && isset( $request['access_period'] ) ) {
			$to_set['access_period'] = $request['access_period'];
		}

		// Redirect.
		if ( ! empty( $schema['properties']['redirect_type'] ) && isset( $request['redirect_type'] ) ) {
			$to_set['checkout_redirect_type'] = $request['redirect_type'];
		}

		// Redirect page.
		if ( ! empty( $schema['properties']['redirect_page'] ) && isset( $request['redirect_page'] ) ) {
			$redirect_page = get_post( $request['redirect_page'] );
			if ( $redirect_page && is_a( $redirect_page, 'WP_Post' ) ) {
				$to_set['checkout_redirect_page'] = $request['redirect_page']; // maybe allow only published pages?
			}
		}

		// Redirect url.
		if ( ! empty( $schema['properties']['redirect_url'] ) && isset( $request['redirect_url'] ) ) {
			$to_set['checkout_redirect_url'] = $request['redirect_url'];
		}

		// Price.
		if ( ! empty( $schema['properties']['price'] ) && isset( $request['price'] ) ) {
			$to_set['price'] = $request['price'];
		}

		// Sale enabled.
		if ( ! empty( $schema['properties']['sale_enabled'] ) && isset( $request['sale_enabled'] ) ) {
			$to_set['on_sale'] = $request['sale_enabled'] ? 'yes' : 'no';
		}

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

		if ( ! empty( $schema['properties']['sale_date_end'] ) && isset( $request['sale_date_end'] ) ) {
			$sale_date_end      = rest_parse_date( $request['sale_date_end'] );
			$to_set['sale_end'] = empty( $sale_date_end ) ? '' : date_i18n( 'Y-m-d H:i:s', $sale_date_end );
		}
		// Sale price.
		if ( ! empty( $schema['properties']['sale_price'] ) && isset( $request['sale_price'] ) ) {
			$to_set['sale_price'] = $request['sale_price'];
		}

		// Trial enabled.
		if ( ! empty( $schema['properties']['trial_enabled'] ) && isset( $request['trial_enabled'] ) ) {
			$to_set['trial_offer'] = $request['trial_enabled'] ? 'yes' : 'no';
		}

		// Trial Length.
		if ( ! empty( $schema['properties']['trial_length'] ) && isset( $request['trial_length'] ) ) {
			$to_set['trial_length'] = $request['trial_length'];
		}
		// Trial Period.
		if ( ! empty( $schema['properties']['trial_period'] ) && isset( $request['trial_period'] ) ) {
			$to_set['trial_period'] = $request['trial_period'];
		}
		// Trial price.
		if ( ! empty( $schema['properties']['trial_price'] ) && isset( $request['trial_price'] ) ) {
			$to_set['trial_price'] = $request['trial_price'];
		}

		// Availability restrictions.
		// If access plan related post type is not a course, set availability to 'open' and clean the `availability_restrictions` array.
		if ( 'course' !== $access_plan->get_product_type() ) {
			$to_set['availability']              = 'open';
			$to_set['availability_restrictions'] = array();
		} elseif ( ! empty( $schema['properties']['availability_restrictions'] ) && isset( $request['availability_restrictions'] ) ) {
			$to_set['availability_restrictions'] = $request['availability_restrictions'];
			// If availability restrictions supplied is not empty, set `availability` to 'members'.
			$to_set['availability'] = ! empty( $to_set['availability_restrictions'] ) ? 'members' : 'open';
		}

		// Redirect forced.
		if ( ! empty( $schema['properties']['redirect_forced'] ) && isset( $request['redirect_forced'] ) ) {
			$to_set['checkout_redirect_forced'] = $request['redirect_forced'] ? 'yes' : 'no';
		}

		// Frequency.
		if ( ! empty( $schema['properties']['frequency'] ) && isset( $request['frequency'] ) ) {
			$to_set['frequency'] = $request['frequency'];
		}

		// Length.
		if ( ! empty( $schema['properties']['length'] ) && isset( $request['length'] ) ) {
			$to_set['length'] = $request['length'];
		}
		// Period.
		if ( ! empty( $schema['properties']['period'] ) && isset( $request['period'] ) ) {
			$to_set['period'] = $request['period'];
		}

		$this->handle_props_interdependency( $to_set, $access_plan, $creating );

		// Visibility.
		if ( ! empty( $schema['properties']['visibility'] ) && isset( $request['visibility'] ) ) {
			$visibility = $access_plan->set_visibility( $request['visibility'] );
			if ( is_wp_error( $visibility ) ) {
				return $visibility;
			}
		}

		// Set bulk.
		if ( ! empty( $to_set ) ) {
			$update = $access_plan->set_bulk( $to_set, true, true );
			if ( is_wp_error( $update ) ) {
				$error = $update;
			}
		}

		if ( $error->errors ) {
			return $error;
		}

		return ! empty( $to_set ) || ! empty( $visibility );
	}

	/**
	 * Handle properties interdependency
	 *
	 * @since 1.0.0-beta.18


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.25 Allow updating meta with the same value as the stored one.
1.0.0-beta.18
1.0.0-beta-24 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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