|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class for managing tooltips |
|
5
|
|
|
* |
|
6
|
|
|
* @package Give |
|
7
|
|
|
* @subpackage Classes/Give_Tooltips |
|
8
|
|
|
* @copyright Copyright (c) 2017, WordImpress |
|
9
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
10
|
|
|
* @since 2.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class Give_Tooltips { |
|
13
|
|
|
/** |
|
14
|
|
|
* Set tooltip arguments. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 2.0 |
|
17
|
|
|
* @access private |
|
18
|
|
|
* |
|
19
|
|
|
* @param $args |
|
20
|
|
|
* |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
private function set_toottip_args( $args ) { |
|
24
|
|
|
$args = wp_parse_args( |
|
25
|
|
|
$args, |
|
26
|
|
|
array( |
|
27
|
|
|
// Tooltip tag. |
|
28
|
|
|
'tag' => 'span', |
|
29
|
|
|
'tag_content' => '', |
|
30
|
|
|
|
|
31
|
|
|
// Set to link of anchor if tooltip tag is anchor. |
|
32
|
|
|
'link' => '#', |
|
33
|
|
|
|
|
34
|
|
|
// Text for tooltip |
|
35
|
|
|
'label' => '', |
|
36
|
|
|
|
|
37
|
|
|
// Value: top-right, top, top-left, right, left, bottom-right, bottom, bottom-left. |
|
38
|
|
|
'position' => 'top', |
|
39
|
|
|
|
|
40
|
|
|
// Value: error, warning, info, success. |
|
41
|
|
|
'status' => '', |
|
42
|
|
|
|
|
43
|
|
|
// Value: small, medium, large. |
|
44
|
|
|
'size' => '', |
|
45
|
|
|
|
|
46
|
|
|
// Value: true/false. |
|
47
|
|
|
'show_always' => false, |
|
48
|
|
|
|
|
49
|
|
|
// Value: true/false |
|
50
|
|
|
'round_edges' => false, |
|
51
|
|
|
|
|
52
|
|
|
// Value: true/false |
|
53
|
|
|
'animate' => true, |
|
54
|
|
|
|
|
55
|
|
|
// Attributes. |
|
56
|
|
|
'attributes' => array(), |
|
57
|
|
|
|
|
58
|
|
|
// Value: true/false |
|
59
|
|
|
'auto_width' => true, |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
// Auto set width of tooltip. |
|
64
|
|
|
if ( |
|
65
|
|
|
! empty( $args['auto_width'] ) && |
|
66
|
|
|
! empty( $args['label'] ) && |
|
67
|
|
|
empty( $args['size'] ) |
|
68
|
|
|
) { |
|
69
|
|
|
if ( 15 < str_word_count( $args['label'] ) ) { |
|
70
|
|
|
$args['size'] = 'large'; |
|
71
|
|
|
} elseif ( 7 < str_word_count( $args['label'] ) ) { |
|
72
|
|
|
$args['size'] = 'medium'; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $args; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Render tooltip |
|
82
|
|
|
* |
|
83
|
|
|
* @since 2.0 |
|
84
|
|
|
* @access public |
|
85
|
|
|
* |
|
86
|
|
|
* @param $args |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function render( $args ) { |
|
91
|
|
|
$args = $this->set_toottip_args( $args ); |
|
92
|
|
|
|
|
93
|
|
|
$tooltip_pos = array( |
|
94
|
|
|
'top' => 'hint--top', |
|
95
|
|
|
'top-right' => 'hint--top-right', |
|
96
|
|
|
'top-left' => 'hint--top-left', |
|
97
|
|
|
'right' => 'hint--right', |
|
98
|
|
|
'left' => 'hint--left', |
|
99
|
|
|
'bottom' => 'hint--bottom', |
|
100
|
|
|
'bottom-right' => 'hint--bottom-right', |
|
101
|
|
|
'bottom-left' => 'hint--bottom-left', |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
$tooltip_status = array( |
|
105
|
|
|
'error' => 'hint--error', |
|
106
|
|
|
'warning' => 'hint--warning', |
|
107
|
|
|
'info' => 'hint--info', |
|
108
|
|
|
'success' => 'hint--success', |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$tooltip_size = array( |
|
112
|
|
|
'small' => 'hint--small', |
|
113
|
|
|
'medium' => 'hint--medium', |
|
114
|
|
|
'large' => 'hint--large', |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
// Set label. |
|
118
|
|
|
$args['attributes']['aria-label'] = $args['label']; |
|
119
|
|
|
|
|
120
|
|
|
// Set classes. |
|
121
|
|
|
$args['attributes']['class'] = ! empty( $args['attributes']['class'] ) ? $args['attributes']['class'] : ''; |
|
122
|
|
|
$args['attributes']['class'] .= " {$tooltip_pos[ $args['position'] ]}"; |
|
123
|
|
|
$args['attributes']['class'] .= ! empty( $args['status'] ) ? " {$tooltip_status[ $args['status'] ]}" : ''; |
|
124
|
|
|
$args['attributes']['class'] .= ! empty( $args['size'] ) ? " {$tooltip_size[ $args['size'] ]}" : ''; |
|
125
|
|
|
$args['attributes']['class'] .= $args['show_always'] ? ' hint--always' : ''; |
|
126
|
|
|
$args['attributes']['class'] .= $args['round_edges'] ? ' hint--rounded' : ''; |
|
127
|
|
|
$args['attributes']['class'] .= $args['animate'] ? ' hint--bounce' : ' hint--no-animate'; |
|
128
|
|
|
$args['attributes']['class'] = trim( $args['attributes']['class'] ); |
|
129
|
|
|
|
|
130
|
|
|
// Set link attribute in tooltip has anchor tag. |
|
131
|
|
|
if ( 'a' === $args['tag'] && ! empty( $args['link'] ) ) { |
|
132
|
|
|
$args['attributes']['href'] = esc_url( $args['link'] ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return sprintf( '<%1$s %2$s rel="tooltip">%3$s</%1$s>', $args['tag'], give_get_attribute_str( $args['attributes'] ), $args['tag_content'] ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Render tooltip with anchor tag |
|
141
|
|
|
* |
|
142
|
|
|
* @since 2.0 |
|
143
|
|
|
* @access public |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $args |
|
146
|
|
|
* |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
function render_link( $args ) { |
|
|
|
|
|
|
150
|
|
|
$args['tag'] = 'a'; |
|
151
|
|
|
$tooltip_markup = $this->render( $args ); |
|
152
|
|
|
|
|
153
|
|
|
return $tooltip_markup; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Render tooltip with span tag |
|
158
|
|
|
* |
|
159
|
|
|
* @since 2.0 |
|
160
|
|
|
* @access public |
|
161
|
|
|
* |
|
162
|
|
|
* @param array|string $args |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
function render_span( $args ) { |
|
|
|
|
|
|
167
|
|
|
// Set tooltip args from string. |
|
168
|
|
|
if ( is_string( $args ) ) { |
|
169
|
|
|
$args = array( 'label' => $args ); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$args['tag'] = 'span'; |
|
173
|
|
|
$tooltip_markup = $this->render( $args ); |
|
174
|
|
|
|
|
175
|
|
|
return $tooltip_markup; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Render tooltip with span tag and question mark icon |
|
180
|
|
|
* |
|
181
|
|
|
* @since 2.0 |
|
182
|
|
|
* @access public |
|
183
|
|
|
* |
|
184
|
|
|
* @param array|string $args |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
|
function render_help( $args ) { |
|
|
|
|
|
|
189
|
|
|
// Set tooltip args from string. |
|
190
|
|
|
if ( is_string( $args ) ) { |
|
191
|
|
|
$args = array( 'label' => $args ); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$args['tag_content'] = '<i class="give-icon give-icon-question"></i>'; |
|
195
|
|
|
$args['attributes']['class'] = 'give-tooltip'; |
|
196
|
|
|
$tooltip_markup = $this->render_span( $args ); |
|
197
|
|
|
|
|
198
|
|
|
return $tooltip_markup; |
|
199
|
|
|
} |
|
200
|
|
|
} |
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.