This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global style for product categories list block #4965
Add global style for product categories list block
- Loading branch information
Showing
3 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
namespace Automattic\WooCommerce\Blocks\Utils; | ||
|
||
/** | ||
* AttributesUtils class used for getting class and style from attributes. | ||
*/ | ||
class AttributesUtils { | ||
|
||
|
||
/** | ||
* Get class and style for font-size from attributes. | ||
* | ||
* @param array $attributes Attributes. | ||
* | ||
* @return string | ||
*/ | ||
public static function get_font_size_class_and_style( $attributes ) { | ||
|
||
$font_size = $attributes['fontSize']; | ||
$custom_font_size = $attributes['style']['typography']['fontSize']; | ||
|
||
if ( ! isset( $font_size ) && ! isset( $custom_font_size ) ) { | ||
return null; | ||
}; | ||
|
||
$has_named_font_size = ! empty( $font_size ); | ||
$has_custom_font_size = isset( $custom_font_size ); | ||
|
||
if ( $has_named_font_size ) { | ||
return array( | ||
'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ), | ||
'style' => null, | ||
); | ||
} elseif ( $has_custom_font_size ) { | ||
return array( | ||
'class' => null, | ||
'style' => sprintf( 'font-size: %s;', $custom_font_size ), | ||
); | ||
} | ||
|
||
return array( | ||
'class' => null, | ||
'style' => null, | ||
); | ||
} | ||
|
||
/** | ||
* Get class and style for text-color from attributes. | ||
* | ||
* @param array $attributes Attributes. | ||
* | ||
* @return (array | null) | ||
*/ | ||
public static function get_text_color_class_and_style( $attributes ) { | ||
|
||
$text_color = $attributes['textColor']; | ||
$custom_text_color = $attributes['style']['color']['text']; | ||
|
||
if ( ! isset( $text_color ) && ! isset( $custom_text_color ) ) { | ||
return null; | ||
}; | ||
|
||
$text_color_class = sprintf( 'has-text-color' ); | ||
$text_color_style = sprintf( 'color: %s;', ( ! empty( $text_color ) ? $text_color : $custom_text_color ) ); | ||
|
||
return array( | ||
'class' => $text_color_class, | ||
'style' => $text_color_style, | ||
); | ||
} | ||
|
||
/** | ||
* Get class and style for text-color from attributes. | ||
* | ||
* @param array $attributes Attributes. | ||
* | ||
* @return (array | null) | ||
*/ | ||
public static function get_link_color_class_and_style( $attributes ) { | ||
|
||
$link_color = $attributes['style']['elements']['link']['color']['text']; | ||
|
||
if ( ! isset( $link_color ) ) { | ||
|
||
return null; | ||
}; | ||
|
||
$link_color_class = sprintf( 'has-link-color', $link_color ); | ||
$link_color_style = sprintf( 'color: %s;', $link_color ); | ||
|
||
return array( | ||
'class' => $link_color_class, | ||
'style' => $link_color_style, | ||
); | ||
} | ||
|
||
/** | ||
* Get class and style for line height from attributes. | ||
* | ||
* @param array $attributes Attributes. | ||
* | ||
* @return (array | null) | ||
*/ | ||
public static function get_line_height_class_and_style( $attributes ) { | ||
|
||
$line_height = $attributes['style']['typography']['lineHeight']; | ||
|
||
if ( ! isset( $line_height ) ) { | ||
return null; | ||
}; | ||
|
||
$line_height_style = 'line-height: ' . $line_height . ';'; | ||
|
||
return array( | ||
'class' => null, | ||
'style' => $line_height_style, | ||
); | ||
} | ||
|
||
/** | ||
* Get classes and styles from attributes. | ||
* | ||
* @param array $attributes Attributes. | ||
* | ||
* @return (array | null) | ||
*/ | ||
public static function get_classes_and_styles_by_attributes( $attributes ) { | ||
$classes_and_styles = array( | ||
line_height => self::get_line_height_class_and_style( $attributes ), | ||
text_color => self::get_text_color_class_and_style( $attributes ), | ||
font_size => self::get_font_size_class_and_style( $attributes ), | ||
link_color => self::get_link_color_class_and_style( $attributes ), | ||
); | ||
|
||
return array_filter( | ||
$classes_and_styles, | ||
function ( $var ) { | ||
return ! is_null( $var ); | ||
} | ||
); | ||
|
||
} | ||
|
||
|
||
} |