Completed
Push — refactor/subscriptions ( 2c6651 )
by Jeremy
23:44 queued 13:28
created

Shortcode::render_form()   F

Complexity

Conditions 12
Paths 512

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 512
nop 1
dl 0
loc 56
rs 3.7422
c 0
b 0
f 0

How to fix   Long Method    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
 * Display the Subscriptions shortcode
4
 * on WordPress.com or in Jetpack
5
 *
6
 * @package Jetpack
7
 */
8
9
namespace Automattic\Jetpack\Subscriptions;
10
11
use Automattic\Jetpack\Subscriptions\Helpers;
12
use Automattic\Jetpack\Subscriptions\Widget;
13
14
add_shortcode( 'jetpack_subscription_form', array( 'Automattic\\Jetpack\\Subscriptions\\Shortcode', 'render_form' ) );
15
add_shortcode( 'blog_subscription_form', array( 'Automattic\\Jetpack\\Subscriptions\\Shortcode', 'render_form' ) );
16
17
/*
18
 * This may be worth trying, it is shorter, looks better
19
 * add_shortcode( 'jetpack_subscription_form', array( __NAMESPACE__ . '\\Shortcode', 'render_form' ) );
20
 * add_shortcode( 'blog_subscription_form', array( __NAMESPACE__ . '\\Shortcode', 'render_form' ) );
21
 */
22
23
/**
24
 * Render a shortcode based on the Subscriptions Widget.
25
 */
26
class Shortcode {
27
	/**
28
	 * Render form.
29
	 *
30
	 * @param array $instance Shortcode parameters.
31
	 */
32
	public static function render_form( $instance ) {
33
		if ( empty( $instance ) || ! is_array( $instance ) ) {
34
			$instance = array();
35
		}
36
37
		if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
38
			$instance['show_subscribers_total'] = false;
39
		} else {
40
			$instance['show_subscribers_total'] = true;
41
		}
42
43
		$show_only_email_and_button = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
44
		$submit_button_text         = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
45
46
		/*
47
		 * Build up a string
48
		 * with the submit button's classes and styles
49
		 * and set it on the instance
50
		 */
51
		$submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
52
		$submit_button_styles  = '';
53
		if ( isset( $instance['custom_background_button_color'] ) ) {
54
			$submit_button_styles .= 'background-color: ' . $instance['custom_background_button_color'] . '; ';
55
		}
56
		if ( isset( $instance['custom_text_button_color'] ) ) {
57
			$submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . ';';
58
		}
59
60
		$instance = shortcode_atts(
61
			Widget::defaults(),
0 ignored issues
show
Bug introduced by
The method defaults() cannot be called from this context as it is declared private in class Automattic\Jetpack\Subscriptions\Widget.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
62
			$instance,
63
			'jetpack_subscription_form'
64
		);
65
66
		// These must come after the call to shortcode_atts().
67
		$instance['submit_button_text']         = $submit_button_text;
68
		$instance['show_only_email_and_button'] = $show_only_email_and_button;
69
		if ( ! empty( $submit_button_classes ) ) {
70
			$instance['submit_button_classes'] = $submit_button_classes;
71
		}
72
		if ( ! empty( $submit_button_styles ) ) {
73
			$instance['submit_button_styles'] = $submit_button_styles;
74
		}
75
76
		$args = array(
77
			'before_widget' => '<div class="jetpack_subscription_widget">',
78
		);
79
80
		ob_start();
81
82
		the_widget( Helpers::widget_classname(), $instance, $args );
83
84
		$output = ob_get_clean();
85
86
		return $output;
87
	}
88
}
89