LLMS_AJAX::favorite_object()

Add Favorite / Unfavorite postmeta for an object.


Return Return

(void)


Top ↑

Source Source

File: includes/class.llms.ajax.php

	public function favorite_object() {

		// Grab the data if it exists.
		$user_action = llms_filter_input_sanitize_string( INPUT_POST, 'user_action' );
		$object_id   = llms_filter_input( INPUT_POST, 'object_id', FILTER_SANITIZE_NUMBER_INT );
		$object_type = llms_filter_input_sanitize_string( INPUT_POST, 'object_type' );
		$user_id     = get_current_user_id();
		$student     = llms_get_student( $user_id );
		if ( is_null( $object_id ) || ! $student ) {
			return;
		}

		if ( 'favorite' === $user_action ) {
			// You can never mark favorite a non-free lesson of a course you're not enrolled into.
			if ( 'lesson' === $object_type ) {
				$lesson            = llms_get_post( $object_id );
				$can_mark_favorite = $lesson && ( $student->is_enrolled( $object_id ) || $lesson->is_free() );
				if ( ! $can_mark_favorite ) {
					return;
				}
			}

			llms_mark_favorite( $user_id, $object_id, $object_type );
		} elseif ( 'unfavorite' === $user_action ) {
			llms_mark_unfavorite( $user_id, $object_id, $object_type );
		}

		echo wp_json_encode(
			array(
				'total_favorites' => llms_get_object_total_favorites( $object_id ),
				'success'         => true,
			)
		);

		wp_die();
	}


Top ↑

Changelog Changelog

Changelog
Version Description
7.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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