【收藏整理】Ajax提交评论后刷新网页前可以修改

这段时间对自用主题整改的比较凶残,各种代码各种尝试,之前使用大发的Ajax提交评论,现在又开始尝试常阳时光的WordPress新版ajax评论成功提交评论刷新网页前,可以重新修改评论(更新:2014-11-20)的方法,感觉非常好用!

一、下面的代码加到functions.php中,注意修改成你自己的评论输出样式。

add_action('wp_ajax_nopriv_ajax_comment', 'ajax_comment');
add_action('wp_ajax_ajax_comment', 'ajax_comment');
function ajax_comment(){
    global $wpdb;
    //nocache_headers();
    $comment_post_ID = isset($_POST['comment_post_ID']) ? (int) $_POST['comment_post_ID'] : 0;
    $post = get_post($comment_post_ID);
    $post_author = $post->post_author;
    if ( empty($post->comment_status) ) {
        do_action('comment_id_not_found', $comment_post_ID);
        ajax_comment_err('Invalid comment status.');
    }
    $status = get_post_status($post);
    $status_obj = get_post_status_object($status);
    if ( !comments_open($comment_post_ID) ) {
        do_action('comment_closed', $comment_post_ID);
        ajax_comment_err('Sorry, comments are closed for this item.');
    } elseif ( 'trash' == $status ) {
        do_action('comment_on_trash', $comment_post_ID);
        ajax_comment_err('Invalid comment status.');
    } elseif ( !$status_obj->public && !$status_obj->private ) {
        do_action('comment_on_draft', $comment_post_ID);
        ajax_comment_err('Invalid comment status.');
    } elseif ( post_password_required($comment_post_ID) ) {
        do_action('comment_on_password_protected', $comment_post_ID);
        ajax_comment_err('Password Protected');
    } else {
        do_action('pre_comment_on_post', $comment_post_ID);
    }
    $comment_author       = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
    $comment_author_email = ( isset($_POST['email']) )   ? trim($_POST['email']) : null;
    $comment_author_url   = ( isset($_POST['url']) )     ? trim($_POST['url']) : null;
    $comment_content      = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
    $edit_id              = ( isset($_POST['edit_id']) ) ? $_POST['edit_id'] : null; // 提取 edit_id
    $user = wp_get_current_user();
    if ( $user->exists() ) {
        if ( empty( $user->display_name ) )
            $user->display_name=$user->user_login;
        $comment_author       = esc_sql($user->display_name);
        $comment_author_email = esc_sql($user->user_email);
        $comment_author_url   = esc_sql($user->user_url);
        $user_ID              = esc_sql($user->ID);
        if ( current_user_can('unfiltered_html') ) {
            if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) {
                kses_remove_filters();
                kses_init_filters();
            }
        }
    } else {
        if ( get_option('comment_registration') || 'private' == $status )
            ajax_comment_err('Sorry, you must be logged in to post a comment.');
    }
    $comment_type = '';
    if ( get_option('require_name_email') && !$user->exists() ) {
        if ( 6 > strlen($comment_author_email) || '' == $comment_author )
            ajax_comment_err( __('Error: 请填写昵称以及邮箱。') );
        elseif ( !is_email($comment_author_email))
            ajax_comment_err( __('Error: 请输入一个有效的电子邮箱地址。') );
    }
    if ( '' == $comment_content )
        ajax_comment_err( 'Error: please type a comment.' );
    $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
    if ( $comment_author_email ) $dupe .= "OR comment_author_email = '$comment_author_email' ";
    $dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
    if ( $wpdb->get_var($dupe) ) {
        ajax_comment_err('Duplicate comment detected; it looks as though you’ve already said that!');
    }
    if ( $lasttime = $wpdb->get_var( $wpdb->prepare("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author = %s ORDER BY comment_date DESC LIMIT 1", $comment_author) ) ) {
        $time_lastcomment = mysql2date('U', $lasttime, false);
        $time_newcomment  = mysql2date('U', current_time('mysql', 1), false);
        $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
        if ( $flood_die ) {
            ajax_comment_err('You are posting comments too quickly.  Slow down.');
        }
    }
    $comment_parent = isset($_POST['comment_parent']) ? absint($_POST['comment_parent']) : 0;
    $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
    if ( $edit_id )
    {
        $comment_id = $commentdata['comment_ID'] = $edit_id;
        if( ihacklog_user_can_edit_comment($commentdata,$comment_id) )
        {
            wp_update_comment( $commentdata );
        }
        else
        {
            ajax_comment_err( 'Cheatin’ uh?' );
        }
    }
    else
    {
        $comment_id = wp_new_comment( $commentdata );
    }
    $comment = get_comment($comment_id);
    do_action('set_comment_cookies', $comment, $user);
    $comment_depth = 1;
    $tmp_c = $comment;
    while($tmp_c->comment_parent != 0){
        $comment_depth++;
        $tmp_c = get_comment($tmp_c->comment_parent);
    }
    $GLOBALS['comment'] = $comment;
    ?>
    <!-- Your comments here  edit start //这里修改成你的评论结构 -->
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
        <article id="comment-<?php comment_ID(); ?>" class="comment">
            <footer>
                <div class="comment-author vcard">
                    <?php echo get_avatar( $comment, 40 ); ?>
                    <?php printf( __( '%s <span class="says">says:</span>', 'publish' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
                    <?php   if (function_exists("CID_init")) CID_print_comment_flag(); 
                            if (function_exists("is_friend_link")) is_friend_link($comment->comment_author_url);
                    ?>
                    <?php if(user_can($comment->user_id, 1)){ ?><i class="comment-admin" title="博主"></i><?php } ?>
                    <?php echo $commentcountText; //主评论楼层号?>
                </div><!-- .comment-author .vcard -->
                <?php if ( $comment->comment_approved == '0' ) : ?>
                    <em><?php _e( 'Your comment is awaiting moderation.', 'publish' ); ?></em>
                    <br />
                <?php endif; ?>

                <div class="comment-meta commentmetadata">
                    <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><time pubdate datetime="<?php comment_time( 'c' ); ?>">
                    <?php
                        /* translators: 1: date, 2: time */
                        printf( __( '%1$s at %2$s', 'publish' ), get_comment_date(), get_comment_time() ); ?>
                    </time></a>
                </div><!-- .comment-meta .commentmetadata -->
            </footer>
            <div class="comment-content"><?php comment_text(); ?></div>
        </article><!-- #comment-## -->
    <!-- edit end -->
    <?php die();
}
function ajax_comment_err($a) {
    header('HTTP/1.0 500 Internal Server Error');
    header('Content-Type: text/plain;charset=UTF-8');
    echo $a;
    exit;
}
function ihacklog_user_can_edit_comment($new_cmt_data,$comment_ID = 0) {
    if(current_user_can('edit_comment', $comment_ID)) {
        return true;
    }
    $comment = get_comment( $comment_ID );
    $old_timestamp = strtotime( $comment->comment_date);
    $new_timestamp = current_time('timestamp');
    // 不用get_comment_author_email($comment_ID) , get_comment_author_IP($comment_ID)
    $rs = $comment->comment_author_email === $new_cmt_data['comment_author_email']
        && $comment->comment_author_IP === $_SERVER['REMOTE_ADDR']
        && $new_timestamp - $old_timestamp < 3600;
    return $rs;
}

根据你主题的评论显示输出结构修改

<!-- Your comments here    edit start -->  和  <!-- edit end -->

之间的内容。

二、JS 代码,注意将里面的 http://cyhour.com 替换成你的网址。

/**
 * jQuery-Ajax-Comments by Bigfa.http://fatesinger.com/59
 */
jQuery(document).ready(function(jQuery) {
    var jQuerycommentform = jQuery('#commentform'),
    txt1 = '<div id="loading"><img src="https://cyhour.com/wp-admin/images/wpspin_light.gif" style="vertical-align:middle;" alt=""/>  正在提交, 请稍候...</div>',
    txt2 = '<div id="error">#</div>',
    txt3 = '"><img src="https://cyhour.com/wp-admin/images/yes.png" style="vertical-align:middle;" alt=""/> 评论提交成功',
    edt1 = ', 刷新页面之前您可以<a rel="nofollow" class="comment-reply-link" href="#edit" onclick=\'return addComment.moveForm("',
    edt2 = ')\'>点击这里修改评论</a>',
    cancel_edit = '取消编辑',
    edit,
    num = 1,
    jQuerycomments = jQuery('#comments-title'),
    jQuerycancel = jQuery('#cancel-comment-reply-link'),
    cancel_text = jQuerycancel.text(),
    jQuerysubmit = jQuery('#commentform #submit');
    jQuerysubmit.attr('disabled', false),
    jQuerybody = (window.opera) ? (document.compatMode == "CSS1Compat" ? jQuery('html') : jQuery('body')) : jQuery('html,body'),
    comm_array = [];
    comm_array.push('');
    jQuery('#comment').after(txt1 + txt2);
    jQuery('#loading').hide();
    jQuery('#error').hide();
    jQuery(document).on("submit", "#commentform",
    function() {
        if (edit) jQuery('#comment').after('<input type="text" name="edit_id" id="edit_id" value="' + edit + '" style="display:none;" />');
        editcode();
        jQuerysubmit.attr('disabled', true).fadeTo('slow', 0.5);
        jQuery('#loading').slideDown();
        jQuery.ajax({
            url: 'https://cyhour.com/wp-admin/admin-ajax.php',
            data: jQuery(this).serialize() + "&action=ajax_comment",
            type: jQuery(this).attr('method'),
            error: function(request) {
                jQuery('#loading').hide();
                jQuery("#error").slideDown().html(request.responseText);
                setTimeout(function() {
                    jQuerysubmit.attr('disabled', false).fadeTo('slow', 1);
                    jQuery('#error').slideUp();
                },
                3000);
            },
            success: function(data) {
                jQuery('#loading').hide();
                comm_array.push(jQuery('#comment').val());
                jQuery('textarea').each(function() {
                    this.value = ''
                });
                var t = addComment,
                cancel = t.I('cancel-comment-reply-link'),
                temp = t.I('wp-temp-form-div'),
                respond = t.I(t.respondId),
                post = t.I('comment_post_ID').value,
                parent = t.I('comment_parent').value;
                if (!edit && jQuerycomments.length) {
                    n = parseInt(jQuerycomments.text().match(/\d+/));
                    jQuerycomments.text(jQuerycomments.text().replace(n, n + 1));
                }
                new_htm = '" id="new_comm_' + num + '"></';
                new_htm = (parent == '0') ? ('\n<ol style="clear:both;" class="commentlist' + new_htm + 'ol>') : ('\n<ul class="children' + new_htm + 'ul>');
                ok_htm = '\n<div class="ajax-notice" id="success_' + num + txt3;
                div_ = (document.body.innerHTML.indexOf('div-comment-') == -1) ? '': ((document.body.innerHTML.indexOf('li-comment-') == -1) ? 'div-': '');
                ok_htm = ok_htm.concat(edt1, div_, 'comment-', parent, '", "', parent, '", "respond", "', post, '", ', num, edt2);
                ok_htm += '</span><span></span>\n';
                ok_htm += '</div>\n';
                jQuery('#respond').before(new_htm);
                jQuery('#new_comm_' + num).append(data);
                jQuery('#new_comm_' + num + ' li').append(ok_htm);
                jQuerybody.animate({
                    scrollTop: jQuery('#new_comm_' + num).offset().top - 200
                },
                900);
                countdown();
                num++;
                edit = '';
                jQuery('*').remove('#edit_id');
                cancel.style.display = 'none';
                cancel.onclick = null;
                t.I('comment_parent').value = '0';
                if (temp && respond) {
                    temp.parentNode.insertBefore(respond, temp);
                    temp.parentNode.removeChild(temp)
                }
            }
        });
        return false;
    });
    addComment = {
        moveForm: function(commId, parentId, respondId, postId, num) {
            var t = this,
            div,
            comm = t.I(commId),
            respond = t.I(respondId),
            cancel = t.I('cancel-comment-reply-link'),
            parent = t.I('comment_parent'),
            post = t.I('comment_post_ID');
            if (edit) exit_prev_edit();
            num ? (t.I('comment').value = comm_array[num], edit = t.I('new_comm_' + num).innerHTML.match(/(comment-)(\d+)/)[2], jQuerynew_sucs = jQuery('#success_' + num), jQuerynew_sucs.hide(), jQuerynew_comm = jQuery('#new_comm_' + num), jQuerynew_comm.hide(), jQuerycancel.text(cancel_edit)) : jQuerycancel.text(cancel_text);
            t.respondId = respondId;
            postId = postId || false;
            if (!t.I('wp-temp-form-div')) {
                div = document.createElement('div');
                div.id = 'wp-temp-form-div';
                div.style.display = 'none';
                respond.parentNode.insertBefore(div, respond)
            } ! comm ? (temp = t.I('wp-temp-form-div'), t.I('comment_parent').value = '0', temp.parentNode.insertBefore(respond, temp), temp.parentNode.removeChild(temp)) : comm.parentNode.insertBefore(respond, comm.nextSibling);
            jQuerybody.animate({
                scrollTop: jQuery('#respond').offset().top - 180
            },
            400);
            if (post && postId) post.value = postId;
            parent.value = parentId;
            cancel.style.display = '';
            cancel.onclick = function() {
                if (edit) exit_prev_edit();
                var t = addComment,
                temp = t.I('wp-temp-form-div'),
                respond = t.I(t.respondId);
                t.I('comment_parent').value = '0';
                if (temp && respond) {
                    temp.parentNode.insertBefore(respond, temp);
                    temp.parentNode.removeChild(temp);
                }
                this.style.display = 'none';
                this.onclick = null;
                return false;
            };
            try {
                t.I('comment').focus();
            }
             catch(e) {}
            return false;
        },
        I: function(e) {
            return document.getElementById(e);
        }
    };
    function exit_prev_edit() {
        jQuerynew_comm.show();
        jQuerynew_sucs.show();
        jQuery('textarea').each(function() {
            this.value = ''
        });
        edit = '';
    }
    var wait = 15,
    submit_val = jQuerysubmit.val();
    function countdown() {
        if (wait > 0) {
            jQuerysubmit.val(wait);
            wait--;
            setTimeout(countdown, 1000);
        } else {
            jQuerysubmit.val(submit_val).attr('disabled', false).fadeTo('slow', 1);
            wait = 15;
        }
    }
    function editcode() {
        var a = "",
        b = jQuery("#comment").val(),
        start = b.indexOf("<code>"),
        end = b.indexOf("</code>");
        if (start > -1 && end > -1 && start < end) {
            a = "";
            while (end != -1) {
                a += b.substring(0, start + 6) + b.substring(start + 6, end).replace(/<(?=[^>]*?>)/gi, "&lt;").replace(/>/gi, "&gt;");
                b = b.substring(end + 7, b.length);
                start = b.indexOf("<code>") == -1 ? -6: b.indexOf("<code>");
                end = b.indexOf("</code>");
                if (end == -1) {
                    a += "</code>" + b;
                    jQuery("#comment").val(a)
                } else if (start == -6) {
                    myFielde += "&lt;/code&gt;"
                } else {
                    a += "</code>"
                }
            }
        }
        var b = a ? a: jQuery("#comment").val(),
        a = "",
        start = b.indexOf("<pre>"),
        end = b.indexOf("</pre>");
        if (start > -1 && end > -1 && start < end) {
            a = a
        } else return;
        while (end != -1) {
            a += b.substring(0, start + 5) + b.substring(start + 5, end).replace(/<(?=[^>]*?>)/gi, "&lt;").replace(/>/gi, "&gt;");
            b = b.substring(end + 6, b.length);
            start = b.indexOf("<pre>") == -1 ? -5: b.indexOf("<pre>");
            end = b.indexOf("</pre>");
            if (end == -1) {
                a += "</pre>" + b;
                jQuery("#comment").val(a)
            } else if (start == -5) {
                myFielde += "&lt;/pre&gt;"
            } else {
                a += "</pre>"
            }
        }
    }
    function grin(a) {
        var b;
        a = " " + a + " ";
        if (document.getElementById("comment") && document.getElementById("comment").type == "textarea") {
            b = document.getElementById("comment")
        } else {
            return false
        }
        if (document.selection) {
            b.focus();
            sel = document.selection.createRange();
            sel.text = a;
            b.focus()
        } else if (b.selectionStart || b.selectionStart == "0") {
            var c = b.selectionStart;
            var d = b.selectionEnd;
            var e = d;
            b.value = b.value.substring(0, c) + a + b.value.substring(d, b.value.length);
            e += a.length;
            b.focus();
            b.selectionStart = e;
            b.selectionEnd = e
        } else {
            b.value += a;
            b.focus()
        }
    }
});

将 js代码保存为 comments-ajax.js 文件,放到主题目录下的js目录里面。然后在主题functions.php里面加载 jQuery 库 和 comments-ajax.js即可。

if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
    wp_enqueue_script('jquery');//如果主题在其他地方加载了 jQuery 库,可以删除此行
    wp_enqueue_script('comments-ajax', (get_template_directory_uri()."/js/comments-ajax.js"));
}

本文转自:https://cyhour.com/wordpress-new-ajax-comments.html

本文共 12 个回复

  1. JV says:
    很高端,很凶残 :idea:
    1. 小二苘 PO主 says:
      @JV :wink: 都是瞎折腾,正在研究怎么全站全站ajax翻页
      1. JV says:
        @小二苘 ajax本身不难的,看一下jq的ajax方法,然后找一块比较典型的代码一句句理解一下就OK了。
  2. 萌口组 says:
    哇!突然更新了好多还是技术向!
  3. 萌口组 says:
    好想放到萌口组上用呢!诶。。没修改成功?

发表留言,喵!