forked from BCcampus/pressbooks-textbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pressbooks-textbook.php
389 lines (326 loc) · 9.86 KB
/
pressbooks-textbook.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
<?php
/**
* PressBooks Textbook
*
* @package PressBooks_Textbook
* @author Brad Payne <[email protected]>
* @license GPL-2.0+
* @copyright 2014 Brad Payne
*
* @wordpress-plugin
* Plugin Name: PressBooks Textbook
* Description: A plugin that extends PressBooks for textbook authoring
* Version: 1.2.5
* Author: Brad Payne
* Author URI: http://bradpayne.ca
* Text Domain: pressbooks-textbook
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
* GitHub Plugin URI: https://github.com/BCcampus/pressbooks-textbook
*/
namespace PBT;
use PBT\Admin;
// If file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die();
}
class Textbook {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
* @var string
*/
const VERSION = '1.2.5';
/**
* Unique identifier for plugin.
*
* @since 1.0.0
* @var string
*/
protected $plugin_slug = 'pressbooks-textbook';
/**
* Instance of this class.
*
* @since 1.0.0
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin by setting localization and loading public scripts
* and styles.
*
* @since 1.0.1
*/
private function __construct() {
// Define plugin constants
if ( ! defined( 'PBT_PLUGIN_DIR' ) )
define( 'PBT_PLUGIN_DIR', __DIR__ . '/' );
if ( ! defined( 'PBT_PLUGIN_URL' ) )
define( 'PBT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
// Load translations
add_action( 'init', array( $this, 'loadPluginTextDomain' ) );
// Setup our activation and deactivation hooks
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
// Hook in our pieces
add_action( 'plugins_loaded', array( &$this, 'includes' ) );
add_action( 'init', array( &$this, 'pbtInit' ) );
add_action( 'template_redirect', '\PBT\Rewrite\do_open', 0 );
add_action( 'wp_enqueue_scripts', array( &$this, 'enqueueChildThemes' ) );
add_filter( 'allowed_themes', array( &$this, 'filterChildThemes' ), 11 );
add_action( 'pressbooks_new_blog', array( $this, 'newBook' ) );
// include other functions
require( PBT_PLUGIN_DIR . 'includes/pbt-utility.php' );
require( PBT_PLUGIN_DIR . 'includes/pbt-rewrite.php' );
wp_cache_add_global_groups( array( 'pbt' ) );
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Include our plugins
*
* @since 1.0.8
*/
function includes() {
$pbt_plugin = array(
'mce-table-buttons/mce_table_buttons.php' => 1,
'mce-textbook-buttons/mce-textbook-buttons.php' => 1,
'creative-commons-configurator-1/cc-configurator.php' => 1,
'hypothesis/hypothesis.php' => 1,
'tinymce-spellcheck/tinymce-spellcheck.php' => 1,
'disable-comments/disable-comments.php' => 1,
);
$pbt_plugin = $this->filterPlugins( $pbt_plugin );
// include plugins
if ( ! empty( $pbt_plugin ) ) {
foreach ( $pbt_plugin as $key => $val ) {
require_once( PBT_PLUGIN_DIR . 'symbionts/' . $key);
}
}
}
/**
* Filters out active plugins, to avoid collisions with plugins already installed
*
* @since 1.0.8
* @param array $pbt_plugin
* @return array
*/
private function filterPlugins( $pbt_plugin ) {
$already_active = get_option('active_plugins');
$network_already_active = get_site_option('active_sitewide_plugins');
// activate only if one of our themes is being used
if ( false == self::isTextbookTheme() ) {
unset( $pbt_plugin['mce-textbook-buttons/mce-textbook-buttons.php'] );
unset( $pbt_plugin['hypothesis/hypothesis.php'] );
unset( $pbt_plugin['creative-commons-configurator-1/cc-configurator.php'] );
unset( $pbt_plugin['mce-table-buttons/mce_table_buttons.php'] );
unset( $pbt_plugin['tinymce-spellcheck/tinymce-spellcheck.php'] );
unset( $pbt_plugin['disable-comments/disable-comments.php'] );
}
// don't include plugins already active at the site level, network level
if ( ! empty( $pbt_plugin ) ) {
foreach ( $pbt_plugin as $key => $val ) {
if ( in_array( $key, $already_active ) || array_key_exists( $key, $network_already_active )) {
unset( $pbt_plugin[$key] );
}
}
}
// don't include plugins if the user doesn't want them
if ( ! empty( $pbt_plugin ) ) {
// get user options
$user_options = $this->getUserOptions();
if ( is_array( $user_options ) ) {
foreach ( $pbt_plugin as $key => $val ) {
$name = strstr( $key, '/', true );
$pbt_option = "pbt_" . $name . "_active";
// either it doesn't exist, or the client doesn't want it
if ( array_key_exists( $pbt_option, $user_options ) ) {
// check the value
if ( false == $user_options[$pbt_option] ) {
unset( $pbt_plugin[$key] );
}
}
}
}
}
return $pbt_plugin;
}
/**
* Returns merged array of all PBT user options
*
* @since 1.0.2
* @return array
*/
private function getUserOptions() {
$result = array();
( array ) $other = get_option( 'pbt_other_settings' );
( array ) $reuse = get_option( 'pbt_reuse_settings' );
( array ) $redistribute = get_option( 'pbt_redistribute_settings' );
$result = @array_merge( $other, $reuse, $redistribute );
return $result;
}
/**
* Checks to see if one of our child themes is active
*
* @return boolean
*/
static function isTextbookTheme() {
$t = wp_get_theme()->Tags;
if ( is_array( $t ) && in_array( 'Pressbooks Textbook', $t ) ) {
return true;
}
return false;
}
/**
* Register all scripts and styles
*
* @since 1.0.1
*/
function pbtInit() {
// Register theme directory
register_theme_directory( PBT_PLUGIN_DIR . 'themes-book' );
// Add a rewrite rule for the keyword "open"
add_rewrite_endpoint( 'open', EP_ROOT );
// Flush, if we haven't already
\PBT\Rewrite\flusher();
}
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
*/
function activate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
// @TODO - update timezone and tagline
// update_option('blogdescription', 'The Open Textbook Project provides flexible and affordable access to higher education resources');
add_site_option( 'pressbooks-textbook-activated', true );
}
/**
* Fired when the plugin is deactivated.
*
* @since 1.0.0
*/
function deactivate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
delete_site_option( 'pressbooks-textbook-activated' );
}
/**
* Return the plugin slug.
*
* @since 1.0.0
* @return Plugin slug variable.
*/
function getPluginSlug() {
return $this->plugin_slug;
}
/**
* Load the plugin text domain for translation.
*
* @since 1.0.0
*/
function loadPluginTextDomain() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, FALSE, basename( plugin_dir_path( dirname( __FILE__ ) ) ) . '/languages/' );
}
/**
* Queue child theme
*
* @since 1.0.0
*/
function enqueueChildThemes() {
wp_register_style( 'open-textbook', PBT_PLUGIN_URL . 'themes-book/opentextbook/style.css', array( 'pressbooks' ), self::VERSION, 'screen' );
wp_enqueue_style( 'pressbooks-book' );
wp_enqueue_style( 'open-textbook' );
}
/**
* Pressbooks filters allowed themes, this adds our themes to the list
*
* @since 1.0.7
* @param array $themes
* @return array
*/
function filterChildThemes( $themes ) {
$pbt_themes = array();
if ( \Pressbooks\Book::isBook() ) {
$registered_themes = search_theme_directories();
foreach ( $registered_themes as $key => $val ) {
if ( $val['theme_root'] == PBT_PLUGIN_DIR . 'themes-book' ) {
$pbt_themes[$key] = 1;
}
}
// add our theme
$themes = array_merge( $themes, $pbt_themes );
return $themes;
} else {
return $themes;
}
}
/**
* This function is added to the PB hook 'pressbooks_new_blog' to add some time
* saving customizations
*
* @since 1.2.1
* @see pressbooks/includes/class-pb-activation.php
*
*/
function newBook() {
$display_copyright = array(
'copyright_license' => 1,
);
$pdf_options = array(
'pdf_page_size' => 3,
'pdf_blankpages' => 2,
);
$epub_compress_images = array(
'ebook_compress_images' => 1
);
// // set the default theme to opentextbooks
// switch_theme( 'opentextbook' );
// check_theme_switched();
//
// // safety
// if ( ( get_option( 'template' ) != 'pressbooks-book' ) || ( get_option( 'stylesheet' ) != 'opentextbook' ) ) {
// update_option( 'template', 'pressbooks-book' );
// update_option( 'stylesheet', 'opentextbook' );
// };
// send validation logs
update_option( 'pressbooks_email_validation_logs', 1 );
// set display copyright information to on
update_option( 'pressbooks_theme_options_global', $display_copyright );
// choose 'US Letter size' for PDF exports
update_option( 'pressbooks_theme_options_pdf', $pdf_options );
// EPUB export - reduce image size and quality
update_option( 'pressbooks_theme_options_ebook', $epub_compress_images );
// modify the book description
update_option( 'blogdescription', __( 'Open Textbook', $this->plugin_slug ) );
}
}
// Prohibit installation if PB is not installed
if ( get_site_option( 'pressbooks-activated' ) ) {
if ( is_admin() ) {
require (dirname( __FILE__ ) . '/admin/class-pbt-textbook-admin.php');
$pbt = new Admin\TextbookAdmin;
} else {
$pbt = \PBT\Textbook::get_instance();
}
}