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