/
totally-booked.php
405 lines (328 loc) · 11.1 KB
/
totally-booked.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
<?php
/**
Plugin Name: Totally Booked
Version: 0.6
Description: A plugin to help authors and book affiliates list their books quickly and easily in any WordPress website
Author: Ben Casey
Plugin URI: http://wordpress.org/extend/plugins/totally-booked/
Text Domain: totally-booked
Domain Path: /lang
FILTERS:
tb_options_capability
author_rewrite_slug
genre_rewrite_slug
series_rewrite_slug
book_rewrite_slug
tb_buynow_button_text
tb_find_store_popup_text
tb_book_shortcode_template_part
tb_series_shortcode_template_part
tb_genre_shortcode_template_part
*/
/**
* Class TotallyBooked
*
* @package totally-booked
*/
class TotallyBooked{
/**
* Administration Object
*/
var $admin = null;
/**
* Templating
*/
var $templating = null;
/**
* Base Constructor
*
* @since 0.1
*/
function __construct(){
//Include The Lib Files
$this->lib_includes();
//Run The Initial Setup Function
$this->initial_setup();
//Setup The Actions
$this->setup_actions();
//Setup The Filters
$this->setup_filters();
}
/**
* Run The Initial Setup Functionality
*
* @since 0.1
*/
public function initial_setup(){
register_activation_hook( __FILE__, array( 'TotallyBooked', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'TotallyBooked', 'deactivate' ) );
//Some Path Defines
define( 'TB_ABSPATH' , trailingslashit( WP_PLUGIN_DIR . '/' . str_replace(basename( __FILE__ ) , "" , plugin_basename( __FILE__ ) ) ) );
define( 'TB_URI' , trailingslashit( WP_PLUGIN_URL . '/' . str_replace(basename( __FILE__ ) , "" , plugin_basename( __FILE__ ) ) ) );
}
/**
* Includes The Lib Files
*
* @since 0.1
*/
public function lib_includes(){
require_once 'lib/post_types.php';
require_once 'lib/admin.php';
require_once 'lib/templating.php';
require_once 'lib/template_tags.php';
require_once 'lib/shortcodes.php';
require_once 'lib/widgets.php';
}
/**
* Sets Up The WordPress Actions Required For The Plugin To Function.
*
* @since 0.1
* @uses add_action
*/
public function setup_actions(){
//Init Functionality
add_action( 'init', array( $this, 'init' ) );
//Init Functionality
add_action( 'plugins_loaded' ,array( $this, 'plugins_loaded' ) );
//Pre Get posts
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
//Wrapper Start
add_action( 'tb_wrapper_start', array( $this, 'wrapper_start' ) );
//Wrapper End
add_action( 'tb_wrapper_end', array( $this, 'wrapper_end' ) );
}
/**
* Sets Up The WordPress Filters
*
* @since 0.1
*/
public function setup_filters(){
//Filter The Permalink HTML
add_filter( 'get_sample_permalink_html', array( $this, 'filter_permalink_html' ), 10, 4 );
add_filter( 'wpseo_metabox_prio', array( $this, 'filter_wpseo_metabox_prio' ) );
}
/**
* Function To Run On Init
*
* @since 0.1
*/
public function init(){
//Initialise The Post Types And Taxonomies
new TotallyBookedPostTypes();
//Setup The Enqueues
$this->plugin_enqueues();
}
/**
* Function To Run On plugins_loaded
*
* @since 0.1
*/
public function plugins_loaded(){
//Load Up The I18n
load_plugin_textdomain( 'totally-booked', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
// Initialise The Administration Functionality
$this->admin = new TotallyBookedAdmin();
//Initialise The Templating
$this->templating = new TotallyBookedTemplating();
}
/**
* Plugin Activation
*
* Flushes The Rewrite Rules after checking and handling an existing books page.
*
* @since 0.1
*/
public static function activate(){
global $wp_rewrite;
/*
* Test for an existing books page, if one exists, set the option for the books slug to be "tb-books" instead of "books"
*/
$args = array(
'name' => 'books',
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$pages = get_posts( $args );
if( $pages && ! empty( $pages ) ){
$old_opt = get_option( 'book_archive_slug' );
if( ! $old_opt || empty( $old_opt ) )
update_option( 'book_archive_slug', 'tb-books' );
}
/*
* Test For previous JS / CSS Options, If none, Add them in.
*/
$opts = array(
'tb_output_css' => 1,
'tb_output_js' => 1
);
foreach( $opts as $opt => $val ){
if( get_option( $opt ) === false )
update_option( $opt, $val );
}
//Flush The Rewrite Rules
new TotallyBookedPostTypes();
$wp_rewrite->flush_rules();
}
/**
* Deactivation Function
*
* @since 0.1
*/
public static function deactivate(){
}
/**
* Sets Up The Script And Style Enqueues For The Frontend.
*
* @since 0.1
*/
public function plugin_enqueues(){
if( is_admin() ) return;
$output_css = get_option( 'tb_output_css' );
if( is_string( $output_css ) && $output_css === '1' ){
wp_enqueue_style( 'totally-booked-css', TB_URI . 'assets/css/totally-booked.css' );
}
$output_js = get_option( 'tb_output_js' );
if( is_string( $output_js ) && $output_js === '1' ){
wp_enqueue_script( 'totally-booked-js', TB_URI . 'assets/js/totally-booked.js', array( 'jquery' ) );
}
}
/**
* Filters The Permalink HTML To Add In The Shortcode Snippet.
*
* @param $return
* @param $id
* @return string
* @since 0.1
*/
public function filter_permalink_html( $return, $id ){
if( get_post_type() !== 'tb_book' ) return $return;
return $return . '<br />' . __( 'Shortcode:', 'totally-booked' ) . '<code>[tb_book id="' . esc_attr( $id ) . '"]</code>';
}
/**
* Moves The WPSEO Metabox Lower.
*
* @return string
* @since 0.1
*/
public function filter_wpseo_metabox_prio(){
return 'low';
}
/**
* Runs Pre Get Posts Functionality.
*
* @param $query
* @since 0.1
*/
public function pre_get_posts( $query ){
/**
* Add Support for simple page ordering.
*/
if( class_exists( 'Simple_Page_Ordering' ) ){
if( is_post_type_archive( 'tb_book' ) || is_tax( 'tb_genre' ) || is_tax( 'tb_series' ) || is_tax( 'tb_author' ) ){
$query->set( 'orderby' , 'menu_order' );
$query->set( 'order' , 'ASC' );
}
}
/**
* Initialise the max posts option
*/
$maxposts = get_option( 'tb_archive_posts_per_page' );
if( $maxposts && $maxposts > 0 ){
if( is_post_type_archive( 'tb_book' ) || is_tax( array( 'tb_genre', 'tb_series', 'tb_author' ) ) ){
$query->set( 'posts_per_page', (int)$maxposts );
}
}
}
/**
* Returns an array of the link meta field details.
*
* @return array
*/
public function get_book_link_meta_details(){
return array(
'amazon_url' => array(
'title' => __( 'Amazon URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/amazon_icon.jpg',
'popup_class' => 'amazon'
),
'audible_url' => array(
'title' => __( 'Audible URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/audible_icon.jpg',
'popup_class' => 'audible'
),
'barnes_noble_url' => array(
'title' => __( 'Barnes And Noble URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/barnesandnoble_icon.jpg',
'popup_class' => 'barnes_noble'
),
'books_a_million_url' => array(
'title' => __( 'Books A Million URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/booksamillion_icon.jpg',
'popup_class' => 'booksamillion'
),
'christian_books_url' => array(
'title' => __( 'Christianbook URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/christianbook_icon.jpg',
'popup_class' => 'christian_books'
),
'googleplay_url' => array(
'title' => __( 'Google Play URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/googleplay_icon.jpg',
'popup_class' => 'googleplay'
),
'indiebound_url' => array(
'title' => __( 'Indiebound URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/indiebound_icon.jpg',
'popup_class' => 'indiebound'
),
'itunes_url' => array(
'title' => __( 'Itunes URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/itunes_icon.jpg',
'popup_class' => 'itunes'
),
'kobo_url' => array(
'title' => __( 'Kobo URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/kobo_icon.jpg',
'popup_class' => 'kobo'
),
'smashwords_url' => array(
'title' => __( 'Smashwords URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/smashwords_icon.jpg',
'popup_class' => 'itunes'
),
'sony_url' => array(
'title' => __( 'Sony Reader URL', 'totally-booked' ),
'icon_url' => TB_URI . 'assets/images/sony_icon.jpg',
'popup_class' => 'sonyreader'
)
);
}
/**
* Output the HTML in the options page if any entered instead of looking for a template file.
*
* @since 0.2
*/
public function wrapper_start(){
$wrapper_start = get_option( 'tb_wrapper_start' );
if( $wrapper_start && ! empty( $wrapper_start ) ){
echo $wrapper_start;
} else {
tb_get_template_part( 'parts/wrapper_start' );
}
}
/**
* Output the HTML in the options page if any entered instead of looking for a template file.
*
* @since 0.2
*/
public function wrapper_end(){
$wrapper_end = get_option( 'tb_wrapper_end' );
if( $wrapper_end && ! empty( $wrapper_end ) ){
echo $wrapper_end;
} else {
tb_get_template_part( 'parts/wrapper_end' );
}
}
}
$GLOBALS['TotallyBooked'] = new TotallyBooked();