|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\services\shortcodes; |
|
3
|
|
|
|
|
4
|
|
|
use DomainException; |
|
5
|
|
|
use EventEspresso\core\domain\EnqueueAssetsInterface; |
|
6
|
|
|
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay; |
|
7
|
|
|
use EventEspresso\core\exceptions\InvalidClassException; |
|
8
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
9
|
|
|
use EventEspresso\core\exceptions\InvalidEntityException; |
|
10
|
|
|
use EventEspresso\core\exceptions\InvalidFilePathException; |
|
11
|
|
|
use EventEspresso\core\exceptions\InvalidIdentifierException; |
|
12
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
13
|
|
|
use EventEspresso\core\services\collections\CollectionDetails; |
|
14
|
|
|
use EventEspresso\core\services\collections\CollectionInterface; |
|
15
|
|
|
use EventEspresso\core\services\collections\CollectionLoader; |
|
16
|
|
|
use Exception; |
|
17
|
|
|
|
|
18
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ShortcodesManager |
|
24
|
|
|
* Loads a Collection of ShortcodeInterface classes |
|
25
|
|
|
* then retrieves shortcode tags and calls add_shortcode() for each |
|
26
|
|
|
* ensures assets are registered and enqueued at the appropriate time |
|
27
|
|
|
* |
|
28
|
|
|
* @package Event Espresso |
|
29
|
|
|
* @author Brent Christensen |
|
30
|
|
|
* @since 4.9.26 |
|
31
|
|
|
*/ |
|
32
|
|
|
class ShortcodesManager |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var LegacyShortcodesManager $legacy_shortcodes_manager |
|
37
|
|
|
*/ |
|
38
|
|
|
private $legacy_shortcodes_manager; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ShortcodeInterface[] $shortcodes |
|
42
|
|
|
*/ |
|
43
|
|
|
private $shortcodes; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* ShortcodesManager constructor |
|
49
|
|
|
* |
|
50
|
|
|
* @param LegacyShortcodesManager $legacy_shortcodes_manager |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(LegacyShortcodesManager $legacy_shortcodes_manager) { |
|
53
|
|
|
$this->legacy_shortcodes_manager = $legacy_shortcodes_manager; |
|
54
|
|
|
// assemble a list of installed and active shortcodes |
|
55
|
|
|
add_action( |
|
56
|
|
|
'AHEE__EE_System__register_shortcodes_modules_and_widgets', |
|
57
|
|
|
array($this, 'registerShortcodes'), |
|
58
|
|
|
999 |
|
59
|
|
|
); |
|
60
|
|
|
// call add_shortcode() for all installed shortcodes |
|
61
|
|
|
add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'addShortcodes')); |
|
62
|
|
|
// check content for shortcodes the old way |
|
63
|
|
|
add_action('parse_query', array($this->legacy_shortcodes_manager, 'initializeShortcodes'), 5); |
|
64
|
|
|
// check content for shortcodes the NEW more efficient way |
|
65
|
|
|
add_action('template_redirect', array($this, 'templateRedirect'), 999); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return CollectionInterface|ShortcodeInterface[] |
|
72
|
|
|
* @throws InvalidIdentifierException |
|
73
|
|
|
* @throws InvalidInterfaceException |
|
74
|
|
|
* @throws InvalidFilePathException |
|
75
|
|
|
* @throws InvalidEntityException |
|
76
|
|
|
* @throws InvalidDataTypeException |
|
77
|
|
|
* @throws InvalidClassException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getShortcodes() |
|
80
|
|
|
{ |
|
81
|
|
|
if ( ! $this->shortcodes instanceof CollectionInterface) { |
|
82
|
|
|
$this->shortcodes = $this->loadShortcodesCollection(); |
|
83
|
|
|
} |
|
84
|
|
|
return $this->shortcodes; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return CollectionInterface|ShortcodeInterface[] |
|
91
|
|
|
* @throws InvalidIdentifierException |
|
92
|
|
|
* @throws InvalidInterfaceException |
|
93
|
|
|
* @throws InvalidFilePathException |
|
94
|
|
|
* @throws InvalidEntityException |
|
95
|
|
|
* @throws InvalidDataTypeException |
|
96
|
|
|
* @throws InvalidClassException |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function loadShortcodesCollection() |
|
99
|
|
|
{ |
|
100
|
|
|
$loader = new CollectionLoader( |
|
101
|
|
|
new CollectionDetails( |
|
102
|
|
|
// collection name |
|
103
|
|
|
'shortcodes', |
|
104
|
|
|
// collection interface |
|
105
|
|
|
'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
106
|
|
|
// FQCNs for classes to add (all classes within that namespace will be loaded) |
|
107
|
|
|
array('EventEspresso\core\domain\entities\shortcodes'), |
|
108
|
|
|
// filepaths to classes to add |
|
109
|
|
|
array(), |
|
110
|
|
|
// file mask to use if parsing folder for files to add |
|
111
|
|
|
'', |
|
112
|
|
|
// what to use as identifier for collection entities |
|
113
|
|
|
// using CLASS NAME prevents duplicates (works like a singleton) |
|
114
|
|
|
CollectionDetails::ID_CLASS_NAME |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
return $loader->getCollection(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return void |
|
124
|
|
|
* @throws DomainException |
|
125
|
|
|
* @throws InvalidInterfaceException |
|
126
|
|
|
* @throws InvalidIdentifierException |
|
127
|
|
|
* @throws InvalidFilePathException |
|
128
|
|
|
* @throws InvalidEntityException |
|
129
|
|
|
* @throws InvalidDataTypeException |
|
130
|
|
|
* @throws InvalidClassException |
|
131
|
|
|
*/ |
|
132
|
|
|
public function registerShortcodes() |
|
133
|
|
|
{ |
|
134
|
|
|
try { |
|
135
|
|
|
$this->shortcodes = apply_filters( |
|
136
|
|
|
'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
137
|
|
|
$this->getShortcodes() |
|
138
|
|
|
); |
|
139
|
|
View Code Duplication |
if (! $this->shortcodes instanceof CollectionInterface) { |
|
140
|
|
|
throw new InvalidEntityException( |
|
141
|
|
|
$this->shortcodes, |
|
142
|
|
|
'CollectionInterface', |
|
143
|
|
|
sprintf( |
|
144
|
|
|
esc_html__( |
|
145
|
|
|
'The "FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection" filter must return a Collection of EspressoShortcode objects. "%1$s" was received instead.', |
|
146
|
|
|
'event_espresso' |
|
147
|
|
|
), |
|
148
|
|
|
is_object($this->shortcodes) ? get_class($this->shortcodes) : gettype($this->shortcodes) |
|
149
|
|
|
) |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
$this->legacy_shortcodes_manager->registerShortcodes(); |
|
153
|
|
|
} catch (Exception $exception) { |
|
154
|
|
|
new ExceptionStackTraceDisplay($exception); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
|
|
public function addShortcodes() |
|
164
|
|
|
{ |
|
165
|
|
|
try { |
|
166
|
|
|
// cycle thru shortcode folders |
|
167
|
|
|
foreach ($this->shortcodes as $shortcode) { |
|
168
|
|
|
/** @var ShortcodeInterface $shortcode */ |
|
169
|
|
|
if ( $shortcode instanceof EnqueueAssetsInterface) { |
|
170
|
|
|
add_action('wp_enqueue_scripts', array($shortcode, 'registerScriptsAndStylesheets'), 10); |
|
171
|
|
|
add_action('wp_enqueue_scripts', array($shortcode, 'enqueueStylesheets'), 11); |
|
172
|
|
|
} |
|
173
|
|
|
// add_shortcode() if it has not already been added |
|
174
|
|
|
if ( ! shortcode_exists($shortcode->getTag())) { |
|
175
|
|
|
add_shortcode($shortcode->getTag(), array($shortcode, 'processShortcodeCallback')); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
$this->legacy_shortcodes_manager->addShortcodes(); |
|
179
|
|
|
} catch (Exception $exception) { |
|
180
|
|
|
new ExceptionStackTraceDisplay($exception); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* callback for the "template_redirect" hook point |
|
188
|
|
|
* checks posts for EE shortcodes, and initializes them, |
|
189
|
|
|
* then toggles filter switch that loads core default assets |
|
190
|
|
|
* |
|
191
|
|
|
* @return void |
|
192
|
|
|
*/ |
|
193
|
|
|
public function templateRedirect() |
|
194
|
|
|
{ |
|
195
|
|
|
global $wp_query; |
|
196
|
|
|
if (empty($wp_query->posts)) { |
|
197
|
|
|
return; |
|
198
|
|
|
} |
|
199
|
|
|
$load_assets = false; |
|
200
|
|
|
// array of posts displayed in current request |
|
201
|
|
|
$posts = is_array($wp_query->posts) ? $wp_query->posts : array($wp_query->posts); |
|
202
|
|
|
foreach ($posts as $post) { |
|
203
|
|
|
// now check post content and excerpt for EE shortcodes |
|
204
|
|
|
$load_assets = $this->parseContentForShortcodes($post->post_content) |
|
205
|
|
|
? true |
|
206
|
|
|
: $load_assets; |
|
207
|
|
|
} |
|
208
|
|
View Code Duplication |
if ($load_assets) { |
|
209
|
|
|
$this->legacy_shortcodes_manager->registry()->REQ->set_espresso_page(true); |
|
210
|
|
|
add_filter('FHEE_load_css', '__return_true'); |
|
211
|
|
|
add_filter('FHEE_load_js', '__return_true'); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* checks supplied content against list of shortcodes, |
|
219
|
|
|
* then initializes any found shortcodes, and returns true. |
|
220
|
|
|
* returns false if no shortcodes found. |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $content |
|
223
|
|
|
* @return bool |
|
224
|
|
|
*/ |
|
225
|
|
|
public function parseContentForShortcodes($content) |
|
226
|
|
|
{ |
|
227
|
|
|
$has_shortcode = false; |
|
228
|
|
|
if(empty($this->shortcodes)){ |
|
229
|
|
|
return $has_shortcode; |
|
230
|
|
|
} |
|
231
|
|
|
foreach ($this->shortcodes as $shortcode) { |
|
232
|
|
|
/** @var ShortcodeInterface $shortcode */ |
|
233
|
|
|
if (has_shortcode($content, $shortcode->getTag())) { |
|
234
|
|
|
$shortcode->initializeShortcode(); |
|
235
|
|
|
$has_shortcode = true; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
return $has_shortcode; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
// End of file ShortcodesManager.php |
|
243
|
|
|
// Location: EventEspresso\core\services\shortcodes/ShortcodesManager.php |
|
244
|
|
|
|