@@ -20,922 +20,922 @@ |
||
20 | 20 | */ |
21 | 21 | abstract class EE_Addon extends EE_Configurable implements RequiresDependencyMapInterface, RequiresDomainInterface |
22 | 22 | { |
23 | - /** |
|
24 | - * prefix to be added onto an addon's plugin slug to make a wp option name |
|
25 | - * which will be used to store the plugin's activation history |
|
26 | - */ |
|
27 | - const ee_addon_version_history_option_prefix = 'ee_version_history_'; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var $_version |
|
31 | - * @type string |
|
32 | - */ |
|
33 | - protected $_version = ''; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var $_min_core_version |
|
37 | - * @type string |
|
38 | - */ |
|
39 | - protected $_min_core_version = ''; |
|
40 | - |
|
41 | - /** |
|
42 | - * derived from plugin 'main_file_path using plugin_basename() |
|
43 | - * |
|
44 | - * @type string $_plugin_basename |
|
45 | - */ |
|
46 | - protected $_plugin_basename = ''; |
|
47 | - |
|
48 | - /** |
|
49 | - * A non-internationalized name to identify this addon for use in URLs, etc |
|
50 | - * |
|
51 | - * @type string $_plugin_slug |
|
52 | - */ |
|
53 | - protected $_plugin_slug = ''; |
|
54 | - |
|
55 | - /** |
|
56 | - * A non-internationalized name to identify this addon. Eg 'Calendar','MailChimp',etc/ |
|
57 | - * |
|
58 | - * @type string _addon_name |
|
59 | - */ |
|
60 | - protected $_addon_name = ''; |
|
61 | - |
|
62 | - /** |
|
63 | - * one of the EE_System::req_type_* constants |
|
64 | - * |
|
65 | - * @type int $_req_type |
|
66 | - */ |
|
67 | - protected $_req_type; |
|
68 | - |
|
69 | - /** |
|
70 | - * page slug to be used when generating the "Settings" link on the WP plugin page |
|
71 | - * |
|
72 | - * @type string $_plugin_action_slug |
|
73 | - */ |
|
74 | - protected $_plugin_action_slug = ''; |
|
75 | - |
|
76 | - /** |
|
77 | - * if not empty, inserts a new table row after this plugin's row on the WP Plugins page |
|
78 | - * that can be used for adding upgrading/marketing info |
|
79 | - * |
|
80 | - * @type array $_plugins_page_row |
|
81 | - */ |
|
82 | - protected $_plugins_page_row = []; |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * filepath to the main file, which can be used for register_activation_hook, register_deactivation_hook, etc. |
|
87 | - * |
|
88 | - * @type string |
|
89 | - */ |
|
90 | - protected $_main_plugin_file; |
|
91 | - |
|
92 | - /** |
|
93 | - * This is the slug used to identify this add-on within the plugin update engine. |
|
94 | - * |
|
95 | - * @type string |
|
96 | - */ |
|
97 | - protected $pue_slug = ''; |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @var EE_Dependency_Map $dependency_map |
|
102 | - */ |
|
103 | - private $dependency_map; |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * @var DomainInterface $domain |
|
108 | - */ |
|
109 | - private $domain; |
|
110 | - |
|
111 | - |
|
112 | - private string $payment_method_slug = ''; |
|
113 | - |
|
114 | - private bool $is_payment_method = false; |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * @param EE_Dependency_Map|null $dependency_map [optional] |
|
119 | - * @param DomainInterface|null $domain [optional] |
|
120 | - */ |
|
121 | - public function __construct(EE_Dependency_Map $dependency_map = null, DomainInterface $domain = null) |
|
122 | - { |
|
123 | - if ($dependency_map instanceof EE_Dependency_Map) { |
|
124 | - $this->setDependencyMap($dependency_map); |
|
125 | - } |
|
126 | - if ($domain instanceof DomainInterface) { |
|
127 | - $this->setDomain($domain); |
|
128 | - } |
|
129 | - add_action('AHEE__EE_System__load_controllers__load_admin_controllers', [$this, 'admin_init']); |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * @param EE_Dependency_Map $dependency_map |
|
135 | - */ |
|
136 | - public function setDependencyMap($dependency_map) |
|
137 | - { |
|
138 | - $this->dependency_map = $dependency_map; |
|
139 | - } |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * @return EE_Dependency_Map |
|
144 | - */ |
|
145 | - public function dependencyMap(): ?EE_Dependency_Map |
|
146 | - { |
|
147 | - return $this->dependency_map; |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * @param DomainInterface $domain |
|
153 | - */ |
|
154 | - public function setDomain(DomainInterface $domain) |
|
155 | - { |
|
156 | - $this->domain = $domain; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * @return DomainInterface |
|
162 | - */ |
|
163 | - public function domain(): ?DomainInterface |
|
164 | - { |
|
165 | - return $this->domain; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * @param string $version |
|
171 | - */ |
|
172 | - public function set_version(string $version = '') |
|
173 | - { |
|
174 | - $this->_version = $version; |
|
175 | - } |
|
176 | - |
|
177 | - |
|
178 | - /** |
|
179 | - * get__version |
|
180 | - * |
|
181 | - * @return string |
|
182 | - */ |
|
183 | - public function version(): string |
|
184 | - { |
|
185 | - return $this->_version; |
|
186 | - } |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * @param mixed $min_core_version |
|
191 | - */ |
|
192 | - public function set_min_core_version($min_core_version = null) |
|
193 | - { |
|
194 | - $this->_min_core_version = $min_core_version; |
|
195 | - } |
|
23 | + /** |
|
24 | + * prefix to be added onto an addon's plugin slug to make a wp option name |
|
25 | + * which will be used to store the plugin's activation history |
|
26 | + */ |
|
27 | + const ee_addon_version_history_option_prefix = 'ee_version_history_'; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var $_version |
|
31 | + * @type string |
|
32 | + */ |
|
33 | + protected $_version = ''; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var $_min_core_version |
|
37 | + * @type string |
|
38 | + */ |
|
39 | + protected $_min_core_version = ''; |
|
40 | + |
|
41 | + /** |
|
42 | + * derived from plugin 'main_file_path using plugin_basename() |
|
43 | + * |
|
44 | + * @type string $_plugin_basename |
|
45 | + */ |
|
46 | + protected $_plugin_basename = ''; |
|
47 | + |
|
48 | + /** |
|
49 | + * A non-internationalized name to identify this addon for use in URLs, etc |
|
50 | + * |
|
51 | + * @type string $_plugin_slug |
|
52 | + */ |
|
53 | + protected $_plugin_slug = ''; |
|
54 | + |
|
55 | + /** |
|
56 | + * A non-internationalized name to identify this addon. Eg 'Calendar','MailChimp',etc/ |
|
57 | + * |
|
58 | + * @type string _addon_name |
|
59 | + */ |
|
60 | + protected $_addon_name = ''; |
|
61 | + |
|
62 | + /** |
|
63 | + * one of the EE_System::req_type_* constants |
|
64 | + * |
|
65 | + * @type int $_req_type |
|
66 | + */ |
|
67 | + protected $_req_type; |
|
68 | + |
|
69 | + /** |
|
70 | + * page slug to be used when generating the "Settings" link on the WP plugin page |
|
71 | + * |
|
72 | + * @type string $_plugin_action_slug |
|
73 | + */ |
|
74 | + protected $_plugin_action_slug = ''; |
|
75 | + |
|
76 | + /** |
|
77 | + * if not empty, inserts a new table row after this plugin's row on the WP Plugins page |
|
78 | + * that can be used for adding upgrading/marketing info |
|
79 | + * |
|
80 | + * @type array $_plugins_page_row |
|
81 | + */ |
|
82 | + protected $_plugins_page_row = []; |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * filepath to the main file, which can be used for register_activation_hook, register_deactivation_hook, etc. |
|
87 | + * |
|
88 | + * @type string |
|
89 | + */ |
|
90 | + protected $_main_plugin_file; |
|
91 | + |
|
92 | + /** |
|
93 | + * This is the slug used to identify this add-on within the plugin update engine. |
|
94 | + * |
|
95 | + * @type string |
|
96 | + */ |
|
97 | + protected $pue_slug = ''; |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @var EE_Dependency_Map $dependency_map |
|
102 | + */ |
|
103 | + private $dependency_map; |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * @var DomainInterface $domain |
|
108 | + */ |
|
109 | + private $domain; |
|
110 | + |
|
111 | + |
|
112 | + private string $payment_method_slug = ''; |
|
113 | + |
|
114 | + private bool $is_payment_method = false; |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * @param EE_Dependency_Map|null $dependency_map [optional] |
|
119 | + * @param DomainInterface|null $domain [optional] |
|
120 | + */ |
|
121 | + public function __construct(EE_Dependency_Map $dependency_map = null, DomainInterface $domain = null) |
|
122 | + { |
|
123 | + if ($dependency_map instanceof EE_Dependency_Map) { |
|
124 | + $this->setDependencyMap($dependency_map); |
|
125 | + } |
|
126 | + if ($domain instanceof DomainInterface) { |
|
127 | + $this->setDomain($domain); |
|
128 | + } |
|
129 | + add_action('AHEE__EE_System__load_controllers__load_admin_controllers', [$this, 'admin_init']); |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * @param EE_Dependency_Map $dependency_map |
|
135 | + */ |
|
136 | + public function setDependencyMap($dependency_map) |
|
137 | + { |
|
138 | + $this->dependency_map = $dependency_map; |
|
139 | + } |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * @return EE_Dependency_Map |
|
144 | + */ |
|
145 | + public function dependencyMap(): ?EE_Dependency_Map |
|
146 | + { |
|
147 | + return $this->dependency_map; |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * @param DomainInterface $domain |
|
153 | + */ |
|
154 | + public function setDomain(DomainInterface $domain) |
|
155 | + { |
|
156 | + $this->domain = $domain; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * @return DomainInterface |
|
162 | + */ |
|
163 | + public function domain(): ?DomainInterface |
|
164 | + { |
|
165 | + return $this->domain; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * @param string $version |
|
171 | + */ |
|
172 | + public function set_version(string $version = '') |
|
173 | + { |
|
174 | + $this->_version = $version; |
|
175 | + } |
|
176 | + |
|
177 | + |
|
178 | + /** |
|
179 | + * get__version |
|
180 | + * |
|
181 | + * @return string |
|
182 | + */ |
|
183 | + public function version(): string |
|
184 | + { |
|
185 | + return $this->_version; |
|
186 | + } |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * @param mixed $min_core_version |
|
191 | + */ |
|
192 | + public function set_min_core_version($min_core_version = null) |
|
193 | + { |
|
194 | + $this->_min_core_version = $min_core_version; |
|
195 | + } |
|
196 | 196 | |
197 | 197 | |
198 | - /** |
|
199 | - * get__min_core_version |
|
200 | - * |
|
201 | - * @return string |
|
202 | - */ |
|
203 | - public function min_core_version(): string |
|
204 | - { |
|
205 | - return $this->_min_core_version; |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * Sets addon_name |
|
211 | - * |
|
212 | - * @param string $addon_name |
|
213 | - */ |
|
214 | - public function set_name(string $addon_name) |
|
215 | - { |
|
216 | - $this->_addon_name = $addon_name; |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * Gets addon_name |
|
222 | - * |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - public function name() |
|
226 | - { |
|
227 | - return $this->_addon_name; |
|
228 | - } |
|
229 | - |
|
230 | - |
|
231 | - /** |
|
232 | - * @return string |
|
233 | - */ |
|
234 | - public function plugin_basename(): string |
|
235 | - { |
|
236 | - |
|
237 | - return $this->_plugin_basename; |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * @param string $plugin_basename |
|
243 | - */ |
|
244 | - public function set_plugin_basename(string $plugin_basename) |
|
245 | - { |
|
246 | - |
|
247 | - $this->_plugin_basename = $plugin_basename; |
|
248 | - } |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * @return string |
|
253 | - */ |
|
254 | - public function plugin_slug(): string |
|
255 | - { |
|
256 | - |
|
257 | - return $this->_plugin_slug; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * @param string $plugin_slug |
|
263 | - */ |
|
264 | - public function set_plugin_slug(string $plugin_slug) |
|
265 | - { |
|
266 | - |
|
267 | - $this->_plugin_slug = $plugin_slug; |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * @return string |
|
273 | - */ |
|
274 | - public function plugin_action_slug(): string |
|
275 | - { |
|
276 | - |
|
277 | - return $this->_plugin_action_slug; |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * @param string $plugin_action_slug |
|
283 | - */ |
|
284 | - public function set_plugin_action_slug(string $plugin_action_slug) |
|
285 | - { |
|
286 | - |
|
287 | - $this->_plugin_action_slug = $plugin_action_slug; |
|
288 | - } |
|
289 | - |
|
290 | - |
|
291 | - /** |
|
292 | - * @return array |
|
293 | - */ |
|
294 | - public function get_plugins_page_row(): array |
|
295 | - { |
|
296 | - |
|
297 | - return $this->_plugins_page_row; |
|
298 | - } |
|
299 | - |
|
300 | - |
|
301 | - /** |
|
302 | - * @param array|string $plugins_page_row |
|
303 | - */ |
|
304 | - public function set_plugins_page_row(array $plugins_page_row = []) |
|
305 | - { |
|
306 | - // sigh.... check for example content that I stupidly merged to master and remove it if found |
|
307 | - if ( |
|
308 | - ! is_array($plugins_page_row) |
|
309 | - && strpos($plugins_page_row, '<h3>Promotions Addon Upsell Info</h3>') !== false |
|
310 | - ) { |
|
311 | - $plugins_page_row = []; |
|
312 | - } |
|
313 | - $this->_plugins_page_row = (array) $plugins_page_row; |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * Called when EE core detects this addon has been activated for the first time. |
|
319 | - * If the site isn't in maintenance mode, should setup the addon's database |
|
320 | - * |
|
321 | - * @return void |
|
322 | - * @throws EE_Error |
|
323 | - */ |
|
324 | - public function new_install() |
|
325 | - { |
|
326 | - $classname = get_class($this); |
|
327 | - do_action("AHEE__{$classname}__new_install"); |
|
328 | - do_action('AHEE__EE_Addon__new_install', $this); |
|
329 | - EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
|
330 | - add_action( |
|
331 | - 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
332 | - [$this, 'initialize_db_if_no_migrations_required'] |
|
333 | - ); |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * Called when EE core detects this addon has been reactivated. When this happens, |
|
339 | - * it's good to just check that your data is still intact |
|
340 | - * |
|
341 | - * @return void |
|
342 | - * @throws EE_Error |
|
343 | - */ |
|
344 | - public function reactivation() |
|
345 | - { |
|
346 | - $classname = get_class($this); |
|
347 | - do_action("AHEE__{$classname}__reactivation"); |
|
348 | - do_action('AHEE__EE_Addon__reactivation', $this); |
|
349 | - EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
|
350 | - add_action( |
|
351 | - 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
352 | - [$this, 'initialize_db_if_no_migrations_required'] |
|
353 | - ); |
|
354 | - } |
|
355 | - |
|
356 | - |
|
357 | - /** |
|
358 | - * Called when the registered deactivation hook for this addon fires. |
|
359 | - * |
|
360 | - * @throws EE_Error |
|
361 | - */ |
|
362 | - public function deactivation() |
|
363 | - { |
|
364 | - $classname = get_class($this); |
|
365 | - do_action("AHEE__{$classname}__deactivation"); |
|
366 | - do_action('AHEE__EE_Addon__deactivation', $this); |
|
367 | - // check if the site no longer needs to be in maintenance mode |
|
368 | - EE_Register_Addon::deregister($this->name()); |
|
369 | - EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
|
370 | - } |
|
371 | - |
|
372 | - |
|
373 | - /** |
|
374 | - * Takes care of double-checking that we're not in maintenance mode, and then |
|
375 | - * initializing this addon's necessary initial data. This is called by default on new activations |
|
376 | - * and reactivations. |
|
377 | - * |
|
378 | - * @param bool $verify_schema whether to verify the database's schema for this addon, or just its data. |
|
379 | - * This is a resource-intensive job so we prefer to only do it when necessary |
|
380 | - * @return void |
|
381 | - * @throws EE_Error |
|
382 | - * @throws InvalidInterfaceException |
|
383 | - * @throws InvalidDataTypeException |
|
384 | - * @throws InvalidArgumentException |
|
385 | - * @throws ReflectionException |
|
386 | - */ |
|
387 | - public function initialize_db_if_no_migrations_required($verify_schema = true) |
|
388 | - { |
|
389 | - if ($verify_schema === '') { |
|
390 | - // wp core bug imo: if no args are passed to `do_action('some_hook_name')` besides the hook's name |
|
391 | - // (ie, no 2nd or 3rd arguments), instead of calling the registered callbacks with no arguments, it |
|
392 | - // calls them with an argument of an empty string (ie ""), which evaluates to false |
|
393 | - // so we need to treat the empty string as if nothing had been passed, and should instead use the default |
|
394 | - $verify_schema = true; |
|
395 | - } |
|
396 | - if (DbStatus::isOnline()) { |
|
397 | - if ($verify_schema) { |
|
398 | - $this->initialize_db(); |
|
399 | - } |
|
400 | - $this->initialize_default_data(); |
|
401 | - // @todo: this will probably need to be adjusted in 4.4 as the array changed formats I believe |
|
402 | - EE_Data_Migration_Manager::instance()->update_current_database_state_to( |
|
403 | - [ |
|
404 | - 'slug' => $this->name(), |
|
405 | - 'version' => $this->version(), |
|
406 | - ] |
|
407 | - ); |
|
408 | - /* make sure core's data is a-ok |
|
198 | + /** |
|
199 | + * get__min_core_version |
|
200 | + * |
|
201 | + * @return string |
|
202 | + */ |
|
203 | + public function min_core_version(): string |
|
204 | + { |
|
205 | + return $this->_min_core_version; |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * Sets addon_name |
|
211 | + * |
|
212 | + * @param string $addon_name |
|
213 | + */ |
|
214 | + public function set_name(string $addon_name) |
|
215 | + { |
|
216 | + $this->_addon_name = $addon_name; |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * Gets addon_name |
|
222 | + * |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + public function name() |
|
226 | + { |
|
227 | + return $this->_addon_name; |
|
228 | + } |
|
229 | + |
|
230 | + |
|
231 | + /** |
|
232 | + * @return string |
|
233 | + */ |
|
234 | + public function plugin_basename(): string |
|
235 | + { |
|
236 | + |
|
237 | + return $this->_plugin_basename; |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * @param string $plugin_basename |
|
243 | + */ |
|
244 | + public function set_plugin_basename(string $plugin_basename) |
|
245 | + { |
|
246 | + |
|
247 | + $this->_plugin_basename = $plugin_basename; |
|
248 | + } |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * @return string |
|
253 | + */ |
|
254 | + public function plugin_slug(): string |
|
255 | + { |
|
256 | + |
|
257 | + return $this->_plugin_slug; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * @param string $plugin_slug |
|
263 | + */ |
|
264 | + public function set_plugin_slug(string $plugin_slug) |
|
265 | + { |
|
266 | + |
|
267 | + $this->_plugin_slug = $plugin_slug; |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * @return string |
|
273 | + */ |
|
274 | + public function plugin_action_slug(): string |
|
275 | + { |
|
276 | + |
|
277 | + return $this->_plugin_action_slug; |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * @param string $plugin_action_slug |
|
283 | + */ |
|
284 | + public function set_plugin_action_slug(string $plugin_action_slug) |
|
285 | + { |
|
286 | + |
|
287 | + $this->_plugin_action_slug = $plugin_action_slug; |
|
288 | + } |
|
289 | + |
|
290 | + |
|
291 | + /** |
|
292 | + * @return array |
|
293 | + */ |
|
294 | + public function get_plugins_page_row(): array |
|
295 | + { |
|
296 | + |
|
297 | + return $this->_plugins_page_row; |
|
298 | + } |
|
299 | + |
|
300 | + |
|
301 | + /** |
|
302 | + * @param array|string $plugins_page_row |
|
303 | + */ |
|
304 | + public function set_plugins_page_row(array $plugins_page_row = []) |
|
305 | + { |
|
306 | + // sigh.... check for example content that I stupidly merged to master and remove it if found |
|
307 | + if ( |
|
308 | + ! is_array($plugins_page_row) |
|
309 | + && strpos($plugins_page_row, '<h3>Promotions Addon Upsell Info</h3>') !== false |
|
310 | + ) { |
|
311 | + $plugins_page_row = []; |
|
312 | + } |
|
313 | + $this->_plugins_page_row = (array) $plugins_page_row; |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * Called when EE core detects this addon has been activated for the first time. |
|
319 | + * If the site isn't in maintenance mode, should setup the addon's database |
|
320 | + * |
|
321 | + * @return void |
|
322 | + * @throws EE_Error |
|
323 | + */ |
|
324 | + public function new_install() |
|
325 | + { |
|
326 | + $classname = get_class($this); |
|
327 | + do_action("AHEE__{$classname}__new_install"); |
|
328 | + do_action('AHEE__EE_Addon__new_install', $this); |
|
329 | + EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
|
330 | + add_action( |
|
331 | + 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
332 | + [$this, 'initialize_db_if_no_migrations_required'] |
|
333 | + ); |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * Called when EE core detects this addon has been reactivated. When this happens, |
|
339 | + * it's good to just check that your data is still intact |
|
340 | + * |
|
341 | + * @return void |
|
342 | + * @throws EE_Error |
|
343 | + */ |
|
344 | + public function reactivation() |
|
345 | + { |
|
346 | + $classname = get_class($this); |
|
347 | + do_action("AHEE__{$classname}__reactivation"); |
|
348 | + do_action('AHEE__EE_Addon__reactivation', $this); |
|
349 | + EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
|
350 | + add_action( |
|
351 | + 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
352 | + [$this, 'initialize_db_if_no_migrations_required'] |
|
353 | + ); |
|
354 | + } |
|
355 | + |
|
356 | + |
|
357 | + /** |
|
358 | + * Called when the registered deactivation hook for this addon fires. |
|
359 | + * |
|
360 | + * @throws EE_Error |
|
361 | + */ |
|
362 | + public function deactivation() |
|
363 | + { |
|
364 | + $classname = get_class($this); |
|
365 | + do_action("AHEE__{$classname}__deactivation"); |
|
366 | + do_action('AHEE__EE_Addon__deactivation', $this); |
|
367 | + // check if the site no longer needs to be in maintenance mode |
|
368 | + EE_Register_Addon::deregister($this->name()); |
|
369 | + EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
|
370 | + } |
|
371 | + |
|
372 | + |
|
373 | + /** |
|
374 | + * Takes care of double-checking that we're not in maintenance mode, and then |
|
375 | + * initializing this addon's necessary initial data. This is called by default on new activations |
|
376 | + * and reactivations. |
|
377 | + * |
|
378 | + * @param bool $verify_schema whether to verify the database's schema for this addon, or just its data. |
|
379 | + * This is a resource-intensive job so we prefer to only do it when necessary |
|
380 | + * @return void |
|
381 | + * @throws EE_Error |
|
382 | + * @throws InvalidInterfaceException |
|
383 | + * @throws InvalidDataTypeException |
|
384 | + * @throws InvalidArgumentException |
|
385 | + * @throws ReflectionException |
|
386 | + */ |
|
387 | + public function initialize_db_if_no_migrations_required($verify_schema = true) |
|
388 | + { |
|
389 | + if ($verify_schema === '') { |
|
390 | + // wp core bug imo: if no args are passed to `do_action('some_hook_name')` besides the hook's name |
|
391 | + // (ie, no 2nd or 3rd arguments), instead of calling the registered callbacks with no arguments, it |
|
392 | + // calls them with an argument of an empty string (ie ""), which evaluates to false |
|
393 | + // so we need to treat the empty string as if nothing had been passed, and should instead use the default |
|
394 | + $verify_schema = true; |
|
395 | + } |
|
396 | + if (DbStatus::isOnline()) { |
|
397 | + if ($verify_schema) { |
|
398 | + $this->initialize_db(); |
|
399 | + } |
|
400 | + $this->initialize_default_data(); |
|
401 | + // @todo: this will probably need to be adjusted in 4.4 as the array changed formats I believe |
|
402 | + EE_Data_Migration_Manager::instance()->update_current_database_state_to( |
|
403 | + [ |
|
404 | + 'slug' => $this->name(), |
|
405 | + 'version' => $this->version(), |
|
406 | + ] |
|
407 | + ); |
|
408 | + /* make sure core's data is a-ok |
|
409 | 409 | * (at the time of writing, we especially want to verify all the caps are present |
410 | 410 | * because payment method type capabilities are added dynamically, and it's |
411 | 411 | * possible this addon added a payment method. But it's also possible |
412 | 412 | * other data needs to be verified) |
413 | 413 | */ |
414 | - EEH_Activation::initialize_db_content(); |
|
415 | - /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
416 | - $rewrite_rules = LoaderFactory::getLoader()->getShared( |
|
417 | - 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
418 | - ); |
|
419 | - $rewrite_rules->flushRewriteRules(); |
|
420 | - // in case there are lots of addons being activated at once, let's force garbage collection |
|
421 | - // to help avoid memory limit errors |
|
422 | - // EEH_Debug_Tools::instance()->measure_memory( 'db content initialized for ' . get_class( $this), true ); |
|
423 | - gc_collect_cycles(); |
|
424 | - } else { |
|
425 | - // ask the data migration manager to init this addon's data |
|
426 | - // when migrations are finished because we can't do it now |
|
427 | - EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for($this->name()); |
|
428 | - } |
|
429 | - } |
|
430 | - |
|
431 | - |
|
432 | - /** |
|
433 | - * Used to setup this addon's database tables, but not necessarily any default |
|
434 | - * data in them. The default is to actually use the most up-to-date data migration script |
|
435 | - * for this addon, and just use its schema_changes_before_migration() and schema_changes_after_migration() |
|
436 | - * methods to setup the db. |
|
437 | - * |
|
438 | - * @throws EE_Error |
|
439 | - * @throws ReflectionException |
|
440 | - */ |
|
441 | - public function initialize_db() |
|
442 | - { |
|
443 | - // find the migration script that sets the database to be compatible with the code |
|
444 | - $current_dms_name = EE_Data_Migration_Manager::instance()->get_most_up_to_date_dms($this->name()); |
|
445 | - if ($current_dms_name) { |
|
446 | - $current_data_migration_script = EE_Registry::instance()->load_dms($current_dms_name); |
|
447 | - $current_data_migration_script->set_migrating(false); |
|
448 | - $current_data_migration_script->schema_changes_before_migration(); |
|
449 | - $current_data_migration_script->schema_changes_after_migration(); |
|
450 | - if ($current_data_migration_script->get_errors()) { |
|
451 | - foreach ($current_data_migration_script->get_errors() as $error) { |
|
452 | - EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__); |
|
453 | - } |
|
454 | - } |
|
455 | - } |
|
456 | - // if not DMS was found that should be ok. This addon just doesn't require any database changes |
|
457 | - EE_Data_Migration_Manager::instance()->update_current_database_state_to( |
|
458 | - [ |
|
459 | - 'slug' => $this->name(), |
|
460 | - 'version' => $this->version(), |
|
461 | - ] |
|
462 | - ); |
|
463 | - } |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * If you want to setup default data for the addon, override this method, and call |
|
468 | - * parent::initialize_default_data() from within it. This is normally called |
|
469 | - * from EE_Addon::initialize_db_if_no_migrations_required(), just after EE_Addon::initialize_db() |
|
470 | - * and should verify default data is present (but this is also called |
|
471 | - * on reactivations and just after migrations, so please verify you actually want |
|
472 | - * to ADD default data, because it may already be present). |
|
473 | - * However, please call this parent (currently it just fires a hook which other |
|
474 | - * addons may be depending on) |
|
475 | - */ |
|
476 | - public function initialize_default_data() |
|
477 | - { |
|
478 | - /** |
|
479 | - * Called when an addon is ensuring its default data is set (possibly called |
|
480 | - * on a reactivation, so first check for the absence of other data before setting |
|
481 | - * default data) |
|
482 | - * |
|
483 | - * @param EE_Addon $addon the addon that called this |
|
484 | - */ |
|
485 | - do_action('AHEE__EE_Addon__initialize_default_data__begin', $this); |
|
486 | - // override to insert default data. It is safe to use the models here |
|
487 | - // because the site should not be in maintenance mode |
|
488 | - } |
|
489 | - |
|
490 | - |
|
491 | - /** |
|
492 | - * EE Core detected that this addon has been upgraded. We should check if there |
|
493 | - * are any new migration scripts, and if so put the site into maintenance mode until |
|
494 | - * they're ran |
|
495 | - * |
|
496 | - * @return void |
|
497 | - * @throws EE_Error |
|
498 | - */ |
|
499 | - public function upgrade() |
|
500 | - { |
|
501 | - $classname = get_class($this); |
|
502 | - do_action("AHEE__{$classname}__upgrade"); |
|
503 | - do_action('AHEE__EE_Addon__upgrade', $this); |
|
504 | - EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
|
505 | - // also it's possible there is new default data that needs to be added |
|
506 | - add_action( |
|
507 | - 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
508 | - [$this, 'initialize_db_if_no_migrations_required'] |
|
509 | - ); |
|
510 | - } |
|
511 | - |
|
512 | - |
|
513 | - /** |
|
514 | - * If Core detects this addon has been downgraded, you may want to invoke some special logic here. |
|
515 | - */ |
|
516 | - public function downgrade() |
|
517 | - { |
|
518 | - $classname = get_class($this); |
|
519 | - do_action("AHEE__{$classname}__downgrade"); |
|
520 | - do_action('AHEE__EE_Addon__downgrade', $this); |
|
521 | - // it's possible there's old default data that needs to be double-checked |
|
522 | - add_action( |
|
523 | - 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
524 | - [$this, 'initialize_db_if_no_migrations_required'] |
|
525 | - ); |
|
526 | - } |
|
527 | - |
|
528 | - |
|
529 | - /** |
|
530 | - * set_db_update_option_name |
|
531 | - * Until we do something better, we'll just check for migration scripts upon |
|
532 | - * plugin activation only. In the future, we'll want to do it on plugin updates too |
|
533 | - * |
|
534 | - * @return bool |
|
535 | - */ |
|
536 | - public function set_db_update_option_name(): bool |
|
537 | - { |
|
538 | - EE_Error::doing_it_wrong( |
|
539 | - __FUNCTION__, |
|
540 | - esc_html__( |
|
541 | - 'EE_Addon::set_db_update_option_name was renamed to EE_Addon::set_activation_indicator_option', |
|
542 | - 'event_espresso' |
|
543 | - ), |
|
544 | - '4.3.0.alpha.016' |
|
545 | - ); |
|
546 | - // let's just handle this on the next request, ok? right now we're just not really ready |
|
547 | - return $this->set_activation_indicator_option(); |
|
548 | - } |
|
549 | - |
|
550 | - |
|
551 | - /** |
|
552 | - * Returns the name of the activation indicator option |
|
553 | - * (an option which is set temporarily to indicate that this addon was just activated) |
|
554 | - * |
|
555 | - * @return string |
|
556 | - * @deprecated since version 4.3.0.alpha.016 |
|
557 | - */ |
|
558 | - public function get_db_update_option_name(): string |
|
559 | - { |
|
560 | - EE_Error::doing_it_wrong( |
|
561 | - __FUNCTION__, |
|
562 | - esc_html__( |
|
563 | - 'EE_Addon::get_db_update_option was renamed to EE_Addon::get_activation_indicator_option_name', |
|
564 | - 'event_espresso' |
|
565 | - ), |
|
566 | - '4.3.0.alpha.016' |
|
567 | - ); |
|
568 | - return $this->get_activation_indicator_option_name(); |
|
569 | - } |
|
570 | - |
|
571 | - |
|
572 | - /** |
|
573 | - * When the addon is activated, this should be called to set a wordpress option that |
|
574 | - * indicates it was activated. This is especially useful for detecting reactivations. |
|
575 | - * |
|
576 | - * @return bool |
|
577 | - */ |
|
578 | - public function set_activation_indicator_option(): bool |
|
579 | - { |
|
580 | - // let's just handle this on the next request, ok? right now we're just not really ready |
|
581 | - return update_option($this->get_activation_indicator_option_name(), true); |
|
582 | - } |
|
583 | - |
|
584 | - |
|
585 | - /** |
|
586 | - * Gets the name of the wp option which is used to temporarily indicate that this addon was activated |
|
587 | - * |
|
588 | - * @return string |
|
589 | - */ |
|
590 | - public function get_activation_indicator_option_name(): string |
|
591 | - { |
|
592 | - return 'ee_activation_' . $this->name(); |
|
593 | - } |
|
594 | - |
|
595 | - |
|
596 | - /** |
|
597 | - * Used by EE_System to set the request type of this addon. Should not be used by addon developers |
|
598 | - * |
|
599 | - * @param int $req_type |
|
600 | - */ |
|
601 | - public function set_req_type(int $req_type) |
|
602 | - { |
|
603 | - $this->_req_type = $req_type; |
|
604 | - } |
|
605 | - |
|
606 | - |
|
607 | - /** |
|
608 | - * Returns the request type of this addon (ie, EE_System::req_type_normal, EE_System::req_type_new_activation, |
|
609 | - * EE_System::req_type_reactivation, EE_System::req_type_upgrade, or EE_System::req_type_downgrade). This is set by |
|
610 | - * EE_System when it is checking for new install or upgrades of addons |
|
611 | - */ |
|
612 | - public function detect_req_type($redetect = false): int |
|
613 | - { |
|
614 | - if ($this->_req_type === null || $redetect) { |
|
615 | - $this->detect_activation_or_upgrade(); |
|
616 | - } |
|
617 | - return $this->_req_type; |
|
618 | - } |
|
619 | - |
|
620 | - |
|
621 | - /** |
|
622 | - * Detects the request type for this addon (whether it was just activated, upgrades, a normal request, etc.) |
|
623 | - * Should only be called once per request |
|
624 | - * |
|
625 | - * @return void |
|
626 | - * @throws EE_Error |
|
627 | - */ |
|
628 | - public function detect_activation_or_upgrade() |
|
629 | - { |
|
630 | - $activation_history_for_addon = $this->get_activation_history(); |
|
631 | - $request_type = EE_System::detect_req_type_given_activation_history( |
|
632 | - $activation_history_for_addon, |
|
633 | - $this->get_activation_indicator_option_name(), |
|
634 | - $this->version() |
|
635 | - ); |
|
636 | - $this->set_req_type($request_type); |
|
637 | - $classname = get_class($this); |
|
638 | - switch ($request_type) { |
|
639 | - case EE_System::req_type_new_activation: |
|
640 | - do_action("AHEE__{$classname}__detect_activations_or_upgrades__new_activation"); |
|
641 | - do_action('AHEE__EE_Addon__detect_activations_or_upgrades__new_activation', $this); |
|
642 | - $this->new_install(); |
|
643 | - $this->update_list_of_installed_versions($activation_history_for_addon); |
|
644 | - break; |
|
645 | - case EE_System::req_type_reactivation: |
|
646 | - do_action("AHEE__{$classname}__detect_activations_or_upgrades__reactivation"); |
|
647 | - do_action('AHEE__EE_Addon__detect_activations_or_upgrades__reactivation', $this); |
|
648 | - $this->reactivation(); |
|
649 | - $this->update_list_of_installed_versions($activation_history_for_addon); |
|
650 | - break; |
|
651 | - case EE_System::req_type_upgrade: |
|
652 | - do_action("AHEE__{$classname}__detect_activations_or_upgrades__upgrade"); |
|
653 | - do_action('AHEE__EE_Addon__detect_activations_or_upgrades__upgrade', $this); |
|
654 | - $this->upgrade(); |
|
655 | - $this->update_list_of_installed_versions($activation_history_for_addon); |
|
656 | - break; |
|
657 | - case EE_System::req_type_downgrade: |
|
658 | - do_action("AHEE__{$classname}__detect_activations_or_upgrades__downgrade"); |
|
659 | - do_action('AHEE__EE_Addon__detect_activations_or_upgrades__downgrade', $this); |
|
660 | - $this->downgrade(); |
|
661 | - $this->update_list_of_installed_versions($activation_history_for_addon); |
|
662 | - break; |
|
663 | - case EE_System::req_type_normal: |
|
664 | - default: |
|
665 | - break; |
|
666 | - } |
|
667 | - |
|
668 | - do_action("AHEE__{$classname}__detect_if_activation_or_upgrade__complete"); |
|
669 | - } |
|
670 | - |
|
671 | - |
|
672 | - /** |
|
673 | - * Updates the version history for this addon |
|
674 | - * |
|
675 | - * @param array $version_history |
|
676 | - * @param string $current_version_to_add |
|
677 | - * @return bool success |
|
678 | - */ |
|
679 | - public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null): bool |
|
680 | - { |
|
681 | - if (! $version_history) { |
|
682 | - $version_history = $this->get_activation_history(); |
|
683 | - } |
|
684 | - if ($current_version_to_add === null) { |
|
685 | - $current_version_to_add = $this->version(); |
|
686 | - } |
|
687 | - $version_history[ $current_version_to_add ][] = date('Y-m-d H:i:s', time()); |
|
688 | - return update_option($this->get_activation_history_option_name(), $version_history); |
|
689 | - } |
|
690 | - |
|
691 | - |
|
692 | - /** |
|
693 | - * Gets the name of the wp option that stores the activation history |
|
694 | - * of this addon |
|
695 | - * |
|
696 | - * @return string |
|
697 | - */ |
|
698 | - public function get_activation_history_option_name(): string |
|
699 | - { |
|
700 | - return self::ee_addon_version_history_option_prefix . $this->name(); |
|
701 | - } |
|
702 | - |
|
703 | - |
|
704 | - /** |
|
705 | - * Gets the wp option which stores the activation history for this addon |
|
706 | - * |
|
707 | - * @return array |
|
708 | - */ |
|
709 | - public function get_activation_history(): array |
|
710 | - { |
|
711 | - return get_option($this->get_activation_history_option_name(), []); |
|
712 | - } |
|
713 | - |
|
714 | - |
|
715 | - /** |
|
716 | - * @param string $config_section |
|
717 | - */ |
|
718 | - public function set_config_section($config_section = '') |
|
719 | - { |
|
720 | - $this->_config_section = ! empty($config_section) ? $config_section : 'addons'; |
|
721 | - } |
|
722 | - |
|
723 | - |
|
724 | - /** |
|
725 | - * Sets the filepath to the main plugin file |
|
726 | - * |
|
727 | - * @param string $filepath |
|
728 | - */ |
|
729 | - public function set_main_plugin_file(string $filepath) |
|
730 | - { |
|
731 | - $this->_main_plugin_file = $filepath; |
|
732 | - } |
|
733 | - |
|
734 | - |
|
735 | - /** |
|
736 | - * gets the filepath to teh main file |
|
737 | - * |
|
738 | - * @return string |
|
739 | - */ |
|
740 | - public function get_main_plugin_file(): string |
|
741 | - { |
|
742 | - return $this->_main_plugin_file; |
|
743 | - } |
|
744 | - |
|
745 | - |
|
746 | - /** |
|
747 | - * Gets the filename (no path) of the main file (the main file loaded |
|
748 | - * by WP) |
|
749 | - * |
|
750 | - * @return string |
|
751 | - */ |
|
752 | - public function get_main_plugin_file_basename(): string |
|
753 | - { |
|
754 | - return plugin_basename($this->get_main_plugin_file()); |
|
755 | - } |
|
756 | - |
|
757 | - |
|
758 | - /** |
|
759 | - * Gets the folder name which contains the main plugin file |
|
760 | - * |
|
761 | - * @return string |
|
762 | - */ |
|
763 | - public function get_main_plugin_file_dirname(): string |
|
764 | - { |
|
765 | - return dirname($this->get_main_plugin_file()); |
|
766 | - } |
|
767 | - |
|
768 | - |
|
769 | - /** |
|
770 | - * sets hooks used in the admin |
|
771 | - * |
|
772 | - * @return void |
|
773 | - */ |
|
774 | - public function admin_init() |
|
775 | - { |
|
776 | - // is admin and not in M-Mode ? |
|
777 | - if (is_admin() && MaintenanceStatus::isDisabled()) { |
|
778 | - add_filter('plugin_action_links', [$this, 'plugin_action_links'], 10, 2); |
|
779 | - add_filter('after_plugin_row_' . $this->_plugin_basename, [$this, 'after_plugin_row'], 10, 3); |
|
780 | - } |
|
781 | - } |
|
782 | - |
|
783 | - |
|
784 | - /** |
|
785 | - * plugin_actions |
|
786 | - * Add a settings link to the Plugins page, so people can go straight from the plugin page to the settings page. |
|
787 | - * |
|
788 | - * @param array $links |
|
789 | - * @param string $file |
|
790 | - * @return array |
|
791 | - */ |
|
792 | - public function plugin_action_links(array $links, string $file): array |
|
793 | - { |
|
794 | - $plugin_action_slug = $this->plugin_action_slug(); |
|
795 | - if ($this->plugin_basename() !== $file || empty($plugin_action_slug)) { |
|
796 | - return $links; |
|
797 | - } |
|
798 | - // ensure the plugin action slug starts with 'espresso_' |
|
799 | - $plugin_action_slug = strpos($plugin_action_slug, 'espresso_') !== 0 |
|
800 | - ? 'espresso_' . $plugin_action_slug |
|
801 | - : $plugin_action_slug; |
|
802 | - |
|
803 | - $plugin_action_link_url = 'admin.php?page=' . $plugin_action_slug; |
|
804 | - if ($this->isPaymentMethod()) { |
|
805 | - $pm_slug = $this->paymentMethodSlug(); |
|
806 | - $plugin_action_link_url = "admin.php?page=espresso_payment_settings&action=default"; |
|
807 | - $plugin_action_link_url .= "&payment_method=$pm_slug#espresso_{$pm_slug}_payment_settings"; |
|
808 | - } |
|
809 | - // filter settings link url and text |
|
810 | - $plugin_action_link_url = apply_filters( |
|
811 | - "FHEE__EE_Addon_plugin_action_links__url", |
|
812 | - $plugin_action_link_url, |
|
813 | - $this->plugin_slug(), |
|
814 | - $file |
|
815 | - ); |
|
816 | - if (! $plugin_action_link_url) { |
|
817 | - return $links; |
|
818 | - } |
|
819 | - $plugin_action_link_text = apply_filters( |
|
820 | - "FHEE__EE_Addon_plugin_action_links__text", |
|
821 | - __('Settings', 'event_espresso'), |
|
822 | - $this->plugin_slug(), |
|
823 | - $file |
|
824 | - ); |
|
825 | - |
|
826 | - // before other links |
|
827 | - array_unshift( |
|
828 | - $links, |
|
829 | - '<a href="' . $plugin_action_link_url . '">' . esc_html($plugin_action_link_text) . '</a>' |
|
830 | - ); |
|
831 | - return $links; |
|
832 | - } |
|
833 | - |
|
834 | - |
|
835 | - /** |
|
836 | - * after_plugin_row |
|
837 | - * Add additional content to the plugins page plugin row |
|
838 | - * Inserts another row |
|
839 | - * |
|
840 | - * @param string $plugin_file |
|
841 | - * @param array $plugin_data |
|
842 | - * @param string $status |
|
843 | - * @return void |
|
844 | - */ |
|
845 | - public function after_plugin_row(string $plugin_file, array $plugin_data, string $status) |
|
846 | - { |
|
847 | - $after_plugin_row = ''; |
|
848 | - $plugins_page_row = $this->get_plugins_page_row(); |
|
849 | - if (! empty($plugins_page_row) && $plugin_file === $this->plugin_basename()) { |
|
850 | - $class = $status ? 'active' : 'inactive'; |
|
851 | - $link_text = $plugins_page_row['link_text'] ?? ''; |
|
852 | - $link_url = $plugins_page_row['link_url'] ?? ''; |
|
853 | - $description = $plugins_page_row['description'] ?? ''; |
|
854 | - if (! empty($link_text) && ! empty($link_url) && ! empty($description)) { |
|
855 | - $after_plugin_row .= '<tr id="' . sanitize_title($plugin_file) . '-ee-addon" class="' . $class . '">'; |
|
856 | - $after_plugin_row .= '<th class="check-column" scope="row"></th>'; |
|
857 | - $after_plugin_row .= '<td class="ee-addon-upsell-info-title-td plugin-title column-primary">'; |
|
858 | - $after_plugin_row .= '<p class="ee-addon-upsell-info-dv"> |
|
414 | + EEH_Activation::initialize_db_content(); |
|
415 | + /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
416 | + $rewrite_rules = LoaderFactory::getLoader()->getShared( |
|
417 | + 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
418 | + ); |
|
419 | + $rewrite_rules->flushRewriteRules(); |
|
420 | + // in case there are lots of addons being activated at once, let's force garbage collection |
|
421 | + // to help avoid memory limit errors |
|
422 | + // EEH_Debug_Tools::instance()->measure_memory( 'db content initialized for ' . get_class( $this), true ); |
|
423 | + gc_collect_cycles(); |
|
424 | + } else { |
|
425 | + // ask the data migration manager to init this addon's data |
|
426 | + // when migrations are finished because we can't do it now |
|
427 | + EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for($this->name()); |
|
428 | + } |
|
429 | + } |
|
430 | + |
|
431 | + |
|
432 | + /** |
|
433 | + * Used to setup this addon's database tables, but not necessarily any default |
|
434 | + * data in them. The default is to actually use the most up-to-date data migration script |
|
435 | + * for this addon, and just use its schema_changes_before_migration() and schema_changes_after_migration() |
|
436 | + * methods to setup the db. |
|
437 | + * |
|
438 | + * @throws EE_Error |
|
439 | + * @throws ReflectionException |
|
440 | + */ |
|
441 | + public function initialize_db() |
|
442 | + { |
|
443 | + // find the migration script that sets the database to be compatible with the code |
|
444 | + $current_dms_name = EE_Data_Migration_Manager::instance()->get_most_up_to_date_dms($this->name()); |
|
445 | + if ($current_dms_name) { |
|
446 | + $current_data_migration_script = EE_Registry::instance()->load_dms($current_dms_name); |
|
447 | + $current_data_migration_script->set_migrating(false); |
|
448 | + $current_data_migration_script->schema_changes_before_migration(); |
|
449 | + $current_data_migration_script->schema_changes_after_migration(); |
|
450 | + if ($current_data_migration_script->get_errors()) { |
|
451 | + foreach ($current_data_migration_script->get_errors() as $error) { |
|
452 | + EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__); |
|
453 | + } |
|
454 | + } |
|
455 | + } |
|
456 | + // if not DMS was found that should be ok. This addon just doesn't require any database changes |
|
457 | + EE_Data_Migration_Manager::instance()->update_current_database_state_to( |
|
458 | + [ |
|
459 | + 'slug' => $this->name(), |
|
460 | + 'version' => $this->version(), |
|
461 | + ] |
|
462 | + ); |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * If you want to setup default data for the addon, override this method, and call |
|
468 | + * parent::initialize_default_data() from within it. This is normally called |
|
469 | + * from EE_Addon::initialize_db_if_no_migrations_required(), just after EE_Addon::initialize_db() |
|
470 | + * and should verify default data is present (but this is also called |
|
471 | + * on reactivations and just after migrations, so please verify you actually want |
|
472 | + * to ADD default data, because it may already be present). |
|
473 | + * However, please call this parent (currently it just fires a hook which other |
|
474 | + * addons may be depending on) |
|
475 | + */ |
|
476 | + public function initialize_default_data() |
|
477 | + { |
|
478 | + /** |
|
479 | + * Called when an addon is ensuring its default data is set (possibly called |
|
480 | + * on a reactivation, so first check for the absence of other data before setting |
|
481 | + * default data) |
|
482 | + * |
|
483 | + * @param EE_Addon $addon the addon that called this |
|
484 | + */ |
|
485 | + do_action('AHEE__EE_Addon__initialize_default_data__begin', $this); |
|
486 | + // override to insert default data. It is safe to use the models here |
|
487 | + // because the site should not be in maintenance mode |
|
488 | + } |
|
489 | + |
|
490 | + |
|
491 | + /** |
|
492 | + * EE Core detected that this addon has been upgraded. We should check if there |
|
493 | + * are any new migration scripts, and if so put the site into maintenance mode until |
|
494 | + * they're ran |
|
495 | + * |
|
496 | + * @return void |
|
497 | + * @throws EE_Error |
|
498 | + */ |
|
499 | + public function upgrade() |
|
500 | + { |
|
501 | + $classname = get_class($this); |
|
502 | + do_action("AHEE__{$classname}__upgrade"); |
|
503 | + do_action('AHEE__EE_Addon__upgrade', $this); |
|
504 | + EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
|
505 | + // also it's possible there is new default data that needs to be added |
|
506 | + add_action( |
|
507 | + 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
508 | + [$this, 'initialize_db_if_no_migrations_required'] |
|
509 | + ); |
|
510 | + } |
|
511 | + |
|
512 | + |
|
513 | + /** |
|
514 | + * If Core detects this addon has been downgraded, you may want to invoke some special logic here. |
|
515 | + */ |
|
516 | + public function downgrade() |
|
517 | + { |
|
518 | + $classname = get_class($this); |
|
519 | + do_action("AHEE__{$classname}__downgrade"); |
|
520 | + do_action('AHEE__EE_Addon__downgrade', $this); |
|
521 | + // it's possible there's old default data that needs to be double-checked |
|
522 | + add_action( |
|
523 | + 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
524 | + [$this, 'initialize_db_if_no_migrations_required'] |
|
525 | + ); |
|
526 | + } |
|
527 | + |
|
528 | + |
|
529 | + /** |
|
530 | + * set_db_update_option_name |
|
531 | + * Until we do something better, we'll just check for migration scripts upon |
|
532 | + * plugin activation only. In the future, we'll want to do it on plugin updates too |
|
533 | + * |
|
534 | + * @return bool |
|
535 | + */ |
|
536 | + public function set_db_update_option_name(): bool |
|
537 | + { |
|
538 | + EE_Error::doing_it_wrong( |
|
539 | + __FUNCTION__, |
|
540 | + esc_html__( |
|
541 | + 'EE_Addon::set_db_update_option_name was renamed to EE_Addon::set_activation_indicator_option', |
|
542 | + 'event_espresso' |
|
543 | + ), |
|
544 | + '4.3.0.alpha.016' |
|
545 | + ); |
|
546 | + // let's just handle this on the next request, ok? right now we're just not really ready |
|
547 | + return $this->set_activation_indicator_option(); |
|
548 | + } |
|
549 | + |
|
550 | + |
|
551 | + /** |
|
552 | + * Returns the name of the activation indicator option |
|
553 | + * (an option which is set temporarily to indicate that this addon was just activated) |
|
554 | + * |
|
555 | + * @return string |
|
556 | + * @deprecated since version 4.3.0.alpha.016 |
|
557 | + */ |
|
558 | + public function get_db_update_option_name(): string |
|
559 | + { |
|
560 | + EE_Error::doing_it_wrong( |
|
561 | + __FUNCTION__, |
|
562 | + esc_html__( |
|
563 | + 'EE_Addon::get_db_update_option was renamed to EE_Addon::get_activation_indicator_option_name', |
|
564 | + 'event_espresso' |
|
565 | + ), |
|
566 | + '4.3.0.alpha.016' |
|
567 | + ); |
|
568 | + return $this->get_activation_indicator_option_name(); |
|
569 | + } |
|
570 | + |
|
571 | + |
|
572 | + /** |
|
573 | + * When the addon is activated, this should be called to set a wordpress option that |
|
574 | + * indicates it was activated. This is especially useful for detecting reactivations. |
|
575 | + * |
|
576 | + * @return bool |
|
577 | + */ |
|
578 | + public function set_activation_indicator_option(): bool |
|
579 | + { |
|
580 | + // let's just handle this on the next request, ok? right now we're just not really ready |
|
581 | + return update_option($this->get_activation_indicator_option_name(), true); |
|
582 | + } |
|
583 | + |
|
584 | + |
|
585 | + /** |
|
586 | + * Gets the name of the wp option which is used to temporarily indicate that this addon was activated |
|
587 | + * |
|
588 | + * @return string |
|
589 | + */ |
|
590 | + public function get_activation_indicator_option_name(): string |
|
591 | + { |
|
592 | + return 'ee_activation_' . $this->name(); |
|
593 | + } |
|
594 | + |
|
595 | + |
|
596 | + /** |
|
597 | + * Used by EE_System to set the request type of this addon. Should not be used by addon developers |
|
598 | + * |
|
599 | + * @param int $req_type |
|
600 | + */ |
|
601 | + public function set_req_type(int $req_type) |
|
602 | + { |
|
603 | + $this->_req_type = $req_type; |
|
604 | + } |
|
605 | + |
|
606 | + |
|
607 | + /** |
|
608 | + * Returns the request type of this addon (ie, EE_System::req_type_normal, EE_System::req_type_new_activation, |
|
609 | + * EE_System::req_type_reactivation, EE_System::req_type_upgrade, or EE_System::req_type_downgrade). This is set by |
|
610 | + * EE_System when it is checking for new install or upgrades of addons |
|
611 | + */ |
|
612 | + public function detect_req_type($redetect = false): int |
|
613 | + { |
|
614 | + if ($this->_req_type === null || $redetect) { |
|
615 | + $this->detect_activation_or_upgrade(); |
|
616 | + } |
|
617 | + return $this->_req_type; |
|
618 | + } |
|
619 | + |
|
620 | + |
|
621 | + /** |
|
622 | + * Detects the request type for this addon (whether it was just activated, upgrades, a normal request, etc.) |
|
623 | + * Should only be called once per request |
|
624 | + * |
|
625 | + * @return void |
|
626 | + * @throws EE_Error |
|
627 | + */ |
|
628 | + public function detect_activation_or_upgrade() |
|
629 | + { |
|
630 | + $activation_history_for_addon = $this->get_activation_history(); |
|
631 | + $request_type = EE_System::detect_req_type_given_activation_history( |
|
632 | + $activation_history_for_addon, |
|
633 | + $this->get_activation_indicator_option_name(), |
|
634 | + $this->version() |
|
635 | + ); |
|
636 | + $this->set_req_type($request_type); |
|
637 | + $classname = get_class($this); |
|
638 | + switch ($request_type) { |
|
639 | + case EE_System::req_type_new_activation: |
|
640 | + do_action("AHEE__{$classname}__detect_activations_or_upgrades__new_activation"); |
|
641 | + do_action('AHEE__EE_Addon__detect_activations_or_upgrades__new_activation', $this); |
|
642 | + $this->new_install(); |
|
643 | + $this->update_list_of_installed_versions($activation_history_for_addon); |
|
644 | + break; |
|
645 | + case EE_System::req_type_reactivation: |
|
646 | + do_action("AHEE__{$classname}__detect_activations_or_upgrades__reactivation"); |
|
647 | + do_action('AHEE__EE_Addon__detect_activations_or_upgrades__reactivation', $this); |
|
648 | + $this->reactivation(); |
|
649 | + $this->update_list_of_installed_versions($activation_history_for_addon); |
|
650 | + break; |
|
651 | + case EE_System::req_type_upgrade: |
|
652 | + do_action("AHEE__{$classname}__detect_activations_or_upgrades__upgrade"); |
|
653 | + do_action('AHEE__EE_Addon__detect_activations_or_upgrades__upgrade', $this); |
|
654 | + $this->upgrade(); |
|
655 | + $this->update_list_of_installed_versions($activation_history_for_addon); |
|
656 | + break; |
|
657 | + case EE_System::req_type_downgrade: |
|
658 | + do_action("AHEE__{$classname}__detect_activations_or_upgrades__downgrade"); |
|
659 | + do_action('AHEE__EE_Addon__detect_activations_or_upgrades__downgrade', $this); |
|
660 | + $this->downgrade(); |
|
661 | + $this->update_list_of_installed_versions($activation_history_for_addon); |
|
662 | + break; |
|
663 | + case EE_System::req_type_normal: |
|
664 | + default: |
|
665 | + break; |
|
666 | + } |
|
667 | + |
|
668 | + do_action("AHEE__{$classname}__detect_if_activation_or_upgrade__complete"); |
|
669 | + } |
|
670 | + |
|
671 | + |
|
672 | + /** |
|
673 | + * Updates the version history for this addon |
|
674 | + * |
|
675 | + * @param array $version_history |
|
676 | + * @param string $current_version_to_add |
|
677 | + * @return bool success |
|
678 | + */ |
|
679 | + public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null): bool |
|
680 | + { |
|
681 | + if (! $version_history) { |
|
682 | + $version_history = $this->get_activation_history(); |
|
683 | + } |
|
684 | + if ($current_version_to_add === null) { |
|
685 | + $current_version_to_add = $this->version(); |
|
686 | + } |
|
687 | + $version_history[ $current_version_to_add ][] = date('Y-m-d H:i:s', time()); |
|
688 | + return update_option($this->get_activation_history_option_name(), $version_history); |
|
689 | + } |
|
690 | + |
|
691 | + |
|
692 | + /** |
|
693 | + * Gets the name of the wp option that stores the activation history |
|
694 | + * of this addon |
|
695 | + * |
|
696 | + * @return string |
|
697 | + */ |
|
698 | + public function get_activation_history_option_name(): string |
|
699 | + { |
|
700 | + return self::ee_addon_version_history_option_prefix . $this->name(); |
|
701 | + } |
|
702 | + |
|
703 | + |
|
704 | + /** |
|
705 | + * Gets the wp option which stores the activation history for this addon |
|
706 | + * |
|
707 | + * @return array |
|
708 | + */ |
|
709 | + public function get_activation_history(): array |
|
710 | + { |
|
711 | + return get_option($this->get_activation_history_option_name(), []); |
|
712 | + } |
|
713 | + |
|
714 | + |
|
715 | + /** |
|
716 | + * @param string $config_section |
|
717 | + */ |
|
718 | + public function set_config_section($config_section = '') |
|
719 | + { |
|
720 | + $this->_config_section = ! empty($config_section) ? $config_section : 'addons'; |
|
721 | + } |
|
722 | + |
|
723 | + |
|
724 | + /** |
|
725 | + * Sets the filepath to the main plugin file |
|
726 | + * |
|
727 | + * @param string $filepath |
|
728 | + */ |
|
729 | + public function set_main_plugin_file(string $filepath) |
|
730 | + { |
|
731 | + $this->_main_plugin_file = $filepath; |
|
732 | + } |
|
733 | + |
|
734 | + |
|
735 | + /** |
|
736 | + * gets the filepath to teh main file |
|
737 | + * |
|
738 | + * @return string |
|
739 | + */ |
|
740 | + public function get_main_plugin_file(): string |
|
741 | + { |
|
742 | + return $this->_main_plugin_file; |
|
743 | + } |
|
744 | + |
|
745 | + |
|
746 | + /** |
|
747 | + * Gets the filename (no path) of the main file (the main file loaded |
|
748 | + * by WP) |
|
749 | + * |
|
750 | + * @return string |
|
751 | + */ |
|
752 | + public function get_main_plugin_file_basename(): string |
|
753 | + { |
|
754 | + return plugin_basename($this->get_main_plugin_file()); |
|
755 | + } |
|
756 | + |
|
757 | + |
|
758 | + /** |
|
759 | + * Gets the folder name which contains the main plugin file |
|
760 | + * |
|
761 | + * @return string |
|
762 | + */ |
|
763 | + public function get_main_plugin_file_dirname(): string |
|
764 | + { |
|
765 | + return dirname($this->get_main_plugin_file()); |
|
766 | + } |
|
767 | + |
|
768 | + |
|
769 | + /** |
|
770 | + * sets hooks used in the admin |
|
771 | + * |
|
772 | + * @return void |
|
773 | + */ |
|
774 | + public function admin_init() |
|
775 | + { |
|
776 | + // is admin and not in M-Mode ? |
|
777 | + if (is_admin() && MaintenanceStatus::isDisabled()) { |
|
778 | + add_filter('plugin_action_links', [$this, 'plugin_action_links'], 10, 2); |
|
779 | + add_filter('after_plugin_row_' . $this->_plugin_basename, [$this, 'after_plugin_row'], 10, 3); |
|
780 | + } |
|
781 | + } |
|
782 | + |
|
783 | + |
|
784 | + /** |
|
785 | + * plugin_actions |
|
786 | + * Add a settings link to the Plugins page, so people can go straight from the plugin page to the settings page. |
|
787 | + * |
|
788 | + * @param array $links |
|
789 | + * @param string $file |
|
790 | + * @return array |
|
791 | + */ |
|
792 | + public function plugin_action_links(array $links, string $file): array |
|
793 | + { |
|
794 | + $plugin_action_slug = $this->plugin_action_slug(); |
|
795 | + if ($this->plugin_basename() !== $file || empty($plugin_action_slug)) { |
|
796 | + return $links; |
|
797 | + } |
|
798 | + // ensure the plugin action slug starts with 'espresso_' |
|
799 | + $plugin_action_slug = strpos($plugin_action_slug, 'espresso_') !== 0 |
|
800 | + ? 'espresso_' . $plugin_action_slug |
|
801 | + : $plugin_action_slug; |
|
802 | + |
|
803 | + $plugin_action_link_url = 'admin.php?page=' . $plugin_action_slug; |
|
804 | + if ($this->isPaymentMethod()) { |
|
805 | + $pm_slug = $this->paymentMethodSlug(); |
|
806 | + $plugin_action_link_url = "admin.php?page=espresso_payment_settings&action=default"; |
|
807 | + $plugin_action_link_url .= "&payment_method=$pm_slug#espresso_{$pm_slug}_payment_settings"; |
|
808 | + } |
|
809 | + // filter settings link url and text |
|
810 | + $plugin_action_link_url = apply_filters( |
|
811 | + "FHEE__EE_Addon_plugin_action_links__url", |
|
812 | + $plugin_action_link_url, |
|
813 | + $this->plugin_slug(), |
|
814 | + $file |
|
815 | + ); |
|
816 | + if (! $plugin_action_link_url) { |
|
817 | + return $links; |
|
818 | + } |
|
819 | + $plugin_action_link_text = apply_filters( |
|
820 | + "FHEE__EE_Addon_plugin_action_links__text", |
|
821 | + __('Settings', 'event_espresso'), |
|
822 | + $this->plugin_slug(), |
|
823 | + $file |
|
824 | + ); |
|
825 | + |
|
826 | + // before other links |
|
827 | + array_unshift( |
|
828 | + $links, |
|
829 | + '<a href="' . $plugin_action_link_url . '">' . esc_html($plugin_action_link_text) . '</a>' |
|
830 | + ); |
|
831 | + return $links; |
|
832 | + } |
|
833 | + |
|
834 | + |
|
835 | + /** |
|
836 | + * after_plugin_row |
|
837 | + * Add additional content to the plugins page plugin row |
|
838 | + * Inserts another row |
|
839 | + * |
|
840 | + * @param string $plugin_file |
|
841 | + * @param array $plugin_data |
|
842 | + * @param string $status |
|
843 | + * @return void |
|
844 | + */ |
|
845 | + public function after_plugin_row(string $plugin_file, array $plugin_data, string $status) |
|
846 | + { |
|
847 | + $after_plugin_row = ''; |
|
848 | + $plugins_page_row = $this->get_plugins_page_row(); |
|
849 | + if (! empty($plugins_page_row) && $plugin_file === $this->plugin_basename()) { |
|
850 | + $class = $status ? 'active' : 'inactive'; |
|
851 | + $link_text = $plugins_page_row['link_text'] ?? ''; |
|
852 | + $link_url = $plugins_page_row['link_url'] ?? ''; |
|
853 | + $description = $plugins_page_row['description'] ?? ''; |
|
854 | + if (! empty($link_text) && ! empty($link_url) && ! empty($description)) { |
|
855 | + $after_plugin_row .= '<tr id="' . sanitize_title($plugin_file) . '-ee-addon" class="' . $class . '">'; |
|
856 | + $after_plugin_row .= '<th class="check-column" scope="row"></th>'; |
|
857 | + $after_plugin_row .= '<td class="ee-addon-upsell-info-title-td plugin-title column-primary">'; |
|
858 | + $after_plugin_row .= '<p class="ee-addon-upsell-info-dv"> |
|
859 | 859 | <a class="button button--primary" href="' . esc_url_raw($link_url) . '">' |
860 | - . $link_text |
|
861 | - . ' <span class="dashicons dashicons-arrow-right-alt2" style="margin:0;"></span>' |
|
862 | - . '</a> |
|
860 | + . $link_text |
|
861 | + . ' <span class="dashicons dashicons-arrow-right-alt2" style="margin:0;"></span>' |
|
862 | + . '</a> |
|
863 | 863 | </p>'; |
864 | - $after_plugin_row .= '</td>'; |
|
865 | - $after_plugin_row .= '<td class="ee-addon-upsell-info-desc-td column-description desc">'; |
|
866 | - $after_plugin_row .= $description; |
|
867 | - $after_plugin_row .= '</td>'; |
|
868 | - $after_plugin_row .= '</tr>'; |
|
869 | - } else { |
|
870 | - $after_plugin_row .= $description; |
|
871 | - } |
|
872 | - } |
|
873 | - |
|
874 | - echo wp_kses($after_plugin_row, AllowedTags::getAllowedTags()); |
|
875 | - } |
|
876 | - |
|
877 | - |
|
878 | - /** |
|
879 | - * A safe space for addons to add additional logic like setting hooks that need to be set early in the request. |
|
880 | - * Child classes that have logic like that to run can override this method declaration. This was not made abstract |
|
881 | - * for back compat reasons. |
|
882 | - * |
|
883 | - * This will fire on the `AHEE__EE_System__load_espresso_addons__complete` hook at priority 999. |
|
884 | - * |
|
885 | - * It is recommended, if client code is `de-registering` an add-on, then do it on the |
|
886 | - * `AHEE__EE_System__load_espresso_addons__complete` hook before priority 999 so as to ensure any code logic in this |
|
887 | - * callback does not get run/set in that request. |
|
888 | - * |
|
889 | - * Also, keep in mind that if a registered add-on happens to be deactivated via |
|
890 | - * EE_System::_deactivate_incompatible_addons() because its incompatible, any code executed in this method |
|
891 | - * (including setting hooks etc) will have executed before the plugin was deactivated. If you use |
|
892 | - * `after_registration` to set any filter and/or action hooks and want to ensure they are removed on this add-on's |
|
893 | - * deactivation, you can override `EE_Addon::deactivation` and unset your hooks and filters there. Just remember |
|
894 | - * to call `parent::deactivation`. |
|
895 | - * |
|
896 | - * @since 4.9.26 |
|
897 | - */ |
|
898 | - public function after_registration() |
|
899 | - { |
|
900 | - // cricket chirp... cricket chirp... |
|
901 | - } |
|
902 | - |
|
903 | - |
|
904 | - /** |
|
905 | - * @return string |
|
906 | - */ |
|
907 | - public function getPueSlug(): string |
|
908 | - { |
|
909 | - return $this->pue_slug; |
|
910 | - } |
|
911 | - |
|
912 | - |
|
913 | - /** |
|
914 | - * @param string $pue_slug |
|
915 | - */ |
|
916 | - public function setPueSlug(string $pue_slug) |
|
917 | - { |
|
918 | - $this->pue_slug = $pue_slug; |
|
919 | - } |
|
920 | - |
|
921 | - |
|
922 | - public function isPaymentMethod(): bool |
|
923 | - { |
|
924 | - return $this->is_payment_method; |
|
925 | - } |
|
926 | - |
|
927 | - |
|
928 | - public function paymentMethodSlug(): string |
|
929 | - { |
|
930 | - return $this->payment_method_slug; |
|
931 | - } |
|
932 | - |
|
933 | - |
|
934 | - public function setPaymentMethodSlug(string $payment_method_slug): void |
|
935 | - { |
|
936 | - $this->payment_method_slug = $payment_method_slug; |
|
937 | - $this->is_payment_method = true; |
|
938 | - } |
|
864 | + $after_plugin_row .= '</td>'; |
|
865 | + $after_plugin_row .= '<td class="ee-addon-upsell-info-desc-td column-description desc">'; |
|
866 | + $after_plugin_row .= $description; |
|
867 | + $after_plugin_row .= '</td>'; |
|
868 | + $after_plugin_row .= '</tr>'; |
|
869 | + } else { |
|
870 | + $after_plugin_row .= $description; |
|
871 | + } |
|
872 | + } |
|
873 | + |
|
874 | + echo wp_kses($after_plugin_row, AllowedTags::getAllowedTags()); |
|
875 | + } |
|
876 | + |
|
877 | + |
|
878 | + /** |
|
879 | + * A safe space for addons to add additional logic like setting hooks that need to be set early in the request. |
|
880 | + * Child classes that have logic like that to run can override this method declaration. This was not made abstract |
|
881 | + * for back compat reasons. |
|
882 | + * |
|
883 | + * This will fire on the `AHEE__EE_System__load_espresso_addons__complete` hook at priority 999. |
|
884 | + * |
|
885 | + * It is recommended, if client code is `de-registering` an add-on, then do it on the |
|
886 | + * `AHEE__EE_System__load_espresso_addons__complete` hook before priority 999 so as to ensure any code logic in this |
|
887 | + * callback does not get run/set in that request. |
|
888 | + * |
|
889 | + * Also, keep in mind that if a registered add-on happens to be deactivated via |
|
890 | + * EE_System::_deactivate_incompatible_addons() because its incompatible, any code executed in this method |
|
891 | + * (including setting hooks etc) will have executed before the plugin was deactivated. If you use |
|
892 | + * `after_registration` to set any filter and/or action hooks and want to ensure they are removed on this add-on's |
|
893 | + * deactivation, you can override `EE_Addon::deactivation` and unset your hooks and filters there. Just remember |
|
894 | + * to call `parent::deactivation`. |
|
895 | + * |
|
896 | + * @since 4.9.26 |
|
897 | + */ |
|
898 | + public function after_registration() |
|
899 | + { |
|
900 | + // cricket chirp... cricket chirp... |
|
901 | + } |
|
902 | + |
|
903 | + |
|
904 | + /** |
|
905 | + * @return string |
|
906 | + */ |
|
907 | + public function getPueSlug(): string |
|
908 | + { |
|
909 | + return $this->pue_slug; |
|
910 | + } |
|
911 | + |
|
912 | + |
|
913 | + /** |
|
914 | + * @param string $pue_slug |
|
915 | + */ |
|
916 | + public function setPueSlug(string $pue_slug) |
|
917 | + { |
|
918 | + $this->pue_slug = $pue_slug; |
|
919 | + } |
|
920 | + |
|
921 | + |
|
922 | + public function isPaymentMethod(): bool |
|
923 | + { |
|
924 | + return $this->is_payment_method; |
|
925 | + } |
|
926 | + |
|
927 | + |
|
928 | + public function paymentMethodSlug(): string |
|
929 | + { |
|
930 | + return $this->payment_method_slug; |
|
931 | + } |
|
932 | + |
|
933 | + |
|
934 | + public function setPaymentMethodSlug(string $payment_method_slug): void |
|
935 | + { |
|
936 | + $this->payment_method_slug = $payment_method_slug; |
|
937 | + $this->is_payment_method = true; |
|
938 | + } |
|
939 | 939 | |
940 | 940 | |
941 | 941 | } |
@@ -589,7 +589,7 @@ discard block |
||
589 | 589 | */ |
590 | 590 | public function get_activation_indicator_option_name(): string |
591 | 591 | { |
592 | - return 'ee_activation_' . $this->name(); |
|
592 | + return 'ee_activation_'.$this->name(); |
|
593 | 593 | } |
594 | 594 | |
595 | 595 | |
@@ -678,13 +678,13 @@ discard block |
||
678 | 678 | */ |
679 | 679 | public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null): bool |
680 | 680 | { |
681 | - if (! $version_history) { |
|
681 | + if ( ! $version_history) { |
|
682 | 682 | $version_history = $this->get_activation_history(); |
683 | 683 | } |
684 | 684 | if ($current_version_to_add === null) { |
685 | 685 | $current_version_to_add = $this->version(); |
686 | 686 | } |
687 | - $version_history[ $current_version_to_add ][] = date('Y-m-d H:i:s', time()); |
|
687 | + $version_history[$current_version_to_add][] = date('Y-m-d H:i:s', time()); |
|
688 | 688 | return update_option($this->get_activation_history_option_name(), $version_history); |
689 | 689 | } |
690 | 690 | |
@@ -697,7 +697,7 @@ discard block |
||
697 | 697 | */ |
698 | 698 | public function get_activation_history_option_name(): string |
699 | 699 | { |
700 | - return self::ee_addon_version_history_option_prefix . $this->name(); |
|
700 | + return self::ee_addon_version_history_option_prefix.$this->name(); |
|
701 | 701 | } |
702 | 702 | |
703 | 703 | |
@@ -776,7 +776,7 @@ discard block |
||
776 | 776 | // is admin and not in M-Mode ? |
777 | 777 | if (is_admin() && MaintenanceStatus::isDisabled()) { |
778 | 778 | add_filter('plugin_action_links', [$this, 'plugin_action_links'], 10, 2); |
779 | - add_filter('after_plugin_row_' . $this->_plugin_basename, [$this, 'after_plugin_row'], 10, 3); |
|
779 | + add_filter('after_plugin_row_'.$this->_plugin_basename, [$this, 'after_plugin_row'], 10, 3); |
|
780 | 780 | } |
781 | 781 | } |
782 | 782 | |
@@ -797,23 +797,23 @@ discard block |
||
797 | 797 | } |
798 | 798 | // ensure the plugin action slug starts with 'espresso_' |
799 | 799 | $plugin_action_slug = strpos($plugin_action_slug, 'espresso_') !== 0 |
800 | - ? 'espresso_' . $plugin_action_slug |
|
800 | + ? 'espresso_'.$plugin_action_slug |
|
801 | 801 | : $plugin_action_slug; |
802 | 802 | |
803 | - $plugin_action_link_url = 'admin.php?page=' . $plugin_action_slug; |
|
803 | + $plugin_action_link_url = 'admin.php?page='.$plugin_action_slug; |
|
804 | 804 | if ($this->isPaymentMethod()) { |
805 | 805 | $pm_slug = $this->paymentMethodSlug(); |
806 | 806 | $plugin_action_link_url = "admin.php?page=espresso_payment_settings&action=default"; |
807 | 807 | $plugin_action_link_url .= "&payment_method=$pm_slug#espresso_{$pm_slug}_payment_settings"; |
808 | 808 | } |
809 | 809 | // filter settings link url and text |
810 | - $plugin_action_link_url = apply_filters( |
|
810 | + $plugin_action_link_url = apply_filters( |
|
811 | 811 | "FHEE__EE_Addon_plugin_action_links__url", |
812 | 812 | $plugin_action_link_url, |
813 | 813 | $this->plugin_slug(), |
814 | 814 | $file |
815 | 815 | ); |
816 | - if (! $plugin_action_link_url) { |
|
816 | + if ( ! $plugin_action_link_url) { |
|
817 | 817 | return $links; |
818 | 818 | } |
819 | 819 | $plugin_action_link_text = apply_filters( |
@@ -826,7 +826,7 @@ discard block |
||
826 | 826 | // before other links |
827 | 827 | array_unshift( |
828 | 828 | $links, |
829 | - '<a href="' . $plugin_action_link_url . '">' . esc_html($plugin_action_link_text) . '</a>' |
|
829 | + '<a href="'.$plugin_action_link_url.'">'.esc_html($plugin_action_link_text).'</a>' |
|
830 | 830 | ); |
831 | 831 | return $links; |
832 | 832 | } |
@@ -846,17 +846,17 @@ discard block |
||
846 | 846 | { |
847 | 847 | $after_plugin_row = ''; |
848 | 848 | $plugins_page_row = $this->get_plugins_page_row(); |
849 | - if (! empty($plugins_page_row) && $plugin_file === $this->plugin_basename()) { |
|
849 | + if ( ! empty($plugins_page_row) && $plugin_file === $this->plugin_basename()) { |
|
850 | 850 | $class = $status ? 'active' : 'inactive'; |
851 | 851 | $link_text = $plugins_page_row['link_text'] ?? ''; |
852 | 852 | $link_url = $plugins_page_row['link_url'] ?? ''; |
853 | 853 | $description = $plugins_page_row['description'] ?? ''; |
854 | - if (! empty($link_text) && ! empty($link_url) && ! empty($description)) { |
|
855 | - $after_plugin_row .= '<tr id="' . sanitize_title($plugin_file) . '-ee-addon" class="' . $class . '">'; |
|
854 | + if ( ! empty($link_text) && ! empty($link_url) && ! empty($description)) { |
|
855 | + $after_plugin_row .= '<tr id="'.sanitize_title($plugin_file).'-ee-addon" class="'.$class.'">'; |
|
856 | 856 | $after_plugin_row .= '<th class="check-column" scope="row"></th>'; |
857 | 857 | $after_plugin_row .= '<td class="ee-addon-upsell-info-title-td plugin-title column-primary">'; |
858 | 858 | $after_plugin_row .= '<p class="ee-addon-upsell-info-dv"> |
859 | - <a class="button button--primary" href="' . esc_url_raw($link_url) . '">' |
|
859 | + <a class="button button--primary" href="' . esc_url_raw($link_url).'">' |
|
860 | 860 | . $link_text |
861 | 861 | . ' <span class="dashicons dashicons-arrow-right-alt2" style="margin:0;"></span>' |
862 | 862 | . '</a> |
@@ -12,78 +12,78 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_Bootstrap |
14 | 14 | { |
15 | - /** |
|
16 | - * load_espresso_addons |
|
17 | - * runs during the WP 'plugins_loaded' action at priority 1 |
|
18 | - * and is the initial loading phase for EE addons |
|
19 | - * no other logic should be performed at this point |
|
20 | - */ |
|
21 | - public static function load_espresso_addons() |
|
22 | - { |
|
23 | - do_action('AHEE__EE_Bootstrap__load_espresso_addons'); |
|
24 | - } |
|
15 | + /** |
|
16 | + * load_espresso_addons |
|
17 | + * runs during the WP 'plugins_loaded' action at priority 1 |
|
18 | + * and is the initial loading phase for EE addons |
|
19 | + * no other logic should be performed at this point |
|
20 | + */ |
|
21 | + public static function load_espresso_addons() |
|
22 | + { |
|
23 | + do_action('AHEE__EE_Bootstrap__load_espresso_addons'); |
|
24 | + } |
|
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * detect_activations_or_upgrades |
|
29 | - * runs during the WP 'plugins_loaded' action at priority 3 |
|
30 | - * Now that all the addons have been loaded, |
|
31 | - * we can determine if anything needs activating or upgrading |
|
32 | - */ |
|
33 | - public static function detect_activations_or_upgrades() |
|
34 | - { |
|
35 | - do_action('AHEE__EE_Bootstrap__detect_activations_or_upgrades'); |
|
36 | - } |
|
27 | + /** |
|
28 | + * detect_activations_or_upgrades |
|
29 | + * runs during the WP 'plugins_loaded' action at priority 3 |
|
30 | + * Now that all the addons have been loaded, |
|
31 | + * we can determine if anything needs activating or upgrading |
|
32 | + */ |
|
33 | + public static function detect_activations_or_upgrades() |
|
34 | + { |
|
35 | + do_action('AHEE__EE_Bootstrap__detect_activations_or_upgrades'); |
|
36 | + } |
|
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * load_core_configuration |
|
41 | - * runs during the WP 'plugins_loaded' action at priority 5 |
|
42 | - * Now that the database is assumed to be at the correct version |
|
43 | - * we can load and set all the system configurations |
|
44 | - */ |
|
45 | - public static function load_core_configuration() |
|
46 | - { |
|
47 | - do_action('AHEE__EE_Bootstrap__load_core_configuration'); |
|
48 | - } |
|
39 | + /** |
|
40 | + * load_core_configuration |
|
41 | + * runs during the WP 'plugins_loaded' action at priority 5 |
|
42 | + * Now that the database is assumed to be at the correct version |
|
43 | + * we can load and set all the system configurations |
|
44 | + */ |
|
45 | + public static function load_core_configuration() |
|
46 | + { |
|
47 | + do_action('AHEE__EE_Bootstrap__load_core_configuration'); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * register_shortcodes_modules_and_widgets |
|
53 | - * runs during the WP 'plugins_loaded' action at priority 7 |
|
54 | - * and handles registering all o four shortcodes, modules and widgets |
|
55 | - * so that they are ready to be used throughout the system |
|
56 | - */ |
|
57 | - public static function register_shortcodes_modules_and_widgets() |
|
58 | - { |
|
59 | - do_action('AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets'); |
|
60 | - } |
|
51 | + /** |
|
52 | + * register_shortcodes_modules_and_widgets |
|
53 | + * runs during the WP 'plugins_loaded' action at priority 7 |
|
54 | + * and handles registering all o four shortcodes, modules and widgets |
|
55 | + * so that they are ready to be used throughout the system |
|
56 | + */ |
|
57 | + public static function register_shortcodes_modules_and_widgets() |
|
58 | + { |
|
59 | + do_action('AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets'); |
|
60 | + } |
|
61 | 61 | |
62 | 62 | |
63 | - /** |
|
64 | - * brew_espresso |
|
65 | - * runs during the WP 'plugins_loaded' action at priority 9 |
|
66 | - * bootstrapping is considered complete at this point, |
|
67 | - * so let the fun begin... |
|
68 | - */ |
|
69 | - public static function brew_espresso() |
|
70 | - { |
|
71 | - do_action('AHEE__EE_Bootstrap__brew_espresso'); |
|
72 | - } |
|
63 | + /** |
|
64 | + * brew_espresso |
|
65 | + * runs during the WP 'plugins_loaded' action at priority 9 |
|
66 | + * bootstrapping is considered complete at this point, |
|
67 | + * so let the fun begin... |
|
68 | + */ |
|
69 | + public static function brew_espresso() |
|
70 | + { |
|
71 | + do_action('AHEE__EE_Bootstrap__brew_espresso'); |
|
72 | + } |
|
73 | 73 | |
74 | 74 | |
75 | - /** |
|
76 | - * @deprecated 4.9.53 |
|
77 | - */ |
|
78 | - public function run_request_stack() |
|
79 | - { |
|
80 | - } |
|
75 | + /** |
|
76 | + * @deprecated 4.9.53 |
|
77 | + */ |
|
78 | + public function run_request_stack() |
|
79 | + { |
|
80 | + } |
|
81 | 81 | |
82 | 82 | |
83 | - /** |
|
84 | - * @deprecated 4.9.53 |
|
85 | - */ |
|
86 | - public function build_request_stack() |
|
87 | - { |
|
88 | - } |
|
83 | + /** |
|
84 | + * @deprecated 4.9.53 |
|
85 | + */ |
|
86 | + public function build_request_stack() |
|
87 | + { |
|
88 | + } |
|
89 | 89 | } |
@@ -19,516 +19,516 @@ |
||
19 | 19 | */ |
20 | 20 | final class EE_Front_Controller |
21 | 21 | { |
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - private $_template_path; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - private $_template; |
|
31 | - |
|
32 | - /** |
|
33 | - * @type EE_Registry |
|
34 | - */ |
|
35 | - protected $Registry; |
|
36 | - |
|
37 | - /** |
|
38 | - * @type EE_Request_Handler |
|
39 | - */ |
|
40 | - protected $Request_Handler; |
|
41 | - |
|
42 | - /** |
|
43 | - * @type EE_Module_Request_Router |
|
44 | - */ |
|
45 | - protected $Module_Request_Router; |
|
46 | - |
|
47 | - /** |
|
48 | - * @type CurrentPage |
|
49 | - */ |
|
50 | - protected $current_page; |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * class constructor |
|
55 | - * should fire after shortcode, module, addon, or other plugin's default priority init phases have run |
|
56 | - * |
|
57 | - * @access public |
|
58 | - * @param EE_Registry $Registry |
|
59 | - * @param CurrentPage $EspressoPage |
|
60 | - * @param EE_Module_Request_Router $Module_Request_Router |
|
61 | - */ |
|
62 | - public function __construct( |
|
63 | - EE_Registry $Registry, |
|
64 | - CurrentPage $EspressoPage, |
|
65 | - EE_Module_Request_Router $Module_Request_Router |
|
66 | - ) { |
|
67 | - $this->Registry = $Registry; |
|
68 | - $this->current_page = $EspressoPage; |
|
69 | - $this->Module_Request_Router = $Module_Request_Router; |
|
70 | - // load other resources and begin to actually run shortcodes and modules |
|
71 | - // analyse the incoming WP request |
|
72 | - add_action('parse_request', array($this, 'get_request'), 1, 1); |
|
73 | - // process request with module factory |
|
74 | - add_action('pre_get_posts', array($this, 'pre_get_posts'), 10, 1); |
|
75 | - // before headers sent |
|
76 | - add_action('wp', array($this, 'wp'), 5); |
|
77 | - // primarily used to process any content shortcodes |
|
78 | - add_action('template_redirect', array($this, 'templateRedirect'), 999); |
|
79 | - // header |
|
80 | - add_action('wp_head', array($this, 'header_meta_tag'), 5); |
|
81 | - add_action('wp_print_scripts', array($this, 'wp_print_scripts'), 10); |
|
82 | - add_filter('template_include', array($this, 'template_include'), 1); |
|
83 | - // display errors |
|
84 | - add_action('loop_start', array($this, 'display_errors'), 2); |
|
85 | - // the content |
|
86 | - // add_filter( 'the_content', array( $this, 'the_content' ), 5, 1 ); |
|
87 | - // exclude our private cpt comments |
|
88 | - add_filter('comments_clauses', array($this, 'filter_wp_comments'), 10, 1); |
|
89 | - // make sure any ajax requests will respect the url schema when requests are made against admin-ajax.php (http:// or https://) |
|
90 | - add_filter('admin_url', array($this, 'maybe_force_admin_ajax_ssl'), 200, 1); |
|
91 | - // action hook EE |
|
92 | - do_action('AHEE__EE_Front_Controller__construct__done', $this); |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * @return EE_Request_Handler |
|
98 | - * @deprecated 4.10.14.p |
|
99 | - */ |
|
100 | - public function Request_Handler() |
|
101 | - { |
|
102 | - if (! $this->Request_Handler instanceof EE_Request_Handler) { |
|
103 | - $this->Request_Handler = LoaderFactory::getLoader()->getShared('EE_Request_Handler'); |
|
104 | - } |
|
105 | - return $this->Request_Handler; |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @return EE_Module_Request_Router |
|
111 | - */ |
|
112 | - public function Module_Request_Router() |
|
113 | - { |
|
114 | - return $this->Module_Request_Router; |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @return LegacyShortcodesManager |
|
120 | - * @deprecated 4.10.14.p |
|
121 | - */ |
|
122 | - public function getLegacyShortcodesManager() |
|
123 | - { |
|
124 | - return EE_Config::getLegacyShortcodesManager(); |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - |
|
129 | - |
|
130 | - |
|
131 | - /*********************************************** INIT ACTION HOOK ***********************************************/ |
|
132 | - /** |
|
133 | - * filter_wp_comments |
|
134 | - * This simply makes sure that any "private" EE CPTs do not have their comments show up in any wp comment |
|
135 | - * widgets/queries done on frontend |
|
136 | - * |
|
137 | - * @param array $clauses array of comment clauses setup by WP_Comment_Query |
|
138 | - * @return array array of comment clauses with modifications. |
|
139 | - * @throws InvalidArgumentException |
|
140 | - * @throws InvalidDataTypeException |
|
141 | - * @throws InvalidInterfaceException |
|
142 | - */ |
|
143 | - public function filter_wp_comments($clauses) |
|
144 | - { |
|
145 | - global $wpdb; |
|
146 | - if (strpos($clauses['join'], $wpdb->posts) !== false) { |
|
147 | - /** @var EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions $custom_post_types */ |
|
148 | - $custom_post_types = LoaderFactory::getLoader()->getShared( |
|
149 | - 'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' |
|
150 | - ); |
|
151 | - $cpts = $custom_post_types->getPrivateCustomPostTypes(); |
|
152 | - foreach ($cpts as $cpt => $details) { |
|
153 | - $clauses['where'] .= $wpdb->prepare(" AND $wpdb->posts.post_type != %s", $cpt); |
|
154 | - } |
|
155 | - } |
|
156 | - return $clauses; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * this just makes sure that if the site is using ssl that we force that for any admin ajax calls from frontend |
|
162 | - * |
|
163 | - * @param string $url incoming url |
|
164 | - * @return string final assembled url |
|
165 | - */ |
|
166 | - public function maybe_force_admin_ajax_ssl($url) |
|
167 | - { |
|
168 | - if (is_ssl() && preg_match('/admin-ajax.php/', $url)) { |
|
169 | - $url = str_replace('http://', 'https://', $url); |
|
170 | - } |
|
171 | - return $url; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - |
|
176 | - |
|
177 | - |
|
178 | - |
|
179 | - /*********************************************** WP_LOADED ACTION HOOK ***********************************************/ |
|
180 | - |
|
181 | - |
|
182 | - /** |
|
183 | - * wp_loaded - should fire after shortcode, module, addon, or other plugin's have been registered and their |
|
184 | - * default priority init phases have run |
|
185 | - * |
|
186 | - * @access public |
|
187 | - * @return void |
|
188 | - */ |
|
189 | - public function wp_loaded() |
|
190 | - { |
|
191 | - } |
|
192 | - |
|
193 | - |
|
194 | - |
|
195 | - |
|
196 | - |
|
197 | - /*********************************************** PARSE_REQUEST HOOK ***********************************************/ |
|
198 | - /** |
|
199 | - * _get_request |
|
200 | - * |
|
201 | - * @access public |
|
202 | - * @param WP $WP |
|
203 | - * @return void |
|
204 | - */ |
|
205 | - public function get_request(WP $WP) |
|
206 | - { |
|
207 | - do_action('AHEE__EE_Front_Controller__get_request__start'); |
|
208 | - $this->current_page->parseQueryVars($WP); |
|
209 | - do_action('AHEE__EE_Front_Controller__get_request__complete'); |
|
210 | - remove_action('parse_request', [$this, 'get_request'], 1); |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * pre_get_posts - basically a module factory for instantiating modules and selecting the final view template |
|
216 | - * |
|
217 | - * @access public |
|
218 | - * @param WP_Query $WP_Query |
|
219 | - * @return void |
|
220 | - * @throws EE_Error |
|
221 | - * @throws ReflectionException |
|
222 | - */ |
|
223 | - public function pre_get_posts($WP_Query) |
|
224 | - { |
|
225 | - // only load Module_Request_Router if this is the main query |
|
226 | - if ( |
|
227 | - $this->Module_Request_Router instanceof EE_Module_Request_Router |
|
228 | - && $WP_Query->is_main_query() |
|
229 | - ) { |
|
230 | - // cycle thru module routes |
|
231 | - while ($route = $this->Module_Request_Router->get_route($WP_Query)) { |
|
232 | - // determine module and method for route |
|
233 | - $module = $this->Module_Request_Router->resolve_route($route[0], $route[1]); |
|
234 | - if ($module instanceof EED_Module) { |
|
235 | - // get registered view for route |
|
236 | - $this->_template_path = $this->Module_Request_Router->get_view($route); |
|
237 | - // grab module name |
|
238 | - $module_name = $module->module_name(); |
|
239 | - // map the module to the module objects |
|
240 | - $this->Registry->modules->{$module_name} = $module; |
|
241 | - } |
|
242 | - } |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /*********************************************** WP HOOK ***********************************************/ |
|
251 | - |
|
252 | - |
|
253 | - /** |
|
254 | - * wp - basically last chance to do stuff before headers sent |
|
255 | - * |
|
256 | - * @access public |
|
257 | - * @return void |
|
258 | - */ |
|
259 | - public function wp() |
|
260 | - { |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - |
|
265 | - /*********************** GET_HEADER && WP_HEAD HOOK ***********************/ |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * callback for the "template_redirect" hook point |
|
270 | - * checks sidebars for EE widgets |
|
271 | - * loads resources and assets accordingly |
|
272 | - * |
|
273 | - * @return void |
|
274 | - */ |
|
275 | - public function templateRedirect() |
|
276 | - { |
|
277 | - global $wp_query; |
|
278 | - if (empty($wp_query->posts)) { |
|
279 | - return; |
|
280 | - } |
|
281 | - // if we already know this is an espresso page, then load assets |
|
282 | - $load_assets = $this->current_page->isEspressoPage(); |
|
283 | - // if we are already loading assets then just move along, otherwise check for widgets |
|
284 | - $load_assets = $load_assets || $this->espresso_widgets_in_active_sidebars(); |
|
285 | - if ($load_assets) { |
|
286 | - add_action('wp_enqueue_scripts', array($this, 'enqueueStyle'), 10); |
|
287 | - add_action('wp_enqueue_scripts', array($this, 'enqueueScripts'), 10); |
|
288 | - } |
|
289 | - |
|
290 | - if (is_singular(EspressoPostType::EVENTS)) { |
|
291 | - new FilterNextPreviousEventPostQuery(); |
|
292 | - } |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * builds list of active widgets then scans active sidebars looking for them |
|
298 | - * returns true is an EE widget is found in an active sidebar |
|
299 | - * Please Note: this does NOT mean that the sidebar or widget |
|
300 | - * is actually in use in a given template, as that is unfortunately not known |
|
301 | - * until a sidebar and it's widgets are actually loaded |
|
302 | - * |
|
303 | - * @return boolean |
|
304 | - */ |
|
305 | - private function espresso_widgets_in_active_sidebars() |
|
306 | - { |
|
307 | - $espresso_widgets = array(); |
|
308 | - foreach ($this->Registry->widgets as $widget_class => $widget) { |
|
309 | - $id_base = EspressoWidget::getIdBase($widget_class); |
|
310 | - if (is_active_widget(false, false, $id_base)) { |
|
311 | - $espresso_widgets[] = $id_base; |
|
312 | - } |
|
313 | - } |
|
314 | - $all_sidebar_widgets = wp_get_sidebars_widgets(); |
|
315 | - foreach ($all_sidebar_widgets as $sidebar_widgets) { |
|
316 | - if (is_array($sidebar_widgets) && ! empty($sidebar_widgets)) { |
|
317 | - foreach ($sidebar_widgets as $sidebar_widget) { |
|
318 | - foreach ($espresso_widgets as $espresso_widget) { |
|
319 | - if (strpos($sidebar_widget, $espresso_widget) !== false) { |
|
320 | - return true; |
|
321 | - } |
|
322 | - } |
|
323 | - } |
|
324 | - } |
|
325 | - } |
|
326 | - return false; |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * header_meta_tag |
|
332 | - * |
|
333 | - * @access public |
|
334 | - * @return void |
|
335 | - */ |
|
336 | - public function header_meta_tag() |
|
337 | - { |
|
338 | - print( |
|
339 | - apply_filters( |
|
340 | - 'FHEE__EE_Front_Controller__header_meta_tag', |
|
341 | - '<meta name="generator" content="Event Espresso Version ' . EVENT_ESPRESSO_VERSION . "\" />\n" |
|
342 | - ) |
|
343 | - ); |
|
344 | - |
|
345 | - // let's exclude all event type taxonomy term archive pages from search engine indexing |
|
346 | - // @see https://events.codebasehq.com/projects/event-espresso/tickets/10249 |
|
347 | - // also exclude all critical pages from indexing |
|
348 | - if ( |
|
349 | - ( |
|
350 | - is_tax('espresso_event_type') |
|
351 | - && get_option('blog_public') !== '0' |
|
352 | - ) |
|
353 | - || is_page(EE_Registry::instance()->CFG->core->get_critical_pages_array()) |
|
354 | - ) { |
|
355 | - print( |
|
356 | - apply_filters( |
|
357 | - 'FHEE__EE_Front_Controller__header_meta_tag__noindex_for_event_type', |
|
358 | - '<meta name="robots" content="noindex,follow" />' . "\n" |
|
359 | - ) |
|
360 | - ); |
|
361 | - } |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * wp_print_scripts |
|
367 | - * |
|
368 | - * @return void |
|
369 | - * @throws EE_Error |
|
370 | - */ |
|
371 | - public function wp_print_scripts() |
|
372 | - { |
|
373 | - global $post; |
|
374 | - if ( |
|
375 | - isset($post->EE_Event) |
|
376 | - && $post->EE_Event instanceof EE_Event |
|
377 | - && get_post_type() === EspressoPostType::EVENTS |
|
378 | - && is_singular() |
|
379 | - ) { |
|
380 | - EEH_Schema::add_json_linked_data_for_event($post->EE_Event); |
|
381 | - } |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - public function enqueueStyle() |
|
386 | - { |
|
387 | - wp_enqueue_style('espresso_default'); |
|
388 | - wp_enqueue_style('espresso_custom_css'); |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - |
|
393 | - /*********************************************** WP_FOOTER ***********************************************/ |
|
394 | - |
|
395 | - |
|
396 | - public function enqueueScripts() |
|
397 | - { |
|
398 | - wp_enqueue_script('espresso_core'); |
|
399 | - } |
|
400 | - |
|
401 | - |
|
402 | - /** |
|
403 | - * @param WP_Query $query |
|
404 | - * @return void |
|
405 | - * @throws DomainException |
|
406 | - */ |
|
407 | - public function display_errors(WP_Query $query) |
|
408 | - { |
|
409 | - if (! $query->is_main_query()) { |
|
410 | - return; |
|
411 | - } |
|
412 | - static $shown_already = false; |
|
413 | - do_action('AHEE__EE_Front_Controller__display_errors__begin'); |
|
414 | - if ( |
|
415 | - ! $shown_already |
|
416 | - && apply_filters('FHEE__EE_Front_Controller__display_errors', true) |
|
417 | - && is_main_query() |
|
418 | - && ! is_feed() |
|
419 | - && in_the_loop() |
|
420 | - && $this->current_page->isEspressoPage() |
|
421 | - ) { |
|
422 | - $shown_already = true; |
|
423 | - if (did_action('wp_head')) { |
|
424 | - echo wp_kses($this->printNotices(), AllowedTags::getAllowedTags()); |
|
425 | - } else { |
|
426 | - // block enabled themes run their query loop before headers are sent |
|
427 | - // so we need to add our notices onto the beginning of the content |
|
428 | - add_filter('the_content', [$this, 'prependNotices'], 1, 1); |
|
429 | - } |
|
430 | - } |
|
431 | - do_action('AHEE__EE_Front_Controller__display_errors__end'); |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - /** |
|
436 | - * @param string $the_content |
|
437 | - * @return string |
|
438 | - * @since 4.10.30.p |
|
439 | - */ |
|
440 | - public function prependNotices($the_content) |
|
441 | - { |
|
442 | - $notices = $this->printNotices(); |
|
443 | - return $notices ? $notices . $the_content : $the_content; |
|
444 | - } |
|
445 | - |
|
446 | - |
|
447 | - /** |
|
448 | - * @return false|string |
|
449 | - * @since 4.10.30.p |
|
450 | - */ |
|
451 | - public function printNotices() |
|
452 | - { |
|
453 | - ob_start(); |
|
454 | - echo wp_kses(EE_Error::get_notices(), AllowedTags::getWithFormTags()); |
|
455 | - EEH_Template::display_template(EE_TEMPLATES . 'espresso-ajax-notices.template.php'); |
|
456 | - return ob_get_clean(); |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - |
|
461 | - /*********************************************** UTILITIES ***********************************************/ |
|
462 | - |
|
463 | - |
|
464 | - /** |
|
465 | - * @param bool|string $template_include_path |
|
466 | - * @return string |
|
467 | - * @throws EE_Error |
|
468 | - * @throws ReflectionException |
|
469 | - */ |
|
470 | - public function template_include($template_include_path = '') |
|
471 | - { |
|
472 | - if ($this->current_page->isEspressoPage()) { |
|
473 | - // despite all helpers having autoloaders set, we need to manually load the template loader |
|
474 | - // because there are some side effects in that class for triggering template tag functions |
|
475 | - $this->Registry->load_helper('EEH_Template'); |
|
476 | - $this->_template_path = ! empty($this->_template_path) |
|
477 | - ? basename($this->_template_path) |
|
478 | - : basename((string) $template_include_path); |
|
479 | - $template_path = EEH_Template::locate_template($this->_template_path, [], false); |
|
480 | - $this->_template_path = ! empty($template_path) ? $template_path : $template_include_path; |
|
481 | - $this->_template = basename($this->_template_path); |
|
482 | - return $this->_template_path; |
|
483 | - } |
|
484 | - return $template_include_path; |
|
485 | - } |
|
486 | - |
|
487 | - |
|
488 | - /** |
|
489 | - * @param bool $with_path |
|
490 | - * @return string |
|
491 | - */ |
|
492 | - public function get_selected_template($with_path = false) |
|
493 | - { |
|
494 | - return $with_path ? $this->_template_path : $this->_template; |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - /** |
|
499 | - * @param string $shortcode_class |
|
500 | - * @param WP $wp |
|
501 | - * @throws ReflectionException |
|
502 | - * @deprecated 4.9.26 |
|
503 | - */ |
|
504 | - public function initialize_shortcode($shortcode_class = '', WP $wp = null) |
|
505 | - { |
|
506 | - EE_Error::doing_it_wrong( |
|
507 | - __METHOD__, |
|
508 | - esc_html__( |
|
509 | - 'Usage is deprecated. Please use \EventEspresso\core\services\shortcodes\LegacyShortcodesManager::initializeShortcode() instead.', |
|
510 | - 'event_espresso' |
|
511 | - ), |
|
512 | - '4.9.26' |
|
513 | - ); |
|
514 | - $this->getLegacyShortcodesManager()->initializeShortcode($shortcode_class, $wp); |
|
515 | - } |
|
516 | - |
|
517 | - |
|
518 | - /** |
|
519 | - * @return void |
|
520 | - * @deprecated 4.9.57.p |
|
521 | - */ |
|
522 | - public function loadPersistentAdminNoticeManager() |
|
523 | - { |
|
524 | - } |
|
525 | - |
|
526 | - |
|
527 | - /** |
|
528 | - * @return void |
|
529 | - * @deprecated 4.9.64.p |
|
530 | - */ |
|
531 | - public function employ_CPT_Strategy() |
|
532 | - { |
|
533 | - } |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + private $_template_path; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + private $_template; |
|
31 | + |
|
32 | + /** |
|
33 | + * @type EE_Registry |
|
34 | + */ |
|
35 | + protected $Registry; |
|
36 | + |
|
37 | + /** |
|
38 | + * @type EE_Request_Handler |
|
39 | + */ |
|
40 | + protected $Request_Handler; |
|
41 | + |
|
42 | + /** |
|
43 | + * @type EE_Module_Request_Router |
|
44 | + */ |
|
45 | + protected $Module_Request_Router; |
|
46 | + |
|
47 | + /** |
|
48 | + * @type CurrentPage |
|
49 | + */ |
|
50 | + protected $current_page; |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * class constructor |
|
55 | + * should fire after shortcode, module, addon, or other plugin's default priority init phases have run |
|
56 | + * |
|
57 | + * @access public |
|
58 | + * @param EE_Registry $Registry |
|
59 | + * @param CurrentPage $EspressoPage |
|
60 | + * @param EE_Module_Request_Router $Module_Request_Router |
|
61 | + */ |
|
62 | + public function __construct( |
|
63 | + EE_Registry $Registry, |
|
64 | + CurrentPage $EspressoPage, |
|
65 | + EE_Module_Request_Router $Module_Request_Router |
|
66 | + ) { |
|
67 | + $this->Registry = $Registry; |
|
68 | + $this->current_page = $EspressoPage; |
|
69 | + $this->Module_Request_Router = $Module_Request_Router; |
|
70 | + // load other resources and begin to actually run shortcodes and modules |
|
71 | + // analyse the incoming WP request |
|
72 | + add_action('parse_request', array($this, 'get_request'), 1, 1); |
|
73 | + // process request with module factory |
|
74 | + add_action('pre_get_posts', array($this, 'pre_get_posts'), 10, 1); |
|
75 | + // before headers sent |
|
76 | + add_action('wp', array($this, 'wp'), 5); |
|
77 | + // primarily used to process any content shortcodes |
|
78 | + add_action('template_redirect', array($this, 'templateRedirect'), 999); |
|
79 | + // header |
|
80 | + add_action('wp_head', array($this, 'header_meta_tag'), 5); |
|
81 | + add_action('wp_print_scripts', array($this, 'wp_print_scripts'), 10); |
|
82 | + add_filter('template_include', array($this, 'template_include'), 1); |
|
83 | + // display errors |
|
84 | + add_action('loop_start', array($this, 'display_errors'), 2); |
|
85 | + // the content |
|
86 | + // add_filter( 'the_content', array( $this, 'the_content' ), 5, 1 ); |
|
87 | + // exclude our private cpt comments |
|
88 | + add_filter('comments_clauses', array($this, 'filter_wp_comments'), 10, 1); |
|
89 | + // make sure any ajax requests will respect the url schema when requests are made against admin-ajax.php (http:// or https://) |
|
90 | + add_filter('admin_url', array($this, 'maybe_force_admin_ajax_ssl'), 200, 1); |
|
91 | + // action hook EE |
|
92 | + do_action('AHEE__EE_Front_Controller__construct__done', $this); |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * @return EE_Request_Handler |
|
98 | + * @deprecated 4.10.14.p |
|
99 | + */ |
|
100 | + public function Request_Handler() |
|
101 | + { |
|
102 | + if (! $this->Request_Handler instanceof EE_Request_Handler) { |
|
103 | + $this->Request_Handler = LoaderFactory::getLoader()->getShared('EE_Request_Handler'); |
|
104 | + } |
|
105 | + return $this->Request_Handler; |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @return EE_Module_Request_Router |
|
111 | + */ |
|
112 | + public function Module_Request_Router() |
|
113 | + { |
|
114 | + return $this->Module_Request_Router; |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @return LegacyShortcodesManager |
|
120 | + * @deprecated 4.10.14.p |
|
121 | + */ |
|
122 | + public function getLegacyShortcodesManager() |
|
123 | + { |
|
124 | + return EE_Config::getLegacyShortcodesManager(); |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + |
|
129 | + |
|
130 | + |
|
131 | + /*********************************************** INIT ACTION HOOK ***********************************************/ |
|
132 | + /** |
|
133 | + * filter_wp_comments |
|
134 | + * This simply makes sure that any "private" EE CPTs do not have their comments show up in any wp comment |
|
135 | + * widgets/queries done on frontend |
|
136 | + * |
|
137 | + * @param array $clauses array of comment clauses setup by WP_Comment_Query |
|
138 | + * @return array array of comment clauses with modifications. |
|
139 | + * @throws InvalidArgumentException |
|
140 | + * @throws InvalidDataTypeException |
|
141 | + * @throws InvalidInterfaceException |
|
142 | + */ |
|
143 | + public function filter_wp_comments($clauses) |
|
144 | + { |
|
145 | + global $wpdb; |
|
146 | + if (strpos($clauses['join'], $wpdb->posts) !== false) { |
|
147 | + /** @var EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions $custom_post_types */ |
|
148 | + $custom_post_types = LoaderFactory::getLoader()->getShared( |
|
149 | + 'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' |
|
150 | + ); |
|
151 | + $cpts = $custom_post_types->getPrivateCustomPostTypes(); |
|
152 | + foreach ($cpts as $cpt => $details) { |
|
153 | + $clauses['where'] .= $wpdb->prepare(" AND $wpdb->posts.post_type != %s", $cpt); |
|
154 | + } |
|
155 | + } |
|
156 | + return $clauses; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * this just makes sure that if the site is using ssl that we force that for any admin ajax calls from frontend |
|
162 | + * |
|
163 | + * @param string $url incoming url |
|
164 | + * @return string final assembled url |
|
165 | + */ |
|
166 | + public function maybe_force_admin_ajax_ssl($url) |
|
167 | + { |
|
168 | + if (is_ssl() && preg_match('/admin-ajax.php/', $url)) { |
|
169 | + $url = str_replace('http://', 'https://', $url); |
|
170 | + } |
|
171 | + return $url; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + |
|
176 | + |
|
177 | + |
|
178 | + |
|
179 | + /*********************************************** WP_LOADED ACTION HOOK ***********************************************/ |
|
180 | + |
|
181 | + |
|
182 | + /** |
|
183 | + * wp_loaded - should fire after shortcode, module, addon, or other plugin's have been registered and their |
|
184 | + * default priority init phases have run |
|
185 | + * |
|
186 | + * @access public |
|
187 | + * @return void |
|
188 | + */ |
|
189 | + public function wp_loaded() |
|
190 | + { |
|
191 | + } |
|
192 | + |
|
193 | + |
|
194 | + |
|
195 | + |
|
196 | + |
|
197 | + /*********************************************** PARSE_REQUEST HOOK ***********************************************/ |
|
198 | + /** |
|
199 | + * _get_request |
|
200 | + * |
|
201 | + * @access public |
|
202 | + * @param WP $WP |
|
203 | + * @return void |
|
204 | + */ |
|
205 | + public function get_request(WP $WP) |
|
206 | + { |
|
207 | + do_action('AHEE__EE_Front_Controller__get_request__start'); |
|
208 | + $this->current_page->parseQueryVars($WP); |
|
209 | + do_action('AHEE__EE_Front_Controller__get_request__complete'); |
|
210 | + remove_action('parse_request', [$this, 'get_request'], 1); |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * pre_get_posts - basically a module factory for instantiating modules and selecting the final view template |
|
216 | + * |
|
217 | + * @access public |
|
218 | + * @param WP_Query $WP_Query |
|
219 | + * @return void |
|
220 | + * @throws EE_Error |
|
221 | + * @throws ReflectionException |
|
222 | + */ |
|
223 | + public function pre_get_posts($WP_Query) |
|
224 | + { |
|
225 | + // only load Module_Request_Router if this is the main query |
|
226 | + if ( |
|
227 | + $this->Module_Request_Router instanceof EE_Module_Request_Router |
|
228 | + && $WP_Query->is_main_query() |
|
229 | + ) { |
|
230 | + // cycle thru module routes |
|
231 | + while ($route = $this->Module_Request_Router->get_route($WP_Query)) { |
|
232 | + // determine module and method for route |
|
233 | + $module = $this->Module_Request_Router->resolve_route($route[0], $route[1]); |
|
234 | + if ($module instanceof EED_Module) { |
|
235 | + // get registered view for route |
|
236 | + $this->_template_path = $this->Module_Request_Router->get_view($route); |
|
237 | + // grab module name |
|
238 | + $module_name = $module->module_name(); |
|
239 | + // map the module to the module objects |
|
240 | + $this->Registry->modules->{$module_name} = $module; |
|
241 | + } |
|
242 | + } |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /*********************************************** WP HOOK ***********************************************/ |
|
251 | + |
|
252 | + |
|
253 | + /** |
|
254 | + * wp - basically last chance to do stuff before headers sent |
|
255 | + * |
|
256 | + * @access public |
|
257 | + * @return void |
|
258 | + */ |
|
259 | + public function wp() |
|
260 | + { |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + |
|
265 | + /*********************** GET_HEADER && WP_HEAD HOOK ***********************/ |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * callback for the "template_redirect" hook point |
|
270 | + * checks sidebars for EE widgets |
|
271 | + * loads resources and assets accordingly |
|
272 | + * |
|
273 | + * @return void |
|
274 | + */ |
|
275 | + public function templateRedirect() |
|
276 | + { |
|
277 | + global $wp_query; |
|
278 | + if (empty($wp_query->posts)) { |
|
279 | + return; |
|
280 | + } |
|
281 | + // if we already know this is an espresso page, then load assets |
|
282 | + $load_assets = $this->current_page->isEspressoPage(); |
|
283 | + // if we are already loading assets then just move along, otherwise check for widgets |
|
284 | + $load_assets = $load_assets || $this->espresso_widgets_in_active_sidebars(); |
|
285 | + if ($load_assets) { |
|
286 | + add_action('wp_enqueue_scripts', array($this, 'enqueueStyle'), 10); |
|
287 | + add_action('wp_enqueue_scripts', array($this, 'enqueueScripts'), 10); |
|
288 | + } |
|
289 | + |
|
290 | + if (is_singular(EspressoPostType::EVENTS)) { |
|
291 | + new FilterNextPreviousEventPostQuery(); |
|
292 | + } |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * builds list of active widgets then scans active sidebars looking for them |
|
298 | + * returns true is an EE widget is found in an active sidebar |
|
299 | + * Please Note: this does NOT mean that the sidebar or widget |
|
300 | + * is actually in use in a given template, as that is unfortunately not known |
|
301 | + * until a sidebar and it's widgets are actually loaded |
|
302 | + * |
|
303 | + * @return boolean |
|
304 | + */ |
|
305 | + private function espresso_widgets_in_active_sidebars() |
|
306 | + { |
|
307 | + $espresso_widgets = array(); |
|
308 | + foreach ($this->Registry->widgets as $widget_class => $widget) { |
|
309 | + $id_base = EspressoWidget::getIdBase($widget_class); |
|
310 | + if (is_active_widget(false, false, $id_base)) { |
|
311 | + $espresso_widgets[] = $id_base; |
|
312 | + } |
|
313 | + } |
|
314 | + $all_sidebar_widgets = wp_get_sidebars_widgets(); |
|
315 | + foreach ($all_sidebar_widgets as $sidebar_widgets) { |
|
316 | + if (is_array($sidebar_widgets) && ! empty($sidebar_widgets)) { |
|
317 | + foreach ($sidebar_widgets as $sidebar_widget) { |
|
318 | + foreach ($espresso_widgets as $espresso_widget) { |
|
319 | + if (strpos($sidebar_widget, $espresso_widget) !== false) { |
|
320 | + return true; |
|
321 | + } |
|
322 | + } |
|
323 | + } |
|
324 | + } |
|
325 | + } |
|
326 | + return false; |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * header_meta_tag |
|
332 | + * |
|
333 | + * @access public |
|
334 | + * @return void |
|
335 | + */ |
|
336 | + public function header_meta_tag() |
|
337 | + { |
|
338 | + print( |
|
339 | + apply_filters( |
|
340 | + 'FHEE__EE_Front_Controller__header_meta_tag', |
|
341 | + '<meta name="generator" content="Event Espresso Version ' . EVENT_ESPRESSO_VERSION . "\" />\n" |
|
342 | + ) |
|
343 | + ); |
|
344 | + |
|
345 | + // let's exclude all event type taxonomy term archive pages from search engine indexing |
|
346 | + // @see https://events.codebasehq.com/projects/event-espresso/tickets/10249 |
|
347 | + // also exclude all critical pages from indexing |
|
348 | + if ( |
|
349 | + ( |
|
350 | + is_tax('espresso_event_type') |
|
351 | + && get_option('blog_public') !== '0' |
|
352 | + ) |
|
353 | + || is_page(EE_Registry::instance()->CFG->core->get_critical_pages_array()) |
|
354 | + ) { |
|
355 | + print( |
|
356 | + apply_filters( |
|
357 | + 'FHEE__EE_Front_Controller__header_meta_tag__noindex_for_event_type', |
|
358 | + '<meta name="robots" content="noindex,follow" />' . "\n" |
|
359 | + ) |
|
360 | + ); |
|
361 | + } |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * wp_print_scripts |
|
367 | + * |
|
368 | + * @return void |
|
369 | + * @throws EE_Error |
|
370 | + */ |
|
371 | + public function wp_print_scripts() |
|
372 | + { |
|
373 | + global $post; |
|
374 | + if ( |
|
375 | + isset($post->EE_Event) |
|
376 | + && $post->EE_Event instanceof EE_Event |
|
377 | + && get_post_type() === EspressoPostType::EVENTS |
|
378 | + && is_singular() |
|
379 | + ) { |
|
380 | + EEH_Schema::add_json_linked_data_for_event($post->EE_Event); |
|
381 | + } |
|
382 | + } |
|
383 | + |
|
384 | + |
|
385 | + public function enqueueStyle() |
|
386 | + { |
|
387 | + wp_enqueue_style('espresso_default'); |
|
388 | + wp_enqueue_style('espresso_custom_css'); |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + |
|
393 | + /*********************************************** WP_FOOTER ***********************************************/ |
|
394 | + |
|
395 | + |
|
396 | + public function enqueueScripts() |
|
397 | + { |
|
398 | + wp_enqueue_script('espresso_core'); |
|
399 | + } |
|
400 | + |
|
401 | + |
|
402 | + /** |
|
403 | + * @param WP_Query $query |
|
404 | + * @return void |
|
405 | + * @throws DomainException |
|
406 | + */ |
|
407 | + public function display_errors(WP_Query $query) |
|
408 | + { |
|
409 | + if (! $query->is_main_query()) { |
|
410 | + return; |
|
411 | + } |
|
412 | + static $shown_already = false; |
|
413 | + do_action('AHEE__EE_Front_Controller__display_errors__begin'); |
|
414 | + if ( |
|
415 | + ! $shown_already |
|
416 | + && apply_filters('FHEE__EE_Front_Controller__display_errors', true) |
|
417 | + && is_main_query() |
|
418 | + && ! is_feed() |
|
419 | + && in_the_loop() |
|
420 | + && $this->current_page->isEspressoPage() |
|
421 | + ) { |
|
422 | + $shown_already = true; |
|
423 | + if (did_action('wp_head')) { |
|
424 | + echo wp_kses($this->printNotices(), AllowedTags::getAllowedTags()); |
|
425 | + } else { |
|
426 | + // block enabled themes run their query loop before headers are sent |
|
427 | + // so we need to add our notices onto the beginning of the content |
|
428 | + add_filter('the_content', [$this, 'prependNotices'], 1, 1); |
|
429 | + } |
|
430 | + } |
|
431 | + do_action('AHEE__EE_Front_Controller__display_errors__end'); |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + /** |
|
436 | + * @param string $the_content |
|
437 | + * @return string |
|
438 | + * @since 4.10.30.p |
|
439 | + */ |
|
440 | + public function prependNotices($the_content) |
|
441 | + { |
|
442 | + $notices = $this->printNotices(); |
|
443 | + return $notices ? $notices . $the_content : $the_content; |
|
444 | + } |
|
445 | + |
|
446 | + |
|
447 | + /** |
|
448 | + * @return false|string |
|
449 | + * @since 4.10.30.p |
|
450 | + */ |
|
451 | + public function printNotices() |
|
452 | + { |
|
453 | + ob_start(); |
|
454 | + echo wp_kses(EE_Error::get_notices(), AllowedTags::getWithFormTags()); |
|
455 | + EEH_Template::display_template(EE_TEMPLATES . 'espresso-ajax-notices.template.php'); |
|
456 | + return ob_get_clean(); |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + |
|
461 | + /*********************************************** UTILITIES ***********************************************/ |
|
462 | + |
|
463 | + |
|
464 | + /** |
|
465 | + * @param bool|string $template_include_path |
|
466 | + * @return string |
|
467 | + * @throws EE_Error |
|
468 | + * @throws ReflectionException |
|
469 | + */ |
|
470 | + public function template_include($template_include_path = '') |
|
471 | + { |
|
472 | + if ($this->current_page->isEspressoPage()) { |
|
473 | + // despite all helpers having autoloaders set, we need to manually load the template loader |
|
474 | + // because there are some side effects in that class for triggering template tag functions |
|
475 | + $this->Registry->load_helper('EEH_Template'); |
|
476 | + $this->_template_path = ! empty($this->_template_path) |
|
477 | + ? basename($this->_template_path) |
|
478 | + : basename((string) $template_include_path); |
|
479 | + $template_path = EEH_Template::locate_template($this->_template_path, [], false); |
|
480 | + $this->_template_path = ! empty($template_path) ? $template_path : $template_include_path; |
|
481 | + $this->_template = basename($this->_template_path); |
|
482 | + return $this->_template_path; |
|
483 | + } |
|
484 | + return $template_include_path; |
|
485 | + } |
|
486 | + |
|
487 | + |
|
488 | + /** |
|
489 | + * @param bool $with_path |
|
490 | + * @return string |
|
491 | + */ |
|
492 | + public function get_selected_template($with_path = false) |
|
493 | + { |
|
494 | + return $with_path ? $this->_template_path : $this->_template; |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + /** |
|
499 | + * @param string $shortcode_class |
|
500 | + * @param WP $wp |
|
501 | + * @throws ReflectionException |
|
502 | + * @deprecated 4.9.26 |
|
503 | + */ |
|
504 | + public function initialize_shortcode($shortcode_class = '', WP $wp = null) |
|
505 | + { |
|
506 | + EE_Error::doing_it_wrong( |
|
507 | + __METHOD__, |
|
508 | + esc_html__( |
|
509 | + 'Usage is deprecated. Please use \EventEspresso\core\services\shortcodes\LegacyShortcodesManager::initializeShortcode() instead.', |
|
510 | + 'event_espresso' |
|
511 | + ), |
|
512 | + '4.9.26' |
|
513 | + ); |
|
514 | + $this->getLegacyShortcodesManager()->initializeShortcode($shortcode_class, $wp); |
|
515 | + } |
|
516 | + |
|
517 | + |
|
518 | + /** |
|
519 | + * @return void |
|
520 | + * @deprecated 4.9.57.p |
|
521 | + */ |
|
522 | + public function loadPersistentAdminNoticeManager() |
|
523 | + { |
|
524 | + } |
|
525 | + |
|
526 | + |
|
527 | + /** |
|
528 | + * @return void |
|
529 | + * @deprecated 4.9.64.p |
|
530 | + */ |
|
531 | + public function employ_CPT_Strategy() |
|
532 | + { |
|
533 | + } |
|
534 | 534 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public function Request_Handler() |
101 | 101 | { |
102 | - if (! $this->Request_Handler instanceof EE_Request_Handler) { |
|
102 | + if ( ! $this->Request_Handler instanceof EE_Request_Handler) { |
|
103 | 103 | $this->Request_Handler = LoaderFactory::getLoader()->getShared('EE_Request_Handler'); |
104 | 104 | } |
105 | 105 | return $this->Request_Handler; |
@@ -338,7 +338,7 @@ discard block |
||
338 | 338 | print( |
339 | 339 | apply_filters( |
340 | 340 | 'FHEE__EE_Front_Controller__header_meta_tag', |
341 | - '<meta name="generator" content="Event Espresso Version ' . EVENT_ESPRESSO_VERSION . "\" />\n" |
|
341 | + '<meta name="generator" content="Event Espresso Version '.EVENT_ESPRESSO_VERSION."\" />\n" |
|
342 | 342 | ) |
343 | 343 | ); |
344 | 344 | |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | print( |
356 | 356 | apply_filters( |
357 | 357 | 'FHEE__EE_Front_Controller__header_meta_tag__noindex_for_event_type', |
358 | - '<meta name="robots" content="noindex,follow" />' . "\n" |
|
358 | + '<meta name="robots" content="noindex,follow" />'."\n" |
|
359 | 359 | ) |
360 | 360 | ); |
361 | 361 | } |
@@ -406,7 +406,7 @@ discard block |
||
406 | 406 | */ |
407 | 407 | public function display_errors(WP_Query $query) |
408 | 408 | { |
409 | - if (! $query->is_main_query()) { |
|
409 | + if ( ! $query->is_main_query()) { |
|
410 | 410 | return; |
411 | 411 | } |
412 | 412 | static $shown_already = false; |
@@ -440,7 +440,7 @@ discard block |
||
440 | 440 | public function prependNotices($the_content) |
441 | 441 | { |
442 | 442 | $notices = $this->printNotices(); |
443 | - return $notices ? $notices . $the_content : $the_content; |
|
443 | + return $notices ? $notices.$the_content : $the_content; |
|
444 | 444 | } |
445 | 445 | |
446 | 446 | |
@@ -452,7 +452,7 @@ discard block |
||
452 | 452 | { |
453 | 453 | ob_start(); |
454 | 454 | echo wp_kses(EE_Error::get_notices(), AllowedTags::getWithFormTags()); |
455 | - EEH_Template::display_template(EE_TEMPLATES . 'espresso-ajax-notices.template.php'); |
|
455 | + EEH_Template::display_template(EE_TEMPLATES.'espresso-ajax-notices.template.php'); |
|
456 | 456 | return ob_get_clean(); |
457 | 457 | } |
458 | 458 |
@@ -32,1189 +32,1189 @@ |
||
32 | 32 | */ |
33 | 33 | final class EE_System implements ResettableInterface |
34 | 34 | { |
35 | - /** |
|
36 | - * indicates this is a 'normal' request. Ie, not activation, nor upgrade, nor activation. |
|
37 | - * So examples of this would be a normal GET request on the frontend or backend, or a POST, etc |
|
38 | - */ |
|
39 | - const req_type_normal = 0; |
|
40 | - |
|
41 | - /** |
|
42 | - * Indicates this is a brand-new installation of EE so we should install |
|
43 | - * tables and default data etc |
|
44 | - */ |
|
45 | - const req_type_new_activation = 1; |
|
46 | - |
|
47 | - /** |
|
48 | - * we've detected that EE has been reactivated (or EE was activated during maintenance mode, |
|
49 | - * and we just exited maintenance mode). We MUST check the database is set up properly |
|
50 | - * and that default data is set up too |
|
51 | - */ |
|
52 | - const req_type_reactivation = 2; |
|
53 | - |
|
54 | - /** |
|
55 | - * indicates that EE has been upgraded since its previous request. |
|
56 | - * We may have data migration scripts to call and will want to trigger maintenance mode |
|
57 | - */ |
|
58 | - const req_type_upgrade = 3; |
|
59 | - |
|
60 | - /** |
|
61 | - * TODO will detect that EE has been DOWNGRADED. We probably don't want to run in this case... |
|
62 | - */ |
|
63 | - const req_type_downgrade = 4; |
|
64 | - |
|
65 | - /** |
|
66 | - * @deprecated since version 4.6.0.dev.006 |
|
67 | - * Now whenever a new_activation is detected the request type is still just |
|
68 | - * new_activation (same for reactivation, upgrade, downgrade etc), but if we're in maintenance mode |
|
69 | - * EE_System::initialize_db_if_no_migrations_required and EE_Addon::initialize_db_if_no_migrations_required |
|
70 | - * will instead enqueue that EE plugin's db initialization for when we're taken out of maintenance mode. |
|
71 | - * (Specifically, when the migration manager indicates migrations are finished |
|
72 | - * EE_Data_Migration_Manager::initialize_db_for_enqueued_ee_plugins() will be called) |
|
73 | - */ |
|
74 | - const req_type_activation_but_not_installed = 5; |
|
75 | - |
|
76 | - /** |
|
77 | - * option prefix for recording the activation history (like core's "espresso_db_update") of addons |
|
78 | - */ |
|
79 | - const addon_activation_history_option_prefix = 'ee_addon_activation_history_'; |
|
80 | - |
|
81 | - private static ?EE_System $_instance = null; |
|
82 | - |
|
83 | - private ?LoaderInterface $loader; |
|
84 | - |
|
85 | - private ?EE_Maintenance_Mode $maintenance_mode; |
|
86 | - |
|
87 | - private ?EE_Registry $registry; |
|
88 | - |
|
89 | - private ?RequestInterface $request; |
|
90 | - |
|
91 | - private Router $router; |
|
92 | - |
|
93 | - private ?AddonManager $addon_manager = null; |
|
94 | - |
|
95 | - private ?EE_Capabilities $capabilities = null; |
|
96 | - |
|
97 | - protected ?FeatureFlags $feature = null; |
|
98 | - |
|
99 | - private ?RegisterCustomPostTypes $register_custom_post_types = null; |
|
100 | - |
|
101 | - private ?RegisterCustomTaxonomies $register_custom_taxonomies = null; |
|
102 | - |
|
103 | - private ?RegisterCustomTaxonomyTerms $register_custom_taxonomy_terms = null; |
|
104 | - |
|
105 | - /** |
|
106 | - * Stores which type of request this is, options being one of the constants on EE_System starting with req_type_*. |
|
107 | - * It can be a brand-new activation, a reactivation, an upgrade, a downgrade, or a normal request. |
|
108 | - */ |
|
109 | - private ?int $_req_type = null; |
|
110 | - |
|
111 | - /** |
|
112 | - * Whether there was a non-micro version change in EE core version during this request |
|
113 | - */ |
|
114 | - private bool $_major_version_change = false; |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * @singleton method used to instantiate class object |
|
119 | - * @param LoaderInterface|null $loader |
|
120 | - * @param EE_Maintenance_Mode|null $maintenance_mode |
|
121 | - * @param EE_Registry|null $registry |
|
122 | - * @param RequestInterface|null $request |
|
123 | - * @param Router|null $router |
|
124 | - * @param FeatureFlags|null $feature |
|
125 | - * @return EE_System |
|
126 | - */ |
|
127 | - public static function instance( |
|
128 | - ?LoaderInterface $loader = null, |
|
129 | - ?EE_Maintenance_Mode $maintenance_mode = null, |
|
130 | - ?EE_Registry $registry = null, |
|
131 | - ?RequestInterface $request = null, |
|
132 | - ?Router $router = null, |
|
133 | - ?FeatureFlags $feature = null |
|
134 | - ): EE_System { |
|
135 | - // check if class object is instantiated |
|
136 | - if (! self::$_instance instanceof EE_System) { |
|
137 | - self::$_instance = new self($loader, $maintenance_mode, $registry, $request, $router, $feature); |
|
138 | - } |
|
139 | - return self::$_instance; |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - /** |
|
144 | - * resets the instance and returns it |
|
145 | - * |
|
146 | - * @return EE_System |
|
147 | - * @throws EE_Error |
|
148 | - */ |
|
149 | - public static function reset(): EE_System |
|
150 | - { |
|
151 | - self::$_instance->_req_type = null; |
|
152 | - // make sure none of the old hooks are left hanging around |
|
153 | - remove_all_actions('AHEE__EE_System__perform_activations_upgrades_and_migrations'); |
|
154 | - // we need to reset the migration manager in order for it to detect DMSs properly |
|
155 | - EE_Data_Migration_Manager::reset(); |
|
156 | - self::instance()->detect_activations_or_upgrades(); |
|
157 | - self::instance()->perform_activations_upgrades_and_migrations(); |
|
158 | - return self::instance(); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * sets hooks for running rest of system |
|
164 | - * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
|
165 | - * starting EE Addons from any other point may lead to problems |
|
166 | - * |
|
167 | - * @param LoaderInterface $loader |
|
168 | - * @param EE_Maintenance_Mode $maintenance_mode |
|
169 | - * @param EE_Registry $registry |
|
170 | - * @param RequestInterface $request |
|
171 | - * @param Router $router |
|
172 | - * @param FeatureFlags $feature |
|
173 | - */ |
|
174 | - private function __construct( |
|
175 | - LoaderInterface $loader, |
|
176 | - EE_Maintenance_Mode $maintenance_mode, |
|
177 | - EE_Registry $registry, |
|
178 | - RequestInterface $request, |
|
179 | - Router $router, |
|
180 | - FeatureFlags $feature |
|
181 | - ) { |
|
182 | - $this->registry = $registry; |
|
183 | - $this->loader = $loader; |
|
184 | - $this->request = $request; |
|
185 | - $this->router = $router; |
|
186 | - $this->maintenance_mode = $maintenance_mode; |
|
187 | - $this->feature = $feature; |
|
188 | - do_action('AHEE__EE_System__construct__begin', $this); |
|
189 | - add_action( |
|
190 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
191 | - [$this, 'loadCapabilities'], |
|
192 | - 5 |
|
193 | - ); |
|
194 | - add_action( |
|
195 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
196 | - [$this, 'loadCommandBus'], |
|
197 | - 7 |
|
198 | - ); |
|
199 | - add_action( |
|
200 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
201 | - [$this, 'loadPluginApi'], |
|
202 | - 9 |
|
203 | - ); |
|
204 | - // give caff stuff a chance to play during the activation process too. |
|
205 | - add_action( |
|
206 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
207 | - [$this, 'brewCaffeinated'], |
|
208 | - 9 |
|
209 | - ); |
|
210 | - // allow addons to load first so that they can register autoloaders, set hooks for running DMS's, etc |
|
211 | - add_action( |
|
212 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
213 | - [$this, 'load_espresso_addons'] |
|
214 | - ); |
|
215 | - // when an ee addon is activated, we want to call the core hook(s) again |
|
216 | - // because the newly-activated addon didn't get a chance to run at all |
|
217 | - add_action('activate_plugin', [$this, 'load_espresso_addons'], 1); |
|
218 | - // detect whether install or upgrade |
|
219 | - add_action( |
|
220 | - 'AHEE__EE_Bootstrap__detect_activations_or_upgrades', |
|
221 | - [$this, 'detect_activations_or_upgrades'], |
|
222 | - 3 |
|
223 | - ); |
|
224 | - // load EE_Config, EE_Textdomain, etc |
|
225 | - add_action( |
|
226 | - 'AHEE__EE_Bootstrap__load_core_configuration', |
|
227 | - [$this, 'load_core_configuration'], |
|
228 | - 5 |
|
229 | - ); |
|
230 | - // load specifications for matching routes to current request |
|
231 | - add_action( |
|
232 | - 'AHEE__EE_Bootstrap__load_core_configuration', |
|
233 | - [$this, 'loadRouteMatchSpecifications'] |
|
234 | - ); |
|
235 | - // load specifications for custom post types |
|
236 | - add_action( |
|
237 | - 'AHEE__EE_Bootstrap__load_core_configuration', |
|
238 | - [$this, 'loadCustomPostTypes'] |
|
239 | - ); |
|
240 | - // register shortcodes, modules, and widgets |
|
241 | - add_action( |
|
242 | - 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets', |
|
243 | - [$this, 'register_shortcodes_modules_and_widgets'], |
|
244 | - 7 |
|
245 | - ); |
|
246 | - // you wanna get going? I wanna get going... let's get going! |
|
247 | - add_action( |
|
248 | - 'AHEE__EE_Bootstrap__brew_espresso', |
|
249 | - [$this, 'brew_espresso'], |
|
250 | - 9 |
|
251 | - ); |
|
252 | - // other housekeeping |
|
253 | - // exclude EE critical pages from wp_list_pages |
|
254 | - add_filter( |
|
255 | - 'wp_list_pages_excludes', |
|
256 | - [$this, 'remove_pages_from_wp_list_pages'] |
|
257 | - ); |
|
258 | - // ALL EE Addons should use the following hook point to attach their initial setup too |
|
259 | - // it's extremely important for EE Addons to register any class autoloaders so that they can be available when the EE_Config loads |
|
260 | - do_action('AHEE__EE_System__construct__complete', $this); |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * @return void |
|
266 | - */ |
|
267 | - public function loadCapabilities() |
|
268 | - { |
|
269 | - $this->capabilities = $this->loader->getShared('EE_Capabilities'); |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * create and cache the CommandBus, and also add middleware |
|
275 | - * The CapChecker middleware requires the use of EE_Capabilities |
|
276 | - * which is why we need to load the CommandBus after Caps are set up |
|
277 | - * CommandBus middleware operate FIFO - First In First Out |
|
278 | - * so LocateMovedCommands will run first in order to return any new commands |
|
279 | - * |
|
280 | - * @return void |
|
281 | - */ |
|
282 | - public function loadCommandBus() |
|
283 | - { |
|
284 | - $this->loader->getShared( |
|
285 | - 'CommandBusInterface', |
|
286 | - [ |
|
287 | - null, |
|
288 | - apply_filters( |
|
289 | - 'FHEE__EE_Load_Espresso_Core__handle_request__CommandBus_middleware', |
|
290 | - [ |
|
291 | - $this->loader->getShared('EventEspresso\core\services\commands\middleware\LocateMovedCommands'), |
|
292 | - $this->loader->getShared('EventEspresso\core\services\commands\middleware\CapChecker'), |
|
293 | - $this->loader->getShared('EventEspresso\core\services\commands\middleware\AddActionHook'), |
|
294 | - ] |
|
295 | - ), |
|
296 | - ] |
|
297 | - ); |
|
298 | - } |
|
299 | - |
|
300 | - |
|
301 | - /** |
|
302 | - * @return void |
|
303 | - * @throws Exception |
|
304 | - */ |
|
305 | - public function loadPluginApi() |
|
306 | - { |
|
307 | - $this->addon_manager = $this->loader->getShared(AddonManager::class); |
|
308 | - $this->addon_manager->initialize(); |
|
309 | - $this->loader->getShared('EE_Request_Handler'); |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * The purpose of this method is to simply check for a file named "caffeinated/brewing_regular.php" for any hooks |
|
315 | - * that need to be setup before our EE_System launches. |
|
316 | - * |
|
317 | - * @return void |
|
318 | - * @throws DomainException |
|
319 | - * @throws InvalidArgumentException |
|
320 | - * @throws InvalidDataTypeException |
|
321 | - * @throws InvalidInterfaceException |
|
322 | - * @throws InvalidClassException |
|
323 | - * @throws InvalidFilePathException |
|
324 | - * @throws EE_Error |
|
325 | - * @throws Exception |
|
326 | - */ |
|
327 | - public function brewCaffeinated() |
|
328 | - { |
|
329 | - static $brew; |
|
330 | - /** @var Domain $domain */ |
|
331 | - $domain = DomainFactory::getEventEspressoCoreDomain(); |
|
332 | - if ($domain->isCaffeinated() && ! $brew instanceof EE_Brewing_Regular) { |
|
333 | - require_once EE_CAFF_PATH . 'brewing_regular.php'; |
|
334 | - /** @var EE_Brewing_Regular $brew */ |
|
335 | - $brew = $this->loader->getShared(EE_Brewing_Regular::class); |
|
336 | - if (! $this->feature->allowed(FeatureFlag::USE_EDD_PLUGIN_LICENSING)) { |
|
337 | - $brew->initializePUE(); |
|
338 | - } |
|
339 | - add_action( |
|
340 | - 'AHEE__EE_System__load_core_configuration__begin', |
|
341 | - [$brew, 'caffeinated'] |
|
342 | - ); |
|
343 | - } |
|
344 | - } |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * allow addons to load first so that they can set hooks for running DMS's, etc |
|
349 | - * this is hooked into both: |
|
350 | - * 'AHEE__EE_Bootstrap__load_core_configuration' |
|
351 | - * which runs during the WP 'plugins_loaded' action at priority 5 |
|
352 | - * and the WP 'activate_plugin' hook point |
|
353 | - * |
|
354 | - * @return void |
|
355 | - * @throws Exception |
|
356 | - * @throws Throwable |
|
357 | - */ |
|
358 | - public function load_espresso_addons() |
|
359 | - { |
|
360 | - if ( |
|
361 | - $this->feature->allowed(FeatureFlag::USE_EDD_PLUGIN_LICENSING) |
|
362 | - && ! $this->request->isWordPressHeartbeat() |
|
363 | - ) { |
|
364 | - $core_license = new PluginLicense( |
|
365 | - EVENT_ESPRESSO_MAIN_FILE, |
|
366 | - Domain::LICENSE_PLUGIN_ID, |
|
367 | - Domain::LICENSE_PLUGIN_NAME, |
|
368 | - Domain::LICENSE_PLUGIN_SLUG, |
|
369 | - espresso_version() |
|
370 | - ); |
|
371 | - $core_license->setHooks(); |
|
372 | - $this->loader->share(PluginLicense::class, $core_license); |
|
373 | - } |
|
374 | - // looking for hooks? they've been moved into the AddonManager to maintain compatibility |
|
375 | - $this->addon_manager->loadAddons(); |
|
376 | - } |
|
377 | - |
|
378 | - |
|
379 | - /** |
|
380 | - * Checks for activation or upgrade of core first; |
|
381 | - * then also checks if any registered addons have been activated or upgraded |
|
382 | - * This is hooked into 'AHEE__EE_Bootstrap__detect_activations_or_upgrades' |
|
383 | - * which runs during the WP 'plugins_loaded' action at priority 3 |
|
384 | - * |
|
385 | - * @return void |
|
386 | - * @throws EE_Error |
|
387 | - */ |
|
388 | - public function detect_activations_or_upgrades() |
|
389 | - { |
|
390 | - // first off: let's make sure to handle core |
|
391 | - $this->detect_if_activation_or_upgrade(); |
|
392 | - foreach ($this->registry->addons as $addon) { |
|
393 | - if ($addon instanceof EE_Addon) { |
|
394 | - // detect the request type for that addon |
|
395 | - $addon->detect_req_type(); |
|
396 | - } |
|
397 | - } |
|
398 | - } |
|
399 | - |
|
400 | - |
|
401 | - /** |
|
402 | - * Takes care of detecting whether this is a brand new install or code upgrade, |
|
403 | - * and either setting up the DB or setting up maintenance mode etc. |
|
404 | - * |
|
405 | - * @return void |
|
406 | - * @throws EE_Error |
|
407 | - */ |
|
408 | - public function detect_if_activation_or_upgrade() |
|
409 | - { |
|
410 | - do_action('AHEE__EE_System___detect_if_activation_or_upgrade__begin'); |
|
411 | - // check if db has been updated, or if it's a brand-new installation |
|
412 | - $espresso_db_update = $this->fix_espresso_db_upgrade_option(); |
|
413 | - $request_type = $this->detect_req_type($espresso_db_update); |
|
414 | - switch ($request_type) { |
|
415 | - case EE_System::req_type_new_activation: |
|
416 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__new_activation'); |
|
417 | - $this->_handle_core_version_change($espresso_db_update); |
|
418 | - break; |
|
419 | - case EE_System::req_type_reactivation: |
|
420 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__reactivation'); |
|
421 | - $this->_handle_core_version_change($espresso_db_update); |
|
422 | - break; |
|
423 | - case EE_System::req_type_upgrade: |
|
424 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__upgrade'); |
|
425 | - // migrations may be required now that we've upgraded |
|
426 | - $this->maintenance_mode->set_maintenance_mode_if_db_old(); |
|
427 | - $this->_handle_core_version_change($espresso_db_update); |
|
428 | - break; |
|
429 | - case EE_System::req_type_downgrade: |
|
430 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__downgrade'); |
|
431 | - // its possible migrations are no longer required |
|
432 | - $this->maintenance_mode->set_maintenance_mode_if_db_old(); |
|
433 | - $this->_handle_core_version_change($espresso_db_update); |
|
434 | - break; |
|
435 | - case EE_System::req_type_normal: |
|
436 | - default: |
|
437 | - break; |
|
438 | - } |
|
439 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__complete'); |
|
440 | - } |
|
441 | - |
|
442 | - |
|
443 | - /** |
|
444 | - * Updates the list of installed versions and sets hooks for |
|
445 | - * initializing the database later during the request |
|
446 | - * |
|
447 | - * @param array $espresso_db_update |
|
448 | - */ |
|
449 | - private function _handle_core_version_change(array $espresso_db_update) |
|
450 | - { |
|
451 | - $this->update_list_of_installed_versions($espresso_db_update); |
|
452 | - // get ready to verify the DB is ok (provided we aren't in maintenance mode, of course) |
|
453 | - add_action( |
|
454 | - 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
455 | - [$this, 'initialize_db_if_no_migrations_required'] |
|
456 | - ); |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - /** |
|
461 | - * standardizes the wp option 'espresso_db_upgrade' which actually stores |
|
462 | - * information about what versions of EE have been installed and activated, |
|
463 | - * NOT necessarily the state of the database |
|
464 | - * |
|
465 | - * @param mixed $espresso_db_update the value of the WordPress option. |
|
466 | - * If not supplied, fetches it from the options table |
|
467 | - * @return array the correct value of 'espresso_db_upgrade', after saving it, if it needed correction |
|
468 | - */ |
|
469 | - private function fix_espresso_db_upgrade_option($espresso_db_update = null): array |
|
470 | - { |
|
471 | - do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__begin', $espresso_db_update); |
|
472 | - if (! $espresso_db_update) { |
|
473 | - $espresso_db_update = get_option('espresso_db_update'); |
|
474 | - } |
|
475 | - // check that option is an array |
|
476 | - if (! is_array($espresso_db_update)) { |
|
477 | - // if option is FALSE, then it never existed |
|
478 | - if ($espresso_db_update === false) { |
|
479 | - // make $espresso_db_update an array and save option with autoload OFF |
|
480 | - $espresso_db_update = []; |
|
481 | - add_option('espresso_db_update', $espresso_db_update, '', 'no'); |
|
482 | - } else { |
|
483 | - // option is NOT FALSE but also is NOT an array, so make it an array and save it |
|
484 | - $espresso_db_update = [$espresso_db_update => []]; |
|
485 | - update_option('espresso_db_update', $espresso_db_update); |
|
486 | - } |
|
487 | - } else { |
|
488 | - $corrected_db_update = []; |
|
489 | - // if IS an array, but is it an array where KEYS are version numbers, and values are arrays? |
|
490 | - foreach ($espresso_db_update as $should_be_version_string => $should_be_array) { |
|
491 | - if (is_int($should_be_version_string) && ! is_array($should_be_array)) { |
|
492 | - // the key is an int, and the value IS NOT an array |
|
493 | - // so it must be numerically-indexed, where values are versions installed... |
|
494 | - // fix it! |
|
495 | - $version_string = $should_be_array; |
|
496 | - $corrected_db_update[ $version_string ] = ['unknown-date']; |
|
497 | - } else { |
|
498 | - // ok it checks out |
|
499 | - $corrected_db_update[ $should_be_version_string ] = $should_be_array; |
|
500 | - } |
|
501 | - } |
|
502 | - $espresso_db_update = $corrected_db_update; |
|
503 | - update_option('espresso_db_update', $espresso_db_update); |
|
504 | - } |
|
505 | - do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__complete', $espresso_db_update); |
|
506 | - return ! empty($espresso_db_update) |
|
507 | - ? $espresso_db_update |
|
508 | - : []; |
|
509 | - } |
|
510 | - |
|
511 | - |
|
512 | - /** |
|
513 | - * Does the traditional work of setting up the plugin's database and adding default data. |
|
514 | - * If migration script/process did not exist, this is what would happen on every activation/reactivation/upgrade. |
|
515 | - * NOTE: if we're in maintenance mode (which would be the case if we detect there are data |
|
516 | - * migration scripts that need to be run and a version change happens), enqueues core for database initialization, |
|
517 | - * so that it will be done when migrations are finished |
|
518 | - * |
|
519 | - * @param bool $initialize_addons_too if true, we double-check addon's database tables etc too; |
|
520 | - * @param bool $verify_schema if true will re-check the database tables have the correct schema. |
|
521 | - * This is a resource-intensive job |
|
522 | - * so we prefer to only do it when necessary |
|
523 | - * @return void |
|
524 | - * @throws EE_Error |
|
525 | - * @throws ReflectionException |
|
526 | - */ |
|
527 | - public function initialize_db_if_no_migrations_required( |
|
528 | - bool $initialize_addons_too = false, |
|
529 | - bool $verify_schema = true |
|
530 | - ) { |
|
531 | - $request_type = $this->detect_req_type(); |
|
532 | - // only initialize system if we're not in maintenance mode. |
|
533 | - if (! MaintenanceStatus::isFullSite()) { |
|
534 | - /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
535 | - $rewrite_rules = $this->loader->getShared( |
|
536 | - 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
537 | - ); |
|
538 | - $rewrite_rules->flush(); |
|
539 | - if ($verify_schema) { |
|
540 | - EEH_Activation::initialize_db_and_folders(); |
|
541 | - } |
|
542 | - EEH_Activation::initialize_db_content(); |
|
543 | - EEH_Activation::system_initialization(); |
|
544 | - if ($initialize_addons_too) { |
|
545 | - $this->initialize_addons(); |
|
546 | - } |
|
547 | - } else { |
|
548 | - EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for('Core'); |
|
549 | - } |
|
550 | - if ( |
|
551 | - $request_type === EE_System::req_type_new_activation |
|
552 | - || $request_type === EE_System::req_type_reactivation |
|
553 | - || ( |
|
554 | - $request_type === EE_System::req_type_upgrade |
|
555 | - && $this->is_major_version_change() |
|
556 | - ) |
|
557 | - ) { |
|
558 | - add_action('AHEE__EE_System__initialize_last', [$this, 'redirect_to_about_ee'], 9); |
|
559 | - } |
|
560 | - } |
|
561 | - |
|
562 | - |
|
563 | - /** |
|
564 | - * Initializes the db for all registered addons |
|
565 | - * |
|
566 | - * @throws EE_Error |
|
567 | - * @throws ReflectionException |
|
568 | - */ |
|
569 | - public function initialize_addons() |
|
570 | - { |
|
571 | - // foreach registered addon, make sure its db is up-to-date too |
|
572 | - foreach ($this->registry->addons as $addon) { |
|
573 | - if ($addon instanceof EE_Addon) { |
|
574 | - $addon->initialize_db_if_no_migrations_required(); |
|
575 | - } |
|
576 | - } |
|
577 | - } |
|
578 | - |
|
579 | - |
|
580 | - /** |
|
581 | - * Adds the current code version to the saved wp option which stores a list of all ee versions ever installed. |
|
582 | - * |
|
583 | - * @param array|null $version_history |
|
584 | - * @param string|null $current_version_to_add version to be added to the version history |
|
585 | - * @return bool success if this option was changed |
|
586 | - */ |
|
587 | - public function update_list_of_installed_versions( |
|
588 | - ?array $version_history = null, |
|
589 | - ?string $current_version_to_add = null |
|
590 | - ): bool { |
|
591 | - if (! $version_history) { |
|
592 | - $version_history = $this->fix_espresso_db_upgrade_option($version_history); |
|
593 | - } |
|
594 | - if ($current_version_to_add === null) { |
|
595 | - $current_version_to_add = espresso_version(); |
|
596 | - } |
|
597 | - $version_history[ $current_version_to_add ][] = date('Y-m-d H:i:s', time()); |
|
598 | - // re-save |
|
599 | - return update_option('espresso_db_update', $version_history); |
|
600 | - } |
|
601 | - |
|
602 | - |
|
603 | - /** |
|
604 | - * Detects if the current version indicated in the has existed in the list of |
|
605 | - * previously-installed versions of EE (espresso_db_update). Does NOT modify it (ie, no side effect) |
|
606 | - * |
|
607 | - * @param array|null $espresso_db_update array from the wp option stored under the name 'espresso_db_update'. |
|
608 | - * If not supplied, fetches it from the options table. |
|
609 | - * Also, caches its result so later parts of the code can also know whether |
|
610 | - * there's been an update or not. This way we can add the current version to |
|
611 | - * espresso_db_update, but still know if this is a new install or not |
|
612 | - * @return int one of the constants on EE_System::req_type_ |
|
613 | - */ |
|
614 | - public function detect_req_type(?array $espresso_db_update = null): int |
|
615 | - { |
|
616 | - if ($this->_req_type === null) { |
|
617 | - $espresso_db_update = ! empty($espresso_db_update) |
|
618 | - ? $espresso_db_update |
|
619 | - : $this->fix_espresso_db_upgrade_option(); |
|
620 | - $this->_req_type = EE_System::detect_req_type_given_activation_history( |
|
621 | - $espresso_db_update, |
|
622 | - 'ee_espresso_activation', |
|
623 | - espresso_version() |
|
624 | - ); |
|
625 | - $this->_major_version_change = $this->_detect_major_version_change($espresso_db_update); |
|
626 | - $this->request->setIsActivation($this->_req_type !== EE_System::req_type_normal); |
|
627 | - } |
|
628 | - return $this->_req_type; |
|
629 | - } |
|
630 | - |
|
631 | - |
|
632 | - /** |
|
633 | - * Returns whether there was a non-micro version change (ie, change in either |
|
634 | - * the first or second number in the version. Eg 4.9.0.rc.001 to 4.10.0.rc.000, |
|
635 | - * but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
|
636 | - * |
|
637 | - * @param $activation_history |
|
638 | - * @return bool |
|
639 | - */ |
|
640 | - private function _detect_major_version_change($activation_history): bool |
|
641 | - { |
|
642 | - $previous_version = EE_System::getMostRecentlyActiveVersion($activation_history); |
|
643 | - $previous_version_parts = explode('.', $previous_version); |
|
644 | - $current_version_parts = explode('.', espresso_version()); |
|
645 | - return isset( |
|
646 | - $previous_version_parts[0], |
|
647 | - $previous_version_parts[1], |
|
648 | - $current_version_parts[0], |
|
649 | - $current_version_parts[1] |
|
650 | - ) && ( |
|
651 | - $previous_version_parts[0] !== $current_version_parts[0] |
|
652 | - || $previous_version_parts[1] !== $current_version_parts[1] |
|
653 | - ); |
|
654 | - } |
|
655 | - |
|
656 | - |
|
657 | - /** |
|
658 | - * Returns true if either the major or minor version of EE changed during this request. |
|
659 | - * Eg 4.9.0.rc.001 to 4.10.0.rc.000, but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
|
660 | - * |
|
661 | - * @return bool |
|
662 | - */ |
|
663 | - public function is_major_version_change(): bool |
|
664 | - { |
|
665 | - return $this->_major_version_change; |
|
666 | - } |
|
667 | - |
|
668 | - |
|
669 | - /** |
|
670 | - * Determines the request type for any ee addon, given three-piece of info: the current array of activation |
|
671 | - * histories (for core that' 'espresso_db_update' wp option); the name of the WordPress option which is temporarily |
|
672 | - * set upon activation of the plugin (for core it's 'ee_espresso_activation'); and the version that this plugin was |
|
673 | - * just activated to (for core that will always be espresso_version()) |
|
674 | - * |
|
675 | - * @param array|null $activation_history the option's value which stores the activation history for |
|
676 | - * this |
|
677 | - * ee plugin. for core that's 'espresso_db_update' |
|
678 | - * @param string $activation_indicator_option_name the name of the WordPress option that is temporarily set to |
|
679 | - * indicate that this plugin was just activated |
|
680 | - * @param string $current_version the version that was just upgraded to (for core that will be |
|
681 | - * espresso_version()) |
|
682 | - * @return int one of the constants on EE_System::req_type_ |
|
683 | - */ |
|
684 | - public static function detect_req_type_given_activation_history( |
|
685 | - array $activation_history, |
|
686 | - string $activation_indicator_option_name, |
|
687 | - string $current_version |
|
688 | - ): int { |
|
689 | - $version_change = self::compareVersionWithPrevious($activation_history, $current_version); |
|
690 | - $is_activation = get_option($activation_indicator_option_name, false); |
|
691 | - $req_type = self::getRequestType($activation_history, $version_change, $is_activation); |
|
692 | - if ($is_activation) { |
|
693 | - // cleanup in aisle 6 |
|
694 | - delete_option($activation_indicator_option_name); |
|
695 | - } |
|
696 | - return $req_type; |
|
697 | - } |
|
698 | - |
|
699 | - |
|
700 | - /** |
|
701 | - * @param array $activation_history |
|
702 | - * @param int $version_change |
|
703 | - * @param bool $is_activation |
|
704 | - * @return int |
|
705 | - * @since 5.0.0.p |
|
706 | - */ |
|
707 | - private static function getRequestType(array $activation_history, int $version_change, bool $is_activation): int |
|
708 | - { |
|
709 | - // if no previous activation history exists, then this is a brand new install |
|
710 | - if (empty($activation_history)) { |
|
711 | - return EE_System::req_type_new_activation; |
|
712 | - } |
|
713 | - // current version is higher than previous version, so it's an upgrade |
|
714 | - if ($version_change === 1) { |
|
715 | - return EE_System::req_type_upgrade; |
|
716 | - } |
|
717 | - // current version is lower than previous version, so it's a downgrade |
|
718 | - if ($version_change === -1) { |
|
719 | - return EE_System::req_type_downgrade; |
|
720 | - } |
|
721 | - // version hasn't changed since last version so check if the activation indicator is set |
|
722 | - // to determine if it's a reactivation, or just a normal request |
|
723 | - return $is_activation |
|
724 | - ? EE_System::req_type_reactivation |
|
725 | - : EE_System::req_type_normal; |
|
726 | - } |
|
727 | - |
|
728 | - |
|
729 | - /** |
|
730 | - * Detects if the $version_to_upgrade_to is higher than the most recent version in |
|
731 | - * the $activation_history_for_addon |
|
732 | - * |
|
733 | - * @param array $activation_history array where keys are versions, |
|
734 | - * values are arrays of times activated (sometimes 'unknown-date') |
|
735 | - * @param string $current_version |
|
736 | - * @return int results of version_compare( $version_to_upgrade_to, $most_recently_active_version ). |
|
737 | - * -1 if $version_to_upgrade_to is LOWER (downgrade); |
|
738 | - * 0 if $version_to_upgrade_to MATCHES (reactivation or normal request); |
|
739 | - * 1 if $version_to_upgrade_to is HIGHER (upgrade) ; |
|
740 | - */ |
|
741 | - private static function compareVersionWithPrevious(array $activation_history, string $current_version): int |
|
742 | - { |
|
743 | - // find the most recently-activated version |
|
744 | - $most_recently_active_version = EE_System::getMostRecentlyActiveVersion($activation_history); |
|
745 | - return version_compare($current_version, $most_recently_active_version); |
|
746 | - } |
|
747 | - |
|
748 | - |
|
749 | - /** |
|
750 | - * Gets the most recently active version listed in the activation history, |
|
751 | - * and if none are found (ie, it's a brand new install) returns '0.0.0.dev.000'. |
|
752 | - * |
|
753 | - * @param array|null $activation_history (keys are versions, values are arrays of times activated, |
|
754 | - * sometimes containing 'unknown-date' |
|
755 | - * @return string |
|
756 | - */ |
|
757 | - private static function getMostRecentlyActiveVersion(?array $activation_history): string |
|
758 | - { |
|
759 | - $most_recent_activation_date = '1970-01-01 00:00:00'; |
|
760 | - $most_recently_active_version = '0.0.0.dev.000'; |
|
761 | - if (is_array($activation_history)) { |
|
762 | - foreach ($activation_history as $version => $activation_dates) { |
|
763 | - // check there is a record of when this version was activated. |
|
764 | - // Otherwise, mark it as unknown |
|
765 | - if (! $activation_dates) { |
|
766 | - $activation_dates = ['unknown-date']; |
|
767 | - } |
|
768 | - $activation_dates = is_string($activation_dates) |
|
769 | - ? [$activation_dates] |
|
770 | - : $activation_dates; |
|
771 | - foreach ($activation_dates as $activation_date) { |
|
772 | - if ($activation_date !== 'unknown-date' && $activation_date > $most_recent_activation_date) { |
|
773 | - $most_recently_active_version = $version; |
|
774 | - $most_recent_activation_date = $activation_date; |
|
775 | - } |
|
776 | - } |
|
777 | - } |
|
778 | - } |
|
779 | - return $most_recently_active_version; |
|
780 | - } |
|
781 | - |
|
782 | - |
|
783 | - /** |
|
784 | - * This redirects to the about EE page after activation |
|
785 | - * |
|
786 | - * @return void |
|
787 | - */ |
|
788 | - public function redirect_to_about_ee() |
|
789 | - { |
|
790 | - $notices = EE_Error::get_notices(false); |
|
791 | - // if current user is an admin and it's not an ajax or rest request |
|
792 | - if ( |
|
793 | - ! isset($notices['errors']) |
|
794 | - && $this->request->isAdmin() |
|
795 | - && apply_filters( |
|
796 | - 'FHEE__EE_System__redirect_to_about_ee__do_redirect', |
|
797 | - $this->capabilities->current_user_can('manage_options', 'espresso_about_default') |
|
798 | - ) |
|
799 | - ) { |
|
800 | - $query_params = ['page' => 'espresso_about']; |
|
801 | - if (EE_System::instance()->detect_req_type() === EE_System::req_type_new_activation) { |
|
802 | - $query_params['new_activation'] = true; |
|
803 | - } |
|
804 | - if (EE_System::instance()->detect_req_type() === EE_System::req_type_reactivation) { |
|
805 | - $query_params['reactivation'] = true; |
|
806 | - } |
|
807 | - $url = add_query_arg($query_params, admin_url('admin.php')); |
|
808 | - EEH_URL::safeRedirectAndExit($url); |
|
809 | - } |
|
810 | - } |
|
811 | - |
|
812 | - |
|
813 | - /** |
|
814 | - * this is hooked into 'AHEE__EE_Bootstrap__load_core_configuration' |
|
815 | - * which runs during the WP 'plugins_loaded' action at priority 5 |
|
816 | - * |
|
817 | - * @return void |
|
818 | - * @throws ReflectionException |
|
819 | - * @throws Exception |
|
820 | - */ |
|
821 | - public function load_core_configuration() |
|
822 | - { |
|
823 | - do_action('AHEE__EE_System__load_core_configuration__begin', $this); |
|
824 | - /** @var Textdomain $textdomain */ |
|
825 | - $textdomain = $this->loader->getShared(Textdomain::class); |
|
826 | - $textdomain->loadPluginTextdomain(); |
|
827 | - // load and setup EE_Config and EE_Network_Config |
|
828 | - /** @var EE_Config $config */ |
|
829 | - $config = $this->loader->getShared(EE_Config::class); |
|
830 | - $this->loader->getShared(EE_Network_Config::class); |
|
831 | - // setup autoloaders |
|
832 | - // enable logging? |
|
833 | - $this->loader->getShared('EventEspresso\core\services\orm\TrashLogger'); |
|
834 | - if ($config->admin->use_remote_logging) { |
|
835 | - $this->loader->getShared('EE_Log'); |
|
836 | - } |
|
837 | - // check for activation errors |
|
838 | - $activation_errors = get_option('ee_plugin_activation_errors', false); |
|
839 | - if ($activation_errors) { |
|
840 | - EE_Error::add_error($activation_errors, __FILE__, __FUNCTION__, __LINE__); |
|
841 | - update_option('ee_plugin_activation_errors', false); |
|
842 | - } |
|
843 | - // get model names |
|
844 | - $this->_parse_model_names(); |
|
845 | - // configure custom post type definitions |
|
846 | - $this->loader->getShared('EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions'); |
|
847 | - $this->loader->getShared('EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions'); |
|
848 | - do_action('AHEE__EE_System__load_core_configuration__complete', $this); |
|
849 | - } |
|
850 | - |
|
851 | - |
|
852 | - /** |
|
853 | - * cycles through all the models/*.model.php files, and assembles an array of model names |
|
854 | - * |
|
855 | - * @return void |
|
856 | - * @throws ReflectionException |
|
857 | - */ |
|
858 | - private function _parse_model_names() |
|
859 | - { |
|
860 | - // get all the files in the EE_MODELS folder that end in .model.php |
|
861 | - $models = glob(EE_MODELS . '*.model.php'); |
|
862 | - $model_names = []; |
|
863 | - $non_abstract_db_models = []; |
|
864 | - foreach ($models as $model) { |
|
865 | - // get model classname |
|
866 | - $classname = EEH_File::get_classname_from_filepath_with_standard_filename($model); |
|
867 | - $short_name = str_replace('EEM_', '', $classname); |
|
868 | - $reflectionClass = new ReflectionClass($classname); |
|
869 | - if ($reflectionClass->isSubclassOf('EEM_Base') && ! $reflectionClass->isAbstract()) { |
|
870 | - $non_abstract_db_models[ $short_name ] = $classname; |
|
871 | - } |
|
872 | - $model_names[ $short_name ] = $classname; |
|
873 | - } |
|
874 | - $this->registry->models = apply_filters('FHEE__EE_System__parse_model_names', $model_names); |
|
875 | - $this->registry->non_abstract_db_models = apply_filters( |
|
876 | - 'FHEE__EE_System__parse_implemented_model_names', |
|
877 | - $non_abstract_db_models |
|
878 | - ); |
|
879 | - } |
|
880 | - |
|
881 | - |
|
882 | - /** |
|
883 | - * @throws Exception |
|
884 | - * @throws Throwable |
|
885 | - * @since 4.9.71.p |
|
886 | - */ |
|
887 | - public function loadRouteMatchSpecifications() |
|
888 | - { |
|
889 | - try { |
|
890 | - $this->loader->getShared('EventEspresso\core\services\routing\RouteMatchSpecificationManager'); |
|
891 | - $this->loader->getShared('EventEspresso\core\services\routing\RouteCollection'); |
|
892 | - $this->router->loadPrimaryRoutes(); |
|
893 | - } catch (Exception $exception) { |
|
894 | - new ExceptionStackTraceDisplay($exception); |
|
895 | - } |
|
896 | - do_action('AHEE__EE_System__loadRouteMatchSpecifications'); |
|
897 | - } |
|
898 | - |
|
899 | - |
|
900 | - /** |
|
901 | - * loading CPT related classes earlier so that their definitions are available |
|
902 | - * but not performing any actual registration with WP core until load_CPTs_and_session() is called |
|
903 | - * |
|
904 | - * @since 4.10.21.p |
|
905 | - */ |
|
906 | - public function loadCustomPostTypes() |
|
907 | - { |
|
908 | - $this->register_custom_taxonomies = $this->loader->getShared( |
|
909 | - 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies' |
|
910 | - ); |
|
911 | - $this->register_custom_post_types = $this->loader->getShared( |
|
912 | - 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes' |
|
913 | - ); |
|
914 | - $this->register_custom_taxonomy_terms = $this->loader->getShared( |
|
915 | - 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomyTerms' |
|
916 | - ); |
|
917 | - // integrate WP_Query with the EE models |
|
918 | - $this->loader->getShared('EE_CPT_Strategy'); |
|
919 | - // load legacy EE_Request_Handler in case add-ons still need it |
|
920 | - $this->loader->getShared('EE_Request_Handler'); |
|
921 | - } |
|
922 | - |
|
923 | - |
|
924 | - /** |
|
925 | - * generate lists of shortcodes and modules, then verify paths and classes |
|
926 | - * This is hooked into 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets' |
|
927 | - * which runs during the WP 'plugins_loaded' action at priority 7 |
|
928 | - * |
|
929 | - * @return void |
|
930 | - * @throws Exception |
|
931 | - * @throws Throwable |
|
932 | - */ |
|
933 | - public function register_shortcodes_modules_and_widgets() |
|
934 | - { |
|
935 | - $this->router->registerShortcodesModulesAndWidgets(); |
|
936 | - do_action('AHEE__EE_System__register_shortcodes_modules_and_widgets'); |
|
937 | - // check for addons using old hook point |
|
938 | - if (has_action('AHEE__EE_System__register_shortcodes_modules_and_addons')) { |
|
939 | - $this->_incompatible_addon_error(); |
|
940 | - } |
|
941 | - } |
|
942 | - |
|
943 | - |
|
944 | - /** |
|
945 | - * @return void |
|
946 | - */ |
|
947 | - private function _incompatible_addon_error() |
|
948 | - { |
|
949 | - // get array of classes hooking into here |
|
950 | - $class_names = WordPressHooks::getClassNamesForAllCallbacksOnHook( |
|
951 | - 'AHEE__EE_System__register_shortcodes_modules_and_addons' |
|
952 | - ); |
|
953 | - if (! empty($class_names)) { |
|
954 | - $msg = esc_html__( |
|
955 | - 'The following plugins, addons, or modules appear to be incompatible with this version of Event Espresso and were automatically deactivated to avoid fatal errors:', |
|
956 | - 'event_espresso' |
|
957 | - ); |
|
958 | - $msg .= '<ul>'; |
|
959 | - foreach ($class_names as $class_name) { |
|
960 | - $msg .= '<li><b>Event Espresso - ' |
|
961 | - . str_replace( |
|
962 | - ['EE_', 'EEM_', 'EED_', 'EES_', 'EEW_'], |
|
963 | - '', |
|
964 | - $class_name |
|
965 | - ) . '</b></li>'; |
|
966 | - } |
|
967 | - $msg .= '</ul>'; |
|
968 | - $msg .= esc_html__( |
|
969 | - 'Compatibility issues can be avoided and/or resolved by keeping addons and plugins updated to the latest version.', |
|
970 | - 'event_espresso' |
|
971 | - ); |
|
972 | - // save list of incompatible addons to wp-options for later use |
|
973 | - add_option('ee_incompatible_addons', $class_names, '', 'no'); |
|
974 | - if (is_admin()) { |
|
975 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
976 | - } |
|
977 | - } |
|
978 | - } |
|
979 | - |
|
980 | - |
|
981 | - /** |
|
982 | - * begins the process of setting hooks for initializing EE in the correct order |
|
983 | - * This is happening on the 'AHEE__EE_Bootstrap__brew_espresso' hook point |
|
984 | - * which runs during the WP 'init' action at priority 1 |
|
985 | - * |
|
986 | - * @return void |
|
987 | - * @throws Exception |
|
988 | - * @throws Throwable |
|
989 | - */ |
|
990 | - public function brew_espresso() |
|
991 | - { |
|
992 | - add_action('init', [$this, 'set_hooks_for_core'], 5); |
|
993 | - add_action('init', [$this, 'perform_activations_upgrades_and_migrations'], 6); |
|
994 | - add_action('init', [$this, 'load_CPTs_and_session'], 7); |
|
995 | - add_action('init', [$this, 'load_controllers'], 8); |
|
996 | - add_action('init', [$this, 'core_loaded_and_ready'], 9); |
|
997 | - add_action('init', [$this, 'initialize']); |
|
998 | - add_action('init', [$this, 'initialize_last'], 100); |
|
999 | - $this->router->brewEspresso(); |
|
1000 | - $this->loader->getShared('EventEspresso\PaymentMethods\Manager'); |
|
1001 | - $this->loader->getShared('EE_Payment_Method_Manager'); |
|
1002 | - do_action('AHEE__EE_System__brew_espresso__complete', $this); |
|
1003 | - } |
|
1004 | - |
|
1005 | - |
|
1006 | - /** |
|
1007 | - * @return void |
|
1008 | - * @throws EE_Error |
|
1009 | - */ |
|
1010 | - public function set_hooks_for_core() |
|
1011 | - { |
|
1012 | - $this->_deactivate_incompatible_addons(); |
|
1013 | - do_action('AHEE__EE_System__set_hooks_for_core'); |
|
1014 | - $this->loader->getShared('EventEspresso\core\domain\values\session\SessionLifespan'); |
|
1015 | - // caps need to be initialized on every request so that capability maps are set. |
|
1016 | - // @see https://events.codebasehq.com/projects/event-espresso/tickets/8674 |
|
1017 | - $this->capabilities->init_caps(); |
|
1018 | - } |
|
1019 | - |
|
1020 | - |
|
1021 | - /** |
|
1022 | - * Using the information gathered in EE_System::_incompatible_addon_error, |
|
1023 | - * deactivates any addons considered incompatible with the current version of EE |
|
1024 | - */ |
|
1025 | - private function _deactivate_incompatible_addons() |
|
1026 | - { |
|
1027 | - $incompatible_addons = get_option('ee_incompatible_addons', []); |
|
1028 | - if (! empty($incompatible_addons)) { |
|
1029 | - $active_plugins = get_option('active_plugins', []); |
|
1030 | - foreach ($active_plugins as $active_plugin) { |
|
1031 | - foreach ($incompatible_addons as $incompatible_addon) { |
|
1032 | - if (strpos($active_plugin, $incompatible_addon) !== false) { |
|
1033 | - $this->request->unSetRequestParams(['activate'], true); |
|
1034 | - espresso_deactivate_plugin($active_plugin); |
|
1035 | - } |
|
1036 | - } |
|
1037 | - } |
|
1038 | - } |
|
1039 | - } |
|
1040 | - |
|
1041 | - |
|
1042 | - /** |
|
1043 | - * @return void |
|
1044 | - */ |
|
1045 | - public function perform_activations_upgrades_and_migrations() |
|
1046 | - { |
|
1047 | - do_action('AHEE__EE_System__perform_activations_upgrades_and_migrations'); |
|
1048 | - } |
|
1049 | - |
|
1050 | - |
|
1051 | - /** |
|
1052 | - * @return void |
|
1053 | - * @throws DomainException |
|
1054 | - */ |
|
1055 | - public function load_CPTs_and_session() |
|
1056 | - { |
|
1057 | - do_action('AHEE__EE_System__load_CPTs_and_session__start'); |
|
1058 | - $this->register_custom_taxonomies->registerCustomTaxonomies(); |
|
1059 | - $this->register_custom_post_types->registerCustomPostTypes(); |
|
1060 | - $this->register_custom_taxonomy_terms->registerCustomTaxonomyTerms(); |
|
1061 | - do_action('AHEE__EE_System__load_CPTs_and_session__complete'); |
|
1062 | - } |
|
1063 | - |
|
1064 | - |
|
1065 | - /** |
|
1066 | - * this is the best place to load any additional controllers that needs access to EE core. |
|
1067 | - * it is expected that all basic core EE systems, that are not dependent on the current request are loaded at this |
|
1068 | - * time |
|
1069 | - * |
|
1070 | - * @return void |
|
1071 | - * @throws Exception |
|
1072 | - * @throws Throwable |
|
1073 | - */ |
|
1074 | - public function load_controllers() |
|
1075 | - { |
|
1076 | - do_action('AHEE__EE_System__load_controllers__start'); |
|
1077 | - $this->router->loadControllers(); |
|
1078 | - do_action('AHEE__EE_System__load_controllers__complete'); |
|
1079 | - } |
|
1080 | - |
|
1081 | - |
|
1082 | - /** |
|
1083 | - * all the basic EE core should be loaded at this point and available regardless of M-Mode |
|
1084 | - * |
|
1085 | - * @return void |
|
1086 | - * @throws Exception |
|
1087 | - * @throws Throwable |
|
1088 | - */ |
|
1089 | - public function core_loaded_and_ready() |
|
1090 | - { |
|
1091 | - $this->router->coreLoadedAndReady(); |
|
1092 | - do_action('AHEE__EE_System__core_loaded_and_ready'); |
|
1093 | - // always load template tags, because it's faster than checking if it's a front-end request, and many page |
|
1094 | - // builders require these even on the front-end |
|
1095 | - require_once EE_PUBLIC . 'template_tags.php'; |
|
1096 | - do_action('AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons'); |
|
1097 | - } |
|
1098 | - |
|
1099 | - |
|
1100 | - /** |
|
1101 | - * this is the best place to begin initializing client code |
|
1102 | - * |
|
1103 | - * @return void |
|
1104 | - */ |
|
1105 | - public function initialize() |
|
1106 | - { |
|
1107 | - do_action('AHEE__EE_System__initialize'); |
|
1108 | - add_filter( |
|
1109 | - 'safe_style_css', |
|
1110 | - function ($styles) { |
|
1111 | - $styles[] = 'display'; |
|
1112 | - $styles[] = 'visibility'; |
|
1113 | - $styles[] = 'position'; |
|
1114 | - $styles[] = 'top'; |
|
1115 | - $styles[] = 'right'; |
|
1116 | - $styles[] = 'bottom'; |
|
1117 | - $styles[] = 'left'; |
|
1118 | - $styles[] = 'resize'; |
|
1119 | - $styles[] = 'max-width'; |
|
1120 | - $styles[] = 'max-height'; |
|
1121 | - return $styles; |
|
1122 | - } |
|
1123 | - ); |
|
1124 | - } |
|
1125 | - |
|
1126 | - |
|
1127 | - /** |
|
1128 | - * this is run really late during the WP init hook point, and ensures that mostly everything else that needs to |
|
1129 | - * initialize has done so |
|
1130 | - * |
|
1131 | - * @return void |
|
1132 | - * @throws Exception |
|
1133 | - * @throws Throwable |
|
1134 | - */ |
|
1135 | - public function initialize_last() |
|
1136 | - { |
|
1137 | - $this->router->initializeLast(); |
|
1138 | - do_action('AHEE__EE_System__initialize_last'); |
|
1139 | - /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
1140 | - $rewrite_rules = $this->loader->getShared( |
|
1141 | - 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
1142 | - ); |
|
1143 | - $rewrite_rules->flushRewriteRules(); |
|
1144 | - add_action('admin_bar_init', [$this, 'addEspressoToolbar']); |
|
1145 | - } |
|
1146 | - |
|
1147 | - |
|
1148 | - /** |
|
1149 | - * @return void |
|
1150 | - */ |
|
1151 | - public function addEspressoToolbar() |
|
1152 | - { |
|
1153 | - $this->loader->getShared( |
|
1154 | - 'EventEspresso\core\domain\services\admin\AdminToolBar', |
|
1155 | - [$this->capabilities] |
|
1156 | - ); |
|
1157 | - } |
|
1158 | - |
|
1159 | - |
|
1160 | - /** |
|
1161 | - * sets no cache headers and defines no cache constants for WP plugins |
|
1162 | - * |
|
1163 | - * @return void |
|
1164 | - */ |
|
1165 | - public static function do_not_cache() |
|
1166 | - { |
|
1167 | - // set no cache constants |
|
1168 | - if (! defined('DONOTCACHEPAGE')) { |
|
1169 | - define('DONOTCACHEPAGE', true); |
|
1170 | - } |
|
1171 | - if (! defined('DONOTCACHCEOBJECT')) { |
|
1172 | - define('DONOTCACHCEOBJECT', true); |
|
1173 | - } |
|
1174 | - if (! defined('DONOTCACHEDB')) { |
|
1175 | - define('DONOTCACHEDB', true); |
|
1176 | - } |
|
1177 | - // add no cache headers |
|
1178 | - add_action('send_headers', ['EE_System', 'nocache_headers']); |
|
1179 | - // plus a little extra for nginx and Google Chrome |
|
1180 | - add_filter('nocache_headers', ['EE_System', 'extra_nocache_headers']); |
|
1181 | - // prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes with the registration process |
|
1182 | - remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); |
|
1183 | - } |
|
1184 | - |
|
1185 | - |
|
1186 | - /** |
|
1187 | - * @param $headers |
|
1188 | - * @return array |
|
1189 | - */ |
|
1190 | - public static function extra_nocache_headers($headers): array |
|
1191 | - { |
|
1192 | - // for NGINX |
|
1193 | - $headers['X-Accel-Expires'] = 0; |
|
1194 | - // plus extra for Google Chrome since it doesn't seem to respect "no-cache", but WILL respect "no-store" |
|
1195 | - $headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'; |
|
1196 | - return $headers; |
|
1197 | - } |
|
1198 | - |
|
1199 | - |
|
1200 | - /** |
|
1201 | - * @return void |
|
1202 | - */ |
|
1203 | - public static function nocache_headers() |
|
1204 | - { |
|
1205 | - nocache_headers(); |
|
1206 | - } |
|
1207 | - |
|
1208 | - |
|
1209 | - /** |
|
1210 | - * simply hooks into "wp_list_pages_exclude" filter (for wp_list_pages method) and makes sure EE critical pages are |
|
1211 | - * never returned with the function. |
|
1212 | - * |
|
1213 | - * @param array $exclude_array any existing pages being excluded are in this array. |
|
1214 | - * @return array |
|
1215 | - */ |
|
1216 | - public function remove_pages_from_wp_list_pages(array $exclude_array): array |
|
1217 | - { |
|
1218 | - return array_merge($exclude_array, $this->registry->CFG->core->get_critical_pages_array()); |
|
1219 | - } |
|
35 | + /** |
|
36 | + * indicates this is a 'normal' request. Ie, not activation, nor upgrade, nor activation. |
|
37 | + * So examples of this would be a normal GET request on the frontend or backend, or a POST, etc |
|
38 | + */ |
|
39 | + const req_type_normal = 0; |
|
40 | + |
|
41 | + /** |
|
42 | + * Indicates this is a brand-new installation of EE so we should install |
|
43 | + * tables and default data etc |
|
44 | + */ |
|
45 | + const req_type_new_activation = 1; |
|
46 | + |
|
47 | + /** |
|
48 | + * we've detected that EE has been reactivated (or EE was activated during maintenance mode, |
|
49 | + * and we just exited maintenance mode). We MUST check the database is set up properly |
|
50 | + * and that default data is set up too |
|
51 | + */ |
|
52 | + const req_type_reactivation = 2; |
|
53 | + |
|
54 | + /** |
|
55 | + * indicates that EE has been upgraded since its previous request. |
|
56 | + * We may have data migration scripts to call and will want to trigger maintenance mode |
|
57 | + */ |
|
58 | + const req_type_upgrade = 3; |
|
59 | + |
|
60 | + /** |
|
61 | + * TODO will detect that EE has been DOWNGRADED. We probably don't want to run in this case... |
|
62 | + */ |
|
63 | + const req_type_downgrade = 4; |
|
64 | + |
|
65 | + /** |
|
66 | + * @deprecated since version 4.6.0.dev.006 |
|
67 | + * Now whenever a new_activation is detected the request type is still just |
|
68 | + * new_activation (same for reactivation, upgrade, downgrade etc), but if we're in maintenance mode |
|
69 | + * EE_System::initialize_db_if_no_migrations_required and EE_Addon::initialize_db_if_no_migrations_required |
|
70 | + * will instead enqueue that EE plugin's db initialization for when we're taken out of maintenance mode. |
|
71 | + * (Specifically, when the migration manager indicates migrations are finished |
|
72 | + * EE_Data_Migration_Manager::initialize_db_for_enqueued_ee_plugins() will be called) |
|
73 | + */ |
|
74 | + const req_type_activation_but_not_installed = 5; |
|
75 | + |
|
76 | + /** |
|
77 | + * option prefix for recording the activation history (like core's "espresso_db_update") of addons |
|
78 | + */ |
|
79 | + const addon_activation_history_option_prefix = 'ee_addon_activation_history_'; |
|
80 | + |
|
81 | + private static ?EE_System $_instance = null; |
|
82 | + |
|
83 | + private ?LoaderInterface $loader; |
|
84 | + |
|
85 | + private ?EE_Maintenance_Mode $maintenance_mode; |
|
86 | + |
|
87 | + private ?EE_Registry $registry; |
|
88 | + |
|
89 | + private ?RequestInterface $request; |
|
90 | + |
|
91 | + private Router $router; |
|
92 | + |
|
93 | + private ?AddonManager $addon_manager = null; |
|
94 | + |
|
95 | + private ?EE_Capabilities $capabilities = null; |
|
96 | + |
|
97 | + protected ?FeatureFlags $feature = null; |
|
98 | + |
|
99 | + private ?RegisterCustomPostTypes $register_custom_post_types = null; |
|
100 | + |
|
101 | + private ?RegisterCustomTaxonomies $register_custom_taxonomies = null; |
|
102 | + |
|
103 | + private ?RegisterCustomTaxonomyTerms $register_custom_taxonomy_terms = null; |
|
104 | + |
|
105 | + /** |
|
106 | + * Stores which type of request this is, options being one of the constants on EE_System starting with req_type_*. |
|
107 | + * It can be a brand-new activation, a reactivation, an upgrade, a downgrade, or a normal request. |
|
108 | + */ |
|
109 | + private ?int $_req_type = null; |
|
110 | + |
|
111 | + /** |
|
112 | + * Whether there was a non-micro version change in EE core version during this request |
|
113 | + */ |
|
114 | + private bool $_major_version_change = false; |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * @singleton method used to instantiate class object |
|
119 | + * @param LoaderInterface|null $loader |
|
120 | + * @param EE_Maintenance_Mode|null $maintenance_mode |
|
121 | + * @param EE_Registry|null $registry |
|
122 | + * @param RequestInterface|null $request |
|
123 | + * @param Router|null $router |
|
124 | + * @param FeatureFlags|null $feature |
|
125 | + * @return EE_System |
|
126 | + */ |
|
127 | + public static function instance( |
|
128 | + ?LoaderInterface $loader = null, |
|
129 | + ?EE_Maintenance_Mode $maintenance_mode = null, |
|
130 | + ?EE_Registry $registry = null, |
|
131 | + ?RequestInterface $request = null, |
|
132 | + ?Router $router = null, |
|
133 | + ?FeatureFlags $feature = null |
|
134 | + ): EE_System { |
|
135 | + // check if class object is instantiated |
|
136 | + if (! self::$_instance instanceof EE_System) { |
|
137 | + self::$_instance = new self($loader, $maintenance_mode, $registry, $request, $router, $feature); |
|
138 | + } |
|
139 | + return self::$_instance; |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + /** |
|
144 | + * resets the instance and returns it |
|
145 | + * |
|
146 | + * @return EE_System |
|
147 | + * @throws EE_Error |
|
148 | + */ |
|
149 | + public static function reset(): EE_System |
|
150 | + { |
|
151 | + self::$_instance->_req_type = null; |
|
152 | + // make sure none of the old hooks are left hanging around |
|
153 | + remove_all_actions('AHEE__EE_System__perform_activations_upgrades_and_migrations'); |
|
154 | + // we need to reset the migration manager in order for it to detect DMSs properly |
|
155 | + EE_Data_Migration_Manager::reset(); |
|
156 | + self::instance()->detect_activations_or_upgrades(); |
|
157 | + self::instance()->perform_activations_upgrades_and_migrations(); |
|
158 | + return self::instance(); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * sets hooks for running rest of system |
|
164 | + * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
|
165 | + * starting EE Addons from any other point may lead to problems |
|
166 | + * |
|
167 | + * @param LoaderInterface $loader |
|
168 | + * @param EE_Maintenance_Mode $maintenance_mode |
|
169 | + * @param EE_Registry $registry |
|
170 | + * @param RequestInterface $request |
|
171 | + * @param Router $router |
|
172 | + * @param FeatureFlags $feature |
|
173 | + */ |
|
174 | + private function __construct( |
|
175 | + LoaderInterface $loader, |
|
176 | + EE_Maintenance_Mode $maintenance_mode, |
|
177 | + EE_Registry $registry, |
|
178 | + RequestInterface $request, |
|
179 | + Router $router, |
|
180 | + FeatureFlags $feature |
|
181 | + ) { |
|
182 | + $this->registry = $registry; |
|
183 | + $this->loader = $loader; |
|
184 | + $this->request = $request; |
|
185 | + $this->router = $router; |
|
186 | + $this->maintenance_mode = $maintenance_mode; |
|
187 | + $this->feature = $feature; |
|
188 | + do_action('AHEE__EE_System__construct__begin', $this); |
|
189 | + add_action( |
|
190 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
191 | + [$this, 'loadCapabilities'], |
|
192 | + 5 |
|
193 | + ); |
|
194 | + add_action( |
|
195 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
196 | + [$this, 'loadCommandBus'], |
|
197 | + 7 |
|
198 | + ); |
|
199 | + add_action( |
|
200 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
201 | + [$this, 'loadPluginApi'], |
|
202 | + 9 |
|
203 | + ); |
|
204 | + // give caff stuff a chance to play during the activation process too. |
|
205 | + add_action( |
|
206 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
207 | + [$this, 'brewCaffeinated'], |
|
208 | + 9 |
|
209 | + ); |
|
210 | + // allow addons to load first so that they can register autoloaders, set hooks for running DMS's, etc |
|
211 | + add_action( |
|
212 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
213 | + [$this, 'load_espresso_addons'] |
|
214 | + ); |
|
215 | + // when an ee addon is activated, we want to call the core hook(s) again |
|
216 | + // because the newly-activated addon didn't get a chance to run at all |
|
217 | + add_action('activate_plugin', [$this, 'load_espresso_addons'], 1); |
|
218 | + // detect whether install or upgrade |
|
219 | + add_action( |
|
220 | + 'AHEE__EE_Bootstrap__detect_activations_or_upgrades', |
|
221 | + [$this, 'detect_activations_or_upgrades'], |
|
222 | + 3 |
|
223 | + ); |
|
224 | + // load EE_Config, EE_Textdomain, etc |
|
225 | + add_action( |
|
226 | + 'AHEE__EE_Bootstrap__load_core_configuration', |
|
227 | + [$this, 'load_core_configuration'], |
|
228 | + 5 |
|
229 | + ); |
|
230 | + // load specifications for matching routes to current request |
|
231 | + add_action( |
|
232 | + 'AHEE__EE_Bootstrap__load_core_configuration', |
|
233 | + [$this, 'loadRouteMatchSpecifications'] |
|
234 | + ); |
|
235 | + // load specifications for custom post types |
|
236 | + add_action( |
|
237 | + 'AHEE__EE_Bootstrap__load_core_configuration', |
|
238 | + [$this, 'loadCustomPostTypes'] |
|
239 | + ); |
|
240 | + // register shortcodes, modules, and widgets |
|
241 | + add_action( |
|
242 | + 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets', |
|
243 | + [$this, 'register_shortcodes_modules_and_widgets'], |
|
244 | + 7 |
|
245 | + ); |
|
246 | + // you wanna get going? I wanna get going... let's get going! |
|
247 | + add_action( |
|
248 | + 'AHEE__EE_Bootstrap__brew_espresso', |
|
249 | + [$this, 'brew_espresso'], |
|
250 | + 9 |
|
251 | + ); |
|
252 | + // other housekeeping |
|
253 | + // exclude EE critical pages from wp_list_pages |
|
254 | + add_filter( |
|
255 | + 'wp_list_pages_excludes', |
|
256 | + [$this, 'remove_pages_from_wp_list_pages'] |
|
257 | + ); |
|
258 | + // ALL EE Addons should use the following hook point to attach their initial setup too |
|
259 | + // it's extremely important for EE Addons to register any class autoloaders so that they can be available when the EE_Config loads |
|
260 | + do_action('AHEE__EE_System__construct__complete', $this); |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * @return void |
|
266 | + */ |
|
267 | + public function loadCapabilities() |
|
268 | + { |
|
269 | + $this->capabilities = $this->loader->getShared('EE_Capabilities'); |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * create and cache the CommandBus, and also add middleware |
|
275 | + * The CapChecker middleware requires the use of EE_Capabilities |
|
276 | + * which is why we need to load the CommandBus after Caps are set up |
|
277 | + * CommandBus middleware operate FIFO - First In First Out |
|
278 | + * so LocateMovedCommands will run first in order to return any new commands |
|
279 | + * |
|
280 | + * @return void |
|
281 | + */ |
|
282 | + public function loadCommandBus() |
|
283 | + { |
|
284 | + $this->loader->getShared( |
|
285 | + 'CommandBusInterface', |
|
286 | + [ |
|
287 | + null, |
|
288 | + apply_filters( |
|
289 | + 'FHEE__EE_Load_Espresso_Core__handle_request__CommandBus_middleware', |
|
290 | + [ |
|
291 | + $this->loader->getShared('EventEspresso\core\services\commands\middleware\LocateMovedCommands'), |
|
292 | + $this->loader->getShared('EventEspresso\core\services\commands\middleware\CapChecker'), |
|
293 | + $this->loader->getShared('EventEspresso\core\services\commands\middleware\AddActionHook'), |
|
294 | + ] |
|
295 | + ), |
|
296 | + ] |
|
297 | + ); |
|
298 | + } |
|
299 | + |
|
300 | + |
|
301 | + /** |
|
302 | + * @return void |
|
303 | + * @throws Exception |
|
304 | + */ |
|
305 | + public function loadPluginApi() |
|
306 | + { |
|
307 | + $this->addon_manager = $this->loader->getShared(AddonManager::class); |
|
308 | + $this->addon_manager->initialize(); |
|
309 | + $this->loader->getShared('EE_Request_Handler'); |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * The purpose of this method is to simply check for a file named "caffeinated/brewing_regular.php" for any hooks |
|
315 | + * that need to be setup before our EE_System launches. |
|
316 | + * |
|
317 | + * @return void |
|
318 | + * @throws DomainException |
|
319 | + * @throws InvalidArgumentException |
|
320 | + * @throws InvalidDataTypeException |
|
321 | + * @throws InvalidInterfaceException |
|
322 | + * @throws InvalidClassException |
|
323 | + * @throws InvalidFilePathException |
|
324 | + * @throws EE_Error |
|
325 | + * @throws Exception |
|
326 | + */ |
|
327 | + public function brewCaffeinated() |
|
328 | + { |
|
329 | + static $brew; |
|
330 | + /** @var Domain $domain */ |
|
331 | + $domain = DomainFactory::getEventEspressoCoreDomain(); |
|
332 | + if ($domain->isCaffeinated() && ! $brew instanceof EE_Brewing_Regular) { |
|
333 | + require_once EE_CAFF_PATH . 'brewing_regular.php'; |
|
334 | + /** @var EE_Brewing_Regular $brew */ |
|
335 | + $brew = $this->loader->getShared(EE_Brewing_Regular::class); |
|
336 | + if (! $this->feature->allowed(FeatureFlag::USE_EDD_PLUGIN_LICENSING)) { |
|
337 | + $brew->initializePUE(); |
|
338 | + } |
|
339 | + add_action( |
|
340 | + 'AHEE__EE_System__load_core_configuration__begin', |
|
341 | + [$brew, 'caffeinated'] |
|
342 | + ); |
|
343 | + } |
|
344 | + } |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * allow addons to load first so that they can set hooks for running DMS's, etc |
|
349 | + * this is hooked into both: |
|
350 | + * 'AHEE__EE_Bootstrap__load_core_configuration' |
|
351 | + * which runs during the WP 'plugins_loaded' action at priority 5 |
|
352 | + * and the WP 'activate_plugin' hook point |
|
353 | + * |
|
354 | + * @return void |
|
355 | + * @throws Exception |
|
356 | + * @throws Throwable |
|
357 | + */ |
|
358 | + public function load_espresso_addons() |
|
359 | + { |
|
360 | + if ( |
|
361 | + $this->feature->allowed(FeatureFlag::USE_EDD_PLUGIN_LICENSING) |
|
362 | + && ! $this->request->isWordPressHeartbeat() |
|
363 | + ) { |
|
364 | + $core_license = new PluginLicense( |
|
365 | + EVENT_ESPRESSO_MAIN_FILE, |
|
366 | + Domain::LICENSE_PLUGIN_ID, |
|
367 | + Domain::LICENSE_PLUGIN_NAME, |
|
368 | + Domain::LICENSE_PLUGIN_SLUG, |
|
369 | + espresso_version() |
|
370 | + ); |
|
371 | + $core_license->setHooks(); |
|
372 | + $this->loader->share(PluginLicense::class, $core_license); |
|
373 | + } |
|
374 | + // looking for hooks? they've been moved into the AddonManager to maintain compatibility |
|
375 | + $this->addon_manager->loadAddons(); |
|
376 | + } |
|
377 | + |
|
378 | + |
|
379 | + /** |
|
380 | + * Checks for activation or upgrade of core first; |
|
381 | + * then also checks if any registered addons have been activated or upgraded |
|
382 | + * This is hooked into 'AHEE__EE_Bootstrap__detect_activations_or_upgrades' |
|
383 | + * which runs during the WP 'plugins_loaded' action at priority 3 |
|
384 | + * |
|
385 | + * @return void |
|
386 | + * @throws EE_Error |
|
387 | + */ |
|
388 | + public function detect_activations_or_upgrades() |
|
389 | + { |
|
390 | + // first off: let's make sure to handle core |
|
391 | + $this->detect_if_activation_or_upgrade(); |
|
392 | + foreach ($this->registry->addons as $addon) { |
|
393 | + if ($addon instanceof EE_Addon) { |
|
394 | + // detect the request type for that addon |
|
395 | + $addon->detect_req_type(); |
|
396 | + } |
|
397 | + } |
|
398 | + } |
|
399 | + |
|
400 | + |
|
401 | + /** |
|
402 | + * Takes care of detecting whether this is a brand new install or code upgrade, |
|
403 | + * and either setting up the DB or setting up maintenance mode etc. |
|
404 | + * |
|
405 | + * @return void |
|
406 | + * @throws EE_Error |
|
407 | + */ |
|
408 | + public function detect_if_activation_or_upgrade() |
|
409 | + { |
|
410 | + do_action('AHEE__EE_System___detect_if_activation_or_upgrade__begin'); |
|
411 | + // check if db has been updated, or if it's a brand-new installation |
|
412 | + $espresso_db_update = $this->fix_espresso_db_upgrade_option(); |
|
413 | + $request_type = $this->detect_req_type($espresso_db_update); |
|
414 | + switch ($request_type) { |
|
415 | + case EE_System::req_type_new_activation: |
|
416 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__new_activation'); |
|
417 | + $this->_handle_core_version_change($espresso_db_update); |
|
418 | + break; |
|
419 | + case EE_System::req_type_reactivation: |
|
420 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__reactivation'); |
|
421 | + $this->_handle_core_version_change($espresso_db_update); |
|
422 | + break; |
|
423 | + case EE_System::req_type_upgrade: |
|
424 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__upgrade'); |
|
425 | + // migrations may be required now that we've upgraded |
|
426 | + $this->maintenance_mode->set_maintenance_mode_if_db_old(); |
|
427 | + $this->_handle_core_version_change($espresso_db_update); |
|
428 | + break; |
|
429 | + case EE_System::req_type_downgrade: |
|
430 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__downgrade'); |
|
431 | + // its possible migrations are no longer required |
|
432 | + $this->maintenance_mode->set_maintenance_mode_if_db_old(); |
|
433 | + $this->_handle_core_version_change($espresso_db_update); |
|
434 | + break; |
|
435 | + case EE_System::req_type_normal: |
|
436 | + default: |
|
437 | + break; |
|
438 | + } |
|
439 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__complete'); |
|
440 | + } |
|
441 | + |
|
442 | + |
|
443 | + /** |
|
444 | + * Updates the list of installed versions and sets hooks for |
|
445 | + * initializing the database later during the request |
|
446 | + * |
|
447 | + * @param array $espresso_db_update |
|
448 | + */ |
|
449 | + private function _handle_core_version_change(array $espresso_db_update) |
|
450 | + { |
|
451 | + $this->update_list_of_installed_versions($espresso_db_update); |
|
452 | + // get ready to verify the DB is ok (provided we aren't in maintenance mode, of course) |
|
453 | + add_action( |
|
454 | + 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
455 | + [$this, 'initialize_db_if_no_migrations_required'] |
|
456 | + ); |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + /** |
|
461 | + * standardizes the wp option 'espresso_db_upgrade' which actually stores |
|
462 | + * information about what versions of EE have been installed and activated, |
|
463 | + * NOT necessarily the state of the database |
|
464 | + * |
|
465 | + * @param mixed $espresso_db_update the value of the WordPress option. |
|
466 | + * If not supplied, fetches it from the options table |
|
467 | + * @return array the correct value of 'espresso_db_upgrade', after saving it, if it needed correction |
|
468 | + */ |
|
469 | + private function fix_espresso_db_upgrade_option($espresso_db_update = null): array |
|
470 | + { |
|
471 | + do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__begin', $espresso_db_update); |
|
472 | + if (! $espresso_db_update) { |
|
473 | + $espresso_db_update = get_option('espresso_db_update'); |
|
474 | + } |
|
475 | + // check that option is an array |
|
476 | + if (! is_array($espresso_db_update)) { |
|
477 | + // if option is FALSE, then it never existed |
|
478 | + if ($espresso_db_update === false) { |
|
479 | + // make $espresso_db_update an array and save option with autoload OFF |
|
480 | + $espresso_db_update = []; |
|
481 | + add_option('espresso_db_update', $espresso_db_update, '', 'no'); |
|
482 | + } else { |
|
483 | + // option is NOT FALSE but also is NOT an array, so make it an array and save it |
|
484 | + $espresso_db_update = [$espresso_db_update => []]; |
|
485 | + update_option('espresso_db_update', $espresso_db_update); |
|
486 | + } |
|
487 | + } else { |
|
488 | + $corrected_db_update = []; |
|
489 | + // if IS an array, but is it an array where KEYS are version numbers, and values are arrays? |
|
490 | + foreach ($espresso_db_update as $should_be_version_string => $should_be_array) { |
|
491 | + if (is_int($should_be_version_string) && ! is_array($should_be_array)) { |
|
492 | + // the key is an int, and the value IS NOT an array |
|
493 | + // so it must be numerically-indexed, where values are versions installed... |
|
494 | + // fix it! |
|
495 | + $version_string = $should_be_array; |
|
496 | + $corrected_db_update[ $version_string ] = ['unknown-date']; |
|
497 | + } else { |
|
498 | + // ok it checks out |
|
499 | + $corrected_db_update[ $should_be_version_string ] = $should_be_array; |
|
500 | + } |
|
501 | + } |
|
502 | + $espresso_db_update = $corrected_db_update; |
|
503 | + update_option('espresso_db_update', $espresso_db_update); |
|
504 | + } |
|
505 | + do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__complete', $espresso_db_update); |
|
506 | + return ! empty($espresso_db_update) |
|
507 | + ? $espresso_db_update |
|
508 | + : []; |
|
509 | + } |
|
510 | + |
|
511 | + |
|
512 | + /** |
|
513 | + * Does the traditional work of setting up the plugin's database and adding default data. |
|
514 | + * If migration script/process did not exist, this is what would happen on every activation/reactivation/upgrade. |
|
515 | + * NOTE: if we're in maintenance mode (which would be the case if we detect there are data |
|
516 | + * migration scripts that need to be run and a version change happens), enqueues core for database initialization, |
|
517 | + * so that it will be done when migrations are finished |
|
518 | + * |
|
519 | + * @param bool $initialize_addons_too if true, we double-check addon's database tables etc too; |
|
520 | + * @param bool $verify_schema if true will re-check the database tables have the correct schema. |
|
521 | + * This is a resource-intensive job |
|
522 | + * so we prefer to only do it when necessary |
|
523 | + * @return void |
|
524 | + * @throws EE_Error |
|
525 | + * @throws ReflectionException |
|
526 | + */ |
|
527 | + public function initialize_db_if_no_migrations_required( |
|
528 | + bool $initialize_addons_too = false, |
|
529 | + bool $verify_schema = true |
|
530 | + ) { |
|
531 | + $request_type = $this->detect_req_type(); |
|
532 | + // only initialize system if we're not in maintenance mode. |
|
533 | + if (! MaintenanceStatus::isFullSite()) { |
|
534 | + /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
535 | + $rewrite_rules = $this->loader->getShared( |
|
536 | + 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
537 | + ); |
|
538 | + $rewrite_rules->flush(); |
|
539 | + if ($verify_schema) { |
|
540 | + EEH_Activation::initialize_db_and_folders(); |
|
541 | + } |
|
542 | + EEH_Activation::initialize_db_content(); |
|
543 | + EEH_Activation::system_initialization(); |
|
544 | + if ($initialize_addons_too) { |
|
545 | + $this->initialize_addons(); |
|
546 | + } |
|
547 | + } else { |
|
548 | + EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for('Core'); |
|
549 | + } |
|
550 | + if ( |
|
551 | + $request_type === EE_System::req_type_new_activation |
|
552 | + || $request_type === EE_System::req_type_reactivation |
|
553 | + || ( |
|
554 | + $request_type === EE_System::req_type_upgrade |
|
555 | + && $this->is_major_version_change() |
|
556 | + ) |
|
557 | + ) { |
|
558 | + add_action('AHEE__EE_System__initialize_last', [$this, 'redirect_to_about_ee'], 9); |
|
559 | + } |
|
560 | + } |
|
561 | + |
|
562 | + |
|
563 | + /** |
|
564 | + * Initializes the db for all registered addons |
|
565 | + * |
|
566 | + * @throws EE_Error |
|
567 | + * @throws ReflectionException |
|
568 | + */ |
|
569 | + public function initialize_addons() |
|
570 | + { |
|
571 | + // foreach registered addon, make sure its db is up-to-date too |
|
572 | + foreach ($this->registry->addons as $addon) { |
|
573 | + if ($addon instanceof EE_Addon) { |
|
574 | + $addon->initialize_db_if_no_migrations_required(); |
|
575 | + } |
|
576 | + } |
|
577 | + } |
|
578 | + |
|
579 | + |
|
580 | + /** |
|
581 | + * Adds the current code version to the saved wp option which stores a list of all ee versions ever installed. |
|
582 | + * |
|
583 | + * @param array|null $version_history |
|
584 | + * @param string|null $current_version_to_add version to be added to the version history |
|
585 | + * @return bool success if this option was changed |
|
586 | + */ |
|
587 | + public function update_list_of_installed_versions( |
|
588 | + ?array $version_history = null, |
|
589 | + ?string $current_version_to_add = null |
|
590 | + ): bool { |
|
591 | + if (! $version_history) { |
|
592 | + $version_history = $this->fix_espresso_db_upgrade_option($version_history); |
|
593 | + } |
|
594 | + if ($current_version_to_add === null) { |
|
595 | + $current_version_to_add = espresso_version(); |
|
596 | + } |
|
597 | + $version_history[ $current_version_to_add ][] = date('Y-m-d H:i:s', time()); |
|
598 | + // re-save |
|
599 | + return update_option('espresso_db_update', $version_history); |
|
600 | + } |
|
601 | + |
|
602 | + |
|
603 | + /** |
|
604 | + * Detects if the current version indicated in the has existed in the list of |
|
605 | + * previously-installed versions of EE (espresso_db_update). Does NOT modify it (ie, no side effect) |
|
606 | + * |
|
607 | + * @param array|null $espresso_db_update array from the wp option stored under the name 'espresso_db_update'. |
|
608 | + * If not supplied, fetches it from the options table. |
|
609 | + * Also, caches its result so later parts of the code can also know whether |
|
610 | + * there's been an update or not. This way we can add the current version to |
|
611 | + * espresso_db_update, but still know if this is a new install or not |
|
612 | + * @return int one of the constants on EE_System::req_type_ |
|
613 | + */ |
|
614 | + public function detect_req_type(?array $espresso_db_update = null): int |
|
615 | + { |
|
616 | + if ($this->_req_type === null) { |
|
617 | + $espresso_db_update = ! empty($espresso_db_update) |
|
618 | + ? $espresso_db_update |
|
619 | + : $this->fix_espresso_db_upgrade_option(); |
|
620 | + $this->_req_type = EE_System::detect_req_type_given_activation_history( |
|
621 | + $espresso_db_update, |
|
622 | + 'ee_espresso_activation', |
|
623 | + espresso_version() |
|
624 | + ); |
|
625 | + $this->_major_version_change = $this->_detect_major_version_change($espresso_db_update); |
|
626 | + $this->request->setIsActivation($this->_req_type !== EE_System::req_type_normal); |
|
627 | + } |
|
628 | + return $this->_req_type; |
|
629 | + } |
|
630 | + |
|
631 | + |
|
632 | + /** |
|
633 | + * Returns whether there was a non-micro version change (ie, change in either |
|
634 | + * the first or second number in the version. Eg 4.9.0.rc.001 to 4.10.0.rc.000, |
|
635 | + * but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
|
636 | + * |
|
637 | + * @param $activation_history |
|
638 | + * @return bool |
|
639 | + */ |
|
640 | + private function _detect_major_version_change($activation_history): bool |
|
641 | + { |
|
642 | + $previous_version = EE_System::getMostRecentlyActiveVersion($activation_history); |
|
643 | + $previous_version_parts = explode('.', $previous_version); |
|
644 | + $current_version_parts = explode('.', espresso_version()); |
|
645 | + return isset( |
|
646 | + $previous_version_parts[0], |
|
647 | + $previous_version_parts[1], |
|
648 | + $current_version_parts[0], |
|
649 | + $current_version_parts[1] |
|
650 | + ) && ( |
|
651 | + $previous_version_parts[0] !== $current_version_parts[0] |
|
652 | + || $previous_version_parts[1] !== $current_version_parts[1] |
|
653 | + ); |
|
654 | + } |
|
655 | + |
|
656 | + |
|
657 | + /** |
|
658 | + * Returns true if either the major or minor version of EE changed during this request. |
|
659 | + * Eg 4.9.0.rc.001 to 4.10.0.rc.000, but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
|
660 | + * |
|
661 | + * @return bool |
|
662 | + */ |
|
663 | + public function is_major_version_change(): bool |
|
664 | + { |
|
665 | + return $this->_major_version_change; |
|
666 | + } |
|
667 | + |
|
668 | + |
|
669 | + /** |
|
670 | + * Determines the request type for any ee addon, given three-piece of info: the current array of activation |
|
671 | + * histories (for core that' 'espresso_db_update' wp option); the name of the WordPress option which is temporarily |
|
672 | + * set upon activation of the plugin (for core it's 'ee_espresso_activation'); and the version that this plugin was |
|
673 | + * just activated to (for core that will always be espresso_version()) |
|
674 | + * |
|
675 | + * @param array|null $activation_history the option's value which stores the activation history for |
|
676 | + * this |
|
677 | + * ee plugin. for core that's 'espresso_db_update' |
|
678 | + * @param string $activation_indicator_option_name the name of the WordPress option that is temporarily set to |
|
679 | + * indicate that this plugin was just activated |
|
680 | + * @param string $current_version the version that was just upgraded to (for core that will be |
|
681 | + * espresso_version()) |
|
682 | + * @return int one of the constants on EE_System::req_type_ |
|
683 | + */ |
|
684 | + public static function detect_req_type_given_activation_history( |
|
685 | + array $activation_history, |
|
686 | + string $activation_indicator_option_name, |
|
687 | + string $current_version |
|
688 | + ): int { |
|
689 | + $version_change = self::compareVersionWithPrevious($activation_history, $current_version); |
|
690 | + $is_activation = get_option($activation_indicator_option_name, false); |
|
691 | + $req_type = self::getRequestType($activation_history, $version_change, $is_activation); |
|
692 | + if ($is_activation) { |
|
693 | + // cleanup in aisle 6 |
|
694 | + delete_option($activation_indicator_option_name); |
|
695 | + } |
|
696 | + return $req_type; |
|
697 | + } |
|
698 | + |
|
699 | + |
|
700 | + /** |
|
701 | + * @param array $activation_history |
|
702 | + * @param int $version_change |
|
703 | + * @param bool $is_activation |
|
704 | + * @return int |
|
705 | + * @since 5.0.0.p |
|
706 | + */ |
|
707 | + private static function getRequestType(array $activation_history, int $version_change, bool $is_activation): int |
|
708 | + { |
|
709 | + // if no previous activation history exists, then this is a brand new install |
|
710 | + if (empty($activation_history)) { |
|
711 | + return EE_System::req_type_new_activation; |
|
712 | + } |
|
713 | + // current version is higher than previous version, so it's an upgrade |
|
714 | + if ($version_change === 1) { |
|
715 | + return EE_System::req_type_upgrade; |
|
716 | + } |
|
717 | + // current version is lower than previous version, so it's a downgrade |
|
718 | + if ($version_change === -1) { |
|
719 | + return EE_System::req_type_downgrade; |
|
720 | + } |
|
721 | + // version hasn't changed since last version so check if the activation indicator is set |
|
722 | + // to determine if it's a reactivation, or just a normal request |
|
723 | + return $is_activation |
|
724 | + ? EE_System::req_type_reactivation |
|
725 | + : EE_System::req_type_normal; |
|
726 | + } |
|
727 | + |
|
728 | + |
|
729 | + /** |
|
730 | + * Detects if the $version_to_upgrade_to is higher than the most recent version in |
|
731 | + * the $activation_history_for_addon |
|
732 | + * |
|
733 | + * @param array $activation_history array where keys are versions, |
|
734 | + * values are arrays of times activated (sometimes 'unknown-date') |
|
735 | + * @param string $current_version |
|
736 | + * @return int results of version_compare( $version_to_upgrade_to, $most_recently_active_version ). |
|
737 | + * -1 if $version_to_upgrade_to is LOWER (downgrade); |
|
738 | + * 0 if $version_to_upgrade_to MATCHES (reactivation or normal request); |
|
739 | + * 1 if $version_to_upgrade_to is HIGHER (upgrade) ; |
|
740 | + */ |
|
741 | + private static function compareVersionWithPrevious(array $activation_history, string $current_version): int |
|
742 | + { |
|
743 | + // find the most recently-activated version |
|
744 | + $most_recently_active_version = EE_System::getMostRecentlyActiveVersion($activation_history); |
|
745 | + return version_compare($current_version, $most_recently_active_version); |
|
746 | + } |
|
747 | + |
|
748 | + |
|
749 | + /** |
|
750 | + * Gets the most recently active version listed in the activation history, |
|
751 | + * and if none are found (ie, it's a brand new install) returns '0.0.0.dev.000'. |
|
752 | + * |
|
753 | + * @param array|null $activation_history (keys are versions, values are arrays of times activated, |
|
754 | + * sometimes containing 'unknown-date' |
|
755 | + * @return string |
|
756 | + */ |
|
757 | + private static function getMostRecentlyActiveVersion(?array $activation_history): string |
|
758 | + { |
|
759 | + $most_recent_activation_date = '1970-01-01 00:00:00'; |
|
760 | + $most_recently_active_version = '0.0.0.dev.000'; |
|
761 | + if (is_array($activation_history)) { |
|
762 | + foreach ($activation_history as $version => $activation_dates) { |
|
763 | + // check there is a record of when this version was activated. |
|
764 | + // Otherwise, mark it as unknown |
|
765 | + if (! $activation_dates) { |
|
766 | + $activation_dates = ['unknown-date']; |
|
767 | + } |
|
768 | + $activation_dates = is_string($activation_dates) |
|
769 | + ? [$activation_dates] |
|
770 | + : $activation_dates; |
|
771 | + foreach ($activation_dates as $activation_date) { |
|
772 | + if ($activation_date !== 'unknown-date' && $activation_date > $most_recent_activation_date) { |
|
773 | + $most_recently_active_version = $version; |
|
774 | + $most_recent_activation_date = $activation_date; |
|
775 | + } |
|
776 | + } |
|
777 | + } |
|
778 | + } |
|
779 | + return $most_recently_active_version; |
|
780 | + } |
|
781 | + |
|
782 | + |
|
783 | + /** |
|
784 | + * This redirects to the about EE page after activation |
|
785 | + * |
|
786 | + * @return void |
|
787 | + */ |
|
788 | + public function redirect_to_about_ee() |
|
789 | + { |
|
790 | + $notices = EE_Error::get_notices(false); |
|
791 | + // if current user is an admin and it's not an ajax or rest request |
|
792 | + if ( |
|
793 | + ! isset($notices['errors']) |
|
794 | + && $this->request->isAdmin() |
|
795 | + && apply_filters( |
|
796 | + 'FHEE__EE_System__redirect_to_about_ee__do_redirect', |
|
797 | + $this->capabilities->current_user_can('manage_options', 'espresso_about_default') |
|
798 | + ) |
|
799 | + ) { |
|
800 | + $query_params = ['page' => 'espresso_about']; |
|
801 | + if (EE_System::instance()->detect_req_type() === EE_System::req_type_new_activation) { |
|
802 | + $query_params['new_activation'] = true; |
|
803 | + } |
|
804 | + if (EE_System::instance()->detect_req_type() === EE_System::req_type_reactivation) { |
|
805 | + $query_params['reactivation'] = true; |
|
806 | + } |
|
807 | + $url = add_query_arg($query_params, admin_url('admin.php')); |
|
808 | + EEH_URL::safeRedirectAndExit($url); |
|
809 | + } |
|
810 | + } |
|
811 | + |
|
812 | + |
|
813 | + /** |
|
814 | + * this is hooked into 'AHEE__EE_Bootstrap__load_core_configuration' |
|
815 | + * which runs during the WP 'plugins_loaded' action at priority 5 |
|
816 | + * |
|
817 | + * @return void |
|
818 | + * @throws ReflectionException |
|
819 | + * @throws Exception |
|
820 | + */ |
|
821 | + public function load_core_configuration() |
|
822 | + { |
|
823 | + do_action('AHEE__EE_System__load_core_configuration__begin', $this); |
|
824 | + /** @var Textdomain $textdomain */ |
|
825 | + $textdomain = $this->loader->getShared(Textdomain::class); |
|
826 | + $textdomain->loadPluginTextdomain(); |
|
827 | + // load and setup EE_Config and EE_Network_Config |
|
828 | + /** @var EE_Config $config */ |
|
829 | + $config = $this->loader->getShared(EE_Config::class); |
|
830 | + $this->loader->getShared(EE_Network_Config::class); |
|
831 | + // setup autoloaders |
|
832 | + // enable logging? |
|
833 | + $this->loader->getShared('EventEspresso\core\services\orm\TrashLogger'); |
|
834 | + if ($config->admin->use_remote_logging) { |
|
835 | + $this->loader->getShared('EE_Log'); |
|
836 | + } |
|
837 | + // check for activation errors |
|
838 | + $activation_errors = get_option('ee_plugin_activation_errors', false); |
|
839 | + if ($activation_errors) { |
|
840 | + EE_Error::add_error($activation_errors, __FILE__, __FUNCTION__, __LINE__); |
|
841 | + update_option('ee_plugin_activation_errors', false); |
|
842 | + } |
|
843 | + // get model names |
|
844 | + $this->_parse_model_names(); |
|
845 | + // configure custom post type definitions |
|
846 | + $this->loader->getShared('EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions'); |
|
847 | + $this->loader->getShared('EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions'); |
|
848 | + do_action('AHEE__EE_System__load_core_configuration__complete', $this); |
|
849 | + } |
|
850 | + |
|
851 | + |
|
852 | + /** |
|
853 | + * cycles through all the models/*.model.php files, and assembles an array of model names |
|
854 | + * |
|
855 | + * @return void |
|
856 | + * @throws ReflectionException |
|
857 | + */ |
|
858 | + private function _parse_model_names() |
|
859 | + { |
|
860 | + // get all the files in the EE_MODELS folder that end in .model.php |
|
861 | + $models = glob(EE_MODELS . '*.model.php'); |
|
862 | + $model_names = []; |
|
863 | + $non_abstract_db_models = []; |
|
864 | + foreach ($models as $model) { |
|
865 | + // get model classname |
|
866 | + $classname = EEH_File::get_classname_from_filepath_with_standard_filename($model); |
|
867 | + $short_name = str_replace('EEM_', '', $classname); |
|
868 | + $reflectionClass = new ReflectionClass($classname); |
|
869 | + if ($reflectionClass->isSubclassOf('EEM_Base') && ! $reflectionClass->isAbstract()) { |
|
870 | + $non_abstract_db_models[ $short_name ] = $classname; |
|
871 | + } |
|
872 | + $model_names[ $short_name ] = $classname; |
|
873 | + } |
|
874 | + $this->registry->models = apply_filters('FHEE__EE_System__parse_model_names', $model_names); |
|
875 | + $this->registry->non_abstract_db_models = apply_filters( |
|
876 | + 'FHEE__EE_System__parse_implemented_model_names', |
|
877 | + $non_abstract_db_models |
|
878 | + ); |
|
879 | + } |
|
880 | + |
|
881 | + |
|
882 | + /** |
|
883 | + * @throws Exception |
|
884 | + * @throws Throwable |
|
885 | + * @since 4.9.71.p |
|
886 | + */ |
|
887 | + public function loadRouteMatchSpecifications() |
|
888 | + { |
|
889 | + try { |
|
890 | + $this->loader->getShared('EventEspresso\core\services\routing\RouteMatchSpecificationManager'); |
|
891 | + $this->loader->getShared('EventEspresso\core\services\routing\RouteCollection'); |
|
892 | + $this->router->loadPrimaryRoutes(); |
|
893 | + } catch (Exception $exception) { |
|
894 | + new ExceptionStackTraceDisplay($exception); |
|
895 | + } |
|
896 | + do_action('AHEE__EE_System__loadRouteMatchSpecifications'); |
|
897 | + } |
|
898 | + |
|
899 | + |
|
900 | + /** |
|
901 | + * loading CPT related classes earlier so that their definitions are available |
|
902 | + * but not performing any actual registration with WP core until load_CPTs_and_session() is called |
|
903 | + * |
|
904 | + * @since 4.10.21.p |
|
905 | + */ |
|
906 | + public function loadCustomPostTypes() |
|
907 | + { |
|
908 | + $this->register_custom_taxonomies = $this->loader->getShared( |
|
909 | + 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies' |
|
910 | + ); |
|
911 | + $this->register_custom_post_types = $this->loader->getShared( |
|
912 | + 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes' |
|
913 | + ); |
|
914 | + $this->register_custom_taxonomy_terms = $this->loader->getShared( |
|
915 | + 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomyTerms' |
|
916 | + ); |
|
917 | + // integrate WP_Query with the EE models |
|
918 | + $this->loader->getShared('EE_CPT_Strategy'); |
|
919 | + // load legacy EE_Request_Handler in case add-ons still need it |
|
920 | + $this->loader->getShared('EE_Request_Handler'); |
|
921 | + } |
|
922 | + |
|
923 | + |
|
924 | + /** |
|
925 | + * generate lists of shortcodes and modules, then verify paths and classes |
|
926 | + * This is hooked into 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets' |
|
927 | + * which runs during the WP 'plugins_loaded' action at priority 7 |
|
928 | + * |
|
929 | + * @return void |
|
930 | + * @throws Exception |
|
931 | + * @throws Throwable |
|
932 | + */ |
|
933 | + public function register_shortcodes_modules_and_widgets() |
|
934 | + { |
|
935 | + $this->router->registerShortcodesModulesAndWidgets(); |
|
936 | + do_action('AHEE__EE_System__register_shortcodes_modules_and_widgets'); |
|
937 | + // check for addons using old hook point |
|
938 | + if (has_action('AHEE__EE_System__register_shortcodes_modules_and_addons')) { |
|
939 | + $this->_incompatible_addon_error(); |
|
940 | + } |
|
941 | + } |
|
942 | + |
|
943 | + |
|
944 | + /** |
|
945 | + * @return void |
|
946 | + */ |
|
947 | + private function _incompatible_addon_error() |
|
948 | + { |
|
949 | + // get array of classes hooking into here |
|
950 | + $class_names = WordPressHooks::getClassNamesForAllCallbacksOnHook( |
|
951 | + 'AHEE__EE_System__register_shortcodes_modules_and_addons' |
|
952 | + ); |
|
953 | + if (! empty($class_names)) { |
|
954 | + $msg = esc_html__( |
|
955 | + 'The following plugins, addons, or modules appear to be incompatible with this version of Event Espresso and were automatically deactivated to avoid fatal errors:', |
|
956 | + 'event_espresso' |
|
957 | + ); |
|
958 | + $msg .= '<ul>'; |
|
959 | + foreach ($class_names as $class_name) { |
|
960 | + $msg .= '<li><b>Event Espresso - ' |
|
961 | + . str_replace( |
|
962 | + ['EE_', 'EEM_', 'EED_', 'EES_', 'EEW_'], |
|
963 | + '', |
|
964 | + $class_name |
|
965 | + ) . '</b></li>'; |
|
966 | + } |
|
967 | + $msg .= '</ul>'; |
|
968 | + $msg .= esc_html__( |
|
969 | + 'Compatibility issues can be avoided and/or resolved by keeping addons and plugins updated to the latest version.', |
|
970 | + 'event_espresso' |
|
971 | + ); |
|
972 | + // save list of incompatible addons to wp-options for later use |
|
973 | + add_option('ee_incompatible_addons', $class_names, '', 'no'); |
|
974 | + if (is_admin()) { |
|
975 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
976 | + } |
|
977 | + } |
|
978 | + } |
|
979 | + |
|
980 | + |
|
981 | + /** |
|
982 | + * begins the process of setting hooks for initializing EE in the correct order |
|
983 | + * This is happening on the 'AHEE__EE_Bootstrap__brew_espresso' hook point |
|
984 | + * which runs during the WP 'init' action at priority 1 |
|
985 | + * |
|
986 | + * @return void |
|
987 | + * @throws Exception |
|
988 | + * @throws Throwable |
|
989 | + */ |
|
990 | + public function brew_espresso() |
|
991 | + { |
|
992 | + add_action('init', [$this, 'set_hooks_for_core'], 5); |
|
993 | + add_action('init', [$this, 'perform_activations_upgrades_and_migrations'], 6); |
|
994 | + add_action('init', [$this, 'load_CPTs_and_session'], 7); |
|
995 | + add_action('init', [$this, 'load_controllers'], 8); |
|
996 | + add_action('init', [$this, 'core_loaded_and_ready'], 9); |
|
997 | + add_action('init', [$this, 'initialize']); |
|
998 | + add_action('init', [$this, 'initialize_last'], 100); |
|
999 | + $this->router->brewEspresso(); |
|
1000 | + $this->loader->getShared('EventEspresso\PaymentMethods\Manager'); |
|
1001 | + $this->loader->getShared('EE_Payment_Method_Manager'); |
|
1002 | + do_action('AHEE__EE_System__brew_espresso__complete', $this); |
|
1003 | + } |
|
1004 | + |
|
1005 | + |
|
1006 | + /** |
|
1007 | + * @return void |
|
1008 | + * @throws EE_Error |
|
1009 | + */ |
|
1010 | + public function set_hooks_for_core() |
|
1011 | + { |
|
1012 | + $this->_deactivate_incompatible_addons(); |
|
1013 | + do_action('AHEE__EE_System__set_hooks_for_core'); |
|
1014 | + $this->loader->getShared('EventEspresso\core\domain\values\session\SessionLifespan'); |
|
1015 | + // caps need to be initialized on every request so that capability maps are set. |
|
1016 | + // @see https://events.codebasehq.com/projects/event-espresso/tickets/8674 |
|
1017 | + $this->capabilities->init_caps(); |
|
1018 | + } |
|
1019 | + |
|
1020 | + |
|
1021 | + /** |
|
1022 | + * Using the information gathered in EE_System::_incompatible_addon_error, |
|
1023 | + * deactivates any addons considered incompatible with the current version of EE |
|
1024 | + */ |
|
1025 | + private function _deactivate_incompatible_addons() |
|
1026 | + { |
|
1027 | + $incompatible_addons = get_option('ee_incompatible_addons', []); |
|
1028 | + if (! empty($incompatible_addons)) { |
|
1029 | + $active_plugins = get_option('active_plugins', []); |
|
1030 | + foreach ($active_plugins as $active_plugin) { |
|
1031 | + foreach ($incompatible_addons as $incompatible_addon) { |
|
1032 | + if (strpos($active_plugin, $incompatible_addon) !== false) { |
|
1033 | + $this->request->unSetRequestParams(['activate'], true); |
|
1034 | + espresso_deactivate_plugin($active_plugin); |
|
1035 | + } |
|
1036 | + } |
|
1037 | + } |
|
1038 | + } |
|
1039 | + } |
|
1040 | + |
|
1041 | + |
|
1042 | + /** |
|
1043 | + * @return void |
|
1044 | + */ |
|
1045 | + public function perform_activations_upgrades_and_migrations() |
|
1046 | + { |
|
1047 | + do_action('AHEE__EE_System__perform_activations_upgrades_and_migrations'); |
|
1048 | + } |
|
1049 | + |
|
1050 | + |
|
1051 | + /** |
|
1052 | + * @return void |
|
1053 | + * @throws DomainException |
|
1054 | + */ |
|
1055 | + public function load_CPTs_and_session() |
|
1056 | + { |
|
1057 | + do_action('AHEE__EE_System__load_CPTs_and_session__start'); |
|
1058 | + $this->register_custom_taxonomies->registerCustomTaxonomies(); |
|
1059 | + $this->register_custom_post_types->registerCustomPostTypes(); |
|
1060 | + $this->register_custom_taxonomy_terms->registerCustomTaxonomyTerms(); |
|
1061 | + do_action('AHEE__EE_System__load_CPTs_and_session__complete'); |
|
1062 | + } |
|
1063 | + |
|
1064 | + |
|
1065 | + /** |
|
1066 | + * this is the best place to load any additional controllers that needs access to EE core. |
|
1067 | + * it is expected that all basic core EE systems, that are not dependent on the current request are loaded at this |
|
1068 | + * time |
|
1069 | + * |
|
1070 | + * @return void |
|
1071 | + * @throws Exception |
|
1072 | + * @throws Throwable |
|
1073 | + */ |
|
1074 | + public function load_controllers() |
|
1075 | + { |
|
1076 | + do_action('AHEE__EE_System__load_controllers__start'); |
|
1077 | + $this->router->loadControllers(); |
|
1078 | + do_action('AHEE__EE_System__load_controllers__complete'); |
|
1079 | + } |
|
1080 | + |
|
1081 | + |
|
1082 | + /** |
|
1083 | + * all the basic EE core should be loaded at this point and available regardless of M-Mode |
|
1084 | + * |
|
1085 | + * @return void |
|
1086 | + * @throws Exception |
|
1087 | + * @throws Throwable |
|
1088 | + */ |
|
1089 | + public function core_loaded_and_ready() |
|
1090 | + { |
|
1091 | + $this->router->coreLoadedAndReady(); |
|
1092 | + do_action('AHEE__EE_System__core_loaded_and_ready'); |
|
1093 | + // always load template tags, because it's faster than checking if it's a front-end request, and many page |
|
1094 | + // builders require these even on the front-end |
|
1095 | + require_once EE_PUBLIC . 'template_tags.php'; |
|
1096 | + do_action('AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons'); |
|
1097 | + } |
|
1098 | + |
|
1099 | + |
|
1100 | + /** |
|
1101 | + * this is the best place to begin initializing client code |
|
1102 | + * |
|
1103 | + * @return void |
|
1104 | + */ |
|
1105 | + public function initialize() |
|
1106 | + { |
|
1107 | + do_action('AHEE__EE_System__initialize'); |
|
1108 | + add_filter( |
|
1109 | + 'safe_style_css', |
|
1110 | + function ($styles) { |
|
1111 | + $styles[] = 'display'; |
|
1112 | + $styles[] = 'visibility'; |
|
1113 | + $styles[] = 'position'; |
|
1114 | + $styles[] = 'top'; |
|
1115 | + $styles[] = 'right'; |
|
1116 | + $styles[] = 'bottom'; |
|
1117 | + $styles[] = 'left'; |
|
1118 | + $styles[] = 'resize'; |
|
1119 | + $styles[] = 'max-width'; |
|
1120 | + $styles[] = 'max-height'; |
|
1121 | + return $styles; |
|
1122 | + } |
|
1123 | + ); |
|
1124 | + } |
|
1125 | + |
|
1126 | + |
|
1127 | + /** |
|
1128 | + * this is run really late during the WP init hook point, and ensures that mostly everything else that needs to |
|
1129 | + * initialize has done so |
|
1130 | + * |
|
1131 | + * @return void |
|
1132 | + * @throws Exception |
|
1133 | + * @throws Throwable |
|
1134 | + */ |
|
1135 | + public function initialize_last() |
|
1136 | + { |
|
1137 | + $this->router->initializeLast(); |
|
1138 | + do_action('AHEE__EE_System__initialize_last'); |
|
1139 | + /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
1140 | + $rewrite_rules = $this->loader->getShared( |
|
1141 | + 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
1142 | + ); |
|
1143 | + $rewrite_rules->flushRewriteRules(); |
|
1144 | + add_action('admin_bar_init', [$this, 'addEspressoToolbar']); |
|
1145 | + } |
|
1146 | + |
|
1147 | + |
|
1148 | + /** |
|
1149 | + * @return void |
|
1150 | + */ |
|
1151 | + public function addEspressoToolbar() |
|
1152 | + { |
|
1153 | + $this->loader->getShared( |
|
1154 | + 'EventEspresso\core\domain\services\admin\AdminToolBar', |
|
1155 | + [$this->capabilities] |
|
1156 | + ); |
|
1157 | + } |
|
1158 | + |
|
1159 | + |
|
1160 | + /** |
|
1161 | + * sets no cache headers and defines no cache constants for WP plugins |
|
1162 | + * |
|
1163 | + * @return void |
|
1164 | + */ |
|
1165 | + public static function do_not_cache() |
|
1166 | + { |
|
1167 | + // set no cache constants |
|
1168 | + if (! defined('DONOTCACHEPAGE')) { |
|
1169 | + define('DONOTCACHEPAGE', true); |
|
1170 | + } |
|
1171 | + if (! defined('DONOTCACHCEOBJECT')) { |
|
1172 | + define('DONOTCACHCEOBJECT', true); |
|
1173 | + } |
|
1174 | + if (! defined('DONOTCACHEDB')) { |
|
1175 | + define('DONOTCACHEDB', true); |
|
1176 | + } |
|
1177 | + // add no cache headers |
|
1178 | + add_action('send_headers', ['EE_System', 'nocache_headers']); |
|
1179 | + // plus a little extra for nginx and Google Chrome |
|
1180 | + add_filter('nocache_headers', ['EE_System', 'extra_nocache_headers']); |
|
1181 | + // prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes with the registration process |
|
1182 | + remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); |
|
1183 | + } |
|
1184 | + |
|
1185 | + |
|
1186 | + /** |
|
1187 | + * @param $headers |
|
1188 | + * @return array |
|
1189 | + */ |
|
1190 | + public static function extra_nocache_headers($headers): array |
|
1191 | + { |
|
1192 | + // for NGINX |
|
1193 | + $headers['X-Accel-Expires'] = 0; |
|
1194 | + // plus extra for Google Chrome since it doesn't seem to respect "no-cache", but WILL respect "no-store" |
|
1195 | + $headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'; |
|
1196 | + return $headers; |
|
1197 | + } |
|
1198 | + |
|
1199 | + |
|
1200 | + /** |
|
1201 | + * @return void |
|
1202 | + */ |
|
1203 | + public static function nocache_headers() |
|
1204 | + { |
|
1205 | + nocache_headers(); |
|
1206 | + } |
|
1207 | + |
|
1208 | + |
|
1209 | + /** |
|
1210 | + * simply hooks into "wp_list_pages_exclude" filter (for wp_list_pages method) and makes sure EE critical pages are |
|
1211 | + * never returned with the function. |
|
1212 | + * |
|
1213 | + * @param array $exclude_array any existing pages being excluded are in this array. |
|
1214 | + * @return array |
|
1215 | + */ |
|
1216 | + public function remove_pages_from_wp_list_pages(array $exclude_array): array |
|
1217 | + { |
|
1218 | + return array_merge($exclude_array, $this->registry->CFG->core->get_critical_pages_array()); |
|
1219 | + } |
|
1220 | 1220 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | ?FeatureFlags $feature = null |
134 | 134 | ): EE_System { |
135 | 135 | // check if class object is instantiated |
136 | - if (! self::$_instance instanceof EE_System) { |
|
136 | + if ( ! self::$_instance instanceof EE_System) { |
|
137 | 137 | self::$_instance = new self($loader, $maintenance_mode, $registry, $request, $router, $feature); |
138 | 138 | } |
139 | 139 | return self::$_instance; |
@@ -330,10 +330,10 @@ discard block |
||
330 | 330 | /** @var Domain $domain */ |
331 | 331 | $domain = DomainFactory::getEventEspressoCoreDomain(); |
332 | 332 | if ($domain->isCaffeinated() && ! $brew instanceof EE_Brewing_Regular) { |
333 | - require_once EE_CAFF_PATH . 'brewing_regular.php'; |
|
333 | + require_once EE_CAFF_PATH.'brewing_regular.php'; |
|
334 | 334 | /** @var EE_Brewing_Regular $brew */ |
335 | 335 | $brew = $this->loader->getShared(EE_Brewing_Regular::class); |
336 | - if (! $this->feature->allowed(FeatureFlag::USE_EDD_PLUGIN_LICENSING)) { |
|
336 | + if ( ! $this->feature->allowed(FeatureFlag::USE_EDD_PLUGIN_LICENSING)) { |
|
337 | 337 | $brew->initializePUE(); |
338 | 338 | } |
339 | 339 | add_action( |
@@ -469,11 +469,11 @@ discard block |
||
469 | 469 | private function fix_espresso_db_upgrade_option($espresso_db_update = null): array |
470 | 470 | { |
471 | 471 | do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__begin', $espresso_db_update); |
472 | - if (! $espresso_db_update) { |
|
472 | + if ( ! $espresso_db_update) { |
|
473 | 473 | $espresso_db_update = get_option('espresso_db_update'); |
474 | 474 | } |
475 | 475 | // check that option is an array |
476 | - if (! is_array($espresso_db_update)) { |
|
476 | + if ( ! is_array($espresso_db_update)) { |
|
477 | 477 | // if option is FALSE, then it never existed |
478 | 478 | if ($espresso_db_update === false) { |
479 | 479 | // make $espresso_db_update an array and save option with autoload OFF |
@@ -493,10 +493,10 @@ discard block |
||
493 | 493 | // so it must be numerically-indexed, where values are versions installed... |
494 | 494 | // fix it! |
495 | 495 | $version_string = $should_be_array; |
496 | - $corrected_db_update[ $version_string ] = ['unknown-date']; |
|
496 | + $corrected_db_update[$version_string] = ['unknown-date']; |
|
497 | 497 | } else { |
498 | 498 | // ok it checks out |
499 | - $corrected_db_update[ $should_be_version_string ] = $should_be_array; |
|
499 | + $corrected_db_update[$should_be_version_string] = $should_be_array; |
|
500 | 500 | } |
501 | 501 | } |
502 | 502 | $espresso_db_update = $corrected_db_update; |
@@ -530,7 +530,7 @@ discard block |
||
530 | 530 | ) { |
531 | 531 | $request_type = $this->detect_req_type(); |
532 | 532 | // only initialize system if we're not in maintenance mode. |
533 | - if (! MaintenanceStatus::isFullSite()) { |
|
533 | + if ( ! MaintenanceStatus::isFullSite()) { |
|
534 | 534 | /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
535 | 535 | $rewrite_rules = $this->loader->getShared( |
536 | 536 | 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
@@ -588,13 +588,13 @@ discard block |
||
588 | 588 | ?array $version_history = null, |
589 | 589 | ?string $current_version_to_add = null |
590 | 590 | ): bool { |
591 | - if (! $version_history) { |
|
591 | + if ( ! $version_history) { |
|
592 | 592 | $version_history = $this->fix_espresso_db_upgrade_option($version_history); |
593 | 593 | } |
594 | 594 | if ($current_version_to_add === null) { |
595 | 595 | $current_version_to_add = espresso_version(); |
596 | 596 | } |
597 | - $version_history[ $current_version_to_add ][] = date('Y-m-d H:i:s', time()); |
|
597 | + $version_history[$current_version_to_add][] = date('Y-m-d H:i:s', time()); |
|
598 | 598 | // re-save |
599 | 599 | return update_option('espresso_db_update', $version_history); |
600 | 600 | } |
@@ -762,7 +762,7 @@ discard block |
||
762 | 762 | foreach ($activation_history as $version => $activation_dates) { |
763 | 763 | // check there is a record of when this version was activated. |
764 | 764 | // Otherwise, mark it as unknown |
765 | - if (! $activation_dates) { |
|
765 | + if ( ! $activation_dates) { |
|
766 | 766 | $activation_dates = ['unknown-date']; |
767 | 767 | } |
768 | 768 | $activation_dates = is_string($activation_dates) |
@@ -858,7 +858,7 @@ discard block |
||
858 | 858 | private function _parse_model_names() |
859 | 859 | { |
860 | 860 | // get all the files in the EE_MODELS folder that end in .model.php |
861 | - $models = glob(EE_MODELS . '*.model.php'); |
|
861 | + $models = glob(EE_MODELS.'*.model.php'); |
|
862 | 862 | $model_names = []; |
863 | 863 | $non_abstract_db_models = []; |
864 | 864 | foreach ($models as $model) { |
@@ -867,9 +867,9 @@ discard block |
||
867 | 867 | $short_name = str_replace('EEM_', '', $classname); |
868 | 868 | $reflectionClass = new ReflectionClass($classname); |
869 | 869 | if ($reflectionClass->isSubclassOf('EEM_Base') && ! $reflectionClass->isAbstract()) { |
870 | - $non_abstract_db_models[ $short_name ] = $classname; |
|
870 | + $non_abstract_db_models[$short_name] = $classname; |
|
871 | 871 | } |
872 | - $model_names[ $short_name ] = $classname; |
|
872 | + $model_names[$short_name] = $classname; |
|
873 | 873 | } |
874 | 874 | $this->registry->models = apply_filters('FHEE__EE_System__parse_model_names', $model_names); |
875 | 875 | $this->registry->non_abstract_db_models = apply_filters( |
@@ -950,7 +950,7 @@ discard block |
||
950 | 950 | $class_names = WordPressHooks::getClassNamesForAllCallbacksOnHook( |
951 | 951 | 'AHEE__EE_System__register_shortcodes_modules_and_addons' |
952 | 952 | ); |
953 | - if (! empty($class_names)) { |
|
953 | + if ( ! empty($class_names)) { |
|
954 | 954 | $msg = esc_html__( |
955 | 955 | 'The following plugins, addons, or modules appear to be incompatible with this version of Event Espresso and were automatically deactivated to avoid fatal errors:', |
956 | 956 | 'event_espresso' |
@@ -962,7 +962,7 @@ discard block |
||
962 | 962 | ['EE_', 'EEM_', 'EED_', 'EES_', 'EEW_'], |
963 | 963 | '', |
964 | 964 | $class_name |
965 | - ) . '</b></li>'; |
|
965 | + ).'</b></li>'; |
|
966 | 966 | } |
967 | 967 | $msg .= '</ul>'; |
968 | 968 | $msg .= esc_html__( |
@@ -1025,7 +1025,7 @@ discard block |
||
1025 | 1025 | private function _deactivate_incompatible_addons() |
1026 | 1026 | { |
1027 | 1027 | $incompatible_addons = get_option('ee_incompatible_addons', []); |
1028 | - if (! empty($incompatible_addons)) { |
|
1028 | + if ( ! empty($incompatible_addons)) { |
|
1029 | 1029 | $active_plugins = get_option('active_plugins', []); |
1030 | 1030 | foreach ($active_plugins as $active_plugin) { |
1031 | 1031 | foreach ($incompatible_addons as $incompatible_addon) { |
@@ -1092,7 +1092,7 @@ discard block |
||
1092 | 1092 | do_action('AHEE__EE_System__core_loaded_and_ready'); |
1093 | 1093 | // always load template tags, because it's faster than checking if it's a front-end request, and many page |
1094 | 1094 | // builders require these even on the front-end |
1095 | - require_once EE_PUBLIC . 'template_tags.php'; |
|
1095 | + require_once EE_PUBLIC.'template_tags.php'; |
|
1096 | 1096 | do_action('AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons'); |
1097 | 1097 | } |
1098 | 1098 | |
@@ -1107,7 +1107,7 @@ discard block |
||
1107 | 1107 | do_action('AHEE__EE_System__initialize'); |
1108 | 1108 | add_filter( |
1109 | 1109 | 'safe_style_css', |
1110 | - function ($styles) { |
|
1110 | + function($styles) { |
|
1111 | 1111 | $styles[] = 'display'; |
1112 | 1112 | $styles[] = 'visibility'; |
1113 | 1113 | $styles[] = 'position'; |
@@ -1165,13 +1165,13 @@ discard block |
||
1165 | 1165 | public static function do_not_cache() |
1166 | 1166 | { |
1167 | 1167 | // set no cache constants |
1168 | - if (! defined('DONOTCACHEPAGE')) { |
|
1168 | + if ( ! defined('DONOTCACHEPAGE')) { |
|
1169 | 1169 | define('DONOTCACHEPAGE', true); |
1170 | 1170 | } |
1171 | - if (! defined('DONOTCACHCEOBJECT')) { |
|
1171 | + if ( ! defined('DONOTCACHCEOBJECT')) { |
|
1172 | 1172 | define('DONOTCACHCEOBJECT', true); |
1173 | 1173 | } |
1174 | - if (! defined('DONOTCACHEDB')) { |
|
1174 | + if ( ! defined('DONOTCACHEDB')) { |
|
1175 | 1175 | define('DONOTCACHEDB', true); |
1176 | 1176 | } |
1177 | 1177 | // add no cache headers |
@@ -5,63 +5,63 @@ |
||
5 | 5 | |
6 | 6 | class EE_Boolean_Field extends EE_Model_Field_Base |
7 | 7 | { |
8 | - /** |
|
9 | - * @param string $table_column |
|
10 | - * @param string $nicename |
|
11 | - * @param bool $nullable |
|
12 | - * @param null $default_value |
|
13 | - */ |
|
14 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
15 | - { |
|
16 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
17 | - $this->setSchemaType(SchemaType::BOOLEAN); |
|
18 | - $this->setDataType(DataType::BOOLEAN); |
|
19 | - } |
|
8 | + /** |
|
9 | + * @param string $table_column |
|
10 | + * @param string $nicename |
|
11 | + * @param bool $nullable |
|
12 | + * @param null $default_value |
|
13 | + */ |
|
14 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
15 | + { |
|
16 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
17 | + $this->setSchemaType(SchemaType::BOOLEAN); |
|
18 | + $this->setDataType(DataType::BOOLEAN); |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * Double-checks the value being returned is an boolean. |
|
23 | - * @since 4.9.74.p |
|
24 | - * @param mixed $value_of_field_on_model_object |
|
25 | - * @return boolean |
|
26 | - */ |
|
27 | - public function prepare_for_get($value_of_field_on_model_object) |
|
28 | - { |
|
29 | - return (bool) parent::prepare_for_get($value_of_field_on_model_object); |
|
30 | - } |
|
21 | + /** |
|
22 | + * Double-checks the value being returned is an boolean. |
|
23 | + * @since 4.9.74.p |
|
24 | + * @param mixed $value_of_field_on_model_object |
|
25 | + * @return boolean |
|
26 | + */ |
|
27 | + public function prepare_for_get($value_of_field_on_model_object) |
|
28 | + { |
|
29 | + return (bool) parent::prepare_for_get($value_of_field_on_model_object); |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * @since 4.9.74.p |
|
34 | - * @param $value_inputted_for_field_on_model_object |
|
35 | - * @return boolean |
|
36 | - */ |
|
37 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
38 | - { |
|
39 | - return filter_var($value_inputted_for_field_on_model_object, FILTER_VALIDATE_BOOLEAN); |
|
40 | - } |
|
32 | + /** |
|
33 | + * @since 4.9.74.p |
|
34 | + * @param $value_inputted_for_field_on_model_object |
|
35 | + * @return boolean |
|
36 | + */ |
|
37 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
38 | + { |
|
39 | + return filter_var($value_inputted_for_field_on_model_object, FILTER_VALIDATE_BOOLEAN); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Make sure we're returning booleans |
|
44 | - * |
|
45 | - * @param string $value_inputted_for_field_on_model_object |
|
46 | - * @return boolean |
|
47 | - */ |
|
48 | - public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
49 | - { |
|
50 | - return filter_var($value_inputted_for_field_on_model_object, FILTER_VALIDATE_BOOLEAN); |
|
51 | - } |
|
42 | + /** |
|
43 | + * Make sure we're returning booleans |
|
44 | + * |
|
45 | + * @param string $value_inputted_for_field_on_model_object |
|
46 | + * @return boolean |
|
47 | + */ |
|
48 | + public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
49 | + { |
|
50 | + return filter_var($value_inputted_for_field_on_model_object, FILTER_VALIDATE_BOOLEAN); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Gets a nice Yes/No value for this field |
|
55 | - * |
|
56 | - * @param boolean $value_on_field_to_be_outputted |
|
57 | - * @return string Yes or No |
|
58 | - */ |
|
59 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
60 | - { |
|
61 | - return apply_filters( |
|
62 | - 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
63 | - $value_on_field_to_be_outputted ? esc_html__('Yes', 'event_espresso') : esc_html__('No', 'event_espresso'), |
|
64 | - $value_on_field_to_be_outputted |
|
65 | - ); |
|
66 | - } |
|
53 | + /** |
|
54 | + * Gets a nice Yes/No value for this field |
|
55 | + * |
|
56 | + * @param boolean $value_on_field_to_be_outputted |
|
57 | + * @return string Yes or No |
|
58 | + */ |
|
59 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
60 | + { |
|
61 | + return apply_filters( |
|
62 | + 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
63 | + $value_on_field_to_be_outputted ? esc_html__('Yes', 'event_espresso') : esc_html__('No', 'event_espresso'), |
|
64 | + $value_on_field_to_be_outputted |
|
65 | + ); |
|
66 | + } |
|
67 | 67 | } |
@@ -5,16 +5,16 @@ |
||
5 | 5 | |
6 | 6 | class EE_DB_Only_Int_Field extends EE_DB_Only_Field_Base |
7 | 7 | { |
8 | - /** |
|
9 | - * @param string $table_column |
|
10 | - * @param string $nicename |
|
11 | - * @param bool $nullable |
|
12 | - * @param null $default_value |
|
13 | - */ |
|
14 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
15 | - { |
|
16 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
17 | - $this->setSchemaType(SchemaType::INTEGER); |
|
18 | - $this->setDataType(DataType::INTEGER); |
|
19 | - } |
|
8 | + /** |
|
9 | + * @param string $table_column |
|
10 | + * @param string $nicename |
|
11 | + * @param bool $nullable |
|
12 | + * @param null $default_value |
|
13 | + */ |
|
14 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
15 | + { |
|
16 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
17 | + $this->setSchemaType(SchemaType::INTEGER); |
|
18 | + $this->setDataType(DataType::INTEGER); |
|
19 | + } |
|
20 | 20 | } |
@@ -12,109 +12,109 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_Enum_Integer_Field extends EE_Integer_Field |
14 | 14 | { |
15 | - public array $_allowed_enum_values; |
|
15 | + public array $_allowed_enum_values; |
|
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * @param string $table_column |
|
20 | - * @param string $nicename |
|
21 | - * @param boolean $nullable |
|
22 | - * @param int $default_value |
|
23 | - * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
24 | - */ |
|
25 | - public function __construct($table_column, $nicename, $nullable, $default_value, array $allowed_enum_values) |
|
26 | - { |
|
27 | - $this->_allowed_enum_values = $allowed_enum_values; |
|
28 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
29 | - $this->setSchemaType(SchemaType::OBJECT); |
|
30 | - $this->setDataType(DataType::INTEGER); |
|
31 | - } |
|
18 | + /** |
|
19 | + * @param string $table_column |
|
20 | + * @param string $nicename |
|
21 | + * @param boolean $nullable |
|
22 | + * @param int $default_value |
|
23 | + * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
24 | + */ |
|
25 | + public function __construct($table_column, $nicename, $nullable, $default_value, array $allowed_enum_values) |
|
26 | + { |
|
27 | + $this->_allowed_enum_values = $allowed_enum_values; |
|
28 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
29 | + $this->setSchemaType(SchemaType::OBJECT); |
|
30 | + $this->setDataType(DataType::INTEGER); |
|
31 | + } |
|
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * Returns the list of allowed enum options, but filterable. |
|
36 | - * This is used internally |
|
37 | - * |
|
38 | - * @return array |
|
39 | - */ |
|
40 | - protected function _allowed_enum_values(): array |
|
41 | - { |
|
42 | - return (array) apply_filters( |
|
43 | - 'FHEE__EE_Enum_Integer_Field___allowed_enum_options', |
|
44 | - $this->_allowed_enum_values, |
|
45 | - $this |
|
46 | - ); |
|
47 | - } |
|
34 | + /** |
|
35 | + * Returns the list of allowed enum options, but filterable. |
|
36 | + * This is used internally |
|
37 | + * |
|
38 | + * @return array |
|
39 | + */ |
|
40 | + protected function _allowed_enum_values(): array |
|
41 | + { |
|
42 | + return (array) apply_filters( |
|
43 | + 'FHEE__EE_Enum_Integer_Field___allowed_enum_options', |
|
44 | + $this->_allowed_enum_values, |
|
45 | + $this |
|
46 | + ); |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | - /** |
|
51 | - * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
52 | - * If not, throw an error (but if WP_DEBUG is false, just set the value to default) |
|
53 | - * |
|
54 | - * @param int $value_inputted_for_field_on_model_object |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
58 | - { |
|
59 | - $allowed_enum_values = $this->_allowed_enum_values(); |
|
60 | - if ( |
|
61 | - $value_inputted_for_field_on_model_object !== null |
|
62 | - && ! array_key_exists($value_inputted_for_field_on_model_object, $allowed_enum_values) |
|
63 | - ) { |
|
64 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
65 | - $msg = sprintf( |
|
66 | - esc_html__('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
67 | - $value_inputted_for_field_on_model_object, |
|
68 | - $this->_name |
|
69 | - ); |
|
70 | - $msg2 = sprintf( |
|
71 | - esc_html__('Allowed values for "%1$s" are "%2$s". You provided "%3$s"', 'event_espresso'), |
|
72 | - $this->_name, |
|
73 | - implode(', ', array_keys($allowed_enum_values)), |
|
74 | - $value_inputted_for_field_on_model_object |
|
75 | - ); |
|
76 | - EE_Error::add_error("$msg||$msg2", __FILE__, __FUNCTION__, __LINE__); |
|
77 | - } |
|
78 | - return $this->get_default_value(); |
|
79 | - } |
|
80 | - return (int) $value_inputted_for_field_on_model_object; |
|
81 | - } |
|
50 | + /** |
|
51 | + * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
52 | + * If not, throw an error (but if WP_DEBUG is false, just set the value to default) |
|
53 | + * |
|
54 | + * @param int $value_inputted_for_field_on_model_object |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
58 | + { |
|
59 | + $allowed_enum_values = $this->_allowed_enum_values(); |
|
60 | + if ( |
|
61 | + $value_inputted_for_field_on_model_object !== null |
|
62 | + && ! array_key_exists($value_inputted_for_field_on_model_object, $allowed_enum_values) |
|
63 | + ) { |
|
64 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
65 | + $msg = sprintf( |
|
66 | + esc_html__('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
67 | + $value_inputted_for_field_on_model_object, |
|
68 | + $this->_name |
|
69 | + ); |
|
70 | + $msg2 = sprintf( |
|
71 | + esc_html__('Allowed values for "%1$s" are "%2$s". You provided "%3$s"', 'event_espresso'), |
|
72 | + $this->_name, |
|
73 | + implode(', ', array_keys($allowed_enum_values)), |
|
74 | + $value_inputted_for_field_on_model_object |
|
75 | + ); |
|
76 | + EE_Error::add_error("$msg||$msg2", __FILE__, __FUNCTION__, __LINE__); |
|
77 | + } |
|
78 | + return $this->get_default_value(); |
|
79 | + } |
|
80 | + return (int) $value_inputted_for_field_on_model_object; |
|
81 | + } |
|
82 | 82 | |
83 | 83 | |
84 | - /** |
|
85 | - * Gets the pretty version of the enum's value. |
|
86 | - * |
|
87 | - * @param int | string $value_on_field_to_be_outputted |
|
88 | - * @param string|null $schema |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
92 | - { |
|
93 | - $options = $this->_allowed_enum_values(); |
|
94 | - return $options[ $value_on_field_to_be_outputted ] ?? $value_on_field_to_be_outputted; |
|
95 | - } |
|
84 | + /** |
|
85 | + * Gets the pretty version of the enum's value. |
|
86 | + * |
|
87 | + * @param int | string $value_on_field_to_be_outputted |
|
88 | + * @param string|null $schema |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
92 | + { |
|
93 | + $options = $this->_allowed_enum_values(); |
|
94 | + return $options[ $value_on_field_to_be_outputted ] ?? $value_on_field_to_be_outputted; |
|
95 | + } |
|
96 | 96 | |
97 | 97 | |
98 | - public function getSchemaProperties(): array |
|
99 | - { |
|
100 | - return [ |
|
101 | - 'raw' => [ |
|
102 | - 'description' => sprintf( |
|
103 | - esc_html__('%s - the value in the database.', 'event_espresso'), |
|
104 | - $this->get_nicename() |
|
105 | - ), |
|
106 | - 'enum' => array_keys($this->_allowed_enum_values()), |
|
107 | - 'type' => SchemaType::INTEGER, |
|
108 | - ], |
|
109 | - 'pretty' => [ |
|
110 | - 'description' => sprintf( |
|
111 | - esc_html__('%s - the value for display.', 'event_espresso'), |
|
112 | - $this->get_nicename() |
|
113 | - ), |
|
114 | - 'enum' => array_values($this->_allowed_enum_values()), |
|
115 | - 'type' => SchemaType::STRING, |
|
116 | - 'read_only' => true, |
|
117 | - ], |
|
118 | - ]; |
|
119 | - } |
|
98 | + public function getSchemaProperties(): array |
|
99 | + { |
|
100 | + return [ |
|
101 | + 'raw' => [ |
|
102 | + 'description' => sprintf( |
|
103 | + esc_html__('%s - the value in the database.', 'event_espresso'), |
|
104 | + $this->get_nicename() |
|
105 | + ), |
|
106 | + 'enum' => array_keys($this->_allowed_enum_values()), |
|
107 | + 'type' => SchemaType::INTEGER, |
|
108 | + ], |
|
109 | + 'pretty' => [ |
|
110 | + 'description' => sprintf( |
|
111 | + esc_html__('%s - the value for display.', 'event_espresso'), |
|
112 | + $this->get_nicename() |
|
113 | + ), |
|
114 | + 'enum' => array_values($this->_allowed_enum_values()), |
|
115 | + 'type' => SchemaType::STRING, |
|
116 | + 'read_only' => true, |
|
117 | + ], |
|
118 | + ]; |
|
119 | + } |
|
120 | 120 | } |
@@ -20,764 +20,764 @@ |
||
20 | 20 | */ |
21 | 21 | class EE_Datetime_Field extends EE_Model_Field_Base |
22 | 22 | { |
23 | - /** |
|
24 | - * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
25 | - * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
26 | - * |
|
27 | - * @type string unix_timestamp_regex |
|
28 | - */ |
|
29 | - const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
30 | - |
|
31 | - /** |
|
32 | - * @type string mysql_timestamp_format |
|
33 | - */ |
|
34 | - const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
35 | - |
|
36 | - /** |
|
37 | - * @type string mysql_date_format |
|
38 | - */ |
|
39 | - const mysql_date_format = 'Y-m-d'; |
|
40 | - |
|
41 | - /** |
|
42 | - * @type string mysql_time_format |
|
43 | - */ |
|
44 | - const mysql_time_format = 'H:i:s'; |
|
45 | - |
|
46 | - /** |
|
47 | - * Const for using in the default value. If the field's default is set to this, |
|
48 | - * then we will return the time of calling `get_default_value()`, not |
|
49 | - * just the current time at construction |
|
50 | - */ |
|
51 | - const now = 'now'; |
|
52 | - |
|
53 | - /** |
|
54 | - * The following properties hold the default formats for date and time. |
|
55 | - * Defaults are set via the constructor and can be overridden on class instantiation. |
|
56 | - * However they can also be overridden later by the set_format() method |
|
57 | - * (and corresponding set_date_format, set_time_format methods); |
|
58 | - */ |
|
59 | - |
|
60 | - protected string $_date_format = ''; |
|
61 | - |
|
62 | - protected string $_time_format = ''; |
|
63 | - |
|
64 | - protected string $_pretty_date_format = ''; |
|
65 | - |
|
66 | - protected string $_pretty_time_format = ''; |
|
67 | - |
|
68 | - protected ?DateTimeZone $_DateTimeZone = null; |
|
69 | - |
|
70 | - protected ?DateTimeZone $_UTC_DateTimeZone = null; |
|
71 | - |
|
72 | - protected ?DateTimeZone $_blog_DateTimeZone = null; |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
77 | - * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
78 | - * and time returned via getters. |
|
79 | - * |
|
80 | - * @var mixed (null|string) |
|
81 | - */ |
|
82 | - protected $_date_time_output; |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * timezone string |
|
87 | - * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
88 | - * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
89 | - * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
90 | - */ |
|
91 | - protected ?string $_timezone_string = null; |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
96 | - * offsets for comparison purposes). |
|
97 | - * |
|
98 | - * @var int |
|
99 | - */ |
|
100 | - protected int $_blog_offset = 0; |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * @param string $table_column |
|
105 | - * @param string $nice_name |
|
106 | - * @param bool $nullable |
|
107 | - * @param string|null $default_value |
|
108 | - * @param string|null $timezone_string |
|
109 | - * @param string|null $date_format |
|
110 | - * @param string|null $time_format |
|
111 | - * @param string|null $pretty_date_format |
|
112 | - * @param string|null $pretty_time_format |
|
113 | - * @throws InvalidArgumentException |
|
114 | - * @throws Exception |
|
115 | - */ |
|
116 | - public function __construct( |
|
117 | - string $table_column, |
|
118 | - string $nice_name, |
|
119 | - bool $nullable, |
|
120 | - ?string $default_value, |
|
121 | - ?string $timezone_string = '', |
|
122 | - ?string $date_format = '', |
|
123 | - ?string $time_format = '', |
|
124 | - ?string $pretty_date_format = '', |
|
125 | - ?string $pretty_time_format = '' |
|
126 | - ) { |
|
127 | - $this->set_date_format($date_format); |
|
128 | - $this->set_time_format($time_format); |
|
129 | - $this->set_date_format($pretty_date_format, true); |
|
130 | - $this->set_time_format($pretty_time_format, true); |
|
131 | - |
|
132 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
133 | - $this->set_timezone($timezone_string); |
|
134 | - $this->setSchemaType(SchemaType::STRING); |
|
135 | - $this->setSchemaFormat(SchemaFormat::DATETIME); |
|
136 | - $this->setDataType(DataType::STRING); // best to url encode the date using DateTime::ATOM |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * @return DateTimeZone |
|
142 | - * @throws Exception |
|
143 | - */ |
|
144 | - public function get_UTC_DateTimeZone(): DateTimeZone |
|
145 | - { |
|
146 | - return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
147 | - ? $this->_UTC_DateTimeZone |
|
148 | - : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * @return DateTimeZone |
|
154 | - * @throws Exception |
|
155 | - */ |
|
156 | - public function get_blog_DateTimeZone(): DateTimeZone |
|
157 | - { |
|
158 | - return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
159 | - ? $this->_blog_DateTimeZone |
|
160 | - : $this->_create_timezone_object_from_timezone_string(); |
|
161 | - } |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * this prepares any incoming date data and make sure it's converted to a UTC unix timestamp |
|
166 | - * |
|
167 | - * @param string|int|null $value value inputted for field on model object |
|
168 | - * could be a string formatted date time or int unix timestamp |
|
169 | - * @return DateTime |
|
170 | - * @throws Exception |
|
171 | - */ |
|
172 | - public function prepare_for_set($value) |
|
173 | - { |
|
174 | - return $this->_get_date_object($value); |
|
175 | - } |
|
176 | - |
|
177 | - |
|
178 | - /** |
|
179 | - * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
180 | - * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
181 | - * |
|
182 | - * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
183 | - * @return string The final assembled format string. |
|
184 | - */ |
|
185 | - protected function _get_date_time_output(bool $pretty = false): string |
|
186 | - { |
|
187 | - switch ($this->_date_time_output) { |
|
188 | - case 'time': |
|
189 | - return $pretty |
|
190 | - ? $this->_pretty_time_format |
|
191 | - : $this->_time_format; |
|
192 | - |
|
193 | - case 'date': |
|
194 | - return $pretty |
|
195 | - ? $this->_pretty_date_format |
|
196 | - : $this->_date_format; |
|
197 | - |
|
198 | - default: |
|
199 | - return $pretty |
|
200 | - ? trim($this->_pretty_date_format . ' ' . $this->_pretty_time_format) |
|
201 | - : trim($this->_date_format . ' ' . $this->_time_format); |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
208 | - * returned (using the format properties) |
|
209 | - * |
|
210 | - * @param string|null $what acceptable values are 'time' or 'date'. |
|
211 | - * Any other value will be set but will always result |
|
212 | - * in both 'date' and 'time' being returned. |
|
213 | - * @return void |
|
214 | - */ |
|
215 | - public function set_date_time_output(?string $what = null) |
|
216 | - { |
|
217 | - $this->_date_time_output = $what; |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
223 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
224 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
225 | - * We also set some other properties in this method. |
|
226 | - * |
|
227 | - * @param string|null $timezone_string A valid timezone string as described by @link |
|
228 | - * http://www.php.net/manual/en/timezones.php |
|
229 | - * @return void |
|
230 | - * @throws InvalidArgumentException |
|
231 | - * @throws InvalidDataTypeException |
|
232 | - * @throws InvalidInterfaceException |
|
233 | - * @throws Exception |
|
234 | - */ |
|
235 | - public function set_timezone(?string $timezone_string = '') |
|
236 | - { |
|
237 | - if (empty($timezone_string) && $this->_timezone_string) { |
|
238 | - // leave the timezone AS-IS if we already have one and |
|
239 | - // the function arg didn't provide one |
|
240 | - return; |
|
241 | - } |
|
242 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
243 | - $this->_timezone_string = ! empty($timezone_string) |
|
244 | - ? $timezone_string |
|
245 | - : 'UTC'; |
|
246 | - $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * _create_timezone_object_from_timezone_name |
|
252 | - * |
|
253 | - * @access protected |
|
254 | - * @param string|null $timezone_string |
|
255 | - * @return DateTimeZone |
|
256 | - * @throws InvalidArgumentException |
|
257 | - * @throws InvalidDataTypeException |
|
258 | - * @throws InvalidInterfaceException |
|
259 | - * @throws Exception |
|
260 | - */ |
|
261 | - protected function _create_timezone_object_from_timezone_string(?string $timezone_string = ''): DateTimeZone |
|
262 | - { |
|
263 | - return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * This just returns whatever is set for the current timezone. |
|
269 | - * |
|
270 | - * @access public |
|
271 | - * @return string timezone string |
|
272 | - */ |
|
273 | - public function get_timezone(): string |
|
274 | - { |
|
275 | - return (string) $this->_timezone_string; |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * set the $_date_format property |
|
281 | - * |
|
282 | - * @access public |
|
283 | - * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
284 | - * @param bool $pretty Whether to set pretty format or not. |
|
285 | - * @return void |
|
286 | - */ |
|
287 | - public function set_date_format(string $format, bool $pretty = false) |
|
288 | - { |
|
289 | - if ($pretty) { |
|
290 | - $this->_pretty_date_format = new DateFormat($format); |
|
291 | - } else { |
|
292 | - $this->_date_format = new DateFormat($format); |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - |
|
297 | - /** |
|
298 | - * return the $_date_format property value. |
|
299 | - * |
|
300 | - * @param bool $pretty Whether to get pretty format or not. |
|
301 | - * @return string |
|
302 | - */ |
|
303 | - public function get_date_format(bool $pretty = false): string |
|
304 | - { |
|
305 | - return $pretty |
|
306 | - ? $this->_pretty_date_format |
|
307 | - : $this->_date_format; |
|
308 | - } |
|
309 | - |
|
310 | - |
|
311 | - /** |
|
312 | - * set the $_time_format property |
|
313 | - * |
|
314 | - * @access public |
|
315 | - * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
316 | - * @param bool $pretty Whether to set pretty format or not. |
|
317 | - * @return void |
|
318 | - */ |
|
319 | - public function set_time_format(string $format, bool $pretty = false) |
|
320 | - { |
|
321 | - if ($pretty) { |
|
322 | - $this->_pretty_time_format = new TimeFormat($format); |
|
323 | - } else { |
|
324 | - $this->_time_format = new TimeFormat($format); |
|
325 | - } |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * return the $_time_format property value. |
|
331 | - * |
|
332 | - * @param bool $pretty Whether to get pretty format or not. |
|
333 | - * @return string |
|
334 | - */ |
|
335 | - public function get_time_format(bool $pretty = false): string |
|
336 | - { |
|
337 | - return $pretty |
|
338 | - ? $this->_pretty_time_format |
|
339 | - : $this->_time_format; |
|
340 | - } |
|
341 | - |
|
342 | - |
|
343 | - /** |
|
344 | - * set the $_pretty_date_format property |
|
345 | - * |
|
346 | - * @access public |
|
347 | - * @param string|null $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
348 | - * @return void |
|
349 | - */ |
|
350 | - public function set_pretty_date_format(?string $format) |
|
351 | - { |
|
352 | - $this->set_date_format($format, true); |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * set the $_pretty_time_format property |
|
358 | - * |
|
359 | - * @access public |
|
360 | - * @param string|null $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
361 | - * @return void |
|
362 | - */ |
|
363 | - public function set_pretty_time_format(?string $format) |
|
364 | - { |
|
365 | - $this->set_time_format($format, true); |
|
366 | - } |
|
367 | - |
|
368 | - |
|
369 | - /** |
|
370 | - * Only sets the time portion of the datetime. |
|
371 | - * |
|
372 | - * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
373 | - * @param DateTime $current current DateTime object for the datetime field |
|
374 | - * @return DateTime |
|
375 | - */ |
|
376 | - public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current): DateTime |
|
377 | - { |
|
378 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
379 | - // Otherwise parse the string. |
|
380 | - if ($time_to_set_string instanceof DateTime) { |
|
381 | - $parsed = [ |
|
382 | - 'hour' => $time_to_set_string->format('H'), |
|
383 | - 'minute' => $time_to_set_string->format('i'), |
|
384 | - 'second' => $time_to_set_string->format('s'), |
|
385 | - ]; |
|
386 | - } else { |
|
387 | - // parse incoming string |
|
388 | - $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
389 | - } |
|
390 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
391 | - return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
392 | - } |
|
393 | - |
|
394 | - |
|
395 | - /** |
|
396 | - * Only sets the date portion of the datetime. |
|
397 | - * |
|
398 | - * @param string|DateTime $date_to_set_string like "Friday, January 8th" or a DateTime object. |
|
399 | - * @param DateTime $current current DateTime object for the datetime field |
|
400 | - * @return DateTime |
|
401 | - */ |
|
402 | - public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current): DateTime |
|
403 | - { |
|
404 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
405 | - // Otherwise parse the string. |
|
406 | - if ($date_to_set_string instanceof DateTime) { |
|
407 | - $parsed = [ |
|
408 | - 'year' => $date_to_set_string->format('Y'), |
|
409 | - 'month' => $date_to_set_string->format('m'), |
|
410 | - 'day' => $date_to_set_string->format('d'), |
|
411 | - ]; |
|
412 | - } else { |
|
413 | - // parse incoming string |
|
414 | - $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
415 | - } |
|
416 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
417 | - return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
418 | - } |
|
419 | - |
|
420 | - |
|
421 | - /** |
|
422 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). |
|
423 | - * When the datetime gets to this stage it should ALREADY be in UTC time |
|
424 | - * |
|
425 | - * @param int|string|DateTime|null $DateTime |
|
426 | - * @return string formatted date time for given timezone |
|
427 | - * @throws EE_Error |
|
428 | - */ |
|
429 | - public function prepare_for_get($DateTime): string |
|
430 | - { |
|
431 | - return $this->_prepare_for_display($DateTime); |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - /** |
|
436 | - * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
437 | - * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
438 | - * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
439 | - * abbreviation to the date_string. |
|
440 | - * |
|
441 | - * @param mixed $DateTime |
|
442 | - * @param string|null $schema |
|
443 | - * @return string |
|
444 | - * @throws EE_Error |
|
445 | - */ |
|
446 | - public function prepare_for_pretty_echoing($DateTime, ?string $schema = null): string |
|
447 | - { |
|
448 | - $schema = $schema ?: true; |
|
449 | - return $this->_prepare_for_display($DateTime, $schema); |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
455 | - * timezone). |
|
456 | - * |
|
457 | - * @param int|string|DateTime|null $DateTime |
|
458 | - * @param bool|string $schema |
|
459 | - * @return string |
|
460 | - * @throws EE_Error |
|
461 | - * @throws Exception |
|
462 | - */ |
|
463 | - protected function _prepare_for_display($DateTime, $schema = false): string |
|
464 | - { |
|
465 | - if (! $DateTime instanceof DateTime) { |
|
466 | - if ($this->_nullable) { |
|
467 | - return ''; |
|
468 | - } |
|
469 | - if (WP_DEBUG) { |
|
470 | - throw new EE_Error( |
|
471 | - sprintf( |
|
472 | - esc_html__( |
|
473 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
474 | - 'event_espresso' |
|
475 | - ), |
|
476 | - $this->_nicename |
|
477 | - ) |
|
478 | - ); |
|
479 | - } |
|
480 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now); |
|
481 | - EE_Error::add_error( |
|
482 | - sprintf( |
|
483 | - esc_html__( |
|
484 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
485 | - 'event_espresso' |
|
486 | - ), |
|
487 | - $this->_nicename |
|
488 | - ), |
|
489 | - __FILE__, |
|
490 | - __FUNCTION__, |
|
491 | - __LINE__ |
|
492 | - ); |
|
493 | - } |
|
494 | - $format_string = $this->_get_date_time_output($schema); |
|
495 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
496 | - if ($schema) { |
|
497 | - $timezone_string = ''; |
|
498 | - if ($this->_display_timezone()) { |
|
499 | - // must be explicit because schema could equal true. |
|
500 | - if ($schema === 'no_html') { |
|
501 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
502 | - } else { |
|
503 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
504 | - } |
|
505 | - } |
|
506 | - |
|
507 | - return $DateTime->format($format_string) . $timezone_string; |
|
508 | - } |
|
509 | - return $DateTime->format($format_string); |
|
510 | - } |
|
511 | - |
|
512 | - |
|
513 | - /** |
|
514 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
515 | - * timezone). |
|
516 | - * |
|
517 | - * @param mixed $datetime_value u |
|
518 | - * @return string mysql timestamp in UTC |
|
519 | - * @throws EE_Error |
|
520 | - * @throws Exception |
|
521 | - */ |
|
522 | - public function prepare_for_use_in_db($datetime_value): ?string |
|
523 | - { |
|
524 | - // we allow an empty value or DateTime object, but nothing else. |
|
525 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
526 | - $datetime_value = $this->_get_date_object($datetime_value); |
|
527 | - if (! $datetime_value instanceof DateTime) { |
|
528 | - throw new EE_Error( |
|
529 | - sprintf( |
|
530 | - esc_html__( |
|
531 | - 'The incoming value being prepared for setting in the database for the %1$s field must either be empty or a php DateTime object, instead of: %2$s %3$s', |
|
532 | - 'event_espresso' |
|
533 | - ), |
|
534 | - $this->get_name(), |
|
535 | - '<br />', |
|
536 | - print_r($datetime_value, true) |
|
537 | - ) |
|
538 | - ); |
|
539 | - } |
|
540 | - } |
|
541 | - |
|
542 | - if ($datetime_value instanceof DateTime) { |
|
543 | - if (! $datetime_value instanceof DbSafeDateTime) { |
|
544 | - $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
545 | - } |
|
546 | - EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
547 | - return $datetime_value->format( |
|
548 | - EE_Datetime_Field::mysql_timestamp_format |
|
549 | - ); |
|
550 | - } |
|
551 | - |
|
552 | - // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
553 | - return ! $this->_nullable |
|
554 | - ? current_time('mysql', true) |
|
555 | - : null; |
|
556 | - } |
|
557 | - |
|
558 | - |
|
559 | - /** |
|
560 | - * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
561 | - * allowed) |
|
562 | - * |
|
563 | - * @param string $datetime_string mysql timestamp in UTC |
|
564 | - * @return bool|DbSafeDateTime|null |
|
565 | - * @throws EE_Error |
|
566 | - * @throws Exception |
|
567 | - */ |
|
568 | - public function prepare_for_set_from_db($datetime_string) |
|
569 | - { |
|
570 | - // if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
571 | - if (empty($datetime_string) && $this->_nullable) { |
|
572 | - return null; |
|
573 | - } |
|
574 | - // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
575 | - $DateTime = empty($datetime_string) |
|
576 | - ? new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()) |
|
577 | - : DbSafeDateTime::createFromFormat( |
|
578 | - EE_Datetime_Field::mysql_timestamp_format, |
|
579 | - $datetime_string, |
|
580 | - $this->get_UTC_DateTimeZone() |
|
581 | - ); |
|
582 | - |
|
583 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
584 | - // if still no datetime object, then let's just use now |
|
585 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
586 | - } |
|
587 | - // THEN apply the field's set DateTimeZone |
|
588 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
589 | - return $DateTime; |
|
590 | - } |
|
591 | - |
|
592 | - |
|
593 | - /** |
|
594 | - * All this method does is determine if we're going to display the timezone string or not on any output. |
|
595 | - * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
596 | - * If so, then true. |
|
597 | - * |
|
598 | - * @return bool true for yes false for no |
|
599 | - * @throws Exception |
|
600 | - */ |
|
601 | - protected function _display_timezone(): bool |
|
602 | - { |
|
603 | - // first let's do a comparison of timezone strings. |
|
604 | - // If they match then we can get out without any further calculations |
|
605 | - $blog_string = get_option('timezone_string'); |
|
606 | - if ($blog_string === $this->_timezone_string) { |
|
607 | - return false; |
|
608 | - } |
|
609 | - // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
610 | - $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
611 | - $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
612 | - // now compare |
|
613 | - return $blog_offset !== $this_offset; |
|
614 | - } |
|
615 | - |
|
616 | - |
|
617 | - /** |
|
618 | - * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
619 | - * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
620 | - * with. |
|
621 | - * |
|
622 | - * @param int|string|DateTime|null $date_string This should be the incoming date string. It's assumed to be in |
|
623 | - * the format that is set on the date_field (or DateTime object)! |
|
624 | - * @return DateTime|null |
|
625 | - * @throws Exception |
|
626 | - */ |
|
627 | - protected function _get_date_object($date_string) |
|
628 | - { |
|
629 | - // first if this is an empty date_string and nullable is allowed, just return null. |
|
630 | - if ($this->_nullable && empty($date_string)) { |
|
631 | - return null; |
|
632 | - } |
|
633 | - |
|
634 | - // if incoming date |
|
635 | - if ($date_string instanceof DateTime) { |
|
636 | - EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
637 | - return $date_string; |
|
638 | - } |
|
639 | - // if empty date_string and made it here. |
|
640 | - // Return a datetime object for now in the given timezone. |
|
641 | - if (empty($date_string)) { |
|
642 | - return new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
643 | - } |
|
644 | - // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
645 | - if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
646 | - try { |
|
647 | - // This is operating under the assumption that the incoming Unix timestamp |
|
648 | - // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
649 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
650 | - $DateTime->setTimestamp($date_string); |
|
651 | - |
|
652 | - return $DateTime; |
|
653 | - } catch (Exception $e) { |
|
654 | - // should be rare, but if things got fooled then let's just continue |
|
655 | - } |
|
656 | - } |
|
657 | - // not a unix timestamp. So we will use the set format on this object and set timezone to |
|
658 | - // create the DateTime object. |
|
659 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
660 | - try { |
|
661 | - $DateTime = DbSafeDateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
662 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
663 | - throw new EE_Error( |
|
664 | - sprintf( |
|
665 | - esc_html__( |
|
666 | - '"%1$s" does not represent a valid Date Time in the format "%2$s".', |
|
667 | - 'event_espresso' |
|
668 | - ), |
|
669 | - $date_string, |
|
670 | - $format |
|
671 | - ) |
|
672 | - ); |
|
673 | - } |
|
674 | - } catch (Exception $e) { |
|
675 | - // if we made it here then likely then something went really wrong. |
|
676 | - // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
677 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
678 | - } |
|
679 | - |
|
680 | - return $DateTime; |
|
681 | - } |
|
682 | - |
|
683 | - |
|
684 | - /** |
|
685 | - * get_timezone_transitions |
|
686 | - * |
|
687 | - * @param DateTimeZone $DateTimeZone |
|
688 | - * @param int|null $time |
|
689 | - * @param bool|null $first_only |
|
690 | - * @return array |
|
691 | - */ |
|
692 | - public function get_timezone_transitions( |
|
693 | - DateTimeZone $DateTimeZone, |
|
694 | - ?int $time = null, |
|
695 | - bool $first_only = true |
|
696 | - ): array { |
|
697 | - return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
698 | - } |
|
699 | - |
|
700 | - |
|
701 | - /** |
|
702 | - * get_timezone_offset |
|
703 | - * |
|
704 | - * @param DateTimeZone $DateTimeZone |
|
705 | - * @param int|string|null $time |
|
706 | - * @return mixed |
|
707 | - * @throws DomainException |
|
708 | - */ |
|
709 | - public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
710 | - { |
|
711 | - return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
712 | - } |
|
713 | - |
|
714 | - |
|
715 | - /** |
|
716 | - * This will take an incoming timezone string and return the abbreviation for that timezone |
|
717 | - * |
|
718 | - * @param string|null $timezone_string |
|
719 | - * @return string abbreviation |
|
720 | - * @throws Exception |
|
721 | - */ |
|
722 | - public function get_timezone_abbrev(?string $timezone_string = ''): string |
|
723 | - { |
|
724 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
725 | - $dateTime = new DateTime(EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
726 | - return $dateTime->format('T'); |
|
727 | - } |
|
728 | - |
|
729 | - |
|
730 | - /** |
|
731 | - * Overrides the parent to allow for having a dynamic "now" value |
|
732 | - * |
|
733 | - * @return mixed |
|
734 | - */ |
|
735 | - public function get_default_value() |
|
736 | - { |
|
737 | - if ($this->_default_value === EE_Datetime_Field::now) { |
|
738 | - return time(); |
|
739 | - } |
|
740 | - return parent::get_default_value(); |
|
741 | - } |
|
742 | - |
|
743 | - |
|
744 | - /** |
|
745 | - * Gets the default datetime object from the field's default time |
|
746 | - * |
|
747 | - * @return DbSafeDateTime|DateTime|null |
|
748 | - * @throws InvalidArgumentException |
|
749 | - * @throws InvalidDataTypeException |
|
750 | - * @throws InvalidInterfaceException |
|
751 | - * @throws Exception |
|
752 | - * @since 4.9.66.p |
|
753 | - */ |
|
754 | - public function getDefaultDateTimeObj() |
|
755 | - { |
|
756 | - $default_raw = $this->get_default_value(); |
|
757 | - if ($default_raw instanceof DateTime) { |
|
758 | - return $default_raw; |
|
759 | - } |
|
760 | - if (is_null($default_raw)) { |
|
761 | - return null; |
|
762 | - } |
|
763 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($this->get_timezone()); |
|
764 | - $timezone = new DateTimeZone($timezone_string); |
|
765 | - |
|
766 | - // can't pass unix timestamps directly to Datetime constructor |
|
767 | - if (is_numeric($default_raw)) { |
|
768 | - $datetime = new DbSafeDateTime(); |
|
769 | - $datetime->setTimestamp($default_raw); |
|
770 | - return $datetime; |
|
771 | - } |
|
772 | - return new DbSafeDateTime($default_raw, $timezone); |
|
773 | - } |
|
774 | - |
|
775 | - |
|
776 | - public function getSchemaDescription(): string |
|
777 | - { |
|
778 | - return sprintf( |
|
779 | - esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
780 | - $this->get_nicename() |
|
781 | - ); |
|
782 | - } |
|
23 | + /** |
|
24 | + * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
25 | + * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
26 | + * |
|
27 | + * @type string unix_timestamp_regex |
|
28 | + */ |
|
29 | + const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
30 | + |
|
31 | + /** |
|
32 | + * @type string mysql_timestamp_format |
|
33 | + */ |
|
34 | + const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
35 | + |
|
36 | + /** |
|
37 | + * @type string mysql_date_format |
|
38 | + */ |
|
39 | + const mysql_date_format = 'Y-m-d'; |
|
40 | + |
|
41 | + /** |
|
42 | + * @type string mysql_time_format |
|
43 | + */ |
|
44 | + const mysql_time_format = 'H:i:s'; |
|
45 | + |
|
46 | + /** |
|
47 | + * Const for using in the default value. If the field's default is set to this, |
|
48 | + * then we will return the time of calling `get_default_value()`, not |
|
49 | + * just the current time at construction |
|
50 | + */ |
|
51 | + const now = 'now'; |
|
52 | + |
|
53 | + /** |
|
54 | + * The following properties hold the default formats for date and time. |
|
55 | + * Defaults are set via the constructor and can be overridden on class instantiation. |
|
56 | + * However they can also be overridden later by the set_format() method |
|
57 | + * (and corresponding set_date_format, set_time_format methods); |
|
58 | + */ |
|
59 | + |
|
60 | + protected string $_date_format = ''; |
|
61 | + |
|
62 | + protected string $_time_format = ''; |
|
63 | + |
|
64 | + protected string $_pretty_date_format = ''; |
|
65 | + |
|
66 | + protected string $_pretty_time_format = ''; |
|
67 | + |
|
68 | + protected ?DateTimeZone $_DateTimeZone = null; |
|
69 | + |
|
70 | + protected ?DateTimeZone $_UTC_DateTimeZone = null; |
|
71 | + |
|
72 | + protected ?DateTimeZone $_blog_DateTimeZone = null; |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
77 | + * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
78 | + * and time returned via getters. |
|
79 | + * |
|
80 | + * @var mixed (null|string) |
|
81 | + */ |
|
82 | + protected $_date_time_output; |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * timezone string |
|
87 | + * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
88 | + * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
89 | + * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
90 | + */ |
|
91 | + protected ?string $_timezone_string = null; |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
96 | + * offsets for comparison purposes). |
|
97 | + * |
|
98 | + * @var int |
|
99 | + */ |
|
100 | + protected int $_blog_offset = 0; |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * @param string $table_column |
|
105 | + * @param string $nice_name |
|
106 | + * @param bool $nullable |
|
107 | + * @param string|null $default_value |
|
108 | + * @param string|null $timezone_string |
|
109 | + * @param string|null $date_format |
|
110 | + * @param string|null $time_format |
|
111 | + * @param string|null $pretty_date_format |
|
112 | + * @param string|null $pretty_time_format |
|
113 | + * @throws InvalidArgumentException |
|
114 | + * @throws Exception |
|
115 | + */ |
|
116 | + public function __construct( |
|
117 | + string $table_column, |
|
118 | + string $nice_name, |
|
119 | + bool $nullable, |
|
120 | + ?string $default_value, |
|
121 | + ?string $timezone_string = '', |
|
122 | + ?string $date_format = '', |
|
123 | + ?string $time_format = '', |
|
124 | + ?string $pretty_date_format = '', |
|
125 | + ?string $pretty_time_format = '' |
|
126 | + ) { |
|
127 | + $this->set_date_format($date_format); |
|
128 | + $this->set_time_format($time_format); |
|
129 | + $this->set_date_format($pretty_date_format, true); |
|
130 | + $this->set_time_format($pretty_time_format, true); |
|
131 | + |
|
132 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
133 | + $this->set_timezone($timezone_string); |
|
134 | + $this->setSchemaType(SchemaType::STRING); |
|
135 | + $this->setSchemaFormat(SchemaFormat::DATETIME); |
|
136 | + $this->setDataType(DataType::STRING); // best to url encode the date using DateTime::ATOM |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * @return DateTimeZone |
|
142 | + * @throws Exception |
|
143 | + */ |
|
144 | + public function get_UTC_DateTimeZone(): DateTimeZone |
|
145 | + { |
|
146 | + return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
147 | + ? $this->_UTC_DateTimeZone |
|
148 | + : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * @return DateTimeZone |
|
154 | + * @throws Exception |
|
155 | + */ |
|
156 | + public function get_blog_DateTimeZone(): DateTimeZone |
|
157 | + { |
|
158 | + return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
159 | + ? $this->_blog_DateTimeZone |
|
160 | + : $this->_create_timezone_object_from_timezone_string(); |
|
161 | + } |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * this prepares any incoming date data and make sure it's converted to a UTC unix timestamp |
|
166 | + * |
|
167 | + * @param string|int|null $value value inputted for field on model object |
|
168 | + * could be a string formatted date time or int unix timestamp |
|
169 | + * @return DateTime |
|
170 | + * @throws Exception |
|
171 | + */ |
|
172 | + public function prepare_for_set($value) |
|
173 | + { |
|
174 | + return $this->_get_date_object($value); |
|
175 | + } |
|
176 | + |
|
177 | + |
|
178 | + /** |
|
179 | + * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
180 | + * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
181 | + * |
|
182 | + * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
183 | + * @return string The final assembled format string. |
|
184 | + */ |
|
185 | + protected function _get_date_time_output(bool $pretty = false): string |
|
186 | + { |
|
187 | + switch ($this->_date_time_output) { |
|
188 | + case 'time': |
|
189 | + return $pretty |
|
190 | + ? $this->_pretty_time_format |
|
191 | + : $this->_time_format; |
|
192 | + |
|
193 | + case 'date': |
|
194 | + return $pretty |
|
195 | + ? $this->_pretty_date_format |
|
196 | + : $this->_date_format; |
|
197 | + |
|
198 | + default: |
|
199 | + return $pretty |
|
200 | + ? trim($this->_pretty_date_format . ' ' . $this->_pretty_time_format) |
|
201 | + : trim($this->_date_format . ' ' . $this->_time_format); |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
208 | + * returned (using the format properties) |
|
209 | + * |
|
210 | + * @param string|null $what acceptable values are 'time' or 'date'. |
|
211 | + * Any other value will be set but will always result |
|
212 | + * in both 'date' and 'time' being returned. |
|
213 | + * @return void |
|
214 | + */ |
|
215 | + public function set_date_time_output(?string $what = null) |
|
216 | + { |
|
217 | + $this->_date_time_output = $what; |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
223 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
224 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
225 | + * We also set some other properties in this method. |
|
226 | + * |
|
227 | + * @param string|null $timezone_string A valid timezone string as described by @link |
|
228 | + * http://www.php.net/manual/en/timezones.php |
|
229 | + * @return void |
|
230 | + * @throws InvalidArgumentException |
|
231 | + * @throws InvalidDataTypeException |
|
232 | + * @throws InvalidInterfaceException |
|
233 | + * @throws Exception |
|
234 | + */ |
|
235 | + public function set_timezone(?string $timezone_string = '') |
|
236 | + { |
|
237 | + if (empty($timezone_string) && $this->_timezone_string) { |
|
238 | + // leave the timezone AS-IS if we already have one and |
|
239 | + // the function arg didn't provide one |
|
240 | + return; |
|
241 | + } |
|
242 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
243 | + $this->_timezone_string = ! empty($timezone_string) |
|
244 | + ? $timezone_string |
|
245 | + : 'UTC'; |
|
246 | + $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * _create_timezone_object_from_timezone_name |
|
252 | + * |
|
253 | + * @access protected |
|
254 | + * @param string|null $timezone_string |
|
255 | + * @return DateTimeZone |
|
256 | + * @throws InvalidArgumentException |
|
257 | + * @throws InvalidDataTypeException |
|
258 | + * @throws InvalidInterfaceException |
|
259 | + * @throws Exception |
|
260 | + */ |
|
261 | + protected function _create_timezone_object_from_timezone_string(?string $timezone_string = ''): DateTimeZone |
|
262 | + { |
|
263 | + return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * This just returns whatever is set for the current timezone. |
|
269 | + * |
|
270 | + * @access public |
|
271 | + * @return string timezone string |
|
272 | + */ |
|
273 | + public function get_timezone(): string |
|
274 | + { |
|
275 | + return (string) $this->_timezone_string; |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * set the $_date_format property |
|
281 | + * |
|
282 | + * @access public |
|
283 | + * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
284 | + * @param bool $pretty Whether to set pretty format or not. |
|
285 | + * @return void |
|
286 | + */ |
|
287 | + public function set_date_format(string $format, bool $pretty = false) |
|
288 | + { |
|
289 | + if ($pretty) { |
|
290 | + $this->_pretty_date_format = new DateFormat($format); |
|
291 | + } else { |
|
292 | + $this->_date_format = new DateFormat($format); |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + |
|
297 | + /** |
|
298 | + * return the $_date_format property value. |
|
299 | + * |
|
300 | + * @param bool $pretty Whether to get pretty format or not. |
|
301 | + * @return string |
|
302 | + */ |
|
303 | + public function get_date_format(bool $pretty = false): string |
|
304 | + { |
|
305 | + return $pretty |
|
306 | + ? $this->_pretty_date_format |
|
307 | + : $this->_date_format; |
|
308 | + } |
|
309 | + |
|
310 | + |
|
311 | + /** |
|
312 | + * set the $_time_format property |
|
313 | + * |
|
314 | + * @access public |
|
315 | + * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
316 | + * @param bool $pretty Whether to set pretty format or not. |
|
317 | + * @return void |
|
318 | + */ |
|
319 | + public function set_time_format(string $format, bool $pretty = false) |
|
320 | + { |
|
321 | + if ($pretty) { |
|
322 | + $this->_pretty_time_format = new TimeFormat($format); |
|
323 | + } else { |
|
324 | + $this->_time_format = new TimeFormat($format); |
|
325 | + } |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * return the $_time_format property value. |
|
331 | + * |
|
332 | + * @param bool $pretty Whether to get pretty format or not. |
|
333 | + * @return string |
|
334 | + */ |
|
335 | + public function get_time_format(bool $pretty = false): string |
|
336 | + { |
|
337 | + return $pretty |
|
338 | + ? $this->_pretty_time_format |
|
339 | + : $this->_time_format; |
|
340 | + } |
|
341 | + |
|
342 | + |
|
343 | + /** |
|
344 | + * set the $_pretty_date_format property |
|
345 | + * |
|
346 | + * @access public |
|
347 | + * @param string|null $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
348 | + * @return void |
|
349 | + */ |
|
350 | + public function set_pretty_date_format(?string $format) |
|
351 | + { |
|
352 | + $this->set_date_format($format, true); |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * set the $_pretty_time_format property |
|
358 | + * |
|
359 | + * @access public |
|
360 | + * @param string|null $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
361 | + * @return void |
|
362 | + */ |
|
363 | + public function set_pretty_time_format(?string $format) |
|
364 | + { |
|
365 | + $this->set_time_format($format, true); |
|
366 | + } |
|
367 | + |
|
368 | + |
|
369 | + /** |
|
370 | + * Only sets the time portion of the datetime. |
|
371 | + * |
|
372 | + * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
373 | + * @param DateTime $current current DateTime object for the datetime field |
|
374 | + * @return DateTime |
|
375 | + */ |
|
376 | + public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current): DateTime |
|
377 | + { |
|
378 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
379 | + // Otherwise parse the string. |
|
380 | + if ($time_to_set_string instanceof DateTime) { |
|
381 | + $parsed = [ |
|
382 | + 'hour' => $time_to_set_string->format('H'), |
|
383 | + 'minute' => $time_to_set_string->format('i'), |
|
384 | + 'second' => $time_to_set_string->format('s'), |
|
385 | + ]; |
|
386 | + } else { |
|
387 | + // parse incoming string |
|
388 | + $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
389 | + } |
|
390 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
391 | + return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
392 | + } |
|
393 | + |
|
394 | + |
|
395 | + /** |
|
396 | + * Only sets the date portion of the datetime. |
|
397 | + * |
|
398 | + * @param string|DateTime $date_to_set_string like "Friday, January 8th" or a DateTime object. |
|
399 | + * @param DateTime $current current DateTime object for the datetime field |
|
400 | + * @return DateTime |
|
401 | + */ |
|
402 | + public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current): DateTime |
|
403 | + { |
|
404 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
405 | + // Otherwise parse the string. |
|
406 | + if ($date_to_set_string instanceof DateTime) { |
|
407 | + $parsed = [ |
|
408 | + 'year' => $date_to_set_string->format('Y'), |
|
409 | + 'month' => $date_to_set_string->format('m'), |
|
410 | + 'day' => $date_to_set_string->format('d'), |
|
411 | + ]; |
|
412 | + } else { |
|
413 | + // parse incoming string |
|
414 | + $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
415 | + } |
|
416 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
417 | + return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
418 | + } |
|
419 | + |
|
420 | + |
|
421 | + /** |
|
422 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). |
|
423 | + * When the datetime gets to this stage it should ALREADY be in UTC time |
|
424 | + * |
|
425 | + * @param int|string|DateTime|null $DateTime |
|
426 | + * @return string formatted date time for given timezone |
|
427 | + * @throws EE_Error |
|
428 | + */ |
|
429 | + public function prepare_for_get($DateTime): string |
|
430 | + { |
|
431 | + return $this->_prepare_for_display($DateTime); |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + /** |
|
436 | + * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
437 | + * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
438 | + * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
439 | + * abbreviation to the date_string. |
|
440 | + * |
|
441 | + * @param mixed $DateTime |
|
442 | + * @param string|null $schema |
|
443 | + * @return string |
|
444 | + * @throws EE_Error |
|
445 | + */ |
|
446 | + public function prepare_for_pretty_echoing($DateTime, ?string $schema = null): string |
|
447 | + { |
|
448 | + $schema = $schema ?: true; |
|
449 | + return $this->_prepare_for_display($DateTime, $schema); |
|
450 | + } |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
455 | + * timezone). |
|
456 | + * |
|
457 | + * @param int|string|DateTime|null $DateTime |
|
458 | + * @param bool|string $schema |
|
459 | + * @return string |
|
460 | + * @throws EE_Error |
|
461 | + * @throws Exception |
|
462 | + */ |
|
463 | + protected function _prepare_for_display($DateTime, $schema = false): string |
|
464 | + { |
|
465 | + if (! $DateTime instanceof DateTime) { |
|
466 | + if ($this->_nullable) { |
|
467 | + return ''; |
|
468 | + } |
|
469 | + if (WP_DEBUG) { |
|
470 | + throw new EE_Error( |
|
471 | + sprintf( |
|
472 | + esc_html__( |
|
473 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
474 | + 'event_espresso' |
|
475 | + ), |
|
476 | + $this->_nicename |
|
477 | + ) |
|
478 | + ); |
|
479 | + } |
|
480 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now); |
|
481 | + EE_Error::add_error( |
|
482 | + sprintf( |
|
483 | + esc_html__( |
|
484 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
485 | + 'event_espresso' |
|
486 | + ), |
|
487 | + $this->_nicename |
|
488 | + ), |
|
489 | + __FILE__, |
|
490 | + __FUNCTION__, |
|
491 | + __LINE__ |
|
492 | + ); |
|
493 | + } |
|
494 | + $format_string = $this->_get_date_time_output($schema); |
|
495 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
496 | + if ($schema) { |
|
497 | + $timezone_string = ''; |
|
498 | + if ($this->_display_timezone()) { |
|
499 | + // must be explicit because schema could equal true. |
|
500 | + if ($schema === 'no_html') { |
|
501 | + $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
502 | + } else { |
|
503 | + $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
504 | + } |
|
505 | + } |
|
506 | + |
|
507 | + return $DateTime->format($format_string) . $timezone_string; |
|
508 | + } |
|
509 | + return $DateTime->format($format_string); |
|
510 | + } |
|
511 | + |
|
512 | + |
|
513 | + /** |
|
514 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
515 | + * timezone). |
|
516 | + * |
|
517 | + * @param mixed $datetime_value u |
|
518 | + * @return string mysql timestamp in UTC |
|
519 | + * @throws EE_Error |
|
520 | + * @throws Exception |
|
521 | + */ |
|
522 | + public function prepare_for_use_in_db($datetime_value): ?string |
|
523 | + { |
|
524 | + // we allow an empty value or DateTime object, but nothing else. |
|
525 | + if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
526 | + $datetime_value = $this->_get_date_object($datetime_value); |
|
527 | + if (! $datetime_value instanceof DateTime) { |
|
528 | + throw new EE_Error( |
|
529 | + sprintf( |
|
530 | + esc_html__( |
|
531 | + 'The incoming value being prepared for setting in the database for the %1$s field must either be empty or a php DateTime object, instead of: %2$s %3$s', |
|
532 | + 'event_espresso' |
|
533 | + ), |
|
534 | + $this->get_name(), |
|
535 | + '<br />', |
|
536 | + print_r($datetime_value, true) |
|
537 | + ) |
|
538 | + ); |
|
539 | + } |
|
540 | + } |
|
541 | + |
|
542 | + if ($datetime_value instanceof DateTime) { |
|
543 | + if (! $datetime_value instanceof DbSafeDateTime) { |
|
544 | + $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
545 | + } |
|
546 | + EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
547 | + return $datetime_value->format( |
|
548 | + EE_Datetime_Field::mysql_timestamp_format |
|
549 | + ); |
|
550 | + } |
|
551 | + |
|
552 | + // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
553 | + return ! $this->_nullable |
|
554 | + ? current_time('mysql', true) |
|
555 | + : null; |
|
556 | + } |
|
557 | + |
|
558 | + |
|
559 | + /** |
|
560 | + * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
561 | + * allowed) |
|
562 | + * |
|
563 | + * @param string $datetime_string mysql timestamp in UTC |
|
564 | + * @return bool|DbSafeDateTime|null |
|
565 | + * @throws EE_Error |
|
566 | + * @throws Exception |
|
567 | + */ |
|
568 | + public function prepare_for_set_from_db($datetime_string) |
|
569 | + { |
|
570 | + // if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
571 | + if (empty($datetime_string) && $this->_nullable) { |
|
572 | + return null; |
|
573 | + } |
|
574 | + // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
575 | + $DateTime = empty($datetime_string) |
|
576 | + ? new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()) |
|
577 | + : DbSafeDateTime::createFromFormat( |
|
578 | + EE_Datetime_Field::mysql_timestamp_format, |
|
579 | + $datetime_string, |
|
580 | + $this->get_UTC_DateTimeZone() |
|
581 | + ); |
|
582 | + |
|
583 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
584 | + // if still no datetime object, then let's just use now |
|
585 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
586 | + } |
|
587 | + // THEN apply the field's set DateTimeZone |
|
588 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
589 | + return $DateTime; |
|
590 | + } |
|
591 | + |
|
592 | + |
|
593 | + /** |
|
594 | + * All this method does is determine if we're going to display the timezone string or not on any output. |
|
595 | + * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
596 | + * If so, then true. |
|
597 | + * |
|
598 | + * @return bool true for yes false for no |
|
599 | + * @throws Exception |
|
600 | + */ |
|
601 | + protected function _display_timezone(): bool |
|
602 | + { |
|
603 | + // first let's do a comparison of timezone strings. |
|
604 | + // If they match then we can get out without any further calculations |
|
605 | + $blog_string = get_option('timezone_string'); |
|
606 | + if ($blog_string === $this->_timezone_string) { |
|
607 | + return false; |
|
608 | + } |
|
609 | + // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
610 | + $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
611 | + $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
612 | + // now compare |
|
613 | + return $blog_offset !== $this_offset; |
|
614 | + } |
|
615 | + |
|
616 | + |
|
617 | + /** |
|
618 | + * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
619 | + * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
620 | + * with. |
|
621 | + * |
|
622 | + * @param int|string|DateTime|null $date_string This should be the incoming date string. It's assumed to be in |
|
623 | + * the format that is set on the date_field (or DateTime object)! |
|
624 | + * @return DateTime|null |
|
625 | + * @throws Exception |
|
626 | + */ |
|
627 | + protected function _get_date_object($date_string) |
|
628 | + { |
|
629 | + // first if this is an empty date_string and nullable is allowed, just return null. |
|
630 | + if ($this->_nullable && empty($date_string)) { |
|
631 | + return null; |
|
632 | + } |
|
633 | + |
|
634 | + // if incoming date |
|
635 | + if ($date_string instanceof DateTime) { |
|
636 | + EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
637 | + return $date_string; |
|
638 | + } |
|
639 | + // if empty date_string and made it here. |
|
640 | + // Return a datetime object for now in the given timezone. |
|
641 | + if (empty($date_string)) { |
|
642 | + return new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
643 | + } |
|
644 | + // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
645 | + if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
646 | + try { |
|
647 | + // This is operating under the assumption that the incoming Unix timestamp |
|
648 | + // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
649 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
650 | + $DateTime->setTimestamp($date_string); |
|
651 | + |
|
652 | + return $DateTime; |
|
653 | + } catch (Exception $e) { |
|
654 | + // should be rare, but if things got fooled then let's just continue |
|
655 | + } |
|
656 | + } |
|
657 | + // not a unix timestamp. So we will use the set format on this object and set timezone to |
|
658 | + // create the DateTime object. |
|
659 | + $format = $this->_date_format . ' ' . $this->_time_format; |
|
660 | + try { |
|
661 | + $DateTime = DbSafeDateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
662 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
663 | + throw new EE_Error( |
|
664 | + sprintf( |
|
665 | + esc_html__( |
|
666 | + '"%1$s" does not represent a valid Date Time in the format "%2$s".', |
|
667 | + 'event_espresso' |
|
668 | + ), |
|
669 | + $date_string, |
|
670 | + $format |
|
671 | + ) |
|
672 | + ); |
|
673 | + } |
|
674 | + } catch (Exception $e) { |
|
675 | + // if we made it here then likely then something went really wrong. |
|
676 | + // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
677 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
678 | + } |
|
679 | + |
|
680 | + return $DateTime; |
|
681 | + } |
|
682 | + |
|
683 | + |
|
684 | + /** |
|
685 | + * get_timezone_transitions |
|
686 | + * |
|
687 | + * @param DateTimeZone $DateTimeZone |
|
688 | + * @param int|null $time |
|
689 | + * @param bool|null $first_only |
|
690 | + * @return array |
|
691 | + */ |
|
692 | + public function get_timezone_transitions( |
|
693 | + DateTimeZone $DateTimeZone, |
|
694 | + ?int $time = null, |
|
695 | + bool $first_only = true |
|
696 | + ): array { |
|
697 | + return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
698 | + } |
|
699 | + |
|
700 | + |
|
701 | + /** |
|
702 | + * get_timezone_offset |
|
703 | + * |
|
704 | + * @param DateTimeZone $DateTimeZone |
|
705 | + * @param int|string|null $time |
|
706 | + * @return mixed |
|
707 | + * @throws DomainException |
|
708 | + */ |
|
709 | + public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
710 | + { |
|
711 | + return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
712 | + } |
|
713 | + |
|
714 | + |
|
715 | + /** |
|
716 | + * This will take an incoming timezone string and return the abbreviation for that timezone |
|
717 | + * |
|
718 | + * @param string|null $timezone_string |
|
719 | + * @return string abbreviation |
|
720 | + * @throws Exception |
|
721 | + */ |
|
722 | + public function get_timezone_abbrev(?string $timezone_string = ''): string |
|
723 | + { |
|
724 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
725 | + $dateTime = new DateTime(EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
726 | + return $dateTime->format('T'); |
|
727 | + } |
|
728 | + |
|
729 | + |
|
730 | + /** |
|
731 | + * Overrides the parent to allow for having a dynamic "now" value |
|
732 | + * |
|
733 | + * @return mixed |
|
734 | + */ |
|
735 | + public function get_default_value() |
|
736 | + { |
|
737 | + if ($this->_default_value === EE_Datetime_Field::now) { |
|
738 | + return time(); |
|
739 | + } |
|
740 | + return parent::get_default_value(); |
|
741 | + } |
|
742 | + |
|
743 | + |
|
744 | + /** |
|
745 | + * Gets the default datetime object from the field's default time |
|
746 | + * |
|
747 | + * @return DbSafeDateTime|DateTime|null |
|
748 | + * @throws InvalidArgumentException |
|
749 | + * @throws InvalidDataTypeException |
|
750 | + * @throws InvalidInterfaceException |
|
751 | + * @throws Exception |
|
752 | + * @since 4.9.66.p |
|
753 | + */ |
|
754 | + public function getDefaultDateTimeObj() |
|
755 | + { |
|
756 | + $default_raw = $this->get_default_value(); |
|
757 | + if ($default_raw instanceof DateTime) { |
|
758 | + return $default_raw; |
|
759 | + } |
|
760 | + if (is_null($default_raw)) { |
|
761 | + return null; |
|
762 | + } |
|
763 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($this->get_timezone()); |
|
764 | + $timezone = new DateTimeZone($timezone_string); |
|
765 | + |
|
766 | + // can't pass unix timestamps directly to Datetime constructor |
|
767 | + if (is_numeric($default_raw)) { |
|
768 | + $datetime = new DbSafeDateTime(); |
|
769 | + $datetime->setTimestamp($default_raw); |
|
770 | + return $datetime; |
|
771 | + } |
|
772 | + return new DbSafeDateTime($default_raw, $timezone); |
|
773 | + } |
|
774 | + |
|
775 | + |
|
776 | + public function getSchemaDescription(): string |
|
777 | + { |
|
778 | + return sprintf( |
|
779 | + esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
780 | + $this->get_nicename() |
|
781 | + ); |
|
782 | + } |
|
783 | 783 | } |
@@ -12,78 +12,78 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_Infinite_Integer_Field extends EE_Model_Field_Base |
14 | 14 | { |
15 | - /** |
|
16 | - * @param string $table_column |
|
17 | - * @param string $nicename |
|
18 | - * @param bool $nullable |
|
19 | - * @param null $default_value |
|
20 | - */ |
|
21 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
22 | - { |
|
23 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
24 | - $this->setSchemaType([SchemaType::INTEGER, SchemaType::NULL]); |
|
25 | - $this->setDataType(DataType::INTEGER); |
|
26 | - } |
|
15 | + /** |
|
16 | + * @param string $table_column |
|
17 | + * @param string $nicename |
|
18 | + * @param bool $nullable |
|
19 | + * @param null $default_value |
|
20 | + */ |
|
21 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
22 | + { |
|
23 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
24 | + $this->setSchemaType([SchemaType::INTEGER, SchemaType::NULL]); |
|
25 | + $this->setDataType(DataType::INTEGER); |
|
26 | + } |
|
27 | 27 | |
28 | 28 | |
29 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
30 | - { |
|
31 | - if ($value_of_field_on_model_object === EE_INF) { |
|
32 | - return EE_INF_IN_DB; |
|
33 | - } |
|
34 | - return (int) $value_of_field_on_model_object; |
|
35 | - } |
|
29 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
30 | + { |
|
31 | + if ($value_of_field_on_model_object === EE_INF) { |
|
32 | + return EE_INF_IN_DB; |
|
33 | + } |
|
34 | + return (int) $value_of_field_on_model_object; |
|
35 | + } |
|
36 | 36 | |
37 | 37 | |
38 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
39 | - { |
|
40 | - if ( |
|
41 | - $value_inputted_for_field_on_model_object === EE_INF_IN_DB || |
|
42 | - $value_inputted_for_field_on_model_object === EE_INF || |
|
43 | - $value_inputted_for_field_on_model_object === "EE_INF" || |
|
44 | - $value_inputted_for_field_on_model_object === "" |
|
45 | - ) { |
|
46 | - return EE_INF; |
|
47 | - } |
|
48 | - return (int) $value_inputted_for_field_on_model_object; |
|
49 | - } |
|
38 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
39 | + { |
|
40 | + if ( |
|
41 | + $value_inputted_for_field_on_model_object === EE_INF_IN_DB || |
|
42 | + $value_inputted_for_field_on_model_object === EE_INF || |
|
43 | + $value_inputted_for_field_on_model_object === "EE_INF" || |
|
44 | + $value_inputted_for_field_on_model_object === "" |
|
45 | + ) { |
|
46 | + return EE_INF; |
|
47 | + } |
|
48 | + return (int) $value_inputted_for_field_on_model_object; |
|
49 | + } |
|
50 | 50 | |
51 | 51 | |
52 | - public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
53 | - { |
|
54 | - $intval = (int) $value_inputted_for_field_on_model_object; |
|
55 | - if ($intval == EE_INF_IN_DB) { |
|
56 | - return EE_INF; |
|
57 | - } |
|
58 | - return $intval; |
|
59 | - } |
|
52 | + public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
53 | + { |
|
54 | + $intval = (int) $value_inputted_for_field_on_model_object; |
|
55 | + if ($intval == EE_INF_IN_DB) { |
|
56 | + return EE_INF; |
|
57 | + } |
|
58 | + return $intval; |
|
59 | + } |
|
60 | 60 | |
61 | 61 | |
62 | - /** |
|
63 | - * For outputting this field's value. If you want to output it into an input or something, |
|
64 | - * use $schema=='input', as it will replace EE_INF with ''. If you want a readable version, use $schema=='text' |
|
65 | - * as it will replace EE_INF with i18n Infinite |
|
66 | - * |
|
67 | - * @param mixed $value_on_field_to_be_outputted |
|
68 | - * @param string|null $schema input, symbol, text; or any string you want to show if the value equals EE_INF |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
72 | - { |
|
73 | - if ($value_on_field_to_be_outputted === EE_INF) { |
|
74 | - switch ($schema) { |
|
75 | - case 'input': |
|
76 | - case 'form_input': |
|
77 | - return ''; |
|
78 | - case 'symbol': |
|
79 | - return "∞"; |
|
80 | - case 'text': |
|
81 | - case null: |
|
82 | - return esc_html__("Unlimited", "event_espresso"); |
|
83 | - default: |
|
84 | - return $schema; |
|
85 | - } |
|
86 | - } |
|
87 | - return $value_on_field_to_be_outputted; |
|
88 | - } |
|
62 | + /** |
|
63 | + * For outputting this field's value. If you want to output it into an input or something, |
|
64 | + * use $schema=='input', as it will replace EE_INF with ''. If you want a readable version, use $schema=='text' |
|
65 | + * as it will replace EE_INF with i18n Infinite |
|
66 | + * |
|
67 | + * @param mixed $value_on_field_to_be_outputted |
|
68 | + * @param string|null $schema input, symbol, text; or any string you want to show if the value equals EE_INF |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
72 | + { |
|
73 | + if ($value_on_field_to_be_outputted === EE_INF) { |
|
74 | + switch ($schema) { |
|
75 | + case 'input': |
|
76 | + case 'form_input': |
|
77 | + return ''; |
|
78 | + case 'symbol': |
|
79 | + return "∞"; |
|
80 | + case 'text': |
|
81 | + case null: |
|
82 | + return esc_html__("Unlimited", "event_espresso"); |
|
83 | + default: |
|
84 | + return $schema; |
|
85 | + } |
|
86 | + } |
|
87 | + return $value_on_field_to_be_outputted; |
|
88 | + } |
|
89 | 89 | } |