PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.0
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 All 66 releases
better-payment / includes / Blocks / CSSParser.php

CSSParser.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.0, at includes/Blocks/CSSParser.php

251 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Blocks;
4
5 class CSSParser
6 {
7 private static $instance;
8
9 public static function init()
10 {
11 if ( null === self::$instance ) {
12 self::$instance = new self;
13 }
14
15 return self::$instance;
16 }
17
18 /**
19 * Get Responsive Breakpoints
20 */
21 public static function get_responsive_breakpoints( $size = 'mobile' )
22 {
23 $settings = get_option( 'eb_settings', [ ] );
24 $settingsData = [ ];
25
26 if ( array_key_exists( 'responsiveBreakpoints', $settings ) ) {
27 $settingsData = $settings[ 'responsiveBreakpoints' ];
28 if ( gettype( $settingsData ) === 'string' && strlen( $settingsData ) > 0 ) {
29 $settingsData = (array) json_decode( html_entity_decode( stripslashes( $settingsData ) ) );
30 }
31 } else {
32 $settings[ 'responsiveBreakpoints' ] = [
33 'tablet' => 1024,
34 'mobile' => 767
35 ];
36
37 update_option( 'eb_settings', $settings );
38
39 $settingsData = $settings[ 'responsiveBreakpoints' ];
40 }
41
42 return $settingsData[ $size ];
43 }
44
45 /**
46 * Recursive function for parsing blocks
47 */
48 public static function eb_block_style_recursive( $block, &$eb_blocks, &$blocknames )
49 {
50 if ( count( $block ) > 0 ) {
51 foreach ( $block as $item ) {
52 $attributes = $item[ 'attrs' ];
53
54 $isCustomCssError = isset( $attributes[ 'isCustomCssError' ] ) ? $attributes[ 'isCustomCssError' ] : false;
55
56 $blockId = "";
57 if ( isset( $attributes[ 'blockId' ] ) && ! empty( $attributes[ 'blockId' ] ) ) {
58 $blockId = $attributes[ 'blockId' ];
59 if ( isset( $item[ 'blockName' ] ) ) {
60 $blocknames[ ] = $item[ 'blockName' ];
61 }
62 }
63 $blockMeta = "";
64 if ( isset( $attributes[ 'blockMeta' ] ) && ! empty( $attributes[ 'blockMeta' ] ) ) {
65 // Decode HTML entities that WordPress encodes when saving block attributes
66 $blockMeta = is_array( $attributes[ 'blockMeta' ] )
67 ? array_map( function( $style ) {
68 return html_entity_decode( $style, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
69 }, $attributes[ 'blockMeta' ] )
70 : $attributes[ 'blockMeta' ];
71 }
72 $commonStyles = "";
73 if ( isset( $attributes[ 'commonStyles' ] ) && ! empty( $attributes[ 'commonStyles' ] ) ) {
74 // Decode HTML entities that WordPress encodes when saving block attributes
75 $commonStyles = is_array( $attributes[ 'commonStyles' ] )
76 ? array_map( function( $style ) {
77 return html_entity_decode( $style, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
78 }, $attributes[ 'commonStyles' ] )
79 : $attributes[ 'commonStyles' ];
80 }
81 $customCss = "";
82 if ( ! $isCustomCssError && isset( $attributes[ 'customCss' ] ) && ! empty( $attributes[ 'customCss' ] ) ) {
83 $customCss = str_replace( 'eb_selector', "." . $blockId, $attributes[ 'customCss' ] );
84 }
85
86 if ( isset( $attributes[ 'ref' ] ) && ! empty( $attributes[ 'ref' ] ) && $item[ "blockName" ] === "core/block" ) {
87 $reusable_block = get_post( $attributes[ 'ref' ] );
88 $reusable_content = ! empty( $reusable_block ) ? parse_blocks( $reusable_block->post_content ) : [ ];
89 $reusable_blocks = [ ];
90 $eb_blocks[ "reusableBlocks" ][ $attributes[ 'ref' ] ] = self::eb_block_style_recursive( $reusable_content, $reusable_blocks, $blocknames );
91 } else {
92 if ( isset( $item[ "innerBlocks" ] ) && count( $item[ "innerBlocks" ] ) > 0 ) {
93 self::eb_block_style_recursive( $item[ 'innerBlocks' ], $eb_blocks, $blocknames );
94 if ( isset( $attributes[ 'blockMeta' ] ) && ! empty( $attributes[ 'blockMeta' ] ) ) {
95 $eb_blocks[ $blockId ] = [
96 'blockMeta' => $blockMeta,
97 'commonStyles' => $commonStyles,
98 'customCss' => $customCss
99 ];
100 }
101 } else if ( isset( $attributes[ 'blockMeta' ] ) && ! empty( $attributes[ 'blockMeta' ] ) ) {
102 $eb_blocks[ $blockId ] = [
103 'blockMeta' => $blockMeta,
104 'commonStyles' => $commonStyles,
105 'customCss' => $customCss
106 ];
107 }
108 }
109 }
110 }
111
112 return $eb_blocks;
113 }
114
115 /**
116 * Blockarray to Style Array Function
117 */
118 public static function blocks_to_style_array( $blocks, $styles = [ ] )
119 {
120 if ( is_array( $blocks ) && count( $blocks ) > 0 ) {
121 foreach ( $blocks as $blockId => $block ) {
122 if ( 'reusableBlocks' === $blockId ) {
123 if ( is_array( $block ) && count( $block ) > 0 ) {
124 foreach ( $block as $reusableId => $reusableBlock ) {
125 self::blocks_to_style_array( $reusableBlock, $styles );
126 }
127 }
128 } else {
129 $styles[ $blockId ] = [
130 'desktop' => "",
131 'tab' => "",
132 'mobile' => "",
133 'customCss' => ""
134 ];
135
136 if ( is_array( $block ) && count( $block ) > 0 ) {
137 foreach ( $block as $value ) {
138 if ( is_array( $value ) && count( $value ) > 0 ) {
139 if ( isset( $value[ "desktop" ] ) ) {
140 $styles[ $blockId ][ "desktop" ] .= $value[ "desktop" ];
141 }
142 if ( isset( $value[ "tab" ] ) ) {
143 $styles[ $blockId ][ "tab" ] .= $value[ "tab" ];
144 }
145 if ( isset( $value[ "mobile" ] ) ) {
146 $styles[ $blockId ][ "mobile" ] .= $value[ "mobile" ];
147 }
148 } else if ( isset( $block[ 'customCss' ] ) && is_string( $block[ 'customCss' ] ) && strlen( $block[ 'customCss' ] ) > 0 ) {
149 $styles[ $blockId ][ "customCss" ] .= $block[ 'customCss' ];
150 }
151 }
152 }
153 }
154 }
155 }
156
157 return $styles;
158 }
159
160 /**
161 * Enqueue frontend css for post if have one
162 *
163 * @param array
164 *
165 * @return string
166 * @since 1.0.2
167 */
168 public static function build_css( $style_object )
169 {
170 $block_styles = $style_object;
171
172 $css = '';
173 foreach ( $block_styles as $block_style_key => $block_style ) {
174 if ( ! empty( $block_css = (array) $block_style ) ) {
175 $css .= sprintf( '/* %1$s Starts */', $block_style_key );
176 foreach ( $block_css as $media => $style ) {
177 switch ( $media ) {
178 case "desktop":
179 $css .= preg_replace( '/\s+/', ' ', $style );
180 break;
181 case "tab":
182 $css .= ' @media(max-width: ' . self::get_responsive_breakpoints( 'tablet' ) . 'px){';
183 $css .= preg_replace( '/\s+/', ' ', $style );
184 $css .= '}';
185 break;
186 case "mobile":
187 $css .= ' @media(max-width: ' . self::get_responsive_breakpoints( 'mobile' ) . 'px){';
188 $css .= preg_replace( '/\s+/', ' ', $style );
189 $css .= '}';
190 break;
191 case "customCss":
192 $css .= preg_replace( '/\s+/', ' ', $style );
193 break;
194 }
195 }
196 $css .= sprintf( '/* =%1$s= Ends */', $block_style_key );
197 }
198 }
199
200 $css = trim( $css );
201
202 /**
203 * Filter the generated CSS before it's written to file.
204 *
205 * This filter allows third-party plugins (like Optimole) to modify the CSS output
206 * before it's persisted to disk. This is particularly useful for CDN-based image
207 * optimization plugins that need to process background image URLs.
208 *
209 * @since 5.0.9
210 *
211 * @param string $css The generated CSS string.
212 * @param array $style_object The original style object array used to generate the CSS.
213 */
214 return apply_filters( 'eb_blocks_generated_css', $css, $style_object );
215 }
216
217 /**
218 * Helper function to get string between 2 string
219 * @since 3.3.0
220 */
221 public static function get_between_data( $string, $start, $end )
222 {
223 $pos_string = stripos( $string, $start );
224 $substr_data = substr( $string, $pos_string );
225 $string_two = substr( $substr_data, strlen( $start ) );
226 $second_pos = stripos( $string_two, $end );
227 $string_three = substr( $string_two, 0, $second_pos );
228
229 // remove whitespaces from result
230 $result_unit = trim( $string_three );
231
232 // return result_unit
233 return $result_unit;
234 }
235
236 /**
237 * Is Gutenberg Editor
238 */
239 public static function eb_stylehandler_is_gutenberg_editor( $pagenow, $param )
240 {
241 if ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'site-editor.php' ) {
242 return true;
243 }
244
245 if ( $pagenow == 'themes.php' && ! empty( $param ) && false !== strpos( $param, 'gutenberg-edit-site' ) ) {
246 return true;
247 }
248
249 return false;
250 }
251 }