forked from ashleyfae/book-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook-database.php
586 lines (489 loc) · 19.5 KB
/
book-database.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
<?php
/**
* Plugin Name: Book Database
* Plugin URI: https://shop.nosegraze.com/product/book-database/
* Description: Maintain a database of books and reviews.
* Version: 1.0
* Author: Ashley Gibson
* Author URI: http://www.nosegraze.com
* License: GPL2 License
* URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: book-database
* Domain Path: /languages
*
* Book Database is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* Book Database is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Book Database. If not, see <http://www.gnu.org/licenses/>.
*
* @package book-database
* @copyright Copyright (c) 2019, Ashley Gibson
* @license GPL2+
*/
namespace Book_Database;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'BDB_VERSION' ) ) {
define( 'BDB_VERSION', '1.0' );
}
if ( ! defined( 'BDB_DIR' ) ) {
define( 'BDB_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'BDB_URL' ) ) {
define( 'BDB_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'BDB_FILE' ) ) {
define( 'BDB_FILE', __FILE__ );
}
if ( ! defined( 'NOSE_GRAZE_STORE_URL' ) ) {
define( 'NOSE_GRAZE_STORE_URL', 'https://shop.nosegraze.com' );
}
/**
* Class Book_Database
* @package Book_Database
*/
final class Book_Database {
/**
* Instance of the Book_Database class.
*
* @var Book_Database
*/
private static $instance;
/**
* Array of custom table objects
*
* @var array
*/
private $tables = array();
/**
* @var REST_API
*/
private $rest_api;
/**
* @var HTML
*/
private $html;
/**
* Book_Database instance.
*
* @return Book_Database Instance of Book_Database class
*/
public static function instance() {
// Return if already instantiated
if ( self::is_instantiated() ) {
return self::$instance;
}
// Set up the singleton.
self::setup_instance();
// Bootstrap
self::$instance->setup_files();
self::$instance->setup_application();
add_action( 'admin_init', array( self::$instance, 'install' ), 11 );
return self::$instance;
}
/**
* Whether the main class has been instantiated or not.
*
* @return bool
*/
private static function is_instantiated() {
// Return true if instance is correct class
if ( ! empty( self::$instance ) && ( self::$instance instanceof Book_Database ) ) {
return true;
}
// Return false if not instantiated correctly
return false;
}
/**
* Set up the singleton instance
*/
private static function setup_instance() {
self::$instance = new Book_Database();
}
/**
* Include required files.
*
* @return void
*/
private function setup_files() {
$this->include_files();
// Admin
if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
$this->include_admin();
} else {
$this->include_frontend();
}
}
/**
* Include global files
*/
private function include_files() {
require_once BDB_DIR . 'includes/abstract-class-base-object.php';
require_once BDB_DIR . 'includes/class-exception.php';
// Database engine
require_once BDB_DIR . 'includes/database/engine/base.php';
require_once BDB_DIR . 'includes/database/engine/table.php';
require_once BDB_DIR . 'includes/database/engine/query.php';
require_once BDB_DIR . 'includes/database/engine/column.php';
require_once BDB_DIR . 'includes/database/engine/row.php';
require_once BDB_DIR . 'includes/database/engine/schema.php';
require_once BDB_DIR . 'includes/database/engine/compare.php';
require_once BDB_DIR . 'includes/database/engine/date.php';
require_once BDB_DIR . 'includes/database/engine/series.php';
require_once BDB_DIR . 'includes/database/engine/tax.php';
require_once BDB_DIR . 'includes/database/engine/author.php';
require_once BDB_DIR . 'includes/database/engine/join.php';
require_once BDB_DIR . 'includes/database/engine/book.php';
require_once BDB_DIR . 'includes/database/engine/edition.php';
require_once BDB_DIR . 'includes/database/engine/reading-log.php';
require_once BDB_DIR . 'includes/database/engine/class-where-clause.php';
require_once BDB_DIR . 'includes/database/sanitization.php';
// Database - authors
require_once BDB_DIR . 'includes/database/authors/class-authors-table.php';
require_once BDB_DIR . 'includes/database/authors/class-authors-schema.php';
require_once BDB_DIR . 'includes/database/authors/class-authors-query.php';
// Database - book_author_relationships
require_once BDB_DIR . 'includes/database/book-author-relationships/class-book-author-relationships-table.php';
require_once BDB_DIR . 'includes/database/book-author-relationships/class-book-author-relationships-schema.php';
require_once BDB_DIR . 'includes/database/book-author-relationships/class-book-author-relationships-query.php';
// Database - books
require_once BDB_DIR . 'includes/database/books/class-books-table.php';
require_once BDB_DIR . 'includes/database/books/class-books-schema.php';
require_once BDB_DIR . 'includes/database/books/class-books-query.php';
require_once BDB_DIR . 'includes/database/books/class-book-meta-table.php';
// Database - book_links
require_once BDB_DIR . 'includes/database/book-links/class-book-links-table.php';
require_once BDB_DIR . 'includes/database/book-links/class-book-links-schema.php';
require_once BDB_DIR . 'includes/database/book-links/class-book-links-query.php';
// Database - book_taxonomies
require_once BDB_DIR . 'includes/database/book-taxonomies/class-book-taxonomies-table.php';
require_once BDB_DIR . 'includes/database/book-taxonomies/class-book-taxonomies-schema.php';
require_once BDB_DIR . 'includes/database/book-taxonomies/class-book-taxonomies-query.php';
// Database - book_term_relationships
require_once BDB_DIR . 'includes/database/book-term-relationships/class-book-term-relationships-table.php';
require_once BDB_DIR . 'includes/database/book-term-relationships/class-book-term-relationships-schema.php';
require_once BDB_DIR . 'includes/database/book-term-relationships/class-book-term-relationships-query.php';
// Database - book_terms
require_once BDB_DIR . 'includes/database/book-terms/class-book-terms-table.php';
require_once BDB_DIR . 'includes/database/book-terms/class-book-terms-schema.php';
require_once BDB_DIR . 'includes/database/book-terms/class-book-terms-query.php';
// Database - owned_editions
require_once BDB_DIR . 'includes/database/editions/class-editions-table.php';
require_once BDB_DIR . 'includes/database/editions/class-editions-schema.php';
require_once BDB_DIR . 'includes/database/editions/class-editions-query.php';
// Database - reading_log
require_once BDB_DIR . 'includes/database/reading-logs/class-reading-logs-table.php';
require_once BDB_DIR . 'includes/database/reading-logs/class-reading-logs-schema.php';
require_once BDB_DIR . 'includes/database/reading-logs/class-reading-logs-query.php';
// Database - retailers
require_once BDB_DIR . 'includes/database/retailers/class-retailers-table.php';
require_once BDB_DIR . 'includes/database/retailers/class-retailers-schema.php';
require_once BDB_DIR . 'includes/database/retailers/class-retailers-query.php';
// Database - reviews
require_once BDB_DIR . 'includes/database/reviews/class-reviews-table.php';
require_once BDB_DIR . 'includes/database/reviews/class-reviews-schema.php';
require_once BDB_DIR . 'includes/database/reviews/class-reviews-query.php';
require_once BDB_DIR . 'includes/database/reviews/class-review-meta-table.php';
// Database - series
require_once BDB_DIR . 'includes/database/series/class-series-table.php';
require_once BDB_DIR . 'includes/database/series/class-series-schema.php';
require_once BDB_DIR . 'includes/database/series/class-series-query.php';
// Authors
require_once BDB_DIR . 'includes/authors/class-author.php';
require_once BDB_DIR . 'includes/authors/author-functions.php';
// Book Author Relationships
require_once BDB_DIR . 'includes/book-author-relationships/class-book-author-relationship.php';
require_once BDB_DIR . 'includes/book-author-relationships/book-author-relationship-actions.php';
require_once BDB_DIR . 'includes/book-author-relationships/book-author-relationship-functions.php';
// Books
require_once BDB_DIR . 'includes/books/class-book.php';
require_once BDB_DIR . 'includes/books/class-book-layout.php';
require_once BDB_DIR . 'includes/books/book-functions.php';
require_once BDB_DIR . 'includes/books/book-layout-functions.php';
require_once BDB_DIR . 'includes/books/book-meta.php';
// Book Links
require_once BDB_DIR . 'includes/book-links/class-book-link.php';
require_once BDB_DIR . 'includes/book-links/book-link-functions.php';
// Book Taxonomies
require_once BDB_DIR . 'includes/book-taxonomies/class-book-taxonomy.php';
require_once BDB_DIR . 'includes/book-taxonomies/book-taxonomy-functions.php';
// Book Term Relationships
require_once BDB_DIR . 'includes/book-term-relationships/class-book-term-relationship.php';
require_once BDB_DIR . 'includes/book-term-relationships/book-term-relationship-actions.php';
require_once BDB_DIR . 'includes/book-term-relationships/book-term-relationship-functions.php';
// Book Terms
require_once BDB_DIR . 'includes/book-terms/class-book-term.php';
require_once BDB_DIR . 'includes/book-terms/book-term-functions.php';
// Editions
require_once BDB_DIR . 'includes/editions/class-edition.php';
require_once BDB_DIR . 'includes/editions/edition-functions.php';
// Ratings
require_once BDB_DIR . 'includes/ratings/class-rating.php';
require_once BDB_DIR . 'includes/ratings/rating-functions.php';
// Reading Logs
require_once BDB_DIR . 'includes/reading-logs/class-reading-log.php';
require_once BDB_DIR . 'includes/reading-logs/reading-log-functions.php';
// Retailers
require_once BDB_DIR . 'includes/retailers/class-retailer.php';
require_once BDB_DIR . 'includes/retailers/retailer-functions.php';
// Reviews
require_once BDB_DIR . 'includes/reviews/class-review.php';
require_once BDB_DIR . 'includes/reviews/review-actions.php';
require_once BDB_DIR . 'includes/reviews/review-functions.php';
require_once BDB_DIR . 'includes/reviews/review-meta.php';
// Series
require_once BDB_DIR . 'includes/series/class-series.php';
require_once BDB_DIR . 'includes/series/series-functions.php';
// REST API
require_once BDB_DIR . 'includes/rest-api/class-rest-api.php';
require_once BDB_DIR . 'includes/rest-api/abstract-class-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-analytics-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-author-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-book-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-book-link-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-book-term-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-edition-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-reading-log-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-retailer-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-review-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-series-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-taxonomy-controller.php';
require_once BDB_DIR . 'includes/rest-api/v1/class-utility-controller.php';
// Misc.
require_once BDB_DIR . 'includes/capabilities.php';
require_once BDB_DIR . 'includes/class-analytics.php';
require_once BDB_DIR . 'includes/class-book-reviews-query.php';
require_once BDB_DIR . 'includes/class-book-grid-query.php';
require_once BDB_DIR . 'includes/class-html.php';
require_once BDB_DIR . 'includes/misc-functions.php';
require_once BDB_DIR . 'includes/rewrites.php';
require_once BDB_DIR . 'includes/shortcodes.php';
require_once BDB_DIR . 'includes/template-functions.php';
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once BDB_DIR . 'includes/class-cli.php';
}
}
/**
* Include admin files
*/
private function include_admin() {
require_once BDB_DIR . 'includes/admin/abstract-class-list-table.php';
require_once BDB_DIR . 'includes/admin/admin-assets.php';
require_once BDB_DIR . 'includes/admin/admin-notices.php';
require_once BDB_DIR . 'includes/admin/admin-pages.php';
// Analytics
require_once BDB_DIR . 'includes/admin/analytics/analytics-actions.php';
require_once BDB_DIR . 'includes/admin/analytics/analytics-page.php';
// Authors
require_once BDB_DIR . 'includes/admin/authors/author-actions.php';
require_once BDB_DIR . 'includes/admin/authors/author-functions.php';
require_once BDB_DIR . 'includes/admin/authors/authors-page.php';
// Book Terms
require_once BDB_DIR . 'includes/admin/book-terms/book-term-actions.php';
require_once BDB_DIR . 'includes/admin/book-terms/book-term-functions.php';
require_once BDB_DIR . 'includes/admin/book-terms/book-terms-page.php';
// Books
require_once BDB_DIR . 'includes/admin/books/book-actions.php';
require_once BDB_DIR . 'includes/admin/books/book-functions.php';
require_once BDB_DIR . 'includes/admin/books/books-page.php';
require_once BDB_DIR . 'includes/admin/books/edit-book-fields.php';
// Dashboard
require_once BDB_DIR . 'includes/admin/dashboard/widgets.php';
// Editions
require_once BDB_DIR . 'includes/admin/editions/edition-actions.php';
// Licensing
require_once BDB_DIR . 'vendor/class-edd-sl-plugin-updater.php';
require_once BDB_DIR . 'includes/admin/licensing/class-license-key.php';
require_once BDB_DIR . 'includes/admin/licensing/license-actions.php';
// Posts
require_once BDB_DIR . 'includes/admin/posts/post-actions.php';
// Reading Logs
require_once BDB_DIR . 'includes/admin/reading-logs/reading-log-actions.php';
// Reviews
require_once BDB_DIR . 'includes/admin/reviews/review-actions.php';
require_once BDB_DIR . 'includes/admin/reviews/review-fields.php';
require_once BDB_DIR . 'includes/admin/reviews/review-functions.php';
require_once BDB_DIR . 'includes/admin/reviews/reviews-page.php';
// Series
require_once BDB_DIR . 'includes/admin/series/series-actions.php';
require_once BDB_DIR . 'includes/admin/series/series-functions.php';
require_once BDB_DIR . 'includes/admin/series/series-page.php';
// Settings
require_once BDB_DIR . 'includes/admin/settings/book-layout-functions.php';
require_once BDB_DIR . 'includes/admin/settings/register-settings.php';
require_once BDB_DIR . 'includes/admin/settings/display-settings.php';
}
/**
* Include front-end files
*/
private function include_frontend() {
require_once BDB_DIR . 'includes/assets.php';
}
/**
* Set up custom database tables
*/
private function setup_application() {
self::$instance->tables = array(
'authors' => new Authors_Table(),
'book_author_relationships' => new Book_Author_Relationships_Table(),
'book_links' => new Book_Links_Table(),
'book_taxonomies' => new Book_Taxonomies_Table(),
'book_term_relationships' => new Book_Term_Relationships_Table(),
'book_terms' => new Book_Terms_Table(),
'books' => new Books_Table(),
'book_meta' => new Book_Meta_Table(),
'editions' => new Editions_Table(),
'reading_log' => new Reading_Logs_Table(),
'retailers' => new Retailers_Table(),
'reviews' => new Reviews_Table(),
'review_meta' => new Review_Meta_Table(),
'series' => new Series_Table(),
);
self::$instance->rest_api = new REST_API();
self::$instance->html = new HTML();
}
/**
* Get a table object by its key
*
* @param string $table_key Table key. One of:
* 'authors',
* 'book_author_relationships',
* 'book_links',
* 'book_taxonomies',
* 'book_term_relationships',
* 'book_terms'
* 'books'
* 'book_meta',
* 'editions'
* 'reading_log'
* 'retailers',
* 'reviews',
* 'review_meta',
* 'series'
*
* @return BerlinDB\Database\Table|false
*/
public function get_table( $table_key ) {
return array_key_exists( $table_key, self::$instance->tables ) ? self::$instance->tables[ $table_key ] : false;
}
/**
* Get the HTML helper class
*
* @return HTML
*/
public function get_html() {
return $this->html;
}
/**
* Run installation
*
* - Install default taxonomies.
* - Add rewrite tags/rules and flush
*/
public function install() {
if ( ! get_option( 'bdb_run_activation' ) ) {
return;
}
/**
* Add default taxonomies.
*/
if ( ! $this->get_table( 'book_taxonomies' )->exists() ) {
$this->get_table( 'book_taxonomies' )->install();
}
$default_taxonomies = array(
'publisher' => array(
'slug' => 'publisher',
'name' => esc_html__( 'Publisher', 'book-database' ),
'format' => 'text' // text, checkbox
),
'genre' => array(
'slug' => 'genre',
'name' => esc_html__( 'Genre', 'book-database' ),
'format' => 'text'
),
'source' => array(
'slug' => 'source',
'name' => esc_html__( 'Source', 'book-database' ),
'format' => 'checkbox'
)
);
foreach ( $default_taxonomies as $taxonomy ) {
if ( get_book_taxonomy_by( 'slug', $taxonomy['slug'] ) ) {
continue;
}
try {
add_book_taxonomy( $taxonomy );
} catch ( Exception $e ) {
}
}
/**
* Add rewrite tags/rules and flush
*/
add_rewrite_tags();
add_rewrite_rules();
flush_rewrite_rules( true );
/**
* Add capabilities
*/
$role = get_role( 'administrator' );
foreach ( get_book_capabilities() as $capability ) {
$role->add_cap( $capability, true );
}
/**
* Set version number
*/
update_option( 'bdb_version', BDB_VERSION );
if ( ! get_option( 'bdb_install_date' ) ) {
update_option( 'bdb_install_date', date( 'Y-m-d H:i:s' ), false );
}
delete_option( 'bdb_run_activation' );
}
}
/**
* Require PHP 5.6+
*/
/**
* Insufficient PHP version notice.
*
* @return void
*/
function insufficient_php_version() {
?>
<div class="notice notice-error">
<p><?php printf( __( 'Book Database requires PHP version 7.0 or greater. You have version %s. Please contact your web host to upgrade your version of PHP.', 'book-database' ), PHP_VERSION ); ?></p>
</div>
<?php
}
/**
* Returns the main instance of Book_Database.
*
* @return Book_Database|void
*/
function book_database() {
if ( version_compare( PHP_VERSION, '7.0', '>=' ) ) {
return Book_Database::instance();
} else {
add_action( 'admin_notices', __NAMESPACE__ . '\insufficient_php_version' );
}
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\book_database', 4 );
/**
* On activation, create an option. We'll use this as a flag to actually run our activation later.
*
* @see Book_Database::install()
*
* @since 1.0
*/
function activate() {
add_option( 'bdb_run_activation', date( 'Y-m-d H:i:s' ) );
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\activate' );