Completed
Push — try/another-block-button ( 84ca86...b6e26a )
by
unknown
06:43
created

functions.jetpack-button-helper.php ➔ jetpack_get_button_styles()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 34

Duplication

Lines 8
Ratio 23.53 %

Importance

Changes 0
Metric Value
cc 12
nc 16
nop 1
dl 8
loc 34
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Helper for the shared Button component for CTA blocks.
4
 *
5
 * @package jetpack
6
 */
7
8
if ( ! function_exists( 'jetpack_get_button_classes' ) ) {
9
	/**
10
	 * Get the shared Button component classes.
11
	 *
12
	 * @param array $attributes Array containing the block attributes.
13
	 *
14
	 * @return string
15
	 */
16
	function jetpack_get_button_classes( $attributes ) {
17
		$classes                     = array( 'wp-block-button__link' );
18
		$has_class_name              = array_key_exists( 'className', $attributes );
19
		$has_named_text_color        = array_key_exists( 'buttonTextColor', $attributes );
20
		$has_custom_text_color       = array_key_exists( 'customButtonTextColor', $attributes );
21
		$has_named_background_color  = array_key_exists( 'buttonBackgroundColor', $attributes );
22
		$has_custom_background_color = array_key_exists( 'customButtonBackgroundColor', $attributes );
23
		$has_named_gradient          = array_key_exists( 'buttonGradient', $attributes );
24
		$has_custom_gradient         = array_key_exists( 'customButtonGradient', $attributes );
25
		$has_border_radius           = array_key_exists( 'buttonBorderRadius', $attributes );
26
27
		if ( $has_class_name ) {
28
			$classes[] = $attributes['className'];
29
		}
30
31
		if ( $has_named_text_color || $has_custom_text_color ) {
32
			$classes[] = 'has-text-color';
33
		}
34
		if ( $has_named_text_color ) {
35
			$classes[] = sprintf( 'has-%s-color', $attributes['buttonTextColor'] );
36
		}
37
38
		if (
39
			$has_named_background_color ||
40
			$has_custom_background_color ||
41
			$has_named_gradient ||
42
			$has_custom_gradient
43
		) {
44
			$classes[] = 'has-background';
45
		}
46
		if ( $has_named_background_color && ! $has_custom_gradient ) {
47
			$classes[] = sprintf( 'has-%s-background-color', $attributes['buttonBackgroundColor'] );
48
		}
49
		if ( $has_named_gradient ) {
50
			$classes[] = sprintf( 'has-%s-gradient-background', $attributes['buttonGradient'] );
51
		}
52
53
		// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
54
		if ( $has_border_radius && 0 == $attributes['buttonBorderRadius'] ) {
55
			$classes[] = 'no-border-radius';
56
		}
57
58
		return implode( ' ', $classes );
59
	}
60
}
61
62
if ( ! function_exists( 'jetpack_get_button_styles' ) ) {
63
	/**
64
	 * Get the shared Button component styles.
65
	 *
66
	 * @param array $attributes Array containing the block attributes.
67
	 *
68
	 * @return string
69
	 */
70
	function jetpack_get_button_styles( $attributes ) {
71
		$styles                      = array();
72
		$has_named_text_color        = array_key_exists( 'buttonTextColor', $attributes );
73
		$has_custom_text_color       = array_key_exists( 'customButtonTextColor', $attributes );
74
		$has_named_background_color  = array_key_exists( 'buttonBackgroundColor', $attributes );
75
		$has_custom_background_color = array_key_exists( 'customButtonBackgroundColor', $attributes );
76
		$has_named_gradient          = array_key_exists( 'buttonGradient', $attributes );
77
		$has_custom_gradient         = array_key_exists( 'customButtonGradient', $attributes );
78
		$has_border_radius           = array_key_exists( 'buttonBorderRadius', $attributes );
79
80
		if ( ! $has_named_text_color && $has_custom_text_color ) {
81
			$styles[] = sprintf( 'color: %s;', $attributes['customButtonTextColor'] );
82
		}
83
84
		if ( ! $has_named_background_color && ! $has_named_gradient && $has_custom_gradient ) {
85
			$styles[] = sprintf( 'background: %s;', $attributes['customButtonGradient'] );
86
		}
87
88 View Code Duplication
		if (
89
			$has_custom_background_color &&
90
			! $has_named_background_color &&
91
			! $has_named_gradient &&
92
			! $has_custom_gradient
93
		) {
94
			$styles[] = sprintf( 'background-color: %s;', $attributes['customButtonBackgroundColor'] );
95
		}
96
97
		// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
98
		if ( $has_border_radius && 0 != $attributes['buttonBorderRadius'] ) {
99
			$styles[] = sprintf( 'border-radius: %spx;', $attributes['buttonBorderRadius'] );
100
		}
101
102
		return implode( ' ', $styles );
103
	}
104
}
105