'', 'title' => '', 'target' => '', 'rel' => '', ); $value = trim( (string) $value ); if ( '' === $value || '||' === $value ) { return $out; } if ( false === strpos( $value, 'url:' ) && false === strpos( $value, '|' ) ) { $out['url'] = $value; return $out; } foreach ( explode( '|', $value ) as $part ) { $colon = strpos( $part, ':' ); if ( false === $colon ) { continue; } $key = substr( $part, 0, $colon ); if ( array_key_exists( $key, $out ) ) { $out[ $key ] = trim( rawurldecode( substr( $part, $colon + 1 ) ) ); } } return $out; } /** * Raw HTML content: base64 of a URL-encoded string. Old or hand-written content may be plain HTML. * * @param string $content Content between the tags. * @return string */ public static function raw_html( $content ) { $content = trim( (string) $content ); $text = trim( preg_replace( '/<[^>]*>/', '', $content ) ); if ( '' !== $text && preg_match( '/^[A-Za-z0-9+\/=\s]+$/', $text ) ) { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- WPBakery's storage format. $decoded = base64_decode( preg_replace( '/\s+/', '', $text ), true ); if ( false !== $decoded && '' !== $decoded ) { return rawurldecode( $decoded ); } } return $content; } /** * A repeating group: URL-encoded JSON, a list of objects. * * @param string $value Value. * @return array[] */ public static function param_group( $value ) { $list = json_decode( urldecode( (string) $value ), true ); if ( ! is_array( $list ) ) { return array(); } return array_values( array_filter( $list, 'is_array' ) ); } /** * A "multi attribute" such as font_container: "tag:h2|font_size:32|text_align:center|color:%23333". * * @param string $value Value. * @return array */ public static function multi( $value ) { $out = array(); foreach ( explode( '|', (string) $value ) as $part ) { $colon = strpos( $part, ':' ); if ( false !== $colon ) { $out[ substr( $part, 0, $colon ) ] = rawurldecode( substr( $part, $colon + 1 ) ); } } return $out; } /** * Design Options: ".vc_custom_1699{padding-top: 40px !important;background-color: #f5f5f5 !important;}". * * @param string $value The css attribute. * @return array{class:string,decl:array} */ public static function css( $value ) { $out = array( 'class' => '', 'decl' => array(), ); if ( ! preg_match( '/^\s*\.([A-Za-z0-9_\-]+)\s*\{([^}]*)\}/', (string) $value, $m ) ) { return $out; } $out['class'] = $m[1]; // Split on ";" outside url(...), so a data URL or "?id=1;" never cuts a value. $decls = preg_split( '/;(?![^(]*\))/', $m[2] ); foreach ( $decls as $decl ) { $colon = strpos( $decl, ':' ); if ( false === $colon ) { continue; } $prop = strtolower( trim( substr( $decl, 0, $colon ) ) ); $val = trim( preg_replace( '/\s*!important\s*$/i', '', trim( substr( $decl, $colon + 1 ) ) ) ); if ( '' !== $prop && '' !== $val ) { $out['decl'][ $prop ] = $val; } } return $out; } /** * Expand "background" and "border" shorthands WPBakery writes into the longhands the converter maps. * * @param array $decl Declarations. * @return array */ public static function expand( array $decl ) { if ( isset( $decl['background'] ) ) { $bg = $decl['background']; unset( $decl['background'] ); if ( preg_match( '/url\(\s*[\'"]?([^\'")]+)[\'"]?\s*\)/i', $bg, $u ) ) { $decl += array( 'background-image' => 'url(' . $u[1] . ')' ); } if ( preg_match( '/(#[0-9a-f]{3,8}\b|rgba?\([^)]*\)|hsla?\([^)]*\))/i', $bg, $c ) ) { $decl += array( 'background-color' => $c[1] ); } foreach ( array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) as $r ) { if ( false !== stripos( $bg, $r ) ) { $decl += array( 'background-repeat' => $r ); break; } } } if ( isset( $decl['border-radius'] ) ) { $decl['border-radius'] = trim( $decl['border-radius'] ); } return $decl; } /** * A column width as a percentage: "1/2" => 50.0, "5/12" => 41.67; null when unset or broken. * * @param string $width Width. * @return float|null */ public static function width( $width ) { if ( ! preg_match( '#^\s*(\d+)\s*/\s*(\d+)\s*$#', (string) $width, $m ) || 0 === (int) $m[2] ) { return null; } $pct = round( 100 * (int) $m[1] / (int) $m[2], 2 ); return ( $pct > 0 && $pct <= 100 ) ? $pct : null; } /** * The responsive classes in a column's "offset": widths, offsets and hidden sizes per device. * * @param string $offset Offset attribute. * @return array{width:array,offset:array,hidden:string[]} */ public static function offset( $offset ) { $out = array( 'width' => array(), 'offset' => array(), 'hidden' => array(), ); foreach ( preg_split( '/\s+/', trim( (string) $offset ) ) as $class ) { if ( preg_match( '/^vc_col-(lg|md|sm|xs)-offset-(\d+)$/', $class, $m ) ) { $out['offset'][ $m[1] ] = round( 100 * (int) $m[2] / 12, 2 ); } elseif ( preg_match( '/^vc_col-(lg|md|sm|xs)-(\d+)\/5$/', $class, $m ) ) { $out['width'][ $m[1] ] = round( 20 * (int) $m[2], 2 ); } elseif ( preg_match( '/^vc_col-(lg|md|sm|xs)-(\d+)$/', $class, $m ) ) { $out['width'][ $m[1] ] = round( 100 * min( 12, (int) $m[2] ) / 12, 2 ); } elseif ( preg_match( '/^vc_hidden-(lg|md|sm|xs)$/', $class, $m ) ) { $out['hidden'][] = $m[1]; } } return $out; } /** * Comma-separated attachment IDs. * * @param string $value Value. * @return int[] */ public static function ids( $value ) { $ids = array(); foreach ( explode( ',', (string) $value ) as $id ) { $id = (int) trim( $id ); if ( $id > 0 ) { $ids[] = $id; } } return $ids; } /** * A CSS length WPBakery accepts without a unit ("32" means 32px). * * @param string $value Value. * @param string $unit Default unit. * @return string Empty when not a length. */ public static function length( $value, $unit = 'px' ) { $value = trim( (string) $value ); if ( preg_match( '/^-?\d+(\.\d+)?$/', $value ) ) { return $value . $unit; } if ( preg_match( '/^-?\d+(\.\d+)?(px|em|rem|%|vh|vw)$/', $value ) ) { return $value; } return ''; } /** * A colour safe to put into a style attribute, or empty. * * @param string $value Value. * @return string */ public static function color( $value ) { $value = trim( (string) $value ); if ( preg_match( '/^#[0-9a-fA-F]{3,8}$/', $value ) || preg_match( '/^(rgba?|hsla?)\([0-9.,%\s]+\)$/i', $value ) ) { return strtolower( $value ); } return ''; } }