Completed
Push — add/product-ratings-to-search ( 91683c...b05261 )
by
unknown
344:08 queued 334:42
created

legacy-buttons.php ➔ create_legacy_buttons_markup()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 44

Duplication

Lines 18
Ratio 40.91 %

Importance

Changes 0
Metric Value
cc 12
nc 4
nop 3
dl 18
loc 44
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
 * Create legacy buttons markup.
4
 *
5
 * @package Automattic\Jetpack\Extensions\Premium_Content
6
 */
7
8
namespace Automattic\Jetpack\Extensions\Premium_Content;
9
10
/**
11
 * Creates a subscribe/login buttons markup for legacy blocks.
12
 *
13
 * @param array  $attributes Block attributes.
14
 * @param string $content    String containing the block content.
15
 * @param object $block      Legacy block.
16
 *
17
 * @return string Subscribe/login buttons markup.
18
 */
19
function create_legacy_buttons_markup( $attributes, $content, $block ) {
20
	$button_styles = array();
21 View Code Duplication
	if ( ! empty( $attributes['customBackgroundButtonColor'] ) ) {
22
		array_push(
23
			$button_styles,
24
			sprintf(
25
				'background-color: %s',
26
				isset( $attributes['customBackgroundButtonColor'] ) ? sanitize_hex_color( $attributes['customBackgroundButtonColor'] ) : 'transparent'
27
			)
28
		);
29
	}
30 View Code Duplication
	if ( ! empty( $attributes['customTextButtonColor'] ) ) {
31
		array_push(
32
			$button_styles,
33
			sprintf(
34
				'color: %s',
35
				isset( $attributes['customTextButtonColor'] ) ? sanitize_hex_color( $attributes['customTextButtonColor'] ) : 'inherit'
36
			)
37
		);
38
	}
39
	$button_styles = implode( ';', $button_styles );
40
41
	$login_button = sprintf(
42
		'<div class="wp-block-button"><a role="button" href="%1$s" class="%2$s" style="%3$s">%4$s</a></div>',
43
		subscription_service()->access_url(),
44
		empty( $attributes['buttonClasses'] ) ? 'wp-block-button__link' : esc_attr( $attributes['buttonClasses'] ),
45
		esc_attr( $button_styles ),
46
		empty( $attributes['loginButtonText'] ) ? __( 'Log In', 'jetpack' ) : $attributes['loginButtonText']
47
	);
48
49
	$subscribe_button = \Jetpack_Memberships::get_instance()->render_button(
50
		array(
51
			'planId'                      => empty( $block->context['premium-content/planId'] ) ? 0 : $block->context['premium-content/planId'],
52
			'submitButtonClasses'         => empty( $attributes['buttonClasses'] ) ? 'wp-block-button__link' : esc_attr( $attributes['buttonClasses'] ),
53
			'customTextButtonColor'       => empty( $attributes['customTextButtonColor'] ) ? '' : esc_attr( $attributes['customTextButtonColor'] ),
54
			'customBackgroundButtonColor' => empty( $attributes['customBackgroundButtonColor'] ) ? '' : esc_attr( $attributes['customBackgroundButtonColor'] ),
55
			'submitButtonText'            => empty( $attributes['subscribeButtonText'] ) ? __( 'Subscribe', 'jetpack' ) : esc_attr( $attributes['subscribeButtonText'] ),
56
		),
57
		$content,
58
		$block
59
	);
60
61
	return "<div class='wp-block-premium-content-logged-out-view__buttons'>{$subscribe_button}{$login_button}</div>";
62
}
63