class-integration-manager.php
4.43 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Class MC4WP_Integration_Manager
*
* @ignore
* @access private
*/
class MC4WP_Integration_Manager
{
/**
* @var MC4WP_Integration_Fixture[]
*/
protected $integrations = array();
/**
* @var MC4WP_Integration_Tags
*/
protected $tags;
/**
* Constructor
*/
public function __construct()
{
$this->tags = new MC4WP_Integration_Tags();
}
/**
* Add hooks
*/
public function add_hooks()
{
add_action('after_setup_theme', array( $this, 'initialize' ));
$this->tags->add_hooks();
}
/**
* Add hooks
*/
public function initialize()
{
/*** @var MC4WP_Integration_Fixture $integration */
$enabled_integrations = $this->get_enabled_integrations();
foreach ($enabled_integrations as $integration) {
$integration->load()->initialize();
}
}
/**
* Get an integration instance
*
* @return MC4WP_Integration_Fixture[]
* @throws Exception
*/
public function get_all()
{
return $this->integrations;
}
/**
* Get an integration instance
*
* @param string $slug
* @return MC4WP_Integration
* @throws Exception
*/
public function get($slug)
{
if (! isset($this->integrations[ $slug ])) {
throw new Exception(sprintf('No integration with slug %s has been registered.', $slug));
}
return $this->integrations[ $slug ]->load();
}
/**
* Register a new integration class
*
* @param string $slug
* @param string $class
* @param bool $enabled
*/
public function register_integration($slug, $class, $enabled = false)
{
$raw_options = $this->get_integration_options($slug);
$this->integrations[ $slug ] = new MC4WP_Integration_Fixture($slug, $class, $enabled, $raw_options);
}
/**
* Deregister an integration class
*
* @param string $slug
*/
public function deregister_integration($slug)
{
if (isset($this->integrations[ $slug ])) {
unset($this->integrations[ $slug ]);
}
}
/**
* Checks whether a certain integration is enabled (in the settings)
*
* This is decoupled from the integration class itself as checking an array is way "cheaper" than instantiating an object
*
* @param MC4WP_Integration_Fixture $integration
*
* @return bool
*/
public function is_enabled(MC4WP_Integration_Fixture $integration)
{
return $integration->enabled;
}
/**
* @param MC4WP_Integration $integration
* @return bool
*/
public function is_installed($integration)
{
return $integration->is_installed();
}
/**
* Get the integrations which are enabled
*
* - Some integrations are always enabled because they need manual work
* - Other integrations can be enabled in the settings page
* - Only returns installed integrations
*
* @return array
*/
public function get_enabled_integrations()
{
// get all enabled integrations
$enabled_integrations = array_filter($this->integrations, array( $this, 'is_enabled' ));
// remove duplicate values, for whatever reason..
$enabled_integrations = array_unique($enabled_integrations);
// filter out integrations which are not installed
$installed_enabled_integrations = array_filter($enabled_integrations, array( $this, 'is_installed' ));
return $installed_enabled_integrations;
}
/**
* Gets all integration options in a keyed array
*
* @return array
*/
private function load_options()
{
$options = (array) get_option('mc4wp_integrations', array());
/**
* Filters global integration options
*
* This array holds ALL integration settings
*
* @since 3.0
* @param array $options
* @ignore
*/
return (array) apply_filters('mc4wp_integration_options', $options);
}
/**
* Gets the raw options for an integration
*
* @param $slug
* @return array
*/
public function get_integration_options($slug)
{
static $options;
if ($options === null) {
$options = $this->load_options();
}
return isset($options[ $slug ]) ? $options[ $slug ] : array();
}
}