class-do-css.php
6 years ago
class-enqueue-css.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
433 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 | * |
| 19 | * @return array |
| 20 | */ |
| 21 | function generateblocks_get_block_data( $content, $data = array() ) { |
| 22 | if ( ! is_array( $content ) || empty( $content ) ) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | foreach ( $content as $index => $block ) { |
| 27 | if ( ! is_object( $block ) && is_array( $block ) && isset( $block['blockName'] ) ) { |
| 28 | if ( 'generateblocks/grid' === $block['blockName'] ) { |
| 29 | $data['grid'][] = $block['attrs']; |
| 30 | $data['tempGridId'] = $block['attrs']['uniqueId']; |
| 31 | } |
| 32 | |
| 33 | if ( 'generateblocks/container' === $block['blockName'] ) { |
| 34 | if ( isset( $block['attrs']['isGrid'] ) && $block['attrs']['isGrid'] && isset( $data['tempGridId'] ) ) { |
| 35 | $block['attrs']['gridId'] = $data['tempGridId']; |
| 36 | } |
| 37 | |
| 38 | $data['container'][] = $block['attrs']; |
| 39 | } |
| 40 | |
| 41 | if ( 'generateblocks/headline' === $block['blockName'] ) { |
| 42 | $data['headline'][] = $block['attrs']; |
| 43 | } |
| 44 | |
| 45 | if ( 'generateblocks/button-container' === $block['blockName'] ) { |
| 46 | $data['button-container'][] = $block['attrs']; |
| 47 | } |
| 48 | |
| 49 | if ( 'generateblocks/button' === $block['blockName'] ) { |
| 50 | $data['button'][] = $block['attrs']; |
| 51 | } |
| 52 | |
| 53 | if ( 'core/block' === $block['blockName'] ) { |
| 54 | if ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) { |
| 55 | $atts = $block['attrs']; |
| 56 | |
| 57 | if ( isset( $atts['ref'] ) ) { |
| 58 | $reusable_block = get_post( $atts['ref'] ); |
| 59 | |
| 60 | if ( $reusable_block && 'wp_block' === $reusable_block->post_type ) { |
| 61 | $reuse_data_block = parse_blocks( $reusable_block->post_content ); |
| 62 | $data = generateblocks_get_block_data( $reuse_data_block, $data ); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) { |
| 69 | $data = generateblocks_get_block_data( $block['innerBlocks'], $data ); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return $data; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Shorthand CSS values (padding, margin, border etc..). |
| 79 | * |
| 80 | * @since 0.1 |
| 81 | * |
| 82 | * @param int $top The first value. |
| 83 | * @param int $right The second value. |
| 84 | * @param int $bottom The third value. |
| 85 | * @param int $left The fourth value. |
| 86 | * @param string $unit The unit we're adding. |
| 87 | * |
| 88 | * @return string The shorthand value. |
| 89 | */ |
| 90 | function generateblocks_get_shorthand_css( $top, $right, $bottom, $left, $unit ) { |
| 91 | if ( '' === $top && '' === $right && '' === $bottom && '' === $left ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $top = ( floatval( $top ) <> 0 ) ? floatval( $top ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 96 | $right = ( floatval( $right ) <> 0 ) ? floatval( $right ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 97 | $bottom = ( floatval( $bottom ) <> 0 ) ? floatval( $bottom ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 98 | $left = ( floatval( $left ) <> 0 ) ? floatval( $left ) . $unit . ' ' : '0 '; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 99 | |
| 100 | if ( $right === $left ) { |
| 101 | $left = ''; |
| 102 | |
| 103 | if ( $top === $bottom ) { |
| 104 | $bottom = ''; |
| 105 | |
| 106 | if ( $top === $right ) { |
| 107 | $right = ''; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return trim( $top . $right . $bottom . $left ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get our media query. |
| 117 | * |
| 118 | * @since 0.1 |
| 119 | * @param string $type The media query we're getting. |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | function generateblocks_get_media_query( $type ) { |
| 124 | $queries = apply_filters( |
| 125 | 'generateblocks_media_query', |
| 126 | array( |
| 127 | 'mobile' => '(max-width: 767px)', |
| 128 | 'tablet' => '(max-width: 1024px)', |
| 129 | ) |
| 130 | ); |
| 131 | |
| 132 | return $queries[ $type ]; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Build our list of Google fonts on this page. |
| 137 | * |
| 138 | * @since 0.1 |
| 139 | * |
| 140 | * @param string $content The content to parse. |
| 141 | * |
| 142 | * @return array |
| 143 | */ |
| 144 | function generateblocks_get_google_fonts( $content = '' ) { |
| 145 | if ( ! function_exists( 'has_blocks' ) ) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if ( ! $content && has_blocks( get_the_ID() ) ) { |
| 150 | global $post; |
| 151 | |
| 152 | if ( ! is_object( $post ) ) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | $content = $post->post_content; |
| 157 | } |
| 158 | |
| 159 | $content = apply_filters( 'generateblocks_do_content', $content ); |
| 160 | |
| 161 | if ( ! function_exists( 'parse_blocks' ) ) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | $content = parse_blocks( $content ); |
| 166 | |
| 167 | if ( ! $content ) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | $data = generateblocks_get_block_data( $content ); |
| 172 | |
| 173 | $defaults = generateblocks_get_block_defaults(); |
| 174 | $font_data = array(); |
| 175 | |
| 176 | if ( ! empty( $data ) ) { |
| 177 | foreach ( $data as $name => $blockData ) { |
| 178 | if ( 'button' === $name ) { |
| 179 | foreach ( $blockData as $atts ) { |
| 180 | $button_settings = wp_parse_args( |
| 181 | $atts, |
| 182 | $defaults['button'] |
| 183 | ); |
| 184 | |
| 185 | if ( $button_settings['googleFont'] ) { |
| 186 | $id = $atts['uniqueId']; |
| 187 | |
| 188 | $variants = $button_settings['googleFontVariants']; |
| 189 | |
| 190 | if ( $variants ) { |
| 191 | $variants = str_replace( ' ', '', $variants ); |
| 192 | $variants = explode( ',', $variants ); |
| 193 | } |
| 194 | |
| 195 | $font_data[ $id ] = array( |
| 196 | 'name' => $button_settings['fontFamily'], |
| 197 | 'variants' => $variants, |
| 198 | ); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if ( 'headline' === $name ) { |
| 204 | foreach ( $blockData as $atts ) { |
| 205 | $headline_settings = wp_parse_args( |
| 206 | $atts, |
| 207 | $defaults['headline'] |
| 208 | ); |
| 209 | |
| 210 | if ( $headline_settings['googleFont'] ) { |
| 211 | $id = $atts['uniqueId']; |
| 212 | $variants = $headline_settings['googleFontVariants']; |
| 213 | |
| 214 | if ( $variants ) { |
| 215 | $variants = str_replace( ' ', '', $variants ); |
| 216 | $variants = explode( ',', $variants ); |
| 217 | } |
| 218 | |
| 219 | $font_data[ $id ] = array( |
| 220 | 'name' => $headline_settings['fontFamily'], |
| 221 | 'variants' => $variants, |
| 222 | ); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if ( 'container' === $name ) { |
| 228 | foreach ( $blockData as $atts ) { |
| 229 | $container_settings = wp_parse_args( |
| 230 | $atts, |
| 231 | $defaults['container'] |
| 232 | ); |
| 233 | |
| 234 | if ( $container_settings['googleFont'] ) { |
| 235 | $id = $atts['uniqueId']; |
| 236 | $variants = $container_settings['googleFontVariants']; |
| 237 | |
| 238 | if ( $variants ) { |
| 239 | $variants = str_replace( ' ', '', $variants ); |
| 240 | $variants = explode( ',', $variants ); |
| 241 | } |
| 242 | |
| 243 | $font_data[ $id ] = array( |
| 244 | 'name' => $container_settings['fontFamily'], |
| 245 | 'variants' => $variants, |
| 246 | ); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | $fonts = array(); |
| 254 | |
| 255 | foreach ( (array) $font_data as $font ) { |
| 256 | $id = str_replace( ' ', '', strtolower( $font['name'] ) ); |
| 257 | |
| 258 | $fonts[ $id ]['name'] = $font['name']; |
| 259 | |
| 260 | if ( ! empty( $font['variants'] ) ) { |
| 261 | foreach ( $font['variants'] as $variant ) { |
| 262 | if ( isset( $fonts[ $id ]['variants'] ) ) { |
| 263 | if ( in_array( $variant, (array) $fonts[ $id ]['variants'] ) ) { |
| 264 | continue; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | $fonts[ $id ]['variants'][] = $variant; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return apply_filters( 'generateblocks_google_fonts', $fonts ); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Build the Google Font request URI. |
| 278 | * |
| 279 | * @since 0.1 |
| 280 | * |
| 281 | * @return string The request URI to Google Fonts. |
| 282 | */ |
| 283 | function generateblocks_get_google_fonts_uri() { |
| 284 | $google_fonts = generateblocks_get_google_fonts(); |
| 285 | |
| 286 | if ( ! $google_fonts ) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | $data = array(); |
| 291 | |
| 292 | foreach ( $google_fonts as $font ) { |
| 293 | $variants = array(); |
| 294 | |
| 295 | if ( ! empty( $font['variants'] ) ) { |
| 296 | foreach ( $font['variants'] as $variant ) { |
| 297 | $variants[] = $variant; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | $variants = apply_filters( 'generateblocks_google_font_variants', $variants, $font['name'] ); |
| 302 | |
| 303 | $name = str_replace( ' ', '+', $font['name'] ); |
| 304 | |
| 305 | if ( $variants ) { |
| 306 | $data[] = $name . ':' . implode( ',', $variants ); |
| 307 | } else { |
| 308 | $data[] = $name; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | $font_args = apply_filters( |
| 313 | 'generateblocks_google_font_args', |
| 314 | array( |
| 315 | 'family' => implode( '|', $data ), |
| 316 | 'subset' => null, |
| 317 | 'display' => 'swap', |
| 318 | ) |
| 319 | ); |
| 320 | |
| 321 | return add_query_arg( $font_args, '//fonts.googleapis.com/css' ); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Convert hex to RGBA |
| 326 | * |
| 327 | * @since 0.1 |
| 328 | * @param string $hex The hex value. |
| 329 | * @param int $alpha The opacity value. |
| 330 | * |
| 331 | * @return string The RGBA value. |
| 332 | */ |
| 333 | function generateblocks_hex2rgba( $hex, $alpha ) { |
| 334 | if ( ! $hex ) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | if ( 1 === $alpha ) { |
| 339 | return $hex; |
| 340 | } |
| 341 | |
| 342 | $hex = str_replace( '#', '', $hex ); |
| 343 | |
| 344 | if ( strlen( $hex ) == 3 ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 345 | $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) ); |
| 346 | $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) ); |
| 347 | $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) ); |
| 348 | } else { |
| 349 | $r = hexdec( substr( $hex, 0, 2 ) ); |
| 350 | $g = hexdec( substr( $hex, 2, 2 ) ); |
| 351 | $b = hexdec( substr( $hex, 4, 2 ) ); |
| 352 | } |
| 353 | |
| 354 | $rgba = 'rgba(' . $r . ', ' . $g . ', ' . $b . ', ' . $alpha . ')'; |
| 355 | |
| 356 | return $rgba; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Return old flexblocks values for old browsers. |
| 361 | * |
| 362 | * @since 0.1 |
| 363 | * @param string $value The value to convert. |
| 364 | * |
| 365 | * @return string The old browser value. |
| 366 | */ |
| 367 | function generateblocks_get_vendor_prefix( $value ) { |
| 368 | if ( 'flex-start' === $value || 'left' === $value ) { |
| 369 | return 'start'; |
| 370 | } |
| 371 | |
| 372 | if ( 'flex-end' === $value || 'right' === $value ) { |
| 373 | return 'end'; |
| 374 | } |
| 375 | |
| 376 | return $value; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Return flexbox alignment values from left/right. |
| 381 | * |
| 382 | * @since 0.1 |
| 383 | * @param string $value The value to convert. |
| 384 | * |
| 385 | * @return string The flexbox alignment value. |
| 386 | */ |
| 387 | function generateblocks_get_flexbox_alignment( $value ) { |
| 388 | if ( 'left' === $value || 'top' === $value ) { |
| 389 | return 'flex-start'; |
| 390 | } |
| 391 | |
| 392 | if ( 'right' === $value || 'bottom' === $value ) { |
| 393 | return 'flex-end'; |
| 394 | } |
| 395 | |
| 396 | return $value; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Get an option from the database. |
| 401 | * |
| 402 | * @param string $option The option to get. |
| 403 | * @since 0.1 |
| 404 | */ |
| 405 | function generateblocks_get_option( $option ) { |
| 406 | $defaults = generateblocks_get_option_defaults(); |
| 407 | |
| 408 | if ( ! isset( $defaults[ $option ] ) ) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | $options = wp_parse_args( |
| 413 | get_option( 'generateblocks', array() ), |
| 414 | $defaults |
| 415 | ); |
| 416 | |
| 417 | return $options[ $option ]; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Checks whether a value exists, even if it's a 0. |
| 422 | * |
| 423 | * @param int|string $value The value to check. |
| 424 | * @since 1.0 |
| 425 | */ |
| 426 | function generateblocks_has_number_value( $value ) { |
| 427 | if ( $value || 0 === $value || '0' === $value ) { |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | return false; |
| 432 | } |
| 433 |