@@ -47,17 +47,18 @@ function baztag_func($atts, $content='') {
4747
4848$ shortcode_tags = array ();
4949
50- function add_shortcode ($ tag , $ func ) {
50+ function add_shortcode ($ tag , $ func, $ after_formatting = false ) {
5151 global $ shortcode_tags ;
5252
53- if ( is_callable ($ func ) )
54- $ shortcode_tags [$ tag ] = $ func ;
53+ if ( is_callable ($ func ) ) {
54+ $ shortcode_tags [($ after_formatting )? 11 :9 ][$ tag ] = $ func ;
55+ }
5556}
5657
5758function remove_shortcode ($ tag ) {
5859 global $ shortcode_tags ;
5960
60- unset($ shortcode_tags [$ tag ]);
61+ unset($ shortcode_tags [9 ][ $ tag ], $ shortcode_tags [ 11 ][ $ tag ]);
6162}
6263
6364function remove_all_shortcodes () {
@@ -66,32 +67,48 @@ function remove_all_shortcodes() {
6667 $ shortcode_tags = array ();
6768}
6869
69- function do_shortcode ($ content ) {
70+ function do_shortcode_after_formatting ($ content ) {
71+ return do_shortcode ($ content , true );
72+ }
73+ function do_shortcode ($ content , $ after_formatting = false ) {
74+ $ pattern = get_shortcode_regex ($ after_formatting );
75+ if (!$ pattern ) {
76+ return $ content ;
77+ } else {
78+ $ callback_func = 'do_shortcode_tag ' ;
79+ if ($ after_formatting )
80+ $ callback_func .= '_after_formatting ' ;
81+
82+ return preg_replace_callback ('/ ' . $ pattern . '/s ' , $ callback_func , $ content );
83+ }
84+ }
85+ function get_shortcode_regex ($ after_formatting ) {
7086 global $ shortcode_tags ;
7187
72- if (empty ($ shortcode_tags) || !is_array ($ shortcode_tags ))
73- return $ content ;
88+ if (empty ($ shortcode_tags[( $ after_formatting )? 11 : 9 ]) || !is_array ($ shortcode_tags[( $ after_formatting )? 11 : 9 ] ))
89+ return false ;
7490
75- $ tagnames = array_keys ($ shortcode_tags );
91+ $ tagnames = array_keys ($ shortcode_tags[( $ after_formatting )? 11 : 9 ] );
7692 $ tagregexp = join ( '| ' , array_map ('preg_quote ' , $ tagnames ) );
7793
78- $ pattern = '/\[( ' .$ tagregexp .')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?/s ' ;
79-
80- return preg_replace_callback ($ pattern , 'do_shortcode_tag ' , $ content );
94+ return '\[( ' .$ tagregexp .')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])? ' ;
8195}
8296
83- function do_shortcode_tag ($ m ) {
97+ function do_shortcode_tag_after_formatting ($ m ) {
98+ return do_shortcode_tag ($ m , true );
99+ }
100+ function do_shortcode_tag ($ m , $ after_formatting = false ) {
84101 global $ shortcode_tags ;
85102
86103 $ tag = $ m [1 ];
87104 $ attr = shortcode_parse_atts ($ m [2 ]);
88105
89106 if ( isset ($ m [4 ]) ) {
90107 // enclosing tag - extra parameter
91- return call_user_func ($ shortcode_tags [$ tag ], $ attr , $ m [4 ]);
108+ return call_user_func ($ shortcode_tags [( $ after_formatting )? 11 : 9 ][ $ tag ], $ attr , $ m [4 ]);
92109 } else {
93110 // self-closing tag
94- return call_user_func ($ shortcode_tags [$ tag ], $ attr );
111+ return call_user_func ($ shortcode_tags [( $ after_formatting )? 11 : 9 ][ $ tag ], $ attr );
95112 }
96113}
97114
@@ -129,6 +146,7 @@ function shortcode_atts($pairs, $atts) {
129146 return $ out ;
130147}
131148
132- add_filter ('the_content ' , 'do_shortcode ' , 11 ); // AFTER wpautop()
149+ add_filter ( 'the_content ' , 'do_shortcode ' , 9 );
150+ add_filter ( 'the_content ' , 'do_shortcode_after_formatting ' , 11 );
133151
134152?>
0 commit comments