LLMS_Events::record( array $args = array() )

Store an event in the database.


Parameters Parameters

$args

(array) (Optional) Event data

  • 'actor_id'
    (int) WP_User ID.
  • 'object_type'
    (string) Type of object being acted upon (post,user,comment,etc...).
  • 'object_id'
    (int) WP_Post ID, WP_User ID, WP_Comment ID, etc...
  • 'event_type'
    (string) Type of event (account, page, course, etc...).
  • 'event_action'
    (string) The event action or verb (signon,viewed,launched,etc...).

Default value: array()


Top ↑

Return Return

(LLMS_Event|WP_Error)


Top ↑

Source Source

File: includes/class-llms-events.php

	public function record( $args = array() ) {

		$err = new WP_Error();

		foreach ( array( 'actor_id', 'object_type', 'object_id', 'event_type', 'event_action' ) as $key ) {
			if ( ! in_array( $key, array_keys( $args ), true ) ) {
				// Translators: %s = key name of the missing field.
				$err->add( 'llms_event_record_missing_field', sprintf( __( 'Missing required field: "%s".', 'lifterlms' ), $key ) );
			}
		}

		if ( $err->get_error_codes() ) {
			return $err;
		}

		$event = sprintf( '%1$s.%2$s', $args['event_type'], $args['event_action'] );

		if ( ! $this->is_event_valid( $event ) ) {
			// Translators: %s = Submitted event string.
			return new WP_Error( 'llms_event_record_invalid_event', sprintf( __( 'The event "%s" is invalid.', 'lifterlms' ), $event ) );
		}

		$args = $this->sanitize_raw_event( $args );
		$meta = isset( $args['meta'] ) ? $args['meta'] : null;
		unset( $args['meta'] );

		if ( ! in_array( $event, array( 'session.start', 'session.end' ), true ) ) {

			// Start a session if one isn't open.
			$sessions = LLMS_Sessions::instance();
			$user_id  = 'account.signon' === $event && isset( $args['actor_id'] ) ? $args['actor_id'] : null;

			if ( false === $sessions->get_current( $user_id ) ) {
				$sessions->start( $user_id );
			}
		}

		$llms_event = new LLMS_Event();
		if ( ! $llms_event->setup( $args )->save() ) {
			$err->add( 'llms_event_recored_unknown_error', __( 'An unknown error occurred during event creation.', 'lifterlms' ) );
			return $err;
		}
		if ( $meta && ! empty( $meta ) ) {
			$llms_event->set_metas( $meta, true );
		}

		// End the current session on signout.
		if ( 'account.signout' === $event ) {
			LLMS_Sessions::instance()->end_current();
		}

		return $llms_event;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
4.5.0 Fixed event session end not recorded on sign-out.
3.36.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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