comment.php 6.0 KB

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