,background_image:string} */ public static function map( $css_attr, Context $ctx, array $allow = array( 'padding', 'margin', 'background', 'border' ) ) { $out = array( 'style' => array(), 'className' => '', 'leftover' => array(), 'background_image' => '', ); $parsed = Decode::css( $css_attr ); if ( ! $parsed['decl'] ) { return $out; } $decl = Decode::expand( $parsed['decl'] ); $left = array(); foreach ( $decl as $prop => $val ) { $taken = false; if ( preg_match( '/^padding-(top|right|bottom|left)$/', $prop, $m ) && in_array( 'padding', $allow, true ) ) { $len = Decode::length( $val ); if ( '' !== $len ) { $out['style']['spacing']['padding'][ $m[1] ] = $len; $taken = true; } } elseif ( preg_match( '/^margin-(top|bottom)$/', $prop, $m ) && in_array( 'margin', $allow, true ) ) { $len = Decode::length( $val ); if ( '' !== $len ) { $out['style']['spacing']['margin'][ $m[1] ] = $len; $taken = true; } } elseif ( 'background-color' === $prop && in_array( 'background', $allow, true ) ) { $color = Decode::color( $val ); if ( '' !== $color && 'transparent' !== $color ) { $out['style']['color']['background'] = $color; $taken = true; } } elseif ( $ctx->pro && in_array( 'border', $allow, true ) && self::border( $prop, $val, $out['style'] ) ) { $taken = true; } elseif ( 'background-image' === $prop && preg_match( '/url\(\s*[\'"]?([^\'")]+)[\'"]?\s*\)/i', $val, $u ) ) { $out['background_image'] = $u[1]; } if ( ! $taken ) { $left[ $prop ] = $val; } } $out['leftover'] = $left; return $out; } /** * Map one border declaration onto the style attribute when all four sides agree. * * @param string $prop Property. * @param string $val Value. * @param array $style Style (by reference). * @return bool Taken. */ private static function border( $prop, $val, array &$style ) { if ( preg_match( '/^border-(top|right|bottom|left)-(width|style|color)$/', $prop, $m ) ) { // Per-side borders are collected and only used when every side matches. $style['_sides'][ $m[2] ][ $m[1] ] = $val; return self::settle_sides( $style, $m[2] ); } if ( 'border-radius' === $prop ) { $len = Decode::length( $val ); if ( '' !== $len ) { $style['border']['radius'] = $len; return true; } } if ( in_array( $prop, array( 'border-width', 'border-style', 'border-color' ), true ) ) { $part = substr( $prop, 7 ); $v = 'color' === $part ? Decode::color( $val ) : ( 'width' === $part ? Decode::length( $val ) : ( preg_match( '/^(solid|dashed|dotted|double|groove|ridge|inset|outset|none)$/', $val ) ? $val : '' ) ); if ( '' !== $v ) { $style['border'][ $part ] = $v; return true; } } return false; } /** * Once all four sides of a border part are known and equal, use one value. * * @param array $style Style (by reference). * @param string $part width|style|color. * @return bool */ private static function settle_sides( array &$style, $part ) { $sides = $style['_sides'][ $part ]; if ( 4 === count( $sides ) && 1 === count( array_unique( $sides ) ) ) { $v = reset( $sides ); $v = 'color' === $part ? Decode::color( $v ) : ( 'width' === $part ? Decode::length( $v ) : $v ); if ( '' !== $v ) { $style['border'][ $part ] = $v; } unset( $style['_sides'][ $part ] ); if ( ! $style['_sides'] ) { unset( $style['_sides'] ); } return '' !== $v; } return true; // Held until the other sides arrive. } /** * Remove the working keys left in a style array. * * @param array $style Style. * @param array $left Leftover declarations (by reference): unmatched border sides go here. * @return array */ public static function clean( array $style, array &$left ) { if ( isset( $style['_sides'] ) ) { foreach ( $style['_sides'] as $part => $sides ) { foreach ( $sides as $side => $v ) { $left[ 'border-' . $side . '-' . $part ] = $v; } } unset( $style['_sides'] ); } return $style; } /** * Add declarations as page CSS on a fresh class. * * @param Context $ctx Context. * @param array $decl Declarations. * @return string Class name. */ public static function page_css( Context $ctx, array $decl ) { $class = $ctx->uid(); $rules = array(); foreach ( $decl as $p => $v ) { $v = str_replace( array( '<', '>', '{', '}' ), '', $v ); if ( preg_match( '/^[a-z\-]+$/', $p ) ) { $rules[] = $p . ':' . $v; } } if ( $rules ) { $ctx->css[] = '.' . $class . '{' . implode( ';', $rules ) . '}'; } return $class; } /** * Run map(), settle the border sides and (Pro) keep what is left as page CSS; the shape every * converter uses. * * @param string $css_attr css attribute. * @param Context $ctx Context. * @param array $allow Supported groups. * @param string[] $skip Leftover properties the caller handles itself (e.g. background-image on a cover). * @return array{style:array,className:string,leftover:array,background_image:string} */ public static function resolve( $css_attr, Context $ctx, array $allow = array( 'padding', 'margin', 'background', 'border' ), array $skip = array() ) { $d = self::map( $css_attr, $ctx, $allow ); $left = array(); $d['style'] = self::clean( $d['style'], $left ); $d['leftover'] = array_diff_key( array_merge( $d['leftover'], $left ), array_flip( $skip ) ); if ( $ctx->pro && $d['leftover'] ) { $d['className'] = self::page_css( $ctx, $d['leftover'] ); } elseif ( $d['leftover'] ) { $ctx->note( 'Some Design Options (for example background images or side margins) have no block setting; Bakeout Pro keeps them as page CSS.' ); } return $d; } }