$name, 'attrs' => $attrs, 'inner' => array(), 'open' => '', 'close' => '', 'html' => '', 'void' => false, ), $parts ); } /** * Markup kept as it is (already block markup, e.g. a "Gutenberg" element's content), cleaned by * WordPress's block-aware filter (block settings) and its post filter (the markup). * * @param string $markup Markup. * @return array */ public static function raw( $markup ) { return array( 'name' => '#raw', 'raw' => trim( wp_kses( filter_block_content( (string) $markup, 'post' ), self::allowed_html() ) ), ); } /** * Serialize a list of blocks. * * @param array[] $blocks Blocks. * @return string */ public static function serialize( array $blocks ) { $out = array(); foreach ( $blocks as $block ) { $out[] = self::serialize_block( $block ); } return implode( "\n\n", array_filter( $out, 'strlen' ) ); } /** * Serialize one block. * * @param array $block Block. * @return string */ public static function serialize_block( array $block ) { if ( '#raw' === $block['name'] ) { return $block['raw']; } $name = 0 === strpos( $block['name'], 'core/' ) ? substr( $block['name'], 5 ) : $block['name']; $attrs = self::attrs_json( $block['attrs'] ); $open = ''; } if ( $block['inner'] ) { $body = $block['open'] . self::serialize( $block['inner'] ) . $block['close']; } else { $body = $block['open'] . $block['html'] . $block['close']; } return $open . "-->\n" . $body . "\n'; } /** * Attributes as the editor writes them into the block comment. * * @param array $attrs Attributes. * @return string */ public static function attrs_json( array $attrs ) { $attrs = self::prune( $attrs ); if ( ! $attrs ) { return ''; } $json = json_encode( $attrs, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode -- must match serialize_block_attributes(). return str_replace( array( '--', '<', '>', '&', '\\"' ), array( '\\u002d\\u002d', '\\u003c', '\\u003e', '\\u0026', '\\u0022' ), (string) $json ); } /** * Remove empty values so no attribute is written as "" or []. * * @param array $attrs Attributes. * @return array */ private static function prune( array $attrs ) { foreach ( $attrs as $k => $v ) { if ( is_array( $v ) ) { $v = self::prune( $v ); $attrs[ $k ] = $v; } if ( '' === $v || null === $v || array() === $v ) { unset( $attrs[ $k ] ); } } return $attrs; } /* ----- Text ----- */ /** * A paragraph. * * @param string $html Inner HTML. * @param string $align left|center|right or empty. * @param array $extra className, anchor. * @return array */ public static function paragraph( $html, $align = '', array $extra = array() ) { $attrs = array(); $class = array(); if ( in_array( $align, array( 'left', 'center', 'right' ), true ) ) { $attrs['align'] = $align; $class[] = 'has-text-align-' . $align; } $attrs += self::extra_attrs( $extra ); $class = array_merge( $class, self::extra_class( $extra ) ); return self::make( 'core/paragraph', $attrs, array( 'html' => '' . self::inline( $html ) . '

' ) ); } /** * A heading. * * @param string $html Inner HTML. * @param int $level 1-6. * @param string $align left|center|right or empty. * @param array $extra className, anchor. * @return array */ public static function heading( $html, $level = 2, $align = '', array $extra = array() ) { $level = max( 1, min( 6, (int) $level ) ); $attrs = array(); $class = array( 'wp-block-heading' ); if ( 2 !== $level ) { $attrs['level'] = $level; } if ( in_array( $align, array( 'left', 'center', 'right' ), true ) ) { $attrs['textAlign'] = $align; $class[] = 'has-text-align-' . $align; } $attrs += self::extra_attrs( $extra ); $class = array_merge( $class, self::extra_class( $extra ) ); return self::make( 'core/heading', $attrs, array( 'html' => '' . self::inline( $html ) . '' ) ); } /** * A list. Each item is array( 'html' => inner HTML, 'sub' => a nested list block or null ). * * @param array[] $items Items. * @param bool $ordered Ordered. * @return array */ public static function lst( array $items, $ordered = false ) { $inner = array(); foreach ( $items as $item ) { $li = self::make( 'core/list-item' ); if ( ! empty( $item['sub'] ) ) { $li['open'] = '
  • ' . self::inline( $item['html'] ); $li['inner'] = array( $item['sub'] ); $li['close'] = '
  • '; } else { $li['html'] = '
  • ' . self::inline( $item['html'] ) . '
  • '; } $inner[] = $li; } $tag = $ordered ? 'ol' : 'ul'; return self::make( 'core/list', $ordered ? array( 'ordered' => true ) : array(), array( 'open' => '<' . $tag . ' class="wp-block-list">', 'inner' => $inner, 'close' => '', 'html' => '', ) ); } /** * A quote holding blocks. * * @param array[] $inner Blocks. * @return array */ public static function quote( array $inner ) { return self::make( 'core/quote', array(), array( 'open' => '
    ', 'inner' => $inner, 'close' => '
    ', ) ); } /* ----- Media ----- */ /** * An image from the media library. * * @param int $id Attachment ID (0 for an external image). * @param string $url Image URL. * @param array $opt alt, size, link, target, rel, caption, align, className, title. * @return array */ public static function image( $id, $url, array $opt = array() ) { $opt += array( 'alt' => '', 'size' => 'large', 'link' => '', 'target' => '', 'rel' => '', 'caption' => '', 'align' => '', 'className' => '', 'title' => '', ); $url = self::url( $url ); $opt['link'] = self::url( $opt['link'] ); $attrs = array(); $fclass = array( 'wp-block-image' ); if ( $id > 0 ) { $attrs['id'] = (int) $id; } if ( in_array( $opt['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true ) ) { $attrs['align'] = $opt['align']; $fclass[] = 'align' . $opt['align']; } if ( $id > 0 && '' !== $opt['size'] ) { $attrs['sizeSlug'] = $opt['size']; $fclass[] = 'size-' . $opt['size']; } $attrs['linkDestination'] = '' === $opt['link'] ? 'none' : 'custom'; if ( '' !== $opt['link'] && '' !== $opt['target'] ) { $attrs['linkTarget'] = $opt['target']; } if ( '' !== $opt['link'] && '' !== $opt['rel'] ) { $attrs['rel'] = $opt['rel']; } if ( '' !== $opt['className'] ) { $attrs['className'] = $opt['className']; $fclass[] = $opt['className']; } $img = '' . self::esc_attr( $opt['alt'] ) . ' 0 ? ' class="wp-image-' . (int) $id . '"' : '' ) . ( '' !== $opt['title'] ? ' title="' . self::esc_attr( $opt['title'] ) . '"' : '' ) . '/>'; if ( '' !== $opt['link'] ) { $img = '' . $img . ''; } $caption = self::inline( $opt['caption'] ); if ( '' !== $caption ) { $img .= '
    ' . $caption . '
    '; } return self::make( 'core/image', $attrs, array( 'html' => '
    ' . $img . '
    ' ) ); } /** * A gallery of image blocks. * * @param array[] $images Image blocks. * @param int $columns Columns (0 = automatic). * @return array */ public static function gallery( array $images, $columns = 0 ) { $attrs = array( 'linkTo' => 'none' ); if ( $columns > 0 ) { $attrs = array( 'columns' => min( 8, (int) $columns ) ) + $attrs; } $class = 'wp-block-gallery has-nested-images ' . ( $columns > 0 ? 'columns-' . min( 8, (int) $columns ) : 'columns-default' ) . ' is-cropped'; return self::make( 'core/gallery', $attrs, array( 'open' => '
    ', 'inner' => $images, 'close' => '
    ', ) ); } /** * An embed (YouTube, Vimeo or any oEmbed URL). * * @param string $url URL. * @return array */ public static function embed( $url ) { $url = self::url( $url ); $provider = ''; if ( preg_match( '#(youtube\.com|youtu\.be)#i', $url ) ) { $provider = 'youtube'; } elseif ( preg_match( '#vimeo\.com#i', $url ) ) { $provider = 'vimeo'; } $attrs = array( 'url' => $url ); $class = array( 'wp-block-embed' ); if ( '' !== $provider ) { $attrs += array( 'type' => 'video', 'providerNameSlug' => $provider, 'responsive' => true, 'className' => 'wp-embed-aspect-16-9 wp-has-aspect-ratio', ); $class = array( 'wp-block-embed', 'is-type-video', 'is-provider-' . $provider, 'wp-block-embed-' . $provider, 'wp-embed-aspect-16-9', 'wp-has-aspect-ratio' ); } return self::make( 'core/embed', $attrs, array( 'html' => '
    ' . "\n" . self::esc_html( $url ) . "\n" . '
    ' ) ); } /* ----- Design ----- */ /** * Buttons. * * @param array[] $buttons Button blocks. * @param string $justify left|center|right or empty. * @return array */ public static function buttons( array $buttons, $justify = '' ) { $attrs = array(); if ( in_array( $justify, array( 'left', 'center', 'right' ), true ) ) { $attrs['layout'] = array( 'type' => 'flex', 'justifyContent' => $justify, ); } return self::make( 'core/buttons', $attrs, array( 'open' => '
    ', 'inner' => $buttons, 'close' => '
    ', ) ); } /** * One button. * * @param string $text Inner HTML. * @param array $opt url, target, rel, title, background, color, radius, className. * @return array */ public static function button( $text, array $opt = array() ) { $opt += array( 'url' => '', 'target' => '', 'rel' => '', 'title' => '', 'background' => '', 'color' => '', 'radius' => '', 'className' => '', ); $opt['url'] = self::url( $opt['url'] ); foreach ( array( 'background', 'color', 'radius' ) as $k ) { $opt[ $k ] = self::css_value( $opt[ $k ] ); } $attrs = array(); $aclass = array( 'wp-block-button__link' ); $style = array(); if ( '' !== $opt['url'] ) { $attrs['url'] = $opt['url']; } if ( '' !== $opt['title'] ) { $attrs['title'] = $opt['title']; } if ( '' !== $opt['target'] ) { $attrs['linkTarget'] = $opt['target']; } if ( '' !== $opt['rel'] ) { $attrs['rel'] = $opt['rel']; } $st = array(); if ( '' !== $opt['color'] ) { $st['color']['text'] = $opt['color']; $aclass[] = 'has-text-color'; $style[] = 'color:' . $opt['color']; } if ( '' !== $opt['background'] ) { $st['color']['background'] = $opt['background']; $aclass[] = 'has-background'; $style[] = 'background-color:' . $opt['background']; } if ( '' !== $opt['radius'] ) { $st['border']['radius'] = $opt['radius']; $style[] = 'border-radius:' . $opt['radius']; } if ( $st ) { $attrs['style'] = $st; } $aclass[] = 'wp-element-button'; $wclass = array( 'wp-block-button' ); if ( '' !== $opt['className'] ) { $attrs['className'] = $opt['className']; $wclass[] = $opt['className']; } $a = '' . self::inline( $text ) . ''; return self::make( 'core/button', $attrs, array( 'html' => '
    ' . $a . '
    ' ) ); } /** * A separator. * * @param array $extra className. * @return array */ public static function separator( array $extra = array() ) { $attrs = self::extra_attrs( $extra ); $class = array_merge( array( 'wp-block-separator', 'has-alpha-channel-opacity' ), self::extra_class( $extra ) ); return self::make( 'core/separator', $attrs, array( 'html' => '' ) ); } /** * A spacer. * * @param string $height CSS length. * @return array */ public static function spacer( $height ) { $height = self::css_value( $height ); $height = '' === $height ? '32px' : $height; return self::make( 'core/spacer', array( 'height' => $height ), array( 'html' => '' ) ); } /** * Columns. * * @param array[] $columns Column blocks. * @param string $valign top|center|bottom or empty. * @param array $extra className, anchor, stack (false = keep side by side on phones). * @return array */ public static function columns( array $columns, $valign = '', array $extra = array() ) { $attrs = array(); $class = array( 'wp-block-columns' ); if ( in_array( $valign, array( 'top', 'center', 'bottom' ), true ) ) { $attrs['verticalAlignment'] = $valign; $class[] = 'are-vertically-aligned-' . $valign; } if ( isset( $extra['stack'] ) && false === $extra['stack'] ) { $attrs['isStackedOnMobile'] = false; $class[] = 'is-not-stacked-on-mobile'; } $attrs += self::extra_attrs( $extra ); $class = array_merge( $class, self::extra_class( $extra ) ); return self::make( 'core/columns', $attrs, array( 'open' => '', 'inner' => $columns, 'close' => '', ) ); } /** * A column. * * @param array[] $inner Blocks. * @param float|null $width Percent, or null. * @param array $style Block style attribute (see style_markup()). * @param array $extra className, anchor, valign. * @return array */ public static function column( array $inner, $width = null, array $style = array(), array $extra = array() ) { $attrs = array(); $class = array( 'wp-block-column' ); $css = array(); if ( ! empty( $extra['valign'] ) && in_array( $extra['valign'], array( 'top', 'center', 'bottom' ), true ) ) { $attrs['verticalAlignment'] = $extra['valign']; $class[] = 'is-vertically-aligned-' . $extra['valign']; } if ( null !== $width ) { $attrs['width'] = self::num( $width ) . '%'; $css[] = 'flex-basis:' . self::num( $width ) . '%'; } $style = self::clean_style( $style ); list( $sclass, $scss ) = self::style_markup( $style ); if ( $style ) { $attrs['style'] = $style; } $attrs += self::extra_attrs( $extra ); $class = array_merge( $class, $sclass, self::extra_class( $extra ) ); $css = array_merge( $css, $scss ); return self::make( 'core/column', $attrs, array( 'open' => '', 'inner' => $inner, 'close' => '', 'html' => '', ) ); } /** * A group. * * @param array[] $inner Blocks. * @param array $style Block style attribute. * @param array $extra align (full|wide), className, anchor, tagName. * @return array */ public static function group( array $inner, array $style = array(), array $extra = array() ) { $attrs = array(); $class = array( 'wp-block-group' ); $tag = ( ! empty( $extra['tagName'] ) && in_array( $extra['tagName'], array( 'section', 'header', 'footer', 'aside', 'main', 'article' ), true ) ) ? $extra['tagName'] : 'div'; if ( 'div' !== $tag ) { $attrs['tagName'] = $tag; } if ( ! empty( $extra['align'] ) && in_array( $extra['align'], array( 'full', 'wide' ), true ) ) { $attrs['align'] = $extra['align']; $class[] = 'align' . $extra['align']; } $style = self::clean_style( $style ); list( $sclass, $css ) = self::style_markup( $style ); if ( $style ) { $attrs['style'] = $style; } $attrs += self::extra_attrs( $extra ); $attrs['layout'] = array( 'type' => 'constrained' ); $class = array_merge( $class, $sclass, self::extra_class( $extra ) ); return self::make( 'core/group', $attrs, array( 'open' => '<' . $tag . self::id_attr( $extra ) . self::class_attr( $class ) . ( $css ? ' style="' . self::esc_attr( implode( ';', $css ) ) . '"' : '' ) . '>', 'inner' => $inner, 'close' => '', ) ); } /** * A details (show/hide) block. * * @param string $summary Summary HTML. * @param array[] $inner Blocks. * @param bool $open Open by default. * @return array */ public static function details( $summary, array $inner, $open = false ) { $attrs = array(); if ( $open ) { $attrs['showContent'] = true; } return self::make( 'core/details', $attrs, array( 'open' => '
    ' . self::inline( $summary ) . '', 'inner' => $inner ? $inner : array( self::paragraph( '' ) ), 'close' => '
    ', ) ); } /* ----- Code and fallbacks ----- */ /** * Custom HTML, cleaned by WordPress's own post filter: scripts, event handlers and other code a * post may not hold are removed, so a conversion never writes executable code into a page. * * @param string $html HTML. * @return array */ public static function html( $html ) { return self::make( 'core/html', array(), array( 'html' => trim( wp_kses( (string) $html, self::allowed_html() ) ) ) ); } /** * The tags a converted Custom HTML block may hold: those of a post, plus an embedded frame * (maps and videos pasted into WPBakery's Raw HTML element). * * @return array */ public static function allowed_html() { $tags = wp_kses_allowed_html( 'post' ); $tags['iframe'] = array( 'src' => true, 'width' => true, 'height' => true, 'title' => true, 'style' => true, 'loading' => true, 'allow' => true, 'allowfullscreen' => true, 'frameborder' => true, 'referrerpolicy' => true, ); return $tags; } /** * The page's design CSS (from WPBakery's design options) as a style block. Only CSS is kept: * anything that could close the style element or load or run code is removed. * * @param string[] $rules CSS rules. * @return array|null Null when nothing is left. */ public static function style( array $rules ) { $css = implode( "\n", array_unique( $rules ) ); $css = wp_strip_all_tags( $css ); $css = str_replace( array( '<', '\\' ), '', $css ); $css = preg_replace( '/@import[^;]*;?|expression\s*\(|javascript\s*:|behavior\s*:|-moz-binding\s*:/i', '', $css ); $css = trim( $css ); if ( '' === $css ) { return null; } return self::make( 'core/html', array(), array( 'html' => "" ) ); } /** * A shortcode kept as it is. * * @param string $text Shortcode text. * @return array */ public static function shortcode( $text ) { $text = trim( (string) $text ); if ( false !== strpos( $text, '<' ) ) { $text = trim( wp_kses( $text, 'post' ) ); } return self::make( 'core/shortcode', array(), array( 'html' => $text ) ); } /** * The "more" tag. * * @return array */ public static function more() { return self::make( 'core/more', array(), array( 'html' => '' ) ); } /* ----- Helpers ----- */ /** * The inner HTML of a text block (paragraph, heading, list item, caption, button, summary), cleaned * by WordPress's post filter: scripts, event handlers, javascript: URLs and other code a post may not * hold are removed, and inline CSS keeps only the properties and values WordPress allows in a post * (safecss_filter_attr), so a conversion never writes executable code or arbitrary CSS into a page or * its preview. * * @param string $html HTML. * @return string */ public static function inline( $html ) { return trim( wp_kses( (string) $html, 'post' ) ); } /** * A URL for an href or src, or empty when its protocol is not one WordPress allows (javascript:, * data:, vbscript: and the like). * * @param string $url URL. * @return string */ public static function url( $url ) { $url = trim( (string) $url ); if ( '' === $url ) { return ''; } $decoded = html_entity_decode( $url, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); return wp_kses_bad_protocol( $decoded, wp_allowed_protocols() ) === $decoded ? $url : ''; } /** * A single CSS value for an inline style (a colour, a length), or empty when it holds anything that * could end the declaration, load a resource or run code. * * @param string $value Value. * @return string */ public static function css_value( $value ) { $value = trim( (string) $value ); if ( '' === $value || ! preg_match( '/^[#a-zA-Z0-9.,%()\s\-]+$/', $value ) || preg_match( '/(url|expression|image|element|attr)\s*\(/i', $value ) ) { return ''; } return $value; } /** * A block style attribute with every value that is not a plain CSS value removed (see css_value()), * so the block comment and the inline style it produces hold the same, safe values. * * @param array $style Style attribute. * @return array */ public static function clean_style( array $style ) { foreach ( $style as $k => $v ) { if ( is_array( $v ) ) { $style[ $k ] = self::clean_style( $v ); } elseif ( '' === self::css_value( (string) $v ) ) { unset( $style[ $k ] ); } } return $style; } /** * Classes and inline CSS the editor writes for a block's style attribute. * * @param array $style Style attribute: color.background/text, spacing.padding/margin, border, dimensions.minHeight. * @return array{0:string[],1:string[]} */ public static function style_markup( array $style ) { $class = array(); $css = array(); if ( ! empty( $style['color']['text'] ) ) { $class[] = 'has-text-color'; $css[] = 'color:' . $style['color']['text']; } if ( ! empty( $style['color']['background'] ) ) { $class[] = 'has-background'; $css[] = 'background-color:' . $style['color']['background']; } if ( ! empty( $style['border']['color'] ) ) { $class[] = 'has-border-color'; $css[] = 'border-color:' . $style['border']['color']; } if ( ! empty( $style['border']['style'] ) ) { $css[] = 'border-style:' . $style['border']['style']; } if ( ! empty( $style['border']['width'] ) ) { $css[] = 'border-width:' . $style['border']['width']; } if ( ! empty( $style['border']['radius'] ) ) { $css[] = 'border-radius:' . $style['border']['radius']; } foreach ( array( 'margin', 'padding' ) as $box ) { foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { if ( isset( $style['spacing'][ $box ][ $side ] ) && '' !== $style['spacing'][ $box ][ $side ] ) { $css[] = $box . '-' . $side . ':' . $style['spacing'][ $box ][ $side ]; } } } if ( ! empty( $style['dimensions']['minHeight'] ) ) { $css[] = 'min-height:' . $style['dimensions']['minHeight']; } return array( $class, $css ); } /** * Attributes for className and anchor. * * @param array $extra Extra. * @return array */ private static function extra_attrs( array $extra ) { $attrs = array(); if ( ! empty( $extra['anchor'] ) ) { $attrs['anchor'] = (string) $extra['anchor']; } if ( ! empty( $extra['className'] ) ) { $attrs['className'] = trim( (string) $extra['className'] ); } return $attrs; } /** * The className as a list. * * @param array $extra Extra. * @return string[] */ private static function extra_class( array $extra ) { return empty( $extra['className'] ) ? array() : preg_split( '/\s+/', trim( (string) $extra['className'] ) ); } /** * An id="" attribute for an anchor. * * @param array $extra Extra. * @return string */ private static function id_attr( array $extra ) { return empty( $extra['anchor'] ) ? '' : ' id="' . self::esc_attr( $extra['anchor'] ) . '"'; } /** * A class="" attribute, or nothing. * * @param string[] $class Classes. * @return string */ private static function class_attr( array $class ) { $class = array_values( array_unique( array_filter( $class, 'strlen' ) ) ); return $class ? ' class="' . self::esc_attr( implode( ' ', $class ) ) . '"' : ''; } /** * A number without trailing zeros. * * @param float $n Number. * @return string */ public static function num( $n ) { $s = rtrim( rtrim( number_format( (float) $n, 2, '.', '' ), '0' ), '.' ); return '' === $s ? '0' : $s; } /** * Escape for an attribute (the editor's own escaping). * * @param string $text Text. * @return string */ public static function esc_attr( $text ) { return str_replace( array( '&', '"', '<', '>' ), array( '&', '"', '<', '>' ), html_entity_decode( (string) $text, ENT_QUOTES, 'UTF-8' ) ); } /** * Escape text. * * @param string $text Text. * @return string */ public static function esc_html( $text ) { return str_replace( array( '&', '<', '>' ), array( '&', '<', '>' ), (string) $text ); } }