1
|
|
|
<?php |
2
|
|
|
namespace EventEspresso\core\domain\services\helpers; |
3
|
|
|
|
4
|
|
|
use EE_Error; |
5
|
|
|
use EE_Registry; |
6
|
|
|
use EES_Shortcode; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use WP; |
9
|
|
|
|
10
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Shortcodes |
16
|
|
|
* Description |
17
|
|
|
* |
18
|
|
|
* @package Event Espresso |
19
|
|
|
* @author Brent Christensen |
20
|
|
|
* @since $VID:$ |
21
|
|
|
*/ |
22
|
|
|
class ShortcodeHelper |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @type EE_Registry $registry |
27
|
|
|
* @access protected |
28
|
|
|
*/ |
29
|
|
|
protected $registry; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Shortcodes constructor. |
35
|
|
|
* |
36
|
|
|
* @param EE_Registry $registry |
37
|
|
|
*/ |
38
|
|
|
public function __construct(EE_Registry $registry) |
39
|
|
|
{ |
40
|
|
|
$this->registry = $registry; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* get classname, remove EES_prefix, and convert to UPPERCASE |
47
|
|
|
* |
48
|
|
|
* @param string $class_name |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public static function generateShortcodeTagFromClassName($class_name) |
52
|
|
|
{ |
53
|
|
|
return strtoupper(str_replace('EES_', '', $class_name)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* add EES_prefix and Capitalize words |
60
|
|
|
* |
61
|
|
|
* @param string $tag |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public static function generateShortcodeClassNameFromTag($tag) |
65
|
|
|
{ |
66
|
|
|
// order of operation runs from inside to out |
67
|
|
|
// 5) maybe add prefix |
68
|
|
|
return ShortcodeHelper::addShortcodeClassPrefix( |
69
|
|
|
// 4) find spaces, replace with underscores |
70
|
|
|
str_replace( |
71
|
|
|
' ', |
72
|
|
|
'_', |
73
|
|
|
// 3) capitalize first letter of each word |
74
|
|
|
ucwords( |
75
|
|
|
// 2) also change to lowercase so ucwords() will work |
76
|
|
|
strtolower( |
77
|
|
|
// 1) find underscores, replace with spaces so ucwords() will work |
78
|
|
|
str_replace( |
79
|
|
|
'_', |
80
|
|
|
' ', |
81
|
|
|
$tag |
82
|
|
|
) |
83
|
|
|
) |
84
|
|
|
) |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* maybe add EES_prefix |
93
|
|
|
* |
94
|
|
|
* @param string $class_name |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public static function addShortcodeClassPrefix($class_name) |
98
|
|
|
{ |
99
|
|
|
return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_' . $class_name; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function getEspressoShortcodeTags() |
108
|
|
|
{ |
109
|
|
|
static $shortcode_tags = array(); |
110
|
|
|
if (empty($shortcode_tags)) { |
111
|
|
|
$shortcode_tags = array_keys((array)$this->registry->shortcodes); |
112
|
|
|
} |
113
|
|
|
return $shortcode_tags; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $content |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function doShortcode($content) |
123
|
|
|
{ |
124
|
|
|
foreach ($this->getEspressoShortcodeTags() as $shortcode_tag) { |
125
|
|
|
if (strpos($content, $shortcode_tag) !== false) { |
126
|
|
|
$shortcode_class = ShortcodeHelper::generateShortcodeClassNameFromTag($shortcode_tag); |
127
|
|
|
$this->initializeShortcode($shortcode_class); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
return do_shortcode($content); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* given a shortcode name, will instantiate the shortcode and call it's run() method |
137
|
|
|
* |
138
|
|
|
* @param string $shortcode_class |
139
|
|
|
* @param WP $wp |
140
|
|
|
*/ |
141
|
|
|
public function initializeShortcode($shortcode_class = '', WP $wp = null) |
142
|
|
|
{ |
143
|
|
|
// don't do anything if shortcode is already initialized |
144
|
|
|
if ($this->registry->shortcodes->{$shortcode_class} instanceof EES_Shortcode){ |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
// let's pause to reflect on this... |
148
|
|
|
$sc_reflector = new ReflectionClass(ShortcodeHelper::addShortcodeClassPrefix($shortcode_class)); |
149
|
|
|
// ensure that class is actually a shortcode |
150
|
|
|
if ( |
151
|
|
|
defined('WP_DEBUG') |
152
|
|
|
&& WP_DEBUG === true |
153
|
|
|
&& ! $sc_reflector->isSubclassOf('EES_Shortcode') |
154
|
|
|
) { |
155
|
|
|
EE_Error::add_error( |
156
|
|
|
sprintf( |
157
|
|
|
__( |
158
|
|
|
'The requested %s shortcode is not of the class "EES_Shortcode". Please check your files.', |
159
|
|
|
'event_espresso' |
160
|
|
|
), |
161
|
|
|
$shortcode_class |
162
|
|
|
), |
163
|
|
|
__FILE__, |
164
|
|
|
__FUNCTION__, |
165
|
|
|
__LINE__ |
166
|
|
|
); |
167
|
|
|
add_filter('FHEE_run_EE_the_content', '__return_true'); |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
global $wp; |
171
|
|
|
// and pass the request object to the run method |
172
|
|
|
$this->registry->shortcodes->{$shortcode_class} = $sc_reflector->newInstance(); |
173
|
|
|
// fire the shortcode class's run method, so that it can activate resources |
174
|
|
|
$this->registry->shortcodes->{$shortcode_class}->run($wp); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
// End of file Shortcodes.php |
179
|
|
|
// Location: EventEspresso\core\domain\services\helpers/Shortcodes.php |