forked from wp-plugins/fd-footnotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdfootnotes.php
164 lines (132 loc) · 5.63 KB
/
fdfootnotes.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
<?php
/*
Plugin Name: FD Footnotes
Plugin URI: http://flagrantdisregard.com/footnotes-plugin
Description: Elegant and easy to use footnotes
Author: John Watson
Version: 1.35
Author URI: http://flagrantdisregard.com
Copyright (C) 2008 John Watson
http://flagrantdisregard.com/footnotes-plugin
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
define('FDFOOTNOTE_TEXTDOMAIN', 'fdfootnote');
if (function_exists('load_plugin_textdomain')) {
load_plugin_textdomain(FDFOOTNOTE_TEXTDOMAIN, false, dirname(plugin_basename(__FILE__)).'/languages' );
}
add_action('admin_menu', 'fdfootnote_config_page');
add_action('wp_enqueue_scripts', 'fdfootnote_enqueue_scripts');
function fdfootnote_enqueue_scripts() {
if (is_admin()) return;
wp_enqueue_script('jquery');
wp_register_script('fdfootnote_script', get_bloginfo('wpurl').'/'.PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)).'/fdfootnotes.js', array('jquery'), '1.34');
wp_enqueue_script('fdfootnote_script');
}
function fdfootnote_config_page() {
global $wpdb;
if ( function_exists('add_submenu_page') )
add_submenu_page('options-general.php',
__('Footnotes', FDFOOTNOTE_TEXTDOMAIN),
__('Footnotes', FDFOOTNOTE_TEXTDOMAIN),
'manage_options', __FILE__, 'fdfootnote_conf');
}
function fdfootnote_conf() {
$options = get_option('fdfootnote');
if (!isset($options['fdfootnote_collapse'])) $options['fdfootnote_collapse'] = 0;
$updated = false;
$options['fdfootnote_collapse'] = 0;
$options['fdfootnote_single'] = 0;
if ( isset($_POST['submit']) ) {
check_admin_referer('fdfootnote', 'fdfootnote-admin');
if (isset($_POST['fdfootnote_collapse'])) {
$options['fdfootnote_collapse'] = 1;
}
if (isset($_POST['fdfootnote_single'])) {
$options['fdfootnote_single'] = 1;
}
update_option('fdfootnote', $options);
$updated = true;
}
?>
<div class="wrap">
<?php
if ($updated) {
echo "<div id='message' class='updated fade'><p>";
_e('Configuration updated.', FDFOOTNOTE_TEXTDOMAIN);
echo "</p></div>";
}
?>
<h2><?php _e('Footnotes Configuration', FDFOOTNOTE_TEXTDOMAIN); ?></h2>
<form action="" method="post" id="fdfootnote-conf">
<p>
<input id="fdfootnote_single" name="fdfootnote_single" type="checkbox" value="1"<?php if ($options['fdfootnote_single']==1) echo ' checked'; ?> />
<label for="fdfootnote_single"><?php _e('Only show footnotes on single post/page', FDFOOTNOTE_TEXTDOMAIN); ?></label>
</p>
<p>
<input id="fdfootnote_collapse" name="fdfootnote_collapse" type="checkbox" value="1"<?php if ($options['fdfootnote_collapse']==1) echo ' checked'; ?> />
<label for="fdfootnote_collapse"><?php _e('Collapse footnotes until clicked', FDFOOTNOTE_TEXTDOMAIN); ?></label>
</p>
<p class="submit" style="text-align: left"><?php wp_nonce_field('fdfootnote', 'fdfootnote-admin'); ?><input type="submit" name="submit" value="<?php _e('Save', FDFOOTNOTE_TEXTDOMAIN); ?> »" /></p>
</form>
</div>
<?php
}
// Converts footnote markup into actual footnotes
function fdfootnote_convert($content) {
$options = get_option('fdfootnote');
$collapse = 0;
$single = 0;
$linksingle = false;
if (isset($options['fdfootnote_collapse'])) $collapse = $options['fdfootnote_collapse'];
if (isset($options['fdfootnote_single'])) $single = $options['fdfootnote_single'];
if (!is_page() && !is_single() && $single) $linksingle = true;
$post_id = get_the_ID();
$n = 1;
$notes = array();
if (preg_match_all('/\[(\d+\..*?)\]/s', $content, $matches)) {
foreach($matches[0] as $fn) {
$note = preg_replace('/\[\d+\.(.*?)\]/s', '\1', $fn);
$notes[$n] = $note;
$singleurl = '';
if ($linksingle) $singleurl = get_permalink();
$content = str_replace($fn, "<sup class='footnote'><a href='$singleurl#fn-$post_id-$n' id='fnref-$post_id-$n' onclick='return fdfootnote_show($post_id)'>$n</a></sup>", $content);
$n++;
}
// *****************************************************************************************************
// Workaround for wpautop() bug. Otherwise it sometimes inserts an opening <p> but not the closing </p>.
// There are a bunch of open wpautop tickets. See 4298 and 7988 in particular.
$content .= "\n\n";
// *****************************************************************************************************
if (!$linksingle) {
$content .= "<div class='footnotes' id='footnotes-$post_id'>";
$content .= "<div class='footnotedivider'></div>";
if ($collapse) {
$content .= "<a href='#' onclick='return fdfootnote_togglevisible($post_id)' class='footnotetoggle'>";
$content .= "<span class='footnoteshow'>".sprintf(_n('Show %d footnote', 'Show %d footnotes', $n-1, FDFOOTNOTE_TEXTDOMAIN), $n-1)."</span>";
$content .= "</a>";
$content .= "<ol style='display: none'>";
} else {
$content .= "<ol>";
}
for($i=1; $i<$n; $i++) {
$content .= "<li id='fn-$post_id-$i'>$notes[$i] <span class='footnotereverse'><a href='#fnref-$post_id-$i'>↩</a></span></li>";
}
$content .= "</ol>";
$content .= "</div>";
}
}
return($content);
}
add_action('the_content', 'fdfootnote_convert', 1);
?>