Completed
Push — develop ( df88cb...e66816 )
by Gennady
20:29 queued 01:32
created

GravityView_Admin_View_Item::get_item_info()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 1
dl 0
loc 38
ccs 0
cts 20
cp 0
crap 42
rs 8.6897
c 0
b 0
f 0
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
	/**
43
	 * @var string For ID, if available
44
	 */
45
	protected $form_id;
46
47
	function __construct( $title = '', $item_id, $item = array(), $settings = array(), $form_id = null) {
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...
48
49
		// Backward compat
50
		if ( ! empty( $item['type'] ) ) {
51
			$item['input_type'] = $item['type'];
52
			unset( $item['type'] );
53
		}
54
55
		// Prevent items from not having index set
56
		$item = wp_parse_args( $item, array(
57
			'label_text'    => $title,
58
			'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...
59
			'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...
60
			'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...
61
			'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...
62
			'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...
63
			'adminLabel'    => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
64
			'adminOnly'     => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
65
			'subtitle'      => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
66
			'placeholder'   => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
67
		) );
68
69
		$this->title      = $title;
70
		$this->item       = $item;
71
		$this->id         = $item_id;
72
		$this->form_id    = $form_id;
73
		$this->settings   = $settings;
74
		$this->label_type = $item['label_type'];
75
	}
76
77
	/**
78
	 * When echoing this class, print the HTML output
79
	 * @return string HTML output of the class
80
	 */
81
	public function __toString() {
82
83
		return $this->getOutput();
84
	}
85
86
	/**
87
	 * Overridden by child classes
88
	 * @return array Array of content with arrays for each item. Those arrays have `value`, `label` and (optional) `class` keys
89
	 */
90
	protected function additional_info() {
91
		return array();
92
	}
93
94
	/**
95
	 * Generate the output for a field based on the additional_info() output
96
	 *
97
	 * @see GravityView_Admin_View_Item::additional_info()
98
	 * @param  boolean $html Display HTML output? If yes, output is wrapped in spans. If no, plaintext.
99
	 * @return string|null        If empty, return null. Otherwise, return output HTML/text.
100
	 */
101
	protected function get_item_info( $html = true ) {
102
103
		$output           = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
104
		$field_info_items = $this->additional_info();
105
106
		/**
107
		 * @filter `gravityview_admin_label_item_info` Tap in to modify the field information displayed next to an item
108
		 *
109
		 * @param array $field_info_items Additional information to display in a field
110
		 * @param GravityView_Admin_View_Field $this Field shown in the admin
111
		 */
112
		$field_info_items = apply_filters( 'gravityview_admin_label_item_info', $field_info_items, $this );
113
114
		if ( $html ) {
115
116
			foreach ( $field_info_items as $item ) {
117
118
				if( \GV\Utils::get( $item, 'hide_in_picker', false ) ) {
119
					continue;
120
				}
121
122
				$class = isset( $item['class'] ) ? sanitize_html_class( $item['class'] ) . ' description' : 'description';
123
				// Add the title in case the value's long, in which case, it'll be truncated by CSS.
124
				$output .= '<span class="' . $class . '">';
125
				$output .= esc_html( $item['value'] );
126
				$output .= '</span>';
127
			}
128
129
		} else {
130
131
			$values = wp_list_pluck( $field_info_items, 'value' );
132
133
			$output = esc_html( implode( ', ', $values ) );
134
135
		}
136
137
		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...
138
	}
139
140
	/**
141
	 * Generate HTML for field or a widget modal
142
	 *
143
	 * @return string
144
	 */
145
	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...
146
147
		$settings_title    = sprintf( __( 'Configure %s Settings', 'gravityview' ), ucfirst( $this->label_type ) );
148
		$delete_title      = sprintf( __( 'Remove %s', 'gravityview' ), ucfirst( $this->label_type ) );
149
		$single_link_title = __( 'This field links to the Single Entry', 'gravityview' );
150
151
		// $settings_html will just be hidden inputs if empty. Otherwise, it'll have an <ul>. Ugly hack, I know.
152
		// TODO: Un-hack this
153
		$hide_settings_link = ( empty( $this->item['settings_html'] ) || strpos( $this->item['settings_html'], '<!-- No Options -->' ) > 0 ) ? 'hide-if-js' : '';
154
		$settings_link      = sprintf( '<a href="#settings" class="dashicons-admin-generic dashicons %s" title="%s"></a>', $hide_settings_link, esc_attr( $settings_title ) );
155
156
		// Should we show the icon that the field is being used as a link to single entry?
157
		$hide_show_as_link_class = empty( $this->settings['show_as_link'] ) ? 'hide-if-js' : '';
158
		$show_as_link            = '<span class="dashicons dashicons-admin-links ' . $hide_show_as_link_class . '" title="' . esc_attr( $single_link_title ) . '"></span>';
159
160
		// When a field label is empty, use the Field ID
161
		$label = empty( $this->title ) ? sprintf( _x( 'Field #%s (No Label)', 'Label in field picker for empty label', 'gravityview' ), $this->id ) : $this->title;
162
163
		// If there's a custom label, and show label is checked, use that as the field heading
164
		if ( ! empty( $this->settings['custom_label'] ) && ! empty( $this->settings['show_label'] ) ) {
165
			$label = $this->settings['custom_label'];
166
		} else if ( ! empty( $this->item['customLabel'] ) ) {
167
			$label = $this->item['customLabel'];
168
		}
169
170
		$output = '<h5 class="selectable gfield field-id-' . esc_attr( $this->id ) . '">';
171
172
		$label = esc_attr( $label );
173
174
		if ( ! empty( $this->item['parent'] ) ) {
175
			$label .= ' <small>(' . esc_attr( $this->item['parent']['label'] ) . ')</small>';
176
		}
177
178
		// Name of field / widget
179
		$output .= '<span class="gv-field-label" data-original-title="' . esc_attr( $label ) . '" title="' . $this->get_item_info( false ) . '">' . $label . '</span>';
180
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
181
182
		$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>';
183
184
		// Displays only in the field/widget picker.
185
		if ( $field_info = $this->get_item_info() ) {
186
			$output .= '<span class="gv-field-info">' . $field_info . '</span>';
187
		}
188
189
		$output .= '</h5>';
190
191
		$container_class = ! empty( $this->item['parent'] ) ? ' gv-child-field' : '';
192
		$data_form_id   = ! empty( $this->form_id ) ? 'data-formid="' . esc_attr( $this->form_id ) . '"' : '';
193
194
		$output = '<div data-fieldid="' . esc_attr( $this->id ) . '" ' . $data_form_id . ' data-inputtype="' . esc_attr( $this->item['input_type'] ) . '" class="gv-fields' . $container_class . '">' . $output . $this->item['settings_html'] . '</div>';
195
196
		return $output;
197
	}
198
199
}
200