-
Notifications
You must be signed in to change notification settings - Fork 37
/
StarRatingField.php
363 lines (327 loc) · 10.8 KB
/
StarRatingField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
class StarRatingField extends acf_field
{
/**
* __construct
*
* This function will setup the field type data
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @return void
*/
public function __construct()
{
$this->name = 'star_rating_field';
$this->label = __('Star Rating', 'acf-star_rating_field');
$this->category = 'content';
$this->defaults = array(
'max_stars' => 5,
);
$this->l10n = array(
'error' => __('Error! Please enter a higher value', 'acf-star_rating_field'),
);
parent::__construct();
}
/**
* render_field_settings()
*
* Create extra settings for your field. These are visible when editing a field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field (array) the $field being edited
*
* @return void
*/
public function render_field_settings($field)
{
acf_render_field_setting($field, array(
'label' => __('Maximum Rating', 'acf-star_rating_field'),
'instructions' => __('Maximum number of stars', 'acf-star_rating_field'),
'type' => 'number',
'name' => 'max_stars'
));
acf_render_field_setting($field, array(
'label' => __('Return Type', 'acf-star_rating_field'),
'instructions' => __('What should be returned?', 'acf-star_rating_field'),
'type' => 'select',
'layout' => 'horizontal',
'choices' => array(
'0' => __('Number', 'num'),
'1' => __('List (unstyled)', 'list_u'),
'2' => __('List (fa-awesome)', 'list_fa'),
),
'name' => 'return_type'
));
acf_render_field_setting($field, array(
'label' => __('Allow Half Rating', 'acf-star_rating_field'),
'instructions' => __('Allow half ratings?', 'acf-star_rating_field'),
'type' => 'true_false',
'name' => 'allow_half'
));
acf_render_field_setting($field, array(
'label' => __('Theme', 'acf-star_rating_field'),
'instructions' => __('Select theme?', 'acf-star_rating_field'),
'type' => 'select',
'choices' => array(
'default' => __('Default', 'default'),
'color' => __('Color', 'color'),
),
'name' => 'theme'
));
}
/**
* render_field()
*
* Create the HTML interface for your field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param array $field the $field being rendered
*
* @return string
*/
public function render_field($field)
{
$html = '
<div class="field_type-star_rating_field theme-' . $field['theme'] . '">%s</div>
<a href="#clear-stars" class="btn-star-clear clear-button">%s</a>
<input type="hidden" id="star-rating" data-allow-half="%s" name="%s" value="%s">
';
$starClasses = apply_filters(
'star_rating_field_admin_star_classes',
array('fa fa-star-o', 'fa fa-star-half-o', 'fa fa-star')
);
if (count($starClasses) !== 3) {
return "Error: 3 classes are required to display rating field: blank class, half class and full class.";
}
printf(
'<script>window.starClasses = %s</script>',
json_encode($starClasses)
);
print sprintf(
$html,
$this->make_list(
$field['max_stars'],
$field['value'],
'<li><i class="%s star-%d"></i></li>',
$starClasses,
$field['allow_half']
),
__('Clear', 'acf-star_rating_field'),
$field['allow_half'],
esc_attr($field['name']),
esc_attr($field['value'])
);
}
/**
* input_admin_enqueue_scripts()
*
* This action is called in the admin_enqueue_scripts action on the edit screen where your field is created.
* Use this action to add CSS + JavaScript to assist your render_field() action.
*
* @type action (admin_enqueue_scripts)
* @since 3.6
* @date 23/01/13
*
* @return void
*/
public function input_admin_enqueue_scripts()
{
$dir = plugin_dir_url(__FILE__);
//https://github.com/jonpxpx/acf-star-rating-field/blob/66734ccb957e4dd41f858322a10c1ea6f52df503/StarRatingField.php#L157
//https://github.com/kevinruscoe/acf-star-rating-field/issues/55#issuecomment-2023004441
if( get_post_type() !== 'acf-field-group' ){
wp_enqueue_script('acf-input-star_rating', "{$dir}js/input.js");
}
wp_enqueue_style(
'font-awesome',
"//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css"
);
wp_enqueue_style('acf-input-star_rating', "{$dir}css/input.css");
}
/**
* load_value()
*
* This filter is applied to the $value after it is loaded from the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param mixed $value the value found in the database
* @param mixed $post_id the $post_id from which the value was loaded
* @param array $field the field array holding all the field options
*
* @return float $value
*/
public function load_value($value, $post_id, $field)
{
return floatval($value);
}
/**
* update_value()
*
* This filter is applied to the $value before it is saved in the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param mixed $value the value found in the database
* @param mixed $post_id the $post_id from which the value was loaded
* @param array $field the field array holding all the field options
* @return float $value
*/
public function update_value($value, $post_id, $field)
{
return floatval($value);
}
/**
* format_value()
*
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param mixed $value the value which was loaded from the database
* @param mixed $post_id the $post_id from which the value was loaded
* @param array $field the field array holding all the field options
*
* @return mixed $value the modified value
*/
public function format_value($value, $post_id, $field)
{
if (empty($value)) {
return $value;
}
switch ($field['return_type']) {
case 0:
return floatval($value);
break;
case 1:
return $this->make_list(
$field['max_stars'],
$value,
'<li class="%s">%d</li>',
array('blank', 'half', 'full'),
$field['allow_half']
);
break;
case 2:
$dir = plugin_dir_url(__FILE__);
wp_enqueue_style('acf-input-star_rating', "{$dir}css/input.css");
wp_enqueue_style(
'font-awesome',
"//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css"
);
$html = '<div class="field_type-star_rating_field">%s</div>';
return sprintf(
$html,
$this->make_list(
$field['max_stars'],
$value,
'<li><i class="%s star-%d"></i></li>',
array('fa fa-star-o', 'fa fa-star-half-o', 'fa fa-star'),
$field['allow_half']
)
);
break;
}
}
/**
* validate_value()
*
* This filter is used to perform validation on the value prior to saving.
* All values are validated regardless of the field's required setting. This allows you to validate and return
* messages to the user if the value is not correct
*
* @type filter
* @date 11/02/2014
* @since 5.0.0
*
* @param bool $valid validation status based on the value and the field's required setting
* @param mixed $value the $_POST value
* @param array $field the field array holding all the field options
* @param string $input the corresponding input name for $_POST value
* @return string $valid
*/
public function validate_value($valid, $value, $field, $input)
{
if ($value > $field['max_stars']) {
$valid = __('The value is too large!', 'acf-star_rating_field');
}
if ( $field['required'] == 1 && $value == 0 ) {
$valid = sprintf( __('Please enter a value clicking on stars form 1 to %s', 'acf-star_rating_field'), $field['max_stars']);
}
return $valid;
}
/**
* load_field()
*
* This filter is applied to the $field after it is loaded from the database
*
* @type filter
* @date 23/01/2013
* @since 3.6.0
*
* @param array $field the field array holding all the field options
* @return array $field
*/
public function load_field($field)
{
return $field;
}
/**
* update_field()
*
* This filter is applied to the $field before it is saved to the database
*
* @type filter
* @date 23/01/2013
* @since 3.6.0
*
* @param array $field the field array holding all the field options
* @return array $field
*/
public function update_field($field)
{
return $field;
}
/**
* make_list()
*
* Create a HTML list
*
* @param int $maxStars
* @param int $currentStart
* @param string $li
* @param array $classes
* @return string $html
*/
public function make_list($maxStars = 5, $currentStar = 0, $li = '', $classes = [], $allowHalf = false)
{
$html = '<ul class="star-rating">';
for ($index = 1; $index <= $maxStars; $index++) {
$class = $classes[0];
if ($index <= $currentStar) {
$class = $classes[2];
}
if ($allowHalf && ($index - .5 == $currentStar)) {
$class = $classes[1];
}
$html .= sprintf($li, $class, $index);
}
$html .= "</ul>";
return $html;
}
}