如何启用待定帖子和草稿帖子的评论?

时间:2012-11-26 作者:Andrew

我有一个启用了注释的自定义帖子类型。管理员中的注释元框仅在发布帖子时显示。我想使用评论系统作为某些作者之间的更多讨论(它将隐藏在前端),因此我希望评论系统在帖子起草、待定或发布时处于活动状态。这可能吗?

我应该注意到,我已经尝试了编辑流插件,该插件具有“编辑评论”功能,但它在功能等方面引入了更多问题,不适合我正在尝试实现的目标。

1 个回复
最合适的回答,由SO网友:brasofilo 整理而成

下面启用的默认元框draftpending 职位(在portfolio post_type):

add_action( \'admin_init\', \'wpse_74018_enable_draft_comments\' );

/**
 * Add Comments Meta Box if CPT is \'draft\' or \'pending\'
 */
function wpse_74018_enable_draft_comments()
{
    if( isset( $_GET[\'post\'] ) ) 
    {
        $post_id = absint( $_GET[\'post\'] ); 
        $post = get_post( $post_id ); 
        if ( \'draft\' == $post->post_status || \'pending\' == $post->post_status )
            add_meta_box(
                \'commentsdiv\', 
                __(\'Comments\'), 
                \'post_comment_meta_box\', 
                \'portfolio\', // CHANGE FOR YOUR CPT
                \'normal\', 
                \'core\'
            );
    }
}
但是,我们无法评论。。。error commenting in draft post

错误产生于/wp-admin/includes/ajax-actions.php, 第736行:

要更正此问题,必须通过以下方式拦截Ajax调用:

add_action( \'admin_init\', \'wpse_74018_enable_custom_ajax_comments\' );

function wpse_74018_enable_custom_ajax_comments()
{
    add_action( \'wp_ajax_replyto-comment\', \'wpse_74018_custom_callback\', 0 );
}

/**
 * Copy of wp_ajax_replyto_comment()
 * /wp-admin/includes/ajax-actions.php
 *
 * Adjust the CPT that defines $diff_status   
 */
function wpse_74018_custom_callback( $action ) {
    global $wp_list_table, $wpdb;
    if ( empty( $action ) )
        $action = \'replyto-comment\';

    check_ajax_referer( $action, \'_ajax_nonce-replyto-comment\' );

    set_current_screen( \'edit-comments\' );

    $comment_post_ID = (int) $_POST[\'comment_post_ID\'];
    if ( !current_user_can( \'edit_post\', $comment_post_ID ) )
        wp_die( -1 );

    $status = $wpdb->get_var( $wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );

    if( \'portfolio\' == get_post_type( $comment_post_ID ) )
        $diff_status = array(\'trash\');
    else
        $diff_status = array(\'draft\',\'pending\',\'trash\');
    
    if ( empty($status) )
        wp_die( 1 );
    elseif ( in_array($status, $diff_status ) )
        wp_die( __(\'ERROR: you are replying to a comment on a draft post.\') );

    $user = wp_get_current_user();
    if ( $user->exists() ) {
        $user_ID = $user->ID;
        $comment_author       = $wpdb->escape($user->display_name);
        $comment_author_email = $wpdb->escape($user->user_email);
        $comment_author_url   = $wpdb->escape($user->user_url);
        $comment_content      = trim($_POST[\'content\']);
        if ( current_user_can( \'unfiltered_html\' ) ) {
            if ( wp_create_nonce( \'unfiltered-html-comment\' ) != $_POST[\'_wp_unfiltered_html_comment\'] ) {
                kses_remove_filters(); // start with a clean slate
                kses_init_filters(); // set up the filters
            }
        }
    } else {
        wp_die( __( \'Sorry, you must be logged in to reply to a comment.\' ) );
    }

    if ( \'\' == $comment_content )
        wp_die( __( \'ERROR: please type a comment.\' ) );

    $comment_parent = absint($_POST[\'comment_ID\']);
    $comment_auto_approved = false;
    $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( $commentdata );
    $comment = get_comment($comment_id);
    if ( ! $comment ) wp_die( 1 );

    $position = ( isset($_POST[\'position\']) && (int) $_POST[\'position\'] ) ? (int) $_POST[\'position\'] : \'-1\';

    // automatically approve parent comment
    if ( !empty($_POST[\'approve_parent\']) ) {
        $parent = get_comment( $comment_parent );

        if ( $parent && $parent->comment_approved === \'0\' && $parent->comment_post_ID == $comment_post_ID ) {
            if ( wp_set_comment_status( $parent->comment_ID, \'approve\' ) )
                $comment_auto_approved = true;
        }
    }

    ob_start();
        if ( \'dashboard\' == $_REQUEST[\'mode\'] ) {
            require_once( ABSPATH . \'wp-admin/includes/dashboard.php\' );
            _wp_dashboard_recent_comments_row( $comment );
        } else {
            if ( \'single\' == $_REQUEST[\'mode\'] ) {
                $wp_list_table = _get_list_table(\'WP_Post_Comments_List_Table\');
            } else {
                $wp_list_table = _get_list_table(\'WP_Comments_List_Table\');
            }
            $wp_list_table->single_row( $comment );
        }
        $comment_list_item = ob_get_contents();
    ob_end_clean();

    $response =  array(
        \'what\' => \'comment\',
        \'id\' => $comment->comment_ID,
        \'data\' => $comment_list_item,
        \'position\' => $position
    );

    if ( $comment_auto_approved )
        $response[\'supplemental\'] = array( \'parent_approved\' => $parent->comment_ID );

    $x = new WP_Ajax_Response();
    $x->add( $response );
    $x->send();
}
从编辑角度来看,我知道评论系统和界面会更好。另一种方法是创建一个带有可重复字段的自定义元框,可以将消息留在帖子中。

Basic implementation:

Plugins of interest:


进一步开发

我开发了一个具有更多功能的插件
Kudos@toscho,这为Ajax查询指出了一个入口点。

Private Comments for CPT
在编辑草稿或挂起的帖子时,为给定的自定义帖子类型启用内部评论。注释仅在后端可见。并在仪表板中标记为内部。

结束