forked from pressbooks/pressbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
325 lines (282 loc) · 6.61 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
325
<?php
/**
* Shortcuts for template designers who don't use real namespaces, and other helper functions.
*
* @author Pressbooks <[email protected]>
* @license GPLv3 (or any later version)
*/
if ( ! function_exists( 'app' ) ) {
/**
* Fake Laravel app() so we can use Blade @inject directive
*
* @see https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/helpers.php#L96
*
* @param string $abstract
* @param array $parameters
*
* @return mixed
*/
function app( $abstract = null, array $parameters = [] ) {
if ( is_null( $abstract ) ) {
return \Pressbooks\Container::getInstance();
}
return empty( $parameters )
? \Pressbooks\Container::getInstance()->make( $abstract )
: \Pressbooks\Container::getInstance()->makeWith( $abstract, $parameters );
}
}
/**
* 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( 'prev', true );
*
* @return int|false
*/
function pb_get_prev_post_id() {
return \Pressbooks\Book::get( 'prev', true );
}
/**
* 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( 'next', true );
*
* @return int|false
*/
function pb_get_next_post_id() {
return \Pressbooks\Book::get( 'next', true );
}
/**
* 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::get( 'first', true );
*
* @return int|false
*/
function pb_get_first_post_id() {
return \Pressbooks\Book::get( 'first', true );
}
/**
* 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();
*
* @deprecated Leftover code from old Custom CSS Editor. Use Custom Styles instead.
*
* @return bool
*/
function pb_is_custom_theme() {
return \Pressbooks\CustomCss::isCustomCss();
}
/**
* Shortcut to \Pressbooks\Container::get('Styles')->isCurrentThemeCompatible( $version );
*
* @return bool
*/
function pb_is_scss( $version = 1 ) {
if ( \Pressbooks\Container::get( 'Styles' )->isCurrentThemeCompatible( $version ) ) {
return true;
}
return false;
}
/**
* Shortcut to \Pressbooks\Metadata\get_seo_meta_elements();
*
* @deprecated 5.7.0
*
* @return string
*/
function pb_get_seo_meta_elements() {
_deprecated_function( 'pb_get_seo_meta_elements', '5.7.0' );
}
/**
* Shortcut to \Pressbooks\Metadata\get_microdata_elements();
*
* @deprecated 5.7.0
*
* @return string
*/
function pb_get_microdata_elements() {
_deprecated_function( 'pb_get_microdata_elements', '5.7.0' );
}
/**
* Get url to the custom stylesheet for web.
*
* @deprecated Leftover code from old Custom CSS Editor. Use Custom Styles instead.
*
* @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';
}
}
/**
* Get "real" chapter number for Web + REST API
*
* @param int $post_id
*
* @return int
*/
function pb_get_chapter_number( $post_id ) {
return \Pressbooks\Book::getChapterNumber( $post_id );
}
/**
* Get chapter, front or back matter type
*
* @param WP_Post $post
*
* @return string
*/
function pb_get_section_type( $post ) {
$type = $post->post_type;
$taxonomy = \Pressbooks\Taxonomy::init();
switch ( $type ) {
case 'chapter':
$type = $taxonomy->getChapterType( $post->ID );
break;
case 'front-matter':
$type = $taxonomy->getFrontMatterType( $post->ID );
break;
case 'back-matter':
$type = $taxonomy->getBackMatterType( $post->ID );
break;
case 'glossary':
$type = $taxonomy->getGlossaryType( $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 );
}
/**
* Returns an array of all subsections in the book, grouped by content type.
*
* @param array $book_structure The book structure from getBookStructure()
* @return array The subsections, grouped by parent post type
*/
function pb_get_all_subsections( $book_structure ) {
return \Pressbooks\Book::getAllSubsections( $book_structure );
}
/**
* Is the parse sections option true?
*
* @return boolean
*/
function pb_should_parse_subsections() {
return \Pressbooks\Modules\Export\Export::shouldParseSubsections();
}
/**
* 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 ) {
$tagged_content = \Pressbooks\Book::tagSubsections( $content, $id );
return ( $tagged_content === false ) ? $content : $tagged_content;
}
/**
* 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 );
}