Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

LLMS_Engagement_Handler::create( string $type, int $user_id, int $template_id, string $related_id = '', null|int $engagement_id = null )

Create a new earned achievement or certificate.


Description Description

This method is called by handler callback functions run when engagements are triggered.

Before arriving here the input data ($user_id, $template_id, etc…) has already been validated to ensure that it exists and the engagement can be processed using this data.


Top ↑

Parameters Parameters

$type

(string) (Required) The engagement type, either "achievement" or "certificate".

$user_id

(int) (Required) WP_User ID of the student earning the engagement.

$template_id

(int) (Required) WP_Post ID of the template post (llms_achievement or llms_certificate).

$related_id

(string) (Optional) WP_Post ID of the triggering related post (course, lesson, etc...) or an empty string for user registration.

Default value: ''

$engagement_id

(null|int) (Optional) WP_Post ID of the engagement post used to configure the trigger. A null value maybe be passed for legacy delayed engagements which were created without an engagement ID or when manually awarding via the admin UI.

Default value: null


Top ↑

Return Return

(WP_Error|LLMS_User_Certificate|LLMS_User_Achievement)


Top ↑

Source Source

File: includes/class-llms-engagement-handler.php

	private static function create( $type, $user_id, $template_id, $related_id = '', $engagement_id = null ) {

		$title    = get_post_meta( $template_id, "_llms_{$type}_title", true );
		$template = get_post( $template_id );

		// Setup args, ultimately passed to `wp_insert_post()`.
		$post_args = array(
			'post_author'  => $user_id,
			'post_content' => $template->post_content,
			'post_date'    => llms_current_time( 'mysql' ),
			'post_name'    => 'certificate' === $type ? llms()->certificates()->get_unique_slug( $title ) : null,
			'post_parent'  => $template_id,
			'post_status'  => 'publish',
			'post_title'   => $title,
			'meta_input'   => array(
				'_thumbnail_id'    => self::get_image_id( $type, $template_id ),
				'_llms_engagement' => $engagement_id,
				'_llms_related'    => $related_id,
			),
		);

		// Do deprecated filters. No direct replacement added, instead use `LLMS_Post_Model` creation filters.
		$post_args = self::do_deprecated_creation_filters( $post_args, $type );

		$model_class = sprintf( 'LLMS_User_%s', ucwords( $type ) );
		$generated   = new $model_class( 'new', $post_args );
		if ( ! $generated || ! $generated->get( 'id' ) ) {
			return new WP_Error( 'llms-engagement-init--create', __( 'An error was encountered during post creation.', 'lifterlms' ), compact( 'user_id', 'template_id', 'related_id', 'engagement_id', 'post_args', 'type', 'model_class' ) );
		}

		// Reinstantiate the class so the merged post_content will be retrieved if accessed immediately.
		return new $model_class( $generated->get( 'id' ) );

	}


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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