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) { |
|
|
|
|
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, |
|
|
|
|
59
|
|
|
'parent_label' => NULL, |
|
|
|
|
60
|
|
|
'label_type' => NULL, |
|
|
|
|
61
|
|
|
'input_type' => NULL, |
|
|
|
|
62
|
|
|
'settings_html' => NULL, |
|
|
|
|
63
|
|
|
'adminLabel' => NULL, |
|
|
|
|
64
|
|
|
'adminOnly' => NULL, |
|
|
|
|
65
|
|
|
'subtitle' => NULL, |
|
|
|
|
66
|
|
|
'placeholder' => NULL, |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
$class = isset( $item['class'] ) ? sanitize_html_class( $item['class'] ) . ' description' : 'description'; |
118
|
|
|
// Add the title in case the value's long, in which case, it'll be truncated by CSS. |
119
|
|
|
$output .= '<span class="' . $class . '">'; |
120
|
|
|
$output .= esc_html( $item['value'] ); |
121
|
|
|
$output .= '</span>'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} else { |
125
|
|
|
|
126
|
|
|
$values = wp_list_pluck( $field_info_items, 'value' ); |
127
|
|
|
|
128
|
|
|
$output = esc_html( implode( ', ', $values ) ); |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return empty( $output ) ? NULL : $output; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Generate HTML for field or a widget modal |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
function getOutput() { |
|
|
|
|
141
|
|
|
|
142
|
|
|
$settings_title = sprintf( __( 'Configure %s Settings', 'gravityview' ), ucfirst( $this->label_type ) ); |
143
|
|
|
$delete_title = sprintf( __( 'Remove %s', 'gravityview' ), ucfirst( $this->label_type ) ); |
144
|
|
|
$single_link_title = __( 'This field links to the Single Entry', 'gravityview' ); |
145
|
|
|
|
146
|
|
|
// $settings_html will just be hidden inputs if empty. Otherwise, it'll have an <ul>. Ugly hack, I know. |
147
|
|
|
// TODO: Un-hack this |
148
|
|
|
$hide_settings_link = ( empty( $this->item['settings_html'] ) || strpos( $this->item['settings_html'], '<!-- No Options -->' ) > 0 ) ? 'hide-if-js' : ''; |
149
|
|
|
$settings_link = sprintf( '<a href="#settings" class="dashicons-admin-generic dashicons %s" title="%s"></a>', $hide_settings_link, esc_attr( $settings_title ) ); |
150
|
|
|
|
151
|
|
|
// Should we show the icon that the field is being used as a link to single entry? |
152
|
|
|
$hide_show_as_link_class = empty( $this->settings['show_as_link'] ) ? 'hide-if-js' : ''; |
153
|
|
|
$show_as_link = '<span class="dashicons dashicons-admin-links ' . $hide_show_as_link_class . '" title="' . esc_attr( $single_link_title ) . '"></span>'; |
154
|
|
|
|
155
|
|
|
// When a field label is empty, use the Field ID |
156
|
|
|
$label = empty( $this->title ) ? sprintf( _x( 'Field #%s (No Label)', 'Label in field picker for empty label', 'gravityview' ), $this->id ) : $this->title; |
157
|
|
|
|
158
|
|
|
// If there's a custom label, and show label is checked, use that as the field heading |
159
|
|
|
if ( ! empty( $this->settings['custom_label'] ) && ! empty( $this->settings['show_label'] ) ) { |
160
|
|
|
$label = $this->settings['custom_label']; |
161
|
|
|
} else if ( ! empty( $this->item['customLabel'] ) ) { |
162
|
|
|
$label = $this->item['customLabel']; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$output = '<h5 class="selectable gfield field-id-' . esc_attr( $this->id ) . '">'; |
166
|
|
|
|
167
|
|
|
$label = esc_attr( $label ); |
168
|
|
|
|
169
|
|
|
if ( ! empty( $this->item['parent'] ) ) { |
170
|
|
|
$label .= ' <small>(' . esc_attr( $this->item['parent']['label'] ) . ')</small>'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// Name of field / widget |
174
|
|
|
$output .= '<span class="gv-field-label" data-original-title="' . esc_attr( $label ) . '" title="' . $this->get_item_info( false ) . '">' . $label . '</span>'; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
177
|
|
|
$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>'; |
178
|
|
|
|
179
|
|
|
// Displays only in the field/widget picker. |
180
|
|
|
if ( $field_info = $this->get_item_info() ) { |
181
|
|
|
$output .= '<span class="gv-field-info">' . $field_info . '</span>'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$output .= '</h5>'; |
185
|
|
|
|
186
|
|
|
$container_class = ! empty( $this->item['parent'] ) ? ' gv-child-field' : ''; |
187
|
|
|
$data_form_id = ! empty( $this->form_id ) ? 'data-formid="' . esc_attr( $this->form_id ) . '"' : ''; |
188
|
|
|
|
189
|
|
|
$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>'; |
190
|
|
|
|
191
|
|
|
return $output; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.