'text', 'text' => string ) * shortcode: array( 'type' => 'shortcode', 'tag' => string, 'atts' => array, 'children' => node[], * 'inner' => string|null (raw text between the tags; null when self-closing), * 'raw' => string (the whole shortcode as written) ) * * @package Bakeout */ namespace Bakeout; defined( 'ABSPATH' ) || exit; final class Parser { /** * Tags whose content is HTML for the reader, not more layout. Their inner text is kept as written and * not split into children (a text block may hold "[1]" or a contact form shortcode). * * @var string[] */ /** * Layout tags that hold other elements. Left open by a broken page, they close where their parent * closes (instead of losing their content), which is how the page reads to a person. * * @var string[] */ const CONTAINER_TAGS = array( 'vc_column', 'vc_column_inner', 'vc_row_inner', 'vc_section', 'vc_tta_section', 'vc_tab', 'vc_accordion_tab', 'vc_tta_tabs', 'vc_tta_accordion', 'vc_tta_tour', 'vc_tta_pageable', 'vc_tta_toggle', 'vc_tabs', 'vc_tour', 'vc_accordion' ); const TEXT_TAGS = array( 'vc_column_text', 'vc_message', 'vc_toggle', 'vc_cta', 'vc_raw_html', 'vc_raw_js', 'vc_wp_text', 'vc_gutenberg', 'vc_btn', 'vc_hoverbox', 'vc_pricing_table' ); /** * Parse content into a list of nodes. * * @param string $content Post content. * @return array[] */ public static function parse( $content ) { $content = (string) $content; $pattern = '/\[(\[?)(\/?)([A-Za-z_][\w\-]*)((?:[^\[\]"\']|"[^"]*"|\'[^\']*\')*?)(\/?)\](\]?)/s'; if ( ! preg_match_all( $pattern, $content, $m, PREG_SET_ORDER | PREG_OFFSET_CAPTURE ) ) { return '' === $content ? array() : array( self::text( $content ) ); } $root = array( 'type' => 'shortcode', 'tag' => '#root', 'atts' => array(), 'children' => array(), 'start' => 0, 'open_end' => 0, ); $stack = array( $root ); $pos = 0; $skip = -1; // Offset up to which a text tag's content has been taken whole. foreach ( $m as $match ) { $offset = $match[0][1]; $len = strlen( $match[0][0] ); if ( $offset < $skip ) { continue; } if ( $offset > $pos ) { self::append( $stack, self::text( substr( $content, $pos, $offset - $pos ) ) ); } $pos = $offset + $len; // [[tag]] is an escaped shortcode: shown as "[tag]". if ( '[' === $match[1][0] && ']' === $match[6][0] ) { self::append( $stack, self::text( substr( $match[0][0], 1, -1 ) ) ); continue; } $tag = strtolower( $match[3][0] ); if ( '/' === $match[2][0] ) { self::close( $stack, $tag, $content, $offset, $pos ); continue; } $node = array( 'type' => 'shortcode', 'tag' => $tag, 'atts' => self::atts( $match[4][0] ), 'children' => array(), 'inner' => null, 'raw' => $match[0][0], 'start' => $offset, 'open_end' => $pos, ); if ( '/' === $match[5][0] ) { self::append( $stack, self::finish( $node ) ); continue; } if ( in_array( $tag, self::TEXT_TAGS, true ) ) { // Take everything up to the matching close tag as text, if there is one. $close = self::find_close( $content, $tag, $pos ); if ( null !== $close ) { $node['inner'] = substr( $content, $pos, $close[0] - $pos ); $node['raw'] = substr( $content, $offset, $close[1] - $offset ); $node['children'] = '' === $node['inner'] ? array() : array( self::text( $node['inner'] ) ); self::append( $stack, self::finish( $node ) ); $pos = $close[1]; $skip = $close[1]; continue; } } $stack[] = $node; } if ( $pos < strlen( $content ) ) { self::append( $stack, self::text( substr( $content, $pos ) ) ); } // Anything left open was self-closing after all (or, for layout tags, closes at the end). while ( count( $stack ) > 1 ) { self::unwind_one( $stack, $content, strlen( $content ) ); } return $stack[0]['children']; } /** * Parse a shortcode's attribute text the way WordPress does, then undo WPBakery's own escaping. * * @param string $text Attribute text. * @return array */ public static function atts( $text ) { $atts = array(); $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', (string) $text ); $re = '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|\'([^\']*)\'(?:\s|$)|(\S+)(?:\s|$)/'; if ( preg_match_all( $re, (string) $text, $all, PREG_SET_ORDER ) ) { foreach ( $all as $a ) { if ( ! empty( $a[1] ) ) { $atts[ strtolower( $a[1] ) ] = Decode::safe( stripcslashes( $a[2] ) ); } elseif ( ! empty( $a[3] ) ) { $atts[ strtolower( $a[3] ) ] = Decode::safe( stripcslashes( $a[4] ) ); } elseif ( ! empty( $a[5] ) ) { $atts[ strtolower( $a[5] ) ] = Decode::safe( stripcslashes( $a[6] ) ); } } } return $atts; } /** * Visit every shortcode node, depth first. * * @param array[] $nodes Nodes. * @param callable $fn Called with each shortcode node. * @return void */ public static function walk( array $nodes, $fn ) { foreach ( $nodes as $node ) { if ( 'shortcode' !== $node['type'] ) { continue; } $fn( $node ); if ( ! in_array( $node['tag'], self::TEXT_TAGS, true ) ) { self::walk( $node['children'], $fn ); } else { // Shortcodes inside a text block still count (for the scan). self::walk( self::parse( (string) $node['inner'] ), $fn ); } } } /** * A text node. * * @param string $text Text. * @return array */ private static function text( $text ) { return array( 'type' => 'text', 'text' => $text, ); } /** * Add a node to the node on top of the stack, merging adjacent text. * * @param array[] $stack Stack (by reference). * @param array $node Node. * @return void */ private static function append( array &$stack, array $node ) { $top = count( $stack ) - 1; $children = &$stack[ $top ]['children']; $last = count( $children ) - 1; if ( 'text' === $node['type'] && $last >= 0 && 'text' === $children[ $last ]['type'] ) { $children[ $last ]['text'] .= $node['text']; return; } $children[] = $node; } /** * Close the innermost open tag with this name; tags opened after it were self-closing. * * @param array[] $stack Stack (by reference). * @param string $tag Tag. * @param string $content Whole content. * @param int $offset Start of the closing tag. * @param int $end End of the closing tag. * @return void */ private static function close( array &$stack, $tag, $content, $offset, $end ) { for ( $i = count( $stack ) - 1; $i >= 1; $i-- ) { if ( $stack[ $i ]['tag'] === $tag ) { while ( count( $stack ) - 1 > $i ) { self::unwind_one( $stack, $content, $offset ); } $node = array_pop( $stack ); $node['inner'] = substr( $content, $node['open_end'], $offset - $node['open_end'] ); $node['raw'] = substr( $content, $node['start'], $end - $node['start'] ); self::append( $stack, self::finish( $node ) ); return; } } // A closing tag nobody opened: keep it as text. self::append( $stack, self::text( substr( $content, $offset, $end - $offset ) ) ); } /** * Pop the top node that was never closed. A layout tag closes here, keeping its children; any * other tag was self-closing, so its children become its following siblings. * * @param array[] $stack Stack (by reference). * @param string $content Whole content. * @param int $at Where the enclosing tag closes. * @return void */ private static function unwind_one( array &$stack, $content, $at ) { $node = array_pop( $stack ); if ( in_array( $node['tag'], self::CONTAINER_TAGS, true ) ) { $node['inner'] = substr( $content, $node['open_end'], $at - $node['open_end'] ); $node['raw'] = substr( $content, $node['start'], $at - $node['start'] ); self::append( $stack, self::finish( $node ) ); return; } $children = $node['children']; $node['children'] = array(); $node['inner'] = null; self::append( $stack, self::finish( $node ) ); foreach ( $children as $child ) { self::append( $stack, $child ); } } /** * Find the matching close tag of a text tag, allowing nested tags of the same name. * * @param string $content Content. * @param string $tag Tag. * @param int $from Offset after the opening tag. * @return int[]|null Start and end offsets of the closing tag. */ private static function find_close( $content, $tag, $from ) { $depth = 1; $re = '/\[(\/?)' . preg_quote( $tag, '/' ) . '(?=[\s\]\/])(?:[^\[\]"\']|"[^"]*"|\'[^\']*\')*?(\/?)\]/i'; $at = $from; while ( preg_match( $re, $content, $m, PREG_OFFSET_CAPTURE, $at ) ) { $at = $m[0][1] + strlen( $m[0][0] ); if ( '/' === $m[1][0] ) { --$depth; if ( 0 === $depth ) { return array( $m[0][1], $at ); } } elseif ( '/' !== $m[2][0] ) { ++$depth; } } return null; } /** * Drop the parse-time offsets. * * @param array $node Node. * @return array */ private static function finish( array $node ) { unset( $node['start'], $node['open_end'] ); return $node; } }