forked from bpiwowar/papercite
-
Notifications
You must be signed in to change notification settings - Fork 6
/
papercite-wp-plugin.php
286 lines (225 loc) · 8.75 KB
/
papercite-wp-plugin.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
<?php
/**
* Plugin Name: Papercite
* Plugin URI: http://www.bpiwowar.net/papercite
* Description: papercite enables to add BibTeX entries formatted as HTML in wordpress pages and posts. The input data is the bibtex text file and the output is HTML. This fork adds the feature of textual footnotes, besides the references stored in bibtex files.
* Version: 0.6.0
* Author: Benjamin Piwowarski
* Author URI: http://www.bpiwowar.net
* Author: digfish
* Author URI: http://digfish.org
* License: GPL3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: papercite
* Domain Path: /languages
*
* @package papercite
*/
// isolate papercite class in their own class file, keeping only the wordpress integtation
// in this file
require_once( "wp-papercite.php" );
// Options
include( "papercite_options.php" );
// -------------------- Interface with WordPress
// --- Head of the HTML ----
function papercite_head() {
if ( ! function_exists( 'wp_enqueue_script' ) ) {
// In case there is no wp_enqueue_script function (WP < 2.6), we load the javascript ourselves
echo "\n" . '<script src="' . get_bloginfo( 'wpurl' ) . '/wp-content/plugins/papercite/js/jquery.js" type="text/javascript"></script>' . "\n";
echo '<script src="' . get_bloginfo( 'wpurl' ) . '/wp-content/plugins/papercite/js/papercite.js" type="text/javascript"></script>' . "\n";
}
}
// --- Initialise papercite ---
function papercite_init() {
global $papercite;
if ( function_exists( 'wp_enqueue_script' ) ) {
wp_register_script( 'papercite', plugins_url( 'papercite/js/papercite.js' ), array( 'jquery' ) );
wp_enqueue_script( 'papercite' );
// register the scripts for the post editor
if ( is_admin() ) {
// wp_register_script( 'papercite-post-editor', plugins_url( 'papercite/js/ppc_post_editor.js' ), array( 'wp-blocks' ) );
// wp_enqueue_script( 'papercite-post-editor' );
wp_enqueue_editor();
wp_enqueue_script( 'jquery-ui-dialog' );
wp_enqueue_style( 'wp-jquery-ui-dialog');
}
}
// Register and enqueue the stylesheet
wp_register_style( 'papercite_css', plugins_url( 'papercite/papercite.css' ) );
wp_enqueue_style( 'papercite_css' );
// wp_register_style( 'ppc-post-editor-css', plugins_url( 'papercite/ppc_post_editor.css' ) );
wp_enqueue_style( 'ppc-post-editor-css' );
// Initialise the singleton
$papercite = new WpPapercite();
}
// --- Callback function ----
/**
* @param $myContent
*
* @return mixed|string|string[]|null
*/
function &papercite_cb( $myContent ) {
// Init
$papercite = &$GLOBALS["papercite"];
// Fixes issue #39 (maintenance mode support)
if ( ! is_object( $papercite ) ) {
return $myContent;
}
$papercite->init();
// Database support if needed
if ( $papercite->options["use_db"] ) {
require_once( dirname( __FILE__ ) . "/papercite_db.php" );
}
// (0) Skip processing on this page?
if ( $papercite->options['skip_for_post_lists'] && ! is_single() && ! is_page() ) {
// return preg_replace("/\[\s*((?:\/)bibshow|bibshow|bibcite|bibtex|ppcnote)(?:\s+([^[]+))?]/", '', $myContent);
$replaced_content = preg_replace( "/\[\s*((?:\/)bibshow|bibshow|bibcite|bibtex)(?:\s+([^[]+))?]/", '', $myContent );
// remove the shortcodes for textual footnotes in the post list or main page if the user set to do that
$replaced_content = preg_replace( "/\[ppcnote\](.+?)\[\/ppcnote\]/i", '', $replaced_content );
return $replaced_content;
}
// (1) First phase - handles everything but bibcite keys
$text = preg_replace_callback(
// "/\[\s*((?:\/)bibshow|bibshow|bibcite|bibtex|bibfilter|ppcnote)(?:\s+([^[]+))?]/",
"/\[\s*((?:\/)bibshow|bibshow|bibcite|bibtex|bibfilter)(?:\s+([^[]+))?]/",
array( $papercite, "process" ),
$myContent
);
$post_id = get_the_ID();
$text = preg_replace_callback(
'/\[ppcnote\](.+?)\[\/ppcnote\]/i',
function ( $match ) use ( $post_id, $papercite ) {
return $papercite->processTextualFootnotes( $match, $post_id );
},
$text
);
if ( count( $papercite->getTextualFootnotes() ) > 0 ) {
$text .= $papercite->showTextualFootnotes( get_the_ID() );
}
// digfish: reset the footnotes after the end of post/page
$papercite->textual_footnotes = array();
$papercite->textual_footnotes_counter = 0;
// (2) Handles missing bibshow tags
while ( sizeof( $papercite->bibshows ) > 0 ) {
$text .= $papercite->end_bibshow();
}
// (3) Handles custom keys in bibshow and return
$text = str_replace( $papercite->keys, $papercite->keyValues, $text );
// (4) add CSS styles to the markup if there is some
$text = renderCssStyles( $text );
return $text;
}
function renderCssStyles( $text = '' ) {
global $papercite;
if ( ! empty( $papercite->cssStyles ) ) {
$text = "<style type='text/css' rel='stylesheet' id='citeproc-styles'>\n{$papercite->cssStyles}</style> $text";
}
return $text;
}
// --- Add the documentation link in the plugin list
function papercite_row_cb( $data, $file ) {
if ( $file == "papercite/papercite-wp-plugin.php" ) {
$data[] = "<a href='" . WP_PLUGIN_URL . "/papercite/documentation/index.html'>Documentation</a>";
}
return $data;
}
add_filter( 'plugin_row_meta', 'papercite_row_cb', 1, 2 );
// --- Register the MIME type for Bibtex files
function papercite_mime_types( $mime_types ) {
// Adjust the $mime_types, which is an associative array where the key is extension and value is mime type.
$mime_types['bib'] = 'application/x-bibtex'; // Adding bibtex
return $mime_types;
}
add_filter( 'upload_mimes', 'papercite_mime_types', 1, 1 );
/**
* by digfish (09 Apr 2019)
* Restore .bib upload functionality in Media Library for WordPress 4.9.9 and up
* adapted from https://gist.github.com/rmpel/e1e2452ca06ab621fe061e0fde7ae150
*/
add_filter( 'wp_check_filetype_and_ext', function ( $values, $file, $filename, $mimes ) {
if ( extension_loaded( 'fileinfo' ) ) {
// with the php-extension, a bib file is issues type text/plain so we fix that back to
// application/x-bibtex by trusting the file extension.
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$real_mime = finfo_file( $finfo, $file );
finfo_close( $finfo );
if ( $real_mime === 'text/plain' && preg_match( '/\.(bib)$/i', $filename ) ) {
$values['ext'] = 'bib';
$values['type'] = 'application/x-bibtex';
}
} else {
// without the php- extension, we probably don't have the issue at all, but just to be sure...
if ( preg_match( '/\.(bib)$/i', $filename ) ) {
$values['ext'] = 'bib';
$values['type'] = 'application/x-bibtex';
}
}
return $values;
}, PHP_INT_MAX, 4 );
// digfish --- add the sidebard (metabox) to the post edit with the bibliography items
function papercite_render_metabox() {
global $papercite;
require_once WP_PLUGIN_DIR . "/papercite/lib/BibTex_pear.php";
if ( empty( $papercite->options ) ) {
$papercite->init();
}
$entries = $papercite->getEntries( $papercite->options );
array_shift( $entries );
//$entries = array_slice( $entries, 0, 100 );
//d($entries[0]);
echo "<p><INPUT type='text' id='papercite-entries-search' name='papercite-entries-search' value='Enter key author name, etc. to search'></p>";
echo "<UL id='papercite-metabox-content' style=''>";
foreach ( $entries as $entry ) {
//var_dump($entry['author']->creators);
$key = $entry['cite'];
if (!empty($entry['author'])) {
$authors = join( ";", array_map( function ( $creator ) {
return $creator['surname'] . "," . $creator['firstname'];
}, (array) $entry['author']->creators ) );
} else {
$authors = "s.a.";
}
$year = empty( $entry['year'] ) ? "s.d." : $entry['year'];
$title = $entry['title'];
echo "<LI class='papercite-metabox-bibentry'><button>[bibcite key=$key]</button> $authors ($year) - $title </LI>";
}
echo "</UL>";
}
function papercite_metabox( $post_type ) {
add_meta_box( 'papercite-metabox', 'Papercite', 'papercite_render_metabox', 'post', 'side', 'default' );
}
add_action( 'add_meta_boxes', 'papercite_metabox' );
add_action( 'wp_ajax_search_citations', function () {
$papercite = &$GLOBALS["papercite"];
$entries = $papercite->getEntries( $papercite->options );
$q = $_REQUEST['q'];
$found = array();
foreach ( $entries as $entry ) {
if ( stristr( $entry['cite'], $q ) !== false ) {
$found[] = $entry;
}
}
header( "Content-type: application/json" );
echo json_encode( $found );
wp_die();
} );
add_action( 'wp_ajax_list_styles', function () {
global $papercite;
$type = $_REQUEST['type'];
$list = papercite_list_formats();
switch ( $type ) {
case 'tpl':
$list = papercite_list_formats();
break;
case 'csl':
$list = papercite_list_csl_formats();
break;
}
header( "Content-type: application/json" );
echo json_encode( $list );
wp_die();
} );
// --- Add the different handlers to WordPress ---
add_action( 'init', 'papercite_init' );
add_action( 'wp_head', 'papercite_head' );
add_filter( 'the_content', 'papercite_cb', - 1 );