Completed
Push — master ( 7ba298...2ce894 )
by Zack
38:21 queued 18:44
created

GravityView_Admin_View_Item   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 178
ccs 0
cts 75
cp 0
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 28 2
A __toString() 0 4 1
A additional_info() 0 3 1
B get_item_info() 0 33 5
D getOutput() 0 52 11
1
<?php
2
/**
3
 * @file class-gravityview-admin-view-item.php
4
 * @since 1.17.3
5
 */
6
7
/**
8
 * A field or widget in GravityView view configuration
9
 */
10
abstract class GravityView_Admin_View_Item {
11
12
	/**
13
	 * @var string Name of the item in the field or widget picker
14
	 */
15
	protected $title;
16
17
	/**
18
	 * @var string The field ID or the widget slug ( `2.3` or `custom_content`)
19
	 */
20
	protected $id;
21
22
	/**
23
	 * @var string Description of the item
24
	 */
25
	protected $subtitle;
26
27
	/**
28
	 * @var string The type of item ("field" or "widget")
29
	 */
30
	protected $label_type;
31
32
	/**
33
	 * @var array Associative array of item details
34
	 */
35
	protected $item;
36
37
	/**
38
	 * @var array Existing settings for the item
39
	 */
40
	protected $settings;
41
42
	function __construct( $title = '', $item_id, $item = array(), $settings = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
44
		// Backward compat
45
		if ( ! empty( $item['type'] ) ) {
46
			$item['input_type'] = $item['type'];
47
			unset( $item['type'] );
48
		}
49
50
		// Prevent items from not having index set
51
		$item = wp_parse_args( $item, array(
52
			'label_text'    => $title,
53
			'field_id'      => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
54
			'parent_label'  => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
55
			'label_type'    => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
56
			'input_type'    => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
57
			'settings_html' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
58
			'adminLabel'    => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
59
			'adminOnly'     => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
60
			'subtitle'      => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
61
			'placeholder'   => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
62
		) );
63
64
		$this->title      = $title;
65
		$this->item       = $item;
66
		$this->id         = $item_id;
67
		$this->settings   = $settings;
68
		$this->label_type = $item['label_type'];
69
	}
70
71
	/**
72
	 * When echoing this class, print the HTML output
73
	 * @return string HTML output of the class
74
	 */
75
	public function __toString() {
76
77
		return $this->getOutput();
78
	}
79
80
	/**
81
	 * Overridden by child classes
82
	 * @return array Array of content with arrays for each item. Those arrays have `value`, `label` and (optional) `class` keys
83
	 */
84
	protected function additional_info() {
85
		return array();
86
	}
87
88
	/**
89
	 * Generate the output for a field based on the additional_info() output
90
	 *
91
	 * @see GravityView_Admin_View_Item::additional_info()
92
	 * @param  boolean $html Display HTML output? If yes, output is wrapped in spans. If no, plaintext.
93
	 * @return string|null        If empty, return null. Otherwise, return output HTML/text.
94
	 */
95
	protected function get_item_info( $html = true ) {
96
97
		$output           = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
98
		$field_info_items = $this->additional_info();
99
100
		/**
101
		 * @filter `gravityview_admin_label_item_info` Tap in to modify the field information displayed next to an item
102
		 *
103
		 * @param array $field_info_items Additional information to display in a field
104
		 * @param GravityView_Admin_View_Field $this Field shown in the admin
105
		 */
106
		$field_info_items = apply_filters( 'gravityview_admin_label_item_info', $field_info_items, $this );
107
108
		if ( $html ) {
109
110
			foreach ( $field_info_items as $item ) {
111
				$class = isset( $item['class'] ) ? sanitize_html_class( $item['class'] ) . ' description' : 'description';
112
				// Add the title in case the value's long, in which case, it'll be truncated by CSS.
113
				$output .= '<span class="' . $class . '">';
114
				$output .= esc_html( $item['value'] );
115
				$output .= '</span>';
116
			}
117
118
		} else {
119
120
			$values = wp_list_pluck( $field_info_items, 'value' );
121
122
			$output = esc_html( implode( ', ', $values ) );
123
124
		}
125
126
		return empty( $output ) ? NULL : $output;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
127
	}
128
129
	/**
130
	 * Generate HTML for field or a widget modal
131
	 *
132
	 * @return string
133
	 */
134
	function getOutput() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
The function name getOutput is in camel caps, but expected get_output instead as per the coding standard.
Loading history...
135
136
		$settings_title    = sprintf( __( 'Configure %s Settings', 'gravityview' ), ucfirst( $this->label_type ) );
137
		$delete_title      = sprintf( __( 'Remove %s', 'gravityview' ), ucfirst( $this->label_type ) );
138
		$single_link_title = __( 'This field links to the Single Entry', 'gravityview' );
139
140
		// $settings_html will just be hidden inputs if empty. Otherwise, it'll have an <ul>. Ugly hack, I know.
141
		// TODO: Un-hack this
142
		$hide_settings_link = ( empty( $this->item['settings_html'] ) || strpos( $this->item['settings_html'], '<!-- No Options -->' ) > 0 ) ? 'hide-if-js' : '';
143
		$settings_link      = sprintf( '<a href="#settings" class="dashicons-admin-generic dashicons %s" title="%s"></a>', $hide_settings_link, esc_attr( $settings_title ) );
144
145
		// Should we show the icon that the field is being used as a link to single entry?
146
		$hide_show_as_link_class = empty( $this->settings['show_as_link'] ) ? 'hide-if-js' : '';
147
		$show_as_link            = '<span class="dashicons dashicons-admin-links ' . $hide_show_as_link_class . '" title="' . esc_attr( $single_link_title ) . '"></span>';
148
149
		// When a field label is empty, use the Field ID
150
		$label = empty( $this->title ) ? sprintf( _x( 'Field #%s (No Label)', 'Label in field picker for empty label', 'gravityview' ), $this->id ) : $this->title;
151
152
		// If there's a custom label, and show label is checked, use that as the field heading
153
		if ( ! empty( $this->settings['custom_label'] ) && ! empty( $this->settings['show_label'] ) ) {
154
			$label = $this->settings['custom_label'];
155
		} else if ( ! empty( $this->item['customLabel'] ) ) {
156
			$label = $this->item['customLabel'];
157
		}
158
159
		$output = '<h5 class="selectable gfield field-id-' . esc_attr( $this->id ) . '">';
160
161
		$label = esc_attr( $label );
162
163
		if ( ! empty( $this->item['parent'] ) ) {
164
			$label .= ' <small>(' . esc_attr( $this->item['parent']['label'] ) . ')</small>';
165
		}
166
167
		// Name of field / widget
168
		$output .= '<span class="gv-field-label" data-original-title="' . esc_attr( $label ) . '" title="' . $this->get_item_info( false ) . '">' . $label . '</span>';
169
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
170
171
		$output .= '<span class="gv-field-controls">' . $settings_link . $show_as_link . '<a href="#remove" class="dashicons-dismiss dashicons" title="' . esc_attr( $delete_title ) . '"></a></span>';
172
173
		// Displays only in the field/widget picker.
174
		if ( $field_info = $this->get_item_info() ) {
175
			$output .= '<span class="gv-field-info">' . $field_info . '</span>';
176
		}
177
178
		$output .= '</h5>';
179
180
		$container_class = ! empty( $this->item['parent'] ) ? ' gv-child-field' : '';
181
182
		$output = '<div data-fieldid="' . esc_attr( $this->id ) . '" data-inputtype="' . esc_attr( $this->item['input_type'] ) . '" class="gv-fields' . $container_class . '">' . $output . $this->item['settings_html'] . '</div>';
183
184
		return $output;
185
	}
186
187
}
188