PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.1.1
GenerateBlocks v1.1.1
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
515 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 $parsed_content = wp_cache_get( 'generateblocks_parsed_content' );
87
88 if ( ! $parsed_content ) {
89 if ( ! function_exists( 'has_blocks' ) ) {
90 return;
91 }
92
93 if ( ! $content && has_blocks( get_the_ID() ) ) {
94 global $post;
95
96 if ( ! is_object( $post ) ) {
97 return;
98 }
99
100 $content = $post->post_content;
101 }
102
103 $content = apply_filters( 'generateblocks_do_content', $content );
104
105 if ( ! function_exists( 'parse_blocks' ) ) {
106 return;
107 }
108
109 $parsed_content = parse_blocks( $content );
110
111 wp_cache_set( 'generateblocks_parsed_content', $parsed_content );
112 }
113
114 return $parsed_content;
115 }
116
117 /**
118 * Shorthand CSS values (padding, margin, border etc..).
119 *
120 * @since 0.1
121 *
122 * @param int $top The first value.
123 * @param int $right The second value.
124 * @param int $bottom The third value.
125 * @param int $left The fourth value.
126 * @param string $unit The unit we're adding.
127 *
128 * @return string The shorthand value.
129 */
130 function generateblocks_get_shorthand_css( $top, $right, $bottom, $left, $unit ) {
131 if ( '' === $top && '' === $right && '' === $bottom && '' === $left ) {
132 return;
133 }
134
135 $top = ( floatval( $top ) <> 0 ) ? floatval( $top ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
136 $right = ( floatval( $right ) <> 0 ) ? floatval( $right ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
137 $bottom = ( floatval( $bottom ) <> 0 ) ? floatval( $bottom ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
138 $left = ( floatval( $left ) <> 0 ) ? floatval( $left ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
139
140 if ( $right === $left ) {
141 $left = '';
142
143 if ( $top === $bottom ) {
144 $bottom = '';
145
146 if ( $top === $right ) {
147 $right = '';
148 }
149 }
150 }
151
152 return trim( $top . $right . $bottom . $left );
153 }
154
155 /**
156 * Get our media query.
157 *
158 * @since 0.1
159 * @param string $type The media query we're getting.
160 *
161 * @return string
162 */
163 function generateblocks_get_media_query( $type ) {
164 $queries = apply_filters(
165 'generateblocks_media_query',
166 array(
167 'mobile' => '(max-width: 767px)',
168 'tablet' => '(max-width: 1024px)',
169 )
170 );
171
172 return $queries[ $type ];
173 }
174
175 /**
176 * Build our list of Google fonts on this page.
177 *
178 * @since 0.1
179 * @param string $content Optional content to parse.
180 * @return array
181 */
182 function generateblocks_get_google_fonts( $content = '' ) {
183 $content = generateblocks_get_parsed_content( $content );
184
185 if ( ! $content ) {
186 return;
187 }
188
189 $data = generateblocks_get_block_data( $content );
190
191 $defaults = generateblocks_get_block_defaults();
192 $font_data = array();
193
194 if ( ! empty( $data ) ) {
195 foreach ( $data as $name => $blockData ) {
196 if ( 'button' === $name ) {
197 foreach ( $blockData as $atts ) {
198 $button_settings = wp_parse_args(
199 $atts,
200 $defaults['button']
201 );
202
203 if ( $button_settings['googleFont'] ) {
204 $id = $atts['uniqueId'];
205
206 $variants = $button_settings['googleFontVariants'];
207
208 if ( $variants ) {
209 $variants = str_replace( ' ', '', $variants );
210 $variants = explode( ',', $variants );
211 }
212
213 $font_data[ $id ] = array(
214 'name' => $button_settings['fontFamily'],
215 'variants' => $variants,
216 );
217 }
218 }
219 }
220
221 if ( 'headline' === $name ) {
222 foreach ( $blockData as $atts ) {
223 $headline_settings = wp_parse_args(
224 $atts,
225 $defaults['headline']
226 );
227
228 if ( $headline_settings['googleFont'] ) {
229 $id = $atts['uniqueId'];
230 $variants = $headline_settings['googleFontVariants'];
231
232 if ( $variants ) {
233 $variants = str_replace( ' ', '', $variants );
234 $variants = explode( ',', $variants );
235 }
236
237 $font_data[ $id ] = array(
238 'name' => $headline_settings['fontFamily'],
239 'variants' => $variants,
240 );
241 }
242 }
243 }
244
245 if ( 'container' === $name ) {
246 foreach ( $blockData as $atts ) {
247 $container_settings = wp_parse_args(
248 $atts,
249 $defaults['container']
250 );
251
252 if ( $container_settings['googleFont'] ) {
253 $id = $atts['uniqueId'];
254 $variants = $container_settings['googleFontVariants'];
255
256 if ( $variants ) {
257 $variants = str_replace( ' ', '', $variants );
258 $variants = explode( ',', $variants );
259 }
260
261 $font_data[ $id ] = array(
262 'name' => $container_settings['fontFamily'],
263 'variants' => $variants,
264 );
265 }
266 }
267 }
268 }
269 }
270
271 $fonts = array();
272
273 foreach ( (array) $font_data as $font ) {
274 $id = str_replace( ' ', '', strtolower( $font['name'] ) );
275
276 $fonts[ $id ]['name'] = $font['name'];
277
278 if ( ! empty( $font['variants'] ) ) {
279 foreach ( $font['variants'] as $variant ) {
280 if ( isset( $fonts[ $id ]['variants'] ) ) {
281 if ( in_array( $variant, (array) $fonts[ $id ]['variants'] ) ) {
282 continue;
283 }
284 }
285
286 $fonts[ $id ]['variants'][] = $variant;
287 }
288 }
289 }
290
291 return apply_filters( 'generateblocks_google_fonts', $fonts );
292 }
293
294 /**
295 * Build the Google Font request URI.
296 *
297 * @since 0.1
298 *
299 * @return string The request URI to Google Fonts.
300 */
301 function generateblocks_get_google_fonts_uri() {
302 $google_fonts = generateblocks_get_google_fonts();
303
304 if ( ! $google_fonts ) {
305 return;
306 }
307
308 $data = array();
309
310 foreach ( $google_fonts as $font ) {
311 $variants = array();
312
313 if ( ! empty( $font['variants'] ) ) {
314 foreach ( $font['variants'] as $variant ) {
315 $variants[] = $variant;
316 }
317 }
318
319 $variants = apply_filters( 'generateblocks_google_font_variants', $variants, $font['name'] );
320
321 $name = str_replace( ' ', '+', $font['name'] );
322
323 if ( $variants ) {
324 $data[] = $name . ':' . implode( ',', $variants );
325 } else {
326 $data[] = $name;
327 }
328 }
329
330 $font_args = apply_filters(
331 'generateblocks_google_font_args',
332 array(
333 'family' => implode( '|', $data ),
334 'subset' => null,
335 'display' => 'swap',
336 )
337 );
338
339 return add_query_arg( $font_args, '//fonts.googleapis.com/css' );
340 }
341
342 /**
343 * Convert hex to RGBA
344 *
345 * @since 0.1
346 * @param string $hex The hex value.
347 * @param int $alpha The opacity value.
348 *
349 * @return string The RGBA value.
350 */
351 function generateblocks_hex2rgba( $hex, $alpha ) {
352 if ( ! $hex ) {
353 return;
354 }
355
356 if ( 1 === $alpha ) {
357 return $hex;
358 }
359
360 $hex = str_replace( '#', '', $hex );
361
362 if ( strlen( $hex ) == 3 ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
363 $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
364 $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
365 $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
366 } else {
367 $r = hexdec( substr( $hex, 0, 2 ) );
368 $g = hexdec( substr( $hex, 2, 2 ) );
369 $b = hexdec( substr( $hex, 4, 2 ) );
370 }
371
372 $rgba = 'rgba(' . $r . ', ' . $g . ', ' . $b . ', ' . $alpha . ')';
373
374 return $rgba;
375 }
376
377 /**
378 * Return old flexblocks values for old browsers.
379 *
380 * @since 0.1
381 * @param string $value The value to convert.
382 *
383 * @return string The old browser value.
384 */
385 function generateblocks_get_vendor_prefix( $value ) {
386 if ( 'flex-start' === $value || 'left' === $value ) {
387 return 'start';
388 }
389
390 if ( 'flex-end' === $value || 'right' === $value ) {
391 return 'end';
392 }
393
394 return $value;
395 }
396
397 /**
398 * Return flexbox alignment values from left/right.
399 *
400 * @since 0.1
401 * @param string $value The value to convert.
402 *
403 * @return string The flexbox alignment value.
404 */
405 function generateblocks_get_flexbox_alignment( $value ) {
406 if ( 'left' === $value || 'top' === $value ) {
407 return 'flex-start';
408 }
409
410 if ( 'right' === $value || 'bottom' === $value ) {
411 return 'flex-end';
412 }
413
414 return $value;
415 }
416
417 /**
418 * Get an option from the database.
419 *
420 * @param string $option The option to get.
421 * @since 0.1
422 */
423 function generateblocks_get_option( $option ) {
424 $defaults = generateblocks_get_option_defaults();
425
426 if ( ! isset( $defaults[ $option ] ) ) {
427 return;
428 }
429
430 $options = wp_parse_args(
431 get_option( 'generateblocks', array() ),
432 $defaults
433 );
434
435 return $options[ $option ];
436 }
437
438 /**
439 * Checks whether a value exists, even if it's a 0.
440 *
441 * @param int|string $value The value to check.
442 * @since 1.0
443 */
444 function generateblocks_has_number_value( $value ) {
445 if ( $value || 0 === $value || '0' === $value ) {
446 return true;
447 }
448
449 return false;
450 }
451
452 /**
453 * Get the background-image value.
454 *
455 * @param array $settings Our background image settings.
456 * @param array $custom_args Custom args that will overwrite the settings.
457 */
458 function generateblocks_get_background_image_css( $settings, $custom_args = array() ) {
459 $args = array(
460 'backgroundColor' => $settings['backgroundColor'],
461 'backgroundColorOpacity' => $settings['backgroundColorOpacity'],
462 'gradient' => $settings['gradient'],
463 'gradientDirection' => $settings['gradientDirection'],
464 'gradientColorOne' => $settings['gradientColorOne'],
465 'gradientColorOneOpacity' => $settings['gradientColorOneOpacity'],
466 'gradientColorStopOne' => $settings['gradientColorStopOne'],
467 'gradientColorTwo' => $settings['gradientColorTwo'],
468 'gradientColorTwoOpacity' => $settings['gradientColorTwoOpacity'],
469 'gradientColorStopTwo' => $settings['gradientColorStopTwo'],
470 'bgImage' => $settings['bgImage'],
471 'bgOptions' => $settings['bgOptions'],
472 );
473
474 $args = wp_parse_args(
475 $args,
476 $custom_args
477 );
478
479 $background_image = false;
480 $gradientColorStopOneValue = '';
481 $gradientColorStopTwoValue = '';
482
483 $args['backgroundColor'] = generateblocks_hex2rgba( $args['backgroundColor'], $args['backgroundColorOpacity'] );
484 $args['gradientColorOne'] = generateblocks_hex2rgba( $args['gradientColorOne'], $args['gradientColorOneOpacity'] );
485 $args['gradientColorTwo'] = generateblocks_hex2rgba( $args['gradientColorTwo'], $args['gradientColorTwoOpacity'] );
486
487 if ( $args['gradient'] ) {
488 if ( $args['gradientColorOne'] && '' !== $args['gradientColorStopOne'] ) {
489 $gradientColorStopOneValue = ' ' . $args['gradientColorStopOne'] . '%';
490 }
491
492 if ( $args['gradientColorTwo'] && '' !== $args['gradientColorStopTwo'] ) {
493 $gradientColorStopTwoValue = ' ' . $args['gradientColorStopTwo'] . '%';
494 }
495 }
496
497 if ( $args['bgImage'] && 'element' === $args['bgOptions']['selector'] ) {
498 $url = $args['bgImage']['image']['url'];
499
500 if ( ( $args['backgroundColor'] || $args['gradient'] ) && isset( $args['bgOptions']['overlay'] ) && $args['bgOptions']['overlay'] ) {
501 if ( $args['gradient'] ) {
502 $background_image = 'linear-gradient(' . $args['gradientDirection'] . 'deg, ' . $args['gradientColorOne'] . $gradientColorStopOneValue . ', ' . $args['gradientColorTwo'] . $gradientColorStopTwoValue . '), url(' . esc_url( $url ) . ')';
503 } elseif ( $args['backgroundColor'] ) {
504 $background_image = 'linear-gradient(0deg, ' . $args['backgroundColor'] . ', ' . $args['backgroundColor'] . '), url(' . esc_url( $url ) . ')';
505 }
506 } else {
507 $background_image = 'url(' . esc_url( $url ) . ')';
508 }
509 } elseif ( $args['gradient'] ) {
510 $background_image = 'linear-gradient(' . $args['gradientDirection'] . 'deg, ' . $args['gradientColorOne'] . $gradientColorStopOneValue . ', ' . $args['gradientColorTwo'] . $gradientColorStopTwoValue . ')';
511 }
512
513 return $background_image;
514 }
515