1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use EventEspresso\core\domain\DomainInterface; |
4
|
|
|
use EventEspresso\core\domain\RequiresDependencyMapInterface; |
5
|
|
|
use EventEspresso\core\domain\RequiresDomainInterface; |
6
|
|
|
|
7
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit('No direct access allowed.'); |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class EE_Addon |
11
|
|
|
* Abstract Parent class for all classes that want to function as EE Addons |
12
|
|
|
* |
13
|
|
|
* @package Event Espresso |
14
|
|
|
* @subpackage core |
15
|
|
|
* @author Michael Nelson, Brent Christensen |
16
|
|
|
*/ |
17
|
|
|
abstract class EE_Addon extends EE_Configurable implements RequiresDependencyMapInterface, RequiresDomainInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* prefix to be added onto an addon's plugin slug to make a wp option name |
23
|
|
|
* which will be used to store the plugin's activation history |
24
|
|
|
*/ |
25
|
|
|
const ee_addon_version_history_option_prefix = 'ee_version_history_'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var $_version |
29
|
|
|
* @type string |
30
|
|
|
*/ |
31
|
|
|
protected $_version = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var $_min_core_version |
35
|
|
|
* @type string |
36
|
|
|
*/ |
37
|
|
|
protected $_min_core_version = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* derived from plugin 'main_file_path using plugin_basename() |
41
|
|
|
* |
42
|
|
|
* @type string $_plugin_basename |
43
|
|
|
*/ |
44
|
|
|
protected $_plugin_basename = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A non-internationalized name to identify this addon for use in URLs, etc |
48
|
|
|
* |
49
|
|
|
* @type string $_plugin_slug |
50
|
|
|
*/ |
51
|
|
|
protected $_plugin_slug = ''; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* A non-internationalized name to identify this addon. Eg 'Calendar','MailChimp',etc/ |
55
|
|
|
* |
56
|
|
|
* @type string _addon_name |
57
|
|
|
*/ |
58
|
|
|
protected $_addon_name = ''; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* one of the EE_System::req_type_* constants |
62
|
|
|
* |
63
|
|
|
* @type int $_req_type |
64
|
|
|
*/ |
65
|
|
|
protected $_req_type; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* page slug to be used when generating the "Settings" link on the WP plugin page |
69
|
|
|
* |
70
|
|
|
* @type string $_plugin_action_slug |
71
|
|
|
*/ |
72
|
|
|
protected $_plugin_action_slug = ''; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* if not empty, inserts a new table row after this plugin's row on the WP Plugins page |
76
|
|
|
* that can be used for adding upgrading/marketing info |
77
|
|
|
* |
78
|
|
|
* @type array $_plugins_page_row |
79
|
|
|
*/ |
80
|
|
|
protected $_plugins_page_row = array(); |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* filepath to the main file, which can be used for register_activation_hook, register_deactivation_hook, etc. |
86
|
|
|
* |
87
|
|
|
* @type string |
88
|
|
|
*/ |
89
|
|
|
protected $_main_plugin_file; |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var EE_Dependency_Map $dependency_map |
94
|
|
|
*/ |
95
|
|
|
private $dependency_map; |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var DomainInterface $domain |
100
|
|
|
*/ |
101
|
|
|
private $domain; |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param EE_Dependency_Map $dependency_map [optional] |
106
|
|
|
* @param DomainInterface $domain [optional] |
107
|
|
|
*/ |
108
|
|
|
public function __construct(EE_Dependency_Map $dependency_map = null, DomainInterface $domain = null) |
109
|
|
|
{ |
110
|
|
|
if($dependency_map instanceof EE_Dependency_Map) { |
111
|
|
|
$this->setDependencyMap($dependency_map); |
112
|
|
|
} |
113
|
|
|
if ($domain instanceof DomainInterface) { |
114
|
|
|
$this->setDomain($domain); |
115
|
|
|
} |
116
|
|
|
add_action('AHEE__EE_System__load_controllers__load_admin_controllers', array($this, 'admin_init')); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param EE_Dependency_Map $dependency_map |
122
|
|
|
*/ |
123
|
|
|
public function setDependencyMap($dependency_map) |
124
|
|
|
{ |
125
|
|
|
$this->dependency_map = $dependency_map; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return EE_Dependency_Map |
131
|
|
|
*/ |
132
|
|
|
public function dependencyMap() |
133
|
|
|
{ |
134
|
|
|
return $this->dependency_map; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param DomainInterface $domain |
140
|
|
|
*/ |
141
|
|
|
public function setDomain(DomainInterface $domain) |
142
|
|
|
{ |
143
|
|
|
$this->domain = $domain; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return DomainInterface |
148
|
|
|
*/ |
149
|
|
|
public function domain() |
150
|
|
|
{ |
151
|
|
|
return $this->domain; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param mixed $version |
157
|
|
|
*/ |
158
|
|
|
public function set_version($version = null) |
159
|
|
|
{ |
160
|
|
|
$this->_version = $version; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* get__version |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function version() |
170
|
|
|
{ |
171
|
|
|
return $this->_version; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param mixed $min_core_version |
177
|
|
|
*/ |
178
|
|
|
public function set_min_core_version($min_core_version = null) |
179
|
|
|
{ |
180
|
|
|
$this->_min_core_version = $min_core_version; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* get__min_core_version |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function min_core_version() |
190
|
|
|
{ |
191
|
|
|
return $this->_min_core_version; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Sets addon_name |
197
|
|
|
* |
198
|
|
|
* @param string $addon_name |
199
|
|
|
* @return boolean |
200
|
|
|
*/ |
201
|
|
|
public function set_name($addon_name) |
202
|
|
|
{ |
203
|
|
|
return $this->_addon_name = $addon_name; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Gets addon_name |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function name() |
213
|
|
|
{ |
214
|
|
|
return $this->_addon_name; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function plugin_basename() |
222
|
|
|
{ |
223
|
|
|
|
224
|
|
|
return $this->_plugin_basename; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string $plugin_basename |
230
|
|
|
*/ |
231
|
|
|
public function set_plugin_basename($plugin_basename) |
232
|
|
|
{ |
233
|
|
|
|
234
|
|
|
$this->_plugin_basename = $plugin_basename; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function plugin_slug() |
242
|
|
|
{ |
243
|
|
|
|
244
|
|
|
return $this->_plugin_slug; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param string $plugin_slug |
250
|
|
|
*/ |
251
|
|
|
public function set_plugin_slug($plugin_slug) |
252
|
|
|
{ |
253
|
|
|
|
254
|
|
|
$this->_plugin_slug = $plugin_slug; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
public function plugin_action_slug() |
262
|
|
|
{ |
263
|
|
|
|
264
|
|
|
return $this->_plugin_action_slug; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string $plugin_action_slug |
270
|
|
|
*/ |
271
|
|
|
public function set_plugin_action_slug($plugin_action_slug) |
272
|
|
|
{ |
273
|
|
|
|
274
|
|
|
$this->_plugin_action_slug = $plugin_action_slug; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
public function get_plugins_page_row() |
282
|
|
|
{ |
283
|
|
|
|
284
|
|
|
return $this->_plugins_page_row; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param array $plugins_page_row |
290
|
|
|
*/ |
291
|
|
|
public function set_plugins_page_row($plugins_page_row = array()) |
292
|
|
|
{ |
293
|
|
|
// sigh.... check for example content that I stupidly merged to master and remove it if found |
294
|
|
|
if (! is_array($plugins_page_row) |
295
|
|
|
&& strpos($plugins_page_row, '<h3>Promotions Addon Upsell Info</h3>') !== false |
296
|
|
|
) { |
297
|
|
|
$plugins_page_row = array(); |
298
|
|
|
} |
299
|
|
|
$this->_plugins_page_row = (array) $plugins_page_row; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Called when EE core detects this addon has been activated for the first time. |
305
|
|
|
* If the site isn't in maintenance mode, should setup the addon's database |
306
|
|
|
* |
307
|
|
|
* @return void |
308
|
|
|
*/ |
309
|
|
View Code Duplication |
public function new_install() |
310
|
|
|
{ |
311
|
|
|
$classname = get_class($this); |
312
|
|
|
do_action("AHEE__{$classname}__new_install"); |
313
|
|
|
do_action('AHEE__EE_Addon__new_install', $this); |
314
|
|
|
EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
315
|
|
|
add_action( |
316
|
|
|
'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
317
|
|
|
array($this, 'initialize_db_if_no_migrations_required') |
318
|
|
|
); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Called when EE core detects this addon has been reactivated. When this happens, |
324
|
|
|
* it's good to just check that your data is still intact |
325
|
|
|
* |
326
|
|
|
* @return void |
327
|
|
|
*/ |
328
|
|
View Code Duplication |
public function reactivation() |
329
|
|
|
{ |
330
|
|
|
$classname = get_class($this); |
331
|
|
|
do_action("AHEE__{$classname}__reactivation"); |
332
|
|
|
do_action('AHEE__EE_Addon__reactivation', $this); |
333
|
|
|
EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
334
|
|
|
add_action( |
335
|
|
|
'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
336
|
|
|
array($this, 'initialize_db_if_no_migrations_required') |
337
|
|
|
); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Called when the registered deactivation hook for this addon fires. |
343
|
|
|
* @throws EE_Error |
344
|
|
|
*/ |
345
|
|
|
public function deactivation() |
346
|
|
|
{ |
347
|
|
|
$classname = get_class($this); |
348
|
|
|
do_action("AHEE__{$classname}__deactivation"); |
349
|
|
|
do_action('AHEE__EE_Addon__deactivation', $this); |
350
|
|
|
//check if the site no longer needs to be in maintenance mode |
351
|
|
|
EE_Register_Addon::deregister($this->name()); |
352
|
|
|
EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Takes care of double-checking that we're not in maintenance mode, and then |
358
|
|
|
* initializing this addon's necessary initial data. This is called by default on new activations |
359
|
|
|
* and reactivations. |
360
|
|
|
* |
361
|
|
|
* @param boolean $verify_schema whether to verify the database's schema for this addon, or just its data. |
362
|
|
|
* This is a resource-intensive job so we prefer to only do it when necessary |
363
|
|
|
* @return void |
364
|
|
|
* @throws \EE_Error |
365
|
|
|
*/ |
366
|
|
|
public function initialize_db_if_no_migrations_required($verify_schema = true) |
367
|
|
|
{ |
368
|
|
|
if ($verify_schema === '') { |
369
|
|
|
//wp core bug imo: if no args are passed to `do_action('some_hook_name')` besides the hook's name |
370
|
|
|
//(ie, no 2nd or 3rd arguments), instead of calling the registered callbacks with no arguments, it |
371
|
|
|
//calls them with an argument of an empty string (ie ""), which evaluates to false |
372
|
|
|
//so we need to treat the empty string as if nothing had been passed, and should instead use the default |
373
|
|
|
$verify_schema = true; |
374
|
|
|
} |
375
|
|
|
if (EE_Maintenance_Mode::instance()->level() !== EE_Maintenance_Mode::level_2_complete_maintenance) { |
376
|
|
|
if ($verify_schema) { |
377
|
|
|
$this->initialize_db(); |
378
|
|
|
} |
379
|
|
|
$this->initialize_default_data(); |
380
|
|
|
//@todo: this will probably need to be adjusted in 4.4 as the array changed formats I believe |
381
|
|
|
EE_Data_Migration_Manager::instance()->update_current_database_state_to( |
382
|
|
|
array( |
383
|
|
|
'slug' => $this->name(), |
384
|
|
|
'version' => $this->version(), |
385
|
|
|
) |
386
|
|
|
); |
387
|
|
|
/* make sure core's data is a-ok |
388
|
|
|
* (at the time of writing, we especially want to verify all the caps are present |
389
|
|
|
* because payment method type capabilities are added dynamically, and it's |
390
|
|
|
* possible this addon added a payment method. But it's also possible |
391
|
|
|
* other data needs to be verified) |
392
|
|
|
*/ |
393
|
|
|
EEH_Activation::initialize_db_content(); |
394
|
|
|
update_option('ee_flush_rewrite_rules', true); |
395
|
|
|
//in case there are lots of addons being activated at once, let's force garbage collection |
396
|
|
|
//to help avoid memory limit errors |
397
|
|
|
//EEH_Debug_Tools::instance()->measure_memory( 'db content initialized for ' . get_class( $this), true ); |
398
|
|
|
gc_collect_cycles(); |
399
|
|
|
} else { |
400
|
|
|
//ask the data migration manager to init this addon's data |
401
|
|
|
//when migrations are finished because we can't do it now |
402
|
|
|
EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for($this->name()); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Used to setup this addon's database tables, but not necessarily any default |
409
|
|
|
* data in them. The default is to actually use the most up-to-date data migration script |
410
|
|
|
* for this addon, and just use its schema_changes_before_migration() and schema_changes_after_migration() |
411
|
|
|
* methods to setup the db. |
412
|
|
|
*/ |
413
|
|
|
public function initialize_db() |
414
|
|
|
{ |
415
|
|
|
//find the migration script that sets the database to be compatible with the code |
416
|
|
|
$current_dms_name = EE_Data_Migration_Manager::instance()->get_most_up_to_date_dms($this->name()); |
417
|
|
|
if ($current_dms_name) { |
418
|
|
|
$current_data_migration_script = EE_Registry::instance()->load_dms($current_dms_name); |
419
|
|
|
$current_data_migration_script->set_migrating(false); |
420
|
|
|
$current_data_migration_script->schema_changes_before_migration(); |
421
|
|
|
$current_data_migration_script->schema_changes_after_migration(); |
422
|
|
|
if ($current_data_migration_script->get_errors()) { |
423
|
|
|
foreach ($current_data_migration_script->get_errors() as $error) { |
424
|
|
|
EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__); |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
//if not DMS was found that should be ok. This addon just doesn't require any database changes |
429
|
|
|
EE_Data_Migration_Manager::instance()->update_current_database_state_to( |
430
|
|
|
array( |
431
|
|
|
'slug' => $this->name(), |
432
|
|
|
'version' => $this->version(), |
433
|
|
|
) |
434
|
|
|
); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* If you want to setup default data for the addon, override this method, and call |
440
|
|
|
* parent::initialize_default_data() from within it. This is normally called |
441
|
|
|
* from EE_Addon::initialize_db_if_no_migrations_required(), just after EE_Addon::initialize_db() |
442
|
|
|
* and should verify default data is present (but this is also called |
443
|
|
|
* on reactivations and just after migrations, so please verify you actually want |
444
|
|
|
* to ADD default data, because it may already be present). |
445
|
|
|
* However, please call this parent (currently it just fires a hook which other |
446
|
|
|
* addons may be depending on) |
447
|
|
|
*/ |
448
|
|
|
public function initialize_default_data() |
449
|
|
|
{ |
450
|
|
|
/** |
451
|
|
|
* Called when an addon is ensuring its default data is set (possibly called |
452
|
|
|
* on a reactivation, so first check for the absence of other data before setting |
453
|
|
|
* default data) |
454
|
|
|
* |
455
|
|
|
* @param EE_Addon $addon the addon that called this |
456
|
|
|
*/ |
457
|
|
|
do_action('AHEE__EE_Addon__initialize_default_data__begin', $this); |
458
|
|
|
//override to insert default data. It is safe to use the models here |
459
|
|
|
//because the site should not be in maintenance mode |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* EE Core detected that this addon has been upgraded. We should check if there |
465
|
|
|
* are any new migration scripts, and if so put the site into maintenance mode until |
466
|
|
|
* they're ran |
467
|
|
|
* |
468
|
|
|
* @return void |
469
|
|
|
*/ |
470
|
|
View Code Duplication |
public function upgrade() |
471
|
|
|
{ |
472
|
|
|
$classname = get_class($this); |
473
|
|
|
do_action("AHEE__{$classname}__upgrade"); |
474
|
|
|
do_action('AHEE__EE_Addon__upgrade', $this); |
475
|
|
|
EE_Maintenance_Mode::instance()->set_maintenance_mode_if_db_old(); |
476
|
|
|
//also it's possible there is new default data that needs to be added |
477
|
|
|
add_action( |
478
|
|
|
'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
479
|
|
|
array($this, 'initialize_db_if_no_migrations_required') |
480
|
|
|
); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* If Core detects this addon has been downgraded, you may want to invoke some special logic here. |
486
|
|
|
*/ |
487
|
|
|
public function downgrade() |
488
|
|
|
{ |
489
|
|
|
$classname = get_class($this); |
490
|
|
|
do_action("AHEE__{$classname}__downgrade"); |
491
|
|
|
do_action('AHEE__EE_Addon__downgrade', $this); |
492
|
|
|
//it's possible there's old default data that needs to be double-checked |
493
|
|
|
add_action( |
494
|
|
|
'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
495
|
|
|
array($this, 'initialize_db_if_no_migrations_required') |
496
|
|
|
); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* set_db_update_option_name |
502
|
|
|
* Until we do something better, we'll just check for migration scripts upon |
503
|
|
|
* plugin activation only. In the future, we'll want to do it on plugin updates too |
504
|
|
|
* |
505
|
|
|
* @return bool |
506
|
|
|
*/ |
507
|
|
|
public function set_db_update_option_name() |
508
|
|
|
{ |
509
|
|
|
EE_Error::doing_it_wrong( |
510
|
|
|
__FUNCTION__, |
511
|
|
|
esc_html__( |
512
|
|
|
'EE_Addon::set_db_update_option_name was renamed to EE_Addon::set_activation_indicator_option', |
513
|
|
|
'event_espresso' |
514
|
|
|
), |
515
|
|
|
'4.3.0.alpha.016' |
516
|
|
|
); |
517
|
|
|
//let's just handle this on the next request, ok? right now we're just not really ready |
518
|
|
|
return $this->set_activation_indicator_option(); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Returns the name of the activation indicator option |
524
|
|
|
* (an option which is set temporarily to indicate that this addon was just activated) |
525
|
|
|
* |
526
|
|
|
* @deprecated since version 4.3.0.alpha.016 |
527
|
|
|
* @return string |
528
|
|
|
*/ |
529
|
|
|
public function get_db_update_option_name() |
530
|
|
|
{ |
531
|
|
|
EE_Error::doing_it_wrong( |
532
|
|
|
__FUNCTION__, |
533
|
|
|
esc_html__( |
534
|
|
|
'EE_Addon::get_db_update_option was renamed to EE_Addon::get_activation_indicator_option_name', |
535
|
|
|
'event_espresso' |
536
|
|
|
), |
537
|
|
|
'4.3.0.alpha.016' |
538
|
|
|
); |
539
|
|
|
return $this->get_activation_indicator_option_name(); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* When the addon is activated, this should be called to set a wordpress option that |
545
|
|
|
* indicates it was activated. This is especially useful for detecting reactivations. |
546
|
|
|
* |
547
|
|
|
* @return bool |
548
|
|
|
*/ |
549
|
|
|
public function set_activation_indicator_option() |
550
|
|
|
{ |
551
|
|
|
// let's just handle this on the next request, ok? right now we're just not really ready |
552
|
|
|
return update_option($this->get_activation_indicator_option_name(), true); |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Gets the name of the wp option which is used to temporarily indicate that this addon was activated |
558
|
|
|
* |
559
|
|
|
* @return string |
560
|
|
|
*/ |
561
|
|
|
public function get_activation_indicator_option_name() |
562
|
|
|
{ |
563
|
|
|
return 'ee_activation_' . $this->name(); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* Used by EE_System to set the request type of this addon. Should not be used by addon developers |
569
|
|
|
* |
570
|
|
|
* @param int $req_type |
571
|
|
|
*/ |
572
|
|
|
public function set_req_type($req_type) |
573
|
|
|
{ |
574
|
|
|
$this->_req_type = $req_type; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Returns the request type of this addon (ie, EE_System::req_type_normal, EE_System::req_type_new_activation, |
580
|
|
|
* EE_System::req_type_reactivation, EE_System::req_type_upgrade, or EE_System::req_type_downgrade). This is set by |
581
|
|
|
* EE_System when it is checking for new install or upgrades of addons |
582
|
|
|
*/ |
583
|
|
|
public function detect_req_type() |
584
|
|
|
{ |
585
|
|
|
if (! $this->_req_type) { |
586
|
|
|
$this->detect_activation_or_upgrade(); |
587
|
|
|
} |
588
|
|
|
return $this->_req_type; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Detects the request type for this addon (whether it was just activated, upgrades, a normal request, etc.) |
594
|
|
|
* Should only be called once per request |
595
|
|
|
* |
596
|
|
|
* @return void |
597
|
|
|
*/ |
598
|
|
|
public function detect_activation_or_upgrade() |
599
|
|
|
{ |
600
|
|
|
$activation_history_for_addon = $this->get_activation_history(); |
601
|
|
|
// d($activation_history_for_addon); |
602
|
|
|
$request_type = EE_System::detect_req_type_given_activation_history( |
603
|
|
|
$activation_history_for_addon, |
604
|
|
|
$this->get_activation_indicator_option_name(), |
605
|
|
|
$this->version() |
606
|
|
|
); |
607
|
|
|
$this->set_req_type($request_type); |
608
|
|
|
$classname = get_class($this); |
609
|
|
|
switch ($request_type) { |
610
|
|
|
case EE_System::req_type_new_activation: |
611
|
|
|
do_action("AHEE__{$classname}__detect_activations_or_upgrades__new_activation"); |
612
|
|
|
do_action('AHEE__EE_Addon__detect_activations_or_upgrades__new_activation', $this); |
613
|
|
|
$this->new_install(); |
614
|
|
|
$this->update_list_of_installed_versions($activation_history_for_addon); |
615
|
|
|
break; |
616
|
|
|
case EE_System::req_type_reactivation: |
617
|
|
|
do_action("AHEE__{$classname}__detect_activations_or_upgrades__reactivation"); |
618
|
|
|
do_action('AHEE__EE_Addon__detect_activations_or_upgrades__reactivation', $this); |
619
|
|
|
$this->reactivation(); |
620
|
|
|
$this->update_list_of_installed_versions($activation_history_for_addon); |
621
|
|
|
break; |
622
|
|
|
case EE_System::req_type_upgrade: |
623
|
|
|
do_action("AHEE__{$classname}__detect_activations_or_upgrades__upgrade"); |
624
|
|
|
do_action('AHEE__EE_Addon__detect_activations_or_upgrades__upgrade', $this); |
625
|
|
|
$this->upgrade(); |
626
|
|
|
$this->update_list_of_installed_versions($activation_history_for_addon); |
627
|
|
|
break; |
628
|
|
|
case EE_System::req_type_downgrade: |
629
|
|
|
do_action("AHEE__{$classname}__detect_activations_or_upgrades__downgrade"); |
630
|
|
|
do_action('AHEE__EE_Addon__detect_activations_or_upgrades__downgrade', $this); |
631
|
|
|
$this->downgrade(); |
632
|
|
|
$this->update_list_of_installed_versions($activation_history_for_addon); |
633
|
|
|
break; |
634
|
|
|
case EE_System::req_type_normal: |
635
|
|
|
default: |
636
|
|
|
// $this->_maybe_redirect_to_ee_about(); |
637
|
|
|
break; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
do_action("AHEE__{$classname}__detect_if_activation_or_upgrade__complete"); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* Updates the version history for this addon |
645
|
|
|
* |
646
|
|
|
* @param array $version_history |
647
|
|
|
* @param string $current_version_to_add |
648
|
|
|
* @return boolean success |
649
|
|
|
*/ |
650
|
|
|
public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
651
|
|
|
{ |
652
|
|
|
if (! $version_history) { |
653
|
|
|
$version_history = $this->get_activation_history(); |
654
|
|
|
} |
655
|
|
|
if ($current_version_to_add === null) { |
656
|
|
|
$current_version_to_add = $this->version(); |
657
|
|
|
} |
658
|
|
|
$version_history[$current_version_to_add][] = date('Y-m-d H:i:s', time()); |
659
|
|
|
// resave |
660
|
|
|
// echo "updating list of installed versions:".$this->get_activation_history_option_name();d($version_history); |
661
|
|
|
return update_option($this->get_activation_history_option_name(), $version_history); |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Gets the name of the wp option that stores the activation history |
666
|
|
|
* of this addon |
667
|
|
|
* |
668
|
|
|
* @return string |
669
|
|
|
*/ |
670
|
|
|
public function get_activation_history_option_name() |
671
|
|
|
{ |
672
|
|
|
return self::ee_addon_version_history_option_prefix . $this->name(); |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
|
676
|
|
|
/** |
677
|
|
|
* Gets the wp option which stores the activation history for this addon |
678
|
|
|
* |
679
|
|
|
* @return array |
680
|
|
|
*/ |
681
|
|
|
public function get_activation_history() |
682
|
|
|
{ |
683
|
|
|
return get_option($this->get_activation_history_option_name(), null); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* @param string $config_section |
689
|
|
|
*/ |
690
|
|
|
public function set_config_section($config_section = '') |
691
|
|
|
{ |
692
|
|
|
$this->_config_section = ! empty($config_section) ? $config_section : 'addons'; |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
/** |
696
|
|
|
* Sets the filepath to the main plugin file |
697
|
|
|
* |
698
|
|
|
* @param string $filepath |
699
|
|
|
*/ |
700
|
|
|
public function set_main_plugin_file($filepath) |
701
|
|
|
{ |
702
|
|
|
$this->_main_plugin_file = $filepath; |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
/** |
706
|
|
|
* gets the filepath to teh main file |
707
|
|
|
* |
708
|
|
|
* @return string |
709
|
|
|
*/ |
710
|
|
|
public function get_main_plugin_file() |
711
|
|
|
{ |
712
|
|
|
return $this->_main_plugin_file; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* Gets the filename (no path) of the main file (the main file loaded |
717
|
|
|
* by WP) |
718
|
|
|
* |
719
|
|
|
* @return string |
720
|
|
|
*/ |
721
|
|
|
public function get_main_plugin_file_basename() |
722
|
|
|
{ |
723
|
|
|
return plugin_basename($this->get_main_plugin_file()); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Gets the folder name which contains the main plugin file |
728
|
|
|
* |
729
|
|
|
* @return string |
730
|
|
|
*/ |
731
|
|
|
public function get_main_plugin_file_dirname() |
732
|
|
|
{ |
733
|
|
|
return dirname($this->get_main_plugin_file()); |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* sets hooks used in the admin |
739
|
|
|
* |
740
|
|
|
* @return void |
741
|
|
|
*/ |
742
|
|
|
public function admin_init() |
743
|
|
|
{ |
744
|
|
|
// is admin and not in M-Mode ? |
745
|
|
|
if (is_admin() && ! EE_Maintenance_Mode::instance()->level()) { |
746
|
|
|
add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2); |
747
|
|
|
add_filter('after_plugin_row_' . $this->_plugin_basename, array($this, 'after_plugin_row'), 10, 3); |
748
|
|
|
} |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
|
752
|
|
|
/** |
753
|
|
|
* plugin_actions |
754
|
|
|
* Add a settings link to the Plugins page, so people can go straight from the plugin page to the settings page. |
755
|
|
|
* |
756
|
|
|
* @param $links |
757
|
|
|
* @param $file |
758
|
|
|
* @return array |
759
|
|
|
*/ |
760
|
|
|
public function plugin_action_links($links, $file) |
761
|
|
|
{ |
762
|
|
|
if ($file === $this->plugin_basename() && $this->plugin_action_slug() !== '') { |
763
|
|
|
// before other links |
764
|
|
|
array_unshift( |
765
|
|
|
$links, |
766
|
|
|
'<a href="admin.php?page=' . $this->plugin_action_slug() . '">' |
767
|
|
|
. esc_html__('Settings', 'event_espresso') |
768
|
|
|
. '</a>' |
769
|
|
|
); |
770
|
|
|
} |
771
|
|
|
return $links; |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
|
775
|
|
|
/** |
776
|
|
|
* after_plugin_row |
777
|
|
|
* Add additional content to the plugins page plugin row |
778
|
|
|
* Inserts another row |
779
|
|
|
* |
780
|
|
|
* @param $plugin_file |
781
|
|
|
* @param $plugin_data |
782
|
|
|
* @param $status |
783
|
|
|
* @return void |
784
|
|
|
*/ |
785
|
|
|
public function after_plugin_row($plugin_file, $plugin_data, $status) |
786
|
|
|
{ |
787
|
|
|
$after_plugin_row = ''; |
788
|
|
|
$plugins_page_row = $this->get_plugins_page_row(); |
789
|
|
|
if (! empty($plugins_page_row) && $plugin_file === $this->plugin_basename()) { |
790
|
|
|
$class = $status ? 'active' : 'inactive'; |
791
|
|
|
$link_text = isset($plugins_page_row['link_text']) ? $plugins_page_row['link_text'] : ''; |
792
|
|
|
$link_url = isset($plugins_page_row['link_url']) ? $plugins_page_row['link_url'] : ''; |
793
|
|
|
$description = isset($plugins_page_row['description']) |
794
|
|
|
? $plugins_page_row['description'] |
795
|
|
|
: ''; |
796
|
|
|
if (! empty($link_text) && ! empty($link_url) && ! empty($description)) { |
797
|
|
|
$after_plugin_row .= '<tr id="' . sanitize_title($plugin_file) . '-ee-addon" class="' . $class . '">'; |
798
|
|
|
$after_plugin_row .= '<th class="check-column" scope="row"></th>'; |
799
|
|
|
$after_plugin_row .= '<td class="ee-addon-upsell-info-title-td plugin-title column-primary">'; |
800
|
|
|
$after_plugin_row .= '<style> |
801
|
|
|
.ee-button, |
802
|
|
|
.ee-button:active, |
803
|
|
|
.ee-button:visited { |
804
|
|
|
box-sizing: border-box; |
805
|
|
|
display: inline-block; |
806
|
|
|
position: relative; |
807
|
|
|
top: -1px; |
808
|
|
|
padding:.5em 1em; |
809
|
|
|
margin: 0; |
810
|
|
|
background: #00B1CA -webkit-linear-gradient( #4EBFDE, #00B1CA ); /* For Safari 5.1 to 6.0 */ |
811
|
|
|
background: #00B1CA -o-linear-gradient( #4EBFDE, #00B1CA ); /* For Opera 11.1 to 12.0 */ |
812
|
|
|
background: #00B1CA -moz-linear-gradient( #4EBFDE, #00B1CA ); /* For Firefox 3.6 to 15 */ |
813
|
|
|
background: #00B1CA linear-gradient( #4EBFDE, #00B1CA ); /* Standard syntax */ |
814
|
|
|
border: 1px solid rgba(0,0,0,0.1) !important; |
815
|
|
|
border-top: 1px solid rgba(255,255,255,0.5) !important; |
816
|
|
|
border-bottom: 2px solid rgba(0,0,0,0.25) !important; |
817
|
|
|
font-weight: normal; |
818
|
|
|
cursor: pointer; |
819
|
|
|
color: #fff !important; |
820
|
|
|
text-decoration: none !important; |
821
|
|
|
text-align: center; |
822
|
|
|
line-height: 1em; |
823
|
|
|
/* line-height: 1;*/ |
824
|
|
|
-moz-border-radius: 3px; |
825
|
|
|
-webkit-border-radius: 3px; |
826
|
|
|
border-radius: 3px; |
827
|
|
|
-moz-box-shadow: none; |
828
|
|
|
-webkit-box-shadow: none; |
829
|
|
|
box-shadow: none; |
830
|
|
|
} |
831
|
|
|
.ee-button:hover { |
832
|
|
|
color: #fff !important; |
833
|
|
|
background: #4EBFDE; |
834
|
|
|
} |
835
|
|
|
.ee-button:active { top:0; } |
836
|
|
|
</style>'; |
837
|
|
|
$after_plugin_row .= ' |
838
|
|
|
<p class="ee-addon-upsell-info-dv"> |
839
|
|
|
<a class="ee-button" href="' . $link_url . '">' |
840
|
|
|
. $link_text |
841
|
|
|
. ' <span class="dashicons dashicons-arrow-right-alt2" style="margin:0;"></span>' |
842
|
|
|
. '</a> |
843
|
|
|
</p>'; |
844
|
|
|
$after_plugin_row .= '</td>'; |
845
|
|
|
$after_plugin_row .= '<td class="ee-addon-upsell-info-desc-td column-description desc">'; |
846
|
|
|
$after_plugin_row .= $description; |
847
|
|
|
$after_plugin_row .= '</td>'; |
848
|
|
|
$after_plugin_row .= '</tr>'; |
849
|
|
|
} else { |
850
|
|
|
$after_plugin_row .= $description; |
851
|
|
|
} |
852
|
|
|
} |
853
|
|
|
|
854
|
|
|
echo $after_plugin_row; |
855
|
|
|
} |
856
|
|
|
|
857
|
|
|
|
858
|
|
|
/** |
859
|
|
|
* A safe space for addons to add additional logic like setting hooks that need to be set early in the request. |
860
|
|
|
* Child classes that have logic like that to run can override this method declaration. This was not made abstract |
861
|
|
|
* for back compat reasons. |
862
|
|
|
* |
863
|
|
|
* This will fire on the `AHEE__EE_System__load_espresso_addons__complete` hook at priority 999. |
864
|
|
|
* |
865
|
|
|
* It is recommended, if client code is `de-registering` an add-on, then do it on the |
866
|
|
|
* `AHEE__EE_System__load_espresso_addons__complete` hook before priority 999 so as to ensure any code logic in this |
867
|
|
|
* callback does not get run/set in that request. |
868
|
|
|
* |
869
|
|
|
* Also, keep in mind that if a registered add-on happens to be deactivated via |
870
|
|
|
* EE_System::_deactivate_incompatible_addons() because its incompatible, any code executed in this method |
871
|
|
|
* (including setting hooks etc) will have executed before the plugin was deactivated. If you use |
872
|
|
|
* `after_registration` to set any filter and/or action hooks and want to ensure they are removed on this add-on's |
873
|
|
|
* deactivation, you can override `EE_Addon::deactivation` and unset your hooks and filters there. Just remember |
874
|
|
|
* to call `parent::deactivation`. |
875
|
|
|
* |
876
|
|
|
* @since 4.9.26 |
877
|
|
|
*/ |
878
|
|
|
public function after_registration() |
879
|
|
|
{ |
880
|
|
|
// cricket chirp... cricket chirp... |
881
|
|
|
} |
882
|
|
|
} |
883
|
|
|
|