-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemplate-functions.php
1081 lines (926 loc) · 27.7 KB
/
template-functions.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Functions for theming and templating.
* @package now-reading
*/
/**
* The array index of the current book in the {@link $books} array.
* @global integer $GLOBALS['current_book']
* @name $current_book
*/
$current_book = 0;
/**
* The array of books for the current query.
* @global array $GLOBALS['books']
* @name $books
*/
$books = null;
/**
* The current book in the loop.
* @global object $GLOBALS['book']
* @name $book
*/
$book = null;
/**
* Formats a date according to the date format option.
* @param string The date to format, in any string recogniseable by strtotime.
*/
function nr_format_date( $date ) {
$options = get_option(NOW_READING_OPTIONS);
if ( !is_numeric($date) )
$date = strtotime($date);
if ( empty($date) )
return '';
return apply_filters('nr_format_date', date($options['formatDate'], $date));
}
/**
* Returns true if the date is a valid one; false if it isn't.
* @param string The date to check.
*/
function nr_empty_date( $date ) {
return ( empty($date) || $date == "0000-00-00 00:00:00" );
}
/*
* Returns an html-entity escaped string of $value (if not empty) or
* of the translation of $default (if value is empty).
*/
function text_or_default($value, $default)
{
$text = $value;
if (empty($text))
{
$text = __($default, NRTD);
}
return htmlentities($text, ENT_QUOTES, "UTF-8");
}
/**
* Prints the book's title.
* @param bool $echo Whether or not to echo the results.
*/
function book_title( $echo = true ) {
global $book;
$title = stripslashes(apply_filters('book_title', $book->title));
if ( $echo )
echo $title;
return $title;
}
/**
* Prints the book's reader.
* @param bool $echo Wether or not to echo the results.
*/
function book_reader( $echo=true ) {
global $book;
$user_info = get_userdata($book->reader);
if ( $echo )
echo $user_info->display_name;
return $user_info->display_name;
}
/**
* Prints the user name
* @param int $reader_id Wordpress ID of the reader. If 0, prints the current user name.
*/
function print_reader( $echo=true, $reader_id = 0) {
global $userdata;
$username='';
if (!$reader_id) {
get_currentuserinfo();
$username = $userdata->user_login;
} else {
$user_info = get_userdata($reader_id);
$username = $user_info->user_login;
}
if ($echo)
echo $username;
return $username;
}
/**
* Prints the author of the book.
* @param bool $echo Whether or not to echo the results.
*/
function book_author( $echo = true ) {
global $book;
$author = apply_filters('book_author', $book->author);
if ( $echo )
echo $author;
return $author;
}
/**
* Prints a URL to the book's image, usually used within an HTML img element.
* @param bool $echo Whether or not to echo the results.
*/
function book_image( $echo = true ) {
global $book;
$image = apply_filters('book_image', $book->image);
if ( $echo )
echo $image;
return $image;
}
/**
* Prints the date when the book was added to the database.
* @param bool $echo Whether or not to echo the results.
*/
function book_added( $echo = true ) {
global $book;
$added = apply_filters('book_added', $book->added);
if ( $echo )
echo $added;
return $added;
}
/**
* Prints the date when the book's status was changed from unread to reading.
* @param bool $echo Whether or not to echo the results.
*/
function book_started( $echo = true ) {
global $book;
if ( nr_empty_date($book->started) )
$started = __('Not yet started.', NRTD);
else
$started = apply_filters('book_started', $book->started);
if ( $echo )
echo $started;
return $started;
}
/**
* Prints the date when the book's status was changed from reading to read.
* @param bool $echo Whether or not to echo the results.
*/
function book_finished( $echo = true ) {
global $book;
if ( nr_empty_date($book->finished) )
$finished = __('Not yet finished.', NRTD);
else
$finished = apply_filters('book_finished', $book->finished);
if ( $echo )
echo $finished;
return $finished;
}
/**
* Prints the current book's status with optional overrides for messages.
* @param bool $echo Whether or not to echo the results.
*/
function book_status( $echo = true, $unread = '', $reading = '', $read = '', $onhold = '' ) {
global $book, $nr_statuses;
if ( empty($unread) )
$unread = $nr_statuses['unread'];
if ( empty($reading) )
$reading = $nr_statuses['reading'];
if ( empty($read) )
$read = $nr_statuses['read'];
if ( empty($onhold) )
$onhold = $nr_statuses['onhold'];
switch ( $book->status ) {
case 'unread':
$text = $unread;
break;
case 'onhold':
$text = $onhold;
break;
case 'reading':
$text = $reading;
break;
case 'read':
$text = $read;
break;
default:
return;
}
if ( $echo )
echo $text;
return $text;
}
/**
* Prints the number of books started and finished within a given time period.
* @param string $interval The time interval, eg "1 year", "3 month"
* @param bool $echo Whether or not to echo the results.
*/
function books_read_since($interval, $echo = true)
{
global $wpdb;
$interval = $wpdb->escape($interval);
$num = $wpdb->get_var("
SELECT
COUNT(*) AS count
FROM
{$wpdb->prefix}now_reading
WHERE
b_finished >= DATE_SUB(CURDATE(), INTERVAL {$interval})
");
if ($echo)
{
echo "$num book" . ($num != 1 ? 's' : '');
}
return $num;
}
/**
* Prints book reading statistics.
* @param string $time_period The period to measure average over, eg "year", "month".
*/
function print_book_average($duration_months = 12, $time_period = 'year')
{
echo __("There are ");
total_books(0);
echo __(" listed, of which ");
books_read_since("{$duration_months} MONTH");
echo __(" have been read in the last year, ");
books_read_since('1 MONTH');
echo __(" read in the last month. That's ");
average_books($time_period, true, false);
echo ".";
}
/**
* Prints book reading statistics per month.
* @param int $duration_months The duration of the stats in number of months.
* @param int $bar_width The width of the graph bars. Overriden by graph_width, if specified.
* @param int $graph_height The height of the graph in pixels.
* @param int $graph_width The width of the graph in pixels. <0 to calculate based on duration_months * default bar width.
* @param bool $print_average Whether or not to print average books read per year.
* @param bool $only_finished True to calculate the finished books per month, otherwise, the number of being read during the month.
*/
function print_book_stats($duration_months = 12, $bar_width = 40, $graph_height = 150, $graph_width = -1, $div_id = 'book_stats_graph', $print_average = true, $only_finished = true)
{
global $wpdb;
if ($graph_width < 0)
{
$graph_width = $bar_width * $duration_months;
}
// Print the statistics per month.
echo "
<div class='graph-caption' style='margin: auto; text-align: center'><i>";
printf(__("Number of Books read during the past %d months.", NRTD), $duration_months);
echo "</i></div>
";
echo '
<link rel="stylesheet" href="' . plugins_url('/css/tufte-graph.css', __FILE__) . '" type="text/css" />
<script type="text/javascript" src="' . plugins_url('/js/jquery.enumerable.js', __FILE__) .'"></script>
<script type="text/javascript" src="' . plugins_url('/js/jquery.tufte-graph.js', __FILE__) .'"></script>
<script type="text/javascript" src="' . plugins_url('/js/raphael-min.js', __FILE__) .'"></script>
<script type="text/javascript">
jQuery(document).ready(function () {';
echo "
jQuery('#{$div_id}').tufteBar({
data: [";
if ($only_finished)
{
// Calculate number of books finished per month for $duration_months.
// Query the number of books finished during each month during the past {$duration_months} months.
$query = "
SELECT YEAR(b_finished) AS Year, MONTH(b_finished) AS Month, COUNT(*) AS Count
FROM {$wpdb->prefix}now_reading
WHERE b_started > 0
AND b_finished > DATE_SUB(CURDATE(), INTERVAL {$duration_months} MONTH)
AND b_status != 'onhold'
GROUP BY YEAR(b_finished), MONTH(b_finished)
ORDER BY YEAR(b_finished), MONTH(b_finished)
";
$statistics = $wpdb->get_results($query);
$statistics = apply_filters('get_books', $statistics);
// For each year/month, create an entry of the total finished count.
// Entry format: [0, {label: '2005'}, {barLabel: '0 Books'}],
foreach ((array)$statistics as $stat)
{
$label = date("M", mktime(0, 0, 0, $stat->Month, 1, 2000)) . ' ' . $stat->Year;
echo "
[{$stat->Count}, {label: '" . $label . "'}, {barLabel: '{$stat->Count}'}],";
}
}
else
{
// Calculate number of books being read per month for $duration_months.
// For each year/month, create an entry of the total books being read during it.
// Entry format: [0, {label: '2005'}, {barLabel: '0 Books'}],
for ($i = $duration_months; $i >= 0; --$i)
{
$query = "
SELECT YEAR(DATE_SUB(CURDATE(), INTERVAL {$i} MONTH)) AS Year, MONTH(DATE_SUB(CURDATE(), INTERVAL {$i} MONTH)) AS Month, COUNT(*) AS Count
FROM {$wpdb->prefix}now_reading
WHERE
b_started <= DATE_SUB(CURDATE(), INTERVAL {$i} MONTH)
AND (b_finished >= DATE_SUB(CURDATE(), INTERVAL {$i} MONTH) OR b_status = 'reading')
AND b_status != 'onhold'
";
$statistics = $wpdb->get_results($query);
$statistics = apply_filters('get_books', $statistics);
foreach ((array)$statistics as $stat)
{
$label = date("M", mktime(0, 0, 0, $stat->Month, 1, 2000)) . ' ' . $stat->Year;
echo "
[{$stat->Count}, {label: '" . $label . "'}, {barLabel: '{$stat->Count}'}],";
}
}
}
echo "
],
barLabel: function(index) {
return this[2].barLabel;
},
axisLabel: function(index) { return this[1].label },
});
});
</script>
<div id='{$div_id}' class='graph' style='width: {$graph_width}px; height: {$graph_height}px;'></div>
";
if ($print_average)
{
echo "
<div class='graph-caption' style='margin: auto; text-align: center; padding: 10px;'>";
print_book_average($duration_months);
echo "</div>
";
}
}
/**
* Prints the total number of books in the library.
* @param string $status A comma-separated list of statuses to include in the count. If ommitted, all statuses will be counted.
* @param bool $echo Whether or not to echo the results.
* @param int $userID Counting only userID's books.
*/
function total_books($status = '', $echo = true , $userID = 0)
{
global $wpdb;
$reader = get_reader_visibility_filter($userID, false);
if ($status)
{
if (strpos($status, ',') === false)
{
$status = 'WHERE b_status = "' . $wpdb->escape($status) . '"';
}
else
{
$statuses = explode(',', $status);
$status = 'WHERE 1=0';
foreach ( (array) $statuses as $st )
{
$status .= ' OR b_status = "' . $wpdb->escape(trim($st)) . '" ';
}
}
if (!empty($reader))
{
$status .= ' AND ' . $reader;
}
}
else
{
if (!empty($reader))
{
$status = ' WHERE ' . $reader;
}
}
$num = $wpdb->get_var("
SELECT
COUNT(*) AS count
FROM
{$wpdb->prefix}now_reading
$status
");
if ($echo)
{
echo "$num book".($num != 1 ? 's' : '');
}
return $num;
}
/**
* Prints the average number of books read in the given time limit.
* Unless $absolute is true, the average is computed based on the weighted average of
* books read witin the last 365 days and those read within the last 30 days.
* @param string $time_period The period to measure average over, eg "year", "month".
* @param bool $echo Whether or not to echo the results.
* @param bool $absolute If true, the average is computed based on the oldest finished date.
*/
function average_books($time_period = 'week', $echo = true, $absolute = true)
{
global $wpdb;
if ($absolute)
{
$books_per_day = $wpdb->get_var("
SELECT
( COUNT(*) / ( TO_DAYS(CURDATE()) - TO_DAYS(MIN(b_finished)) ) ) AS books_per_day_in_year
FROM
{$wpdb->prefix}now_reading
WHERE
b_status = 'read'
AND b_finished > 0
");
}
else
{
$books_per_day_in_year = $wpdb->get_var("
SELECT
( COUNT(*) / 365.0 ) AS books_per_day_in_year
FROM
{$wpdb->prefix}now_reading
WHERE
b_status = 'read'
AND TO_DAYS(b_finished) >= (TO_DAYS(CURDATE()) - 365)
AND TO_DAYS(b_started) >= (TO_DAYS(CURDATE()) - 365)
");
$books_per_day_in_month = $wpdb->get_var("
SELECT
( COUNT(*) / 30.0 ) AS books_per_day_in_month
FROM
{$wpdb->prefix}now_reading
WHERE
b_status = 'read'
AND TO_DAYS(b_finished) >= (TO_DAYS(CURDATE()) - 30)
");
// Give more weight for the last month's average than last year's.
$books_per_day = ((3.0 * $books_per_day_in_month) + (2.0 * $books_per_day_in_year)) / 5.0;
}
$average = 0;
switch ($time_period)
{
case 'year':
$average = round($books_per_day * 365);
break;
case 'month':
$average = round($books_per_day * 31);
break;
case 'week':
$average = round($books_per_day * 7);
break;
case 'day':
$average = round($books_per_day * 1);
break;
default:
return 0;
}
if($echo)
{
if ($absolute)
{
$type = __("an absolute");
}
else
{
$type = __("a current");
}
printf(__("%s average of %s book(s) per %s", NRTD), $type, $average, $time_period);
}
return $average;
}
/**
* Prints the URL to an internal page displaying data about the book.
* @param bool $echo Whether or not to echo the results.
* @param int $id The ID of the book to link to. If ommitted, the current book's ID will be used.
*/
function book_permalink( $echo = true, $id = 0 ) {
global $book, $wpdb;
$options = get_option(NOW_READING_OPTIONS);
if ( !empty($book) && empty($id) )
$the_book = $book;
elseif ( !empty($id) )
$the_book = get_book(intval($id));
if ( $the_book->id < 1 )
return;
$author = $the_book->nice_author;
$title = $the_book->nice_title;
if ( $options['useModRewrite'] )
$url = get_option('home') . "/" . preg_replace("/^\/|\/+$/", "", $options['permalinkBase']) . "/$author/$title/";
else
$url = get_option('home') . "/index.php?now_reading_author=$author&now_reading_title=$title";
$url = apply_filters('book_permalink', $url);
if ( $echo )
echo $url;
return $url;
}
/**
* Prints the URL to an internal page displaying books by a certain author.
* @param bool $echo Whether or not to echo the results.
* @param string $author The author to link to. If ommitted, the global book's author will be used.
*/
function book_author_permalink( $echo = true, $author = null ) {
global $book, $wpdb;
$options = get_option(NOW_READING_OPTIONS);
if ( !$author )
$author = $book->author;
if ( !$author )
return;
$nice_author = sanitize_title($author);
if ( $options['useModRewrite'] )
$url = get_option('home') . "/" . preg_replace("/^\/|\/+$/", "", $options['permalinkBase']) . "/$nice_author/";
else
$url = get_option('home') . "/index.php?now_reading_author=$nice_author";
$url = apply_filters('book_author_permalink', $url);
if ( $echo )
echo $url;
return $url;
}
/**
* Prints the URL to an internal page displaying books by a certain reader.
* @param bool $echo Wether or not to echo the results.
* @param int $reader The reader id. If omitted, links to all books.
*/
function book_reader_permalink( $echo = true, $reader = 0) { //added by B. Spyckerelle for multiuser mode
global $book, $wpdb;
$options = get_option(NOW_READING_OPTIONS);
if ( !$reader )
$reader = $book->reader;
if ( !$reader )
return;
if ($options['multiuserMode']) {
$url = get_option('home') . "/" . preg_replace("/^\/|\/+$/", "", $options['permalinkBase']) . "/reader/$reader/";
} else {
$url = get_option('home') . "/index.php?now_reading_library=1&now_reading_reader=$reader";
}
if ($echo)
echo $url;
return $url;
}
/**
* Prints a URL to the book's Amazon detail page. If the book is a custom one, it will print a URL to the book's permalink page.
* @param bool $echo Whether or not to echo the results.
* @param string $domain The Amazon domain to link to. If ommitted, the default domain will be used.
* @see book_permalink()
* @see is_custom_book()
*/
function book_url( $echo = true, $domain = null ) {
global $book;
$options = get_option(NOW_READING_OPTIONS);
if ( empty($domain) )
$domain = $options['domain'];
if ( is_custom_book() )
return book_permalink($echo);
else {
$url = apply_filters('book_url', "http://www.amazon{$domain}/exec/obidos/ASIN/{$book->asin}/ref=nosim/{$options['associate']}");
if ( $echo )
echo $url;
return $url;
}
}
/**
* Returns true if the current book is linked to a post, false if it isn't.
*/
function book_has_post()
{
global $book;
return ($book->post > 0);
}
/**
* Returns or prints the permalink of the post linked to the current book.
* @param bool $echo Whether or not to echo the results.
*/
function book_post_url($echo = true)
{
global $book;
if (!book_has_post())
{
return;
}
$permalink = get_permalink($book->post);
if ($echo)
{
echo $permalink;
}
return $permalink;
}
/**
* Returns or prints the title of the post linked to the current book.
* @param bool $echo Whether or not to echo the results.
*/
function book_post_title($echo = true)
{
global $book;
if (!book_has_post())
{
return;
}
$post = get_post($book->post);
if ($echo)
{
echo $post->post_title;
}
return $post->post_title;
}
/**
* If the current book is linked to a post, prints an HTML link to said post.
* @param bool $echo Whether or not to echo the results.
*/
function book_post_link($echo = true)
{
global $book;
if (!book_has_post())
{
return;
}
$link = '<a href="' . book_post_url(0) . '">' . book_post_title(0) . '</a>';
if ($echo)
{
echo $link;
}
return $link;
}
/**
* If the user has the correct permissions, prints a URL to the Manage -> Now Reading page of the WP admin.
* @param bool $echo Whether or not to echo the results.
*/
function manage_library_url( $echo = true ) {
global $nr_url;
if ( can_now_reading_admin() )
echo apply_filters('book_manage_url', $nr_url->urls['manage']);
}
/**
* If the user has the correct permissions, prints a URL to the review-writing screen for the current book.
* @param bool $echo Whether or not to echo the results.
*/
function book_edit_url( $echo = true ) {
global $book, $nr_url;
if ( can_now_reading_admin() )
echo apply_filters('book_edit_url', $nr_url->urls['manage'] . '&action=editsingle&id=' . $book->id);
}
/**
* Returns true if the book is a custom one or false if it is one from Amazon.
*/
function is_custom_book() {
global $book;
return empty($book->asin);
}
/**
* Returns true if the user has the correct permissions to view the Now Reading admin panel.
*/
function can_now_reading_admin() {
//depends on multiuser mode (B. Spyckerelle)
$options = get_option(NOW_READING_OPTIONS);
$nr_level = $options['multiuserMode'] ? 'level_2' : 'level_9';
return current_user_can($nr_level);
}
/**
* Returns true if the current book is owned by the current user
* Meaningful only when a user is logged in.
* Works for both multi-user and single-user modes.
*/
function is_my_book()
{
global $book, $userdata;
if (is_user_logged_in())
{
get_currentuserinfo();
return $book->reader == $userdata->ID;
}
else
{
return false;
}
}
/**
* Prints a URL pointing to the main library page that respects the useModRewrite option.
* @param bool $echo Whether or not to echo the results.
*/
function library_url( $echo = true ) {
$options = get_option(NOW_READING_OPTIONS);
if ( $options['useModRewrite'] )
$url = get_option('home') . "/" . preg_replace("/^\/|\/+$/", "", $options['permalinkBase']);
else
$url = get_option('home') . '/index.php?now_reading_library=true';
$url = apply_filters('book_library_url', $url);
if ( $echo )
echo $url;
return $url;
}
/**
* Prints the book's rating or "Unrated" if the book is unrated.
* @param bool $echo Whether or not to echo the results.
*/
function book_rating( $echo = true ) {
global $book;
if ( $book->rating )
$rate = apply_filters('book_rating', $book->rating);
else
$rate = apply_filters('book_rating', __('Unrated', NRTD));
if ( $echo )
echo $rate;
return $rate;
}
/**
* Prints the book's review or "This book has not yet been reviewed" if the book is unreviewed.
* @param bool $echo Whether or not to echo the results.
*/
function book_review( $echo = true ) {
global $book;
if ( $book->review )
echo apply_filters('book_review', $book->review);
else
echo apply_filters('book_review', '<p>' . __('This book has not yet been reviewed.', NRTD) . '</p>');
}
/**
* Prints the URL of the search page, ready to be appended with a query or simply used as the action of a GET form.
* @param bool $echo Whether or not to echo the results.
*/
function search_url( $echo = true ) {
$options = get_option(NOW_READING_OPTIONS);
if ( $options['useModRewrite'] )
$url = get_option('home') . "/" . preg_replace("/^\/|\/+$/", "", $options['permalinkBase']) . "/search";
else
$url = get_option('home');
$url = apply_filters('library_search_url', $url);
if ( $echo )
echo $url;
return $url;
}
/**
* Prints the current search query, if it exists.
* @param bool $echo Whether or not to echo the results.
*/
function search_query( $echo = true ) {
global $query;
if ( empty($query) )
return;
$query = htmlentities(stripslashes($query));
if ( $echo )
echo $query;
return $query;
}
/**
* Prints a standard search form for users who don't want to create their own.
* @param bool $echo Whether or not to echo the results.
*/
function library_search_form($echo = true)
{
$options = get_option(NOW_READING_OPTIONS);
$html = '
<form method="get" action="' . search_url(0) . '">
';
if (!$options['useModRewrite'])
{
$html .= '<input type="hidden" name="now_reading_search" value="1" />';
}
$html .= '
<div style="text-align:center">
<input type="text" name="q" />
<input type="submit" value="' . __("Search Library", NRTD) . '" />
</div>
</form>
';
if ($echo)
{
echo $html;
}
return $html;
}
/**
* Prints the book's meta data in a definition list.
* @see get_book_meta()
* @param bool $new_list Whether to start a new list (creating new <dl> tags).
*/
function print_book_meta( $new_list = true ) {
global $book;
$meta = get_book_meta($book->id);
if ( count($meta) < 1 )
return;
if ( $new_list )
echo '<dl>';
foreach ( (array) $meta as $key => $value ) {
$key = apply_filters('book_meta_key', $key);
$value = apply_filters('book_meta_val', $value);
echo '<dt>';
if ( strtolower($key) == $key )
echo ucwords($key);
else
echo $key;
echo '</dt>';
echo "<dd>$value</dd>";
}
if ( $new_list )
echo '</dl>';
}
/**
* Prints a single book meta value.
* @param $key The meta key to fetch
* @param $echo Whether to echo the result or just return it
* @returns string The meta value for the given $key.
*/
function book_meta( $key, $echo = true ) {
global $book;
$meta = get_book_meta($book->id, $key);
if ( empty($meta) )
return;
$meta = apply_filters('book_meta_val', $meta);
if ( $echo )
echo $meta;
return $meta;
}
/**
* Prints a comma-separated list of tags for the current book.
* @param bool $echo Whether or not to echo the results.
*/
function print_book_tags($echo = true)
{
global $book;
$tags = get_book_tags($book->id);
if ( count($tags) < 1 )
return;
$i = 0;
$string = '';
foreach ( (array) $tags as $tag ) {
if ( $i++ != 0 )
$string .= ', ';
$link = book_tag_url($tag, 0);
$string .= "<a href='$link'>$tag</a>";
}
if ( $echo )
echo $string;
return $string;
}
/**
* Returns a URL to the permalink for a given tag.
* @param bool $echo Whether or not to echo the results.
*/
function book_tag_url( $tag, $echo = true ) {
$options = get_option(NOW_READING_OPTIONS);
if ( $options['useModRewrite'] )
$url = get_option('home') . "/" . preg_replace("/^\/|\/+$/", "", $options['permalinkBase']) . "/tag/" . urlencode($tag);
else
$url = get_option('home') . '/index.php?now_reading_tag=' . urlencode($tag);
$url = apply_filters('library_tag_url', $url);
if ( $echo )
echo $url;
return $url;
}
/**
* Returns a URL to the permalink for a given (custom) page.
* @param string $page Page name (e.g. custom.php) to create URL for.
* @param bool $echo Whether or not to echo the results.
*/
function library_page_url( $page, $echo = true ) {
$options = get_option(NOW_READING_OPTIONS);
if ( $options['useModRewrite'] )
$url = get_option('home') . "/" . preg_replace("/^\/|\/+$/", "", $options['permalinkBase']) . "/page/" . urlencode($page);
else
$url = get_option('home') . '/index.php?now_reading_page=' . urlencode($page);
$url = apply_filters('library_page_url', $url);
if ( $echo )
echo $url;
return $url;
}
/**