PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Utils / CSSParser.php

CSSParser.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.0, at includes/Utils/CSSParser.php

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