comment.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * WordPress Comment Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 2.3.0
  8. */
  9. /**
  10. * Determine if a comment exists based on author and date.
  11. *
  12. * For best performance, use `$timezone = 'gmt'`, which queries a field that is properly indexed. The default value
  13. * for `$timezone` is 'blog' for legacy reasons.
  14. *
  15. * @since 2.0.0
  16. * @since 4.4.0 Added the `$timezone` parameter.
  17. *
  18. * @global wpdb $wpdb WordPress database abstraction object.
  19. *
  20. * @param string $comment_author Author of the comment.
  21. * @param string $comment_date Date of the comment.
  22. * @param string $timezone Timezone. Accepts 'blog' or 'gmt'. Default 'blog'.
  23. *
  24. * @return mixed Comment post ID on success.
  25. */
  26. function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) {
  27. global $wpdb;
  28. $date_field = 'comment_date';
  29. if ( 'gmt' === $timezone ) {
  30. $date_field = 'comment_date_gmt';
  31. }
  32. return $wpdb->get_var(
  33. $wpdb->prepare(
  34. "SELECT comment_post_ID FROM $wpdb->comments
  35. WHERE comment_author = %s AND $date_field = %s",
  36. stripslashes( $comment_author ),
  37. stripslashes( $comment_date )
  38. )
  39. );
  40. }
  41. /**
  42. * Update a comment with values provided in $_POST.
  43. *
  44. * @since 2.0.0
  45. */
  46. function edit_comment() {
  47. if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) ) {
  48. wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) );
  49. }
  50. if ( isset( $_POST['newcomment_author'] ) ) {
  51. $_POST['comment_author'] = $_POST['newcomment_author'];
  52. }
  53. if ( isset( $_POST['newcomment_author_email'] ) ) {
  54. $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
  55. }
  56. if ( isset( $_POST['newcomment_author_url'] ) ) {
  57. $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
  58. }
  59. if ( isset( $_POST['comment_status'] ) ) {
  60. $_POST['comment_approved'] = $_POST['comment_status'];
  61. }
  62. if ( isset( $_POST['content'] ) ) {
  63. $_POST['comment_content'] = $_POST['content'];
  64. }
  65. if ( isset( $_POST['comment_ID'] ) ) {
  66. $_POST['comment_ID'] = (int) $_POST['comment_ID'];
  67. }
  68. foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
  69. if ( ! empty( $_POST[ 'hidden_' . $timeunit ] ) && $_POST[ 'hidden_' . $timeunit ] != $_POST[ $timeunit ] ) {
  70. $_POST['edit_date'] = '1';
  71. break;
  72. }
  73. }
  74. if ( ! empty( $_POST['edit_date'] ) ) {
  75. $aa = $_POST['aa'];
  76. $mm = $_POST['mm'];
  77. $jj = $_POST['jj'];
  78. $hh = $_POST['hh'];
  79. $mn = $_POST['mn'];
  80. $ss = $_POST['ss'];
  81. $jj = ( $jj > 31 ) ? 31 : $jj;
  82. $hh = ( $hh > 23 ) ? $hh - 24 : $hh;
  83. $mn = ( $mn > 59 ) ? $mn - 60 : $mn;
  84. $ss = ( $ss > 59 ) ? $ss - 60 : $ss;
  85. $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
  86. }
  87. wp_update_comment( $_POST );
  88. }
  89. /**
  90. * Returns a WP_Comment object based on comment ID.
  91. *
  92. * @since 2.0.0
  93. *
  94. * @param int $id ID of comment to retrieve.
  95. * @return WP_Comment|false Comment if found. False on failure.
  96. */
  97. function get_comment_to_edit( $id ) {
  98. $comment = get_comment( $id );
  99. if ( ! $comment ) {
  100. return false;
  101. }
  102. $comment->comment_ID = (int) $comment->comment_ID;
  103. $comment->comment_post_ID = (int) $comment->comment_post_ID;
  104. $comment->comment_content = format_to_edit( $comment->comment_content );
  105. /**
  106. * Filters the comment content before editing.
  107. *
  108. * @since 2.0.0
  109. *
  110. * @param string $comment->comment_content Comment content.
  111. */
  112. $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
  113. $comment->comment_author = format_to_edit( $comment->comment_author );
  114. $comment->comment_author_email = format_to_edit( $comment->comment_author_email );
  115. $comment->comment_author_url = format_to_edit( $comment->comment_author_url );
  116. $comment->comment_author_url = esc_url( $comment->comment_author_url );
  117. return $comment;
  118. }
  119. /**
  120. * Get the number of pending comments on a post or posts
  121. *
  122. * @since 2.3.0
  123. *
  124. * @global wpdb $wpdb WordPress database abstraction object.
  125. *
  126. * @param int|array $post_id Either a single Post ID or an array of Post IDs
  127. * @return int|array Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
  128. */
  129. function get_pending_comments_num( $post_id ) {
  130. global $wpdb;
  131. $single = false;
  132. if ( ! is_array( $post_id ) ) {
  133. $post_id_array = (array) $post_id;
  134. $single = true;
  135. } else {
  136. $post_id_array = $post_id;
  137. }
  138. $post_id_array = array_map( 'intval', $post_id_array );
  139. $post_id_in = "'" . implode( "', '", $post_id_array ) . "'";
  140. $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 );
  141. if ( $single ) {
  142. if ( empty( $pending ) ) {
  143. return 0;
  144. } else {
  145. return absint( $pending[0]['num_comments'] );
  146. }
  147. }
  148. $pending_keyed = array();
  149. // Default to zero pending for all posts in request
  150. foreach ( $post_id_array as $id ) {
  151. $pending_keyed[ $id ] = 0;
  152. }
  153. if ( ! empty( $pending ) ) {
  154. foreach ( $pending as $pend ) {
  155. $pending_keyed[ $pend['comment_post_ID'] ] = absint( $pend['num_comments'] );
  156. }
  157. }
  158. return $pending_keyed;
  159. }
  160. /**
  161. * Add avatars to relevant places in admin, or try to.
  162. *
  163. * @since 2.5.0
  164. *
  165. * @param string $name User name.
  166. * @return string Avatar with Admin name.
  167. */
  168. function floated_admin_avatar( $name ) {
  169. $avatar = get_avatar( get_comment(), 32, 'mystery' );
  170. return "$avatar $name";
  171. }
  172. /**
  173. * @since 2.7.0
  174. */
  175. function enqueue_comment_hotkeys_js() {
  176. if ( 'true' == get_user_option( 'comment_shortcuts' ) ) {
  177. wp_enqueue_script( 'jquery-table-hotkeys' );
  178. }
  179. }
  180. /**
  181. * Display error message at bottom of comments.
  182. *
  183. * @param string $msg Error Message. Assumed to contain HTML and be sanitized.
  184. */
  185. function comment_footer_die( $msg ) {
  186. echo "<div class='wrap'><p>$msg</p></div>";
  187. include( ABSPATH . 'wp-admin/admin-footer.php' );
  188. die;
  189. }