|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YIKES Inc. Easy Mailchimp Forms. |
|
4
|
|
|
* |
|
5
|
|
|
* @package YIKES\EasyForms |
|
6
|
|
|
* @author Freddie Mixell |
|
7
|
|
|
* @license GPL2 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace YIKES\EasyForms; |
|
11
|
|
|
|
|
12
|
|
|
use YIKES\EasyForms\Assets\AssetsAware; |
|
13
|
|
|
use YIKES\EasyForms\Assets\AssetsHandler; |
|
14
|
|
|
use YIKES\EasyForms\Exception\InvalidClass; |
|
15
|
|
|
use YIKES\EasyForms\CustomPostType\EasyForms; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Plugin. |
|
19
|
|
|
* |
|
20
|
|
|
* Main plugin controller class that hooks the plugin's functionality into the |
|
21
|
|
|
* WordPress request lifecycle. |
|
22
|
|
|
* |
|
23
|
|
|
* @since %VERSION% |
|
24
|
|
|
* |
|
25
|
|
|
* @package YIKES\EasyForms |
|
26
|
|
|
* @author Freddie Mixell |
|
27
|
|
|
*/ |
|
28
|
|
|
final class Plugin implements Registerable { |
|
29
|
|
|
|
|
30
|
|
|
use PluginHelper; |
|
31
|
|
|
|
|
32
|
|
|
const VERSION = '7.0.0'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Assets handler instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @since %VERSION% |
|
38
|
|
|
* |
|
39
|
|
|
* @var AssetsHandler |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $assets_handler; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Container instance. |
|
45
|
|
|
* |
|
46
|
|
|
* @since %VERSION% |
|
47
|
|
|
* @var Container |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $container; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Array of registered services. |
|
53
|
|
|
* |
|
54
|
|
|
* @since %VERSION% |
|
55
|
|
|
* @var Service[] |
|
56
|
|
|
*/ |
|
57
|
|
|
private $services = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Instantiate a Plugin object. |
|
61
|
|
|
* |
|
62
|
|
|
* @since %VERSION% |
|
63
|
|
|
* |
|
64
|
|
|
* @param Container $container The container object. |
|
65
|
|
|
* @param AssetsHandler $assets_handler Optional. Instance of the assets handler to use. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct( Container $container, AssetsHandler $assets_handler = null ) { |
|
68
|
|
|
$this->container = $container; |
|
69
|
|
|
$this->assets_handler = $assets_handler ?: new AssetsHandler(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Register the plugin with the WordPress system. |
|
74
|
|
|
* |
|
75
|
|
|
* @since %VERSION% |
|
76
|
|
|
*/ |
|
77
|
|
|
public function register() { |
|
78
|
|
|
add_action( 'init', [ $this, 'register_assets_handler' ] ); |
|
79
|
|
|
register_activation_hook( $this->get_main_file(), [ $this, 'activate' ] ); |
|
80
|
|
|
register_deactivation_hook( $this->get_main_file(), [ $this, 'deactivate' ] ); |
|
81
|
|
|
|
|
82
|
|
|
add_action( 'plugins_loaded', [ $this, 'register_services' ], 20 ); |
|
83
|
|
|
add_action( 'plugins_loaded', function() { |
|
84
|
|
|
/** |
|
85
|
|
|
* Fires after the Easy Forms plugin has been loaded. |
|
86
|
|
|
* |
|
87
|
|
|
* This runs on the plugins_loaded hook so that other plugins have a chance to hook |
|
88
|
|
|
* in. It also runs on an early priority of 0 so that other plugins hooking in have |
|
89
|
|
|
* a chance to modify our early filters. |
|
90
|
|
|
* |
|
91
|
|
|
* @since %VERSION% |
|
92
|
|
|
* |
|
93
|
|
|
* @param Plugin $emf_plugin The main plugin instance. |
|
94
|
|
|
*/ |
|
95
|
|
|
do_action( 'emf_loaded', $this ); |
|
96
|
|
|
}, 0 ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Run activation logic. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function activate() { |
|
103
|
|
|
$this->register_services(); |
|
104
|
|
|
foreach ( $this->services as $service ) { |
|
105
|
|
|
if ( $service instanceof Activateable ) { |
|
106
|
|
|
$service->activate(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
flush_rewrite_rules(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Run deactivation logic. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function deactivate() { |
|
117
|
|
|
foreach ( $this->services as $service ) { |
|
118
|
|
|
if ( $service instanceof Deactivateable ) { |
|
119
|
|
|
$service->deactivate(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Register the individual services of this plugin. |
|
126
|
|
|
* |
|
127
|
|
|
* @since %VERSION% |
|
128
|
|
|
*/ |
|
129
|
|
|
public function register_services() { |
|
130
|
|
|
$services = $this->get_services(); |
|
131
|
|
|
$services = array_map( [ $this, 'instantiate_service' ], $services ); |
|
132
|
|
|
array_walk( $services, function( Service $service ) { |
|
133
|
|
|
$service->register(); |
|
134
|
|
|
} ); |
|
135
|
|
|
$this->services = $services; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get the list of services to register. |
|
140
|
|
|
* |
|
141
|
|
|
* @since %VERSION% |
|
142
|
|
|
* |
|
143
|
|
|
* @return string[] Array of fully qualified class names. |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function get_services() { |
|
146
|
|
|
/** |
|
147
|
|
|
* Fires right before the Easy Forms services are retrieved. |
|
148
|
|
|
* |
|
149
|
|
|
* @param Container $container The services container object. |
|
150
|
|
|
*/ |
|
151
|
|
|
do_action( 'emf_pre_get_services', $this->container ); |
|
152
|
|
|
|
|
153
|
|
|
return array_keys( $this->container->get_services() ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Register the assets handler. |
|
158
|
|
|
* |
|
159
|
|
|
* @since %VERSION% |
|
160
|
|
|
*/ |
|
161
|
|
|
public function register_assets_handler() { |
|
162
|
|
|
$this->assets_handler->register(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Return the instance of the assets handler in use. |
|
167
|
|
|
* |
|
168
|
|
|
* @since %VERSION% |
|
169
|
|
|
* |
|
170
|
|
|
* @return AssetsHandler |
|
171
|
|
|
*/ |
|
172
|
|
|
public function get_assets_handler() { |
|
173
|
|
|
return $this->assets_handler; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Instantiate a single service. |
|
178
|
|
|
* |
|
179
|
|
|
* @since %VERSION% |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $class Service class to instantiate. |
|
182
|
|
|
* |
|
183
|
|
|
* @return Service |
|
184
|
|
|
* @throws Exception\InvalidService If the service is not valid. |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function instantiate_service( $class ) { |
|
187
|
|
|
if ( ! class_exists( $class ) ) { |
|
188
|
|
|
throw InvalidClass::not_found( $class ); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$service = new $class(); |
|
192
|
|
|
|
|
193
|
|
|
if ( ! ( $service instanceof Service ) ) { |
|
194
|
|
|
throw InvalidClass::from_interface( $class, Service::class ); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if ( $service instanceof AssetsAware ) { |
|
198
|
|
|
$service->with_assets_handler( $this->assets_handler ); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $service; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|