web/wp-admin/includes/comment.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * {@internal Missing Short Description}}
    10  * {@internal Missing Short Description}}
    11  *
    11  *
    12  * @since unknown
    12  * @since 2.0.0
    13  * @uses $wpdb
    13  * @uses $wpdb
    14  *
    14  *
    15  * @param string $comment_author
    15  * @param string $comment_author Author of the comment
    16  * @param string $comment_date
    16  * @param string $comment_date Date of the comment
    17  * @return mixed Comment ID on success.
    17  * @return mixed Comment ID on success.
    18  */
    18  */
    19 function comment_exists($comment_author, $comment_date) {
    19 function comment_exists($comment_author, $comment_date) {
    20 	global $wpdb;
    20 	global $wpdb;
    21 
    21 
    25 	return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
    25 	return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
    26 			WHERE comment_author = %s AND comment_date = %s", $comment_author, $comment_date) );
    26 			WHERE comment_author = %s AND comment_date = %s", $comment_author, $comment_date) );
    27 }
    27 }
    28 
    28 
    29 /**
    29 /**
    30  * {@internal Missing Short Description}}
    30  * Update a comment with values provided in $_POST.
    31  *
    31  *
    32  * @since unknown
    32  * @since 2.0.0
    33  */
    33  */
    34 function edit_comment() {
    34 function edit_comment() {
    35 
    35 
    36 	$comment_post_ID = (int) $_POST['comment_post_ID'];
    36 	if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) )
    37 
    37 		wp_die ( __( 'You are not allowed to edit comments on this post.' ) );
    38 	if (!current_user_can( 'edit_post', $comment_post_ID ))
       
    39 		wp_die( __('You are not allowed to edit comments on this post, so you cannot edit this comment.' ));
       
    40 
    38 
    41 	$_POST['comment_author'] = $_POST['newcomment_author'];
    39 	$_POST['comment_author'] = $_POST['newcomment_author'];
    42 	$_POST['comment_author_email'] = $_POST['newcomment_author_email'];
    40 	$_POST['comment_author_email'] = $_POST['newcomment_author_email'];
    43 	$_POST['comment_author_url'] = $_POST['newcomment_author_url'];
    41 	$_POST['comment_author_url'] = $_POST['newcomment_author_url'];
    44 	$_POST['comment_approved'] = $_POST['comment_status'];
    42 	$_POST['comment_approved'] = $_POST['comment_status'];
    50 			$_POST['edit_date'] = '1';
    48 			$_POST['edit_date'] = '1';
    51 			break;
    49 			break;
    52 		}
    50 		}
    53 	}
    51 	}
    54 
    52 
    55 	if (!empty ( $_POST['edit_date'] ) ) {
    53 	if ( !empty ( $_POST['edit_date'] ) ) {
    56 		$aa = $_POST['aa'];
    54 		$aa = $_POST['aa'];
    57 		$mm = $_POST['mm'];
    55 		$mm = $_POST['mm'];
    58 		$jj = $_POST['jj'];
    56 		$jj = $_POST['jj'];
    59 		$hh = $_POST['hh'];
    57 		$hh = $_POST['hh'];
    60 		$mn = $_POST['mn'];
    58 		$mn = $_POST['mn'];
    64 		$mn = ($mn > 59 ) ? $mn -60 : $mn;
    62 		$mn = ($mn > 59 ) ? $mn -60 : $mn;
    65 		$ss = ($ss > 59 ) ? $ss -60 : $ss;
    63 		$ss = ($ss > 59 ) ? $ss -60 : $ss;
    66 		$_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
    64 		$_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
    67 	}
    65 	}
    68 
    66 
    69 	wp_update_comment( $_POST);
    67 	wp_update_comment( $_POST );
    70 }
    68 }
    71 
    69 
    72 /**
    70 /**
    73  * {@internal Missing Short Description}}
    71  * {@internal Missing Short Description}}
    74  *
    72  *
    75  * @since unknown
    73  * @since 2.0.0
    76  *
    74  *
    77  * @param unknown_type $id
    75  * @param int $id ID of comment to retrieve
    78  * @return unknown
    76  * @return bool|object Comment if found. False on failure.
    79  */
    77  */
    80 function get_comment_to_edit( $id ) {
    78 function get_comment_to_edit( $id ) {
    81 	if ( !$comment = get_comment($id) )
    79 	if ( !$comment = get_comment($id) )
    82 		return false;
    80 		return false;
    83 
    81 
    94 
    92 
    95 	return $comment;
    93 	return $comment;
    96 }
    94 }
    97 
    95 
    98 /**
    96 /**
    99  * {@internal Missing Short Description}}
    97  * Get the number of pending comments on a post or posts
   100  *
    98  *
   101  * @since unknown
    99  * @since 2.3.0
   102  * @uses $wpdb
   100  * @uses $wpdb
   103  *
   101  *
   104  * @param int $post_id Post ID
   102  * @param int|array $post_id Either a single Post ID or an array of Post IDs
   105  * @return unknown
   103  * @return int|array Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
   106  */
   104  */
   107 function get_pending_comments_num( $post_id ) {
   105 function get_pending_comments_num( $post_id ) {
   108 	global $wpdb;
   106 	global $wpdb;
   109 
   107 
   110 	$single = false;
   108 	$single = false;
   111 	if ( !is_array($post_id) ) {
   109 	if ( !is_array($post_id) ) {
   112 		$post_id = (array) $post_id;
   110 		$post_id_array = (array) $post_id;
   113 		$single = true;
   111 		$single = true;
       
   112 	} else {
       
   113 		$post_id_array = $post_id;
   114 	}
   114 	}
   115 	$post_id = array_map('intval', $post_id);
   115 	$post_id_array = array_map('intval', $post_id_array);
   116 	$post_id = "'" . implode("', '", $post_id) . "'";
   116 	$post_id_in = "'" . implode("', '", $post_id_array) . "'";
   117 
   117 
   118 	$pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_N );
   118 	$pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_A );
   119 
   119 
   120 	if ( empty($pending) )
   120 	if ( $single ) {
   121 		return 0;
   121 		if ( empty($pending) )
   122 
   122 			return 0;
   123 	if ( $single )
   123 		else
   124 		return $pending[0][1];
   124 			return absint($pending[0]['num_comments']);
       
   125 	}
   125 
   126 
   126 	$pending_keyed = array();
   127 	$pending_keyed = array();
   127 	foreach ( $pending as $pend )
   128 
   128 		$pending_keyed[$pend[0]] = $pend[1];
   129 	// Default to zero pending for all posts in request
       
   130 	foreach ( $post_id_array as $id )
       
   131 		$pending_keyed[$id] = 0;
       
   132 
       
   133 	if ( !empty($pending) )
       
   134 		foreach ( $pending as $pend )
       
   135 			$pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);
   129 
   136 
   130 	return $pending_keyed;
   137 	return $pending_keyed;
   131 }
   138 }
   132 
   139 
   133 /**
   140 /**
   134  * Add avatars to relevant places in admin, or try to.
   141  * Add avatars to relevant places in admin, or try to.
   135  *
   142  *
   136  * @since unknown
   143  * @since 2.5.0
   137  * @uses $comment
   144  * @uses $comment
   138  *
   145  *
   139  * @param string $name User name.
   146  * @param string $name User name.
   140  * @return string Avatar with Admin name.
   147  * @return string Avatar with Admin name.
   141  */
   148  */
   142 function floated_admin_avatar( $name ) {
   149 function floated_admin_avatar( $name ) {
   143 	global $comment;
   150 	global $comment;
   144 
   151 	$avatar = get_avatar( $comment, 32 );
   145 	$id = $avatar = false;
       
   146 	if ( $comment->comment_author_email )
       
   147 		$id = $comment->comment_author_email;
       
   148 	if ( $comment->user_id )
       
   149 		$id = $comment->user_id;
       
   150 
       
   151 	if ( $id )
       
   152 		$avatar = get_avatar( $id, 32 );
       
   153 
       
   154 	return "$avatar $name";
   152 	return "$avatar $name";
   155 }
   153 }
   156 
   154 
   157 function enqueue_comment_hotkeys_js() {
   155 function enqueue_comment_hotkeys_js() {
   158 	if ( 'true' == get_user_option( 'comment_shortcuts' ) )
   156 	if ( 'true' == get_user_option( 'comment_shortcuts' ) )
   159 		wp_enqueue_script( 'jquery-table-hotkeys' );
   157 		wp_enqueue_script( 'jquery-table-hotkeys' );
   160 }
   158 }
   161 
       
   162 if ( is_admin() && isset($pagenow) && ('edit-comments.php' == $pagenow || 'edit.php' == $pagenow) ) {
       
   163 	if ( get_option('show_avatars') )
       
   164 		add_filter( 'comment_author', 'floated_admin_avatar' );
       
   165 }
       
   166 
       
   167 ?>