forked from pressbooks/pressbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
324 lines (270 loc) · 6.39 KB
/
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
<?php
/**
* Shortcuts for template designers who don't use real namespaces, and other helper functions.
*
* @author Pressbooks <[email protected]>
* @license GPLv2 (or any later version)
*/
/**
* Shortcut to \Pressbooks\Book::get( 'prev' );
*
* @return string URL of previous post
*/
function pb_get_prev() {
return \Pressbooks\Book::get( 'prev' );
}
/**
* Shortcut to \Pressbooks\Book::get( 'next' );
*
* @return string URL of next post
*/
function pb_get_next() {
return \Pressbooks\Book::get( 'next' );
}
/**
* Shortcut to \Pressbooks\Book::get( 'first' );
*
* @return string URL of first post
*/
function pb_get_first() {
return \Pressbooks\Book::get( 'first' );
}
/**
* Shortcut to \Pressbooks\Book::getBookInformation();
*
* @return array
*/
function pb_get_book_information() {
return \Pressbooks\Book::getBookInformation();
}
/**
* Shortcut to \Pressbooks\Book::getBookStructure();
*
* @return array
*/
function pb_get_book_structure() {
return \Pressbooks\Book::getBookStructure();
}
/**
* Shortcut to \Pressbooks\Sanitize\decode();
*
* @param $val
*
* @return mixed
*/
function pb_decode( $val ) {
return \Pressbooks\Sanitize\decode( $val );
}
/**
* Shortcut to \Pressbooks\Sanitize\strip_br();
*
* @param $val
*
* @return string
*/
function pb_strip_br( $val ) {
return \Pressbooks\Sanitize\strip_br( $val );
}
/**
* Shortcut to \Pressbooks\CustomCss::isCustomCss();
*
* @return bool
*/
function pb_is_custom_theme() {
return \Pressbooks\CustomCss::isCustomCss();
}
/**
* Shortcut to \Pressbooks\Container::get('Sass')->isCurrentThemeCompatible( $version );
*
* @return bool
*/
function pb_is_scss( $version = 1 ) {
if ( \Pressbooks\Container::get( 'Sass' )->isCurrentThemeCompatible( $version ) ) {
return true;
}
return false;
}
/**
* Shortcut to \Pressbooks\Metadata::getSeoMetaElements();
*
* @return string
*/
function pb_get_seo_meta_elements() {
return \Pressbooks\Metadata::getSeoMetaElements();
}
/**
* Shortcut to \Pressbooks\Metadata::getMicrodataElements();
*
* @return string
*/
function pb_get_microdata_elements() {
return \Pressbooks\Metadata::getMicrodataElements();
}
/**
* Get url to the custom stylesheet for web.
*
* @see: \Pressbooks\CustomCss
* @return string
*/
function pb_get_custom_stylesheet_url() {
$current_blog_id = get_current_blog_id();
if ( is_file( WP_CONTENT_DIR . "/blogs.dir/{$current_blog_id}/files/custom-css/web.css" ) ) {
return \Pressbooks\Sanitize\maybe_https( content_url( "/blogs.dir/{$current_blog_id}/files/custom-css/web.css" ) );
} elseif ( is_file( WP_CONTENT_DIR . "/uploads/sites/{$current_blog_id}/custom-css/web.css" ) ) {
return \Pressbooks\Sanitize\maybe_https( content_url( "/uploads/sites/{$current_blog_id}/custom-css/web.css" ) );
} else {
return PB_PLUGIN_URL . 'themes-book/pressbooks-custom-css/style.css';
}
}
/**
* Check if custom stylesheet for web already imports pressbooks-book/style.css
*
* @see: \Pressbooks\CustomCss
* @return bool
*/
function pb_custom_stylesheet_imports_base() {
$current_blog_id = get_current_blog_id();
$custom_file = false;
$_res = false;
if ( is_file( WP_CONTENT_DIR . "/blogs.dir/{$current_blog_id}/files/custom-css/web.css" ) ) {
$custom_file = WP_CONTENT_DIR . "/blogs.dir/{$current_blog_id}/files/custom-css/web.css";
} elseif ( is_file( WP_CONTENT_DIR . "/uploads/sites/{$current_blog_id}/custom-css/web.css" ) ) {
$custom_file = WP_CONTENT_DIR . "/uploads/sites/{$current_blog_id}/custom-css/web.css";
}
if ( $custom_file ) {
$custom_file_contents = file_get_contents( $custom_file, null, null, null, 2600 );
$import_pattern = '#@import(\s+)url\(([\s])?([\"|\'])?(.*?)themes-book/pressbooks-book/style\.css([\"|\'])?([\s])?\)#i';
if ( preg_match( $import_pattern, $custom_file_contents ) ) {
$_res = true;
}
}
return $_res;
}
/**
* Get "real" chapter number
*
* @param $post_name
*
* @return int
*/
function pb_get_chapter_number( $post_name ) {
$options = get_option( 'pressbooks_theme_options_global' );
if ( ! @$options['chapter_numbers'] ) {
return 0;
}
$lookup = \Pressbooks\Book::getBookStructure();
$lookup = $lookup['__export_lookup'];
if ( 'chapter' != @$lookup[ $post_name ] ) {
return 0;
}
$i = 0;
foreach ( $lookup as $key => $val ) {
if ( 'chapter' == $val ) {
$chapter = get_posts( array( 'name' => $key, 'post_type' => 'chapter', 'post_status' => 'publish', 'numberposts' => 1 ) );
if ( isset( $chapter[0] ) ) {
$type = pb_get_section_type( $chapter[0] );
if ( 'numberless' !== $type ) {
++$i;
}
} else {
return 0;
}
if ( $key == $post_name ) {
break;
}
}
}
if ( 'numberless' == $type ) {
$i = 0;
}
return $i;
}
/**
* Get chapter, front or back matter type
*
* @param $post
*
* @return string
*/
function pb_get_section_type( $post ) {
$type = $post->post_type;
switch ( $type ) {
case 'chapter':
$type = \Pressbooks\Taxonomy::getChapterType( $post->ID );
break;
case 'front-matter':
$type = \Pressbooks\Taxonomy::getFrontMatterType( $post->ID );
break;
case 'back-matter':
$type = \Pressbooks\Taxonomy::getBackMatterType( $post->ID );
break;
}
return $type;
}
/**
* Returns an array of subsections in front matter, back matter, or chapters.
*
* @param $id
*
* @return array
*/
function pb_get_subsections( $id ) {
return \Pressbooks\Book::getSubsections( $id );
}
/**
* Alias for pb_get_subsections.
*
* @param $id
*
* @return array
*/
function pb_get_sections( $id ) {
return pb_get_subsections( $id );
}
/**
* Is the parse sections option true?
*
* @return boolean
*/
function pb_should_parse_subsections() {
return \Pressbooks\Modules\Export\Export::isParsingSubsections();
}
/**
* Alias for pb_should_parse_subsections.
*
* @return boolean
*/
function pb_should_parse_sections() {
return pb_should_parse_subsections();
}
/**
* Tag the subsections
*
* @param $content string
*
* @return string
*/
function pb_tag_subsections( $content, $id ) {
return \Pressbooks\Book::tagSubsections( $content, $id );
}
/**
* Alias for pb_tag_subsections.
*
* @param $content string
*
* @return string
*/
function pb_tag_sections( $content, $id ) {
return pb_tag_subsections( $content, $id );
}
/**
* Rename image with arbitrary suffix (before extension)
*
* @param $thumb
* @param $path
*
* @return string
*/
function pb_thumbify( $thumb, $path ) {
return \PressBooks\Image\thumbify( $thumb, $path );
}