classes.php
16 years ago
controller.php
16 years ago
formatting.php
16 years ago
functions.php
16 years ago
pipe.php
16 years ago
shortcodes.php
16 years ago
taggenerator.php
16 years ago
shortcodes.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | class WPCF7_ShortcodeManager { |
| 4 | |
| 5 | var $shortcode_tags = array(); |
| 6 | |
| 7 | // Taggs scanned at the last time of do_shortcode() |
| 8 | var $scanned_tags = null; |
| 9 | |
| 10 | // Executing shortcodes (true) or just scanning (false) |
| 11 | var $exec = true; |
| 12 | |
| 13 | function add_shortcode( $tag, $func, $has_name = false ) { |
| 14 | if ( is_callable( $func ) ) |
| 15 | $this->shortcode_tags[$tag] = array( |
| 16 | 'function' => $func, |
| 17 | 'has_name' => (boolean) $has_name ); |
| 18 | } |
| 19 | |
| 20 | function remove_shortcode( $tag ) { |
| 21 | unset( $this->shortcode_tags[$tag] ); |
| 22 | } |
| 23 | |
| 24 | function do_shortcode( $content, $exec = true ) { |
| 25 | $this->exec = (bool) $exec; |
| 26 | $this->scanned_tags = array(); |
| 27 | |
| 28 | if ( empty( $this->shortcode_tags ) || ! is_array( $this->shortcode_tags) ) |
| 29 | return $content; |
| 30 | |
| 31 | $pattern = $this->get_shortcode_regex(); |
| 32 | return preg_replace_callback( '/' . $pattern . '/s', |
| 33 | array(&$this, 'do_shortcode_tag'), $content ); |
| 34 | } |
| 35 | |
| 36 | function scan_shortcode( $content ) { |
| 37 | $this->do_shortcode( $content, false ); |
| 38 | return $this->scanned_tags; |
| 39 | } |
| 40 | |
| 41 | function get_shortcode_regex() { |
| 42 | $tagnames = array_keys( $this->shortcode_tags ); |
| 43 | $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) ); |
| 44 | |
| 45 | return '(\[?)\[(' . $tagregexp . ')(?:\s(.*?))?(?:\s(\/))?\](?:(.+?)\[\/\2\])?(\]?)'; |
| 46 | } |
| 47 | |
| 48 | function do_shortcode_tag( $m ) { |
| 49 | // allow [[foo]] syntax for escaping a tag |
| 50 | if ( $m[1] == '[' && $m[6] == ']' ) { |
| 51 | return substr( $m[0], 1, -1 ); |
| 52 | } |
| 53 | |
| 54 | $tag = $m[2]; |
| 55 | $attr = $this->shortcode_parse_atts( $m[3] ); |
| 56 | |
| 57 | $scanned_tag = array(); |
| 58 | $scanned_tag['type'] = $tag; |
| 59 | |
| 60 | if ( is_array( $attr ) ) { |
| 61 | if ( is_array( $attr['options'] ) ) { |
| 62 | if ( $this->shortcode_tags[$tag]['has_name'] && ! empty( $attr['options'] ) ) |
| 63 | $scanned_tag['name'] = array_shift( $attr['options'] ); |
| 64 | |
| 65 | $scanned_tag['options'] = (array) $attr['options']; |
| 66 | } |
| 67 | $scanned_tag['raw_values'] = (array) $attr['values']; |
| 68 | |
| 69 | if ( WPCF7_USE_PIPE ) { |
| 70 | $pipes = new WPCF7_Pipes( $scanned_tag['raw_values'] ); |
| 71 | $scanned_tag['values'] = $pipes->collect_befores(); |
| 72 | $scanned_tag['pipes'] = $pipes; |
| 73 | } else { |
| 74 | $scanned_tag['values'] = $scanned_tag['raw_values']; |
| 75 | } |
| 76 | |
| 77 | $scanned_tag['labels'] = $scanned_tag['values']; |
| 78 | |
| 79 | } else { |
| 80 | $scanned_tag['attr'] = $attr; |
| 81 | } |
| 82 | |
| 83 | $content = trim( $m[5] ); |
| 84 | $content = preg_replace( "/<br\s*\/?>$/m", '', $content ); |
| 85 | $scanned_tag['content'] = $content; |
| 86 | |
| 87 | $this->scanned_tags[] = $scanned_tag; |
| 88 | |
| 89 | $func = $this->shortcode_tags[$tag]['function']; |
| 90 | |
| 91 | if ( $this->exec ) { |
| 92 | $scanned_tag = apply_filters( 'wpcf7_form_tag', $scanned_tag ); |
| 93 | return $m[1] . call_user_func( $func, $scanned_tag ) . $m[6]; |
| 94 | } else { |
| 95 | return $m[0]; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | function shortcode_parse_atts( $text ) { |
| 100 | $atts = array( 'options' => array(), 'values' => array() ); |
| 101 | $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", " ", $text ); |
| 102 | $text = stripcslashes( trim( $text ) ); |
| 103 | |
| 104 | $pattern = '%^([-0-9a-zA-Z:.#_/|\s]*?)((?:\s*"[^"]*"|\s*\'[^\']*\')*)$%'; |
| 105 | |
| 106 | if ( preg_match( $pattern, $text, $match ) ) { |
| 107 | if ( ! empty( $match[1] ) ) { |
| 108 | $atts['options'] = preg_split( '/[\s]+/', trim( $match[1] ) ); |
| 109 | } |
| 110 | if ( ! empty( $match[2] ) ) { |
| 111 | preg_match_all( '/"[^"]*"|\'[^\']*\'/', $match[2], $matched_values ); |
| 112 | $atts['values'] = wpcf7_strip_quote_deep( $matched_values[0] ); |
| 113 | } |
| 114 | } else { |
| 115 | $atts = $text; |
| 116 | } |
| 117 | |
| 118 | return $atts; |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |
| 123 | $wpcf7_shortcode_manager = new WPCF7_ShortcodeManager(); |
| 124 | |
| 125 | function wpcf7_add_shortcode( $tag, $func, $has_name = false ) { |
| 126 | global $wpcf7_shortcode_manager; |
| 127 | |
| 128 | return $wpcf7_shortcode_manager->add_shortcode( $tag, $func, $has_name ); |
| 129 | } |
| 130 | |
| 131 | function wpcf7_remove_shortcode( $tag ) { |
| 132 | global $wpcf7_shortcode_manager; |
| 133 | |
| 134 | return $wpcf7_shortcode_manager->remove_shortcode( $tag ); |
| 135 | } |
| 136 | |
| 137 | function wpcf7_do_shortcode( $content ) { |
| 138 | global $wpcf7_shortcode_manager; |
| 139 | |
| 140 | return $wpcf7_shortcode_manager->do_shortcode( $content ); |
| 141 | } |
| 142 | |
| 143 | function wpcf7_get_shortcode_regex() { |
| 144 | global $wpcf7_shortcode_manager; |
| 145 | |
| 146 | return $wpcf7_shortcode_manager->get_shortcode_regex(); |
| 147 | } |
| 148 | |
| 149 | ?> |