Repository navigation
Expand file tree
/
Copy pathblank-footnotes.php
More file actions
197 lines (166 loc) · 5.06 KB
/
Copy pathblank-footnotes.php
File metadata and controls
197 lines (166 loc) · 5.06 KB
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
<?php
/**
* Convert footnotes Markdown in html
*
* PHP version 7.4 or later
*
* @category Wordpress_Plugin
* @package Blank_Footnotes
* @author Corrado Franco <conraid@linux.it>
* @copyright 2016-2026 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.8
Author: Corrado Franco <conraid@linux.it>
Author URI: https://corradofranco.it
License: GPL-2
Text Domain: blank-footnotes
Domain Path: /langs
*/
/**
* Copyright 2016-2026 Corrado Franco <conraid@linux.it>
*
* 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 2
* of the License, or (at your option) any later version.
*/
// 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.
// preg_replace() can return null on a regex engine error (e.g. backtrack
// limit); fall back to the original content instead of losing it.
$content = preg_replace( '/\[\^(\d+)\]:/', "<span class='footnote' id='fn-$post_id-$1'><a href='#fnref-$post_id-$1'>$1</a>.</span>", $content ) ?? $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 ) ?? $content;
return $content;
}
/**
* Register footnote filters.
*
* Separated into a dedicated function for better modularity and to allow
* future expansion (e.g., adding extra cleanup filters).
*
* @return void
*/
function bfn_register_filters() {
add_filter( 'the_content', 'bfn_markdown_convert', 1 );
}
/**
* Register the filters only if Jetpack's Markdown module is not active.
*
* We check active modules directly because the classic method does not always work:
* if ( ! class_exists( 'Jetpack' ) || ! Jetpack::is_module_active( 'markdown' ) ) { ... }
*/
$jetpack_active_modules = get_option( 'jetpack_active_modules' );
if ( ! is_array( $jetpack_active_modules ) || ! in_array( 'markdown', $jetpack_active_modules, true ) ) {
bfn_register_filters();
}
/**
* 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<string, string> $plugin_array TinyMCE Plugins.
* @return array<string, string>
*/
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<int, string> $buttons TinyMCE buttons.
* @return array<int, string>
*/
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' );