WP 4.4.1允许通过ADD_ACTION‘PRE_COMMENT_ON_POST’空评论

时间:2016-01-20 作者:Becki Beckmann

有一个关于允许WorpPress v.4.4.1的空评论的问题

为此,我使用“pre\\u comment\\u on\\u post”操作。在WP 4.3.1中,我可以轻松做到这一点:

if (isset($_POST[\'comment\']) && $_POST[\'comment\'] == "") {

                    $_POST[\'comment\'] = "dummy content";
}
稍后将通过另一个add\\u filter调用筛选出虚拟内容。。。wp 4.3.1中的wp评论帖子。php是以下代码行:

$comment_content      = ( isset($_POST[\'comment\']) ) ? trim($_POST[\'comment\']) : null;
因此,我的add\\u操作非常有效;)

现在有了wp 4.4.1,核心开发人员更改了代码,并在/wp includes/comment中引入了一个4.4.0的新功能。php在线2627

function wp_handle_comment_submission( $comment_data ) {


$comment_post_ID = $comment_parent = 0;
$comment_author = $comment_author_email = $comment_author_url = $comment_content = $_wp_unfiltered_html_comment = null;

if ( isset( $comment_data[\'comment_post_ID\'] ) ) {
    $comment_post_ID = (int) $comment_data[\'comment_post_ID\'];
}
if ( isset( $comment_data[\'author\'] ) && is_string( $comment_data[\'author\'] ) ) {
    $comment_author = trim( strip_tags( $comment_data[\'author\'] ) );
}
if ( isset( $comment_data[\'email\'] ) && is_string( $comment_data[\'email\'] ) ) {
    $comment_author_email = trim( $comment_data[\'email\'] );
}
if ( isset( $comment_data[\'url\'] ) && is_string( $comment_data[\'url\'] ) ) {
    $comment_author_url = trim( $comment_data[\'url\'] );
}
if ( isset( $comment_data[\'comment\'] ) && is_string( $comment_data[\'comment\'] ) ) {
    $comment_content = trim( $comment_data[\'comment\'] );

}
if ( isset( $comment_data[\'comment_parent\'] ) ) {
    $comment_parent = absint( $comment_data[\'comment_parent\'] );
}
if ( isset( $comment_data[\'_wp_unfiltered_html_comment\'] ) && is_string( $comment_data[\'_wp_unfiltered_html_comment\'] ) ) {
    $_wp_unfiltered_html_comment = trim( $comment_data[\'_wp_unfiltered_html_comment\'] );
}

$post = get_post( $comment_post_ID );

if ( empty( $post->comment_status ) ) {

    /**
     * Fires when a comment is attempted on a post that does not exist.
     *
     * @since 1.5.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( \'comment_id_not_found\', $comment_post_ID );

    return new WP_Error( \'comment_id_not_found\' );

}

// get_post_status() will get the parent status for attachments.
$status = get_post_status( $post );

if ( ( \'private\' == $status ) && ! current_user_can( \'read_post\', $comment_post_ID ) ) {
    return new WP_Error( \'comment_id_not_found\' );
}

$status_obj = get_post_status_object( $status );

if ( ! comments_open( $comment_post_ID ) ) {

    /**
     * Fires when a comment is attempted on a post that has comments closed.
     *
     * @since 1.5.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( \'comment_closed\', $comment_post_ID );

    return new WP_Error( \'comment_closed\', __( \'Sorry, comments are closed for this item.\' ), 403 );

} elseif ( \'trash\' == $status ) {

    /**
     * Fires when a comment is attempted on a trashed post.
     *
     * @since 2.9.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( \'comment_on_trash\', $comment_post_ID );

    return new WP_Error( \'comment_on_trash\' );

} elseif ( ! $status_obj->public && ! $status_obj->private ) {

    /**
     * Fires when a comment is attempted on a post in draft mode.
     *
     * @since 1.5.1
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( \'comment_on_draft\', $comment_post_ID );

    return new WP_Error( \'comment_on_draft\' );

} elseif ( post_password_required( $comment_post_ID ) ) {

    /**
     * Fires when a comment is attempted on a password-protected post.
     *
     * @since 2.9.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( \'comment_on_password_protected\', $comment_post_ID );

    return new WP_Error( \'comment_on_password_protected\' );

} else {

    /**
     * Fires before a comment is posted.
     *
     * @since 2.8.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( \'pre_comment_on_post\', $comment_post_ID );

}



// If the user is logged in
$user = wp_get_current_user();
if ( $user->exists() ) {
    if ( empty( $user->display_name ) ) {
        $user->display_name=$user->user_login;
    }
    $comment_author       = $user->display_name;
    $comment_author_email = $user->user_email;
    $comment_author_url   = $user->user_url;
    $user_ID              = $user->ID;
    if ( current_user_can( \'unfiltered_html\' ) ) {
        if ( ! isset( $comment_data[\'_wp_unfiltered_html_comment\'] )
            || ! wp_verify_nonce( $comment_data[\'_wp_unfiltered_html_comment\'], \'unfiltered-html-comment_\' . $comment_post_ID )
        ) {
            kses_remove_filters(); // start with a clean slate
            kses_init_filters(); // set up the filters
        }
    }
} else {
    if ( get_option( \'comment_registration\' ) ) {
        return new WP_Error( \'not_logged_in\', __( \'Sorry, you must be logged in to post a comment.\' ), 403 );
    }
}

$comment_type = \'\';

if ( get_option( \'require_name_email\' ) && ! $user->exists() ) {
    if ( 6 > strlen( $comment_author_email ) || \'\' == $comment_author ) {
        return new WP_Error( \'require_name_email\', __( \'<strong>ERROR</strong>: please fill the required fields (name, email).\' ), 200 );
    } elseif ( ! is_email( $comment_author_email ) ) {
        return new WP_Error( \'require_valid_email\', __( \'<strong>ERROR</strong>: please enter a valid email address.\' ), 200 );
    }
}

if ( \'\' == $comment_content ) {
    return new WP_Error( \'require_valid_comment\', __( \'<strong>ERROR</strong>: please type a comment.\' ), 200 );
}

$commentdata = compact(
    \'comment_post_ID\',
    \'comment_author\',
    \'comment_author_email\',
    \'comment_author_url\',
    \'comment_content\',
    \'comment_type\',
    \'comment_parent\',
    \'user_ID\'
);

$comment_id = wp_new_comment( wp_slash( $commentdata ) );
if ( ! $comment_id ) {
    return new WP_Error( \'comment_save_error\', __( \'<strong>ERROR</strong>: The comment could not be saved. Please try again later.\' ), 500 );
}

return get_comment( $comment_id );
}

并且超级全局$\\u POST被传递给wp comments POST中的函数。php

$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
因此,现在在wp 4.4.1中,为$\\u POST[\'comment\']赋值没有任何效果,因为在新函数中,它们将$\\u POST[\'comment\']映射到$comment\\u内容

因此,我想在我的add\\u action函数中简单地将$comment\\u内容设置为全局:

    function action_pre_comment_on_post($comment_post_ID) {

    global $comment_content;


                if (isset($_POST[\'comment\']) && $_POST[\'comment\'] == "") {

                    $comment_content = "dummy comment";

                return;

            }

}   
但遗憾的是,这不起作用:(因为$comment\\u内容没有在全局范围内声明(

如果我修改核心代码并在全局范围内定义$comment\\u内容:

function wp_handle_comment_submission( $comment_data ) {

global $comment_content;
我的add\\u action函数现在可以向$comment\\u内容传递一个值,一切正常。但您实际上不应该修改核心代码!我应该在函数中声明$comment\\u content global吗。php或通过add\\u操作“init”?或

所以你怎样才能最好地解决这个案子?

感谢您的帮助;)

感谢(&A);所有bestbecki

1 个回复
SO网友:Becki Beckmann

)

首先,非常感谢@swisspidy和在init hook上修改$\\u帖子的建议。

但在仔细思考和查看代码之后,我采取了一种不同的方法。

在表单提交之后,我决定在表单提交期间通过jQuery进行修复,并对那里的注释输入字段进行必要的检查,而不是事后进行修复。

我已经通过jquery验证插件进行了表单验证:

http://jqueryvalidation.org/

它确实提供了一个自定义提交处理程序

http://jqueryvalidation.org/documentation/

我决定修复自定义submitHandler中的问题,如果选中了其他复选框#addTimeSheet且未填写#comment字段,则为#comment指定一个值:

jQuery(document).ready(function() {jQuery("#commentform").validate({

   submitHandler: function(form) {
    // do other things for a valid form

        if(jQuery("#addTimeSheet").is(":checked") == true) {

            if(jQuery("#comment").val() == "") {


                jQuery("#comment").val("%comment-dummy%");

            }

        }          

    form.submit();
  }
,则,

这样我就不需要事后再解决问题,我认为这是一个更优雅的解决方案;)

无论如何,再次感谢您的帮助&;祝你一切顺利

问候贝基