-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
284 lines (243 loc) · 9.83 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
<?php
/**
* Include child theme stylesheets
*/
function enqueue_styles() {
// enqueue parent styles
wp_enqueue_style('parent-theme', get_template_directory_uri() .'/style.css');
// enqueue child styles
wp_enqueue_style('child-theme', get_stylesheet_directory_uri() .'/style.css', array('parent-theme'));
}
add_action('wp_enqueue_scripts', 'enqueue_styles');
/**
* Highlight search terms
*/
function highlight_search_term($text){
if(is_search()){
$keys = implode('|', explode(' ', get_search_query()));
$text = preg_replace('/(' . $keys .')/iu', '<span class="search-highlight">\0</span>', $text);
}
return $text;
}
add_filter('the_excerpt', 'highlight_search_term');
add_filter('the_title', 'highlight_search_term');
/**
* Render Previous and next buttons
*
* @param bool $echo
*/
function rula_pb_get_links( $echo = true ) {
global $first_chapter, $prev_chapter, $next_chapter;
$first_chapter = pb_get_first();
$prev_chapter = pb_get_prev();
$next_chapter = pb_get_next();
if ( $echo ) {
echo '<div class="nav">';
if ($prev_chapter != '/' ) {
echo '<span class="previous"><a href="'. $prev_chapter .'">Previous page</a></span>';
}
if ($next_chapter != '/' ) {
echo '<span class="next"><a href="'. $next_chapter .'">Next page</a></span>';
}
echo '</div>';
}
}
/**
* Returns an html blog of meta elements
*
* @return string $html metadata
*/
function pbt_get_seo_meta_elements() {
// map items that are already captured
$meta_mapping = array(
'citation_title' => 'pb_title',
'citation_author' => 'pb_authors_file_as',
'citation_language' => 'pb_language',
'citation_keywords' => 'pb_keywords_tags',
'citation_pdf_url' => pbt_get_citation_pdf_url(),
'citation_publication_date' => 'pb_publication_date',
);
$html = "<meta name='application-name' content='Pressbooks'>\n";
$metadata = \Pressbooks\Book::getBookInformation();
// create meta elements
foreach ( $meta_mapping as $name => $content ) {
if ( array_key_exists( $content, $metadata ) ) {
$html .= "<meta name='" . $name . "' content='" . $metadata[ $content ] . "'>\n";
} elseif ( 'citation_pdf_url' == $name ) {
$html .= "<meta name='" . $name . "' content='" . $content . "'>\n";
}
}
return $html;
}
function pbt_get_citation_pdf_url() {
$url = '';
$domain = site_url();
if ( method_exists( '\Pressbooks\Utility', 'latest_exports' ) ) {
$files = \Pressbooks\Utility\latest_exports();
$options = get_option( 'pbt_redistribute_settings' );
if ( ! empty( $files ) && ( true == $options['latest_files_public'] ) ) {
foreach ( $files as $filetype => $filename ) {
if ( 'pdf' == $filetype || 'mpdf' == $filetype ) {
$filename = preg_replace( '/(-\d{10})(.*)/ui', "$1", $filename );
// rewrite rule
$url = $domain . "/open/download?filename={$filename}&type={$filetype}";
}
}
}
}
return $url;
}
function pbt_get_microdata_meta_elements() {
// map items that are already captured
$html = '';
// add elements that aren't captured, and don't need user input
$edu_align = ( isset( $metadata['pb_bisac_subject'] ) ) ? $metadata['pb_bisac_subject'] : '';
$lrmi_meta = array(
'educationalAlignment' => $edu_align,
'educationalUse' => 'Open textbook study',
'audience' => 'student',
'interactivityType' => 'mixed',
'learningResourceType' => 'textbook',
'typicalAgeRange' => '17-',
);
foreach ( $lrmi_meta as $itemprop => $content ) {
// @todo parse educationalAlignment items into alignmentOjects
$html .= "<meta itemprop='" . $itemprop . "' content='" . $content . "' id='" . $itemprop . "'>\n";
}
return $html;
}
/**
* Modifies 'chapters' to 'page' for text processed in __() to avoid confusion.
* Lightly modified function, original author Lumen Learning
* https://github.com/lumenlearning/candela
*
*
* @param type $translated
* @param type $original
* @param type $domain
*
* @return type
*/
function pbt_terminology_modify( $translated, $original, $domain ) {
if ( 'pressbooks' == $domain ) {
$modify = array(
"Chapter Metadata" => "Page Metadata",
"Chapter Short Title (appears in the PDF running header)" => "Page Short Title (appears in the PDF running header)",
"Chapter Subtitle (appears in the Web/ebook/PDF output)" => "Page Subtitle (appears in the Web/ebook/PDF output)",
"Chapter Author (appears in Web/ebook/PDF output)" => "Page Author (appears in Web/ebook/PDF output)",
"Chapter Copyright License (overrides book license on this page)" => "Page Copyright License (overrides book license on this page)",
"Promote your book, set individual chapters privacy below." => "Promote your book, set individual page's privacy below.",
"Add Chapter" => "Add Page",
"Reordering the Chapters" => "Reordering the Pages",
"Chapter 1" => "Page 1",
"Imported %s chapters." => "Imported %s pages.",
"Chapters" => "Pages",
"Chapter" => "Page",
"Add New Chapter" => "Add New Page",
"Edit Chapter" => "Edit Page",
"New Chapter" => "New Page",
"View Chapter" => "View Page",
"Search Chapters" => "Search Pages",
"No chapters found" => "No pages found",
"No chapters found in Trash" => "No pages found in Trash",
"Chapter numbers" => "Page numbers",
"display chapter numbers" => "display page numbers",
"do not display chapter numbers" => "do not display page numbers",
"Chapter Numbers" => "Page Numbers",
"Display chapter numbers" => "Display page numbers",
"This is the first chapter in the main body of the text. You can change the " => "This is the first page in the main body of the text. You can change the ",
"text, rename the chapter, add new chapters, and add new parts." => "text, rename the page, add new pages, and add new parts.",
"Only users you invite can see your book, regardless of individual chapter " => "Only users you invite can see your book, regardless of individual page ",
);
if ( isset( $modify[ $original ] ) ) {
$translated = $modify[ $original ];
}
}
return $translated;
}
/**
* Modifies 'chapter' to 'page' for text processed in _x()
* Lightly modified function, original author Lumen Learning
* https://github.com/lumenlearning/candela
*
* @param type $translated
* @param type $original
* @param type $context
* @param type $domain
*
* @return type
*/
function pbt_terminology_modify_context( $translated, $original, $context, $domain ) {
if ( 'pressbooks' == $domain && 'book' == $context ) {
$translated = pbt_terminology_modify( $translated, $original, $domain );
}
return $translated;
}
/**** Removing these filters Jume 2015 to remain consistent with the PB documentation *****/
//add_filter( 'gettext', 'pbt_terminology_modify', 11, 3 );
//add_filter( 'gettext_with_context', 'pbt_terminology_modify_context', 11, 4 );
// removes incorrect notice on epub/pdf export that the book was created on pressbooks.com
$GLOBALS['PB_SECRET_SAUCE']['TURN_OFF_FREEBIE_NOTICES_EPUB'] = 'not_created_on_pb_com';
$GLOBALS['PB_SECRET_SAUCE']['TURN_OFF_FREEBIE_NOTICES_PDF'] = 'not_created_on_pb_com';
/**
*
* @staticvar array $searches
*
* @param type $content
*
* @return type
*/
function pbt_fix_img_relative( $content ) {
static $searches = array(
'#<(?:img) .*?src=[\'"]\Khttp://[^\'"]+#i', // fix image and iframe elements
);
$content = preg_replace_callback( $searches, 'pbt_fix_img_relative_callback', $content );
return $content;
}
/**
*
* @param type $matches
*
* @return type
*/
function pbt_fix_img_relative_callback( $matches ) {
$avoid = 'http://s.wordpress.com';
$protocol = '';
if ( 0 === strcmp( $avoid, substr( $matches[0], 0, 22 ) ) ) {
$protocol = $matches[0];
} else {
$protocol = '' . substr( $matches[0], 5 );
}
return $protocol;
}
if ( ! empty( $_SERVER['HTTPS'] ) ) {
add_filter( 'the_content', 'pbt_fix_img_relative', 9999 );
}
/**
* adds metadata to head element
*/
function pbt_add_metadata() {
if ( is_front_page() ) {
echo pbt_get_seo_meta_elements();
echo pbt_get_microdata_meta_elements();
} else {
echo pbt_get_microdata_meta_elements();
}
}
add_action( 'wp_head', 'pbt_add_metadata' );
/**
* adds content to the footer
*/
function pbt_add_openstax() {
// tmp fix, rush job
$openstax = get_bloginfo( 'url' );
if ( 'https://opentextbc.ca/anatomyandphysiology' == $openstax ) {
echo "<small class='aligncenter'>";
__( 'Download for free at http://cnx.org/contents/[email protected]', 'pressbooks-textbook' );
echo "</small>";
}
}
add_action( 'wp_footer', 'pbt_add_openstax' );
add_filter( 'pressbooks_download_tracking_code', function ( $tracking, $filetype ) {
return "_paq.push(['trackEvent','exportFiles','Downloads','{$filetype}']);";
}, 10, 2 );