/var/www/html_uk/wp-content/plugins/automatewoo/includes/Triggers/Sensei_Lesson_Started.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace AutomateWoo;

use 
AutomateWoo\DataTypes\DataTypes;

defined'ABSPATH' ) || exit;

/**
 * Class Trigger_Sensei_Lesson_Started.
 *
 * @since 5.6.10
 * @package AutomateWoo
 */
class Trigger_Sensei_Lesson_Started extends Trigger {

    
/**
     * Sets supplied data for the trigger.
     *
     * @var array
     */
    
public $supplied_data_items = [ DataTypes::SENSEI_COURSEDataTypes::SENSEI_LESSONDataTypes::SENSEI_TEACHERDataTypes::CUSTOMER ];

    
/**
     * Load admin details.
     */
    
public function load_admin_details() {
        
$this->title       __'Lesson Started''automatewoo' );
        
$this->description __'This trigger fires after the lesson is started.''automatewoo' );
        
$this->group       Sensei_Workflow_Helper::get_group_name();
    }

    
/**
     * Register trigger hooks.
     */
    
public function register_hooks() {
        
add_action'sensei_user_lesson_start', array( $this'handle_lesson_started' ), 10);
    }

    
/**
     * Registers any fields used on for a trigger
     */
    
public function load_fields() {
        
$lessons Sensei_Workflow_Helper::get_lessons_field();
        
$this->add_field$lessons );
    }

    
/**
     * Handle Lesson Start.
     *
     * @param int $user_id   User ID.
     * @param int $lesson_id Lesson ID.
     */
    
public function handle_lesson_started$user_id$lesson_id ) {
        
$lesson get_post$lesson_id );
        
$user   get_user_by'id'$user_id );
        if ( ! 
$lesson || ! $user ) {
            return;
        }

        
$course_id Sensei()->lesson->get_course_id$lesson_id );
        
$course    get_post$course_id );

        if ( ! 
$course ) {
            return;
        }

        foreach ( 
$this->get_workflows() as $workflow ) {
            
$sensei_lessons Clean::ids$workflow->get_trigger_option'sensei_lessons' ) );

            if ( ! empty( 
$sensei_lessons ) && ! in_array$lesson->ID$sensei_lessonstrue ) ) {
                continue;
            }

            
// Check if the lesson has been already started.
            
if ( \Sensei_Utils::user_started_lesson$lesson_id$user_id ) ) {
                continue;
            }

            
// Prevent duplicate triggers.
            
$query = new Queue_Query();
            
$query->where_workflow$workflow->get_id() );
            
$query->where_user$user_id );
            
$query->where_lesson$lesson_id );
            
$results $query->get_results();
            if ( ! empty( 
$results ) ) {
                continue;
            }

            
$workflow->maybe_run(
                [
                    
DataTypes::SENSEI_COURSE  => $course,
                    
DataTypes::SENSEI_TEACHER => get_user_by'id'$course->post_author ),
                    
DataTypes::SENSEI_LESSON  => $lesson,
                    
DataTypes::CUSTOMER       => Customer_Factory::get_by_user_id$user_id ),
                ]
            );
        }
    }
}