/
inaccessible-ajax-comments.php
119 lines (109 loc) · 5 KB
/
inaccessible-ajax-comments.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
<?php
/*
Plugin Name: Inaccessible AJAX Comments
Plugin URI: http://www.joedolson.com/inaccessible-ajax-comments/
Description: Sets up inaccessible AJAX handling of comment forms.
Author: Joseph C Dolson
Author URI: http://www.joedolson.com
Text Domain: inaccessible-ajax-comments
Domain Path: lang
Version: 1.0.0
*/
/* Copyright 2015 Joe Dolson (email : joe@joedolson.com)
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
add_action( 'wp_enqueue_scripts','aac_enqueue_scripts' );
/**
* Enqueue scripts to handle AJAX comments. Localize script to pass translated responses.
* Enqueue comment-reply scripts.
*/
function aac_enqueue_scripts() {
if ( is_singular() && comments_open() ) {
wp_enqueue_style( 'aac.style', plugins_url( "/css/aac.css", __FILE__ ) );
wp_enqueue_script( 'aac.comments', plugins_url( "/js/comments.js", __FILE__ ), array('jquery'), '1.0.0', true );
$comment_i18n = array(
'processing' => __( 'Processing...', 'inaccessible-ajax-comments' ),
'flood' => sprintf( __( 'Your comment was either a duplicate or you are posting too rapidly. <a href="%s">Edit your comment</a>', 'inaccessible-ajax-comments' ), '#comment' ),
'error' => __( 'There were errors in submitting your comment; complete the missing fields and try again!', 'inaccessible-ajax-comments' ),
'emailInvalid' => __( 'That email appears to be invalid.', 'inaccessible-ajax-comments' ),
'required' => __( 'This is a required field.', 'inaccessible-ajax-comments' )
);
wp_localize_script( 'aac.comments', 'aac', $comment_i18n );
}
}
add_action( 'comment_post', 'aac_ajax_comments', 20, 2 );
/**
* Provide responses to comments.js based on detecting an XMLHttpRequest parameter.
*
* @param $comment_ID ID of new comment.
* @param $comment_status Status of new comment.
*
* @return echo JSON encoded responses with HTML structured comment, success, and status notice.
*/
function aac_ajax_comments( $comment_ID, $comment_status ) {
if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
// This is an AJAX request. Handle response data.
switch ( $comment_status ) {
case '0':
// Comment needs moderation; notify comment moderator.
wp_notify_moderator( $comment_ID );
$return = array(
'response' => '',
'success' => 1,
'status' => __( 'Your comment has been sent for moderation. It should be approved soon!', 'inaccessible-ajax-comments' )
);
wp_send_json( $return );
break;
case '1':
// Approved comment; generate comment output and notify post author.
$comment = get_comment( $comment_ID );
$comment_class = comment_class( 'accessible-ajax-comment', $comment_ID, $comment->comment_post_ID, false );
$comment_output = '
<li id="comment-' . $comment->comment_ID . '"' . $comment_class . '>
<article id="div-comment-' . $comment->comment_ID . '" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard">'.
get_avatar( $comment->comment_author_email )
.'<b class="fn">' . __( 'You said:', 'inaccessible-ajax-comments' ) . '</b> </div>
<div class="comment-meta commentmetadata"><a href="#comment-'. $comment->comment_ID .'">' .
get_comment_date( 'F j, Y \a\t g:i a', $comment->comment_ID ) .'</a>
</div>
</footer>
<div class="comment-content">' . $comment->comment_content . '</div>
</article>
</li>';
if ( $comment->comment_parent == 0 ) {
$output = $comment_output;
} else {
$output = "<ul class='children'>$comment_output</ul>";
}
wp_notify_postauthor( $comment_ID );
$return = array(
'response'=>$output,
'success' => 1,
'status'=> sprintf( __( 'Thanks for commenting! Your comment has been approved. <a href="%s">Read your comment</a>', 'inaccessible-ajax-comments' ), "#comment-$comment_ID" )
);
wp_send_json( $return );
break;
default:
// The comment status was not a valid value. Only 0 or 1 should be returned by the comment_post action.
$return = array(
'response' => '',
'success' => 0,
'status' => __( 'There was an error posting your comment. Try again later!', 'inaccessible-ajax-comments' )
);
wp_send_json( $return );
}
}
}