Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
myCRED Custom Hook: LearnDash
/**
* Register Custom myCRED Hook
* @since 1.0
* @version 1.0
*/
add_filter( 'mycred_setup_hooks', 'Learndash_myCRED_Hook' );
function Learndash_myCRED_Hook( $installed ) {
$installed['hook_learndash'] = array(
'title' => __( 'LearnDash', 'mycred' ),
'description' => __( 'Awards %_plural% for LearnDash actions.', 'mycred' ),
'callback' => array( 'myCRED_Hook_Learndash' )
);
return $installed;
}
/**
* Hook for LearnDash
* @since 1.0
* @version 1.0
*/
if ( ! class_exists( 'myCRED_Hook_Learndash' ) && class_exists( 'myCRED_Hook' ) ) {
class myCRED_Hook_Learndash extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs, $type = 'mycred_default' ) {
parent::__construct( array(
'id' => 'hook_learndash',
'defaults' => array(
'course_completed' => array(
'creds' => 0,
'log' => '%plural% for Completing a Course'
),
'lesson_completed' => array(
'creds' => 0,
'log' => '%plural% for Completing a Lesson'
),
'topic_completed' => array(
'creds' => 0,
'log' => '%plural% for Completing a Topic'
),
'quiz_completed' => array(
'creds' => 0,
'log' => '%plural% for Completing a Quiz'
)
)
), $hook_prefs, $type );
}
/**
* Run
* @since 1.0
* @version 1.1
*/
public function run() {
// Course Completed
if ( $this->prefs['course_completed']['creds'] != 0 )
add_action( 'learndash_course_completed', array( $this, 'course_completed' ), 5, 1 );
// Lesson Completed
if ( $this->prefs['lesson_completed']['creds'] != 0 )
add_action( 'learndash_lesson_completed', array( $this, 'lesson_completed' ), 5, 1 );
// Topic Completed
if ( $this->prefs['topic_completed']['creds'] != 0 )
add_action( 'learndash_topic_completed', array( $this, 'topic_completed' ), 5, 1 );
// Quiz Completed
if ( $this->prefs['quiz_completed']['creds'] != 0 )
add_action( 'learndash_quiz_completed', array( $this, 'quiz_completed' ), 5, 1 );
add_filter( 'mycred_all_references', array( $this, 'add_references' ) );
}
/**
* Course Completed
* @since 1.0
* @version 1.1
*/
public function course_completed( $data ) {
$course_id = $data['course']->ID;
// Must be logged in
if ( ! is_user_logged_in() ) return;
$user_id = get_current_user_id();
// Check if user is excluded
if ( $this->core->exclude_user( $user_id ) ) return;
// Make sure this is unique event
if ( $this->core->has_entry( 'course_completed', $course_id, $user_id ) ) return;
// Execute
$this->core->add_creds(
'course_completed',
$user_id,
$this->prefs['course_completed']['creds'],
$this->prefs['course_completed']['log'],
$course_id,
array( 'ref_type' => 'post' ),
$this->mycred_type
);
}
/**
* Lesson Completed
* @since 1.0
* @version 1.1
*/
public function lesson_completed( $data ) {
$lesson_id = $data['lesson']->ID;
// Must be logged in
if ( ! is_user_logged_in() ) return;
$user_id = get_current_user_id();
// Check if user is excluded
if ( $this->core->exclude_user( $user_id ) ) return;
// Make sure this is unique event
if ( $this->core->has_entry( 'lesson_completed', $lesson_id, $user_id ) ) return;
// Execute
$this->core->add_creds(
'lesson_completed',
$user_id,
$this->prefs['lesson_completed']['creds'],
$this->prefs['lesson_completed']['log'],
$lesson_id,
array( 'ref_type' => 'post' ),
$this->mycred_type
);
}
/**
* Topic Completed
* @since 1.0
* @version 1.1
*/
public function topic_completed( $data ) {
$topic_id = $data['topic']->ID;
// Must be logged in
if ( ! is_user_logged_in() ) return;
$user_id = get_current_user_id();
// Check if user is excluded
if ( $this->core->exclude_user( $user_id ) ) return;
// Make sure this is unique event
if ( $this->core->has_entry( 'topic_completed', $topic_id, $user_id ) ) return;
// Execute
$this->core->add_creds(
'topic_completed',
$user_id,
$this->prefs['topic_completed']['creds'],
$this->prefs['topic_completed']['log'],
$topic_id,
array( 'ref_type' => 'post' ),
$this->mycred_type
);
}
/**
* Quiz Completed
* @since 1.0
* @version 1.1
*/
public function quiz_completed( $data ) {
$quiz_id = $data['quiz']->ID;
// Must be logged in
if ( ! is_user_logged_in() ) return;
$user_id = get_current_user_id();
// Check if user is excluded
if ( $this->core->exclude_user( $user_id ) ) return;
// Make sure this is unique event
if ( $this->core->has_entry( 'quiz_completed', $quiz_id, $user_id ) ) return;
// Execute
$this->core->add_creds(
'quiz_completed',
$user_id,
$this->prefs['quiz_completed']['creds'],
$this->prefs['quiz_completed']['log'],
$quiz_id,
array( 'ref_type' => 'post' ),
$this->mycred_type
);
}
/**
* Register Custom myCRED References
* @since 1.0
* @version 1.0
*/
public function add_references( $references ) {
// LearnDash References
$references['course_completed'] = 'Completed Course';
$references['lesson_completed'] = 'Completed Lesson';
$references['topic_completed'] = 'Completed Topic';
$references['quiz_completed'] = 'Completed Quiz';
return $references;
}
/**
* Preferences for LearnDash
* @since 1.1
* @version 1.0
*/
public function preferences() {
$prefs = $this->prefs; ?>
<label class="subheader" for="<?php echo $this->field_id( array( 'course_completed' => 'creds' ) ); ?>"><?php _e( 'Completing a Course', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'course_completed' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'course_completed' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['course_completed']['creds'] ); ?>" size="8" /></div>
</li>
</ol>
<label class="subheader" for="<?php echo $this->field_id( array( 'course_completed' => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'course_completed' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'course_completed' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['course_completed']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
</li>
</ol>
<label class="subheader" for="<?php echo $this->field_id( array( 'lesson_completed' => 'creds' ) ); ?>"><?php _e( 'Completing a Lesson', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'lesson_completed' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'lesson_completed' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['lesson_completed']['creds'] ); ?>" size="8" /></div>
</li>
</ol>
<label class="subheader" for="<?php echo $this->field_id( array( 'lesson_completed' => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'lesson_completed' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'lesson_completed' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['lesson_completed']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
</li>
</ol>
<label class="subheader" for="<?php echo $this->field_id( array( 'topic_completed' => 'creds' ) ); ?>"><?php _e( 'Completing a Topic', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'topic_completed' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'topic_completed' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['topic_completed']['creds'] ); ?>" size="8" /></div>
</li>
</ol>
<label class="subheader" for="<?php echo $this->field_id( array( 'topic_completed' => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'topic_completed' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'topic_completed' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['topic_completed']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
</li>
</ol>
<label class="subheader" for="<?php echo $this->field_id( array( 'quiz_completed' => 'creds' ) ); ?>"><?php _e( 'Completing a Quiz', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'quiz_completed' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'quiz_completed' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['quiz_completed']['creds'] ); ?>" size="8" /></div>
</li>
</ol>
<label class="subheader" for="<?php echo $this->field_id( array( 'quiz_completed' => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'quiz_completed' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'quiz_completed' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['quiz_completed']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
</li>
</ol>
<?php
}
}
}
@heroherohero

This comment has been minimized.

Copy link

heroherohero commented Nov 1, 2015

Hello,

I placed the snippet on my theme function.php. But I get a error message and no contents of the page shows up.

I've no knowledge of php language. However, the code "<?php" on line 289 suggests a mistake in the coding?

I hope someone can confirm that the code is functional on their website.

Cheers,
Hiro

@X-Raym

This comment has been minimized.

Copy link

X-Raym commented Nov 19, 2015

You should propose it to LearnDash Team and myCred Team to make it an official add-on. :)

@kocherm

This comment has been minimized.

Copy link

kocherm commented Aug 16, 2016

It doesn't seem to work as of myCred 1.7
Any plans to update it? I could help chip in for funding.

@adeel-raza

This comment has been minimized.

Copy link

adeel-raza commented Aug 12, 2017

LearnDash myCRED integration is available now with support for multiple myCRED point types as well. Have a look at this here.
https://wooninjas.com/wn-products/learndash-mycred-addon/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.