| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Utils; |
| 4 |
|
| 5 |
class CSSParser { |
| 6 |
private static $instance; |
| 7 |
|
| 8 |
public static function init() { |
| 9 |
if ( null === self::$instance ) { |
| 10 |
self::$instance = new self(); |
| 11 |
} |
| 12 |
|
| 13 |
return self::$instance; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Whether a parsed block is one of BetterDocs' own (`betterdocs/*`). |
| 18 |
* |
| 19 |
* `blockId` / `blockMeta` are the same attribute names Essential Blocks |
| 20 |
* uses, so without this check BetterDocs collected every Essential Blocks |
| 21 |
* block and wrote a full copy of its generated CSS (909 KB on the |
| 22 |
* essential-blocks.com homepage) into betterdocs-style-{id}.min.css. |
| 23 |
* |
| 24 |
* @param array $item Parsed block. |
| 25 |
* @return bool |
| 26 |
*/ |
| 27 |
public static function is_betterdocs_block( $item ) { |
| 28 |
return isset( $item['blockName'] ) && is_string( $item['blockName'] ) && 0 === strpos( $item['blockName'], 'betterdocs/' ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Recursive function for parsing blocks |
| 33 |
*/ |
| 34 |
public static function betterdocs_block_style_recursive( $block, &$betterdocs_blocks ) { |
| 35 |
if ( count( $block ) > 0 ) { |
| 36 |
foreach ( $block as $item ) { |
| 37 |
$attributes = $item['attrs']; |
| 38 |
// Other plugins' blocks are still walked (below) for nested |
| 39 |
// BetterDocs blocks, but never contribute styles themselves. |
| 40 |
$is_own = self::is_betterdocs_block( $item ); |
| 41 |
|
| 42 |
$blockId = ''; |
| 43 |
if ( isset( $attributes['blockId'] ) && ! empty( $attributes['blockId'] ) ) { |
| 44 |
$blockId = $attributes['blockId']; |
| 45 |
} |
| 46 |
$blockMeta = ''; |
| 47 |
if ( isset( $attributes['blockMeta'] ) && ! empty( $attributes['blockMeta'] ) ) { |
| 48 |
$blockMeta = $attributes['blockMeta']; |
| 49 |
} |
| 50 |
$commonStyles = ''; |
| 51 |
if ( isset( $attributes['commonStyles'] ) && ! empty( $attributes['commonStyles'] ) ) { |
| 52 |
$commonStyles = $attributes['commonStyles']; |
| 53 |
} |
| 54 |
$customCss = ''; |
| 55 |
if ( isset( $attributes['customCss'] ) && ! empty( $attributes['customCss'] ) ) { |
| 56 |
$customCss = $attributes['customCss']; |
| 57 |
} |
| 58 |
|
| 59 |
if ( isset( $attributes['ref'] ) && ! empty( $attributes['ref'] ) && $item['blockName'] === 'core/block' ) { |
| 60 |
$reusable_block = get_post( $attributes['ref'] ); |
| 61 |
$reusable_content = ! empty( $reusable_block ) ? parse_blocks( $reusable_block->post_content ) : []; |
| 62 |
$reusable_blocks = []; |
| 63 |
$betterdocs_blocks['reusableBlocks'][ $attributes['ref'] ] = self::betterdocs_block_style_recursive( $reusable_content, $reusable_blocks ); |
| 64 |
} elseif ( isset( $item['innerBlocks'] ) && count( $item['innerBlocks'] ) > 0 ) { |
| 65 |
self::betterdocs_block_style_recursive( $item['innerBlocks'], $betterdocs_blocks ); |
| 66 |
if ( $is_own && isset( $attributes['blockMeta'] ) && ! empty( $attributes['blockMeta'] ) ) { |
| 67 |
$betterdocs_blocks[ $blockId ] = [ |
| 68 |
'blockMeta' => $blockMeta, |
| 69 |
'commonStyles' => $commonStyles, |
| 70 |
'customCss' => $customCss |
| 71 |
]; |
| 72 |
} |
| 73 |
} elseif ( $is_own && isset( $attributes['blockMeta'] ) && ! empty( $attributes['blockMeta'] ) ) { |
| 74 |
$betterdocs_blocks[ $blockId ] = [ |
| 75 |
'blockMeta' => $blockMeta, |
| 76 |
'commonStyles' => $commonStyles, |
| 77 |
'customCss' => $customCss |
| 78 |
]; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $betterdocs_blocks; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Blockarray to Style Array Function |
| 88 |
*/ |
| 89 |
public static function blocks_to_style_array( $blocks, $styles = [] ) { |
| 90 |
if ( is_array( $blocks ) && count( $blocks ) > 0 ) { |
| 91 |
foreach ( $blocks as $blockId => $block ) { |
| 92 |
if ( 'reusableBlocks' === $blockId ) { |
| 93 |
if ( is_array( $block ) && count( $block ) > 0 ) { |
| 94 |
foreach ( $block as $reusableId => $reusableBlock ) { |
| 95 |
self::blocks_to_style_array( $reusableBlock, $styles ); |
| 96 |
} |
| 97 |
} |
| 98 |
} else { |
| 99 |
$styles[ $blockId ] = [ |
| 100 |
'desktop' => '', |
| 101 |
'tab' => '', |
| 102 |
'mobile' => '', |
| 103 |
'customCss' => '' |
| 104 |
]; |
| 105 |
|
| 106 |
if ( is_array( $block ) && count( $block ) > 0 ) { |
| 107 |
foreach ( $block as $value ) { |
| 108 |
if ( is_array( $value ) && count( $value ) > 0 ) { |
| 109 |
if ( isset( $value['desktop'] ) ) { |
| 110 |
$styles[ $blockId ]['desktop'] .= $value['desktop']; |
| 111 |
} |
| 112 |
if ( isset( $value['desktopHover'] ) ) { |
| 113 |
$styles[ $blockId ]['desktop'] .= $value['desktopHover']; |
| 114 |
} |
| 115 |
if ( isset( $value['tab'] ) ) { |
| 116 |
$styles[ $blockId ]['tab'] .= $value['tab']; |
| 117 |
} |
| 118 |
if ( isset( $value['tabHover'] ) ) { |
| 119 |
$styles[ $blockId ]['tab'] .= $value['tabHover']; |
| 120 |
} |
| 121 |
if ( isset( $value['mobile'] ) ) { |
| 122 |
$styles[ $blockId ]['mobile'] .= $value['mobile']; |
| 123 |
} |
| 124 |
if ( isset( $value['mobileHover'] ) ) { |
| 125 |
$styles[ $blockId ]['mobile'] .= $value['mobileHover']; |
| 126 |
} |
| 127 |
} elseif ( isset( $block['customCss'] ) && is_string( $block['customCss'] ) && strlen( $block['customCss'] ) > 0 ) { |
| 128 |
$styles[ $blockId ]['customCss'] .= $block['customCss']; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $styles; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Enqueue frontend css for post if have one |
| 141 |
* |
| 142 |
* @param array |
| 143 |
* |
| 144 |
* @return string |
| 145 |
* @since 1.0.2 |
| 146 |
*/ |
| 147 |
public static function build_css( $style_object ) { |
| 148 |
$block_styles = $style_object; |
| 149 |
|
| 150 |
$css = ''; |
| 151 |
foreach ( $block_styles as $block_style_key => $block_style ) { |
| 152 |
if ( ! empty( $block_css = (array) $block_style ) ) { |
| 153 |
$css .= sprintf( '/* %1$s Starts */', $block_style_key ); |
| 154 |
foreach ( $block_css as $media => $style ) { |
| 155 |
switch ( $media ) { |
| 156 |
case 'desktop': |
| 157 |
$css .= preg_replace( '/\s+/', ' ', $style ); |
| 158 |
break; |
| 159 |
case 'tab': |
| 160 |
$css .= ' @media(max-width: 1024px){'; |
| 161 |
$css .= preg_replace( '/\s+/', ' ', $style ); |
| 162 |
$css .= '}'; |
| 163 |
break; |
| 164 |
case 'mobile': |
| 165 |
$css .= ' @media(max-width: 767px){'; |
| 166 |
$css .= preg_replace( '/\s+/', ' ', $style ); |
| 167 |
$css .= '}'; |
| 168 |
break; |
| 169 |
case 'customCss': |
| 170 |
$css .= preg_replace( '/\s+/', ' ', $style ); |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
$css .= sprintf( '/* =%1$s= Ends */', $block_style_key ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return trim( $css ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Helper function to get string between 2 string |
| 183 |
* @since 3.3.0 |
| 184 |
*/ |
| 185 |
public static function get_between_data( $string, $start, $end ) { |
| 186 |
$pos_string = stripos( $string, $start ); |
| 187 |
$substr_data = substr( $string, $pos_string ); |
| 188 |
$string_two = substr( $substr_data, strlen( $start ) ); |
| 189 |
$second_pos = stripos( $string_two, $end ); |
| 190 |
$string_three = substr( $string_two, 0, $second_pos ); |
| 191 |
|
| 192 |
// remove whitespaces from result |
| 193 |
$result_unit = trim( $string_three ); |
| 194 |
|
| 195 |
// return result_unit |
| 196 |
return $result_unit; |
| 197 |
} |
| 198 |
} |
| 199 |
|