| 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 |
* Recursive function for parsing blocks |
| 18 |
*/ |
| 19 |
public static function betterdocs_block_style_recursive( $block, &$betterdocs_blocks ) { |
| 20 |
if ( count( $block ) > 0 ) { |
| 21 |
foreach ( $block as $item ) { |
| 22 |
$attributes = $item['attrs']; |
| 23 |
|
| 24 |
$blockId = ""; |
| 25 |
if ( isset( $attributes['blockId'] ) && ! empty( $attributes['blockId'] ) ) { |
| 26 |
$blockId = $attributes['blockId']; |
| 27 |
} |
| 28 |
$blockMeta = ""; |
| 29 |
if ( isset( $attributes['blockMeta'] ) && ! empty( $attributes['blockMeta'] ) ) { |
| 30 |
$blockMeta = $attributes['blockMeta']; |
| 31 |
} |
| 32 |
$commonStyles = ""; |
| 33 |
if ( isset( $attributes['commonStyles'] ) && ! empty( $attributes['commonStyles'] ) ) { |
| 34 |
$commonStyles = $attributes['commonStyles']; |
| 35 |
} |
| 36 |
$customCss = ""; |
| 37 |
if ( isset( $attributes['customCss'] ) && ! empty( $attributes['customCss'] ) ) { |
| 38 |
$customCss = $attributes['customCss']; |
| 39 |
} |
| 40 |
|
| 41 |
if ( isset( $attributes['ref'] ) && ! empty( $attributes['ref'] ) && $item["blockName"] === "core/block" ) { |
| 42 |
$reusable_block = get_post( $attributes['ref'] ); |
| 43 |
$reusable_content = ! empty( $reusable_block ) ? parse_blocks( $reusable_block->post_content ) : []; |
| 44 |
$reusable_blocks = []; |
| 45 |
$betterdocs_blocks["reusableBlocks"][ $attributes['ref'] ] = self::betterdocs_block_style_recursive( $reusable_content, $reusable_blocks ); |
| 46 |
} else { |
| 47 |
if ( isset( $item["innerBlocks"] ) && count( $item["innerBlocks"] ) > 0 ) { |
| 48 |
self::betterdocs_block_style_recursive( $item['innerBlocks'], $betterdocs_blocks ); |
| 49 |
if ( isset( $attributes['blockMeta'] ) && ! empty( $attributes['blockMeta'] ) ) { |
| 50 |
$betterdocs_blocks[ $blockId ] = [ |
| 51 |
'blockMeta' => $blockMeta, |
| 52 |
'commonStyles' => $commonStyles, |
| 53 |
'customCss' => $customCss |
| 54 |
]; |
| 55 |
} |
| 56 |
} else if ( isset( $attributes['blockMeta'] ) && ! empty( $attributes['blockMeta'] ) ) { |
| 57 |
$betterdocs_blocks[ $blockId ] = [ |
| 58 |
'blockMeta' => $blockMeta, |
| 59 |
'commonStyles' => $commonStyles, |
| 60 |
'customCss' => $customCss |
| 61 |
]; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return $betterdocs_blocks; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Blockarray to Style Array Function |
| 72 |
*/ |
| 73 |
public static function blocks_to_style_array( $blocks, $styles = [] ) { |
| 74 |
if ( is_array( $blocks ) && count( $blocks ) > 0 ) { |
| 75 |
foreach ( $blocks as $blockId => $block ) { |
| 76 |
if ( 'reusableBlocks' === $blockId ) { |
| 77 |
if ( is_array( $block ) && count( $block ) > 0 ) { |
| 78 |
foreach ( $block as $reusableId => $reusableBlock ) { |
| 79 |
self::blocks_to_style_array( $reusableBlock, $styles ); |
| 80 |
} |
| 81 |
} |
| 82 |
} else { |
| 83 |
$styles[ $blockId ] = [ |
| 84 |
'desktop' => "", |
| 85 |
'tab' => "", |
| 86 |
'mobile' => "", |
| 87 |
'customCss' => "" |
| 88 |
]; |
| 89 |
|
| 90 |
if ( is_array( $block ) && count( $block ) > 0 ) { |
| 91 |
foreach ( $block as $value ) { |
| 92 |
if ( is_array( $value ) && count( $value ) > 0 ) { |
| 93 |
if ( isset( $value["desktop"] ) ) { |
| 94 |
$styles[ $blockId ]["desktop"] .= $value["desktop"]; |
| 95 |
} |
| 96 |
if( isset( $value['desktopHover'] ) ) { |
| 97 |
$styles[ $blockId ]["desktop"] .= $value['desktopHover']; |
| 98 |
} |
| 99 |
if ( isset( $value["tab"] ) ) { |
| 100 |
$styles[ $blockId ]["tab"] .= $value["tab"]; |
| 101 |
} |
| 102 |
if ( isset( $value["tabHover"] ) ) { |
| 103 |
$styles[ $blockId ]["tab"] .= $value["tabHover"]; |
| 104 |
} |
| 105 |
if ( isset( $value["mobile"] ) ) { |
| 106 |
$styles[ $blockId ]["mobile"] .= $value["mobile"]; |
| 107 |
} |
| 108 |
if ( isset( $value["mobileHover"] ) ) { |
| 109 |
$styles[ $blockId ]["mobile"] .= $value["mobileHover"]; |
| 110 |
} |
| 111 |
} else if ( isset( $block['customCss'] ) && is_string( $block['customCss'] ) && strlen( $block['customCss'] ) > 0 ) { |
| 112 |
$styles[ $blockId ]["customCss"] .= $block['customCss']; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $styles; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Enqueue frontend css for post if have one |
| 125 |
* |
| 126 |
* @param array |
| 127 |
* |
| 128 |
* @return string |
| 129 |
* @since 1.0.2 |
| 130 |
*/ |
| 131 |
public static function build_css( $style_object ) { |
| 132 |
$block_styles = $style_object; |
| 133 |
|
| 134 |
$css = ''; |
| 135 |
foreach ( $block_styles as $block_style_key => $block_style ) { |
| 136 |
if ( ! empty( $block_css = (array) $block_style ) ) { |
| 137 |
$css .= sprintf( '/* %1$s Starts */', $block_style_key ); |
| 138 |
foreach ( $block_css as $media => $style ) { |
| 139 |
switch ( $media ) { |
| 140 |
case "desktop": |
| 141 |
$css .= preg_replace( '/\s+/', ' ', $style ); |
| 142 |
break; |
| 143 |
case "tab": |
| 144 |
$css .= ' @media(max-width: 1024px){'; |
| 145 |
$css .= preg_replace( '/\s+/', ' ', $style ); |
| 146 |
$css .= '}'; |
| 147 |
break; |
| 148 |
case "mobile": |
| 149 |
$css .= ' @media(max-width: 767px){'; |
| 150 |
$css .= preg_replace( '/\s+/', ' ', $style ); |
| 151 |
$css .= '}'; |
| 152 |
break; |
| 153 |
case "customCss": |
| 154 |
$css .= preg_replace( '/\s+/', ' ', $style ); |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
$css .= sprintf( '/* =%1$s= Ends */', $block_style_key ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return trim( $css ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Helper function to get string between 2 string |
| 167 |
* @since 3.3.0 |
| 168 |
*/ |
| 169 |
public static function get_between_data( $string, $start, $end ) { |
| 170 |
$pos_string = stripos( $string, $start ); |
| 171 |
$substr_data = substr( $string, $pos_string ); |
| 172 |
$string_two = substr( $substr_data, strlen( $start ) ); |
| 173 |
$second_pos = stripos( $string_two, $end ); |
| 174 |
$string_three = substr( $string_two, 0, $second_pos ); |
| 175 |
|
| 176 |
// remove whitespaces from result |
| 177 |
$result_unit = trim( $string_three ); |
| 178 |
|
| 179 |
// return result_unit |
| 180 |
return $result_unit; |
| 181 |
} |
| 182 |
} |
| 183 |
|