Skip to content

Commit cefde48

Browse files
committed
Change method employed to return the entire shortcode unchanged for a less intrusive method using pre_do_shortcode_tag
1 parent 736f10a commit cefde48

File tree

1 file changed

+22
-41
lines changed

1 file changed

+22
-41
lines changed

syntaxhighlighter.php

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,24 @@ function output_shortcodes_for_tinymce() {
617617
}
618618

619619

620+
/**
621+
* Returns all shortcodes not handled by SyntaxHighlighter unchanged, so they
622+
* can be processed by their original handlers after SyntaxHighlighter has
623+
* run.
624+
*
625+
* @param mixed $output The shortcode's returned value (false by default).
626+
* @param string $tag The name of the shortcode.
627+
* @param array|null $attr The shortcode attributes.
628+
* @param array $m Regular expression match array.
629+
* @return string|false Return the matched shortcode as-is for all shortcodes not handled by SyntaxHighlighter, returns $output otherwise.
630+
*/
631+
function pre_do_shortcode_shortcode_hack_skip_others( $output, $tag, $attr, $m ) {
632+
if ( ! in_array( $tag, $this->shortcodes, true ) ) {
633+
return $m[0];
634+
}
635+
return $output;
636+
}
637+
620638
/**
621639
* Process only this plugin's shortcodes.
622640
*
@@ -670,7 +688,8 @@ function shortcode_hack( $content, $callback, $ignore_html = true ) {
670688

671689
// Register all other shortcodes, ensuring their content remains unchanged using yet another hack.
672690
foreach ( $orig_shortcode_tags as $shortcode_tagname => $shortcode ) {
673-
add_shortcode( $shortcode_tagname, array( $this, 'return_entire_shortcode_callback' ) );
691+
add_shortcode( $shortcode_tagname, '__return_empty_string' );
692+
add_filter( 'pre_do_shortcode_tag', array( $this, 'pre_do_shortcode_shortcode_hack_skip_others' ), 10, 4 );
674693
}
675694

676695
$regex = '/' . get_shortcode_regex() . '/';
@@ -697,8 +716,9 @@ function shortcode_hack( $content, $callback, $ignore_html = true ) {
697716
);
698717
}
699718

700-
// Put the original shortcodes back
719+
// Put the original shortcodes back, and remove the hacky pre_do_shortcode_tag filter
701720
$shortcode_tags = $orig_shortcode_tags;
721+
remove_filter('pre_do_shortcode_tag', array($this, 'pre_do_shortcode_shortcode_hack_skip_others'), 10);
702722

703723
return $content;
704724
}
@@ -754,45 +774,6 @@ function shortcode_hack_extra_escape_escaped_shortcodes_and_parse( $match ) {
754774
return do_shortcode_tag( $match );
755775
}
756776

757-
/**
758-
* Callback function to return the entire shortcode string as it appears in content.
759-
*
760-
* @param array $atts Array of attributes passed to the shortcode.
761-
* @param string|null $content Content enclosed between the opening and closing shortcode tags. Default is null.
762-
* @param string $tag Shortcode name/tag.
763-
*
764-
* @return string The complete shortcode string including the opening and closing tags, attributes, and content.
765-
*/
766-
function return_entire_shortcode_callback( $atts, $content = null, $tag = '' ) {
767-
$shortcode_string = '[' . $tag;
768-
769-
if ( ! empty( $atts ) ) {
770-
foreach ( $atts as $key => $value ) {
771-
if ( strpos( $value, "'" ) !== false && strpos( $value, '"' ) === false ) {
772-
// Use double quotes if value contains a single quote but not a double quote.
773-
$shortcode_string .= ' ' . $key . '="' . $value . '"';
774-
} elseif ( strpos( $value, '"' ) !== false && strpos( $value, "'" ) === false ) {
775-
// Use single quotes if value contains a double quote but not a single quote.
776-
$shortcode_string .= ' ' . $key . "='" . $value . "'";
777-
} elseif ( strpos( $value, "'" ) !== false && strpos( $value, '"' ) !== false ) {
778-
// If value contains both types of quotes, use double quotes and escape inner double quotes.
779-
$pattern = '/(?<!\\\\)"/'; // This pattern looks for double quotes that aren't preceded by a backslash.
780-
$escaped_value = preg_replace( $pattern, '\"', $content );
781-
$shortcode_string .= ' ' . $key . '="' . $escaped_value . '"';
782-
} else {
783-
$shortcode_string .= ' ' . $key . '="' . $value . '"';
784-
}
785-
}
786-
}
787-
788-
$shortcode_string .= ']';
789-
790-
if ( $content !== null ) {
791-
$shortcode_string .= $content . '[/' . $tag . ']';
792-
}
793-
return $shortcode_string;
794-
}
795-
796777
// The main filter for the post contents. The regular shortcode filter can't be used as it's post-wpautop().
797778
function parse_shortcodes( $content ) {
798779
return $this->shortcode_hack( $content, array( $this, 'shortcode_callback' ) );

0 commit comments

Comments
 (0)