Created
November 14, 2016 12:32
-
-
Save gabrielmerovingi/143c14d4f6a1d39c5d9bec0ecd80eb44 to your computer and use it in GitHub Desktop.
Learndash hook.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Register Custom myCRED Hook | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
add_filter( 'mycred_setup_hooks', 'mycred_register_learndash_hook' ); | |
function mycred_register_learndash_hook( $installed ) { | |
$installed['hook_learndash'] = array( | |
'title' => __( 'LearnDash' ), | |
'description' => __( 'Awards %_plural% for LearnDash actions.' ), | |
'callback' => array( 'myCRED_Hook_Learndash' ) | |
); | |
return $installed; | |
} | |
/** | |
* Hook for LearnDash | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
add_action( 'mycred_load_hooks', 'mycred_load_learndash_hook' ); | |
function mycred_load_learndash_hook() { | |
if ( class_exists( 'myCRED_Hook_Learndash' ) && ! class_exists( 'myCRED_Hook' ) ) return; | |
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment