[44] | 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'] ) && ! empty( $attr['options'] ) ) {
|
---|
| 62 | if ( $this->shortcode_tags[$tag]['has_name'] )
|
---|
| 63 | $scanned_tag['name'] = array_shift( $attr['options'] );
|
---|
| 64 | $scanned_tag['options'] = $attr['options'];
|
---|
| 65 | }
|
---|
| 66 | $scanned_tag['raw_values'] = (array) $attr['values'];
|
---|
| 67 |
|
---|
| 68 | if ( WPCF7_USE_PIPE ) {
|
---|
| 69 | $pipes = new WPCF7_Pipes( $scanned_tag['raw_values'] );
|
---|
| 70 | $scanned_tag['values'] = $pipes->collect_befores();
|
---|
| 71 | $scanned_tag['pipes'] = $pipes;
|
---|
| 72 | } else {
|
---|
| 73 | $scanned_tag['values'] = $scanned_tag['raw_values'];
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | } else {
|
---|
| 77 | $scanned_tag['attr'] = $attr;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | $content = trim( $m[5] );
|
---|
| 81 | $content = preg_replace( "/<br\s*\/?>$/m", '', $content );
|
---|
| 82 | $scanned_tag['content'] = $content;
|
---|
| 83 |
|
---|
| 84 | $this->scanned_tags[] = $scanned_tag;
|
---|
| 85 |
|
---|
| 86 | $func = $this->shortcode_tags[$tag]['function'];
|
---|
| 87 |
|
---|
| 88 | if ( $this->exec ) {
|
---|
| 89 | $scanned_tag = apply_filters( 'wpcf7_form_tag', $scanned_tag );
|
---|
| 90 | return $m[1] . call_user_func( $func, $scanned_tag ) . $m[6];
|
---|
| 91 | } else {
|
---|
| 92 | return $m[0];
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | function shortcode_parse_atts( $text ) {
|
---|
| 97 | $atts = array();
|
---|
| 98 | $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", " ", $text );
|
---|
| 99 | $text = stripcslashes( trim( $text ) );
|
---|
| 100 |
|
---|
| 101 | $pattern = '%^([-0-9a-zA-Z:.#_/|\s]*?)((?:\s*"[^"]*"|\s*\'[^\']*\')*)$%';
|
---|
| 102 |
|
---|
| 103 | if ( preg_match( $pattern, $text, $match ) ) {
|
---|
| 104 | if ( ! empty( $match[1] ) ) {
|
---|
| 105 | $atts['options'] = preg_split( '/[\s]+/', trim( $match[1] ) );
|
---|
| 106 | }
|
---|
| 107 | if ( ! empty( $match[2] ) ) {
|
---|
| 108 | preg_match_all( '/"[^"]*"|\'[^\']*\'/', $match[2], $matched_values );
|
---|
| 109 | $atts['values'] = wpcf7_strip_quote_deep( $matched_values[0] );
|
---|
| 110 | }
|
---|
| 111 | } else {
|
---|
| 112 | $atts = $text;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | return $atts;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | $wpcf7_shortcode_manager = new WPCF7_ShortcodeManager();
|
---|
| 121 |
|
---|
| 122 | function wpcf7_add_shortcode( $tag, $func, $has_name = false ) {
|
---|
| 123 | global $wpcf7_shortcode_manager;
|
---|
| 124 |
|
---|
| 125 | return $wpcf7_shortcode_manager->add_shortcode( $tag, $func, $has_name );
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | function wpcf7_remove_shortcode( $tag ) {
|
---|
| 129 | global $wpcf7_shortcode_manager;
|
---|
| 130 |
|
---|
| 131 | return $wpcf7_shortcode_manager->remove_shortcode( $tag );
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | function wpcf7_do_shortcode( $content ) {
|
---|
| 135 | global $wpcf7_shortcode_manager;
|
---|
| 136 |
|
---|
| 137 | return $wpcf7_shortcode_manager->do_shortcode( $content );
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | function wpcf7_get_shortcode_regex() {
|
---|
| 141 | global $wpcf7_shortcode_manager;
|
---|
| 142 |
|
---|
| 143 | return $wpcf7_shortcode_manager->get_shortcode_regex();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | ?> |
---|