Advertisement
tpflanz

Worpress Backend Editor Comments on Post

Apr 20th, 2012
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.25 KB | None | 0 0
  1. <?php
  2.         /*
  3.         Plugin Name: Unpublished Post: Editor Comments
  4.         Plugin URI: http://travis.pflanz.me/2012/unpublished-post-editor-comments-wordpress-plugin
  5.         Description: Plugin for allowing Editors and Administrators to leave comments on an unpublished post. The comments will be published to the frontend when the post is published.
  6.         Author: Travis Pflanz
  7.         Version: 1.0
  8.         Author URI: http://Travis.Pflanz.ME
  9.         */
  10.  
  11. /* Add a new meta box to the admin menu. */
  12.     add_action( 'admin_menu', 'editor_comment_create_meta_box' );
  13.  
  14. /* Saves the meta box data. */
  15.     add_action( 'save_post', 'editor_comment_save_meta_data' );
  16.  
  17. function editor_comment_create_meta_box() {
  18.     global $theme_name;
  19.  
  20.     add_meta_box( 'post-meta-boxes', __('Editor Comment on the Post'), 'post_meta_boxes', 'post', 'normal', 'high' );
  21. }
  22.  
  23. function editor_comment_post_meta_boxes() {
  24.  
  25.     /* Array of the meta box options. */
  26.     $meta_boxes = array(
  27.         'editor_commets' => array( 'name' => 'Comments', 'title' => __('Comments', 'upec'), 'type' => 'textarea' ),
  28.        
  29.     );
  30.  
  31.     return apply_filters( 'editor_comment_post_meta_boxes', $meta_boxes );
  32. }
  33. function post_meta_boxes() {
  34.     global $post;
  35.     $meta_boxes = editor_comment_post_meta_boxes(); ?>
  36.  
  37.     <table class="form-table">
  38.     <?php foreach ( $meta_boxes as $meta ) :
  39.  
  40.         $value = get_post_meta( $post->ID, $meta['name'], true );
  41.  
  42.         if ( $meta['type'] == 'text' )
  43.             get_meta_text_input( $meta, $value );
  44.         elseif ( $meta['type'] == 'textarea' )
  45.             get_meta_textarea( $meta, $value );
  46.         elseif ( $meta['type'] == 'select' )
  47.             get_meta_select( $meta, $value );
  48.  
  49.     endforeach; ?>
  50.     </table>
  51. <?php
  52. }
  53.  
  54. function get_meta_text_input( $args = array(), $value = false ) {
  55.  
  56.     extract( $args ); ?>
  57.  
  58.     <tr>
  59.         <th style="width:10%;">
  60.             <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
  61.         </th>
  62.         <td>
  63.             <input type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo wp_specialchars( $value, 1 ); ?>" size="30" tabindex="30" style="width: 97%;" />
  64.             <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
  65.         </td>
  66.     </tr>
  67.     <?php
  68. }
  69.  
  70. function get_meta_select( $args = array(), $value = false ) {
  71.  
  72.     extract( $args ); ?>
  73.  
  74.     <tr>
  75.         <th style="width:10%;">
  76.             <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
  77.         </th>
  78.         <td>
  79.             <select name="<?php echo $name; ?>" id="<?php echo $name; ?>">
  80.             <?php foreach ( $options as $option ) : ?>
  81.                 <option <?php if ( htmlentities( $value, ENT_QUOTES ) == $option ) echo ' selected="selected"'; ?>>
  82.                     <?php echo $option; ?>
  83.                 </option>
  84.             <?php endforeach; ?>
  85.             </select>
  86.             <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
  87.         </td>
  88.     </tr>
  89.     <?php
  90. }
  91.  
  92. function get_meta_textarea( $args = array(), $value = false ) {
  93.  
  94.     extract( $args ); ?>
  95.  
  96.     <tr>
  97.         <th style="width:10%;">
  98.             <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
  99.         </th>
  100.         <td>
  101.             <textarea name="<?php echo $name; ?>" id="<?php echo $name; ?>" cols="60" rows="4" tabindex="30" style="width: 97%;"><?php echo wp_specialchars( $value, 1 ); ?></textarea>
  102.             <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
  103.         </td>
  104.     </tr>
  105.     <?php
  106. }
  107.  
  108. function editor_comment_save_meta_data( $post_id ) {
  109.     global $post;
  110.  
  111.             $meta_boxes = array_merge( editor_comment_post_meta_boxes() );
  112.  
  113.     foreach ( $meta_boxes as $meta_box ) :
  114.  
  115.         if ( !wp_verify_nonce( $_POST[$meta_box['name'] . '_noncename'], plugin_basename( __FILE__ ) ) )
  116.             return $post_id;
  117.  
  118.         elseif ( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
  119.             return $post_id;
  120.  
  121.         $data = stripslashes( $_POST[$meta_box['name']] );
  122.  
  123.         if ( get_post_meta( $post_id, $meta_box['name'] ) == '' )
  124.             add_post_meta( $post_id, $meta_box['name'], $data, true );
  125.  
  126.         elseif ( $data != get_post_meta( $post_id, $meta_box['name'], true ) )
  127.             update_post_meta( $post_id, $meta_box['name'], $data );
  128.  
  129.         elseif ( $data == '' )
  130.             delete_post_meta( $post_id, $meta_box['name'], get_post_meta( $post_id, $meta_box['name'], true ) );
  131.  
  132.     endforeach;
  133. }  
  134. add_action('pending_to_future', 'insert_editor_comment');
  135. add_action('pending_to_publish', 'insert_editor_comment');
  136.  
  137. function insert_editor_comment( $data){
  138.  
  139. global $current_user;
  140.     get_currentuserinfo();
  141.  
  142. global $post;
  143.  
  144. $time = current_time('mysql');
  145. $editor_comment = get_post_meta($post->ID, 'Comments', TRUE);
  146.  
  147. $data = array(
  148. 'comment_post_ID' => $post->ID,
  149. 'comment_author' => $current_user->display_name,
  150. 'comment_author_email' => $current_user->user_email,
  151. 'comment_author_url' => $current_user->user_url,
  152. 'comment_content' => $editor_comment,
  153. 'comment_type' => 'comment',
  154. 'comment_parent' => 0,
  155. 'user_id' => $current_user->ID ,
  156. 'comment_author_IP' => '127.0.0.1',
  157. 'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)',
  158. 'comment_date' => $time,
  159. 'comment_approved' => 1,
  160. 'comment_subscribe' => 'Y',
  161. );
  162.  
  163. wp_insert_comment($data);
  164. }
  165. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement