@@ -14,41 +14,41 @@ |
||
14 | 14 | interface CacheManagerInterface |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * returns a string that will be prepended to all cache identifiers |
|
19 | - * |
|
20 | - * @return string |
|
21 | - */ |
|
22 | - public function cachePrefix(); |
|
23 | - |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * @param string $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types. |
|
28 | - * May also be helpful to include an additional specific identifier, |
|
29 | - * such as a post ID as part of the $id_prefix so that individual caches |
|
30 | - * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
31 | - * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
32 | - * @param string $cache_id [required] Additional identifying details that make this cache unique. |
|
33 | - * It is advisable to use some of the actual data |
|
34 | - * that is used to generate the content being cached, |
|
35 | - * in order to guarantee that the cache id is unique for that content. |
|
36 | - * The cache id will be md5'd before usage to make it more db friendly, |
|
37 | - * and the entire cache id string will be truncated to 190 characters. |
|
38 | - * @param Closure $callback [required] since the point of caching is to avoid generating content when not |
|
39 | - * necessary, |
|
40 | - * we wrap our content creation in a Closure so that it is not executed until needed. |
|
41 | - * @param int $expiration |
|
42 | - * @return Closure|mixed |
|
43 | - */ |
|
44 | - public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS); |
|
45 | - |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * @param array|string $cache_id [required] Could be an ID prefix affecting many caches |
|
50 | - * or a specific ID targeting a single cache item |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function clear($cache_id); |
|
17 | + /** |
|
18 | + * returns a string that will be prepended to all cache identifiers |
|
19 | + * |
|
20 | + * @return string |
|
21 | + */ |
|
22 | + public function cachePrefix(); |
|
23 | + |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * @param string $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types. |
|
28 | + * May also be helpful to include an additional specific identifier, |
|
29 | + * such as a post ID as part of the $id_prefix so that individual caches |
|
30 | + * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
31 | + * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
32 | + * @param string $cache_id [required] Additional identifying details that make this cache unique. |
|
33 | + * It is advisable to use some of the actual data |
|
34 | + * that is used to generate the content being cached, |
|
35 | + * in order to guarantee that the cache id is unique for that content. |
|
36 | + * The cache id will be md5'd before usage to make it more db friendly, |
|
37 | + * and the entire cache id string will be truncated to 190 characters. |
|
38 | + * @param Closure $callback [required] since the point of caching is to avoid generating content when not |
|
39 | + * necessary, |
|
40 | + * we wrap our content creation in a Closure so that it is not executed until needed. |
|
41 | + * @param int $expiration |
|
42 | + * @return Closure|mixed |
|
43 | + */ |
|
44 | + public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS); |
|
45 | + |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * @param array|string $cache_id [required] Could be an ID prefix affecting many caches |
|
50 | + * or a specific ID targeting a single cache item |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function clear($cache_id); |
|
54 | 54 | } |
@@ -11,50 +11,50 @@ |
||
11 | 11 | interface CacheStorageInterface |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * Saves supplied data |
|
16 | - * if an expiration is set, then it automatically schedules the cache for deletion |
|
17 | - * |
|
18 | - * @param string $key [required] |
|
19 | - * @param string $data [required] |
|
20 | - * @param int $expiration number of seconds until the cache expires |
|
21 | - * @return bool |
|
22 | - */ |
|
23 | - public function add($key, $data, $expiration = 0); |
|
24 | - |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * retrieves cache data |
|
29 | - * should automatically trigger early cache refresh for standard cache items |
|
30 | - * in order to avoid cache stampedes on busy sites. |
|
31 | - * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
32 | - * the $standard_cache parameter should be set to false when retrieving data |
|
33 | - * |
|
34 | - * @param string $key [required] |
|
35 | - * @param bool $standard_cache |
|
36 | - * @return mixed |
|
37 | - */ |
|
38 | - public function get($key, $standard_cache = true); |
|
39 | - |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * delete a single cached item |
|
44 | - * |
|
45 | - * @param string $key [required] full or partial cache key to be deleted |
|
46 | - */ |
|
47 | - public function delete($key); |
|
48 | - |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * delete multiple cached items |
|
53 | - * |
|
54 | - * @param array $keys [required] array of full or partial cache keys to be deleted |
|
55 | - * @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
|
56 | - * and proceed directly to deleting those entries from the cache storage |
|
57 | - * @return |
|
58 | - */ |
|
59 | - public function deleteMany(array $keys, $force_delete = false); |
|
14 | + /** |
|
15 | + * Saves supplied data |
|
16 | + * if an expiration is set, then it automatically schedules the cache for deletion |
|
17 | + * |
|
18 | + * @param string $key [required] |
|
19 | + * @param string $data [required] |
|
20 | + * @param int $expiration number of seconds until the cache expires |
|
21 | + * @return bool |
|
22 | + */ |
|
23 | + public function add($key, $data, $expiration = 0); |
|
24 | + |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * retrieves cache data |
|
29 | + * should automatically trigger early cache refresh for standard cache items |
|
30 | + * in order to avoid cache stampedes on busy sites. |
|
31 | + * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
32 | + * the $standard_cache parameter should be set to false when retrieving data |
|
33 | + * |
|
34 | + * @param string $key [required] |
|
35 | + * @param bool $standard_cache |
|
36 | + * @return mixed |
|
37 | + */ |
|
38 | + public function get($key, $standard_cache = true); |
|
39 | + |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * delete a single cached item |
|
44 | + * |
|
45 | + * @param string $key [required] full or partial cache key to be deleted |
|
46 | + */ |
|
47 | + public function delete($key); |
|
48 | + |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * delete multiple cached items |
|
53 | + * |
|
54 | + * @param array $keys [required] array of full or partial cache keys to be deleted |
|
55 | + * @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
|
56 | + * and proceed directly to deleting those entries from the cache storage |
|
57 | + * @return |
|
58 | + */ |
|
59 | + public function deleteMany(array $keys, $force_delete = false); |
|
60 | 60 | } |
@@ -12,21 +12,21 @@ |
||
12 | 12 | interface FormatterInterface |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * Applies the formatting to all items in the array |
|
17 | - * |
|
18 | - * @param array $input accepts a multi-dimensional array, but all "leaf nodes" are easily cast to a string |
|
19 | - * @return array |
|
20 | - */ |
|
21 | - public function formatArray($input); |
|
15 | + /** |
|
16 | + * Applies the formatting to all items in the array |
|
17 | + * |
|
18 | + * @param array $input accepts a multi-dimensional array, but all "leaf nodes" are easily cast to a string |
|
19 | + * @return array |
|
20 | + */ |
|
21 | + public function formatArray($input); |
|
22 | 22 | |
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * Formats the string |
|
27 | - * |
|
28 | - * @param string|int|float $input anything easily cast into a string |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function format($input); |
|
25 | + /** |
|
26 | + * Formats the string |
|
27 | + * |
|
28 | + * @param string|int|float $input anything easily cast into a string |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function format($input); |
|
32 | 32 | } |
@@ -4,50 +4,50 @@ |
||
4 | 4 | interface ShortcodeInterface |
5 | 5 | { |
6 | 6 | |
7 | - /** |
|
8 | - * the actual shortcode tag that gets registered with WordPress |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - public function getTag(); |
|
13 | - |
|
14 | - /** |
|
15 | - * the length of time in seconds to cache the results of the processShortcode() method |
|
16 | - * 0 means the processShortcode() results will NOT be cached at all |
|
17 | - * |
|
18 | - * @return int |
|
19 | - */ |
|
20 | - public function cacheExpiration(); |
|
21 | - |
|
22 | - /** |
|
23 | - * a place for adding any initialization code that needs to run prior to wp_header(). |
|
24 | - * this may be required for shortcodes that utilize a corresponding module, |
|
25 | - * and need to enqueue assets for that module |
|
26 | - * |
|
27 | - * !!! IMPORTANT !!! |
|
28 | - * After performing any logic within this method required for initialization |
|
29 | - * $this->shortcodeHasBeenInitialized(); |
|
30 | - * should be called to ensure that the shortcode is setup correctly. |
|
31 | - * |
|
32 | - * @return void |
|
33 | - */ |
|
34 | - public function initializeShortcode(); |
|
35 | - |
|
36 | - /** |
|
37 | - * callback that runs when the shortcode is encountered in post content. |
|
38 | - * IMPORTANT !!! |
|
39 | - * remember that shortcode content should be RETURNED and NOT echoed out |
|
40 | - * |
|
41 | - * @param array $attributes |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - public function processShortcode($attributes = array()); |
|
45 | - |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * Returns whether or not this shortcode class has already been initialized |
|
50 | - * @return boolean |
|
51 | - */ |
|
52 | - public function initialized(); |
|
7 | + /** |
|
8 | + * the actual shortcode tag that gets registered with WordPress |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + public function getTag(); |
|
13 | + |
|
14 | + /** |
|
15 | + * the length of time in seconds to cache the results of the processShortcode() method |
|
16 | + * 0 means the processShortcode() results will NOT be cached at all |
|
17 | + * |
|
18 | + * @return int |
|
19 | + */ |
|
20 | + public function cacheExpiration(); |
|
21 | + |
|
22 | + /** |
|
23 | + * a place for adding any initialization code that needs to run prior to wp_header(). |
|
24 | + * this may be required for shortcodes that utilize a corresponding module, |
|
25 | + * and need to enqueue assets for that module |
|
26 | + * |
|
27 | + * !!! IMPORTANT !!! |
|
28 | + * After performing any logic within this method required for initialization |
|
29 | + * $this->shortcodeHasBeenInitialized(); |
|
30 | + * should be called to ensure that the shortcode is setup correctly. |
|
31 | + * |
|
32 | + * @return void |
|
33 | + */ |
|
34 | + public function initializeShortcode(); |
|
35 | + |
|
36 | + /** |
|
37 | + * callback that runs when the shortcode is encountered in post content. |
|
38 | + * IMPORTANT !!! |
|
39 | + * remember that shortcode content should be RETURNED and NOT echoed out |
|
40 | + * |
|
41 | + * @param array $attributes |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + public function processShortcode($attributes = array()); |
|
45 | + |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * Returns whether or not this shortcode class has already been initialized |
|
50 | + * @return boolean |
|
51 | + */ |
|
52 | + public function initialized(); |
|
53 | 53 | } |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | // serialized attributes |
105 | 105 | wp_json_encode($attributes), |
106 | 106 | // Closure for generating content if cache is expired |
107 | - function () use ($shortcode, $attributes) { |
|
107 | + function() use ($shortcode, $attributes) { |
|
108 | 108 | if ($shortcode->initialized() === false) { |
109 | 109 | $shortcode->initializeShortcode(); |
110 | 110 | } |
@@ -191,14 +191,14 @@ discard block |
||
191 | 191 | $custom_sanitization = $this->customAttributeSanitizationMap(); |
192 | 192 | foreach ($attributes as $key => $value) { |
193 | 193 | // is a custom sanitization callback specified ? |
194 | - if (isset($custom_sanitization[ $key ])) { |
|
195 | - $callback = $custom_sanitization[ $key ]; |
|
194 | + if (isset($custom_sanitization[$key])) { |
|
195 | + $callback = $custom_sanitization[$key]; |
|
196 | 196 | if ($callback === 'skip_sanitization') { |
197 | - $attributes[ $key ] = $value; |
|
197 | + $attributes[$key] = $value; |
|
198 | 198 | continue; |
199 | 199 | } |
200 | 200 | if (function_exists($callback)) { |
201 | - $attributes[ $key ] = $callback($value); |
|
201 | + $attributes[$key] = $callback($value); |
|
202 | 202 | continue; |
203 | 203 | } |
204 | 204 | } |
@@ -208,18 +208,18 @@ discard block |
||
208 | 208 | case is_float($value): |
209 | 209 | // typical booleans |
210 | 210 | case in_array($value, array(true, 'true', '1', 'on', 'yes', false, 'false', '0', 'off', 'no'), true): |
211 | - $attributes[ $key ] = $value; |
|
211 | + $attributes[$key] = $value; |
|
212 | 212 | break; |
213 | 213 | case is_string($value): |
214 | - $attributes[ $key ] = sanitize_text_field($value); |
|
214 | + $attributes[$key] = sanitize_text_field($value); |
|
215 | 215 | break; |
216 | 216 | case is_array($value): |
217 | - $attributes[ $key ] = $this->sanitizeAttributes($value); |
|
217 | + $attributes[$key] = $this->sanitizeAttributes($value); |
|
218 | 218 | break; |
219 | 219 | default: |
220 | 220 | // only remaining data types are Object and Resource |
221 | 221 | // which are not allowed as shortcode attributes |
222 | - $attributes[ $key ] = null; |
|
222 | + $attributes[$key] = null; |
|
223 | 223 | break; |
224 | 224 | } |
225 | 225 | } |
@@ -20,223 +20,223 @@ |
||
20 | 20 | abstract class EspressoShortcode implements ShortcodeInterface |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * transient prefix |
|
25 | - * |
|
26 | - * @type string |
|
27 | - */ |
|
28 | - const CACHE_TRANSIENT_PREFIX = 'ee_sc_'; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var PostRelatedCacheManager $cache_manager |
|
32 | - */ |
|
33 | - private $cache_manager; |
|
34 | - |
|
35 | - /** |
|
36 | - * true if ShortcodeInterface::initializeShortcode() has been called |
|
37 | - * if false, then that will get called before processing |
|
38 | - * |
|
39 | - * @var boolean $initialized |
|
40 | - */ |
|
41 | - private $initialized = false; |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * EspressoShortcode constructor |
|
46 | - * |
|
47 | - * @param PostRelatedCacheManager $cache_manager |
|
48 | - */ |
|
49 | - public function __construct(PostRelatedCacheManager $cache_manager) |
|
50 | - { |
|
51 | - $this->cache_manager = $cache_manager; |
|
52 | - } |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function shortcodeHasBeenInitialized() |
|
59 | - { |
|
60 | - $this->initialized = true; |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * enqueues scripts then processes the shortcode |
|
66 | - * |
|
67 | - * @param array $attributes |
|
68 | - * @return string |
|
69 | - * @throws EE_Error |
|
70 | - * @throws ReflectionException |
|
71 | - */ |
|
72 | - final public function processShortcodeCallback($attributes = array()) |
|
73 | - { |
|
74 | - if ($this instanceof EnqueueAssetsInterface) { |
|
75 | - if (is_admin()) { |
|
76 | - $this->enqueueAdminScripts(); |
|
77 | - } else { |
|
78 | - $this->enqueueScripts(); |
|
79 | - } |
|
80 | - } |
|
81 | - return $this->shortcodeContent( |
|
82 | - $this->sanitizeAttributes((array) $attributes) |
|
83 | - ); |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * If shortcode caching is enabled for the shortcode, |
|
89 | - * and cached results exist, then that will be returned |
|
90 | - * else new content will be generated. |
|
91 | - * If caching is enabled, then the new content will be cached for later. |
|
92 | - * |
|
93 | - * @param array $attributes |
|
94 | - * @return mixed|string |
|
95 | - * @throws EE_Error |
|
96 | - * @throws ReflectionException |
|
97 | - */ |
|
98 | - private function shortcodeContent(array $attributes) |
|
99 | - { |
|
100 | - $shortcode = $this; |
|
101 | - $post_ID = $this->currentPostID(); |
|
102 | - // something like "SC_EVENTS-123" |
|
103 | - $cache_ID = $this->shortcodeCacheID($post_ID); |
|
104 | - $this->cache_manager->clearPostRelatedCacheOnUpdate($post_ID, $cache_ID); |
|
105 | - return $this->cache_manager->get( |
|
106 | - $cache_ID, |
|
107 | - // serialized attributes |
|
108 | - wp_json_encode($attributes), |
|
109 | - // Closure for generating content if cache is expired |
|
110 | - function () use ($shortcode, $attributes) { |
|
111 | - if ($shortcode->initialized() === false) { |
|
112 | - $shortcode->initializeShortcode(); |
|
113 | - } |
|
114 | - return $shortcode->processShortcode($attributes); |
|
115 | - }, |
|
116 | - // filterable cache expiration set by each shortcode |
|
117 | - apply_filters( |
|
118 | - 'FHEE__EventEspresso_core_services_shortcodes_EspressoShortcode__shortcodeContent__cache_expiration', |
|
119 | - $this->cacheExpiration(), |
|
120 | - $this->getTag(), |
|
121 | - $this |
|
122 | - ) |
|
123 | - ); |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * @return int |
|
129 | - * @throws EE_Error |
|
130 | - * @throws ReflectionException |
|
131 | - */ |
|
132 | - private function currentPostID() |
|
133 | - { |
|
134 | - // try to get EE_Event any way we can |
|
135 | - $event = EEH_Event_View::get_event(); |
|
136 | - // then get some kind of ID |
|
137 | - if ($event instanceof EE_Event) { |
|
138 | - return $event->ID(); |
|
139 | - } |
|
140 | - global $post; |
|
141 | - if ($post instanceof WP_Post) { |
|
142 | - return $post->ID; |
|
143 | - } |
|
144 | - return 0; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * @param int $post_ID |
|
150 | - * @return string |
|
151 | - */ |
|
152 | - private function shortcodeCacheID($post_ID) |
|
153 | - { |
|
154 | - $tag = str_replace('ESPRESSO_', '', $this->getTag()); |
|
155 | - return "SC_{$tag}-{$post_ID}"; |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * array for defining custom attribute sanitization callbacks, |
|
161 | - * where keys match keys in your attributes array, |
|
162 | - * and values represent the sanitization function you wish to be applied to that attribute. |
|
163 | - * So for example, if you had an integer attribute named "event_id" |
|
164 | - * that you wanted to be sanitized using absint(), |
|
165 | - * then you would return the following: |
|
166 | - * array('event_id' => 'absint') |
|
167 | - * Entering 'skip_sanitization' for the callback value |
|
168 | - * means that no sanitization will be applied |
|
169 | - * on the assumption that the attribute |
|
170 | - * will be sanitized at some point... right? |
|
171 | - * You wouldn't pass around unsanitized attributes would you? |
|
172 | - * That would be very Tom Foolery of you!!! |
|
173 | - * |
|
174 | - * @return array |
|
175 | - */ |
|
176 | - protected function customAttributeSanitizationMap() |
|
177 | - { |
|
178 | - return array(); |
|
179 | - } |
|
180 | - |
|
181 | - |
|
182 | - /** |
|
183 | - * Performs basic sanitization on shortcode attributes |
|
184 | - * Since incoming attributes from the shortcode usage in the WP editor will all be strings, |
|
185 | - * most attributes will by default be sanitized using the sanitize_text_field() function. |
|
186 | - * This can be overridden using the customAttributeSanitizationMap() method (see above), |
|
187 | - * all other attributes would be sanitized using the defaults in the switch statement below |
|
188 | - * |
|
189 | - * @param array $attributes |
|
190 | - * @return array |
|
191 | - */ |
|
192 | - private function sanitizeAttributes(array $attributes) |
|
193 | - { |
|
194 | - $custom_sanitization = $this->customAttributeSanitizationMap(); |
|
195 | - foreach ($attributes as $key => $value) { |
|
196 | - // is a custom sanitization callback specified ? |
|
197 | - if (isset($custom_sanitization[ $key ])) { |
|
198 | - $callback = $custom_sanitization[ $key ]; |
|
199 | - if ($callback === 'skip_sanitization') { |
|
200 | - $attributes[ $key ] = $value; |
|
201 | - continue; |
|
202 | - } |
|
203 | - if (function_exists($callback)) { |
|
204 | - $attributes[ $key ] = $callback($value); |
|
205 | - continue; |
|
206 | - } |
|
207 | - } |
|
208 | - switch (true) { |
|
209 | - case $value === null: |
|
210 | - case is_int($value): |
|
211 | - case is_float($value): |
|
212 | - // typical booleans |
|
213 | - case in_array($value, array(true, 'true', '1', 'on', 'yes', false, 'false', '0', 'off', 'no'), true): |
|
214 | - $attributes[ $key ] = $value; |
|
215 | - break; |
|
216 | - case is_string($value): |
|
217 | - $attributes[ $key ] = sanitize_text_field($value); |
|
218 | - break; |
|
219 | - case is_array($value): |
|
220 | - $attributes[ $key ] = $this->sanitizeAttributes($value); |
|
221 | - break; |
|
222 | - default: |
|
223 | - // only remaining data types are Object and Resource |
|
224 | - // which are not allowed as shortcode attributes |
|
225 | - $attributes[ $key ] = null; |
|
226 | - break; |
|
227 | - } |
|
228 | - } |
|
229 | - return $attributes; |
|
230 | - } |
|
231 | - |
|
232 | - |
|
233 | - /** |
|
234 | - * Returns whether or not this shortcode has been initialized |
|
235 | - * |
|
236 | - * @return boolean |
|
237 | - */ |
|
238 | - public function initialized() |
|
239 | - { |
|
240 | - return $this->initialized; |
|
241 | - } |
|
23 | + /** |
|
24 | + * transient prefix |
|
25 | + * |
|
26 | + * @type string |
|
27 | + */ |
|
28 | + const CACHE_TRANSIENT_PREFIX = 'ee_sc_'; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var PostRelatedCacheManager $cache_manager |
|
32 | + */ |
|
33 | + private $cache_manager; |
|
34 | + |
|
35 | + /** |
|
36 | + * true if ShortcodeInterface::initializeShortcode() has been called |
|
37 | + * if false, then that will get called before processing |
|
38 | + * |
|
39 | + * @var boolean $initialized |
|
40 | + */ |
|
41 | + private $initialized = false; |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * EspressoShortcode constructor |
|
46 | + * |
|
47 | + * @param PostRelatedCacheManager $cache_manager |
|
48 | + */ |
|
49 | + public function __construct(PostRelatedCacheManager $cache_manager) |
|
50 | + { |
|
51 | + $this->cache_manager = $cache_manager; |
|
52 | + } |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function shortcodeHasBeenInitialized() |
|
59 | + { |
|
60 | + $this->initialized = true; |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * enqueues scripts then processes the shortcode |
|
66 | + * |
|
67 | + * @param array $attributes |
|
68 | + * @return string |
|
69 | + * @throws EE_Error |
|
70 | + * @throws ReflectionException |
|
71 | + */ |
|
72 | + final public function processShortcodeCallback($attributes = array()) |
|
73 | + { |
|
74 | + if ($this instanceof EnqueueAssetsInterface) { |
|
75 | + if (is_admin()) { |
|
76 | + $this->enqueueAdminScripts(); |
|
77 | + } else { |
|
78 | + $this->enqueueScripts(); |
|
79 | + } |
|
80 | + } |
|
81 | + return $this->shortcodeContent( |
|
82 | + $this->sanitizeAttributes((array) $attributes) |
|
83 | + ); |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * If shortcode caching is enabled for the shortcode, |
|
89 | + * and cached results exist, then that will be returned |
|
90 | + * else new content will be generated. |
|
91 | + * If caching is enabled, then the new content will be cached for later. |
|
92 | + * |
|
93 | + * @param array $attributes |
|
94 | + * @return mixed|string |
|
95 | + * @throws EE_Error |
|
96 | + * @throws ReflectionException |
|
97 | + */ |
|
98 | + private function shortcodeContent(array $attributes) |
|
99 | + { |
|
100 | + $shortcode = $this; |
|
101 | + $post_ID = $this->currentPostID(); |
|
102 | + // something like "SC_EVENTS-123" |
|
103 | + $cache_ID = $this->shortcodeCacheID($post_ID); |
|
104 | + $this->cache_manager->clearPostRelatedCacheOnUpdate($post_ID, $cache_ID); |
|
105 | + return $this->cache_manager->get( |
|
106 | + $cache_ID, |
|
107 | + // serialized attributes |
|
108 | + wp_json_encode($attributes), |
|
109 | + // Closure for generating content if cache is expired |
|
110 | + function () use ($shortcode, $attributes) { |
|
111 | + if ($shortcode->initialized() === false) { |
|
112 | + $shortcode->initializeShortcode(); |
|
113 | + } |
|
114 | + return $shortcode->processShortcode($attributes); |
|
115 | + }, |
|
116 | + // filterable cache expiration set by each shortcode |
|
117 | + apply_filters( |
|
118 | + 'FHEE__EventEspresso_core_services_shortcodes_EspressoShortcode__shortcodeContent__cache_expiration', |
|
119 | + $this->cacheExpiration(), |
|
120 | + $this->getTag(), |
|
121 | + $this |
|
122 | + ) |
|
123 | + ); |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * @return int |
|
129 | + * @throws EE_Error |
|
130 | + * @throws ReflectionException |
|
131 | + */ |
|
132 | + private function currentPostID() |
|
133 | + { |
|
134 | + // try to get EE_Event any way we can |
|
135 | + $event = EEH_Event_View::get_event(); |
|
136 | + // then get some kind of ID |
|
137 | + if ($event instanceof EE_Event) { |
|
138 | + return $event->ID(); |
|
139 | + } |
|
140 | + global $post; |
|
141 | + if ($post instanceof WP_Post) { |
|
142 | + return $post->ID; |
|
143 | + } |
|
144 | + return 0; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * @param int $post_ID |
|
150 | + * @return string |
|
151 | + */ |
|
152 | + private function shortcodeCacheID($post_ID) |
|
153 | + { |
|
154 | + $tag = str_replace('ESPRESSO_', '', $this->getTag()); |
|
155 | + return "SC_{$tag}-{$post_ID}"; |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * array for defining custom attribute sanitization callbacks, |
|
161 | + * where keys match keys in your attributes array, |
|
162 | + * and values represent the sanitization function you wish to be applied to that attribute. |
|
163 | + * So for example, if you had an integer attribute named "event_id" |
|
164 | + * that you wanted to be sanitized using absint(), |
|
165 | + * then you would return the following: |
|
166 | + * array('event_id' => 'absint') |
|
167 | + * Entering 'skip_sanitization' for the callback value |
|
168 | + * means that no sanitization will be applied |
|
169 | + * on the assumption that the attribute |
|
170 | + * will be sanitized at some point... right? |
|
171 | + * You wouldn't pass around unsanitized attributes would you? |
|
172 | + * That would be very Tom Foolery of you!!! |
|
173 | + * |
|
174 | + * @return array |
|
175 | + */ |
|
176 | + protected function customAttributeSanitizationMap() |
|
177 | + { |
|
178 | + return array(); |
|
179 | + } |
|
180 | + |
|
181 | + |
|
182 | + /** |
|
183 | + * Performs basic sanitization on shortcode attributes |
|
184 | + * Since incoming attributes from the shortcode usage in the WP editor will all be strings, |
|
185 | + * most attributes will by default be sanitized using the sanitize_text_field() function. |
|
186 | + * This can be overridden using the customAttributeSanitizationMap() method (see above), |
|
187 | + * all other attributes would be sanitized using the defaults in the switch statement below |
|
188 | + * |
|
189 | + * @param array $attributes |
|
190 | + * @return array |
|
191 | + */ |
|
192 | + private function sanitizeAttributes(array $attributes) |
|
193 | + { |
|
194 | + $custom_sanitization = $this->customAttributeSanitizationMap(); |
|
195 | + foreach ($attributes as $key => $value) { |
|
196 | + // is a custom sanitization callback specified ? |
|
197 | + if (isset($custom_sanitization[ $key ])) { |
|
198 | + $callback = $custom_sanitization[ $key ]; |
|
199 | + if ($callback === 'skip_sanitization') { |
|
200 | + $attributes[ $key ] = $value; |
|
201 | + continue; |
|
202 | + } |
|
203 | + if (function_exists($callback)) { |
|
204 | + $attributes[ $key ] = $callback($value); |
|
205 | + continue; |
|
206 | + } |
|
207 | + } |
|
208 | + switch (true) { |
|
209 | + case $value === null: |
|
210 | + case is_int($value): |
|
211 | + case is_float($value): |
|
212 | + // typical booleans |
|
213 | + case in_array($value, array(true, 'true', '1', 'on', 'yes', false, 'false', '0', 'off', 'no'), true): |
|
214 | + $attributes[ $key ] = $value; |
|
215 | + break; |
|
216 | + case is_string($value): |
|
217 | + $attributes[ $key ] = sanitize_text_field($value); |
|
218 | + break; |
|
219 | + case is_array($value): |
|
220 | + $attributes[ $key ] = $this->sanitizeAttributes($value); |
|
221 | + break; |
|
222 | + default: |
|
223 | + // only remaining data types are Object and Resource |
|
224 | + // which are not allowed as shortcode attributes |
|
225 | + $attributes[ $key ] = null; |
|
226 | + break; |
|
227 | + } |
|
228 | + } |
|
229 | + return $attributes; |
|
230 | + } |
|
231 | + |
|
232 | + |
|
233 | + /** |
|
234 | + * Returns whether or not this shortcode has been initialized |
|
235 | + * |
|
236 | + * @return boolean |
|
237 | + */ |
|
238 | + public function initialized() |
|
239 | + { |
|
240 | + return $this->initialized; |
|
241 | + } |
|
242 | 242 | } |
@@ -13,31 +13,31 @@ |
||
13 | 13 | interface NoticeConverterInterface |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * @return NoticesContainerInterface |
|
18 | - */ |
|
19 | - public function getNotices(); |
|
16 | + /** |
|
17 | + * @return NoticesContainerInterface |
|
18 | + */ |
|
19 | + public function getNotices(); |
|
20 | 20 | |
21 | - /** |
|
22 | - * @param bool $throw_exceptions |
|
23 | - */ |
|
24 | - public function setThrowExceptions($throw_exceptions); |
|
21 | + /** |
|
22 | + * @param bool $throw_exceptions |
|
23 | + */ |
|
24 | + public function setThrowExceptions($throw_exceptions); |
|
25 | 25 | |
26 | - /** |
|
27 | - * @return bool |
|
28 | - */ |
|
29 | - public function getThrowExceptions(); |
|
26 | + /** |
|
27 | + * @return bool |
|
28 | + */ |
|
29 | + public function getThrowExceptions(); |
|
30 | 30 | |
31 | - /** |
|
32 | - * Converts NoticesContainerInterface objects into other format |
|
33 | - * |
|
34 | - * @param NoticesContainerInterface $notices |
|
35 | - * @return |
|
36 | - */ |
|
37 | - public function process(NoticesContainerInterface $notices); |
|
31 | + /** |
|
32 | + * Converts NoticesContainerInterface objects into other format |
|
33 | + * |
|
34 | + * @param NoticesContainerInterface $notices |
|
35 | + * @return |
|
36 | + */ |
|
37 | + public function process(NoticesContainerInterface $notices); |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return void; |
|
41 | - */ |
|
42 | - public function clearNotices(); |
|
39 | + /** |
|
40 | + * @return void; |
|
41 | + */ |
|
42 | + public function clearNotices(); |
|
43 | 43 | } |
@@ -13,38 +13,38 @@ |
||
13 | 13 | interface NoticeInterface |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - public function type(); |
|
16 | + /** |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + public function type(); |
|
20 | 20 | |
21 | 21 | |
22 | - /** |
|
23 | - * @return string |
|
24 | - */ |
|
25 | - public function message(); |
|
22 | + /** |
|
23 | + * @return string |
|
24 | + */ |
|
25 | + public function message(); |
|
26 | 26 | |
27 | 27 | |
28 | - /** |
|
29 | - * @return bool |
|
30 | - */ |
|
31 | - public function isDismissible(); |
|
28 | + /** |
|
29 | + * @return bool |
|
30 | + */ |
|
31 | + public function isDismissible(); |
|
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function file(); |
|
34 | + /** |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function file(); |
|
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function func(); |
|
40 | + /** |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function func(); |
|
44 | 44 | |
45 | 45 | |
46 | - /** |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function line(); |
|
46 | + /** |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function line(); |
|
50 | 50 | } |
@@ -9,13 +9,13 @@ |
||
9 | 9 | interface LocatorInterface |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * given a string or an array of information for where to look, |
|
14 | - * will find all files in that location |
|
15 | - * |
|
16 | - * @access public |
|
17 | - * @param array|string $location |
|
18 | - * @return \FilesystemIterator |
|
19 | - */ |
|
20 | - public function locate($location); |
|
12 | + /** |
|
13 | + * given a string or an array of information for where to look, |
|
14 | + * will find all files in that location |
|
15 | + * |
|
16 | + * @access public |
|
17 | + * @param array|string $location |
|
18 | + * @return \FilesystemIterator |
|
19 | + */ |
|
20 | + public function locate($location); |
|
21 | 21 | } |
@@ -5,18 +5,18 @@ |
||
5 | 5 | interface LoaderDecoratorInterface |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * @param string $fqcn |
|
10 | - * @param array $arguments |
|
11 | - * @param bool $shared |
|
12 | - * @return mixed |
|
13 | - */ |
|
14 | - public function load($fqcn, $arguments = array(), $shared = true); |
|
8 | + /** |
|
9 | + * @param string $fqcn |
|
10 | + * @param array $arguments |
|
11 | + * @param bool $shared |
|
12 | + * @return mixed |
|
13 | + */ |
|
14 | + public function load($fqcn, $arguments = array(), $shared = true); |
|
15 | 15 | |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * calls reset() on loader if method exists |
|
20 | - */ |
|
21 | - public function reset(); |
|
18 | + /** |
|
19 | + * calls reset() on loader if method exists |
|
20 | + */ |
|
21 | + public function reset(); |
|
22 | 22 | } |