Completed
Push — master ( ebef7a...94d09a )
by Zack
20:55 queued 16:56
created

GravityView_Admin_View_Item::get_item_info()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 33
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 1
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
class GravityView_Admin_View_Field extends GravityView_Admin_View_Item {
4
5
	private $label_type = 'field';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $label_type is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
7
	protected function additional_info() {
8
9
		$field_info = '';
10
11
		$field_info_items = array();
12
13
		// Fields with IDs, not like Source URL or Entry ID
14
		if( is_numeric( $this->id ) ) {
15
16
			$field_type_title = GFCommon::get_field_type_title( $this->item['input_type'] );
17
18
			$field_info_items[] = array(
19
				'value' => sprintf( __('Type: %s', 'gravityview'), $field_type_title )
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
20
			);
21
22
			$field_info_items[] = array(
23
				'value' => sprintf( __('Field ID: %s', 'gravityview'), $this->id ),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
24
			);
25
26
		}
27
28
		if( !empty( $this->item['desc'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
29
			$field_info_items[] = array(
30
				'value' => $this->item['desc']
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
31
			);
32
		}
33
34
		if( !empty( $this->item['adminLabel'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
35
			$field_info_items[] = array(
36
				'value' => sprintf( __('Admin Label: %s', 'gravityview' ), $this->item['adminLabel'] ),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
37
				'class'	=> 'gv-sublabel'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
38
			);
39
		}
40
41
		return $field_info_items;
42
	}
43
44
}
45
46
class GravityView_Admin_View_Widget extends GravityView_Admin_View_Item {
47
48
	private $label_type = 'widget';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $label_type is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
50
	protected function additional_info() {
51
52
		$field_info = '';
53
		$field_info_items = array();
54
55
		if( !empty( $this->item['description'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
56
57
			$field_info_items[] = array(
58
				'value' => $this->item['description']
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
59
			);
60
61
		}
62
63
		return $field_info_items;
64
	}
65
66
}
67
68
/**
69
 * A field or widget in GravityView view configuration
70
 */
71
class GravityView_Admin_View_Item {
72
73
	protected $title;
74
	protected $id;
75
	protected $subtitle;
76
	protected $settings_html;
77
	private   $label_type;
78
	protected $item;
79
80
	function __construct( $title = '', $field_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...
81
82
		// Backward compat
83
		if( !empty( $item['type'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
84
			$item['input_type'] = $item['type'];
85
			unset( $item['type'] );
86
		}
87
88
		// Prevent items from not having index set
89
		$item = wp_parse_args( $item, array(
90
			'label_text' => $title,
91
			'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...
92
			'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...
93
			'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...
94
			'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...
95
			'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...
96
			'adminLabel' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
97
			'adminOnly' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
98
			'subtitle' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
99
		));
100
101
		$this->title = $title;
102
		$this->item = $item;
103
		$this->id = $field_id;
104
		$this->settings = $settings;
0 ignored issues
show
Bug introduced by
The property settings does not seem to exist. Did you mean settings_html?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
		$this->label_type = $item['label_type'];
106
	}
107
108
	/**
109
	 * When echoing this class, print the HTML output
110
	 * @return string HTML output of the class
111
	 */
112
	public function __toString() {
113
114
		return $this->getOutput();
115
	}
116
117
	/**
118
	 * Overridden by child classes
119
	 * @return array Array of content with arrays for each item. Those arrays have `value`, `label` and (optional) `class` keys
120
	 */
121
	protected function additional_info() {
122
		return array();
123
	}
124
125
	/**
126
	 * Generate the output for a field based on the additional_info() output
127
	 *
128
	 * @see GravityView_Admin_View_Item::additional_info()
129
	 * @param  boolean $html Display HTML output? If yes, output is wrapped in spans. If no, plaintext.
130
	 * @return string|null        If empty, return null. Otherwise, return output HTML/text.
131
	 */
132
	protected function get_item_info( $html = true ) {
133
134
		$output = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
135
		$field_info_items = $this->additional_info();
136
137
		/**
138
		 * @filter `gravityview_admin_label_item_info` Tap in to modify the field information displayed next to an item
139
		 * @param array $field_info_items Additional information to display in a field
140
		 * @param GravityView_Admin_View_Field $this Field shown in the admin
141
		 */
142
		$field_info_items = apply_filters( 'gravityview_admin_label_item_info', $field_info_items, $this );
143
144
		if( $html ) {
145
146
			foreach ( $field_info_items as $item ) {
147
				$class = isset($item['class']) ? sanitize_html_class( $item['class'] ).' description' : 'description';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
148
				// Add the title in case the value's long, in which case, it'll be truncated by CSS.
149
				$output .= '<span class="'.$class.'">';
150
				$output .= esc_html( $item['value'] );
151
				$output .= '</span>';
152
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
153
154
		} else {
155
156
			$values = wp_list_pluck( $field_info_items, 'value' );
157
158
			$output = esc_html( implode(', ', $values) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
159
160
		}
161
162
		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...
163
164
	}
165
166
	/**
167
	 * Generate HTML for field or a widget modal
168
	 *
169
	 * @return string
170
	 */
171
	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...
172
173
		$settings_title = sprintf(__('Configure %s Settings', 'gravityview'), ucfirst($this->label_type));
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
174
		$delete_title = sprintf(__('Remove %s', 'gravityview'), ucfirst($this->label_type));
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
175
		$single_link_title = __('This field links to the Single Entry', 'gravityview');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
176
177
		// $settings_html will just be hidden inputs if empty. Otherwise, it'll have an <ul>. Ugly hack, I know.
178
		// TODO: Un-hack this
179
		$hide_settings_link = ( empty( $this->item['settings_html'] ) || strpos( $this->item['settings_html'], '<!-- No Options -->') > 0 ) ? 'hide-if-js' : '';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
180
		$settings_link = sprintf( '<a href="#settings" class="dashicons-admin-generic dashicons %s" title="%s"></a>', $hide_settings_link, esc_attr( $settings_title ) );
181
182
		// Should we show the icon that the field is being used as a link to single entry?
183
		$hide_show_as_link_class = empty( $this->settings['show_as_link'] ) ? 'hide-if-js' : '';
0 ignored issues
show
Bug introduced by
The property settings does not seem to exist. Did you mean settings_html?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
184
		$show_as_link = '<span class="dashicons dashicons-admin-links '.$hide_show_as_link_class.'" title="'.esc_attr( $single_link_title ).'"></span>';
185
186
		// When a field label is empty, use the Field ID
187
		$label = empty( $this->title ) ? sprintf( _x('Field #%s (No Label)', 'Label in field picker for empty label', 'gravityview'), $this->id ) : $this->title;
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
188
189
		// If there's a custom label, and show label is checked, use that as the field heading
190
		if( !empty( $this->settings['custom_label'] ) && !empty( $this->settings['show_label'] ) ) {
0 ignored issues
show
Bug introduced by
The property settings does not seem to exist. Did you mean settings_html?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
191
			$label = $this->settings['custom_label'];
0 ignored issues
show
Bug introduced by
The property settings does not seem to exist. Did you mean settings_html?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
192
		} else if( !empty( $this->item['customLabel'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
193
			$label = $this->item['customLabel'];
194
		}
195
196
		$output = '<h5 class="selectable gfield field-id-'.esc_attr($this->id).'">';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
197
198
		$label = esc_attr( $label );
199
200
		if( !empty( $this->item['parent'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
201
			$label .= ' <small>('.esc_attr( $this->item['parent']['label'] ) .')</small>';
202
		}
203
204
		// Name of field / widget
205
		$output .= '<span class="gv-field-label" data-original-title="'.esc_attr( $label ).'" title="'. $this->get_item_info( false ) .'">'. $label . '</span>';
206
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
207
208
		$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>';
209
210
		// Displays only in the field/widget picker.
211
		if( $field_info = $this->get_item_info() ) {
212
			$output .= '<span class="gv-field-info">'.$field_info.'</span>';
213
		}
214
215
		$output .= '</h5>';
216
217
		$container_class = !empty( $this->item['parent'] ) ? ' gv-child-field' : '';
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
218
219
		$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>';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
220
221
		return $output;
222
	}
223
224
}
225