PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.1.2
GenerateBlocks v1.1.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / functions.php
generateblocks / includes Last commit date
class-do-css.php 6 years ago class-enqueue-css.php 6 years ago class-plugin-update.php 6 years ago class-settings.php 6 years ago dashboard.php 6 years ago defaults.php 6 years ago functions.php 6 years ago general.php 6 years ago generate-css.php 6 years ago
functions.php
509 lines
1 <?php
2 /**
3 * Functions used throughout the plugin.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Retrive attributes from our blocks.
14 *
15 * @since 0.1
16 * @param array $content The content of our page.
17 * @param array $data Data used to loop through the function as needed.
18 * @param int $depth Keep track of how deep we are in nested blocks.
19 *
20 * @return array
21 */
22 function generateblocks_get_block_data( $content, $data = array(), $depth = 0 ) {
23 if ( ! is_array( $content ) || empty( $content ) ) {
24 return;
25 }
26
27 foreach ( $content as $index => $block ) {
28 if ( ! is_object( $block ) && is_array( $block ) && isset( $block['blockName'] ) ) {
29 if ( 'generateblocks/grid' === $block['blockName'] ) {
30 $data['grid'][] = $block['attrs'];
31 $depth++;
32 $data[ 'tempGridId-' . $depth ] = $block['attrs']['uniqueId'];
33 }
34
35 if ( 'generateblocks/container' === $block['blockName'] ) {
36 if ( isset( $block['attrs']['isGrid'] ) && $block['attrs']['isGrid'] && isset( $data[ 'tempGridId-' . $depth ] ) ) {
37 $block['attrs']['gridId'] = $data[ 'tempGridId-' . $depth ];
38 }
39
40 $data['container'][] = $block['attrs'];
41 }
42
43 if ( 'generateblocks/headline' === $block['blockName'] ) {
44 $data['headline'][] = $block['attrs'];
45 }
46
47 if ( 'generateblocks/button-container' === $block['blockName'] ) {
48 $data['button-container'][] = $block['attrs'];
49 }
50
51 if ( 'generateblocks/button' === $block['blockName'] ) {
52 $data['button'][] = $block['attrs'];
53 }
54
55 if ( 'core/block' === $block['blockName'] ) {
56 if ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) {
57 $atts = $block['attrs'];
58
59 if ( isset( $atts['ref'] ) ) {
60 $reusable_block = get_post( $atts['ref'] );
61
62 if ( $reusable_block && 'wp_block' === $reusable_block->post_type ) {
63 $reuse_data_block = parse_blocks( $reusable_block->post_content );
64 $data = generateblocks_get_block_data( $reuse_data_block, $data );
65 }
66 }
67 }
68 }
69
70 if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
71 $data = generateblocks_get_block_data( $block['innerBlocks'], $data, $depth );
72 }
73 }
74 }
75
76 return $data;
77 }
78
79 /**
80 * Parse our content for blocks.
81 *
82 * @param string $content Optional content to parse.
83 * @since 1.1
84 */
85 function generateblocks_get_parsed_content( $content = '' ) {
86 if ( ! function_exists( 'has_blocks' ) ) {
87 return;
88 }
89
90 if ( ! $content && has_blocks( get_the_ID() ) ) {
91 global $post;
92
93 if ( ! is_object( $post ) ) {
94 return;
95 }
96
97 $content = $post->post_content;
98 }
99
100 $content = apply_filters( 'generateblocks_do_content', $content );
101
102 if ( ! function_exists( 'parse_blocks' ) ) {
103 return;
104 }
105
106 $content = parse_blocks( $content );
107
108 return $content;
109 }
110
111 /**
112 * Shorthand CSS values (padding, margin, border etc..).
113 *
114 * @since 0.1
115 *
116 * @param int $top The first value.
117 * @param int $right The second value.
118 * @param int $bottom The third value.
119 * @param int $left The fourth value.
120 * @param string $unit The unit we're adding.
121 *
122 * @return string The shorthand value.
123 */
124 function generateblocks_get_shorthand_css( $top, $right, $bottom, $left, $unit ) {
125 if ( '' === $top && '' === $right && '' === $bottom && '' === $left ) {
126 return;
127 }
128
129 $top = ( floatval( $top ) <> 0 ) ? floatval( $top ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
130 $right = ( floatval( $right ) <> 0 ) ? floatval( $right ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
131 $bottom = ( floatval( $bottom ) <> 0 ) ? floatval( $bottom ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
132 $left = ( floatval( $left ) <> 0 ) ? floatval( $left ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
133
134 if ( $right === $left ) {
135 $left = '';
136
137 if ( $top === $bottom ) {
138 $bottom = '';
139
140 if ( $top === $right ) {
141 $right = '';
142 }
143 }
144 }
145
146 return trim( $top . $right . $bottom . $left );
147 }
148
149 /**
150 * Get our media query.
151 *
152 * @since 0.1
153 * @param string $type The media query we're getting.
154 *
155 * @return string
156 */
157 function generateblocks_get_media_query( $type ) {
158 $queries = apply_filters(
159 'generateblocks_media_query',
160 array(
161 'mobile' => '(max-width: 767px)',
162 'tablet' => '(max-width: 1024px)',
163 )
164 );
165
166 return $queries[ $type ];
167 }
168
169 /**
170 * Build our list of Google fonts on this page.
171 *
172 * @since 0.1
173 * @param string $content Optional content to parse.
174 * @return array
175 */
176 function generateblocks_get_google_fonts( $content = '' ) {
177 $content = generateblocks_get_parsed_content( $content );
178
179 if ( ! $content ) {
180 return;
181 }
182
183 $data = generateblocks_get_block_data( $content );
184
185 $defaults = generateblocks_get_block_defaults();
186 $font_data = array();
187
188 if ( ! empty( $data ) ) {
189 foreach ( $data as $name => $blockData ) {
190 if ( 'button' === $name ) {
191 foreach ( $blockData as $atts ) {
192 $button_settings = wp_parse_args(
193 $atts,
194 $defaults['button']
195 );
196
197 if ( $button_settings['googleFont'] ) {
198 $id = $atts['uniqueId'];
199
200 $variants = $button_settings['googleFontVariants'];
201
202 if ( $variants ) {
203 $variants = str_replace( ' ', '', $variants );
204 $variants = explode( ',', $variants );
205 }
206
207 $font_data[ $id ] = array(
208 'name' => $button_settings['fontFamily'],
209 'variants' => $variants,
210 );
211 }
212 }
213 }
214
215 if ( 'headline' === $name ) {
216 foreach ( $blockData as $atts ) {
217 $headline_settings = wp_parse_args(
218 $atts,
219 $defaults['headline']
220 );
221
222 if ( $headline_settings['googleFont'] ) {
223 $id = $atts['uniqueId'];
224 $variants = $headline_settings['googleFontVariants'];
225
226 if ( $variants ) {
227 $variants = str_replace( ' ', '', $variants );
228 $variants = explode( ',', $variants );
229 }
230
231 $font_data[ $id ] = array(
232 'name' => $headline_settings['fontFamily'],
233 'variants' => $variants,
234 );
235 }
236 }
237 }
238
239 if ( 'container' === $name ) {
240 foreach ( $blockData as $atts ) {
241 $container_settings = wp_parse_args(
242 $atts,
243 $defaults['container']
244 );
245
246 if ( $container_settings['googleFont'] ) {
247 $id = $atts['uniqueId'];
248 $variants = $container_settings['googleFontVariants'];
249
250 if ( $variants ) {
251 $variants = str_replace( ' ', '', $variants );
252 $variants = explode( ',', $variants );
253 }
254
255 $font_data[ $id ] = array(
256 'name' => $container_settings['fontFamily'],
257 'variants' => $variants,
258 );
259 }
260 }
261 }
262 }
263 }
264
265 $fonts = array();
266
267 foreach ( (array) $font_data as $font ) {
268 $id = str_replace( ' ', '', strtolower( $font['name'] ) );
269
270 $fonts[ $id ]['name'] = $font['name'];
271
272 if ( ! empty( $font['variants'] ) ) {
273 foreach ( $font['variants'] as $variant ) {
274 if ( isset( $fonts[ $id ]['variants'] ) ) {
275 if ( in_array( $variant, (array) $fonts[ $id ]['variants'] ) ) {
276 continue;
277 }
278 }
279
280 $fonts[ $id ]['variants'][] = $variant;
281 }
282 }
283 }
284
285 return apply_filters( 'generateblocks_google_fonts', $fonts );
286 }
287
288 /**
289 * Build the Google Font request URI.
290 *
291 * @since 0.1
292 *
293 * @return string The request URI to Google Fonts.
294 */
295 function generateblocks_get_google_fonts_uri() {
296 $google_fonts = generateblocks_get_google_fonts();
297
298 if ( ! $google_fonts ) {
299 return;
300 }
301
302 $data = array();
303
304 foreach ( $google_fonts as $font ) {
305 $variants = array();
306
307 if ( ! empty( $font['variants'] ) ) {
308 foreach ( $font['variants'] as $variant ) {
309 $variants[] = $variant;
310 }
311 }
312
313 $variants = apply_filters( 'generateblocks_google_font_variants', $variants, $font['name'] );
314
315 $name = str_replace( ' ', '+', $font['name'] );
316
317 if ( $variants ) {
318 $data[] = $name . ':' . implode( ',', $variants );
319 } else {
320 $data[] = $name;
321 }
322 }
323
324 $font_args = apply_filters(
325 'generateblocks_google_font_args',
326 array(
327 'family' => implode( '|', $data ),
328 'subset' => null,
329 'display' => 'swap',
330 )
331 );
332
333 return add_query_arg( $font_args, '//fonts.googleapis.com/css' );
334 }
335
336 /**
337 * Convert hex to RGBA
338 *
339 * @since 0.1
340 * @param string $hex The hex value.
341 * @param int $alpha The opacity value.
342 *
343 * @return string The RGBA value.
344 */
345 function generateblocks_hex2rgba( $hex, $alpha ) {
346 if ( ! $hex ) {
347 return;
348 }
349
350 if ( 1 === $alpha ) {
351 return $hex;
352 }
353
354 $hex = str_replace( '#', '', $hex );
355
356 if ( strlen( $hex ) == 3 ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
357 $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
358 $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
359 $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
360 } else {
361 $r = hexdec( substr( $hex, 0, 2 ) );
362 $g = hexdec( substr( $hex, 2, 2 ) );
363 $b = hexdec( substr( $hex, 4, 2 ) );
364 }
365
366 $rgba = 'rgba(' . $r . ', ' . $g . ', ' . $b . ', ' . $alpha . ')';
367
368 return $rgba;
369 }
370
371 /**
372 * Return old flexblocks values for old browsers.
373 *
374 * @since 0.1
375 * @param string $value The value to convert.
376 *
377 * @return string The old browser value.
378 */
379 function generateblocks_get_vendor_prefix( $value ) {
380 if ( 'flex-start' === $value || 'left' === $value ) {
381 return 'start';
382 }
383
384 if ( 'flex-end' === $value || 'right' === $value ) {
385 return 'end';
386 }
387
388 return $value;
389 }
390
391 /**
392 * Return flexbox alignment values from left/right.
393 *
394 * @since 0.1
395 * @param string $value The value to convert.
396 *
397 * @return string The flexbox alignment value.
398 */
399 function generateblocks_get_flexbox_alignment( $value ) {
400 if ( 'left' === $value || 'top' === $value ) {
401 return 'flex-start';
402 }
403
404 if ( 'right' === $value || 'bottom' === $value ) {
405 return 'flex-end';
406 }
407
408 return $value;
409 }
410
411 /**
412 * Get an option from the database.
413 *
414 * @param string $option The option to get.
415 * @since 0.1
416 */
417 function generateblocks_get_option( $option ) {
418 $defaults = generateblocks_get_option_defaults();
419
420 if ( ! isset( $defaults[ $option ] ) ) {
421 return;
422 }
423
424 $options = wp_parse_args(
425 get_option( 'generateblocks', array() ),
426 $defaults
427 );
428
429 return $options[ $option ];
430 }
431
432 /**
433 * Checks whether a value exists, even if it's a 0.
434 *
435 * @param int|string $value The value to check.
436 * @since 1.0
437 */
438 function generateblocks_has_number_value( $value ) {
439 if ( $value || 0 === $value || '0' === $value ) {
440 return true;
441 }
442
443 return false;
444 }
445
446 /**
447 * Get the background-image value.
448 *
449 * @param array $settings Our background image settings.
450 * @param array $custom_args Custom args that will overwrite the settings.
451 */
452 function generateblocks_get_background_image_css( $settings, $custom_args = array() ) {
453 $args = array(
454 'backgroundColor' => $settings['backgroundColor'],
455 'backgroundColorOpacity' => $settings['backgroundColorOpacity'],
456 'gradient' => $settings['gradient'],
457 'gradientDirection' => $settings['gradientDirection'],
458 'gradientColorOne' => $settings['gradientColorOne'],
459 'gradientColorOneOpacity' => $settings['gradientColorOneOpacity'],
460 'gradientColorStopOne' => $settings['gradientColorStopOne'],
461 'gradientColorTwo' => $settings['gradientColorTwo'],
462 'gradientColorTwoOpacity' => $settings['gradientColorTwoOpacity'],
463 'gradientColorStopTwo' => $settings['gradientColorStopTwo'],
464 'bgImage' => $settings['bgImage'],
465 'bgOptions' => $settings['bgOptions'],
466 );
467
468 $args = wp_parse_args(
469 $args,
470 $custom_args
471 );
472
473 $background_image = false;
474 $gradientColorStopOneValue = '';
475 $gradientColorStopTwoValue = '';
476
477 $args['backgroundColor'] = generateblocks_hex2rgba( $args['backgroundColor'], $args['backgroundColorOpacity'] );
478 $args['gradientColorOne'] = generateblocks_hex2rgba( $args['gradientColorOne'], $args['gradientColorOneOpacity'] );
479 $args['gradientColorTwo'] = generateblocks_hex2rgba( $args['gradientColorTwo'], $args['gradientColorTwoOpacity'] );
480
481 if ( $args['gradient'] ) {
482 if ( $args['gradientColorOne'] && '' !== $args['gradientColorStopOne'] ) {
483 $gradientColorStopOneValue = ' ' . $args['gradientColorStopOne'] . '%';
484 }
485
486 if ( $args['gradientColorTwo'] && '' !== $args['gradientColorStopTwo'] ) {
487 $gradientColorStopTwoValue = ' ' . $args['gradientColorStopTwo'] . '%';
488 }
489 }
490
491 if ( $args['bgImage'] && 'element' === $args['bgOptions']['selector'] ) {
492 $url = $args['bgImage']['image']['url'];
493
494 if ( ( $args['backgroundColor'] || $args['gradient'] ) && isset( $args['bgOptions']['overlay'] ) && $args['bgOptions']['overlay'] ) {
495 if ( $args['gradient'] ) {
496 $background_image = 'linear-gradient(' . $args['gradientDirection'] . 'deg, ' . $args['gradientColorOne'] . $gradientColorStopOneValue . ', ' . $args['gradientColorTwo'] . $gradientColorStopTwoValue . '), url(' . esc_url( $url ) . ')';
497 } elseif ( $args['backgroundColor'] ) {
498 $background_image = 'linear-gradient(0deg, ' . $args['backgroundColor'] . ', ' . $args['backgroundColor'] . '), url(' . esc_url( $url ) . ')';
499 }
500 } else {
501 $background_image = 'url(' . esc_url( $url ) . ')';
502 }
503 } elseif ( $args['gradient'] ) {
504 $background_image = 'linear-gradient(' . $args['gradientDirection'] . 'deg, ' . $args['gradientColorOne'] . $gradientColorStopOneValue . ', ' . $args['gradientColorTwo'] . $gradientColorStopTwoValue . ')';
505 }
506
507 return $background_image;
508 }
509