@@ -25,421 +25,421 @@ |
||
| 25 | 25 | class LegacyShortcodesManager |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @var EE_Registry $registry |
|
| 30 | - */ |
|
| 31 | - private $registry; |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * LegacyShortcodesManager constructor. |
|
| 38 | - * |
|
| 39 | - * @param \EE_Registry $registry |
|
| 40 | - */ |
|
| 41 | - public function __construct(EE_Registry $registry) |
|
| 42 | - { |
|
| 43 | - $this->registry = $registry; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @return EE_Registry |
|
| 50 | - */ |
|
| 51 | - public function registry() |
|
| 52 | - { |
|
| 53 | - return $this->registry; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * registerShortcodes |
|
| 60 | - * |
|
| 61 | - * @return void |
|
| 62 | - */ |
|
| 63 | - public function registerShortcodes() |
|
| 64 | - { |
|
| 65 | - $this->registry->shortcodes = $this->getShortcodes(); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * getShortcodes |
|
| 72 | - * |
|
| 73 | - * @return array |
|
| 74 | - */ |
|
| 75 | - public function getShortcodes() |
|
| 76 | - { |
|
| 77 | - // previously this method would glob the shortcodes directory |
|
| 78 | - // then filter that list of shortcodes to register, |
|
| 79 | - // but now we are going to just supply an empty array. |
|
| 80 | - // this allows any shortcodes that have not yet been converted to the new system |
|
| 81 | - // to still get loaded and processed, albeit using the same legacy logic as before |
|
| 82 | - $shortcodes_to_register = apply_filters( |
|
| 83 | - 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
|
| 84 | - array() |
|
| 85 | - ); |
|
| 86 | - if ( ! empty($shortcodes_to_register)) { |
|
| 87 | - // cycle thru shortcode folders |
|
| 88 | - foreach ($shortcodes_to_register as $shortcode_path) { |
|
| 89 | - // add to list of installed shortcode modules |
|
| 90 | - $this->registerShortcode($shortcode_path); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - // filter list of installed modules |
|
| 94 | - return apply_filters( |
|
| 95 | - 'FHEE__EE_Config___register_shortcodes__installed_shortcodes', |
|
| 96 | - ! empty($this->registry->shortcodes) |
|
| 97 | - ? $this->registry->shortcodes |
|
| 98 | - : array() |
|
| 99 | - ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * register_shortcode - makes core aware of this shortcode |
|
| 106 | - * |
|
| 107 | - * @access public |
|
| 108 | - * @param string $shortcode_path - full path up to and including shortcode folder |
|
| 109 | - * @return bool |
|
| 110 | - */ |
|
| 111 | - public function registerShortcode($shortcode_path = null) |
|
| 112 | - { |
|
| 113 | - do_action('AHEE__EE_Config__register_shortcode__begin', $shortcode_path); |
|
| 114 | - $shortcode_ext = '.shortcode.php'; |
|
| 115 | - // make all separators match |
|
| 116 | - $shortcode_path = str_replace(array('\\', '/'), DS, $shortcode_path); |
|
| 117 | - // does the file path INCLUDE the actual file name as part of the path ? |
|
| 118 | - if (strpos($shortcode_path, $shortcode_ext) !== false) { |
|
| 119 | - // grab shortcode file name from directory name and break apart at dots |
|
| 120 | - $shortcode_file = explode('.', basename($shortcode_path)); |
|
| 121 | - // take first segment from file name pieces and remove class prefix if it exists |
|
| 122 | - $shortcode = strpos($shortcode_file[0], 'EES_') === 0 |
|
| 123 | - ? substr($shortcode_file[0], 4) |
|
| 124 | - : $shortcode_file[0]; |
|
| 125 | - // sanitize shortcode directory name |
|
| 126 | - $shortcode = sanitize_key($shortcode); |
|
| 127 | - // now we need to rebuild the shortcode path |
|
| 128 | - $shortcode_path = explode(DS, $shortcode_path); |
|
| 129 | - // remove last segment |
|
| 130 | - array_pop($shortcode_path); |
|
| 131 | - // glue it back together |
|
| 132 | - $shortcode_path = implode(DS, $shortcode_path) . DS; |
|
| 133 | - } else { |
|
| 134 | - // we need to generate the filename based off of the folder name |
|
| 135 | - // grab and sanitize shortcode directory name |
|
| 136 | - $shortcode = sanitize_key(basename($shortcode_path)); |
|
| 137 | - $shortcode_path = rtrim($shortcode_path, DS) . DS; |
|
| 138 | - } |
|
| 139 | - // create classname from shortcode directory or file name |
|
| 140 | - $shortcode = str_replace(' ', '_', ucwords(str_replace('_', ' ', $shortcode))); |
|
| 141 | - // add class prefix |
|
| 142 | - $shortcode_class = 'EES_' . $shortcode; |
|
| 143 | - // does the shortcode exist ? |
|
| 144 | - if ( ! is_readable($shortcode_path . DS . $shortcode_class . $shortcode_ext)) { |
|
| 145 | - $msg = sprintf( |
|
| 146 | - esc_html__( |
|
| 147 | - 'The requested %s shortcode file could not be found or is not readable due to file permissions. It should be in %s', |
|
| 148 | - 'event_espresso' |
|
| 149 | - ), |
|
| 150 | - $shortcode_class, |
|
| 151 | - $shortcode_path . DS . $shortcode_class . $shortcode_ext |
|
| 152 | - ); |
|
| 153 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 154 | - return false; |
|
| 155 | - } |
|
| 156 | - // load the shortcode class file |
|
| 157 | - require_once($shortcode_path . $shortcode_class . $shortcode_ext); |
|
| 158 | - // verify that class exists |
|
| 159 | - if ( ! class_exists($shortcode_class)) { |
|
| 160 | - $msg = sprintf( |
|
| 161 | - esc_html__('The requested %s shortcode class does not exist.', 'event_espresso'), |
|
| 162 | - $shortcode_class |
|
| 163 | - ); |
|
| 164 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 165 | - return false; |
|
| 166 | - } |
|
| 167 | - $shortcode = strtoupper($shortcode); |
|
| 168 | - // add to array of registered shortcodes |
|
| 169 | - $this->registry->shortcodes->{$shortcode} = $shortcode_path . $shortcode_class . $shortcode_ext; |
|
| 170 | - return true; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * _initialize_shortcodes |
|
| 177 | - * allow shortcodes to set hooks for the rest of the system |
|
| 178 | - * |
|
| 179 | - * @access private |
|
| 180 | - * @return void |
|
| 181 | - */ |
|
| 182 | - public function addShortcodes() |
|
| 183 | - { |
|
| 184 | - // cycle thru shortcode folders |
|
| 185 | - foreach ($this->registry->shortcodes as $shortcode => $shortcode_path) { |
|
| 186 | - // add class prefix |
|
| 187 | - $shortcode_class = 'EES_' . $shortcode; |
|
| 188 | - // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system |
|
| 189 | - // which set hooks ? |
|
| 190 | - if (is_admin()) { |
|
| 191 | - // fire immediately |
|
| 192 | - call_user_func(array($shortcode_class, 'set_hooks_admin')); |
|
| 193 | - } else { |
|
| 194 | - // delay until other systems are online |
|
| 195 | - add_action( |
|
| 196 | - 'AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons', |
|
| 197 | - array($shortcode_class, 'set_hooks') |
|
| 198 | - ); |
|
| 199 | - // convert classname to UPPERCASE and create WP shortcode. |
|
| 200 | - $shortcode_tag = strtoupper($shortcode); |
|
| 201 | - // but first check if the shortcode has already |
|
| 202 | - // been added before assigning 'fallback_shortcode_processor' |
|
| 203 | - if ( ! shortcode_exists($shortcode_tag)) { |
|
| 204 | - // NOTE: this shortcode declaration will get overridden if the shortcode |
|
| 205 | - // is successfully detected in the post content in initializeShortcode() |
|
| 206 | - add_shortcode($shortcode_tag, array($shortcode_class, 'fallback_shortcode_processor')); |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * callback for the WP "get_header" hook point |
|
| 216 | - * checks posts for EE shortcodes, and initializes them, |
|
| 217 | - * then toggles filter switch that loads core default assets |
|
| 218 | - * |
|
| 219 | - * @param \WP_Query $wp_query |
|
| 220 | - * @return void |
|
| 221 | - */ |
|
| 222 | - public function initializeShortcodes(WP_Query $wp_query) |
|
| 223 | - { |
|
| 224 | - if (empty($this->registry->shortcodes) || ! $wp_query->is_main_query() || is_admin()) { |
|
| 225 | - return; |
|
| 226 | - } |
|
| 227 | - global $wp; |
|
| 228 | - /** @var EE_Front_controller $Front_Controller */ |
|
| 229 | - $Front_Controller = $this->registry->load_core('Front_Controller', array(), false); |
|
| 230 | - do_action('AHEE__EE_Front_Controller__initialize_shortcodes__begin', $wp, $Front_Controller); |
|
| 231 | - $Front_Controller->Request_Handler()->set_request_vars(); |
|
| 232 | - // grab post_name from request |
|
| 233 | - $current_post = apply_filters( |
|
| 234 | - 'FHEE__EE_Front_Controller__initialize_shortcodes__current_post_name', |
|
| 235 | - $Front_Controller->Request_Handler()->get('post_name') |
|
| 236 | - ); |
|
| 237 | - $show_on_front = get_option('show_on_front'); |
|
| 238 | - // if it's not set, then check if frontpage is blog |
|
| 239 | - if (empty($current_post)) { |
|
| 240 | - // yup.. this is the posts page, prepare to load all shortcode modules |
|
| 241 | - $current_post = 'posts'; |
|
| 242 | - // unless.. |
|
| 243 | - if ($show_on_front === 'page') { |
|
| 244 | - // some other page is set as the homepage |
|
| 245 | - $page_on_front = get_option('page_on_front'); |
|
| 246 | - if ($page_on_front) { |
|
| 247 | - // k now we need to find the post_name for this page |
|
| 248 | - global $wpdb; |
|
| 249 | - $page_on_front = $wpdb->get_var( |
|
| 250 | - $wpdb->prepare( |
|
| 251 | - "SELECT post_name from {$wpdb->posts} WHERE post_type='page' AND post_status NOT IN ('auto-draft', 'inherit', 'trash') AND ID=%d", |
|
| 252 | - $page_on_front |
|
| 253 | - ) |
|
| 254 | - ); |
|
| 255 | - // set the current post slug to what it actually is |
|
| 256 | - $current_post = $page_on_front ? $page_on_front : $current_post; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - // in case $current_post is hierarchical like: /parent-page/current-page |
|
| 261 | - $current_post = basename($current_post); |
|
| 262 | - if ($current_post && is_singular()) { |
|
| 263 | - global $wpdb; |
|
| 264 | - $post_content = $wpdb->get_var( |
|
| 265 | - $wpdb->prepare( |
|
| 266 | - "SELECT post_content from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND post_name=%s", |
|
| 267 | - $current_post |
|
| 268 | - ) |
|
| 269 | - ); |
|
| 270 | - $load_assets = $this->parseContentForShortcodes($post_content); |
|
| 271 | - } else { |
|
| 272 | - // initialize all legacy shortcodes |
|
| 273 | - $load_assets = $this->parseContentForShortcodes('', true); |
|
| 274 | - } |
|
| 275 | - if ($load_assets) { |
|
| 276 | - $this->registry->REQ->set_espresso_page(true); |
|
| 277 | - add_filter('FHEE_load_css', '__return_true'); |
|
| 278 | - add_filter('FHEE_load_js', '__return_true'); |
|
| 279 | - } |
|
| 280 | - do_action('AHEE__EE_Front_Controller__initialize_shortcodes__end', $Front_Controller); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * checks supplied content against list of legacy shortcodes, |
|
| 287 | - * then initializes any found shortcodes, and returns true. |
|
| 288 | - * returns false if no shortcodes found. |
|
| 289 | - * |
|
| 290 | - * @param string $content |
|
| 291 | - * @param bool $load_all if true, then ALL active legacy shortcodes will be initialized |
|
| 292 | - * @return bool |
|
| 293 | - */ |
|
| 294 | - public function parseContentForShortcodes($content = '', $load_all = false) |
|
| 295 | - { |
|
| 296 | - $has_shortcode = false; |
|
| 297 | - foreach ($this->registry->shortcodes as $shortcode_class => $shortcode) { |
|
| 298 | - if ($load_all || has_shortcode($content, $shortcode_class) ) { |
|
| 299 | - // load up the shortcode |
|
| 300 | - $this->initializeShortcode($shortcode_class); |
|
| 301 | - $has_shortcode = true; |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - return $has_shortcode; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * given a shortcode name, will instantiate the shortcode and call it's run() method |
|
| 311 | - * |
|
| 312 | - * @param string $shortcode_class |
|
| 313 | - * @param WP $wp |
|
| 314 | - */ |
|
| 315 | - public function initializeShortcode($shortcode_class = '', WP $wp = null) |
|
| 316 | - { |
|
| 317 | - // don't do anything if shortcode is already initialized |
|
| 318 | - if ( |
|
| 319 | - empty($this->registry->shortcodes->{$shortcode_class}) |
|
| 320 | - || ! is_string($this->registry->shortcodes->{$shortcode_class}) |
|
| 321 | - ) { |
|
| 322 | - return; |
|
| 323 | - } |
|
| 324 | - // let's pause to reflect on this... |
|
| 325 | - $sc_reflector = new ReflectionClass(LegacyShortcodesManager::addShortcodeClassPrefix($shortcode_class)); |
|
| 326 | - // ensure that class is actually a shortcode |
|
| 327 | - if ( |
|
| 328 | - defined('WP_DEBUG') |
|
| 329 | - && WP_DEBUG === true |
|
| 330 | - && ! $sc_reflector->isSubclassOf('EES_Shortcode') |
|
| 331 | - ) { |
|
| 332 | - EE_Error::add_error( |
|
| 333 | - sprintf( |
|
| 334 | - esc_html__( |
|
| 335 | - 'The requested %s shortcode is not of the class "EES_Shortcode". Please check your files.', |
|
| 336 | - 'event_espresso' |
|
| 337 | - ), |
|
| 338 | - $shortcode_class |
|
| 339 | - ), |
|
| 340 | - __FILE__, |
|
| 341 | - __FUNCTION__, |
|
| 342 | - __LINE__ |
|
| 343 | - ); |
|
| 344 | - add_filter('FHEE_run_EE_the_content', '__return_true'); |
|
| 345 | - return; |
|
| 346 | - } |
|
| 347 | - global $wp; |
|
| 348 | - // and pass the request object to the run method |
|
| 349 | - $this->registry->shortcodes->{$shortcode_class} = $sc_reflector->newInstance(); |
|
| 350 | - // fire the shortcode class's run method, so that it can activate resources |
|
| 351 | - $this->registry->shortcodes->{$shortcode_class}->run($wp); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * get classname, remove EES_prefix, and convert to UPPERCASE |
|
| 358 | - * |
|
| 359 | - * @param string $class_name |
|
| 360 | - * @return string |
|
| 361 | - */ |
|
| 362 | - public static function generateShortcodeTagFromClassName($class_name) |
|
| 363 | - { |
|
| 364 | - return strtoupper(str_replace('EES_', '', $class_name)); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * add EES_prefix and Capitalize words |
|
| 371 | - * |
|
| 372 | - * @param string $tag |
|
| 373 | - * @return string |
|
| 374 | - */ |
|
| 375 | - public static function generateShortcodeClassNameFromTag($tag) |
|
| 376 | - { |
|
| 377 | - // order of operation runs from inside to out |
|
| 378 | - // 5) maybe add prefix |
|
| 379 | - return LegacyShortcodesManager::addShortcodeClassPrefix( |
|
| 380 | - // 4) find spaces, replace with underscores |
|
| 381 | - str_replace( |
|
| 382 | - ' ', |
|
| 383 | - '_', |
|
| 384 | - // 3) capitalize first letter of each word |
|
| 385 | - ucwords( |
|
| 386 | - // 2) also change to lowercase so ucwords() will work |
|
| 387 | - strtolower( |
|
| 388 | - // 1) find underscores, replace with spaces so ucwords() will work |
|
| 389 | - str_replace( |
|
| 390 | - '_', |
|
| 391 | - ' ', |
|
| 392 | - $tag |
|
| 393 | - ) |
|
| 394 | - ) |
|
| 395 | - ) |
|
| 396 | - ) |
|
| 397 | - ); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * maybe add EES_prefix |
|
| 404 | - * |
|
| 405 | - * @param string $class_name |
|
| 406 | - * @return string |
|
| 407 | - */ |
|
| 408 | - public static function addShortcodeClassPrefix($class_name) |
|
| 409 | - { |
|
| 410 | - return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_' . $class_name; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * @return array |
|
| 417 | - */ |
|
| 418 | - public function getEspressoShortcodeTags() |
|
| 419 | - { |
|
| 420 | - static $shortcode_tags = array(); |
|
| 421 | - if (empty($shortcode_tags)) { |
|
| 422 | - $shortcode_tags = array_keys((array)$this->registry->shortcodes); |
|
| 423 | - } |
|
| 424 | - return $shortcode_tags; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * @param string $content |
|
| 431 | - * @return string |
|
| 432 | - */ |
|
| 433 | - public function doShortcode($content) |
|
| 434 | - { |
|
| 435 | - foreach ($this->getEspressoShortcodeTags() as $shortcode_tag) { |
|
| 436 | - if (strpos($content, $shortcode_tag) !== false) { |
|
| 437 | - $shortcode_class = LegacyShortcodesManager::generateShortcodeClassNameFromTag($shortcode_tag); |
|
| 438 | - $this->initializeShortcode($shortcode_class); |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - return do_shortcode($content); |
|
| 442 | - } |
|
| 28 | + /** |
|
| 29 | + * @var EE_Registry $registry |
|
| 30 | + */ |
|
| 31 | + private $registry; |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * LegacyShortcodesManager constructor. |
|
| 38 | + * |
|
| 39 | + * @param \EE_Registry $registry |
|
| 40 | + */ |
|
| 41 | + public function __construct(EE_Registry $registry) |
|
| 42 | + { |
|
| 43 | + $this->registry = $registry; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @return EE_Registry |
|
| 50 | + */ |
|
| 51 | + public function registry() |
|
| 52 | + { |
|
| 53 | + return $this->registry; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * registerShortcodes |
|
| 60 | + * |
|
| 61 | + * @return void |
|
| 62 | + */ |
|
| 63 | + public function registerShortcodes() |
|
| 64 | + { |
|
| 65 | + $this->registry->shortcodes = $this->getShortcodes(); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * getShortcodes |
|
| 72 | + * |
|
| 73 | + * @return array |
|
| 74 | + */ |
|
| 75 | + public function getShortcodes() |
|
| 76 | + { |
|
| 77 | + // previously this method would glob the shortcodes directory |
|
| 78 | + // then filter that list of shortcodes to register, |
|
| 79 | + // but now we are going to just supply an empty array. |
|
| 80 | + // this allows any shortcodes that have not yet been converted to the new system |
|
| 81 | + // to still get loaded and processed, albeit using the same legacy logic as before |
|
| 82 | + $shortcodes_to_register = apply_filters( |
|
| 83 | + 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
|
| 84 | + array() |
|
| 85 | + ); |
|
| 86 | + if ( ! empty($shortcodes_to_register)) { |
|
| 87 | + // cycle thru shortcode folders |
|
| 88 | + foreach ($shortcodes_to_register as $shortcode_path) { |
|
| 89 | + // add to list of installed shortcode modules |
|
| 90 | + $this->registerShortcode($shortcode_path); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + // filter list of installed modules |
|
| 94 | + return apply_filters( |
|
| 95 | + 'FHEE__EE_Config___register_shortcodes__installed_shortcodes', |
|
| 96 | + ! empty($this->registry->shortcodes) |
|
| 97 | + ? $this->registry->shortcodes |
|
| 98 | + : array() |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * register_shortcode - makes core aware of this shortcode |
|
| 106 | + * |
|
| 107 | + * @access public |
|
| 108 | + * @param string $shortcode_path - full path up to and including shortcode folder |
|
| 109 | + * @return bool |
|
| 110 | + */ |
|
| 111 | + public function registerShortcode($shortcode_path = null) |
|
| 112 | + { |
|
| 113 | + do_action('AHEE__EE_Config__register_shortcode__begin', $shortcode_path); |
|
| 114 | + $shortcode_ext = '.shortcode.php'; |
|
| 115 | + // make all separators match |
|
| 116 | + $shortcode_path = str_replace(array('\\', '/'), DS, $shortcode_path); |
|
| 117 | + // does the file path INCLUDE the actual file name as part of the path ? |
|
| 118 | + if (strpos($shortcode_path, $shortcode_ext) !== false) { |
|
| 119 | + // grab shortcode file name from directory name and break apart at dots |
|
| 120 | + $shortcode_file = explode('.', basename($shortcode_path)); |
|
| 121 | + // take first segment from file name pieces and remove class prefix if it exists |
|
| 122 | + $shortcode = strpos($shortcode_file[0], 'EES_') === 0 |
|
| 123 | + ? substr($shortcode_file[0], 4) |
|
| 124 | + : $shortcode_file[0]; |
|
| 125 | + // sanitize shortcode directory name |
|
| 126 | + $shortcode = sanitize_key($shortcode); |
|
| 127 | + // now we need to rebuild the shortcode path |
|
| 128 | + $shortcode_path = explode(DS, $shortcode_path); |
|
| 129 | + // remove last segment |
|
| 130 | + array_pop($shortcode_path); |
|
| 131 | + // glue it back together |
|
| 132 | + $shortcode_path = implode(DS, $shortcode_path) . DS; |
|
| 133 | + } else { |
|
| 134 | + // we need to generate the filename based off of the folder name |
|
| 135 | + // grab and sanitize shortcode directory name |
|
| 136 | + $shortcode = sanitize_key(basename($shortcode_path)); |
|
| 137 | + $shortcode_path = rtrim($shortcode_path, DS) . DS; |
|
| 138 | + } |
|
| 139 | + // create classname from shortcode directory or file name |
|
| 140 | + $shortcode = str_replace(' ', '_', ucwords(str_replace('_', ' ', $shortcode))); |
|
| 141 | + // add class prefix |
|
| 142 | + $shortcode_class = 'EES_' . $shortcode; |
|
| 143 | + // does the shortcode exist ? |
|
| 144 | + if ( ! is_readable($shortcode_path . DS . $shortcode_class . $shortcode_ext)) { |
|
| 145 | + $msg = sprintf( |
|
| 146 | + esc_html__( |
|
| 147 | + 'The requested %s shortcode file could not be found or is not readable due to file permissions. It should be in %s', |
|
| 148 | + 'event_espresso' |
|
| 149 | + ), |
|
| 150 | + $shortcode_class, |
|
| 151 | + $shortcode_path . DS . $shortcode_class . $shortcode_ext |
|
| 152 | + ); |
|
| 153 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 154 | + return false; |
|
| 155 | + } |
|
| 156 | + // load the shortcode class file |
|
| 157 | + require_once($shortcode_path . $shortcode_class . $shortcode_ext); |
|
| 158 | + // verify that class exists |
|
| 159 | + if ( ! class_exists($shortcode_class)) { |
|
| 160 | + $msg = sprintf( |
|
| 161 | + esc_html__('The requested %s shortcode class does not exist.', 'event_espresso'), |
|
| 162 | + $shortcode_class |
|
| 163 | + ); |
|
| 164 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 165 | + return false; |
|
| 166 | + } |
|
| 167 | + $shortcode = strtoupper($shortcode); |
|
| 168 | + // add to array of registered shortcodes |
|
| 169 | + $this->registry->shortcodes->{$shortcode} = $shortcode_path . $shortcode_class . $shortcode_ext; |
|
| 170 | + return true; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * _initialize_shortcodes |
|
| 177 | + * allow shortcodes to set hooks for the rest of the system |
|
| 178 | + * |
|
| 179 | + * @access private |
|
| 180 | + * @return void |
|
| 181 | + */ |
|
| 182 | + public function addShortcodes() |
|
| 183 | + { |
|
| 184 | + // cycle thru shortcode folders |
|
| 185 | + foreach ($this->registry->shortcodes as $shortcode => $shortcode_path) { |
|
| 186 | + // add class prefix |
|
| 187 | + $shortcode_class = 'EES_' . $shortcode; |
|
| 188 | + // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system |
|
| 189 | + // which set hooks ? |
|
| 190 | + if (is_admin()) { |
|
| 191 | + // fire immediately |
|
| 192 | + call_user_func(array($shortcode_class, 'set_hooks_admin')); |
|
| 193 | + } else { |
|
| 194 | + // delay until other systems are online |
|
| 195 | + add_action( |
|
| 196 | + 'AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons', |
|
| 197 | + array($shortcode_class, 'set_hooks') |
|
| 198 | + ); |
|
| 199 | + // convert classname to UPPERCASE and create WP shortcode. |
|
| 200 | + $shortcode_tag = strtoupper($shortcode); |
|
| 201 | + // but first check if the shortcode has already |
|
| 202 | + // been added before assigning 'fallback_shortcode_processor' |
|
| 203 | + if ( ! shortcode_exists($shortcode_tag)) { |
|
| 204 | + // NOTE: this shortcode declaration will get overridden if the shortcode |
|
| 205 | + // is successfully detected in the post content in initializeShortcode() |
|
| 206 | + add_shortcode($shortcode_tag, array($shortcode_class, 'fallback_shortcode_processor')); |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * callback for the WP "get_header" hook point |
|
| 216 | + * checks posts for EE shortcodes, and initializes them, |
|
| 217 | + * then toggles filter switch that loads core default assets |
|
| 218 | + * |
|
| 219 | + * @param \WP_Query $wp_query |
|
| 220 | + * @return void |
|
| 221 | + */ |
|
| 222 | + public function initializeShortcodes(WP_Query $wp_query) |
|
| 223 | + { |
|
| 224 | + if (empty($this->registry->shortcodes) || ! $wp_query->is_main_query() || is_admin()) { |
|
| 225 | + return; |
|
| 226 | + } |
|
| 227 | + global $wp; |
|
| 228 | + /** @var EE_Front_controller $Front_Controller */ |
|
| 229 | + $Front_Controller = $this->registry->load_core('Front_Controller', array(), false); |
|
| 230 | + do_action('AHEE__EE_Front_Controller__initialize_shortcodes__begin', $wp, $Front_Controller); |
|
| 231 | + $Front_Controller->Request_Handler()->set_request_vars(); |
|
| 232 | + // grab post_name from request |
|
| 233 | + $current_post = apply_filters( |
|
| 234 | + 'FHEE__EE_Front_Controller__initialize_shortcodes__current_post_name', |
|
| 235 | + $Front_Controller->Request_Handler()->get('post_name') |
|
| 236 | + ); |
|
| 237 | + $show_on_front = get_option('show_on_front'); |
|
| 238 | + // if it's not set, then check if frontpage is blog |
|
| 239 | + if (empty($current_post)) { |
|
| 240 | + // yup.. this is the posts page, prepare to load all shortcode modules |
|
| 241 | + $current_post = 'posts'; |
|
| 242 | + // unless.. |
|
| 243 | + if ($show_on_front === 'page') { |
|
| 244 | + // some other page is set as the homepage |
|
| 245 | + $page_on_front = get_option('page_on_front'); |
|
| 246 | + if ($page_on_front) { |
|
| 247 | + // k now we need to find the post_name for this page |
|
| 248 | + global $wpdb; |
|
| 249 | + $page_on_front = $wpdb->get_var( |
|
| 250 | + $wpdb->prepare( |
|
| 251 | + "SELECT post_name from {$wpdb->posts} WHERE post_type='page' AND post_status NOT IN ('auto-draft', 'inherit', 'trash') AND ID=%d", |
|
| 252 | + $page_on_front |
|
| 253 | + ) |
|
| 254 | + ); |
|
| 255 | + // set the current post slug to what it actually is |
|
| 256 | + $current_post = $page_on_front ? $page_on_front : $current_post; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + // in case $current_post is hierarchical like: /parent-page/current-page |
|
| 261 | + $current_post = basename($current_post); |
|
| 262 | + if ($current_post && is_singular()) { |
|
| 263 | + global $wpdb; |
|
| 264 | + $post_content = $wpdb->get_var( |
|
| 265 | + $wpdb->prepare( |
|
| 266 | + "SELECT post_content from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND post_name=%s", |
|
| 267 | + $current_post |
|
| 268 | + ) |
|
| 269 | + ); |
|
| 270 | + $load_assets = $this->parseContentForShortcodes($post_content); |
|
| 271 | + } else { |
|
| 272 | + // initialize all legacy shortcodes |
|
| 273 | + $load_assets = $this->parseContentForShortcodes('', true); |
|
| 274 | + } |
|
| 275 | + if ($load_assets) { |
|
| 276 | + $this->registry->REQ->set_espresso_page(true); |
|
| 277 | + add_filter('FHEE_load_css', '__return_true'); |
|
| 278 | + add_filter('FHEE_load_js', '__return_true'); |
|
| 279 | + } |
|
| 280 | + do_action('AHEE__EE_Front_Controller__initialize_shortcodes__end', $Front_Controller); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * checks supplied content against list of legacy shortcodes, |
|
| 287 | + * then initializes any found shortcodes, and returns true. |
|
| 288 | + * returns false if no shortcodes found. |
|
| 289 | + * |
|
| 290 | + * @param string $content |
|
| 291 | + * @param bool $load_all if true, then ALL active legacy shortcodes will be initialized |
|
| 292 | + * @return bool |
|
| 293 | + */ |
|
| 294 | + public function parseContentForShortcodes($content = '', $load_all = false) |
|
| 295 | + { |
|
| 296 | + $has_shortcode = false; |
|
| 297 | + foreach ($this->registry->shortcodes as $shortcode_class => $shortcode) { |
|
| 298 | + if ($load_all || has_shortcode($content, $shortcode_class) ) { |
|
| 299 | + // load up the shortcode |
|
| 300 | + $this->initializeShortcode($shortcode_class); |
|
| 301 | + $has_shortcode = true; |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + return $has_shortcode; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * given a shortcode name, will instantiate the shortcode and call it's run() method |
|
| 311 | + * |
|
| 312 | + * @param string $shortcode_class |
|
| 313 | + * @param WP $wp |
|
| 314 | + */ |
|
| 315 | + public function initializeShortcode($shortcode_class = '', WP $wp = null) |
|
| 316 | + { |
|
| 317 | + // don't do anything if shortcode is already initialized |
|
| 318 | + if ( |
|
| 319 | + empty($this->registry->shortcodes->{$shortcode_class}) |
|
| 320 | + || ! is_string($this->registry->shortcodes->{$shortcode_class}) |
|
| 321 | + ) { |
|
| 322 | + return; |
|
| 323 | + } |
|
| 324 | + // let's pause to reflect on this... |
|
| 325 | + $sc_reflector = new ReflectionClass(LegacyShortcodesManager::addShortcodeClassPrefix($shortcode_class)); |
|
| 326 | + // ensure that class is actually a shortcode |
|
| 327 | + if ( |
|
| 328 | + defined('WP_DEBUG') |
|
| 329 | + && WP_DEBUG === true |
|
| 330 | + && ! $sc_reflector->isSubclassOf('EES_Shortcode') |
|
| 331 | + ) { |
|
| 332 | + EE_Error::add_error( |
|
| 333 | + sprintf( |
|
| 334 | + esc_html__( |
|
| 335 | + 'The requested %s shortcode is not of the class "EES_Shortcode". Please check your files.', |
|
| 336 | + 'event_espresso' |
|
| 337 | + ), |
|
| 338 | + $shortcode_class |
|
| 339 | + ), |
|
| 340 | + __FILE__, |
|
| 341 | + __FUNCTION__, |
|
| 342 | + __LINE__ |
|
| 343 | + ); |
|
| 344 | + add_filter('FHEE_run_EE_the_content', '__return_true'); |
|
| 345 | + return; |
|
| 346 | + } |
|
| 347 | + global $wp; |
|
| 348 | + // and pass the request object to the run method |
|
| 349 | + $this->registry->shortcodes->{$shortcode_class} = $sc_reflector->newInstance(); |
|
| 350 | + // fire the shortcode class's run method, so that it can activate resources |
|
| 351 | + $this->registry->shortcodes->{$shortcode_class}->run($wp); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * get classname, remove EES_prefix, and convert to UPPERCASE |
|
| 358 | + * |
|
| 359 | + * @param string $class_name |
|
| 360 | + * @return string |
|
| 361 | + */ |
|
| 362 | + public static function generateShortcodeTagFromClassName($class_name) |
|
| 363 | + { |
|
| 364 | + return strtoupper(str_replace('EES_', '', $class_name)); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * add EES_prefix and Capitalize words |
|
| 371 | + * |
|
| 372 | + * @param string $tag |
|
| 373 | + * @return string |
|
| 374 | + */ |
|
| 375 | + public static function generateShortcodeClassNameFromTag($tag) |
|
| 376 | + { |
|
| 377 | + // order of operation runs from inside to out |
|
| 378 | + // 5) maybe add prefix |
|
| 379 | + return LegacyShortcodesManager::addShortcodeClassPrefix( |
|
| 380 | + // 4) find spaces, replace with underscores |
|
| 381 | + str_replace( |
|
| 382 | + ' ', |
|
| 383 | + '_', |
|
| 384 | + // 3) capitalize first letter of each word |
|
| 385 | + ucwords( |
|
| 386 | + // 2) also change to lowercase so ucwords() will work |
|
| 387 | + strtolower( |
|
| 388 | + // 1) find underscores, replace with spaces so ucwords() will work |
|
| 389 | + str_replace( |
|
| 390 | + '_', |
|
| 391 | + ' ', |
|
| 392 | + $tag |
|
| 393 | + ) |
|
| 394 | + ) |
|
| 395 | + ) |
|
| 396 | + ) |
|
| 397 | + ); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * maybe add EES_prefix |
|
| 404 | + * |
|
| 405 | + * @param string $class_name |
|
| 406 | + * @return string |
|
| 407 | + */ |
|
| 408 | + public static function addShortcodeClassPrefix($class_name) |
|
| 409 | + { |
|
| 410 | + return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_' . $class_name; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * @return array |
|
| 417 | + */ |
|
| 418 | + public function getEspressoShortcodeTags() |
|
| 419 | + { |
|
| 420 | + static $shortcode_tags = array(); |
|
| 421 | + if (empty($shortcode_tags)) { |
|
| 422 | + $shortcode_tags = array_keys((array)$this->registry->shortcodes); |
|
| 423 | + } |
|
| 424 | + return $shortcode_tags; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * @param string $content |
|
| 431 | + * @return string |
|
| 432 | + */ |
|
| 433 | + public function doShortcode($content) |
|
| 434 | + { |
|
| 435 | + foreach ($this->getEspressoShortcodeTags() as $shortcode_tag) { |
|
| 436 | + if (strpos($content, $shortcode_tag) !== false) { |
|
| 437 | + $shortcode_class = LegacyShortcodesManager::generateShortcodeClassNameFromTag($shortcode_tag); |
|
| 438 | + $this->initializeShortcode($shortcode_class); |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + return do_shortcode($content); |
|
| 442 | + } |
|
| 443 | 443 | |
| 444 | 444 | |
| 445 | 445 | |
@@ -129,44 +129,44 @@ discard block |
||
| 129 | 129 | // remove last segment |
| 130 | 130 | array_pop($shortcode_path); |
| 131 | 131 | // glue it back together |
| 132 | - $shortcode_path = implode(DS, $shortcode_path) . DS; |
|
| 132 | + $shortcode_path = implode(DS, $shortcode_path).DS; |
|
| 133 | 133 | } else { |
| 134 | 134 | // we need to generate the filename based off of the folder name |
| 135 | 135 | // grab and sanitize shortcode directory name |
| 136 | 136 | $shortcode = sanitize_key(basename($shortcode_path)); |
| 137 | - $shortcode_path = rtrim($shortcode_path, DS) . DS; |
|
| 137 | + $shortcode_path = rtrim($shortcode_path, DS).DS; |
|
| 138 | 138 | } |
| 139 | 139 | // create classname from shortcode directory or file name |
| 140 | 140 | $shortcode = str_replace(' ', '_', ucwords(str_replace('_', ' ', $shortcode))); |
| 141 | 141 | // add class prefix |
| 142 | - $shortcode_class = 'EES_' . $shortcode; |
|
| 142 | + $shortcode_class = 'EES_'.$shortcode; |
|
| 143 | 143 | // does the shortcode exist ? |
| 144 | - if ( ! is_readable($shortcode_path . DS . $shortcode_class . $shortcode_ext)) { |
|
| 144 | + if ( ! is_readable($shortcode_path.DS.$shortcode_class.$shortcode_ext)) { |
|
| 145 | 145 | $msg = sprintf( |
| 146 | 146 | esc_html__( |
| 147 | 147 | 'The requested %s shortcode file could not be found or is not readable due to file permissions. It should be in %s', |
| 148 | 148 | 'event_espresso' |
| 149 | 149 | ), |
| 150 | 150 | $shortcode_class, |
| 151 | - $shortcode_path . DS . $shortcode_class . $shortcode_ext |
|
| 151 | + $shortcode_path.DS.$shortcode_class.$shortcode_ext |
|
| 152 | 152 | ); |
| 153 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 153 | + EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 154 | 154 | return false; |
| 155 | 155 | } |
| 156 | 156 | // load the shortcode class file |
| 157 | - require_once($shortcode_path . $shortcode_class . $shortcode_ext); |
|
| 157 | + require_once($shortcode_path.$shortcode_class.$shortcode_ext); |
|
| 158 | 158 | // verify that class exists |
| 159 | 159 | if ( ! class_exists($shortcode_class)) { |
| 160 | 160 | $msg = sprintf( |
| 161 | 161 | esc_html__('The requested %s shortcode class does not exist.', 'event_espresso'), |
| 162 | 162 | $shortcode_class |
| 163 | 163 | ); |
| 164 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 164 | + EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 165 | 165 | return false; |
| 166 | 166 | } |
| 167 | 167 | $shortcode = strtoupper($shortcode); |
| 168 | 168 | // add to array of registered shortcodes |
| 169 | - $this->registry->shortcodes->{$shortcode} = $shortcode_path . $shortcode_class . $shortcode_ext; |
|
| 169 | + $this->registry->shortcodes->{$shortcode} = $shortcode_path.$shortcode_class.$shortcode_ext; |
|
| 170 | 170 | return true; |
| 171 | 171 | } |
| 172 | 172 | |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | // cycle thru shortcode folders |
| 185 | 185 | foreach ($this->registry->shortcodes as $shortcode => $shortcode_path) { |
| 186 | 186 | // add class prefix |
| 187 | - $shortcode_class = 'EES_' . $shortcode; |
|
| 187 | + $shortcode_class = 'EES_'.$shortcode; |
|
| 188 | 188 | // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system |
| 189 | 189 | // which set hooks ? |
| 190 | 190 | if (is_admin()) { |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | $current_post |
| 268 | 268 | ) |
| 269 | 269 | ); |
| 270 | - $load_assets = $this->parseContentForShortcodes($post_content); |
|
| 270 | + $load_assets = $this->parseContentForShortcodes($post_content); |
|
| 271 | 271 | } else { |
| 272 | 272 | // initialize all legacy shortcodes |
| 273 | 273 | $load_assets = $this->parseContentForShortcodes('', true); |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | { |
| 296 | 296 | $has_shortcode = false; |
| 297 | 297 | foreach ($this->registry->shortcodes as $shortcode_class => $shortcode) { |
| 298 | - if ($load_all || has_shortcode($content, $shortcode_class) ) { |
|
| 298 | + if ($load_all || has_shortcode($content, $shortcode_class)) { |
|
| 299 | 299 | // load up the shortcode |
| 300 | 300 | $this->initializeShortcode($shortcode_class); |
| 301 | 301 | $has_shortcode = true; |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | */ |
| 408 | 408 | public static function addShortcodeClassPrefix($class_name) |
| 409 | 409 | { |
| 410 | - return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_' . $class_name; |
|
| 410 | + return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_'.$class_name; |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | |
@@ -419,7 +419,7 @@ discard block |
||
| 419 | 419 | { |
| 420 | 420 | static $shortcode_tags = array(); |
| 421 | 421 | if (empty($shortcode_tags)) { |
| 422 | - $shortcode_tags = array_keys((array)$this->registry->shortcodes); |
|
| 422 | + $shortcode_tags = array_keys((array) $this->registry->shortcodes); |
|
| 423 | 423 | } |
| 424 | 424 | return $shortcode_tags; |
| 425 | 425 | } |