Skip to content

Commit

Permalink
Added Comment Metabox support
Browse files Browse the repository at this point in the history
As Per: CMB2#238

*Changes in CMB2.php are optional for functionality.
  • Loading branch information
GregLancaster71 committed Mar 9, 2015
1 parent e863b18 commit a06731b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
5 changes: 5 additions & 0 deletions includes/CMB2.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ public function object_id( $object_id = 0 ) {
$object_id = ! $object_id && isset( $GLOBALS['user_ID'] ) ? $GLOBALS['user_ID'] : $object_id;
break;

case 'comment':
$object_id = isset( $_REQUEST['c'] ) ? $_REQUEST['c'] : $object_id;
$object_id = ! $object_id && isset( $GLOBALS['comments']->comment_ID ) ? $GLOBALS['comments']->comment_ID : $object_id;
break;

default:
$object_id = isset( $GLOBALS['post']->ID ) ? $GLOBALS['post']->ID : $object_id;
$object_id = isset( $_REQUEST['post'] ) ? $_REQUEST['post'] : $object_id;
Expand Down
72 changes: 71 additions & 1 deletion includes/CMB2_hookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ public function admin_hooks() {
$this->once( 'admin_head', array( $this, 'add_post_enctype' ) );
}

} elseif ( 'comment' == $type ) {
// Optionally - You could use the `add_meta_boxes_comment` hook.
add_action( 'add_meta_boxes_comment', array( $this, 'add_comment_metaboxes' ) );
add_action( 'add_attachment', array( $this, 'edit_comment' ) );
add_action( 'edit_attachment', array( $this, 'edit_comment' ) );
add_action( 'edit_comment', array( $this, 'edit_comment' ), 10, 2 );

$this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) );

if ( $has_upload && $pagenow === 'comment.php' ) {
$this->once( 'admin_head', array( $this, 'add_post_enctype' ) );
}

} elseif ( 'user' == $type ) {

$priority = $this->cmb->prop( 'priority' );
Expand Down Expand Up @@ -193,7 +206,7 @@ public static function register_scripts() {
*/
public function do_scripts( $hook ) {
// only enqueue our scripts/styles on the proper pages
if ( in_array( $hook, array( 'post.php', 'post-new.php', 'page-new.php', 'page.php' ), true ) ) {
if ( in_array( $hook, array( 'post.php', 'post-new.php', 'page-new.php', 'page.php', 'comment.php' ), true ) ) {
if ( $this->cmb->prop( 'cmb_styles' ) ) {
self::enqueue_cmb_css();
}
Expand Down Expand Up @@ -238,6 +251,25 @@ public function add_metaboxes() {
}
}

/**
* Add metaboxes (to 'comment' object type)
*/
public function add_comment_metaboxes() {

if ( ! $this->show_on() ) {
return;
}

foreach ( $this->cmb->prop( 'object_types' ) as $page ) {

if ( $this->cmb->prop( 'closed' ) ) {
add_filter( "postbox_classes_{$page}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) );
}

add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'comment_metabox' ), $page, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) );
}
}

/**
* Add 'closed' class to metabox
* @since 2.0.0
Expand All @@ -257,6 +289,14 @@ public function post_metabox() {
$this->cmb->show_form( get_the_ID(), 'post' );
}

/**
* Display metaboxes for a comment object
* @since 1.0.0
*/
public function comment_metabox() {
$this->cmb->show_form( get_comment_ID(), 'comment' );
}

/**
* Display metaboxes for new user page
* @since 1.0.0
Expand Down Expand Up @@ -320,6 +360,36 @@ public function save_post( $post_id, $post = false ) {
$this->cmb->save_fields( $post_id, 'post', $_POST );
}

/**
* Save data from metabox
*/
public function edit_comment( $comment_id, $comment = false ) {

$comment_type = $comment ? $comment->comment_type : get_comment_type( $comment_id );

$do_not_pass_go = (
// check nonce
! isset( $_POST[ $this->cmb->nonce() ] )
|| ! wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() )
// check if autosave
|| defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE
// commented out - fails on custom comment types due to weak filtering in `get_comment_type()`
// || ( 'comment' !== $comment_type && ! current_user_can( 'moderate_comments', $comment_id ) )
// check user editing permissions
|| ! current_user_can( 'moderate_comments', $comment_id )
// get the metabox post_types & compare it to this post_type
|| ! in_array( $comment_type, $this->cmb->prop( 'object_types' ) )
);

if ( $do_not_pass_go ) {
// do not collect $200
return;
}

// take a trip to reading railroad – if you pass go collect $200
$this->cmb->save_fields( $comment_id, 'comment', $_POST );
}

/**
* Save data from metabox
*/
Expand Down

0 comments on commit a06731b

Please sign in to comment.