-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCheck_Plugin_Dependencies.php
81 lines (72 loc) · 2.08 KB
/
Check_Plugin_Dependencies.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
<?php
/**
* Check_Plugin_Dependencies class
*
* The plugin's main class.
*
* @package Check_Plugin_Dependencies
* @since 1.0.0
*/
namespace Check_Plugin_Dependencies;
use Check_Plugin_Dependencies\Admin\Missing_Dependency_Reporter;
use Check_Plugin_Dependencies\Exception\Missing_Dependencies_Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* The plugin's main class that only loads the plugin logic if dependencies are met and displays a notice
* in the admin dashboard otherwise.
*
* @since 1.0.0
*/
class Check_Plugin_Dependencies {
/**
* Runs the plugin only if its dependencies are met. If not, displays a notice in the admin dashboard.
*
* If Missing_Dependencies_Exception is thrown inside $this->check_dependencies(), the $this->run() method
* that follows and does the actual plugin functionality registration will not execute - instead, control will
* be passed to the `catch` block below.
*
* @since 1.0.0
*/
public function setup() {
try {
$this->check_dependencies();
$this->run();
} catch ( Missing_Dependencies_Exception $e ) {
$this->display_missing_dependencies_notice( $e );
}
}
/**
* Instantiates and runs the dependency checker.
*
* @since 1.0.0
*
* @throws Missing_Dependencies_Exception
*/
private function check_dependencies() {
$dependency_checker = new Dependency_Checker();
$dependency_checker->check();
}
/**
* Executes the actual plugin logic.
*
* @since 1.0.0
*/
private function run() {
// Execute the actual plugin functionality here - maybe define some hooks using add_action(), add_filter() etc.
}
/**
* Instantiates and runs the Missing_Dependency_Reporter.
*
* Gets a list of missing plugins from the exception and feeds it to Missing_Dependency_Reporter.
*
* @since 1.0.0
*
* @param Missing_Dependencies_Exception $e
*/
private function display_missing_dependencies_notice( Missing_Dependencies_Exception $e ) {
$missing_dependency_reporter = new Missing_Dependency_Reporter( $e->get_missing_plugin_names() );
$missing_dependency_reporter->init();
}
}