-
Notifications
You must be signed in to change notification settings - Fork 2
/
EE_Promotions.class.php
261 lines (248 loc) · 11.1 KB
/
EE_Promotions.class.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* Class EE_Promotions
*
* @package Event Espresso
* @subpackage espresso-promotions
* @author Brent Christensen
* @version 1.0.0
*/
class EE_Promotions extends EE_Addon
{
/**
* register_addon
*
* @throws EE_Error
*/
public static function register_addon()
{
if (! defined('EE_PROMOTIONS_BASENAME')) {
// define the plugin directory path and URL
define('EE_PROMOTIONS_BASENAME', plugin_basename(EE_PROMOTIONS_PLUGIN_FILE));
define('EE_PROMOTIONS_PATH', plugin_dir_path(__FILE__));
define('EE_PROMOTIONS_URL', plugin_dir_url(__FILE__));
define('EE_PROMOTIONS_ADMIN', EE_PROMOTIONS_PATH . 'admin' . DS . 'promotions' . DS);
define('EE_PROMOTIONS_CORE', EE_PROMOTIONS_PATH . 'core' . DS);
}
// register addon via Plugin API
EE_Register_Addon::register(
'Promotions',
[
'version' => EE_PROMOTIONS_VERSION,
'min_core_version' => EE_PROMOTIONS_CORE_VERSION_REQUIRED,
'main_file_path' => EE_PROMOTIONS_PLUGIN_FILE,
'admin_path' => EE_PROMOTIONS_ADMIN,
'plugin_slug' => 'espresso_promotions',
'config_class' => 'EE_Promotions_Config',
'config_name' => 'promotions',
'namespace' => [
'FQNS' => 'EventEspresso\Promotions',
'DIR' => __DIR__,
],
// 'plugins_page_row' => EE_Promotions::plugins_page_row(),
'dms_paths' => [EE_PROMOTIONS_CORE . 'data_migration_scripts' . DS],
'module_paths' => [EE_PROMOTIONS_PATH . 'EED_Promotions.module.php'],
'shortcode_paths' => [EE_PROMOTIONS_PATH . 'EES_Espresso_Promotions.shortcode.php'],
'widget_paths' => [EE_PROMOTIONS_PATH . 'EEW_Promotions.widget.php'],
// register autoloaders
'autoloader_paths' => [
'EE_Promotions_Config' => EE_PROMOTIONS_PATH . 'EE_Promotions_Config.php',
'Promotions_Admin_Page_Init' => EE_PROMOTIONS_ADMIN . 'Promotions_Admin_Page_Init.core.php',
'Promotions_Admin_Page' => EE_PROMOTIONS_ADMIN . 'Promotions_Admin_Page.core.php',
'Promotions_Admin_List_Table' => EE_PROMOTIONS_ADMIN . 'Promotions_Admin_List_Table.class.php',
'EE_Promotion_Scope'
=> EE_PROMOTIONS_PATH . 'lib' . DS . 'scopes' . DS . 'EE_Promotion_Scope.lib.php',
],
'autoloader_folders' => [
'Promotions_Plugin_API' => EE_PROMOTIONS_PATH . 'lib' . DS . 'plugin_api',
],
'pue_options' => [
'pue_plugin_slug' => 'eea-promotions',
'checkPeriod' => '24',
'use_wp_update' => false,
],
// EE_Register_Model
'model_paths' => [EE_PROMOTIONS_CORE . 'db_models'],
'class_paths' => [EE_PROMOTIONS_CORE . 'db_classes'],
// EE_Register_Model_Extensions
'model_extension_paths' => [EE_PROMOTIONS_CORE . 'db_model_extensions' . DS],
'class_extension_paths' => [EE_PROMOTIONS_CORE . 'db_class_extensions' . DS],
'capabilities' => [
'administrator' => [
'ee_read_promotion',
'ee_read_promotions',
'ee_read_others_promotions',
'ee_edit_promotion',
'ee_edit_promotions',
'ee_edit_others_promotions',
'ee_delete_promotion',
'ee_delete_promotions',
'ee_delete_others_promotions',
],
],
'capability_maps' => [
0 => [
'EE_Meta_Capability_Map_Read' => [
'ee_read_promotion',
['Promotion', '', 'ee_read_others_promotions', ''],
],
],
1 => [
'EE_Meta_Capability_Map_Edit' => [
'ee_edit_promotion',
['Promotion', '', 'ee_edit_others_promotions', ''],
],
],
2 => [
'EE_Meta_Capability_Map_Delete' => [
'ee_delete_promotion',
['Promotion', '', 'ee_delete_others_promotions', ''],
],
],
],
]
);
}
/**
* a safe space for addons to add additional logic like setting hooks
* that will run immediately after addon registration
* making this a great place for code that needs to be "omnipresent"
*/
public function after_registration()
{
// register promotion specific statuses
add_filter(
'FHEE__EEM_Status__localized_status__translation_array',
['EE_Promotions', 'promotion_stati']
);
// add promotion codes shortcode to messages
add_filter('FHEE__EE_Shortcodes__shortcodes', ['EE_Promotions', 'register_new_shortcodes'], 10, 2);
add_filter('FHEE__EE_Shortcodes__parser_after', ['EE_Promotions', 'register_new_shortcode_parser'], 10, 5);
}
/**
* plugins_page_row
* HTML to appear within a new table row on the WP Plugins page, below the promotions plugin row
*
* @return array
*/
public static function plugins_page_row()
{
return [
'link_text' => 'Promotions Addon Upsell Info',
'link_url' => '#',
'description' => 'To edit me, open up ' . __FILE__ . ' and find the ' . __METHOD__ . '() method',
];
}
/**
* This registers the localization for the promotion statuses with the EEM_Status
* translation array
*
* @param array $stati_translation Current localized stati
* @return array Current stati with promotion stati appended.
*/
public static function promotion_stati(array $stati_translation): array
{
$promotion_stati = [
EE_Promotion::upcoming => [
esc_html__('upcoming', 'event_espresso'),
esc_html__('upcoming', 'event_espresso'),
],
EE_Promotion::active => [
esc_html__('active', 'event_espresso'),
esc_html__('active', 'event_espresso'),
],
EE_Promotion::expired => [
esc_html__('expired', 'event_espresso'),
esc_html__('expired', 'event_espresso'),
],
EE_Promotion::unavailable => [
esc_html__('unavailable', 'event_espresso'),
esc_html__('unavailable', 'event_espresso'),
],
];
return array_merge($stati_translation, $promotion_stati);
}
/**
* Callback for FHEE__EE_Shortcodes__shortcodes
*
* @param array $shortcodes The existing shortcodes in this library
* @param EE_Shortcodes $lib
* @return array new array of shortcodes
* @since 1.0.0
*/
public static function register_new_shortcodes(array $shortcodes, EE_Shortcodes $lib): array
{
// Check we have the EE_Transaction_Shortcodes library
if ($lib instanceof EE_Transaction_Shortcodes) {
// Add shortcode to the shortcodes array.
$shortcodes['[PROMOTIONS_USED]'] = esc_html__(
'This shortcode outputs all promotions used on the registration.',
'event_espresso'
);
}
// Return the shortcodes.
return $shortcodes;
}
/**
* Call back for the FHEE__EE_Shortcodes__parser_after filter.
* This contains the logic for parsing the new shortcodes introduced by this addon.
*
* @param string $parsed The current parsed template string.
* @param string $shortcode The incoming shortcode being setup for parsing.
* @param array|object $data Depending on the shortcode parser the filter is called in, this will represent
* either an array of data objects or a specific data object.
* @param array|object $extra_data Depending on the shortcode parser the filter is called in, this will either
* represent an array with an array of templates being parsed, and a
* EE_Addressee_Data object OR just an EE_Addresee_Data object.
* @param EE_Shortcodes $lib
* @return string The parsed string
* @throws EE_Error
* @throws ReflectionException
* @since 1.0.0
*/
public static function register_new_shortcode_parser(
string $parsed,
string $shortcode,
$data,
$extra_data,
EE_Shortcodes $lib
): string {
// Check we have the EE_Transaction_Shortcodes and our the shortcode matches
if ($lib instanceof EE_Transaction_Shortcodes && $shortcode == '[PROMOTIONS_USED]') {
// Pull the transaction from the EE_Messages_Addressee object passed to parser.
$transaction = $data instanceof EE_Messages_Addressee ? $data->txn : null;
// Check we have an EE_Transaction object
if ($transaction instanceof EE_Transaction) {
// Pull in the promotion line items for this transaction
$promo_rows = EEM_Price::instance()->get_all_wpdb_results(
[
[
'Promotion.Line_Item.TXN_ID' => $transaction->ID(),
],
]
);
// Setup an arrey to store all promo codes used on the transaction
$promo_codes = [];
// Loop through promo line items and build the promo_codes array using the Promocode name and code
// (if available)
foreach ($promo_rows as $promo_row) {
if ($promo_row['Promotion.PRO_code']) {
$promo_codes[] = sprintf(
'%1$s [%2$s]',
$promo_row['Price.PRC_name'],
$promo_row['Promotion.PRO_code']
);
} else {
$promo_codes[] = $promo_row['Price.PRC_name'];
}
}
// Implode the promo_codes array into a comma-delimited string.
// Return a single string or promo codes used.
return implode(', ', $promo_codes);
}
}
// If not within the correct section, or parsing the correct shortcode,
// return the currently parsed content.
return $parsed;
}
}