/var/www/html_uk/wp-content/plugins/automatewoo/includes/Usage_Tracking/Initializer.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
101
102
103
104
105
106
<?php

namespace AutomateWoo\Usage_Tracking;

use 
AutomateWoo\Exceptions\InvalidClass;

/**
 * Static Helper Class for tracks.
 *
 * @package AutomateWoo\Usage_Tracking
 * @since   4.9.0
 */
class Initializer {

    
/**
     * The tracks object.
     *
     * @var Tracks_Interface
     */
    
private static $tracks;

    
/**
     * Initialize our tracking classes.
     *
     * There are two kinds of data that we're tracking: events, referred to as "Tracks", and
     * general store data, referred to as the "Tracker". Here we initialize both types of data.
     */
    
public static function init() {
        if ( ! 
apply_filters'automatewoo/usage_tracking/enabled'true ) || 'yes' !== get_option'woocommerce_allow_tracking''no' ) ) {
            return;
        }

        
do_action'automatewoo/usage_tracking/init' );

        
self::initialize_tracks();
        
self::initialize_tracker();
    }

    
/**
     * Initialize the tracks object if needed.
     */
    
private static function maybe_initialize_tracks() {
        if ( 
null === self::$tracks ) {
            
self::$tracks = new Tracks();
        }
    }

    
/**
     * Initialize our tracks classes.
     *
     * @throws InvalidClass When a class does not exist, or the proper interface is not implemented.
     */
    
private static function initialize_tracks() {
        
self::maybe_initialize_tracks();

        
// Allow add-ons to include their own classes for tracking.
        
$addon_classes = (array) apply_filters'automatewoo/usage_tracking/addon_tracking_classes', [] );

        
// Our own list of classes for event tracking.
        
$classes = [
            
Conversions::class,
            
Install::class,
            
Workflows::class,
        ];

        
// Instantiate each class.
        
$classes array_uniquearray_merge$addon_classes$classes ) );
        foreach ( 
$classes as $class ) {
            
self::validate_class$classEvent_Tracker_Interface::class );

            
/** @var Event_Tracker_Interface $instance */
            
$instance = new $class();
            
$instance->init();
            
$instance->set_tracksself::$tracks );
        }
    }

    
/**
     * Hook our custom tracker data to the regular WC tracker data.
     */
    
private static function initialize_tracker() {
        global 
$wpdb;

        
$tracker = new Tracker$wpdb );
        
$tracker->init();
    }

    
/**
     * Validate that a class exists and that it implements the given interface.
     *
     * @param string $class     The class to validate.
     * @param string $interface The interface the class should implement.
     *
     * @throws InvalidClass When the class is invalid.
     */
    
private static function validate_class$class$interface ) {
        if ( ! 
class_exists$class ) ) {
            throw 
InvalidClass::does_not_existesc_html$class ) );
        }

        
$implements class_implements$class );
        if ( ! 
array_key_exists$interface$implements ) ) {
            throw 
InvalidClass::does_not_implement_interfaceesc_html$class ), esc_html$interface ) );
        }
    }
}