Dependencies

Class hendling plugin dependencies

Description

Source

File: bp-integrations/learndash/core/Dependencies.php

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
107
108
class Dependencies
{
    protected $dependencies = [];
    protected $loadedDependencies = [];
 
    /**
     * Constructor
     *
     * @since BuddyBoss 1.0.0
     */
    public function __construct()
    {
        $this->dependencies = [
            'bp_init'        => __('BuddyBoss Platform', 'buddyboss'),
            'learndash_init' => __('Learndash LMS', 'buddyboss')
        ];
 
        $this->registerHooks();
    }
 
    /**
     * Add hook to each dependencies' init hook
     *
     * @since BuddyBoss 1.0.0
     */
    public function registerHooks()
    {
        foreach ($this->dependencies as $hook => $__) {
            add_action($hook, [$this, 'dependencyLoaded']);
        }
 
        add_action('init', [$this, 'appendDependencyChecker']);
    }
 
    /**
     * Set a flag when dependency is init
     *
     * @since BuddyBoss 1.0.0
     */
    public function dependencyLoaded()
    {
        $this->loadedDependencies[] = current_filter();
    }
 
    /**
     * Check if the required dependencies are all init
     *
     * @since BuddyBoss 1.0.0
     */
    public function appendDependencyChecker()
    {
        global $wp_filter;
 
        $callbackKeys = array_keys($wp_filter['init']->callbacks);
        $lastCallbackPriority = $callbackKeys[count($callbackKeys) - 1];
 
        add_action('init', [$this, 'dependencyChecker'], $lastCallbackPriority);
    }
 
    /**
     * Run sub action based on depencency init status
     *
     * @since BuddyBoss 1.0.0
     */
    public function dependencyChecker()
    {
        $success = count($this->dependencies) == count($this->loadedDependencies);
        do_action($success? 'bp_ld_sync/depencencies_loaded' : 'bp_ld_sync/depencencies_failed', $this);
    }
 
    /**
     * Check if any dependencies is missing
     *
     * @since BuddyBoss 1.0.0
     */
    public function getMissingDepencencies()
    {
        return array_diff_key($this->dependencies, array_flip($this->loadedDependencies));
    }
 
    /**
     * Get the dependencies that are loaded successfully
     *
     * @since BuddyBoss 1.0.0
     */
    public function getLoadedDepencencies()
    {
        return array_intersect_key($this->dependencies, array_flip($this->loadedDependencies));
    }
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.