You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

196 rivejä
5.6 KiB

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