-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbook.php
568 lines (479 loc) · 14.3 KB
/
book.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
<?php
/**
* Book fetching/updating functions
* @package now-reading
*/
/**
* Fetches books from the database based on a given query.
*
* Example usage:
* <code>
* $books = get_books('status=reading&orderby=started&order=asc&num=-1&reader=user');
* </code>
* @param string $query Query string containing restrictions on what to fetch.
* Valid variables: $num, $status, $orderby, $order, $search, $author, $title, $rating, $reader, $started_year, $started_month, $finished_year, $finished_month.
* @param bool show_private If true, will show all readers' private books!
* @return array Returns a numerically indexed array in which each element corresponds to a book.
*/
function get_books($query, $show_private = false) {
global $wpdb;
$options = get_option(NOW_READING_OPTIONS);
parse_str($query);
// We're fetching a collection of books, not just one.
switch ( $status ) {
case 'unread':
case 'onhold':
case 'reading':
case 'read':
break;
default:
$status = 'all';
break;
}
if ( $status != 'all' )
$status = "AND b_status = '$status'";
else
$status = '';
if ( !empty($search) ) {
$search = $wpdb->escape($search);
$search = "AND ( b_author LIKE '%$search%' OR b_title LIKE '%$search%' OR m_value LIKE '%$search%')";
} else
$search = '';
$order = ( strtolower($order) == 'desc' ) ? 'DESC' : 'ASC';
switch ( $orderby ) {
case 'added':
$orderby = 'b_added';
break;
case 'started':
$orderby = 'b_started';
break;
case 'finished':
$orderby = 'b_finished';
break;
case 'title':
$orderby = 'b_title';
break;
case 'author':
$orderby = 'b_author';
break;
case 'asin':
$orderby = 'b_asin';
break;
case 'status':
$orderby = "b_status $order, b_added";
break;
case 'rating':
$orderby = 'b_rating';
break;
case 'random':
$orderby = 'RAND()';
break;
default:
$orderby = 'b_added';
break;
}
if ( $num > -1 && $offset >= 0 ) {
$offset = intval($offset);
$num = intval($num);
$limit = "LIMIT $offset, $num";
} else
$limit = '';
if ( !empty($author) ) {
$author = $wpdb->escape($author);
$author = "AND b_author = '$author'";
}
if ( !empty($title) ) {
$title = $wpdb->escape($title);
$title = "AND b_title = '$title'";
}
if ( !empty($tag) ) {
$tag = $wpdb->escape($tag);
$tag = "AND t_name = '$tag'";
}
$meta = '';
if ( !empty($meta_key) ) {
$meta_key = $wpdb->escape($meta_key);
$meta = "AND meta_key = '$meta_key'";
if ( !empty($meta_value )) {
$meta_value = $wpdb->escape($meta_value);
$meta .= " AND meta_value = '$meta_value'";
}
}
if (!empty($started_year))
{
$started_year = "AND YEAR(b_started) " . (is_numeric($started_year) ? "=" : "") . " $started_year";
}
if (!empty($started_month))
{
$started_month = "AND MONTH(b_started) " . (is_numeric($started_month) ? "=" : "") . " $started_month";
}
if (!empty($finished_year))
{
$finished_year = "AND YEAR(b_finished) " . (is_numeric($finished_year) ? "=" : "") . " $finished_year";
}
if (!empty($finished_month))
{
$finished_month = "AND MONTH(b_finished) " . (is_numeric($finished_month) ? "=" : "") . " $finished_month";
}
if (!empty($rating))
{
$rating = "AND b_rating " . (is_numeric($rating) ? "=" : "") . " $rating";
}
$reader = get_reader_visibility_filter($reader, $show_private);
$query = "
SELECT
COUNT(*) AS count,
b_id AS id, b_title AS title, b_author AS author, b_image AS image, b_status AS status, b_nice_title AS nice_title, b_nice_author AS nice_author,
b_added AS added, b_started AS started, b_finished AS finished,
b_asin AS asin, b_rating AS rating, b_review AS review, b_post AS post, b_reader as reader, b_post_op as post_op
FROM
{$wpdb->prefix}now_reading
LEFT JOIN {$wpdb->prefix}now_reading_meta
ON m_book = b_id
LEFT JOIN {$wpdb->prefix}now_reading_books2tags
ON book_id = b_id
LEFT JOIN {$wpdb->prefix}now_reading_tags
ON tag_id = t_id
WHERE
1=1
$status
$id
$search
$author
$title
$tag
$meta
$started_year
$started_month
$finished_year
$finished_month
$rating
AND
$reader
GROUP BY
b_id
ORDER BY
$orderby $order
$limit
";
$books = $wpdb->get_results($query);
$books = apply_filters('get_books', $books);
foreach ( (array) $books as $book ) {
$book->added = ( nr_empty_date($book->added) ) ? '' : $book->added;
$book->started = ( nr_empty_date($book->started) ) ? '' : $book->started;
$book->finished = ( nr_empty_date($book->finished) ) ? '' : $book->finished;
}
return $books;
}
/**
* Fetches a single book with the given ID.
* @param int $id The b_id of the book you want to fetch.
*/
function get_book($id)
{
global $wpdb;
$id = intval($id);
$book = apply_filters('get_single_book', $wpdb->get_row("
SELECT
COUNT(*) AS count,
b_id AS id, b_title AS title, b_author AS author, b_image AS image, b_status AS status, b_nice_title AS nice_title, b_nice_author AS nice_author,
b_added AS added, b_started AS started, b_finished AS finished,
b_asin AS asin, b_rating AS rating, b_review AS review, b_post AS post, b_reader as reader,
b_post_op AS post_op, b_visibility AS visibility
FROM {$wpdb->prefix}now_reading
WHERE b_id = $id
GROUP BY b_id
"));
$book->added = (nr_empty_date($book->added) ? '' : $book->added);
$book->started = (nr_empty_date($book->started) ? '' : $book->started);
$book->finished = (nr_empty_date($book->finished) ? '' : $book->finished);
return $book;
}
/**
* Returns a string to be used in a WHERE clause to
* select books based on a reader (or all) and visibility.
* @param int $userID Interested only in the given userID's books.
* @param bool show_private If true, will show all readers' private books!
*/
function get_reader_visibility_filter($userID = 0, $show_private = false)
{
global $user_ID;
if (!$show_private)
{
// Show publics only.
$visibility = "b_visibility = 1";
}
if (!empty($userID))
{
// we're only interested in this reader.
$reader = "b_reader = '$userID'";
if (is_user_logged_in())
{
get_currentuserinfo();
if ($show_private || ($userID == $user_ID))
{
// Privates are shown, so don't filter them.
return $reader;
}
}
}
else
{
// No specific reader, see if there is a logged-user, then get her private
// books too, otherwise, get everyeone's publics.
if (is_user_logged_in())
{
get_currentuserinfo();
// Either it's the owner, or we get public books only.
return "(b_reader = '$user_ID' OR b_visibility = 1)";
}
}
if (!empty($reader) && !empty($visibility))
{
return $reader . ' AND ' . $visibility;
}
return !empty($reader) ? $reader : $visibility;
}
/**
* Adds a book to the database.
* @param string $query Query string containing the fields to add.
* @return boolean True on success, false on failure.
*/
function add_book( $query ) {
return update_book($query);
}
/**
* Updates a given book's database entry
* @param string $query Query string containing the fields to add.
* @return boolean True on success, false on failure.
*/
function update_book( $query ) {
global $wpdb, $query, $fields, $userdata;
parse_str($query, $fields);
$fields = apply_filters('add_book_fields', $fields);
// If an ID is specified, we're doing an update; otherwise, we're doing an insert.
$insert = empty($fields['b_id']);
$valid_fields = array('b_id', 'b_added', 'b_started', 'b_finished', 'b_title', 'b_nice_title',
'b_author', 'b_nice_author', 'b_image', 'b_asin', 'b_status', 'b_rating', 'b_review', 'b_post');
if ( $insert ) {
$colums = $values = '';
foreach ( (array) $fields as $field => $value ) {
if ( empty($field) || empty($value) || !in_array($field, $valid_fields) )
continue;
$value = $wpdb->escape($value);
$columns .= ", $field";
$values .= ", '$value'";
}
get_currentuserinfo();
$reader_id = $userdata->ID;
$columns .= ", b_reader";
$values .= ", '$reader_id'";
$columns = preg_replace('#^, #', '', $columns);
$values = preg_replace('#^, #', '', $values);
$wpdb->query("
INSERT INTO {$wpdb->prefix}now_reading
($columns)
VALUES($values)
");
$id = $wpdb->get_var("SELECT MAX(b_id) FROM {$wpdb->prefix}now_reading");
if ( $id > 0 ) {
do_action('book_added', $id);
return $id;
} else {
return false;
}
} else {
$id = intval($fields['b_id']);
unset($fields['b_id']);
$set = '';
foreach ( (array) $fields as $field => $value ) {
if ( empty($field) || empty($value) || !in_array($field, $valid_fields) )
continue;
$value = $wpdb->escape($value);
$set .= ", $field = '$value'";
}
$set = preg_replace('#^, #', '', $set);
$wpdb->query("
UPDATE {$wpdb->prefix}now_reading
SET $set
WHERE b_id = $id
");
do_action('book_updated', $id);
return $id;
}
}
/**
* Adds a tag to the database.
*/
function add_library_tag( $tag ) {
global $wpdb;
$exists = library_tag_exists($tag);
if ( $exists ) {
return $exists;
} else {
$tag = $wpdb->escape(trim($tag));
$wpdb->query("INSERT INTO {$wpdb->prefix}now_reading_tags (t_name) VALUES('$tag')");
return $wpdb->insert_id;
}
}
/**
* Checks if a tag exists
* @return int The tag's ID if it exists, 0 if it doesn't
*/
function library_tag_exists( $tag ) {
global $wpdb;
$tag = $wpdb->escape(trim($tag));
$id = $wpdb->get_var("SELECT t_id FROM {$wpdb->prefix}now_reading_tags WHERE t_name = '$tag'");
return intval($id);
}
/**
* Gets the tags for the given book.
*/
function get_book_tags( $id ) {
global $wpdb;
if ( !$id )
return array();
$tags = $wpdb->get_results("
SELECT t_name AS name FROM {$wpdb->prefix}now_reading_tags, {$wpdb->prefix}now_reading_books2tags
WHERE book_id = $id AND tag_id = t_id
ORDER BY t_name ASC
");
$array = array();
if ( count($tags) > 0 ) {
foreach ( (array) $tags as $tag ) {
$array[] = $tag->name;
}
}
return $array;
}
/**
* Tags the book with the given tag.
*/
function tag_book( $id, $tag ) {
return set_book_tags($id, $tag, true);
}
/**
* Sets the tags for the given book.
* @param bool $append If true, add the given tags onto the existing ones; if false, replace current tags with new ones.
*/
function set_book_tags( $id, $tags, $append = false ) {
global $wpdb;
$id = intval($id);
if ( !$id )
return;
if ( !is_array($tags) ) {
$tags = (array) explode(',', $tags);
}
$old_tags = get_book_tags($id);
// Tags to add
$add = array_diff($tags, $old_tags);
if ( $add ) {
foreach ( (array) $add as $tag ) {
$tid = library_tag_exists($tag);
if ( !$tid ) // create the tag if it doesn't exist
$tid = add_library_tag($tag);
$wpdb->query("INSERT INTO {$wpdb->prefix}now_reading_books2tags (book_id, tag_id) VALUES($id, $tid)");
}
}
// Tags to delete
$delete = array_diff($old_tags, $tags);
if ( $delete && !$append ) {
foreach ( (array) $delete as $tag ) {
$tid = library_tag_exists($tag);
$wpdb->query("DELETE FROM {$wpdb->prefix}now_reading_books2tags WHERE book_id = $id AND tag_id = $tid");
}
}
}
/**
* DEPRECATED: Fetches all the books tagged with the given tag.
*/
function get_books_by_tag( $tag, $query ) {
return get_books("tag=$tag");
}
/**
* Fetches meta-data for the given book.
* @see print_book_meta()
*/
function get_book_meta( $id, $key = '' ) {
global $wpdb;
if ( !$id )
return null;
$id = intval($id);
if ( !empty($key) )
$key = 'AND m_key = "' . $wpdb->escape($key) . '"';
else
$key = '';
$raws = $wpdb->get_results("SELECT m_key, m_value FROM {$wpdb->prefix}now_reading_meta WHERE m_book = '$id' $key");
if ( !count($raws) )
return null;
$meta = null;
if ( empty($key) ) {
$meta = array();
foreach ( (array) $raws as $raw ) {
$meta[$raw->m_key] = $raw->m_value;
}
$meta = apply_filters('book_meta', $meta);
} else {
$meta = $raws[0]->m_value;
$meta = apply_filters('book_meta_single', $meta);
}
return $meta;
}
/**
* Adds a meta key-value pairing for the given book.
*/
function add_book_meta( $id, $key, $value ) {
return update_book_meta($id, $key, $value);
}
/**
* Updates the meta key-value pairing for the given book. If the key does not exist, it will be created.
*/
function update_book_meta( $id, $key, $value ) {
global $wpdb;
$key = $wpdb->escape($key);
$value = $wpdb->escape($value);
$existing = $wpdb->get_var("
SELECT
m_id AS id
FROM
{$wpdb->prefix}now_reading_meta
WHERE
m_book = '$id'
AND
m_key = '$key'
");
if ( $existing != null ) {
$result = $wpdb->query("
UPDATE {$wpdb->prefix}now_reading_meta
SET
m_key = '$key',
m_value = '$value'
WHERE
m_id = '$existing'
");
} else {
$result = $wpdb->query("
INSERT INTO {$wpdb->prefix}now_reading_meta
(m_book, m_key, m_value)
VALUES('$id', '$key', '$value')
");
}
return $result;
}
/**
* Deletes the meta key-value pairing for the given book with the given key.
*/
function delete_book_meta( $id, $key ) {
global $wpdb;
$id = intval($id);
$key = $wpdb->escape($key);
return $wpdb->query("
DELETE FROM
{$wpdb->prefix}now_reading_meta
WHERE
m_book = '$id'
AND
m_key = '$key'
");
}
?>