-
Notifications
You must be signed in to change notification settings - Fork 0
/
blank-footnotes.php
201 lines (168 loc) · 5.08 KB
/
blank-footnotes.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
<?php
/**
* Convert footnotes Markdown in html
*
* PHP version 7
*
* @category Wordpress_Plugin
* @package Blank_Footnotes
* @author Corrado Franco <[email protected]>
* @copyright 2016-2019 Corrado Franco
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPL-2
* @link https://github.com/conraid/blank-footnotes
*/
/*
Plugin Name: Blank Footnotes
Plugin URI: https://github.com/conraid/blank-footnotes
Description: Footnotes in Markdown mode
Version: 1.6.6
Author: Corrado Franco <[email protected]>
Author URI: https://corradofranco.it
License: GPL-2
Text Domain: blank-footnotes
Domain Path: /langs
*/
/**
* Copyright 2016-2020 Corrado Franco <[email protected]>
*
* 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.
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Load a plugin’s translated strings.
*
* @return void
*/
function bfn_init() {
// Load file .mo from subdirectory "langs".
load_plugin_textdomain( 'blank-footnotes', false, dirname( plugin_basename( __FILE__ ) ) . '/langs' );
}
add_action( 'plugins_loaded', 'bfn_init' );
/**
* Filter the post content.
*
* Return content with footnotes markdown converted in HTML.
*
* @param string $content Content of the current post.
* @return string Content converted.
*/
function bfn_markdown_convert( $content ) {
// Get Post ID from database.
$post_id = intval( get_the_ID() );
// Regex to convert markdown footnotes to HTML.
$content = preg_replace( '/\[\^(\d+)\]:/', "<span class='footnote' id='fn-$post_id-$1'><a href='#fnref-$post_id-$1'>$1</a>.</span>", $content );
// Regex to convert markdown reference notes to HTML.
$content = preg_replace( '/\[\^(\d+)\]/', "<sup class='footnote' id='fnref-$post_id-$1'><a href='#fn-$post_id-$1' rel='footnote'>$1</a></sup>", $content );
return( $content );
}
/**
* Add action blank_fn_markdown_convert if jetpack markdown is not loaded
*
* Classic method does not work in all occasions.
*
* if ( ! class_exists( 'Jetpack' ) || ! Jetpack::is_module_active( 'photon' ) ) {
* add_action( 'the_content', 'bfn_markdown_convert', 1 );
* }
*/
if ( get_option( 'jetpack_active_modules' ) ) {
if ( ! in_array( 'markdown', get_option( 'jetpack_active_modules' ), true ) ) {
add_action( 'the_content', 'bfn_markdown_convert', 1 );
}
} else {
add_action( 'the_content', 'bfn_markdown_convert', 1 );
}
/**
* Add script to footer
*
* Add script appthemes_add_quicktags to footer
* for add more buttons to the text editor
*
* @return void
*/
function appthemes_add_quicktags() {
if ( wp_script_is( 'quicktags' ) ) {
?>
<script type="text/javascript">
QTags.addButton(
"bfn_note",
"<?php esc_html_e( 'Reference note', 'blank-footnotes' ); ?>",
bfn_callback
);
function bfn_callback() {
var id = prompt("<?php esc_html_e( 'Enter the note number', 'blank-footnotes' ); ?>");
if (id != null) {
QTags.insertContent('[^' + id +'] ');
}
}
QTags.addButton(
"bfn_footnote",
"<?php esc_html_e( 'Note:', 'blank-footnotes' ); ?>",
bfn_foot_callback
);
function bfn_foot_callback() {
var id = prompt("<?php esc_html_e( 'Enter the note number', 'blank-footnotes' ); ?>");
if (id != null) {
QTags.insertContent('[^' + id +']: ');
}
}
</script>
<?php
}
}
add_action( 'admin_print_footer_scripts', 'appthemes_add_quicktags' );
/**
* Add plugin to the Visual Editor TimyMCE plugins.
*
* @param array $plugin_array TinyMCE Plugins.
* @return array
*/
function bfn_enqueue_mce_plugin_scripts( $plugin_array ) {
$plugin_array['bfn_button_plugin'] = plugin_dir_url( __FILE__ ) . 'bfn.js';
return $plugin_array;
}
/**
* Add more buttons to the Visual Editor TimyMCE
*
* @param array $buttons TinyMCE buttons.
* @return array
*/
function bfn_register_mce_buttons( $buttons ) {
array_push( $buttons, 'bfn' );
array_push( $buttons, 'bfn_note' );
return $buttons;
}
/**
* Add two buttons to TinyMCE
*
* Action bfn_add_mce_button to add two buttons to TinyMCE
* if WYSIWYG is enabled and user have permissions right
*
* @return void
*/
function bfn_add_mce_button() {
// Check user permissions.
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
return;
}
// Add filter if WYSIWYG is enabled.
if ( 'true' === get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_external_plugins', 'bfn_enqueue_mce_plugin_scripts' );
add_filter( 'mce_buttons', 'bfn_register_mce_buttons' );
}
}
add_action( 'admin_head', 'bfn_add_mce_button' );