-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbp-quote.php
355 lines (301 loc) · 9.68 KB
/
bbp-quote.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
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
350
351
352
353
354
355
<?php
/*
Plugin Name: bbP Quote
Description: Quote forum posts in bbPress with this nifty plugin!
Author: r-a-y
Author URI: http://profiles.wordpress.org/r-a-y
Version: 0.1
License: GPLv2 or later
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'bbP_Quote' ) ) :
class bbP_Quote {
/**
* Init method.
*/
public static function init() {
return new self();
}
/**
* Constructor.
*/
public function __construct() {
// page injection
add_action( 'bbp_theme_before_reply_form_submit_wrapper', array( $this, 'javascript' ) );
// quote links
add_filter( 'bbp_topic_admin_links', array( $this, 'add_quote_link' ), 1 );
add_filter( 'bbp_reply_admin_links', array( $this, 'add_quote_link' ), 1 );
// kses additions
add_filter( 'bbp_kses_allowed_tags', array( $this, 'allowed_attributes' ) );
// remove kses additions
add_action( 'bbp_theme_after_reply_form_content', array( $this, 'remove_bbp_quote_attributes' ) );
// editor CSS
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_filter( 'mce_css', array( $this, 'editor_styles' ) );
// inline CSS
add_filter( 'bp_email_set_template', array( $this, 'bp_html_css' ) );
// right angle bracket to blockquote
add_filter( 'bbp_get_topic_content', array( $this, 'right_angle_bracket_to_blockquote' ), 45 );
add_filter( 'bbp_get_reply_content', array( $this, 'right_angle_bracket_to_blockquote' ), 45 );
}
/**
* Outputs the javascript.
*
* @todo Move JS to static file. Localize citation string.
*/
public function javascript() {
?>
<script type="text/javascript">
// Selection function that handles HTML.
// @link https://stackoverflow.com/a/6668159
function bbp_get_selection() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}
function bbp_insert_quote( user, text, permalink ){
var content = '<blockquote class="bbp-the-quote" cite="' + permalink + '"><em class="bbp-the-quote-cite"><a href="' + permalink + '">' + user + ' wrote:</a></em>' + text.replace(/(\r\n|\n|\r)/gm,"").replace('<br>',"\n") + '</blockquote>' + "\r\n\n";
// check if tinyMCE is active and visible
if ( window.tinyMCE && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) {
tinyMCE.activeEditor.selection.setContent( content );
tinyMCE.activeEditor.selection.collapse(false);
tinyMCE.activeEditor.focus();
// regular textarea
} else {
var textarea = jQuery("#bbp_reply_content");
// add quote
textarea.val( textarea.val() + content );
// scroll to bottom of textarea and focus
textarea.animate(
{scrollTop: textarea[0].scrollHeight - textarea.height()},
800
).focus();
}
}
jQuery(document).ready( function($) {
$(".bbp-quote").on("click", function(){
var idName = $(this).closest('.bbp-reply-header').prop('id'),
id = idName.replace( 'post-', '' ),
permalink = $('#' + idName + ' .bbp-reply-permalink').prop('href'),
author = $('.' + idName + ' .bbp-author-name').first().text(),
content = bbp_get_selection(),
firstPostId = $('ul.bbp-replies').prop('id').replace('topic-','').replace('-replies',''),
sel, parentEl;
// Check if selection is part of the current forum post.
if ( content ) {
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
parentEl = sel.getRangeAt(0).commonAncestorContainer;
if (parentEl.nodeType != 1) {
parentEl = parentEl.parentNode;
}
}
} else if ( (sel = document.selection) && sel.type != "Control") {
parentEl = sel.createRange().parentElement();
}
if ( parentEl ) {
parentEl = $(parentEl).closest('.hentry').prev('.bbp-reply-header');
if ( parentEl && parentEl.prop('id') !== idName ) {
content = false;
}
}
}
// Fallback to whole forum post for quote.
if ( ! content ) {
content = $('.' + idName + ' .bbp-reply-content :not(ul.bbp-topic-revision-log,ul.bbp-reply-revision-log)').html();
}
/*
* Threaded mode.
*
* Only use moveForm() if we're not quoting the first post to comply with
* how bbPress does threading.
*/
if ( typeof addReply !== 'undefined' && id !== firstPostId ) {
addReply.moveForm( idName , id, 'new-reply-' + firstPostId, firstPostId );
// Linear mode.
} else {
// Scroll to form.
$("html, body").animate(
{scrollTop: $("#new-post").offset().top},
500
);
/*
* Set reply to ID for linear mode.
*
* If quoting first post, reply to ID is always zero as this is how
* bbPress does threading.
*/
$('#bbp_reply_to').val( firstPostId === id ? 0 : id );
}
// insert quote
bbp_insert_quote( author, content, permalink );
});
// when clicking on a citation, do fancy scroll
$(".bbp-the-quote-cite a").on("click", function(e){
var id = $(this.hash),
qd = {};
// Check for BP Forum Settings permalink.
if ( ! id.selector ) {
this.href.split('?')[1].split('&').forEach(function(i){
qd[i.split('=')[0]]=i.split('=')[1];
});
if ( qd.p ) {
id = $( '#post-' + qd.p );
}
}
// Post is on this page, so fancy scroll!
if ( id.length ) {
e.preventDefault();
$("html, body").animate(
{scrollTop: $(id).offset().top},
500
);
location.hash = id.selector;
}
});
});
</script>
<?php
}
/**
* Add "Quote" link to admin links.
*
* @param array $retval Current links.
* @return array
*/
public function add_quote_link( $retval ) {
if ( ! is_user_logged_in() ) {
return $retval;
}
$retval['quote'] = '<a class="bbp-quote" href="javascript:;">' . esc_html__( 'Quote', 'bbp-quote' ) . '</a>';
return $retval;
}
/**
* Add 'class' attribute to 'blockquote' and 'em' elements.
*/
public function allowed_attributes( $retval ) {
$retval['blockquote']['class'] = array();
$retval['em']['class'] = array();
$retval['p'] = array();
return $retval;
}
/**
* Transforms '> ' to a blockquote.
*
* Line must begin with '> ' in order for the transformation to take
* place. To stop a quote, use two breaklines.
*/
public function right_angle_bracket_to_blockquote( $retval ) {
$found = false;
// Check if our blockquote marker is at the beginning of the reply or on a separate line.
$marker = '> ';
if ( 0 === strpos( $retval, $marker ) || false !== strpos( $retval, "\n" . $marker ) ) {
$found = true;
}
// Sometimes the HTML entity could be used, so check for that too.
$marker2 = '> ';
if ( false === $found && ( 0 === strpos( $retval, $marker2 ) || false !== strpos( $retval, "\n" . $marker2 ) ) ) {
$found = true;
}
if ( ! $found ) {
return $retval;
}
$lines = explode( "\n\n", $retval );
foreach ( $lines as $i => $line ) {
if ( 0 === strpos( $line, $marker ) ) {
$len = 2;
} elseif ( 0 === strpos( $line, $marker2 ) ) {
$len = 5;
} else {
continue;
}
$lines[$i] = substr_replace( $line, '<blockquote class="bbp-the-quote">', 0, $len );
$lines[$i] = rtrim( $lines[$i] );
$lines[$i] .= '</blockquote>';
}
return implode( "\n\n", $lines );
}
/**
* For the "Allowed Tags" block that shows up for non-admins on the frontend,
* remove our custom kses additions from {@link bbP_Quote::allowed_attributes()}
* so they will not show up there.
*/
public function remove_bbp_quote_attributes() {
remove_filter( 'bbp_kses_allowed_tags', array( $this, 'allowed_attributes' ) );
}
/**
* Enqueue CSS.
*
* Feel free to disable with the 'bbp_quote_enable_css' filter and roll your
* own in your theme's stylesheet.
*/
public function enqueue_styles() {
if ( ! apply_filters( 'bbp_quote_enable_css', true ) )
return;
// are we on a bbPress page?
$show = is_bbpress();
// check for BuddyPress group forum pages.
if ( empty( $show ) && bbp_is_group_forums_active() && defined( 'BP_VERSION' ) && bp_is_active( 'groups' ) ) {
$show = bp_is_group() && bp_is_current_action( 'forum' );
}
// not on a bbPress page? stop now!
if ( empty( $show ) ) {
return;
}
wp_enqueue_style( 'bbp-quote', plugins_url( 'style.css', __FILE__ ) );
}
/**
* Add CSS to style blockquotes in TinyMCE.
*
* @param string $css String of CSS assets for TinyMCE.
* @return string
*/
public function editor_styles( $css ){
if ( ! apply_filters( 'bbp_quote_enable_editor_css', true ) ) {
return $css;
}
$css .= ',' . plugins_url( 'style.css', __FILE__ );
return $css;
}
/**
* Inject custom inline CSS into BuddyPress HTML emails.
*
* Unfortunately, BuddyPress doesn't make this easy!
*/
public function bp_html_css( $retval ) {
$css = <<<CSS
blockquote {
margin-right:.5em;
}
blockquote blockquote {
border-color: #CDCDCD #CDCDCD #CDCDCD #c4c4c4;
border-style: solid;
border-width: 1px 1px 1px 10px;
margin-left:0;
padding: 0.5em 2em;
}
blockquote .bbp-the-quote-cite {
display: block;
margin-bottom:1em;
}
CSS;
return substr_replace( $retval, $css, strpos( $retval, '</style>' ), 0 );
}
}
add_action( 'bbp_includes', array( 'bbP_Quote', 'init' ) );
endif;