/
func.php
497 lines (476 loc) · 16.5 KB
/
func.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
* Some helper functions used by the plugin
*
* @package Book_Reviews
* @author Chris Reynolds <hello@chrisreynolds.io>
* @license GPL-3.0
* @link http://chrisreynolds.io
* @copyright 2014 Chris Reynolds
*/
/**
* Get Review Author
* returns a formatted list of review authors (comma-separated by default)
*
* @since 1.0.0
*
* @param $before string string to display before the author name
* @param $after string string to display after the author name (comma by default)
* @param $forced boolean by default, if the item is the last in the list, the $after variable doesn't render. If $forced is set to TRUE it will bypass this and render it anyway (e.g. if passing $before = '<li>' / $after = '</li>')
* @return $review_author_list sanitized string of the results
*/
function get_review_author($before = null, $after = ', ', $forced = false) {
global $post;
$review_authors = get_the_terms( $post->ID, 'review-author' );
$review_author_list = null;
if ( $review_authors && !is_wp_error( $review_authors ) ) {
$review_author_out = array();
foreach ( $review_authors as $review_author ) {
$review_author_out[] = $review_author->name;
}
$count = 0;
foreach ( $review_author_out as $out ) {
$review_author_list .= $before . $out;
$count++;
if ( ( count($review_author_out) > 1 ) && ( $after == ', ' ) && ( count($review_author_out) != $count ) || $forced ) {
$review_author_list .= $after;
}
}
}
if ( $review_author_list )
return wp_kses_post($review_author_list);
}
/**
* Get genres
* returns a formatted list of genres (comma-separated by default) with links to each
*
* @since 1.0.0
*
* @param $before string string to display before the genre
* @param $after string string to display after the genre (comma by default)
* @param $forced boolean by default, if the item is the last in the list, the $after variable doesn't render. If $forced is set to TRUE it will bypass this and render it anyway (e.g. if passing $before = '<li>' / $after = '</li>')
* @return $genre_list sanitized string of the results
*/
function get_genres($before = null, $after = ', ', $forced = false) {
global $post;
$genres = get_the_terms( $post->ID, 'genre' );
$genre_list = null;
if ( $genres && !is_wp_error( $genres ) ) {
$genre_out = array();
foreach ( $genres as $genre ) {
$genre_out[] = sprintf( '<a href="%s">%s</a>',
home_url() . '/?genre=' . $genre->slug,
$genre->name);
}
$count = 0;
foreach ( $genre_out as $out ) {
$genre_list .= $before . $out;
$count++;
if ( ( count($genre_out) > 1 ) && ( $after == ', ' ) && ( count($genre_out) != $count ) || $forced ) {
$genre_list .= $after;
}
}
}
if ( $genre_list )
return wp_kses_post($genre_list);
}
/**
* Get Book Author
* returns a formatted list of book authors (comma-separated by default) with links to each
*
* @since 1.0.0
*
* @param $before string string to display before the author name
* @param $after string string to display after the author name (comma by default)
* @param $forced boolean by default, if the item is the last in the list, the $after variable doesn't render. If $forced is set to TRUE it will bypass this and render it anyway (e.g. if passing $before = '<li>' / $after = '</li>')
* @param $post_obj object An optional post object to pass instead of the current post
* @return $book_author_list sanitized string of the results
*/
function get_book_author($before = null, $after = ', ', $forced = false, $post_obj = null) {
global $post;
$post = ( $post_obj ) ? $post_obj : $post;
$book_authors = get_the_terms( $post->ID, 'book-author' );
$book_author_list = null;
if ( $book_authors && !is_wp_error( $book_authors ) ) {
$book_author_out = array();
foreach ( $book_authors as $book_author ) {
$book_author_out[] = sprintf( '<a href="%s">%s</a>',
home_url() . '/?book-author=' . $book_author->slug,
$book_author->name);
}
$count = 0;
foreach ( $book_author_out as $out ) {
$book_author_list .= $before . $out;
$count++;
if ( ( count($book_author_out) > 1 ) && ( $after == ', ' ) && ( count($book_author_out) != $count ) || $forced ) {
$book_author_list .= $after;
}
}
if ( $book_author_list )
return wp_kses_post($book_author_list);
}
}
/**
* Get reading level
* returns a formatted list of reading levels (comma-separated by default) with links to each
*
* @since 1.0.0
*
* @param $before string string to display before the reading level
* @param $after string string to display after the reading level (comma by default)
* @param $forced boolean by default, if the item is the last in the list, the $after variable doesn't render. If $forced is set to TRUE it will bypass this and render it anyway (e.g. if passing $before = '<li>' / $after = '</li>')
* @return $reading_level_list sanitized string of the results
*/
function get_reading_level($before = null, $after = ', ', $forced = false) {
global $post;
$reading_levels = get_the_terms( $post->ID, 'reading-level' );
$reading_level_list = null;
if ( $reading_levels && !is_wp_error( $reading_levels ) ) {
$reading_level_out = array();
foreach ( $reading_levels as $reading_level ) {
$reading_level_out[] = sprintf( '<a href="%s">%s</a>',
home_url() . '/?reading-level=' . $reading_level->slug,
$reading_level->name);
}
$count = 0;
foreach ( $reading_level_out as $out ) {
$reading_level_list .= $before . $out;
$count++;
if ( ( count($reading_level_out) > 1 ) && ( $after == ', ' ) && ( count($reading_level_out) != $count ) || $forced ) {
$reading_level_list .= $after;
}
}
}
if ( $reading_level_list )
return wp_kses_post($reading_level_list);
}
/**
* Get subjects
* returns a formatted list of subjects (comma-separated by default) with links to each
*
* @since 1.0.0
*
* @param $before string string to display before the subject
* @param $after string string to display after the subject (comma by default)
* @param $forced boolean by default, if the item is the last in the list, the $after variable doesn't render. If $forced is set to TRUE it will bypass this and render it anyway (e.g. if passing $before = '<li>' / $after = '</li>')
* @return $subject_list sanitized string of the results
*/
function get_subjects($before = null, $after = ', ', $forced = false) {
global $post;
$subjects = get_the_terms( $post->ID, 'subject' );
$subject_list = null;
if ( $subjects && !is_wp_error( $subjects ) ) {
$subject_out = array();
foreach ( $subjects as $subject ) {
$subject_out[] = sprintf( '<a href="%s">%s</a>',
home_url() . '/?subject=' . $subject->slug,
$subject->name);
}
$count = 0;
foreach ( $subject_out as $out ) {
$subject_list .= $before . $out;
$count++;
if ( ( count($subject_out) > 1 ) && ( $after == ', ' ) && ( count($subject_out) != $count ) || $forced ) {
$subject_list .= $after;
}
}
}
if ( $subject_list )
return wp_kses_post($subject_list);
}
/**
* Get rating stars
* returns a genericon star icon for each star value in the rating taxonomy
*
* @since 1.0.0
*
* @return $rating_out sanitized string of the results
*/
function get_rating_stars() {
global $post;
$ratings = get_the_terms( $post->ID, 'rating' );
if ( $ratings && !is_wp_error( $ratings ) ) {
$rating_out = null;
foreach ( $ratings as $rating ) {
if ( $rating->name == '5' ) {
$stars = $rating->name;
$rating_out = '<div class="genericon genericon-star"></div><div class="genericon genericon-star"></div><div class="genericon genericon-star"></div><div class="genericon genericon-star"></div><div class="genericon genericon-star"></div>';
}
elseif ( $rating->name == '4' ) {
$stars = $rating->name;
$rating_out = '<div class="genericon genericon-star"></div><div class="genericon genericon-star"></div><div class="genericon genericon-star"></div><div class="genericon genericon-star"></div>';
}
elseif ( $rating->name == '3' ) {
$stars = $rating->name;
$rating_out = '<div class="genericon genericon-star"></div><div class="genericon genericon-star"></div><div class="genericon genericon-star"></div>';
}
elseif ( $rating->name == '2' ) {
$stars = $rating->name;
$rating_out = '<div class="genericon genericon-star"></div><div class="genericon genericon-star"></div>';
}
elseif ( $rating->name == '1' ) {
$stars = $rating->name;
$rating_out = '<div class="genericon genericon-star"></div>';
}
elseif ( $rating->name == '0' ) {
$stars = $rating->name;
$rating_out = __( 'Zero stars', 'book-review-library' );
}
}
}
if ( $rating_out )
return wp_kses_post($rating_out);
}
/**
* Get rating
* returns a comma-separated list of ratings (if more than one rating is applied to a review, for some reason)
*
* @since 1.0.0
*
* @return $stars sanitized string of the results
*/
function get_rating($before = null, $after = ', ', $forced = false) {
global $post;
$ratings = get_the_terms( $post->ID, 'rating' );
if ( $ratings && !is_wp_error( $ratings ) ) {
$stars_out = array();
foreach ( $ratings as $rating ) {
if ( $rating->name == '0' ) {
// if rating is 0, get_rating() will return false
// returning "zero" means we can do a check on the result, and deal with the result on the display side
$stars_out[] = "zero";
} else {
$stars_out[] = $rating->name;
}
}
$stars = join( ', ', $stars_out); // join the stars list if for some reason someone has used more than one
}
if ( $stars )
return esc_attr($stars);
}
/**
* Get illustrator
* returns a formatted list of illustrators (comma-separated by default) with links to each
*
* @since 1.0.0
*
* @param $before string string to display before the illustrator name
* @param $after string string to display after the illustrator name (comma by default)
* @param $forced boolean by default, if the item is the last in the list, the $after variable doesn't render. If $forced is set to TRUE it will bypass this and render it anyway (e.g. if passing $before = '<li>' / $after = '</li>')
* @return $illustrator_list sanitized string of the results
*/
function get_illustrator($before = null, $after = ', ', $forced = false) {
global $post;
$illustrators = get_the_terms( $post->ID, 'illustrator' );
$illustrator_list = null;
if ( $illustrators && !is_wp_error( $illustrators ) ) {
$illustrator_out = array();
foreach ( $illustrators as $illustrator ) {
$illustrator_out[] = sprintf( '<a href="%s">%s</a>',
home_url() . '/?illustrator=' . $illustrator->slug,
$illustrator->name);
}
$count = 0;
foreach ( $illustrator_out as $out ) {
$illustrator_list .= $before . $out;
$count++;
if ( ( count($illustrator_out) > 1 ) && ( $after == ', ' ) && ( count($illustrator_out) != $count ) || $forced ) {
$illustrator_list .= $after;
}
}
}
if ( $illustrator_list )
return wp_kses_post($illustrator_list);
}
/**
* Get awards
* returns a formatted list of awards (comma-separated by default) with links to each
*
* @since 1.0.0
*
* @param $before string string to display before the award name
* @param $after string string to display after the award name (comma by default)
* @param $forced boolean by default, if the item is the last in the list, the $after variable doesn't render. If $forced is set to TRUE it will bypass this and render it anyway (e.g. if passing $before = '<li>' / $after = '</li>')
* @return $award_list sanitized string of the results
*/
function get_awards($before = null, $after = ', ', $forced = false) {
global $post;
$awards = get_the_terms( $post->ID, 'awards' );
$award_list = null;
if ( $awards && !is_wp_error( $awards ) ) {
$award_out = array();
foreach ( $awards as $award ) {
$award_out[] = sprintf( '<a href="%s">%s</a>',
home_url() . '/?awards=' . $award->slug,
$award->name);
}
$count = 0;
foreach ( $award_out as $out ) {
$award_list .= $before . $out;
$count++;
if ( ( count($award_out) > 1 ) && ( $after == ', ' ) && ( count($award_out) != $count ) || $forced ) {
$award_list .= $after;
}
}
}
if ( $award_list )
return wp_kses_post($award_list);
}
/**
* Get series
* returns a formatted list of series (comma-separated by default) with links to each
*
* @since 1.0.0
*
* @param $before string string to display before the series name
* @param $after string string to display after the series name (comma by default)
* @param $forced boolean by default, if the item is the last in the list, the $after variable doesn't render. If $forced is set to TRUE it will bypass this and render it anyway (e.g. if passing $before = '<li>' / $after = '</li>')
* @return $series_list sanitized string of the results
*/
function get_book_series($before = null, $after = ', ', $forced = false) {
global $post;
$seriess = get_the_terms( $post->ID, 'series' );
$series_list = null;
if ( $seriess && !is_wp_error( $seriess ) ) {
$illustrator_out = array();
foreach ( $seriess as $series ) {
$series_out[] = sprintf( '<a href="%s">%s</a>',
home_url() . '/?series=' . $series->slug,
$series->name);
}
$count = 0;
foreach ( $series_out as $out ) {
$series_list .= $before . $out;
$count++;
if ( ( count($series_out) > 1 ) && ( $after == ', ' ) && ( count($series_out) != $count ) || $forced ) {
$series_list .= $after;
}
}
}
if ( $series_list )
return wp_kses_post($series_list);
}
/**
* Select box
* returns a select box based on array values passed to it, used by the widget
*
* @since 1.0.0
*
* @param $name string select box value name
* @param $values array an array of possible values
* @param $default array an array of default values
* @param $parameters string any additional parameters
*
* @return $field the final select box
*/
if ( !function_exists('the_select_box') ) {
function the_select_box($name, $values, $default = '', $parameters = '') {
$field = '<select name="' . esc_attr($name) . '"';
if ( !is_null($parameters))
$field .= ' ' . $parameters;
$field .= '>';
if (empty($default) && isset($GLOBALS[$name]))
$default = stripslashes($GLOBALS[$name]);
for ($i = 0, $n = sizeof($values); $i < $n; $i++) {
$field .= '<option value="' . $values[$i]['id'] . '"';
if ($default == $values[$i]['id']) {
$field .= 'selected = "selected"';
}
$field .= '>' . $values[$i]['text'] . '</option>';
}
$field .= '</select>';
return $field;
}
}
/**
* Enabled/Disabled toggle
* used by the options page
*
* @since 1.0.0
*
* @return $result the array of possible values
*/
function book_reviews_true_false() {
$result = array(
'true' => array(
'value' => true,
'label' => __( 'Enabled', 'book-review-library' )
),
'false' => array(
'value' => false,
'label' => __( 'Disabled', 'book-review-library' )
)
);
return $result;
}
/**
* Book Review Title Filter
* used by the options page
* this just lists the possible options for the title filter setting
*
* @since 1.4.7
*
* @return $result the array of possible values
*/
function book_reviews_title_filter() {
$result = array(
'title' => array(
'value' => 'title',
'label' => __( 'With the title', 'book-review-library' )
),
'newline' => array(
'value' => 'newline',
'label' => __( 'On a new line', 'book-review-library' )
),
'disabled' => array(
'value' => 'disabled',
'label' => __( 'Disabled', 'book-review-library' )
)
);
return $result;
}
/**
* Book Review Thumbnail Option
* used by the options page
* allows the user to specify whether they want to use internal image size or post thumbnail image size
* solves some problems when the theme is taking over the thumbnail size
*
* @since 1.4.11
*/
function book_reviews_image_size() {
$result = array(
'thumbnail' => array(
'value' => 'thumbnail',
'label' => __( 'Use the post thumbnail size', 'book-review-library' )
),
'book-cover' => array(
'value' => 'book-cover',
'label' => __( 'Use 6:9 book cover size (133px x 200px)', 'book-review-library' )
)
);
return $result;
}
/**
* Default option settings
*
* @since 1.0.0
*
* @return $defaults all the default settings (everything disabled)
*/
function book_reviews_option_defaults() {
$defaults = array(
'review-author' => false,
'reading-level' => false,
'subject' => false,
'illustrator' => false,
'awards' => false,
'series' => false,
'rating' => false,
'stock' => false,
'roles' => false,
'title-filter' => true,
'comments' => false,
'thumbnail' => 'thumbnail'
);
return $defaults;
}