@@ -348,12 +348,13 @@ function output_shortcodes_for_tinymce() {
348348 *
349349 * Phew!
350350 *
351- * @param string $content The post content.
352- * @param string $callback The callback function that should be used for add_shortcode()
351+ * @param string $content The post content.
352+ * @param string $callback The callback function that should be used for add_shortcode()
353+ * @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped.
353354 *
354355 * @return string The filtered content, with this plugin's shortcodes parsed.
355356 */
356- function shortcode_hack ( $ content , $ callback ) {
357+ function shortcode_hack ( $ content , $ callback, $ ignore_html = true ) {
357358 global $ shortcode_tags ;
358359
359360 // Regex is slow. Let's do some strpos() checks first.
@@ -370,15 +371,29 @@ function shortcode_hack( $content, $callback ) {
370371 add_shortcode ( $ shortcode , $ callback );
371372 }
372373
373- // Extra escape escaped shortcodes because do_shortcode() is going to strip a pair of square brackets when it runs
374- $ content = preg_replace_callback (
375- '/ ' . get_shortcode_regex ( $ this ->shortcodes ) . '/ ' ,
376- array ( $ this , 'shortcode_hack_extra_escape_escaped_shortcodes ' ),
377- $ content
378- );
374+ $ regex = '/ ' . get_shortcode_regex ( $ this ->shortcodes ) . '/ ' ;
379375
380- // Do the shortcodes (only this plugins's are registered)
381- $ content = do_shortcode ( $ content , true );
376+ // Parse the shortcodes (only this plugins's are registered)
377+ if ( $ ignore_html ) {
378+ // Extra escape escaped shortcodes because do_shortcode_tag() called by do_shortcode() is going to strip a pair of square brackets when it runs
379+ $ content = preg_replace_callback (
380+ $ regex ,
381+ array ( $ this , 'shortcode_hack_extra_escape_escaped_shortcodes ' ),
382+ $ content
383+ );
384+
385+ // Normal, safe parsing
386+ $ content = do_shortcode ( $ content , true );
387+ } else {
388+ // Extra escape escaped shortcodes because do_shortcode_tag() called by do_shortcode() is going to strip a pair of square brackets when it runs.
389+ // Then call do_shortcode_tag(). This is basically do_shortcode() without calling do_shortcodes_in_html_tags() which breaks things.
390+ // For context, see https://wordpress.org/support/topic/php-opening-closing-tags-break-code-blocks
391+ $ content = preg_replace_callback (
392+ $ regex ,
393+ array ( $ this , 'shortcode_hack_extra_escape_escaped_shortcodes_and_parse ' ),
394+ $ content
395+ );
396+ }
382397
383398 // Put the original shortcodes back
384399 $ shortcode_tags = $ orig_shortcode_tags ;
@@ -423,6 +438,20 @@ function shortcode_hack_extra_escape_escaped_shortcodes( $match ) {
423438 return $ match [0 ];
424439 }
425440
441+ /**
442+ * This is a combination of this class's shortcode_hack_extra_escape_escaped_shortcodes()
443+ * and do_shortcode_tag() for performance reasons so that we don't have to run some regex twice.
444+ *
445+ * @param array $match Regular expression match array.
446+ *
447+ * @return string|false False on failure, otherwise a parse shortcode tag.
448+ */
449+ function shortcode_hack_extra_escape_escaped_shortcodes_and_parse ( $ match ) {
450+ $ match [0 ] = $ this ->shortcode_hack_extra_escape_escaped_shortcodes ( $ match );
451+
452+ return do_shortcode_tag ( $ match );
453+ }
454+
426455
427456 // The main filter for the post contents. The regular shortcode filter can't be used as it's post-wpautop().
428457 function parse_shortcodes ( $ content ) {
@@ -432,7 +461,7 @@ function parse_shortcodes( $content ) {
432461
433462 // HTML entity encode the contents of shortcodes
434463 function encode_shortcode_contents ( $ content ) {
435- return $ this ->shortcode_hack ( $ content , array ( $ this , 'encode_shortcode_contents_callback ' ) );
464+ return $ this ->shortcode_hack ( $ content , array ( $ this , 'encode_shortcode_contents_callback ' ), false );
436465 }
437466
438467
@@ -465,7 +494,7 @@ function encode_shortcode_contents_slashed_noquickedit( $content ) {
465494
466495 // HTML entity decode the contents of shortcodes
467496 function decode_shortcode_contents ( $ content ) {
468- return $ this ->shortcode_hack ( $ content , array ( $ this , 'decode_shortcode_contents_callback ' ) );
497+ return $ this ->shortcode_hack ( $ content , array ( $ this , 'decode_shortcode_contents_callback ' ), false );
469498 }
470499
471500
0 commit comments