Make WordPress Core

Ticket #14050: plugin.php

File plugin.php, 3.3 KB (added by lkraav, 13 years ago)

Drop-in plugin v1 to demonstrate and test current patchwork

Line 
1<?php
2/*
3Plugin Name: WordPress Bug #14050 Hotfix
4Plugin URI: http://core.trac.wordpress.org/ticket/14050
5Description: Improves the big monster shortcode_unautop() regular expression, which is causing all the ruckus
6Author: Leho Kraav
7Author URI: http://leho.kraav.com
8Version: 1.0
9*/
10
11if ( is_admin() ) return;
12
13/**
14 * Don't auto-p wrap shortcodes that stand alone
15 *
16 * Ensures that shortcodes are not wrapped in <<p>>...<</p>>.
17 *
18 * @since 2.9.0
19 *
20 * @param string $pee The content.
21 * @return string The filtered content.
22 */
23function wp14050_shortcode_unautop( $pee ) {
24 global $shortcode_tags;
25
26 if ( empty( $shortcode_tags ) || !is_array( $shortcode_tags ) ) {
27 return $pee;
28 }
29
30 $tagregexp = join( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
31
32 $pattern =
33 '/'
34 . '<p>' // Opening paragraph
35 . '\\s*+' // Optional leading whitespace
36 . '(' // 1: The shortcode
37 . '\\[\\/?' // Opening bracket for opening or closing shortcode tag
38 . "($tagregexp)" // 2: Shortcode name
39 . '(?![\\w-])' // Not followed by word character or hyphen
40 // Unroll the loop: Inside the opening shortcode tag
41 . '[^\\]\\/]*' // Not a closing bracket or forward slash
42 . '(?:'
43 . '\\/(?!\\])' // A forward slash not followed by a closing bracket
44 . '[^\\]\\/]*' // Not a closing bracket or forward slash
45 . ')*?'
46 . '[\\w\\s="\']*' // Shortcode attributes
47 . '(?:'
48 . '\\s*+' // Optional leading whitespace, supports [footag /]
49 . '\\/\\]' // Self closing tag and closing bracket
50 . '|'
51 . '\\]' // Closing bracket
52 . '(?:' // Unroll the loop: Optionally, anything between the opening and closing shortcode tags
53 . '(?!<\/p>)' // Not followed by closing paragraph
54 . '[^\\[]*+' // Not an opening bracket, matches all content between closing bracket and closing shortcode tag
55 . '(?:'
56 . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
57 . '[^\\[]*+' // Not an opening bracket
58 . ')*+'
59 . '\\[\\/\\2\\]' // Closing shortcode tag
60 . ')?'
61 . ')'
62 . ')'
63 . '\\s*+' // optional trailing whitespace
64 . '<\\/p>' // closing paragraph
65 . '/s';
66
67 return preg_replace( $pattern, '$1', $pee );
68}
69
70# wp-includes/default-filters.php
71foreach ( array( "term_description" ) as $filter ) {
72 remove_filter( $filter, "shortcode_unautop" );
73 add_filter( $filter, "wp14050_shortcode_unautop" );
74}
75
76remove_filter( "the_content", "shortcode_unautop" );
77add_filter( "the_content", "wp14050_shortcode_unautop" );
78
79remove_filter( "the_excerpt", "shortcode_unautop" );
80add_filter( "the_excerpt", "wp14050_shortcode_unautop" );