| @@ -1,780 +1,780 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace LearnPress\Gutenberg\Utils; | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * StyleAttributes class used for getting class and style from attributes. | |
| 7 | - */ | |
| 8 | -class StyleAttributes { | |
| 9 | - | |
| 10 | - // Empty style array. | |
| 11 | - const EMPTY_STYLE = [ | |
| 12 | - 'class' => '', | |
| 13 | - 'style' => '', | |
| 14 | - 'value' => '', | |
| 15 | - ]; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * If color value is in preset format, convert it to a CSS var. Else return same value | |
| 19 | - * For example: | |
| 20 | - * "var:preset|color|pale-pink" -> "var(--wp--preset--color--pale-pink)" | |
| 21 | - * "#98b66e" -> "#98b66e" | |
| 22 | - * | |
| 23 | - * @param string $color_value value to be processed. | |
| 24 | - * | |
| 25 | - * @return (string) | |
| 26 | - */ | |
| 27 | - public static function get_color_value( $color_value ) { | |
| 28 | - if ( is_string( $color_value ) && str_contains( $color_value, 'var:preset|color|' ) ) { | |
| 29 | - $color_value = str_replace( 'var:preset|color|', '', $color_value ); | |
| 30 | - return sprintf( 'var(--wp--preset--color--%s)', $color_value ); | |
| 31 | - } | |
| 32 | - | |
| 33 | - return $color_value; | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * Get CSS value for color preset. | |
| 38 | - * | |
| 39 | - * @param string $preset_name Preset name. | |
| 40 | - * | |
| 41 | - * @return string CSS value for color preset. | |
| 42 | - */ | |
| 43 | - public static function get_preset_value( $preset_name ) { | |
| 44 | - return "var(--wp--preset--color--$preset_name)"; | |
| 45 | - } | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Get CSS value for font size. | |
| 49 | - * | |
| 50 | - * @param string $preset_name Preset name. | |
| 51 | - * | |
| 52 | - * @return string CSS value for font size. | |
| 53 | - */ | |
| 54 | - public static function get_font_size_value( $preset_name ) { | |
| 55 | - return "var(--wp--preset--font-size--$preset_name)"; | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * If spacing value is in preset format, convert it to a CSS var. Else return same value | |
| 60 | - * For example: | |
| 61 | - * "var:preset|spacing|50" -> "var(--wp--preset--spacing--50)" | |
| 62 | - * "50px" -> "50px" | |
| 63 | - * | |
| 64 | - * @param string $spacing_value value to be processed. | |
| 65 | - * | |
| 66 | - * @return (string) | |
| 67 | - */ | |
| 68 | - public static function get_spacing_value( $spacing_value ) { | |
| 69 | - // Used following code as reference: https://github.com/WordPress/gutenberg/blob/cff6d70d6ff5a26e212958623dc3130569f95685/lib/block-supports/layout.php/#L219-L225. | |
| 70 | - if ( is_string( $spacing_value ) && str_contains( $spacing_value, 'var:preset|spacing|' ) ) { | |
| 71 | - $spacing_value = str_replace( 'var:preset|spacing|', '', $spacing_value ); | |
| 72 | - return sprintf( 'var(--wp--preset--spacing--%s)', $spacing_value ); | |
| 73 | - } | |
| 74 | - | |
| 75 | - return $spacing_value; | |
| 76 | - } | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * Get class and style for align from attributes. | |
| 80 | - * | |
| 81 | - * @param array $attributes Block attributes. | |
| 82 | - * @return array | |
| 83 | - */ | |
| 84 | - public static function get_align_class_and_style( $attributes ) { | |
| 85 | - $align_attribute = $attributes['align'] ?? null; | |
| 86 | - | |
| 87 | - if ( 'wide' === $align_attribute ) { | |
| 88 | - return array( | |
| 89 | - 'class' => 'alignwide', | |
| 90 | - 'style' => null, | |
| 91 | - ); | |
| 92 | - } | |
| 93 | - | |
| 94 | - if ( 'full' === $align_attribute ) { | |
| 95 | - return array( | |
| 96 | - 'class' => 'alignfull', | |
| 97 | - 'style' => null, | |
| 98 | - ); | |
| 99 | - } | |
| 100 | - | |
| 101 | - if ( 'left' === $align_attribute ) { | |
| 102 | - return array( | |
| 103 | - 'class' => 'alignleft', | |
| 104 | - 'style' => null, | |
| 105 | - ); | |
| 106 | - } | |
| 107 | - | |
| 108 | - if ( 'right' === $align_attribute ) { | |
| 109 | - return array( | |
| 110 | - 'class' => 'alignright', | |
| 111 | - 'style' => null, | |
| 112 | - ); | |
| 113 | - } | |
| 114 | - | |
| 115 | - if ( 'center' === $align_attribute ) { | |
| 116 | - return array( | |
| 117 | - 'class' => 'aligncenter', | |
| 118 | - 'style' => null, | |
| 119 | - ); | |
| 120 | - } | |
| 121 | - | |
| 122 | - return self::EMPTY_STYLE; | |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * Get class and style for background-color from attributes. | |
| 127 | - * | |
| 128 | - * @param array $attributes Block attributes. | |
| 129 | - * @return array | |
| 130 | - */ | |
| 131 | - public static function get_background_color_class_and_style( $attributes ) { | |
| 132 | - $gradient = $attributes['gradient'] ?? null; | |
| 133 | - $background_color = $attributes['backgroundColor'] ?? ''; | |
| 134 | - $custom_background_color = $attributes['style']['color']['background'] ?? ''; | |
| 135 | - $classes = [ $gradient ]; | |
| 136 | - $styles = []; | |
| 137 | - $value = null; | |
| 138 | - | |
| 139 | - if ( $background_color || $custom_background_color || $gradient ) { | |
| 140 | - $classes[] = 'has-background'; | |
| 141 | - } | |
| 142 | - | |
| 143 | - if ( $background_color ) { | |
| 144 | - $value = self::get_preset_value( $background_color ); | |
| 145 | - $styles[] = sprintf( 'background-color: %s;', $value ); | |
| 146 | - } | |
| 147 | - | |
| 148 | - if ( $custom_background_color ) { | |
| 149 | - $styles[] = sprintf( 'background-color: %s;', $custom_background_color ); | |
| 150 | - $value = $custom_background_color; | |
| 151 | - } | |
| 152 | - | |
| 153 | - if ( $gradient ) { | |
| 154 | - $classes[] = sprintf( 'has-%s-gradient-background', $gradient ); | |
| 155 | - } | |
| 156 | - | |
| 157 | - return array( | |
| 158 | - 'class' => self::join_styles( $classes ), | |
| 159 | - 'style' => self::join_styles( $styles ), | |
| 160 | - 'value' => $value, | |
| 161 | - ); | |
| 162 | - } | |
| 163 | - | |
| 164 | - /** | |
| 165 | - * Join classes and styles while removing duplicates and null values. | |
| 166 | - * | |
| 167 | - * @param array $rules Array of classes or styles. | |
| 168 | - * @return array | |
| 169 | - */ | |
| 170 | - protected static function join_styles( $rules ) { | |
| 171 | - return implode( ' ', array_unique( array_filter( $rules ) ) ); | |
| 172 | - } | |
| 173 | - | |
| 174 | - /** | |
| 175 | - * Get class and style for border-color from attributes. | |
| 176 | - * | |
| 177 | - * Data passed to this function is not always consistent. It can be: | |
| 178 | - * Linked - preset color: $attributes['borderColor'] => 'luminous-vivid-orange'. | |
| 179 | - * Linked - custom color: $attributes['style']['border']['color'] => '#681228'. | |
| 180 | - * Unlinked - preset color: $attributes['style']['border']['top']['color'] => 'var:preset|color|luminous-vivid-orange' | |
| 181 | - * Unlinked - custom color: $attributes['style']['border']['top']['color'] => '#681228'. | |
| 182 | - * | |
| 183 | - * @param array $attributes Block attributes. | |
| 184 | - * @return array | |
| 185 | - */ | |
| 186 | - public static function get_border_color_class_and_style( $attributes ) { | |
| 187 | - $border_color_linked_preset = $attributes['borderColor'] ?? ''; | |
| 188 | - $border_color_linked_custom = $attributes['style']['border']['color'] ?? ''; | |
| 189 | - $custom_border = $attributes['style']['border'] ?? ''; | |
| 190 | - | |
| 191 | - $class = ''; | |
| 192 | - $style = ''; | |
| 193 | - $value = ''; | |
| 194 | - | |
| 195 | - if ( $border_color_linked_preset ) { | |
| 196 | - // Linked preset color. | |
| 197 | - $value = self::get_preset_value( $border_color_linked_preset ); | |
| 198 | - $style .= 'border-color:' . $value . ';'; | |
| 199 | - } elseif ( $border_color_linked_custom ) { | |
| 200 | - // Linked custom color. | |
| 201 | - $style .= 'border-color:' . $border_color_linked_custom . ';'; | |
| 202 | - $value = $border_color_linked_custom; | |
| 203 | - } elseif ( is_array( $custom_border ) ) { | |
| 204 | - // $style .= 'border-style: solid;'; | |
| 205 | - // Unlinked. | |
| 206 | - foreach ( $custom_border as $border_color_key => $border_color_value ) { | |
| 207 | - if ( is_array( $border_color_value ) && array_key_exists( 'color', ( $border_color_value ) ) ) { | |
| 208 | - $style .= 'border-' . $border_color_key . '-color:' . self::get_color_value( $border_color_value['color'] ) . ';'; | |
| 209 | - } | |
| 210 | - } | |
| 211 | - } | |
| 212 | - | |
| 213 | - if ( ! $class && ! $style ) { | |
| 214 | - return self::EMPTY_STYLE; | |
| 215 | - } | |
| 216 | - | |
| 217 | - return array( | |
| 218 | - 'class' => $class, | |
| 219 | - 'style' => $style, | |
| 220 | - 'value' => $value, | |
| 221 | - ); | |
| 222 | - } | |
| 223 | - | |
| 224 | - /** | |
| 225 | - * Get class and style for border-radius from attributes. | |
| 226 | - * | |
| 227 | - * @param array $attributes Block attributes. | |
| 228 | - * @return array | |
| 229 | - */ | |
| 230 | - public static function get_border_radius_class_and_style( $attributes ) { | |
| 231 | - $custom_border_radius = $attributes['style']['border']['radius'] ?? ''; | |
| 232 | - | |
| 233 | - if ( '' === $custom_border_radius ) { | |
| 234 | - return self::EMPTY_STYLE; | |
| 235 | - } | |
| 236 | - | |
| 237 | - $style = ''; | |
| 238 | - | |
| 239 | - if ( is_string( $custom_border_radius ) ) { | |
| 240 | - // Linked sides. | |
| 241 | - $style = 'border-radius:' . $custom_border_radius . ';'; | |
| 242 | - } else { | |
| 243 | - // Unlinked sides. | |
| 244 | - $border_radius = array(); | |
| 245 | - | |
| 246 | - $border_radius['border-top-left-radius'] = $custom_border_radius['topLeft'] ?? ''; | |
| 247 | - $border_radius['border-top-right-radius'] = $custom_border_radius['topRight'] ?? ''; | |
| 248 | - $border_radius['border-bottom-right-radius'] = $custom_border_radius['bottomRight'] ?? ''; | |
| 249 | - $border_radius['border-bottom-left-radius'] = $custom_border_radius['bottomLeft'] ?? ''; | |
| 250 | - | |
| 251 | - foreach ( $border_radius as $border_radius_side => $border_radius_value ) { | |
| 252 | - if ( '' !== $border_radius_value ) { | |
| 253 | - $style .= $border_radius_side . ':' . $border_radius_value . ';'; | |
| 254 | - } | |
| 255 | - } | |
| 256 | - } | |
| 257 | - | |
| 258 | - return array( | |
| 259 | - 'class' => null, | |
| 260 | - 'style' => $style, | |
| 261 | - ); | |
| 262 | - } | |
| 263 | - | |
| 264 | - /** | |
| 265 | - * Get class and style for border width from attributes. | |
| 266 | - * | |
| 267 | - * @param array $attributes Block attributes. | |
| 268 | - * @return array | |
| 269 | - */ | |
| 270 | - public static function get_border_width_class_and_style( $attributes ) { | |
| 271 | - $custom_border = $attributes['style']['border'] ?? ''; | |
| 272 | - | |
| 273 | - if ( '' === $custom_border ) { | |
| 274 | - return self::EMPTY_STYLE; | |
| 275 | - } | |
| 276 | - | |
| 277 | - $style = ''; | |
| 278 | - | |
| 279 | - if ( array_key_exists( 'width', ( $custom_border ) ) && ! empty( $custom_border['width'] ) ) { | |
| 280 | - // Linked sides. | |
| 281 | - $style .= 'border-style: solid;'; | |
| 282 | - $style .= 'border-width:' . $custom_border['width'] . ';'; | |
| 283 | - } else { | |
| 284 | - // Unlinked sides. | |
| 285 | - foreach ( $custom_border as $border_width_side => $border_width_value ) { | |
| 286 | - if ( isset( $border_width_value['width'] ) ) { | |
| 287 | - $style .= 'border-' . $border_width_side . '-style: solid;'; | |
| 288 | - $style .= 'border-' . $border_width_side . '-width:' . $border_width_value['width'] . ';'; | |
| 289 | - } | |
| 290 | - } | |
| 291 | - } | |
| 292 | - | |
| 293 | - return array( | |
| 294 | - 'class' => null, | |
| 295 | - 'style' => $style, | |
| 296 | - ); | |
| 297 | - } | |
| 298 | - | |
| 299 | - /** | |
| 300 | - * Get class and style for border width from attributes. | |
| 301 | - * | |
| 302 | - * @param array $attributes Block attributes. | |
| 303 | - * @return array | |
| 304 | - */ | |
| 305 | - public static function get_border_style_class_and_style( $attributes ) { | |
| 306 | - $custom_border = $attributes['style']['border'] ?? ''; | |
| 307 | - | |
| 308 | - if ( '' === $custom_border ) { | |
| 309 | - return self::EMPTY_STYLE; | |
| 310 | - } | |
| 311 | - | |
| 312 | - $style = ''; | |
| 313 | - | |
| 314 | - if ( array_key_exists( 'style', ( $custom_border ) ) && ! empty( $custom_border['style'] ) ) { | |
| 315 | - $style = 'border-style:' . $custom_border['style'] . ';'; | |
| 316 | - } else { | |
| 317 | - foreach ( $custom_border as $side => $value ) { | |
| 318 | - if ( isset( $value['style'] ) ) { | |
| 319 | - $style .= 'border-' . $side . '-style:' . $value['style'] . ';'; | |
| 320 | - } | |
| 321 | - } | |
| 322 | - } | |
| 323 | - | |
| 324 | - return array( | |
| 325 | - 'class' => null, | |
| 326 | - 'style' => $style, | |
| 327 | - ); | |
| 328 | - } | |
| 329 | - | |
| 330 | - /** | |
| 331 | - * Get space-separated classes from block attributes. | |
| 332 | - * | |
| 333 | - * @param array $attributes Block attributes. | |
| 334 | - * @param array $properties Properties to get classes from. | |
| 335 | - * | |
| 336 | - * @return string Space-separated classes. | |
| 337 | - */ | |
| 338 | - public static function get_classes_by_attributes( $attributes, $properties = array() ) { | |
| 339 | - $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); | |
| 340 | - | |
| 341 | - return $classes_and_styles['classes']; | |
| 342 | - } | |
| 343 | - | |
| 344 | - /** | |
| 345 | - * Get class and style for font-family from attributes. | |
| 346 | - * | |
| 347 | - * @param array $attributes Block attributes. | |
| 348 | - * @return array | |
| 349 | - */ | |
| 350 | - public static function get_font_family_class_and_style( $attributes ) { | |
| 351 | - | |
| 352 | - $font_family = $attributes['fontFamily'] ?? ''; | |
| 353 | - | |
| 354 | - if ( $font_family ) { | |
| 355 | - return array( | |
| 356 | - 'class' => sprintf( 'has-%s-font-family', $font_family ), | |
| 357 | - 'style' => null, | |
| 358 | - ); | |
| 359 | - } | |
| 360 | - return self::EMPTY_STYLE; | |
| 361 | - } | |
| 362 | - | |
| 363 | - /** | |
| 364 | - * Get class and style for font-size from attributes. | |
| 365 | - * | |
| 366 | - * @param array $attributes Block attributes. | |
| 367 | - * @return array | |
| 368 | - */ | |
| 369 | - public static function get_font_size_class_and_style( $attributes ) { | |
| 370 | - | |
| 371 | - $font_size = $attributes['fontSize'] ?? ''; | |
| 372 | - | |
| 373 | - $custom_font_size = $attributes['style']['typography']['fontSize'] ?? ''; | |
| 374 | - | |
| 375 | - if ( ! $font_size && '' === $custom_font_size ) { | |
| 376 | - return self::EMPTY_STYLE; | |
| 377 | - } | |
| 378 | - | |
| 379 | - if ( $font_size ) { | |
| 380 | - return array( | |
| 381 | - 'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ), | |
| 382 | - 'style' => sprintf( 'font-size: %s;', self::get_font_size_value( $font_size ) ), | |
| 383 | - ); | |
| 384 | - } elseif ( '' !== $custom_font_size ) { | |
| 385 | - return array( | |
| 386 | - 'class' => null, | |
| 387 | - 'style' => sprintf( 'font-size: %s;', $custom_font_size ), | |
| 388 | - ); | |
| 389 | - } | |
| 390 | - | |
| 391 | - return self::EMPTY_STYLE; | |
| 392 | - } | |
| 393 | - | |
| 394 | - /** | |
| 395 | - * Get class and style for font-style from attributes. | |
| 396 | - * | |
| 397 | - * @param array $attributes Block attributes. | |
| 398 | - * @return array | |
| 399 | - */ | |
| 400 | - public static function get_font_style_class_and_style( $attributes ) { | |
| 401 | - | |
| 402 | - $custom_font_style = $attributes['style']['typography']['fontStyle'] ?? ''; | |
| 403 | - | |
| 404 | - if ( '' !== $custom_font_style ) { | |
| 405 | - return array( | |
| 406 | - 'class' => null, | |
| 407 | - 'style' => sprintf( 'font-style: %s;', $custom_font_style ), | |
| 408 | - ); | |
| 409 | - } | |
| 410 | - return self::EMPTY_STYLE; | |
| 411 | - } | |
| 412 | - | |
| 413 | - /** | |
| 414 | - * Get class and style for font-weight from attributes. | |
| 415 | - * | |
| 416 | - * @param array $attributes Block attributes. | |
| 417 | - * @return array | |
| 418 | - */ | |
| 419 | - public static function get_font_weight_class_and_style( $attributes ) { | |
| 420 | - | |
| 421 | - $custom_font_weight = $attributes['style']['typography']['fontWeight'] ?? ''; | |
| 422 | - | |
| 423 | - if ( '' !== $custom_font_weight ) { | |
| 424 | - return array( | |
| 425 | - 'class' => null, | |
| 426 | - 'style' => sprintf( 'font-weight: %s;', $custom_font_weight ), | |
| 427 | - ); | |
| 428 | - } | |
| 429 | - return self::EMPTY_STYLE; | |
| 430 | - } | |
| 431 | - | |
| 432 | - /** | |
| 433 | - * Get class and style for letter-spacing from attributes. | |
| 434 | - * | |
| 435 | - * @param array $attributes Block attributes. | |
| 436 | - * @return array | |
| 437 | - */ | |
| 438 | - public static function get_letter_spacing_class_and_style( $attributes ) { | |
| 439 | - | |
| 440 | - $custom_letter_spacing = $attributes['style']['typography']['letterSpacing'] ?? ''; | |
| 441 | - | |
| 442 | - if ( '' !== $custom_letter_spacing ) { | |
| 443 | - return array( | |
| 444 | - 'class' => null, | |
| 445 | - 'style' => sprintf( 'letter-spacing: %s;', $custom_letter_spacing ), | |
| 446 | - ); | |
| 447 | - } | |
| 448 | - return self::EMPTY_STYLE; | |
| 449 | - } | |
| 450 | - | |
| 451 | - /** | |
| 452 | - * Get class and style for line height from attributes. | |
| 453 | - * | |
| 454 | - * @param array $attributes Block attributes. | |
| 455 | - * @return array | |
| 456 | - */ | |
| 457 | - public static function get_line_height_class_and_style( $attributes ) { | |
| 458 | - | |
| 459 | - $line_height = $attributes['style']['typography']['lineHeight'] ?? ''; | |
| 460 | - | |
| 461 | - if ( ! $line_height ) { | |
| 462 | - return self::EMPTY_STYLE; | |
| 463 | - } | |
| 464 | - | |
| 465 | - return array( | |
| 466 | - 'class' => null, | |
| 467 | - 'style' => sprintf( 'line-height: %s;', $line_height ), | |
| 468 | - ); | |
| 469 | - } | |
| 470 | - | |
| 471 | - /** | |
| 472 | - * Get a value from an array based on a path e.g style.elements.link | |
| 473 | - * | |
| 474 | - * @param array $array Target array. | |
| 475 | - * @param string $path Path joined by delimiter. | |
| 476 | - * @param string $delimiter Chosen delimiter defaults to ".". | |
| 477 | - * @return mixed | |
| 478 | - */ | |
| 479 | - protected static function array_get_value_by_path( array &$array, $path, $delimiter = '.' ) { | |
| 480 | - $array_path = explode( $delimiter, $path ); | |
| 481 | - $ref = &$array; | |
| 482 | - | |
| 483 | - foreach ( $array_path as $key ) { | |
| 484 | - if ( is_array( $ref ) && array_key_exists( $key, $ref ) ) { | |
| 485 | - $ref = &$ref[ $key ]; | |
| 486 | - } else { | |
| 487 | - return null; | |
| 488 | - } | |
| 489 | - } | |
| 490 | - return $ref; | |
| 491 | - } | |
| 492 | - | |
| 493 | - /** | |
| 494 | - * Get class and style for link-color from attributes. | |
| 495 | - * | |
| 496 | - * @param array $attributes Block attributes. | |
| 497 | - * @return array | |
| 498 | - */ | |
| 499 | - public static function get_link_color_class_and_style( $attributes ) { | |
| 500 | - $link_color = self::array_get_value_by_path( $attributes, 'style.elements.link.color.text' ); | |
| 501 | - | |
| 502 | - if ( empty( $link_color ) ) { | |
| 503 | - return self::EMPTY_STYLE; | |
| 504 | - } | |
| 505 | - | |
| 506 | - // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. | |
| 507 | - // If the link color is selected from the core color picker, the value of $link_color is an hex value. | |
| 508 | - // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. | |
| 509 | - if ( strstr( $link_color, '|' ) ) { | |
| 510 | - $link_color_parts = explode( '|', $link_color ); | |
| 511 | - $link_color = self::get_preset_value( end( $link_color_parts ) ); | |
| 512 | - } | |
| 513 | - | |
| 514 | - return array( | |
| 515 | - 'class' => 'has-link-color', | |
| 516 | - 'style' => sprintf( 'color: %s;', $link_color ), | |
| 517 | - 'value' => $link_color, | |
| 518 | - ); | |
| 519 | - } | |
| 520 | - | |
| 521 | - /** | |
| 522 | - * Get class and style for link-hover-color from attributes. | |
| 523 | - * | |
| 524 | - * @param array $attributes Block attributes. | |
| 525 | - * @return array | |
| 526 | - */ | |
| 527 | - public static function get_link_hover_color_class_and_style( $attributes ) { | |
| 528 | - $link_color = self::array_get_value_by_path( $attributes, 'style.elements.link.:hover.color.text' ); | |
| 529 | - | |
| 530 | - if ( empty( $link_color ) ) { | |
| 531 | - return self::EMPTY_STYLE; | |
| 532 | - } | |
| 533 | - | |
| 534 | - // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. | |
| 535 | - // If the link color is selected from the core color picker, the value of $link_color is an hex value. | |
| 536 | - // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. | |
| 537 | - if ( strstr( $link_color, '|' ) ) { | |
| 538 | - $link_color_parts = explode( '|', $link_color ); | |
| 539 | - $link_color = self::get_preset_value( end( $link_color_parts ) ); | |
| 540 | - } | |
| 541 | - | |
| 542 | - return array( | |
| 543 | - 'class' => 'has-link-color', | |
| 544 | - 'style' => sprintf( 'color: %s;', $link_color ), | |
| 545 | - 'value' => $link_color, | |
| 546 | - ); | |
| 547 | - } | |
| 548 | - | |
| 549 | - /** | |
| 550 | - * Get class and style for margin from attributes. | |
| 551 | - * | |
| 552 | - * @param array $attributes Block attributes. | |
| 553 | - * @return array | |
| 554 | - */ | |
| 555 | - public static function get_margin_class_and_style( $attributes ) { | |
| 556 | - $margin = $attributes['style']['spacing']['margin'] ?? null; | |
| 557 | - | |
| 558 | - if ( ! $margin ) { | |
| 559 | - return self::EMPTY_STYLE; | |
| 560 | - } | |
| 561 | - | |
| 562 | - $spacing_values_css = ''; | |
| 563 | - | |
| 564 | - foreach ( $margin as $margin_side => $margin_value ) { | |
| 565 | - $spacing_values_css .= 'margin-' . $margin_side . ':' . self::get_spacing_value( $margin_value ) . ';'; | |
| 566 | - } | |
| 567 | - | |
| 568 | - return array( | |
| 569 | - 'class' => null, | |
| 570 | - 'style' => $spacing_values_css, | |
| 571 | - ); | |
| 572 | - } | |
| 573 | - | |
| 574 | - /** | |
| 575 | - * Get class and style for padding from attributes. | |
| 576 | - * | |
| 577 | - * @param array $attributes Block attributes. | |
| 578 | - * | |
| 579 | - * @return array | |
| 580 | - */ | |
| 581 | - public static function get_padding_class_and_style( $attributes ) { | |
| 582 | - $padding = $attributes['style']['spacing']['padding'] ?? null; | |
| 583 | - | |
| 584 | - if ( ! $padding ) { | |
| 585 | - return self::EMPTY_STYLE; | |
| 586 | - } | |
| 587 | - | |
| 588 | - $spacing_values_css = ''; | |
| 589 | - | |
| 590 | - foreach ( $padding as $padding_side => $padding_value ) { | |
| 591 | - $spacing_values_css .= 'padding-' . $padding_side . ':' . self::get_spacing_value( $padding_value ) . ';'; | |
| 592 | - } | |
| 593 | - | |
| 594 | - return array( | |
| 595 | - 'class' => null, | |
| 596 | - 'style' => $spacing_values_css, | |
| 597 | - ); | |
| 598 | - } | |
| 599 | - | |
| 600 | - /** | |
| 601 | - * Get space-separated style rules from block attributes. | |
| 602 | - * | |
| 603 | - * @param array $attributes Block attributes. | |
| 604 | - * @param array $properties Properties to get styles from. | |
| 605 | - * | |
| 606 | - * @return string Space-separated style rules. | |
| 607 | - */ | |
| 608 | - public static function get_styles_by_attributes( $attributes, $properties = array() ) { | |
| 609 | - $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); | |
| 610 | - | |
| 611 | - return $classes_and_styles['styles']; | |
| 612 | - } | |
| 613 | - | |
| 614 | - /** | |
| 615 | - * Get class and style for text align from attributes. | |
| 616 | - * | |
| 617 | - * @param array $attributes Block attributes. | |
| 618 | - * @return array | |
| 619 | - */ | |
| 620 | - public static function get_text_align_class_and_style( $attributes ) { | |
| 621 | - if ( isset( $attributes['textAlign'] ) ) { | |
| 622 | - return array( | |
| 623 | - 'class' => 'has-text-align-' . $attributes['textAlign'], | |
| 624 | - 'style' => null, | |
| 625 | - ); | |
| 626 | - } | |
| 627 | - | |
| 628 | - return self::EMPTY_STYLE; | |
| 629 | - } | |
| 630 | - | |
| 631 | - /** | |
| 632 | - * Get class and style for text-color from attributes. | |
| 633 | - * | |
| 634 | - * @param array $attributes Block attributes. | |
| 635 | - * @return array | |
| 636 | - */ | |
| 637 | - public static function get_text_color_class_and_style( $attributes ) { | |
| 638 | - | |
| 639 | - $text_color = $attributes['textColor'] ?? ''; | |
| 640 | - | |
| 641 | - $custom_text_color = $attributes['style']['color']['text'] ?? ''; | |
| 642 | - | |
| 643 | - if ( ! $text_color && ! $custom_text_color ) { | |
| 644 | - return self::EMPTY_STYLE; | |
| 645 | - } | |
| 646 | - | |
| 647 | - if ( $text_color ) { | |
| 648 | - return array( | |
| 649 | - 'class' => sprintf( 'has-text-color' ), | |
| 650 | - 'value' => self::get_preset_value( $text_color ), | |
| 651 | - 'style' => sprintf( 'color: %s;', self::get_preset_value( $text_color ) ), | |
| 652 | - ); | |
| 653 | - } elseif ( $custom_text_color ) { | |
| 654 | - return array( | |
| 655 | - 'class' => null, | |
| 656 | - 'style' => sprintf( 'color: %s;', $custom_text_color ), | |
| 657 | - 'value' => $custom_text_color, | |
| 658 | - ); | |
| 659 | - } | |
| 660 | - | |
| 661 | - return self::EMPTY_STYLE; | |
| 662 | - } | |
| 663 | - | |
| 664 | - /** | |
| 665 | - * Get class and style for text-decoration from attributes. | |
| 666 | - * | |
| 667 | - * @param array $attributes Block attributes. | |
| 668 | - * | |
| 669 | - * @return array | |
| 670 | - */ | |
| 671 | - public static function get_text_decoration_class_and_style( $attributes ) { | |
| 672 | - | |
| 673 | - $custom_text_decoration = $attributes['style']['typography']['textDecoration'] ?? ''; | |
| 674 | - | |
| 675 | - if ( '' !== $custom_text_decoration ) { | |
| 676 | - return array( | |
| 677 | - 'class' => null, | |
| 678 | - 'style' => sprintf( 'text-decoration: %s;', $custom_text_decoration ), | |
| 679 | - ); | |
| 680 | - } | |
| 681 | - | |
| 682 | - return self::EMPTY_STYLE; | |
| 683 | - } | |
| 684 | - | |
| 685 | - /** | |
| 686 | - * Get class and style for text-transform from attributes. | |
| 687 | - * | |
| 688 | - * @param array $attributes Block attributes. | |
| 689 | - * @return array | |
| 690 | - */ | |
| 691 | - public static function get_text_transform_class_and_style( $attributes ) { | |
| 692 | - | |
| 693 | - $custom_text_transform = $attributes['style']['typography']['textTransform'] ?? ''; | |
| 694 | - | |
| 695 | - if ( '' !== $custom_text_transform ) { | |
| 696 | - return array( | |
| 697 | - 'class' => null, | |
| 698 | - 'style' => sprintf( 'text-transform: %s;', $custom_text_transform ), | |
| 699 | - ); | |
| 700 | - } | |
| 701 | - return self::EMPTY_STYLE; | |
| 702 | - } | |
| 703 | - | |
| 704 | - /** | |
| 705 | - * Get classes and styles from attributes. | |
| 706 | - * | |
| 707 | - * Excludes link_color and link_hover_color since those should not apply to the container. | |
| 708 | - * | |
| 709 | - * @param array $attributes Block attributes. | |
| 710 | - * @param array $properties Properties to get classes/styles from. | |
| 711 | - * @param array $exclude Properties to exclude. | |
| 712 | - * @return array | |
| 713 | - */ | |
| 714 | - public static function get_classes_and_styles_by_attributes( $attributes, $properties = array(), $exclude = array() ) { | |
| 715 | - $classes_and_styles = array( | |
| 716 | - 'align' => self::get_align_class_and_style( $attributes ), | |
| 717 | - 'background_color' => self::get_background_color_class_and_style( $attributes ), | |
| 718 | - 'border_color' => self::get_border_color_class_and_style( $attributes ), | |
| 719 | - 'border_radius' => self::get_border_radius_class_and_style( $attributes ), | |
| 720 | - 'border_width' => self::get_border_width_class_and_style( $attributes ), | |
| 721 | - 'border_style' => self::get_border_style_class_and_style( $attributes ), | |
| 722 | - 'font_family' => self::get_font_family_class_and_style( $attributes ), | |
| 723 | - 'font_size' => self::get_font_size_class_and_style( $attributes ), | |
| 724 | - 'font_style' => self::get_font_style_class_and_style( $attributes ), | |
| 725 | - 'font_weight' => self::get_font_weight_class_and_style( $attributes ), | |
| 726 | - 'letter_spacing' => self::get_letter_spacing_class_and_style( $attributes ), | |
| 727 | - 'line_height' => self::get_line_height_class_and_style( $attributes ), | |
| 728 | - 'margin' => self::get_margin_class_and_style( $attributes ), | |
| 729 | - 'padding' => self::get_padding_class_and_style( $attributes ), | |
| 730 | - 'text_align' => self::get_text_align_class_and_style( $attributes ), | |
| 731 | - 'text_color' => self::get_text_color_class_and_style( $attributes ), | |
| 732 | - 'text_decoration' => self::get_text_decoration_class_and_style( $attributes ), | |
| 733 | - 'text_transform' => self::get_text_transform_class_and_style( $attributes ), | |
| 734 | - ); | |
| 735 | - | |
| 736 | - if ( ! empty( $properties ) ) { | |
| 737 | - foreach ( $classes_and_styles as $key => $value ) { | |
| 738 | - if ( ! in_array( $key, $properties, true ) ) { | |
| 739 | - unset( $classes_and_styles[ $key ] ); | |
| 740 | - } | |
| 741 | - } | |
| 742 | - } | |
| 743 | - | |
| 744 | - if ( ! empty( $exclude ) ) { | |
| 745 | - foreach ( $classes_and_styles as $key => $value ) { | |
| 746 | - if ( in_array( $key, $exclude, true ) ) { | |
| 747 | - unset( $classes_and_styles[ $key ] ); | |
| 748 | - } | |
| 749 | - } | |
| 750 | - } | |
| 751 | - | |
| 752 | - $classes_and_styles = array_filter( $classes_and_styles ); | |
| 753 | - | |
| 754 | - $classes = array_map( | |
| 755 | - function ( $item ) { | |
| 756 | - return $item['class']; | |
| 757 | - }, | |
| 758 | - $classes_and_styles | |
| 759 | - ); | |
| 760 | - | |
| 761 | - $styles = array_map( | |
| 762 | - function ( $item ) { | |
| 763 | - return $item['style']; | |
| 764 | - }, | |
| 765 | - // Exclude link color styles from parent to avoid conflict with text color. | |
| 766 | - array_diff_key( | |
| 767 | - $classes_and_styles, | |
| 768 | - array_flip( array( 'link_color' ) ) | |
| 769 | - ) | |
| 770 | - ); | |
| 771 | - | |
| 772 | - $classes = array_filter( $classes ); | |
| 773 | - $styles = array_filter( $styles ); | |
| 774 | - | |
| 775 | - return array( | |
| 776 | - 'classes' => implode( ' ', $classes ), | |
| 777 | - 'styles' => implode( ' ', $styles ), | |
| 778 | - ); | |
| 779 | - } | |
| 780 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace LearnPress\Gutenberg\Utils; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * StyleAttributes class used for getting class and style from attributes. | |
| 7 | + */ | |
| 8 | +class StyleAttributes { | |
| 9 | + | |
| 10 | + // Empty style array. | |
| 11 | + const EMPTY_STYLE = [ | |
| 12 | + 'class' => '', | |
| 13 | + 'style' => '', | |
| 14 | + 'value' => '', | |
| 15 | + ]; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * If color value is in preset format, convert it to a CSS var. Else return same value | |
| 19 | + * For example: | |
| 20 | + * "var:preset|color|pale-pink" -> "var(--wp--preset--color--pale-pink)" | |
| 21 | + * "#98b66e" -> "#98b66e" | |
| 22 | + * | |
| 23 | + * @param string $color_value value to be processed. | |
| 24 | + * | |
| 25 | + * @return (string) | |
| 26 | + */ | |
| 27 | + public static function get_color_value( $color_value ) { | |
| 28 | + if ( is_string( $color_value ) && str_contains( $color_value, 'var:preset|color|' ) ) { | |
| 29 | + $color_value = str_replace( 'var:preset|color|', '', $color_value ); | |
| 30 | + return sprintf( 'var(--wp--preset--color--%s)', $color_value ); | |
| 31 | + } | |
| 32 | + | |
| 33 | + return $color_value; | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Get CSS value for color preset. | |
| 38 | + * | |
| 39 | + * @param string $preset_name Preset name. | |
| 40 | + * | |
| 41 | + * @return string CSS value for color preset. | |
| 42 | + */ | |
| 43 | + public static function get_preset_value( $preset_name ) { | |
| 44 | + return "var(--wp--preset--color--$preset_name)"; | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Get CSS value for font size. | |
| 49 | + * | |
| 50 | + * @param string $preset_name Preset name. | |
| 51 | + * | |
| 52 | + * @return string CSS value for font size. | |
| 53 | + */ | |
| 54 | + public static function get_font_size_value( $preset_name ) { | |
| 55 | + return "var(--wp--preset--font-size--$preset_name)"; | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * If spacing value is in preset format, convert it to a CSS var. Else return same value | |
| 60 | + * For example: | |
| 61 | + * "var:preset|spacing|50" -> "var(--wp--preset--spacing--50)" | |
| 62 | + * "50px" -> "50px" | |
| 63 | + * | |
| 64 | + * @param string $spacing_value value to be processed. | |
| 65 | + * | |
| 66 | + * @return (string) | |
| 67 | + */ | |
| 68 | + public static function get_spacing_value( $spacing_value ) { | |
| 69 | + // Used following code as reference: https://github.com/WordPress/gutenberg/blob/cff6d70d6ff5a26e212958623dc3130569f95685/lib/block-supports/layout.php/#L219-L225. | |
| 70 | + if ( is_string( $spacing_value ) && str_contains( $spacing_value, 'var:preset|spacing|' ) ) { | |
| 71 | + $spacing_value = str_replace( 'var:preset|spacing|', '', $spacing_value ); | |
| 72 | + return sprintf( 'var(--wp--preset--spacing--%s)', $spacing_value ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + return $spacing_value; | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * Get class and style for align from attributes. | |
| 80 | + * | |
| 81 | + * @param array $attributes Block attributes. | |
| 82 | + * @return array | |
| 83 | + */ | |
| 84 | + public static function get_align_class_and_style( $attributes ) { | |
| 85 | + $align_attribute = $attributes['align'] ?? null; | |
| 86 | + | |
| 87 | + if ( 'wide' === $align_attribute ) { | |
| 88 | + return array( | |
| 89 | + 'class' => 'alignwide', | |
| 90 | + 'style' => null, | |
| 91 | + ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + if ( 'full' === $align_attribute ) { | |
| 95 | + return array( | |
| 96 | + 'class' => 'alignfull', | |
| 97 | + 'style' => null, | |
| 98 | + ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + if ( 'left' === $align_attribute ) { | |
| 102 | + return array( | |
| 103 | + 'class' => 'alignleft', | |
| 104 | + 'style' => null, | |
| 105 | + ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + if ( 'right' === $align_attribute ) { | |
| 109 | + return array( | |
| 110 | + 'class' => 'alignright', | |
| 111 | + 'style' => null, | |
| 112 | + ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + if ( 'center' === $align_attribute ) { | |
| 116 | + return array( | |
| 117 | + 'class' => 'aligncenter', | |
| 118 | + 'style' => null, | |
| 119 | + ); | |
| 120 | + } | |
| 121 | + | |
| 122 | + return self::EMPTY_STYLE; | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Get class and style for background-color from attributes. | |
| 127 | + * | |
| 128 | + * @param array $attributes Block attributes. | |
| 129 | + * @return array | |
| 130 | + */ | |
| 131 | + public static function get_background_color_class_and_style( $attributes ) { | |
| 132 | + $gradient = $attributes['gradient'] ?? null; | |
| 133 | + $background_color = $attributes['backgroundColor'] ?? ''; | |
| 134 | + $custom_background_color = $attributes['style']['color']['background'] ?? ''; | |
| 135 | + $classes = [ $gradient ]; | |
| 136 | + $styles = []; | |
| 137 | + $value = null; | |
| 138 | + | |
| 139 | + if ( $background_color || $custom_background_color || $gradient ) { | |
| 140 | + $classes[] = 'has-background'; | |
| 141 | + } | |
| 142 | + | |
| 143 | + if ( $background_color ) { | |
| 144 | + $value = self::get_preset_value( $background_color ); | |
| 145 | + $styles[] = sprintf( 'background-color: %s;', $value ); | |
| 146 | + } | |
| 147 | + | |
| 148 | + if ( $custom_background_color ) { | |
| 149 | + $styles[] = sprintf( 'background-color: %s;', $custom_background_color ); | |
| 150 | + $value = $custom_background_color; | |
| 151 | + } | |
| 152 | + | |
| 153 | + if ( $gradient ) { | |
| 154 | + $classes[] = sprintf( 'has-%s-gradient-background', $gradient ); | |
| 155 | + } | |
| 156 | + | |
| 157 | + return array( | |
| 158 | + 'class' => self::join_styles( $classes ), | |
| 159 | + 'style' => self::join_styles( $styles ), | |
| 160 | + 'value' => $value, | |
| 161 | + ); | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Join classes and styles while removing duplicates and null values. | |
| 166 | + * | |
| 167 | + * @param array $rules Array of classes or styles. | |
| 168 | + * @return array | |
| 169 | + */ | |
| 170 | + protected static function join_styles( $rules ) { | |
| 171 | + return implode( ' ', array_unique( array_filter( $rules ) ) ); | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Get class and style for border-color from attributes. | |
| 176 | + * | |
| 177 | + * Data passed to this function is not always consistent. It can be: | |
| 178 | + * Linked - preset color: $attributes['borderColor'] => 'luminous-vivid-orange'. | |
| 179 | + * Linked - custom color: $attributes['style']['border']['color'] => '#681228'. | |
| 180 | + * Unlinked - preset color: $attributes['style']['border']['top']['color'] => 'var:preset|color|luminous-vivid-orange' | |
| 181 | + * Unlinked - custom color: $attributes['style']['border']['top']['color'] => '#681228'. | |
| 182 | + * | |
| 183 | + * @param array $attributes Block attributes. | |
| 184 | + * @return array | |
| 185 | + */ | |
| 186 | + public static function get_border_color_class_and_style( $attributes ) { | |
| 187 | + $border_color_linked_preset = $attributes['borderColor'] ?? ''; | |
| 188 | + $border_color_linked_custom = $attributes['style']['border']['color'] ?? ''; | |
| 189 | + $custom_border = $attributes['style']['border'] ?? ''; | |
| 190 | + | |
| 191 | + $class = ''; | |
| 192 | + $style = ''; | |
| 193 | + $value = ''; | |
| 194 | + | |
| 195 | + if ( $border_color_linked_preset ) { | |
| 196 | + // Linked preset color. | |
| 197 | + $value = self::get_preset_value( $border_color_linked_preset ); | |
| 198 | + $style .= 'border-color:' . $value . ';'; | |
| 199 | + } elseif ( $border_color_linked_custom ) { | |
| 200 | + // Linked custom color. | |
| 201 | + $style .= 'border-color:' . $border_color_linked_custom . ';'; | |
| 202 | + $value = $border_color_linked_custom; | |
| 203 | + } elseif ( is_array( $custom_border ) ) { | |
| 204 | + // $style .= 'border-style: solid;'; | |
| 205 | + // Unlinked. | |
| 206 | + foreach ( $custom_border as $border_color_key => $border_color_value ) { | |
| 207 | + if ( is_array( $border_color_value ) && array_key_exists( 'color', ( $border_color_value ) ) ) { | |
| 208 | + $style .= 'border-' . $border_color_key . '-color:' . self::get_color_value( $border_color_value['color'] ) . ';'; | |
| 209 | + } | |
| 210 | + } | |
| 211 | + } | |
| 212 | + | |
| 213 | + if ( ! $class && ! $style ) { | |
| 214 | + return self::EMPTY_STYLE; | |
| 215 | + } | |
| 216 | + | |
| 217 | + return array( | |
| 218 | + 'class' => $class, | |
| 219 | + 'style' => $style, | |
| 220 | + 'value' => $value, | |
| 221 | + ); | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Get class and style for border-radius from attributes. | |
| 226 | + * | |
| 227 | + * @param array $attributes Block attributes. | |
| 228 | + * @return array | |
| 229 | + */ | |
| 230 | + public static function get_border_radius_class_and_style( $attributes ) { | |
| 231 | + $custom_border_radius = $attributes['style']['border']['radius'] ?? ''; | |
| 232 | + | |
| 233 | + if ( '' === $custom_border_radius ) { | |
| 234 | + return self::EMPTY_STYLE; | |
| 235 | + } | |
| 236 | + | |
| 237 | + $style = ''; | |
| 238 | + | |
| 239 | + if ( is_string( $custom_border_radius ) ) { | |
| 240 | + // Linked sides. | |
| 241 | + $style = 'border-radius:' . $custom_border_radius . ';'; | |
| 242 | + } else { | |
| 243 | + // Unlinked sides. | |
| 244 | + $border_radius = array(); | |
| 245 | + | |
| 246 | + $border_radius['border-top-left-radius'] = $custom_border_radius['topLeft'] ?? ''; | |
| 247 | + $border_radius['border-top-right-radius'] = $custom_border_radius['topRight'] ?? ''; | |
| 248 | + $border_radius['border-bottom-right-radius'] = $custom_border_radius['bottomRight'] ?? ''; | |
| 249 | + $border_radius['border-bottom-left-radius'] = $custom_border_radius['bottomLeft'] ?? ''; | |
| 250 | + | |
| 251 | + foreach ( $border_radius as $border_radius_side => $border_radius_value ) { | |
| 252 | + if ( '' !== $border_radius_value ) { | |
| 253 | + $style .= $border_radius_side . ':' . $border_radius_value . ';'; | |
| 254 | + } | |
| 255 | + } | |
| 256 | + } | |
| 257 | + | |
| 258 | + return array( | |
| 259 | + 'class' => null, | |
| 260 | + 'style' => $style, | |
| 261 | + ); | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Get class and style for border width from attributes. | |
| 266 | + * | |
| 267 | + * @param array $attributes Block attributes. | |
| 268 | + * @return array | |
| 269 | + */ | |
| 270 | + public static function get_border_width_class_and_style( $attributes ) { | |
| 271 | + $custom_border = $attributes['style']['border'] ?? ''; | |
| 272 | + | |
| 273 | + if ( '' === $custom_border ) { | |
| 274 | + return self::EMPTY_STYLE; | |
| 275 | + } | |
| 276 | + | |
| 277 | + $style = ''; | |
| 278 | + | |
| 279 | + if ( array_key_exists( 'width', ( $custom_border ) ) && ! empty( $custom_border['width'] ) ) { | |
| 280 | + // Linked sides. | |
| 281 | + $style .= 'border-style: solid;'; | |
| 282 | + $style .= 'border-width:' . $custom_border['width'] . ';'; | |
| 283 | + } else { | |
| 284 | + // Unlinked sides. | |
| 285 | + foreach ( $custom_border as $border_width_side => $border_width_value ) { | |
| 286 | + if ( isset( $border_width_value['width'] ) ) { | |
| 287 | + $style .= 'border-' . $border_width_side . '-style: solid;'; | |
| 288 | + $style .= 'border-' . $border_width_side . '-width:' . $border_width_value['width'] . ';'; | |
| 289 | + } | |
| 290 | + } | |
| 291 | + } | |
| 292 | + | |
| 293 | + return array( | |
| 294 | + 'class' => null, | |
| 295 | + 'style' => $style, | |
| 296 | + ); | |
| 297 | + } | |
| 298 | + | |
| 299 | + /** | |
| 300 | + * Get class and style for border width from attributes. | |
| 301 | + * | |
| 302 | + * @param array $attributes Block attributes. | |
| 303 | + * @return array | |
| 304 | + */ | |
| 305 | + public static function get_border_style_class_and_style( $attributes ) { | |
| 306 | + $custom_border = $attributes['style']['border'] ?? ''; | |
| 307 | + | |
| 308 | + if ( '' === $custom_border ) { | |
| 309 | + return self::EMPTY_STYLE; | |
| 310 | + } | |
| 311 | + | |
| 312 | + $style = ''; | |
| 313 | + | |
| 314 | + if ( array_key_exists( 'style', ( $custom_border ) ) && ! empty( $custom_border['style'] ) ) { | |
| 315 | + $style = 'border-style:' . $custom_border['style'] . ';'; | |
| 316 | + } else { | |
| 317 | + foreach ( $custom_border as $side => $value ) { | |
| 318 | + if ( isset( $value['style'] ) ) { | |
| 319 | + $style .= 'border-' . $side . '-style:' . $value['style'] . ';'; | |
| 320 | + } | |
| 321 | + } | |
| 322 | + } | |
| 323 | + | |
| 324 | + return array( | |
| 325 | + 'class' => null, | |
| 326 | + 'style' => $style, | |
| 327 | + ); | |
| 328 | + } | |
| 329 | + | |
| 330 | + /** | |
| 331 | + * Get space-separated classes from block attributes. | |
| 332 | + * | |
| 333 | + * @param array $attributes Block attributes. | |
| 334 | + * @param array $properties Properties to get classes from. | |
| 335 | + * | |
| 336 | + * @return string Space-separated classes. | |
| 337 | + */ | |
| 338 | + public static function get_classes_by_attributes( $attributes, $properties = array() ) { | |
| 339 | + $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); | |
| 340 | + | |
| 341 | + return $classes_and_styles['classes']; | |
| 342 | + } | |
| 343 | + | |
| 344 | + /** | |
| 345 | + * Get class and style for font-family from attributes. | |
| 346 | + * | |
| 347 | + * @param array $attributes Block attributes. | |
| 348 | + * @return array | |
| 349 | + */ | |
| 350 | + public static function get_font_family_class_and_style( $attributes ) { | |
| 351 | + | |
| 352 | + $font_family = $attributes['fontFamily'] ?? ''; | |
| 353 | + | |
| 354 | + if ( $font_family ) { | |
| 355 | + return array( | |
| 356 | + 'class' => sprintf( 'has-%s-font-family', $font_family ), | |
| 357 | + 'style' => null, | |
| 358 | + ); | |
| 359 | + } | |
| 360 | + return self::EMPTY_STYLE; | |
| 361 | + } | |
| 362 | + | |
| 363 | + /** | |
| 364 | + * Get class and style for font-size from attributes. | |
| 365 | + * | |
| 366 | + * @param array $attributes Block attributes. | |
| 367 | + * @return array | |
| 368 | + */ | |
| 369 | + public static function get_font_size_class_and_style( $attributes ) { | |
| 370 | + | |
| 371 | + $font_size = $attributes['fontSize'] ?? ''; | |
| 372 | + | |
| 373 | + $custom_font_size = $attributes['style']['typography']['fontSize'] ?? ''; | |
| 374 | + | |
| 375 | + if ( ! $font_size && '' === $custom_font_size ) { | |
| 376 | + return self::EMPTY_STYLE; | |
| 377 | + } | |
| 378 | + | |
| 379 | + if ( $font_size ) { | |
| 380 | + return array( | |
| 381 | + 'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ), | |
| 382 | + 'style' => sprintf( 'font-size: %s;', self::get_font_size_value( $font_size ) ), | |
| 383 | + ); | |
| 384 | + } elseif ( '' !== $custom_font_size ) { | |
| 385 | + return array( | |
| 386 | + 'class' => null, | |
| 387 | + 'style' => sprintf( 'font-size: %s;', $custom_font_size ), | |
| 388 | + ); | |
| 389 | + } | |
| 390 | + | |
| 391 | + return self::EMPTY_STYLE; | |
| 392 | + } | |
| 393 | + | |
| 394 | + /** | |
| 395 | + * Get class and style for font-style from attributes. | |
| 396 | + * | |
| 397 | + * @param array $attributes Block attributes. | |
| 398 | + * @return array | |
| 399 | + */ | |
| 400 | + public static function get_font_style_class_and_style( $attributes ) { | |
| 401 | + | |
| 402 | + $custom_font_style = $attributes['style']['typography']['fontStyle'] ?? ''; | |
| 403 | + | |
| 404 | + if ( '' !== $custom_font_style ) { | |
| 405 | + return array( | |
| 406 | + 'class' => null, | |
| 407 | + 'style' => sprintf( 'font-style: %s;', $custom_font_style ), | |
| 408 | + ); | |
| 409 | + } | |
| 410 | + return self::EMPTY_STYLE; | |
| 411 | + } | |
| 412 | + | |
| 413 | + /** | |
| 414 | + * Get class and style for font-weight from attributes. | |
| 415 | + * | |
| 416 | + * @param array $attributes Block attributes. | |
| 417 | + * @return array | |
| 418 | + */ | |
| 419 | + public static function get_font_weight_class_and_style( $attributes ) { | |
| 420 | + | |
| 421 | + $custom_font_weight = $attributes['style']['typography']['fontWeight'] ?? ''; | |
| 422 | + | |
| 423 | + if ( '' !== $custom_font_weight ) { | |
| 424 | + return array( | |
| 425 | + 'class' => null, | |
| 426 | + 'style' => sprintf( 'font-weight: %s;', $custom_font_weight ), | |
| 427 | + ); | |
| 428 | + } | |
| 429 | + return self::EMPTY_STYLE; | |
| 430 | + } | |
| 431 | + | |
| 432 | + /** | |
| 433 | + * Get class and style for letter-spacing from attributes. | |
| 434 | + * | |
| 435 | + * @param array $attributes Block attributes. | |
| 436 | + * @return array | |
| 437 | + */ | |
| 438 | + public static function get_letter_spacing_class_and_style( $attributes ) { | |
| 439 | + | |
| 440 | + $custom_letter_spacing = $attributes['style']['typography']['letterSpacing'] ?? ''; | |
| 441 | + | |
| 442 | + if ( '' !== $custom_letter_spacing ) { | |
| 443 | + return array( | |
| 444 | + 'class' => null, | |
| 445 | + 'style' => sprintf( 'letter-spacing: %s;', $custom_letter_spacing ), | |
| 446 | + ); | |
| 447 | + } | |
| 448 | + return self::EMPTY_STYLE; | |
| 449 | + } | |
| 450 | + | |
| 451 | + /** | |
| 452 | + * Get class and style for line height from attributes. | |
| 453 | + * | |
| 454 | + * @param array $attributes Block attributes. | |
| 455 | + * @return array | |
| 456 | + */ | |
| 457 | + public static function get_line_height_class_and_style( $attributes ) { | |
| 458 | + | |
| 459 | + $line_height = $attributes['style']['typography']['lineHeight'] ?? ''; | |
| 460 | + | |
| 461 | + if ( ! $line_height ) { | |
| 462 | + return self::EMPTY_STYLE; | |
| 463 | + } | |
| 464 | + | |
| 465 | + return array( | |
| 466 | + 'class' => null, | |
| 467 | + 'style' => sprintf( 'line-height: %s;', $line_height ), | |
| 468 | + ); | |
| 469 | + } | |
| 470 | + | |
| 471 | + /** | |
| 472 | + * Get a value from an array based on a path e.g style.elements.link | |
| 473 | + * | |
| 474 | + * @param array $array Target array. | |
| 475 | + * @param string $path Path joined by delimiter. | |
| 476 | + * @param string $delimiter Chosen delimiter defaults to ".". | |
| 477 | + * @return mixed | |
| 478 | + */ | |
| 479 | + protected static function array_get_value_by_path( array &$array, $path, $delimiter = '.' ) { | |
| 480 | + $array_path = explode( $delimiter, $path ); | |
| 481 | + $ref = &$array; | |
| 482 | + | |
| 483 | + foreach ( $array_path as $key ) { | |
| 484 | + if ( is_array( $ref ) && array_key_exists( $key, $ref ) ) { | |
| 485 | + $ref = &$ref[ $key ]; | |
| 486 | + } else { | |
| 487 | + return null; | |
| 488 | + } | |
| 489 | + } | |
| 490 | + return $ref; | |
| 491 | + } | |
| 492 | + | |
| 493 | + /** | |
| 494 | + * Get class and style for link-color from attributes. | |
| 495 | + * | |
| 496 | + * @param array $attributes Block attributes. | |
| 497 | + * @return array | |
| 498 | + */ | |
| 499 | + public static function get_link_color_class_and_style( $attributes ) { | |
| 500 | + $link_color = self::array_get_value_by_path( $attributes, 'style.elements.link.color.text' ); | |
| 501 | + | |
| 502 | + if ( empty( $link_color ) ) { | |
| 503 | + return self::EMPTY_STYLE; | |
| 504 | + } | |
| 505 | + | |
| 506 | + // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. | |
| 507 | + // If the link color is selected from the core color picker, the value of $link_color is an hex value. | |
| 508 | + // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. | |
| 509 | + if ( strstr( $link_color, '|' ) ) { | |
| 510 | + $link_color_parts = explode( '|', $link_color ); | |
| 511 | + $link_color = self::get_preset_value( end( $link_color_parts ) ); | |
| 512 | + } | |
| 513 | + | |
| 514 | + return array( | |
| 515 | + 'class' => 'has-link-color', | |
| 516 | + 'style' => sprintf( 'color: %s;', $link_color ), | |
| 517 | + 'value' => $link_color, | |
| 518 | + ); | |
| 519 | + } | |
| 520 | + | |
| 521 | + /** | |
| 522 | + * Get class and style for link-hover-color from attributes. | |
| 523 | + * | |
| 524 | + * @param array $attributes Block attributes. | |
| 525 | + * @return array | |
| 526 | + */ | |
| 527 | + public static function get_link_hover_color_class_and_style( $attributes ) { | |
| 528 | + $link_color = self::array_get_value_by_path( $attributes, 'style.elements.link.:hover.color.text' ); | |
| 529 | + | |
| 530 | + if ( empty( $link_color ) ) { | |
| 531 | + return self::EMPTY_STYLE; | |
| 532 | + } | |
| 533 | + | |
| 534 | + // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. | |
| 535 | + // If the link color is selected from the core color picker, the value of $link_color is an hex value. | |
| 536 | + // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. | |
| 537 | + if ( strstr( $link_color, '|' ) ) { | |
| 538 | + $link_color_parts = explode( '|', $link_color ); | |
| 539 | + $link_color = self::get_preset_value( end( $link_color_parts ) ); | |
| 540 | + } | |
| 541 | + | |
| 542 | + return array( | |
| 543 | + 'class' => 'has-link-color', | |
| 544 | + 'style' => sprintf( 'color: %s;', $link_color ), | |
| 545 | + 'value' => $link_color, | |
| 546 | + ); | |
| 547 | + } | |
| 548 | + | |
| 549 | + /** | |
| 550 | + * Get class and style for margin from attributes. | |
| 551 | + * | |
| 552 | + * @param array $attributes Block attributes. | |
| 553 | + * @return array | |
| 554 | + */ | |
| 555 | + public static function get_margin_class_and_style( $attributes ) { | |
| 556 | + $margin = $attributes['style']['spacing']['margin'] ?? null; | |
| 557 | + | |
| 558 | + if ( ! $margin ) { | |
| 559 | + return self::EMPTY_STYLE; | |
| 560 | + } | |
| 561 | + | |
| 562 | + $spacing_values_css = ''; | |
| 563 | + | |
| 564 | + foreach ( $margin as $margin_side => $margin_value ) { | |
| 565 | + $spacing_values_css .= 'margin-' . $margin_side . ':' . self::get_spacing_value( $margin_value ) . ';'; | |
| 566 | + } | |
| 567 | + | |
| 568 | + return array( | |
| 569 | + 'class' => null, | |
| 570 | + 'style' => $spacing_values_css, | |
| 571 | + ); | |
| 572 | + } | |
| 573 | + | |
| 574 | + /** | |
| 575 | + * Get class and style for padding from attributes. | |
| 576 | + * | |
| 577 | + * @param array $attributes Block attributes. | |
| 578 | + * | |
| 579 | + * @return array | |
| 580 | + */ | |
| 581 | + public static function get_padding_class_and_style( $attributes ) { | |
| 582 | + $padding = $attributes['style']['spacing']['padding'] ?? null; | |
| 583 | + | |
| 584 | + if ( ! $padding ) { | |
| 585 | + return self::EMPTY_STYLE; | |
| 586 | + } | |
| 587 | + | |
| 588 | + $spacing_values_css = ''; | |
| 589 | + | |
| 590 | + foreach ( $padding as $padding_side => $padding_value ) { | |
| 591 | + $spacing_values_css .= 'padding-' . $padding_side . ':' . self::get_spacing_value( $padding_value ) . ';'; | |
| 592 | + } | |
| 593 | + | |
| 594 | + return array( | |
| 595 | + 'class' => null, | |
| 596 | + 'style' => $spacing_values_css, | |
| 597 | + ); | |
| 598 | + } | |
| 599 | + | |
| 600 | + /** | |
| 601 | + * Get space-separated style rules from block attributes. | |
| 602 | + * | |
| 603 | + * @param array $attributes Block attributes. | |
| 604 | + * @param array $properties Properties to get styles from. | |
| 605 | + * | |
| 606 | + * @return string Space-separated style rules. | |
| 607 | + */ | |
| 608 | + public static function get_styles_by_attributes( $attributes, $properties = array() ) { | |
| 609 | + $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); | |
| 610 | + | |
| 611 | + return $classes_and_styles['styles']; | |
| 612 | + } | |
| 613 | + | |
| 614 | + /** | |
| 615 | + * Get class and style for text align from attributes. | |
| 616 | + * | |
| 617 | + * @param array $attributes Block attributes. | |
| 618 | + * @return array | |
| 619 | + */ | |
| 620 | + public static function get_text_align_class_and_style( $attributes ) { | |
| 621 | + if ( isset( $attributes['textAlign'] ) ) { | |
| 622 | + return array( | |
| 623 | + 'class' => 'has-text-align-' . $attributes['textAlign'], | |
| 624 | + 'style' => null, | |
| 625 | + ); | |
| 626 | + } | |
| 627 | + | |
| 628 | + return self::EMPTY_STYLE; | |
| 629 | + } | |
| 630 | + | |
| 631 | + /** | |
| 632 | + * Get class and style for text-color from attributes. | |
| 633 | + * | |
| 634 | + * @param array $attributes Block attributes. | |
| 635 | + * @return array | |
| 636 | + */ | |
| 637 | + public static function get_text_color_class_and_style( $attributes ) { | |
| 638 | + | |
| 639 | + $text_color = $attributes['textColor'] ?? ''; | |
| 640 | + | |
| 641 | + $custom_text_color = $attributes['style']['color']['text'] ?? ''; | |
| 642 | + | |
| 643 | + if ( ! $text_color && ! $custom_text_color ) { | |
| 644 | + return self::EMPTY_STYLE; | |
| 645 | + } | |
| 646 | + | |
| 647 | + if ( $text_color ) { | |
| 648 | + return array( | |
| 649 | + 'class' => sprintf( 'has-text-color' ), | |
| 650 | + 'value' => self::get_preset_value( $text_color ), | |
| 651 | + 'style' => sprintf( 'color: %s;', self::get_preset_value( $text_color ) ), | |
| 652 | + ); | |
| 653 | + } elseif ( $custom_text_color ) { | |
| 654 | + return array( | |
| 655 | + 'class' => null, | |
| 656 | + 'style' => sprintf( 'color: %s;', $custom_text_color ), | |
| 657 | + 'value' => $custom_text_color, | |
| 658 | + ); | |
| 659 | + } | |
| 660 | + | |
| 661 | + return self::EMPTY_STYLE; | |
| 662 | + } | |
| 663 | + | |
| 664 | + /** | |
| 665 | + * Get class and style for text-decoration from attributes. | |
| 666 | + * | |
| 667 | + * @param array $attributes Block attributes. | |
| 668 | + * | |
| 669 | + * @return array | |
| 670 | + */ | |
| 671 | + public static function get_text_decoration_class_and_style( $attributes ) { | |
| 672 | + | |
| 673 | + $custom_text_decoration = $attributes['style']['typography']['textDecoration'] ?? ''; | |
| 674 | + | |
| 675 | + if ( '' !== $custom_text_decoration ) { | |
| 676 | + return array( | |
| 677 | + 'class' => null, | |
| 678 | + 'style' => sprintf( 'text-decoration: %s;', $custom_text_decoration ), | |
| 679 | + ); | |
| 680 | + } | |
| 681 | + | |
| 682 | + return self::EMPTY_STYLE; | |
| 683 | + } | |
| 684 | + | |
| 685 | + /** | |
| 686 | + * Get class and style for text-transform from attributes. | |
| 687 | + * | |
| 688 | + * @param array $attributes Block attributes. | |
| 689 | + * @return array | |
| 690 | + */ | |
| 691 | + public static function get_text_transform_class_and_style( $attributes ) { | |
| 692 | + | |
| 693 | + $custom_text_transform = $attributes['style']['typography']['textTransform'] ?? ''; | |
| 694 | + | |
| 695 | + if ( '' !== $custom_text_transform ) { | |
| 696 | + return array( | |
| 697 | + 'class' => null, | |
| 698 | + 'style' => sprintf( 'text-transform: %s;', $custom_text_transform ), | |
| 699 | + ); | |
| 700 | + } | |
| 701 | + return self::EMPTY_STYLE; | |
| 702 | + } | |
| 703 | + | |
| 704 | + /** | |
| 705 | + * Get classes and styles from attributes. | |
| 706 | + * | |
| 707 | + * Excludes link_color and link_hover_color since those should not apply to the container. | |
| 708 | + * | |
| 709 | + * @param array $attributes Block attributes. | |
| 710 | + * @param array $properties Properties to get classes/styles from. | |
| 711 | + * @param array $exclude Properties to exclude. | |
| 712 | + * @return array | |
| 713 | + */ | |
| 714 | + public static function get_classes_and_styles_by_attributes( $attributes, $properties = array(), $exclude = array() ) { | |
| 715 | + $classes_and_styles = array( | |
| 716 | + 'align' => self::get_align_class_and_style( $attributes ), | |
| 717 | + 'background_color' => self::get_background_color_class_and_style( $attributes ), | |
| 718 | + 'border_color' => self::get_border_color_class_and_style( $attributes ), | |
| 719 | + 'border_radius' => self::get_border_radius_class_and_style( $attributes ), | |
| 720 | + 'border_width' => self::get_border_width_class_and_style( $attributes ), | |
| 721 | + 'border_style' => self::get_border_style_class_and_style( $attributes ), | |
| 722 | + 'font_family' => self::get_font_family_class_and_style( $attributes ), | |
| 723 | + 'font_size' => self::get_font_size_class_and_style( $attributes ), | |
| 724 | + 'font_style' => self::get_font_style_class_and_style( $attributes ), | |
| 725 | + 'font_weight' => self::get_font_weight_class_and_style( $attributes ), | |
| 726 | + 'letter_spacing' => self::get_letter_spacing_class_and_style( $attributes ), | |
| 727 | + 'line_height' => self::get_line_height_class_and_style( $attributes ), | |
| 728 | + 'margin' => self::get_margin_class_and_style( $attributes ), | |
| 729 | + 'padding' => self::get_padding_class_and_style( $attributes ), | |
| 730 | + 'text_align' => self::get_text_align_class_and_style( $attributes ), | |
| 731 | + 'text_color' => self::get_text_color_class_and_style( $attributes ), | |
| 732 | + 'text_decoration' => self::get_text_decoration_class_and_style( $attributes ), | |
| 733 | + 'text_transform' => self::get_text_transform_class_and_style( $attributes ), | |
| 734 | + ); | |
| 735 | + | |
| 736 | + if ( ! empty( $properties ) ) { | |
| 737 | + foreach ( $classes_and_styles as $key => $value ) { | |
| 738 | + if ( ! in_array( $key, $properties, true ) ) { | |
| 739 | + unset( $classes_and_styles[ $key ] ); | |
| 740 | + } | |
| 741 | + } | |
| 742 | + } | |
| 743 | + | |
| 744 | + if ( ! empty( $exclude ) ) { | |
| 745 | + foreach ( $classes_and_styles as $key => $value ) { | |
| 746 | + if ( in_array( $key, $exclude, true ) ) { | |
| 747 | + unset( $classes_and_styles[ $key ] ); | |
| 748 | + } | |
| 749 | + } | |
| 750 | + } | |
| 751 | + | |
| 752 | + $classes_and_styles = array_filter( $classes_and_styles ); | |
| 753 | + | |
| 754 | + $classes = array_map( | |
| 755 | + function ( $item ) { | |
| 756 | + return $item['class']; | |
| 757 | + }, | |
| 758 | + $classes_and_styles | |
| 759 | + ); | |
| 760 | + | |
| 761 | + $styles = array_map( | |
| 762 | + function ( $item ) { | |
| 763 | + return $item['style']; | |
| 764 | + }, | |
| 765 | + // Exclude link color styles from parent to avoid conflict with text color. | |
| 766 | + array_diff_key( | |
| 767 | + $classes_and_styles, | |
| 768 | + array_flip( array( 'link_color' ) ) | |
| 769 | + ) | |
| 770 | + ); | |
| 771 | + | |
| 772 | + $classes = array_filter( $classes ); | |
| 773 | + $styles = array_filter( $styles ); | |
| 774 | + | |
| 775 | + return array( | |
| 776 | + 'classes' => implode( ' ', $classes ), | |
| 777 | + 'styles' => implode( ' ', $styles ), | |
| 778 | + ); | |
| 779 | + } | |
| 780 | +} | |