Skip to content

Commit ff1e64c

Browse files
committed
Like do_shortcode() does, check to see if any of the plugin's shortcodes are in use before running the post content through regex.
1 parent 583c9e6 commit ff1e64c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

syntaxhighlighter.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,19 @@ function output_shortcodes_for_tinymce() {
348348
*
349349
* Phew!
350350
*
351-
* @param string $content The post content.
351+
* @param string $content The post content.
352352
* @param string $callback The callback function that should be used for add_shortcode()
353353
*
354354
* @return string The filtered content, with this plugin's shortcodes parsed.
355355
*/
356356
function shortcode_hack( $content, $callback ) {
357357
global $shortcode_tags;
358358

359+
// Regex is slow. Let's do some strpos() checks first.
360+
if ( ! $this->string_has_shortcodes( $content, $this->shortcodes ) ) {
361+
return $content;
362+
}
363+
359364
// Backup current registered shortcodes and clear them all out
360365
$orig_shortcode_tags = $shortcode_tags;
361366
remove_all_shortcodes();
@@ -382,6 +387,26 @@ function shortcode_hack( $content, $callback ) {
382387
}
383388

384389

390+
/**
391+
* A quick checker to see if any of this plugin's shortcodes are in use in a string.
392+
* Since all of the tags can't be self-closing, we look for the closing tag.
393+
*
394+
* @param string $string The string to look through. This is a post's contents usually.
395+
* @param array $shortcodes The array of shortcodes to look for.
396+
*
397+
* @return bool Whether any shortcode usage was found.
398+
*/
399+
function string_has_shortcodes( $string, $shortcodes ) {
400+
foreach ( $shortcodes as $shortcode ) {
401+
if ( false !== strpos( $string, "[/{$shortcode}]" ) ) {
402+
return true;
403+
}
404+
}
405+
406+
return false;
407+
}
408+
409+
385410
/**
386411
* Add extra square brackets around escaped shortcodes.
387412
* This is to counteract the beginning of the do_shortcode_tag() function.

0 commit comments

Comments
 (0)