Passed
Pull Request — master (#351)
by Brian
456:50 queued 353:15
created

SD_Hello_World::output()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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