-
Notifications
You must be signed in to change notification settings - Fork 2
/
EE_Promotions_Config.php
148 lines (128 loc) · 4.23 KB
/
EE_Promotions_Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
use EventEspresso\core\services\loaders\LoaderFactory;
/**
* EE_Promotions_Config
* Class defining the Promotions Config object stored on EE_Registry::instance->CFG
*
* @since 1.0.0
* @package EE4 Promotions
* @subpackage config
* @author Darren Ethier
*/
class EE_Promotions_Config extends EE_Config_Base
{
/**
* Holds all the EE_Promotion_Scope objects that are registered for promotions.
*
* @since 1.0.0
* @type EE_Promotion_Scope[]|null $scopes
*/
public ?array $scopes = [];
/**
* what to call promo codes on the frontend. ie: Promo codes, coupon codes, etc
*
* @since 1.0.0
* @type stdClass|null $label
*/
public ?stdClass $label = null;
public ?string $banner_template = 'promo-banner-ribbon.template.php';
public ?string $ribbon_banner_color = 'lite-blue';
protected ?bool $_affects_tax = false;
public function __construct()
{
$this->label = new stdClass();
$this->label->singular = esc_html__('Promotion Code', 'event_espresso');
$this->label->plural = esc_html__('Promotion Codes', 'event_espresso');
add_action('AHEE__EE_Config___load_core_config__end', [$this, 'init'], 99);
}
/**
* init
*
* @return void
*/
public function init()
{
static $initialized = false;
if ($initialized) {
return;
}
$initialized = true;
$this->scopes = $this->_get_scopes();
$this->label = new stdClass();
$this->label->singular = apply_filters(
'FHEE__EE_Promotions_Config____construct__label_singular',
esc_html__('Promotion Code', 'event_espresso')
);
$this->label->plural = apply_filters(
'FHEE__EE_Promotions_Config____construct__label_plural',
esc_html__('Promotion Codes', 'event_espresso')
);
}
/**
* @return array
*/
private function _get_scopes(): array
{
static $scopes = [];
$scopes_to_register = apply_filters(
'FHEE__EE_Promotions_Config___get_scopes__scopes_to_register',
glob(EE_PROMOTIONS_PATH . 'lib/scopes/*.lib.php')
);
foreach ($scopes_to_register as $scope) {
$class_name = EEH_File::get_classname_from_filepath_with_standard_filename($scope);
// if parent let's skip - it's already been required.
if ($class_name == 'EE_Promotion_Scope') {
continue;
}
$loaded = require_once($scope);
// avoid instantiating classes twice by checking whether file has already been loaded
// ( first load returns (int)1, subsequent loads return (bool)true )
if ($loaded === 1) {
if (class_exists($class_name)) {
$promotion_scope = LoaderFactory::getShared($class_name);
if (! $promotion_scope instanceof EE_Promotion_Scope) {
throw new DomainException(
sprintf(
esc_html__('Invalid or missing promotion_scope class: %1$s', 'event_espresso'),
$class_name
)
);
}
$scopes[ $promotion_scope->slug ] = $promotion_scope;
}
}
}
return $scopes;
}
public function __sleep(): array
{
// remove 'scopes' from array of class properties via array_filter()
return array_filter(
array_keys((array) $this),
function ($prop) {
return $prop !== 'scopes';
}
);
}
public function __wakeup()
{
$this->scopes = $this->_get_scopes();
}
/**
* @return bool
*/
public function affects_tax(): bool
{
return apply_filters(
'FHEE__EE_Promotions_Config__affects_tax',
$this->_affects_tax
);
}
/**
* @param bool|int|string|null $affects_tax
*/
public function set_affects_tax($affects_tax)
{
$this->_affects_tax = filter_var($affects_tax, FILTER_VALIDATE_BOOLEAN);
}
}