/wp-admin/includes/comment.php

https://bitbucket.org/lordmuffin/origin · PHP · 158 lines · 86 code · 24 blank · 48 comment · 13 complexity · e4afb576d2748a2ffc7fa616aff8c8a6 MD5 · raw file

  1. <?php
  2. /**
  3. * WordPress Comment Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Determine if a comment exists based on author and date.
  10. *
  11. * @since 2.0.0
  12. * @uses $wpdb
  13. *
  14. * @param string $comment_author Author of the comment
  15. * @param string $comment_date Date of the comment
  16. * @return mixed Comment post ID on success.
  17. */
  18. function comment_exists($comment_author, $comment_date) {
  19. global $wpdb;
  20. $comment_author = stripslashes($comment_author);
  21. $comment_date = stripslashes($comment_date);
  22. return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
  23. WHERE comment_author = %s AND comment_date = %s", $comment_author, $comment_date) );
  24. }
  25. /**
  26. * Update a comment with values provided in $_POST.
  27. *
  28. * @since 2.0.0
  29. */
  30. function edit_comment() {
  31. if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) )
  32. wp_die ( __( 'You are not allowed to edit comments on this post.' ) );
  33. $_POST['comment_author'] = $_POST['newcomment_author'];
  34. $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
  35. $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
  36. $_POST['comment_approved'] = $_POST['comment_status'];
  37. $_POST['comment_content'] = $_POST['content'];
  38. $_POST['comment_ID'] = (int) $_POST['comment_ID'];
  39. foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
  40. if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
  41. $_POST['edit_date'] = '1';
  42. break;
  43. }
  44. }
  45. if ( !empty ( $_POST['edit_date'] ) ) {
  46. $aa = $_POST['aa'];
  47. $mm = $_POST['mm'];
  48. $jj = $_POST['jj'];
  49. $hh = $_POST['hh'];
  50. $mn = $_POST['mn'];
  51. $ss = $_POST['ss'];
  52. $jj = ($jj > 31 ) ? 31 : $jj;
  53. $hh = ($hh > 23 ) ? $hh -24 : $hh;
  54. $mn = ($mn > 59 ) ? $mn -60 : $mn;
  55. $ss = ($ss > 59 ) ? $ss -60 : $ss;
  56. $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
  57. }
  58. wp_update_comment( $_POST );
  59. }
  60. /**
  61. * Returns a comment object based on comment ID.
  62. *
  63. * @since 2.0.0
  64. *
  65. * @param int $id ID of comment to retrieve.
  66. * @return bool|object Comment if found. False on failure.
  67. */
  68. function get_comment_to_edit( $id ) {
  69. if ( !$comment = get_comment($id) )
  70. return false;
  71. $comment->comment_ID = (int) $comment->comment_ID;
  72. $comment->comment_post_ID = (int) $comment->comment_post_ID;
  73. $comment->comment_content = format_to_edit( $comment->comment_content );
  74. $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content);
  75. $comment->comment_author = format_to_edit( $comment->comment_author );
  76. $comment->comment_author_email = format_to_edit( $comment->comment_author_email );
  77. $comment->comment_author_url = format_to_edit( $comment->comment_author_url );
  78. $comment->comment_author_url = esc_url($comment->comment_author_url);
  79. return $comment;
  80. }
  81. /**
  82. * Get the number of pending comments on a post or posts
  83. *
  84. * @since 2.3.0
  85. * @uses $wpdb
  86. *
  87. * @param int|array $post_id Either a single Post ID or an array of Post IDs
  88. * @return int|array Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
  89. */
  90. function get_pending_comments_num( $post_id ) {
  91. global $wpdb;
  92. $single = false;
  93. if ( !is_array($post_id) ) {
  94. $post_id_array = (array) $post_id;
  95. $single = true;
  96. } else {
  97. $post_id_array = $post_id;
  98. }
  99. $post_id_array = array_map('intval', $post_id_array);
  100. $post_id_in = "'" . implode("', '", $post_id_array) . "'";
  101. $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 );
  102. if ( $single ) {
  103. if ( empty($pending) )
  104. return 0;
  105. else
  106. return absint($pending[0]['num_comments']);
  107. }
  108. $pending_keyed = array();
  109. // Default to zero pending for all posts in request
  110. foreach ( $post_id_array as $id )
  111. $pending_keyed[$id] = 0;
  112. if ( !empty($pending) )
  113. foreach ( $pending as $pend )
  114. $pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);
  115. return $pending_keyed;
  116. }
  117. /**
  118. * Add avatars to relevant places in admin, or try to.
  119. *
  120. * @since 2.5.0
  121. * @uses $comment
  122. *
  123. * @param string $name User name.
  124. * @return string Avatar with Admin name.
  125. */
  126. function floated_admin_avatar( $name ) {
  127. global $comment;
  128. $avatar = get_avatar( $comment, 32 );
  129. return "$avatar $name";
  130. }
  131. function enqueue_comment_hotkeys_js() {
  132. if ( 'true' == get_user_option( 'comment_shortcuts' ) )
  133. wp_enqueue_script( 'jquery-table-hotkeys' );
  134. }