Completed
Push — master ( 92cdcd...79386d )
by Zack
20:40 queued 10:41
created

GravityView_Admin_View_Item::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 27
rs 8.8571
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
	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...
38
39
		// Backward compat
40
		if( !empty( $item['type'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
41
			$item['input_type'] = $item['type'];
42
			unset( $item['type'] );
43
		}
44
45
		// Prevent items from not having index set
46
		$item = wp_parse_args( $item, array(
47
			'label_text' => $title,
48
			'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...
49
			'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...
50
			'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...
51
			'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...
52
			'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...
53
			'adminLabel' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
54
			'adminOnly' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
55
			'subtitle' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
56
		));
57
58
		$this->title = $title;
59
		$this->item = $item;
60
		$this->id = $item_id;
61
		$this->settings = $settings;
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62
		$this->label_type = $item['label_type'];
63
	}
64
65
	/**
66
	 * When echoing this class, print the HTML output
67
	 * @return string HTML output of the class
68
	 */
69
	public function __toString() {
70
71
		return $this->getOutput();
72
	}
73
74
	/**
75
	 * Overridden by child classes
76
	 * @return array Array of content with arrays for each item. Those arrays have `value`, `label` and (optional) `class` keys
77
	 */
78
	protected function additional_info() {
79
		return array();
80
	}
81
82
	/**
83
	 * Generate the output for a field based on the additional_info() output
84
	 *
85
	 * @see GravityView_Admin_View_Item::additional_info()
86
	 * @param  boolean $html Display HTML output? If yes, output is wrapped in spans. If no, plaintext.
87
	 * @return string|null        If empty, return null. Otherwise, return output HTML/text.
88
	 */
89
	protected function get_item_info( $html = true ) {
90
91
		$output = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
92
		$field_info_items = $this->additional_info();
93
94
		/**
95
		 * @filter `gravityview_admin_label_item_info` Tap in to modify the field information displayed next to an item
96
		 * @param array $field_info_items Additional information to display in a field
97
		 * @param GravityView_Admin_View_Field $this Field shown in the admin
98
		 */
99
		$field_info_items = apply_filters( 'gravityview_admin_label_item_info', $field_info_items, $this );
100
101
		if( $html ) {
102
103
			foreach ( $field_info_items as $item ) {
104
				$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...
105
				// Add the title in case the value's long, in which case, it'll be truncated by CSS.
106
				$output .= '<span class="'.$class.'">';
107
				$output .= esc_html( $item['value'] );
108
				$output .= '</span>';
109
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
110
111
		} else {
112
113
			$values = wp_list_pluck( $field_info_items, 'value' );
114
115
			$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...
116
117
		}
118
119
		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...
120
121
	}
122
123
	/**
124
	 * Generate HTML for field or a widget modal
125
	 *
126
	 * @return string
127
	 */
128
	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...
129
130
		$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...
131
		$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...
132
		$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...
133
134
		// $settings_html will just be hidden inputs if empty. Otherwise, it'll have an <ul>. Ugly hack, I know.
135
		// TODO: Un-hack this
136
		$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...
137
		$settings_link = sprintf( '<a href="#settings" class="dashicons-admin-generic dashicons %s" title="%s"></a>', $hide_settings_link, esc_attr( $settings_title ) );
138
139
		// Should we show the icon that the field is being used as a link to single entry?
140
		$hide_show_as_link_class = empty( $this->settings['show_as_link'] ) ? 'hide-if-js' : '';
141
		$show_as_link = '<span class="dashicons dashicons-admin-links '.$hide_show_as_link_class.'" title="'.esc_attr( $single_link_title ).'"></span>';
142
143
		// When a field label is empty, use the Field ID
144
		$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...
145
146
		// If there's a custom label, and show label is checked, use that as the field heading
147
		if( !empty( $this->settings['custom_label'] ) && !empty( $this->settings['show_label'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
148
			$label = $this->settings['custom_label'];
149
		} else if( !empty( $this->item['customLabel'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
150
			$label = $this->item['customLabel'];
151
		}
152
153
		$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...
154
155
		$label = esc_attr( $label );
156
157
		if( !empty( $this->item['parent'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
158
			$label .= ' <small>('.esc_attr( $this->item['parent']['label'] ) .')</small>';
159
		}
160
161
		// Name of field / widget
162
		$output .= '<span class="gv-field-label" data-original-title="'.esc_attr( $label ).'" title="'. $this->get_item_info( false ) .'">'. $label . '</span>';
163
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
164
165
		$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>';
166
167
		// Displays only in the field/widget picker.
168
		if( $field_info = $this->get_item_info() ) {
169
			$output .= '<span class="gv-field-info">'.$field_info.'</span>';
170
		}
171
172
		$output .= '</h5>';
173
174
		$container_class = !empty( $this->item['parent'] ) ? ' gv-child-field' : '';
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
175
176
		$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...
177
178
		return $output;
179
	}
180
181
}
182