forked from robertdevore/markdown-comments-for-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-comments-for-wordpress.php
More file actions
349 lines (311 loc) · 12.2 KB
/
markdown-comments-for-wordpress.php
File metadata and controls
349 lines (311 loc) · 12.2 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* Plugin Name: Markdown Comments for WordPress®
* Description: Allow users to write comments in Markdown format and automatically parse them into HTML.
* Plugin URI: https://github.com/robertdevore/markdown-comments-for-wordpress/
* Version: 1.0.0
* Author: Robert DeVore
* Author URI: https://robertdevore.com
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: markdown-comments
* Domain Path: /languages
* Update URI: https://github.com/robertdevore/markdown-comments-for-wordpress/
*/
// Security check to prevent direct access.
if ( ! defined( 'ABSPATH' ) ) exit;
// Include the Plugin Update Checker.
require 'vendor/plugin-update-checker/plugin-update-checker.php';
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
$myUpdateChecker = PucFactory::buildUpdateChecker(
'https://github.com/robertdevore/markdown-comments-for-wordpress/',
__FILE__,
'markdown-comments-for-wordpress'
);
// Set the branch that contains the stable release.
$myUpdateChecker->setBranch( 'main' );
// Include the autoload for Composer.
if ( ! class_exists( 'RobertDevore\WPComCheck\WPComPluginHandler' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
use RobertDevore\WPComCheck\WPComPluginHandler;
// Initialize the WPComPluginHandler with the plugin slug and learn more link.
new WPComPluginHandler( plugin_basename( __FILE__ ), 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' );
/**
* Load the plugin textdomain for translations
*
* @since 1.0.0
* @return void
*/
function markdown_comments_load_textdomain() {
load_plugin_textdomain(
'markdown-comments',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
}
add_action( 'plugins_loaded', 'markdown_comments_load_textdomain' );
define( 'MARKDOWN_COMMENTS_VERSION', '1.0.0' );
define( 'MARKDOWN_COMMENTS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'MARKDOWN_COMMENTS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
/**
* Activate the plugin and set default option
*
* @since 1.0.0
* @return void
*/
function markdown_comments_activate_plugin() {
add_option( 'markdown_comments_enable_markdown_comments', true );
}
register_activation_hook( __FILE__, 'markdown_comments_activate_plugin' );
/**
* Add settings field to enable/disable markdown for comments
*
* @since 1.0.0
* @return void
*/
function markdown_comments_register_settings() {
register_setting( 'discussion', 'markdown_comments_enable_markdown_comments' );
add_settings_field(
'markdown_comments_enable_markdown_comments',
esc_html__( 'Markdown Comments', 'markdown-comments' ),
'markdown_comments_render_settings_field',
'discussion',
'default'
);
}
add_action( 'admin_init', 'markdown_comments_register_settings' );
/**
* Render the settings field
*
* @since 1.0.0
* @return void
*/
function markdown_comments_render_settings_field() {
$value = get_option( 'markdown_comments_enable_markdown_comments', true );
?>
<input type="checkbox" name="markdown_comments_enable_markdown_comments" value="1" <?php checked( 1, $value, true ); ?> />
<label for="markdown_comments_enable_markdown_comments"><?php esc_html_e( 'Enable Markdown for Comments', 'markdown-comments' ); ?></label>
<?php
}
/**
* Enqueue CSS and JavaScript for Markdown support
*
* @since 1.0.0
* @return void
*/
function markdown_comments_enqueue_scripts() {
if ( is_singular() && comments_open() && get_option( 'markdown_comments_enable_markdown_comments', true ) ) {
// Enqueue CSS.
wp_enqueue_style(
'markdown-comments-style',
plugin_dir_url( __FILE__ ) . 'assets/css/markdown-comments.css',
[],
MARKDOWN_COMMENTS_VERSION
);
// Enqueue JavaScript.
wp_enqueue_script(
'markdown-comments-script',
plugin_dir_url( __FILE__ ) . 'assets/js/markdown-comments.js',
[],
MARKDOWN_COMMENTS_VERSION,
true
);
// Localize script to pass PHP variables to JS, like the help text.
wp_localize_script(
'markdown-comments-script',
'markdownCommentsHelp',
[
'text' => esc_js( __( 'You can use Markdown: **bold**, *italic*, `code`, [link](url), # headings, - lists', 'markdown-comments' ) ),
]
);
}
}
add_action( 'wp_enqueue_scripts', 'markdown_comments_enqueue_scripts' );
/**
* Apply inline formatting (bold, italic, code, links) to text
*
* @param string $text The text to format
*
* @since 1.0.0
* @return mixed The formatted text
*/
function markdown_comments_apply_inline_formatting( $text ) {
// Bold **text** or __text__.
$text = preg_replace_callback( '/\*\*(.*?)\*\*/', function($matches) {
return '<strong>' . esc_html($matches[1]) . '</strong>';
}, $text );
$text = preg_replace_callback( '/__(.*?)__/', function($matches) {
return '<strong>' . esc_html($matches[1]) . '</strong>';
}, $text );
// Italic *text* or _text_ (but not if it's part of list markers or bold formatting).
// Handle single asterisks for italics (avoid conflicts with list markers and bold).
$text = preg_replace_callback( '/(?<!\*)(?<!\w)\*([^*\s][^*]*?[^*\s]|\w)\*(?!\*)(?!\w)/', function($matches) {
return '<em>' . esc_html($matches[1]) . '</em>';
}, $text );
// Handle single underscores for italics (avoid conflicts with bold formatting).
$text = preg_replace_callback( '/(?<!_)(?<!\w)_([^_\s][^_]*?[^_\s]|\w)_(?!_)(?!\w)/', function($matches) {
return '<em>' . esc_html($matches[1]) . '</em>';
}, $text );
$text = preg_replace_callback( '/`(.*?)`/', function($matches) {
return '<code>' . esc_html($matches[1]) . '</code>';
}, $text );
// Links [text](url).
$text = preg_replace_callback( '/\[([^\]]+)\]\(([^)]+)\)/', function($matches) {
$url = function_exists('esc_url') ? esc_url($matches[2]) : esc_html($matches[2]);
return '<a href="' . $url . '" rel="nofollow">' . esc_html($matches[1]) . '</a>';
}, $text );
return $text;
}
/**
* Simple Markdown parser for basic formatting
*
* @param string $text The Markdown text to parse
*
* @since 1.0.0
* @return string The parsed HTML
*/
function markdown_comments_parse_markdown( $text ) {
// Split into lines for processing.
$lines = explode( "\n", $text );
$output = [];
$in_list = false;
$list_type = '';
foreach ( $lines as $line ) {
$line = trim( $line );
// Skip empty lines.
if ( empty( $line ) ) {
if ( $in_list ) {
$output[] = $list_type === 'ul' ? '</ul>' : '</ol>';
$in_list = false;
}
$output[] = '';
continue;
}
// Headings.
if ( preg_match( '/^(#{1,6})\s+(.+)/', $line, $matches ) ) {
if ( $in_list ) {
$output[] = $list_type === 'ul' ? '</ul>' : '</ol>';
$in_list = false;
}
$level = strlen( $matches[1] );
// Apply inline formatting to heading content, then wrap in heading tags.
$heading_text = markdown_comments_apply_inline_formatting( trim( $matches[2] ) );
$output[] = '<h' . $level . '>' . $heading_text . '</h' . $level . '>';
continue;
}
// Unordered lists (-, *, +, and en-dash –).
if ( preg_match( '/^[-*+–-]\s+(.+)/', $line, $matches ) ) {
if ( ! $in_list || $list_type !== 'ul' ) {
if ( $in_list && $list_type === 'ol' ) {
$output[] = '</ol>';
}
$output[] = '<ul>';
$in_list = true;
$list_type = 'ul';
}
// Apply inline formatting to list item content.
$list_content = markdown_comments_apply_inline_formatting( trim( $matches[1] ) );
$output[] = '<li>' . $list_content . '</li>';
continue;
}
// Ordered lists (1., 2., etc.).
if ( preg_match( '/^\d+\.\s+(.+)/', $line, $matches ) ) {
if ( ! $in_list || $list_type !== 'ol' ) {
if ( $in_list && $list_type === 'ul' ) {
$output[] = '</ul>';
}
$output[] = '<ol>';
$in_list = true;
$list_type = 'ol';
}
// Apply inline formatting to list item content.
$list_content = markdown_comments_apply_inline_formatting( trim( $matches[1] ) );
$output[] = '<li>' . $list_content . '</li>';
continue;
}
// Regular paragraph text - keep it raw for now.
if ( $in_list ) {
$output[] = $list_type === 'ul' ? '</ul>' : '</ol>';
$in_list = false;
}
$output[] = $line; // Keep raw text
}
// Close any remaining list.
if ( $in_list ) {
$output[] = $list_type === 'ul' ? '</ul>' : '</ol>';
}
// Now handle paragraph grouping and final formatting.
$lines = $output;
$final_output = [];
$current_paragraph = [];
foreach ( $lines as $line ) {
$line = trim( $line );
// If it's an empty line, finish current paragraph.
if ( empty( $line ) ) {
if ( ! empty( $current_paragraph ) ) {
// Apply inline formatting and escaping to paragraph content
$paragraph_text = implode( ' ', $current_paragraph );
$paragraph_text = markdown_comments_apply_inline_formatting( $paragraph_text );
$final_output[] = '<p>' . $paragraph_text . '</p>';
$current_paragraph = [];
}
continue;
}
// If it's a block element (heading, list), add it directly.
if ( preg_match( '/^<(h[1-6]|ul|ol|li)>/', $line ) || preg_match( '/^<\/(ul|ol)>/', $line ) ) {
// Finish any current paragraph first.
if ( ! empty( $current_paragraph ) ) {
$paragraph_text = implode( ' ', $current_paragraph );
$paragraph_text = markdown_comments_apply_inline_formatting( $paragraph_text );
$final_output[] = '<p>' . $paragraph_text . '</p>';
$current_paragraph = [];
}
// Block elements are already formatted, add them directly.
$final_output[] = $line;
} else {
// It's regular text, add to current paragraph.
$current_paragraph[] = $line;
}
}
// Don't forget the last paragraph.
if ( ! empty( $current_paragraph ) ) {
$paragraph_text = implode( ' ', $current_paragraph );
$paragraph_text = markdown_comments_apply_inline_formatting( $paragraph_text );
$final_output[] = '<p>' . $paragraph_text . '</p>';
}
return implode( "\n", $final_output );
}
/**
* Convert Markdown to HTML when displaying the comment
*
* @param string $comment_text The comment content
*
* @since 1.0.0
* @return string The parsed HTML
*/
function markdown_comments_filter_comment_text( $comment_text ) {
if ( get_option( 'markdown_comments_enable_markdown_comments', true ) ) {
// Convert any smart characters back to regular ones before processing.
$comment_text = str_replace( [ '–', '—' ], [ '-', '--' ], $comment_text );
return wp_kses_post( markdown_comments_parse_markdown( $comment_text ) );
}
return $comment_text;
}
add_filter( 'comment_text', 'markdown_comments_filter_comment_text', 5 );
/**
* Remove WordPress default formatting filters for comments when markdown is enabled
*
* @since 1.0.0
* @return void
*/
function markdown_comments_remove_default_filters() {
if ( get_option( 'markdown_comments_enable_markdown_comments', true ) ) {
// Remove default WordPress formatting so it doesn't conflict.
remove_filter( 'comment_text', 'wpautop', 30 );
remove_filter( 'comment_text', 'wptexturize', 10 );
remove_filter( 'comment_text', 'convert_chars', 10 );
remove_filter( 'comment_text', 'convert_smilies', 20 );
}
}
add_action( 'init', 'markdown_comments_remove_default_filters' );