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

admin/class-gravityview-admin-view-item.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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() ) {
38
39
		// Backward compat
40
		if( !empty( $item['type'] ) ) {
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,
49
			'parent_label' => NULL,
50
			'label_type' => NULL,
51
			'input_type' => NULL,
52
			'settings_html' => NULL,
53
			'adminLabel' => NULL,
54
			'adminOnly' => NULL,
55
			'subtitle' => NULL,
56
		));
57
58
		$this->title = $title;
59
		$this->item = $item;
60
		$this->id = $item_id;
61
		$this->settings = $settings;
0 ignored issues
show
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;
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
Expected 1 spaces after opening bracket; 0 found
Loading history...
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
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) );
116
117
		}
118
119
		return empty( $output ) ? NULL : $output;
120
121
	}
122
123
	/**
124
	 * Generate HTML for field or a widget modal
125
	 *
126
	 * @return string
127
	 */
128
	function getOutput() {
129
130
		$settings_title = sprintf(__('Configure %s Settings', 'gravityview'), ucfirst($this->label_type));
131
		$delete_title = sprintf(__('Remove %s', 'gravityview'), ucfirst($this->label_type));
132
		$single_link_title = __('This field links to the Single Entry', 'gravityview');
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' : '';
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;
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'] ) ) {
148
			$label = $this->settings['custom_label'];
149
		} else if( !empty( $this->item['customLabel'] ) ) {
150
			$label = $this->item['customLabel'];
151
		}
152
153
		$output = '<h5 class="selectable gfield field-id-'.esc_attr($this->id).'">';
154
155
		$label = esc_attr( $label );
156
157
		if( !empty( $this->item['parent'] ) ) {
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
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' : '';
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>';
177
178
		return $output;
179
	}
180
181
}
182