Passed
Pull Request — master (#191)
by
unknown
03:10
created

SD_Hello_World::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 60
rs 8.8727
c 0
b 0
f 0

How to fix   Long Method   

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
class SD_Hello_World extends WP_Super_Duper {
4
5
6
	public $arguments;
7
8
	/**
9
	 * Sets up the widgets name etc
10
	 */
11
	public function __construct() {
12
13
		$options = array(
14
			'textdomain'     => 'super-duper',
15
			// textdomain of the plugin/theme (used to prefix the Gutenberg block)
16
			'block-icon'     => 'admin-site',
17
			// Dash icon name for the block: https://developer.wordpress.org/resource/dashicons/#arrow-right
18
			'block-category' => 'widgets',
19
			// the category for the block, 'common', 'formatting', 'layout', 'widgets', 'embed'.
20
			'block-keywords' => "['hello','world']",
21
			// used in the block search, MAX 3
22
//			'block-output'   => array( // the block visual output elements as an array
23
//				'element::p' => array(
24
//					'title'   => __( 'Placeholder', 'hello-world' ),
25
//					'class'   => '[%className%]',
26
//					'content' => 'Hello: [%after_text%]' // block properties can be added by wrapping them in [%name%]
27
//				)
28
//			),
29
			'block-output'   => array( // the block visual output elements as an array
30
				array(
31
					'element' => 'p',
32
					'title'   => __( 'Placeholder', 'hello-world' ),
33
					'class'   => '[%className%]',
34
					'content' => 'Hello: [%after_text%]' // block properties can be added by wrapping them in [%name%]
35
				)
36
			),
37
			'class_name'     => __CLASS__,
38
			// The calling class name
39
			'base_id'        => 'hello_world',
40
			// this is used as the widget id and the shortcode id.
41
			'name'           => __( 'Hello World', 'hello-world' ),
42
			// the name of the widget/block
43
			'widget_ops'     => array(
44
				'classname'   => 'hello-world-class',
45
				// widget class
46
				'description' => esc_html__( 'This is an example that will take a text parameter and output it after `Hello:`.', 'hello-world' ),
47
				// widget description
48
			),
49
			'arguments'      => array( // these are the arguments that will be used in the widget, shortcode and block settings.
50
				'after_text' => array( // this is the input name=''
51
					'title'       => __( 'Text after hello:', 'hello-world' ),
52
					// input title
53
					'desc'        => __( 'This is the text that will appear after `Hello:`.', 'hello-world' ),
54
					// input description
55
					'type'        => 'text',
56
					// the type of input, test, select, checkbox etc.
57
					'placeholder' => 'World',
58
					// the input placeholder text.
59
					'desc_tip'    => true,
60
					// if the input should show the widget description text as a tooltip.
61
					'default'     => 'World',
62
					// the input default value.
63
					'advanced'    => false
64
					// not yet implemented
65
				),
66
			)
67
		);
68
69
		parent::__construct( $options );
70
	}
71
72
73
	/**
74
	 * This is the output function for the widget, shortcode and block (front end).
75
	 *
76
	 * @param array $args The arguments values.
77
	 * @param array $widget_args The widget arguments when used.
78
	 * @param string $content The shortcode content argument
79
	 *
80
	 * @return string
81
	 */
82
	public function output( $args = array(), $widget_args = array(), $content = '' ) {
83
84
		/**
85
		 * @var string $after_text
86
		 * @var string $another_input This is added by filter below.
87
		 */
88
		extract( $args, EXTR_SKIP );
89
90
		/*
91
		 * This value is added by filter so might not exist if filter is removed so we check.
92
		 */
93
		if ( ! $another_input ) {
94
			$another_input = '';
95
		}
96
97
		return "Hello: " . $after_text . "" . $another_input;
98
99
	}
100
101
}
102
103
// register it.
104
add_action( 'widgets_init', function () {
105
	register_widget( 'SD_Hello_World' );
106
} );
107
108
109
/**
110
 * Extend the options via filter hook, this can be done via plugin/theme.
111
 *
112
 * @param $options
113
 *
114
 * @return mixed
115
 */
116
function _my_extra_arguments( $options ) {
117
118
	/*
119
	 * Add a new input option.
120
	 */
121
	$options['arguments']['another_input'] = array(
122
		'name'        => 'another_input', // this is the input name=''
123
		'title'       => __( 'Another input:', 'hello-world' ), // input title
124
		'desc'        => __( 'This is an input added via filter.', 'hello-world' ), // input description
125
		'type'        => 'text', // the type of input, test, select, checkbox etc.
126
		'placeholder' => 'Placeholder text', // the input placeholder text.
127
		'desc_tip'    => true, // if the input should show the widget description text as a tooltip.
128
		'default'     => '', // the input default value.
129
		'advanced'    => false // not yet implemented
130
	);
131
132
	/*
133
	 * Output the new option in the block output also.
134
	 */
135
	$options['block-output']['element::p']['content'] = $options['block-output']['element::p']['content'] . " [%another_input%]";;
136
137
	return $options;
138
}
139
140
//add_filter( 'wp_super_duper_options_hello_world', '_my_extra_arguments' );