Я хочу создать собственный слушатель событий в GamiPress. Код выглядит следующим образом:
function my_prefix_register_specific_triggers_for_purchase_a_product( $triggers ) {
$triggers['My Custom Specific Purchase Events'] = array(
// Register the event my_prefix_custom_specific_purchase_event
'my_prefix_custom_specific_purchase_event' => __( 'Purchase a specific product', 'gamipress' ),
);
return $triggers;
}
add_filter( 'gamipress_activity_triggers', 'my_prefix_register_specific_triggers_for_purchase_a_product' );
function my_prefix_specific_triggers_for_purchase_a_product( $specific_triggers ) {
// Set 'my_prefix_custom_specific_purchase_event' as specific event that requires the 'product' post type
$specific_triggers['my_prefix_custom_specific_purchase_event'] = array( 'product' );
return $specific_triggers;
}
add_filter( 'gamipress_specific_activity_triggers', 'my_prefix_specific_triggers_for_purchase_a_product' );
function my_prefix_specific_label_for_purchase_a_product( $specific_trigger_labels ) {
// %s will be replaced with the product title
$specific_trigger_labels['my_prefix_custom_specific_purchase_event'] = __( 'Purchase the %s product', 'gamipress' );
// GamiPress automatically will use this pattern for auto-generate the requirement label
// Some examples with "Purchase the %s product" label:
// Step example using the product "T-shirt": Purchase the T-shirt product 2 times
// Points award example using the product "CD Album": 10 points for purchase the CD Album product 5 times
return $specific_trigger_labels;
}
add_filter( 'gamipress_specific_activity_trigger_label', 'my_prefix_specific_label_for_purchase_a_product' );
function my_prefix_specific_listener_for_purchase_a_product( $order_id ) {
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item ) {
// Call to gamipress_trigger_event() on each product purchased
gamipress_trigger_event( array(
'event' => 'my_prefix_custom_specific_purchase_event', // Set our custom purchase event
'user_id' => $order->user_id, // User that will be awarded is the one who made the order
'specific_id' => $item->get_product_id(), // Specific ID, here is the product ID
// Add any extra parameters you want
// In this example we passed the order item object and the order object itself
'item' => $item,
'order' => $order,
) );
}
}
// The safe way to check if an user has perform a purchase is when an order payment is set to completed
// So let's hook the 'woocommerce_payment_complete' action
add_action( 'woocommerce_payment_complete', 'my_prefix_specific_listener_for_purchase_a_product' );
Теперь я хочу изменить приведенный выше код, чтобы он становился прослушивателем событий каждый раз, когда пользователь получает баллы в Namaste LMS !. Я просмотрел функцию в файлах Namaste, и она выглядит следующим образом:
static function auto_grade_course($course_id, $student_id) {
global $wpdb;
if(get_option('namaste_use_grading_system') == '') return false;
$auto_grade = get_post_meta($course_id, 'namaste_auto_grade', true);
if(!$auto_grade) return false;
$_lesson = new NamasteLMSLessonModel();
$lessons = $_lesson->select($course_id);
if(!sizeof($lessons)) return false;
$grades_points = self :: grades_points();
$total = $num_graded_lessons = 0;
foreach($lessons as $lesson) {
$lesson_grade = $wpdb->get_var($wpdb->prepare("SELECT grade FROM ".NAMASTE_STUDENT_LESSONS."
WHERE student_id=%d AND lesson_id=%d", $student_id, $lesson->ID));
if(!empty($lesson_grade)) {
$num_graded_lessons++;
$lesson_grade = trim($lesson_grade);
$points = intval(@$grades_points[$lesson_grade]);
$total += $points;
}
} // end foreach lesson
Я не хочу просить о готовом решении, но некоторые из вас могут помочь мне найти мои ответы о том, как создатьконкретное событие, которое срабатывает, когда пользователь получает точку в Namaste LMS?