WordPress.org

Make WordPress Core

Ticket #16129: 16129.diff

File 16129.diff, 4.6 KB (added by casben79, 9 years ago)

Changed

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