changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
20:7b1b88e27a20 | 21:48c4eec2b7e6 |
---|---|
13 * |
13 * |
14 * If the comment has an empty comment_author field, then 'Anonymous' person is |
14 * If the comment has an empty comment_author field, then 'Anonymous' person is |
15 * assumed. |
15 * assumed. |
16 * |
16 * |
17 * @since 1.5.0 |
17 * @since 1.5.0 |
18 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
18 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
19 * |
19 * |
20 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to retrieve the author. |
20 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to retrieve the author. |
21 * Default current comment. |
21 * Default current comment. |
22 * @return string The comment author |
22 * @return string The comment author |
23 */ |
23 */ |
24 function get_comment_author( $comment_ID = 0 ) { |
24 function get_comment_author( $comment_id = 0 ) { |
25 $comment = get_comment( $comment_ID ); |
25 $comment = get_comment( $comment_id ); |
26 $comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : $comment_ID; |
26 |
27 if ( ! empty( $comment->comment_ID ) ) { |
|
28 $comment_id = $comment->comment_ID; |
|
29 } elseif ( is_scalar( $comment_id ) ) { |
|
30 $comment_id = (string) $comment_id; |
|
31 } else { |
|
32 $comment_id = ''; |
|
33 } |
|
27 |
34 |
28 if ( empty( $comment->comment_author ) ) { |
35 if ( empty( $comment->comment_author ) ) { |
29 $user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false; |
36 $user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false; |
30 if ( $user ) { |
37 if ( $user ) { |
31 $author = $user->display_name; |
38 $comment_author = $user->display_name; |
32 } else { |
39 } else { |
33 $author = __( 'Anonymous' ); |
40 $comment_author = __( 'Anonymous' ); |
34 } |
41 } |
35 } else { |
42 } else { |
36 $author = $comment->comment_author; |
43 $comment_author = $comment->comment_author; |
37 } |
44 } |
38 |
45 |
39 /** |
46 /** |
40 * Filters the returned comment author name. |
47 * Filters the returned comment author name. |
41 * |
48 * |
42 * @since 1.5.0 |
49 * @since 1.5.0 |
43 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added. |
50 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. |
44 * |
51 * |
45 * @param string $author The comment author's username. |
52 * @param string $comment_author The comment author's username. |
46 * @param string $comment_ID The comment ID as a numeric string. |
53 * @param string $comment_id The comment ID as a numeric string. |
47 * @param WP_Comment $comment The comment object. |
54 * @param WP_Comment $comment The comment object. |
48 */ |
55 */ |
49 return apply_filters( 'get_comment_author', $author, $comment_ID, $comment ); |
56 return apply_filters( 'get_comment_author', $comment_author, $comment_id, $comment ); |
50 } |
57 } |
51 |
58 |
52 /** |
59 /** |
53 * Displays the author of the current comment. |
60 * Displays the author of the current comment. |
54 * |
61 * |
55 * @since 0.71 |
62 * @since 0.71 |
56 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
63 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
57 * |
64 * |
58 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author. |
65 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author. |
59 * Default current comment. |
66 * Default current comment. |
60 */ |
67 */ |
61 function comment_author( $comment_ID = 0 ) { |
68 function comment_author( $comment_id = 0 ) { |
62 $comment = get_comment( $comment_ID ); |
69 $comment = get_comment( $comment_id ); |
63 $author = get_comment_author( $comment ); |
70 |
71 $comment_author = get_comment_author( $comment ); |
|
64 |
72 |
65 /** |
73 /** |
66 * Filters the comment author's name for display. |
74 * Filters the comment author's name for display. |
67 * |
75 * |
68 * @since 1.2.0 |
76 * @since 1.2.0 |
69 * @since 4.1.0 The `$comment_ID` parameter was added. |
77 * @since 4.1.0 The `$comment_id` parameter was added. |
70 * |
78 * |
71 * @param string $author The comment author's username. |
79 * @param string $comment_author The comment author's username. |
72 * @param string $comment_ID The comment ID as a numeric string. |
80 * @param string $comment_id The comment ID as a numeric string. |
73 */ |
81 */ |
74 echo apply_filters( 'comment_author', $author, $comment->comment_ID ); |
82 echo apply_filters( 'comment_author', $comment_author, $comment->comment_ID ); |
75 } |
83 } |
76 |
84 |
77 /** |
85 /** |
78 * Retrieves the email of the author of the current comment. |
86 * Retrieves the email of the author of the current comment. |
79 * |
87 * |
80 * @since 1.5.0 |
88 * @since 1.5.0 |
81 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
89 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
82 * |
90 * |
83 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's email. |
91 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's email. |
84 * Default current comment. |
92 * Default current comment. |
85 * @return string The current comment author's email |
93 * @return string The current comment author's email |
86 */ |
94 */ |
87 function get_comment_author_email( $comment_ID = 0 ) { |
95 function get_comment_author_email( $comment_id = 0 ) { |
88 $comment = get_comment( $comment_ID ); |
96 $comment = get_comment( $comment_id ); |
89 |
97 |
90 /** |
98 /** |
91 * Filters the comment author's returned email address. |
99 * Filters the comment author's returned email address. |
92 * |
100 * |
93 * @since 1.5.0 |
101 * @since 1.5.0 |
94 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added. |
102 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. |
95 * |
103 * |
96 * @param string $comment_author_email The comment author's email address. |
104 * @param string $comment_author_email The comment author's email address. |
97 * @param string $comment_ID The comment ID as a numeric string. |
105 * @param string $comment_id The comment ID as a numeric string. |
98 * @param WP_Comment $comment The comment object. |
106 * @param WP_Comment $comment The comment object. |
99 */ |
107 */ |
100 return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment ); |
108 return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment ); |
101 } |
109 } |
102 |
110 |
108 * their email address will not appear in raw form on the site. Doing so will |
116 * their email address will not appear in raw form on the site. Doing so will |
109 * enable anyone, including those that people don't want to get the email |
117 * enable anyone, including those that people don't want to get the email |
110 * address and use it for their own means good and bad. |
118 * address and use it for their own means good and bad. |
111 * |
119 * |
112 * @since 0.71 |
120 * @since 0.71 |
113 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
121 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
114 * |
122 * |
115 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's email. |
123 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's email. |
116 * Default current comment. |
124 * Default current comment. |
117 */ |
125 */ |
118 function comment_author_email( $comment_ID = 0 ) { |
126 function comment_author_email( $comment_id = 0 ) { |
119 $comment = get_comment( $comment_ID ); |
127 $comment = get_comment( $comment_id ); |
120 $author_email = get_comment_author_email( $comment ); |
128 |
129 $comment_author_email = get_comment_author_email( $comment ); |
|
121 |
130 |
122 /** |
131 /** |
123 * Filters the comment author's email for display. |
132 * Filters the comment author's email for display. |
124 * |
133 * |
125 * @since 1.2.0 |
134 * @since 1.2.0 |
126 * @since 4.1.0 The `$comment_ID` parameter was added. |
135 * @since 4.1.0 The `$comment_id` parameter was added. |
127 * |
136 * |
128 * @param string $author_email The comment author's email address. |
137 * @param string $comment_author_email The comment author's email address. |
129 * @param string $comment_ID The comment ID as a numeric string. |
138 * @param string $comment_id The comment ID as a numeric string. |
130 */ |
139 */ |
131 echo apply_filters( 'author_email', $author_email, $comment->comment_ID ); |
140 echo apply_filters( 'author_email', $comment_author_email, $comment->comment_ID ); |
132 } |
141 } |
133 |
142 |
134 /** |
143 /** |
135 * Displays the HTML email link to the author of the current comment. |
144 * Displays the HTML email link to the author of the current comment. |
136 * |
145 * |
141 * address and use it for their own means good and bad. |
150 * address and use it for their own means good and bad. |
142 * |
151 * |
143 * @since 0.71 |
152 * @since 0.71 |
144 * @since 4.6.0 Added the `$comment` parameter. |
153 * @since 4.6.0 Added the `$comment` parameter. |
145 * |
154 * |
146 * @param string $linktext Optional. Text to display instead of the comment author's email address. |
155 * @param string $link_text Optional. Text to display instead of the comment author's email address. |
147 * Default empty. |
156 * Default empty. |
148 * @param string $before Optional. Text or HTML to display before the email link. Default empty. |
157 * @param string $before Optional. Text or HTML to display before the email link. Default empty. |
149 * @param string $after Optional. Text or HTML to display after the email link. Default empty. |
158 * @param string $after Optional. Text or HTML to display after the email link. Default empty. |
150 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment. |
159 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment. |
151 */ |
160 */ |
152 function comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) { |
161 function comment_author_email_link( $link_text = '', $before = '', $after = '', $comment = null ) { |
153 $link = get_comment_author_email_link( $linktext, $before, $after, $comment ); |
162 $link = get_comment_author_email_link( $link_text, $before, $after, $comment ); |
154 if ( $link ) { |
163 if ( $link ) { |
155 echo $link; |
164 echo $link; |
156 } |
165 } |
157 } |
166 } |
158 |
167 |
166 * address and use it for their own means good and bad. |
175 * address and use it for their own means good and bad. |
167 * |
176 * |
168 * @since 2.7.0 |
177 * @since 2.7.0 |
169 * @since 4.6.0 Added the `$comment` parameter. |
178 * @since 4.6.0 Added the `$comment` parameter. |
170 * |
179 * |
171 * @param string $linktext Optional. Text to display instead of the comment author's email address. |
180 * @param string $link_text Optional. Text to display instead of the comment author's email address. |
172 * Default empty. |
181 * Default empty. |
173 * @param string $before Optional. Text or HTML to display before the email link. Default empty. |
182 * @param string $before Optional. Text or HTML to display before the email link. Default empty. |
174 * @param string $after Optional. Text or HTML to display after the email link. Default empty. |
183 * @param string $after Optional. Text or HTML to display after the email link. Default empty. |
175 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment. |
184 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment. |
176 * @return string HTML markup for the comment author email link. By default, the email address is obfuscated |
185 * @return string HTML markup for the comment author email link. By default, the email address is obfuscated |
177 * via the {@see 'comment_email'} filter with antispambot(). |
186 * via the {@see 'comment_email'} filter with antispambot(). |
178 */ |
187 */ |
179 function get_comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) { |
188 function get_comment_author_email_link( $link_text = '', $before = '', $after = '', $comment = null ) { |
180 $comment = get_comment( $comment ); |
189 $comment = get_comment( $comment ); |
181 |
190 |
182 /** |
191 /** |
183 * Filters the comment author's email for display. |
192 * Filters the comment author's email for display. |
184 * |
193 * |
189 * @since 4.1.0 The `$comment` parameter was added. |
198 * @since 4.1.0 The `$comment` parameter was added. |
190 * |
199 * |
191 * @param string $comment_author_email The comment author's email address. |
200 * @param string $comment_author_email The comment author's email address. |
192 * @param WP_Comment $comment The comment object. |
201 * @param WP_Comment $comment The comment object. |
193 */ |
202 */ |
194 $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment ); |
203 $comment_author_email = apply_filters( 'comment_email', $comment->comment_author_email, $comment ); |
195 |
204 |
196 if ( ( ! empty( $email ) ) && ( '@' !== $email ) ) { |
205 if ( ( ! empty( $comment_author_email ) ) && ( '@' !== $comment_author_email ) ) { |
197 $display = ( '' !== $linktext ) ? $linktext : $email; |
206 $display = ( '' !== $link_text ) ? $link_text : $comment_author_email; |
198 $return = $before; |
207 |
199 $return .= sprintf( '<a href="%1$s">%2$s</a>', esc_url( 'mailto:' . $email ), esc_html( $display ) ); |
208 $comment_author_email_link = $before . sprintf( |
200 $return .= $after; |
209 '<a href="%1$s">%2$s</a>', |
201 return $return; |
210 esc_url( 'mailto:' . $comment_author_email ), |
211 esc_html( $display ) |
|
212 ) . $after; |
|
213 |
|
214 return $comment_author_email_link; |
|
202 } else { |
215 } else { |
203 return ''; |
216 return ''; |
204 } |
217 } |
205 } |
218 } |
206 |
219 |
207 /** |
220 /** |
208 * Retrieves the HTML link to the URL of the author of the current comment. |
221 * Retrieves the HTML link to the URL of the author of the current comment. |
209 * |
222 * |
210 * Both get_comment_author_url() and get_comment_author() rely on get_comment(), |
223 * Both get_comment_author_url() and get_comment_author() rely on get_comment(), |
211 * which falls back to the global comment variable if the $comment_ID argument is empty. |
224 * which falls back to the global comment variable if the $comment_id argument is empty. |
212 * |
225 * |
213 * @since 1.5.0 |
226 * @since 1.5.0 |
214 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
227 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
215 * |
228 * |
216 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's link. |
229 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's link. |
217 * Default current comment. |
230 * Default current comment. |
218 * @return string The comment author name or HTML link for author's URL. |
231 * @return string The comment author name or HTML link for author's URL. |
219 */ |
232 */ |
220 function get_comment_author_link( $comment_ID = 0 ) { |
233 function get_comment_author_link( $comment_id = 0 ) { |
221 $comment = get_comment( $comment_ID ); |
234 $comment = get_comment( $comment_id ); |
222 $url = get_comment_author_url( $comment ); |
235 |
223 $author = get_comment_author( $comment ); |
236 $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_id; |
224 |
237 |
225 if ( empty( $url ) || 'http://' === $url ) { |
238 $comment_author_url = get_comment_author_url( $comment ); |
226 $return = $author; |
239 $comment_author = get_comment_author( $comment ); |
240 |
|
241 if ( empty( $comment_author_url ) || 'http://' === $comment_author_url ) { |
|
242 $comment_author_link = $comment_author; |
|
227 } else { |
243 } else { |
228 $return = "<a href='$url' rel='external nofollow ugc' class='url'>$author</a>"; |
244 $rel_parts = array( 'ugc' ); |
245 if ( ! wp_is_internal_link( $comment_author_url ) ) { |
|
246 $rel_parts = array_merge( |
|
247 $rel_parts, |
|
248 array( 'external', 'nofollow' ) |
|
249 ); |
|
250 } |
|
251 |
|
252 /** |
|
253 * Filters the rel attributes of the comment author's link. |
|
254 * |
|
255 * @since 6.2.0 |
|
256 * |
|
257 * @param string[] $rel_parts An array of strings representing the rel tags |
|
258 * which will be joined into the anchor's rel attribute. |
|
259 * @param WP_Comment $comment The comment object. |
|
260 */ |
|
261 $rel_parts = apply_filters( 'comment_author_link_rel', $rel_parts, $comment ); |
|
262 |
|
263 $rel = implode( ' ', $rel_parts ); |
|
264 $rel = esc_attr( $rel ); |
|
265 // Empty space before 'rel' is necessary for later sprintf(). |
|
266 $rel = ! empty( $rel ) ? sprintf( ' rel="%s"', $rel ) : ''; |
|
267 |
|
268 $comment_author_link = sprintf( |
|
269 '<a href="%1$s" class="url"%2$s>%3$s</a>', |
|
270 $comment_author_url, |
|
271 $rel, |
|
272 $comment_author |
|
273 ); |
|
229 } |
274 } |
230 |
275 |
231 /** |
276 /** |
232 * Filters the comment author's link for display. |
277 * Filters the comment author's link for display. |
233 * |
278 * |
234 * @since 1.5.0 |
279 * @since 1.5.0 |
235 * @since 4.1.0 The `$author` and `$comment_ID` parameters were added. |
280 * @since 4.1.0 The `$comment_author` and `$comment_id` parameters were added. |
236 * |
281 * |
237 * @param string $return The HTML-formatted comment author link. |
282 * @param string $comment_author_link The HTML-formatted comment author link. |
238 * Empty for an invalid URL. |
283 * Empty for an invalid URL. |
239 * @param string $author The comment author's username. |
284 * @param string $comment_author The comment author's username. |
240 * @param string $comment_ID The comment ID as a numeric string. |
285 * @param string $comment_id The comment ID as a numeric string. |
241 */ |
286 */ |
242 return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID ); |
287 return apply_filters( 'get_comment_author_link', $comment_author_link, $comment_author, $comment_id ); |
243 } |
288 } |
244 |
289 |
245 /** |
290 /** |
246 * Displays the HTML link to the URL of the author of the current comment. |
291 * Displays the HTML link to the URL of the author of the current comment. |
247 * |
292 * |
248 * @since 0.71 |
293 * @since 0.71 |
249 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
294 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
250 * |
295 * |
251 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's link. |
296 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's link. |
252 * Default current comment. |
297 * Default current comment. |
253 */ |
298 */ |
254 function comment_author_link( $comment_ID = 0 ) { |
299 function comment_author_link( $comment_id = 0 ) { |
255 echo get_comment_author_link( $comment_ID ); |
300 echo get_comment_author_link( $comment_id ); |
256 } |
301 } |
257 |
302 |
258 /** |
303 /** |
259 * Retrieves the IP address of the author of the current comment. |
304 * Retrieves the IP address of the author of the current comment. |
260 * |
305 * |
261 * @since 1.5.0 |
306 * @since 1.5.0 |
262 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
307 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
263 * |
308 * |
264 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's IP address. |
309 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's IP address. |
265 * Default current comment. |
310 * Default current comment. |
266 * @return string Comment author's IP address, or an empty string if it's not available. |
311 * @return string Comment author's IP address, or an empty string if it's not available. |
267 */ |
312 */ |
268 function get_comment_author_IP( $comment_ID = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
313 function get_comment_author_IP( $comment_id = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
269 $comment = get_comment( $comment_ID ); |
314 $comment = get_comment( $comment_id ); |
270 |
315 |
271 /** |
316 /** |
272 * Filters the comment author's returned IP address. |
317 * Filters the comment author's returned IP address. |
273 * |
318 * |
274 * @since 1.5.0 |
319 * @since 1.5.0 |
275 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added. |
320 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. |
276 * |
321 * |
277 * @param string $comment_author_IP The comment author's IP address, or an empty string if it's not available. |
322 * @param string $comment_author_ip The comment author's IP address, or an empty string if it's not available. |
278 * @param string $comment_ID The comment ID as a numeric string. |
323 * @param string $comment_id The comment ID as a numeric string. |
279 * @param WP_Comment $comment The comment object. |
324 * @param WP_Comment $comment The comment object. |
280 */ |
325 */ |
281 return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase |
326 return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase |
282 } |
327 } |
283 |
328 |
284 /** |
329 /** |
285 * Displays the IP address of the author of the current comment. |
330 * Displays the IP address of the author of the current comment. |
286 * |
331 * |
287 * @since 0.71 |
332 * @since 0.71 |
288 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
333 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
289 * |
334 * |
290 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's IP address. |
335 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's IP address. |
291 * Default current comment. |
336 * Default current comment. |
292 */ |
337 */ |
293 function comment_author_IP( $comment_ID = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
338 function comment_author_IP( $comment_id = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
294 echo esc_html( get_comment_author_IP( $comment_ID ) ); |
339 echo esc_html( get_comment_author_IP( $comment_id ) ); |
295 } |
340 } |
296 |
341 |
297 /** |
342 /** |
298 * Retrieves the URL of the author of the current comment, not linked. |
343 * Retrieves the URL of the author of the current comment, not linked. |
299 * |
344 * |
300 * @since 1.5.0 |
345 * @since 1.5.0 |
301 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
346 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
302 * |
347 * |
303 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's URL. |
348 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's URL. |
304 * Default current comment. |
349 * Default current comment. |
305 * @return string Comment author URL, if provided, an empty string otherwise. |
350 * @return string Comment author URL, if provided, an empty string otherwise. |
306 */ |
351 */ |
307 function get_comment_author_url( $comment_ID = 0 ) { |
352 function get_comment_author_url( $comment_id = 0 ) { |
308 $comment = get_comment( $comment_ID ); |
353 $comment = get_comment( $comment_id ); |
309 $url = ''; |
354 |
310 $id = 0; |
355 $comment_author_url = ''; |
356 $comment_id = 0; |
|
311 |
357 |
312 if ( ! empty( $comment ) ) { |
358 if ( ! empty( $comment ) ) { |
313 $author_url = ( 'http://' === $comment->comment_author_url ) ? '' : $comment->comment_author_url; |
359 $comment_author_url = ( 'http://' === $comment->comment_author_url ) ? '' : $comment->comment_author_url; |
314 $url = esc_url( $author_url, array( 'http', 'https' ) ); |
360 $comment_author_url = esc_url( $comment_author_url, array( 'http', 'https' ) ); |
315 $id = $comment->comment_ID; |
361 |
362 $comment_id = $comment->comment_ID; |
|
316 } |
363 } |
317 |
364 |
318 /** |
365 /** |
319 * Filters the comment author's URL. |
366 * Filters the comment author's URL. |
320 * |
367 * |
321 * @since 1.5.0 |
368 * @since 1.5.0 |
322 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added. |
369 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. |
323 * |
370 * |
324 * @param string $url The comment author's URL, or an empty string. |
371 * @param string $comment_author_url The comment author's URL, or an empty string. |
325 * @param string|int $comment_ID The comment ID as a numeric string, or 0 if not found. |
372 * @param string|int $comment_id The comment ID as a numeric string, or 0 if not found. |
326 * @param WP_Comment|null $comment The comment object, or null if not found. |
373 * @param WP_Comment|null $comment The comment object, or null if not found. |
327 */ |
374 */ |
328 return apply_filters( 'get_comment_author_url', $url, $id, $comment ); |
375 return apply_filters( 'get_comment_author_url', $comment_author_url, $comment_id, $comment ); |
329 } |
376 } |
330 |
377 |
331 /** |
378 /** |
332 * Displays the URL of the author of the current comment, not linked. |
379 * Displays the URL of the author of the current comment, not linked. |
333 * |
380 * |
334 * @since 0.71 |
381 * @since 0.71 |
335 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
382 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
336 * |
383 * |
337 * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's URL. |
384 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's URL. |
338 * Default current comment. |
385 * Default current comment. |
339 */ |
386 */ |
340 function comment_author_url( $comment_ID = 0 ) { |
387 function comment_author_url( $comment_id = 0 ) { |
341 $comment = get_comment( $comment_ID ); |
388 $comment = get_comment( $comment_id ); |
342 $author_url = get_comment_author_url( $comment ); |
389 |
390 $comment_author_url = get_comment_author_url( $comment ); |
|
343 |
391 |
344 /** |
392 /** |
345 * Filters the comment author's URL for display. |
393 * Filters the comment author's URL for display. |
346 * |
394 * |
347 * @since 1.2.0 |
395 * @since 1.2.0 |
348 * @since 4.1.0 The `$comment_ID` parameter was added. |
396 * @since 4.1.0 The `$comment_id` parameter was added. |
349 * |
397 * |
350 * @param string $author_url The comment author's URL. |
398 * @param string $comment_author_url The comment author's URL. |
351 * @param string $comment_ID The comment ID as a numeric string. |
399 * @param string $comment_id The comment ID as a numeric string. |
352 */ |
400 */ |
353 echo apply_filters( 'comment_url', $author_url, $comment->comment_ID ); |
401 echo apply_filters( 'comment_url', $comment_author_url, $comment->comment_ID ); |
354 } |
402 } |
355 |
403 |
356 /** |
404 /** |
357 * Retrieves the HTML link of the URL of the author of the current comment. |
405 * Retrieves the HTML link of the URL of the author of the current comment. |
358 * |
406 * |
359 * $linktext parameter is only used if the URL does not exist for the comment |
407 * $link_text parameter is only used if the URL does not exist for the comment |
360 * author. If the URL does exist then the URL will be used and the $linktext |
408 * author. If the URL does exist then the URL will be used and the $link_text |
361 * will be ignored. |
409 * will be ignored. |
362 * |
410 * |
363 * Encapsulate the HTML link between the $before and $after. So it will appear |
411 * Encapsulate the HTML link between the $before and $after. So it will appear |
364 * in the order of $before, link, and finally $after. |
412 * in the order of $before, link, and finally $after. |
365 * |
413 * |
366 * @since 1.5.0 |
414 * @since 1.5.0 |
367 * @since 4.6.0 Added the `$comment` parameter. |
415 * @since 4.6.0 Added the `$comment` parameter. |
368 * |
416 * |
369 * @param string $linktext Optional. The text to display instead of the comment |
417 * @param string $link_text Optional. The text to display instead of the comment |
370 * author's email address. Default empty. |
418 * author's email address. Default empty. |
371 * @param string $before Optional. The text or HTML to display before the email link. |
419 * @param string $before Optional. The text or HTML to display before the email link. |
372 * Default empty. |
420 * Default empty. |
373 * @param string $after Optional. The text or HTML to display after the email link. |
421 * @param string $after Optional. The text or HTML to display after the email link. |
374 * Default empty. |
422 * Default empty. |
375 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. |
423 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. |
376 * Default is the current comment. |
424 * Default is the current comment. |
377 * @return string The HTML link between the $before and $after parameters. |
425 * @return string The HTML link between the $before and $after parameters. |
378 */ |
426 */ |
379 function get_comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) { |
427 function get_comment_author_url_link( $link_text = '', $before = '', $after = '', $comment = 0 ) { |
380 $url = get_comment_author_url( $comment ); |
428 $comment_author_url = get_comment_author_url( $comment ); |
381 $display = ( '' !== $linktext ) ? $linktext : $url; |
429 |
430 $display = ( '' !== $link_text ) ? $link_text : $comment_author_url; |
|
382 $display = str_replace( 'http://www.', '', $display ); |
431 $display = str_replace( 'http://www.', '', $display ); |
383 $display = str_replace( 'http://', '', $display ); |
432 $display = str_replace( 'http://', '', $display ); |
384 |
433 |
385 if ( '/' === substr( $display, -1 ) ) { |
434 if ( str_ends_with( $display, '/' ) ) { |
386 $display = substr( $display, 0, -1 ); |
435 $display = substr( $display, 0, -1 ); |
387 } |
436 } |
388 |
437 |
389 $return = "$before<a href='$url' rel='external'>$display</a>$after"; |
438 $comment_author_url_link = $before . sprintf( |
439 '<a href="%1$s" rel="external">%2$s</a>', |
|
440 $comment_author_url, |
|
441 $display |
|
442 ) . $after; |
|
390 |
443 |
391 /** |
444 /** |
392 * Filters the comment author's returned URL link. |
445 * Filters the comment author's returned URL link. |
393 * |
446 * |
394 * @since 1.5.0 |
447 * @since 1.5.0 |
395 * |
448 * |
396 * @param string $return The HTML-formatted comment author URL link. |
449 * @param string $comment_author_url_link The HTML-formatted comment author URL link. |
397 */ |
450 */ |
398 return apply_filters( 'get_comment_author_url_link', $return ); |
451 return apply_filters( 'get_comment_author_url_link', $comment_author_url_link ); |
399 } |
452 } |
400 |
453 |
401 /** |
454 /** |
402 * Displays the HTML link of the URL of the author of the current comment. |
455 * Displays the HTML link of the URL of the author of the current comment. |
403 * |
456 * |
404 * @since 0.71 |
457 * @since 0.71 |
405 * @since 4.6.0 Added the `$comment` parameter. |
458 * @since 4.6.0 Added the `$comment` parameter. |
406 * |
459 * |
407 * @param string $linktext Optional. Text to display instead of the comment author's |
460 * @param string $link_text Optional. Text to display instead of the comment author's |
408 * email address. Default empty. |
461 * email address. Default empty. |
409 * @param string $before Optional. Text or HTML to display before the email link. |
462 * @param string $before Optional. Text or HTML to display before the email link. |
410 * Default empty. |
463 * Default empty. |
411 * @param string $after Optional. Text or HTML to display after the email link. |
464 * @param string $after Optional. Text or HTML to display after the email link. |
412 * Default empty. |
465 * Default empty. |
413 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. |
466 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. |
414 * Default is the current comment. |
467 * Default is the current comment. |
415 */ |
468 */ |
416 function comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) { |
469 function comment_author_url_link( $link_text = '', $before = '', $after = '', $comment = 0 ) { |
417 echo get_comment_author_url_link( $linktext, $before, $after, $comment ); |
470 echo get_comment_author_url_link( $link_text, $before, $after, $comment ); |
418 } |
471 } |
419 |
472 |
420 /** |
473 /** |
421 * Generates semantic classes for each comment element. |
474 * Generates semantic classes for each comment element. |
422 * |
475 * |
423 * @since 2.7.0 |
476 * @since 2.7.0 |
424 * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. |
477 * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. |
425 * |
478 * |
426 * @param string|string[] $css_class Optional. One or more classes to add to the class list. |
479 * @param string|string[] $css_class Optional. One or more classes to add to the class list. |
427 * Default empty. |
480 * Default empty. |
428 * @param int|WP_Comment $comment Comment ID or WP_Comment object. Default current comment. |
481 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default current comment. |
429 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post. |
482 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. |
430 * @param bool $display Optional. Whether to print or return the output. |
483 * @param bool $display Optional. Whether to print or return the output. |
431 * Default true. |
484 * Default true. |
432 * @return void|string Void if `$display` argument is true, comment classes if `$display` is false. |
485 * @return void|string Void if `$display` argument is true, comment classes if `$display` is false. |
433 */ |
486 */ |
434 function comment_class( $css_class = '', $comment = null, $post_id = null, $display = true ) { |
487 function comment_class( $css_class = '', $comment = null, $post = null, $display = true ) { |
435 // Separates classes with a single space, collates classes for comment DIV. |
488 // Separates classes with a single space, collates classes for comment DIV. |
436 $css_class = 'class="' . implode( ' ', get_comment_class( $css_class, $comment, $post_id ) ) . '"'; |
489 $css_class = 'class="' . implode( ' ', get_comment_class( $css_class, $comment, $post ) ) . '"'; |
437 |
490 |
438 if ( $display ) { |
491 if ( $display ) { |
439 echo $css_class; |
492 echo $css_class; |
440 } else { |
493 } else { |
441 return $css_class; |
494 return $css_class; |
450 * |
503 * |
451 * @global int $comment_alt |
504 * @global int $comment_alt |
452 * @global int $comment_depth |
505 * @global int $comment_depth |
453 * @global int $comment_thread_alt |
506 * @global int $comment_thread_alt |
454 * |
507 * |
455 * @param string|string[] $css_class Optional. One or more classes to add to the class list. Default empty. |
508 * @param string|string[] $css_class Optional. One or more classes to add to the class list. |
456 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. Default current comment. |
509 * Default empty. |
457 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post. |
510 * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object. Default current comment. |
511 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. |
|
458 * @return string[] An array of classes. |
512 * @return string[] An array of classes. |
459 */ |
513 */ |
460 function get_comment_class( $css_class = '', $comment_id = null, $post_id = null ) { |
514 function get_comment_class( $css_class = '', $comment_id = null, $post = null ) { |
461 global $comment_alt, $comment_depth, $comment_thread_alt; |
515 global $comment_alt, $comment_depth, $comment_thread_alt; |
462 |
516 |
463 $classes = array(); |
517 $classes = array(); |
464 |
518 |
465 $comment = get_comment( $comment_id ); |
519 $comment = get_comment( $comment_id ); |
474 $user = $comment->user_id ? get_userdata( $comment->user_id ) : false; |
528 $user = $comment->user_id ? get_userdata( $comment->user_id ) : false; |
475 if ( $user ) { |
529 if ( $user ) { |
476 $classes[] = 'byuser'; |
530 $classes[] = 'byuser'; |
477 $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id ); |
531 $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id ); |
478 // For comment authors who are the author of the post. |
532 // For comment authors who are the author of the post. |
479 $post = get_post( $post_id ); |
533 $_post = get_post( $post ); |
480 if ( $post ) { |
534 if ( $_post ) { |
481 if ( $comment->user_id === $post->post_author ) { |
535 if ( $comment->user_id === $_post->post_author ) { |
482 $classes[] = 'bypostauthor'; |
536 $classes[] = 'bypostauthor'; |
483 } |
537 } |
484 } |
538 } |
485 } |
539 } |
486 |
540 |
499 $classes[] = 'alt'; |
553 $classes[] = 'alt'; |
500 } else { |
554 } else { |
501 $classes[] = 'even'; |
555 $classes[] = 'even'; |
502 } |
556 } |
503 |
557 |
504 $comment_alt++; |
558 ++$comment_alt; |
505 |
559 |
506 // Alt for top-level comments. |
560 // Alt for top-level comments. |
507 if ( 1 == $comment_depth ) { |
561 if ( 1 == $comment_depth ) { |
508 if ( $comment_thread_alt % 2 ) { |
562 if ( $comment_thread_alt % 2 ) { |
509 $classes[] = 'thread-odd'; |
563 $classes[] = 'thread-odd'; |
510 $classes[] = 'thread-alt'; |
564 $classes[] = 'thread-alt'; |
511 } else { |
565 } else { |
512 $classes[] = 'thread-even'; |
566 $classes[] = 'thread-even'; |
513 } |
567 } |
514 $comment_thread_alt++; |
568 ++$comment_thread_alt; |
515 } |
569 } |
516 |
570 |
517 $classes[] = "depth-$comment_depth"; |
571 $classes[] = "depth-$comment_depth"; |
518 |
572 |
519 if ( ! empty( $css_class ) ) { |
573 if ( ! empty( $css_class ) ) { |
532 * |
586 * |
533 * @param string[] $classes An array of comment classes. |
587 * @param string[] $classes An array of comment classes. |
534 * @param string[] $css_class An array of additional classes added to the list. |
588 * @param string[] $css_class An array of additional classes added to the list. |
535 * @param string $comment_id The comment ID as a numeric string. |
589 * @param string $comment_id The comment ID as a numeric string. |
536 * @param WP_Comment $comment The comment object. |
590 * @param WP_Comment $comment The comment object. |
537 * @param int|WP_Post $post_id The post ID or WP_Post object. |
591 * @param int|WP_Post $post The post ID or WP_Post object. |
538 */ |
592 */ |
539 return apply_filters( 'comment_class', $classes, $css_class, $comment->comment_ID, $comment, $post_id ); |
593 return apply_filters( 'comment_class', $classes, $css_class, $comment->comment_ID, $comment, $post ); |
540 } |
594 } |
541 |
595 |
542 /** |
596 /** |
543 * Retrieves the comment date of the current comment. |
597 * Retrieves the comment date of the current comment. |
544 * |
598 * |
545 * @since 1.5.0 |
599 * @since 1.5.0 |
546 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
600 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
547 * |
601 * |
548 * @param string $format Optional. PHP date format. Defaults to the 'date_format' option. |
602 * @param string $format Optional. PHP date format. Defaults to the 'date_format' option. |
549 * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the date. |
603 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the date. |
550 * Default current comment. |
604 * Default current comment. |
551 * @return string The comment's date. |
605 * @return string The comment's date. |
552 */ |
606 */ |
553 function get_comment_date( $format = '', $comment_ID = 0 ) { |
607 function get_comment_date( $format = '', $comment_id = 0 ) { |
554 $comment = get_comment( $comment_ID ); |
608 $comment = get_comment( $comment_id ); |
555 |
609 |
556 $_format = ! empty( $format ) ? $format : get_option( 'date_format' ); |
610 $_format = ! empty( $format ) ? $format : get_option( 'date_format' ); |
557 |
611 |
558 $date = mysql2date( $_format, $comment->comment_date ); |
612 $comment_date = mysql2date( $_format, $comment->comment_date ); |
559 |
613 |
560 /** |
614 /** |
561 * Filters the returned comment date. |
615 * Filters the returned comment date. |
562 * |
616 * |
563 * @since 1.5.0 |
617 * @since 1.5.0 |
564 * |
618 * |
565 * @param string|int $date Formatted date string or Unix timestamp. |
619 * @param string|int $comment_date Formatted date string or Unix timestamp. |
566 * @param string $format PHP date format. |
620 * @param string $format PHP date format. |
567 * @param WP_Comment $comment The comment object. |
621 * @param WP_Comment $comment The comment object. |
568 */ |
622 */ |
569 return apply_filters( 'get_comment_date', $date, $format, $comment ); |
623 return apply_filters( 'get_comment_date', $comment_date, $format, $comment ); |
570 } |
624 } |
571 |
625 |
572 /** |
626 /** |
573 * Displays the comment date of the current comment. |
627 * Displays the comment date of the current comment. |
574 * |
628 * |
575 * @since 0.71 |
629 * @since 0.71 |
576 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
630 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
577 * |
631 * |
578 * @param string $format Optional. PHP date format. Defaults to the 'date_format' option. |
632 * @param string $format Optional. PHP date format. Defaults to the 'date_format' option. |
579 * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the date. |
633 * @param int|WP_Comment $comment_id WP_Comment or ID of the comment for which to print the date. |
580 * Default current comment. |
634 * Default current comment. |
581 */ |
635 */ |
582 function comment_date( $format = '', $comment_ID = 0 ) { |
636 function comment_date( $format = '', $comment_id = 0 ) { |
583 echo get_comment_date( $format, $comment_ID ); |
637 echo get_comment_date( $format, $comment_id ); |
584 } |
638 } |
585 |
639 |
586 /** |
640 /** |
587 * Retrieves the excerpt of the given comment. |
641 * Retrieves the excerpt of the given comment. |
588 * |
642 * |
589 * Returns a maximum of 20 words with an ellipsis appended if necessary. |
643 * Returns a maximum of 20 words with an ellipsis appended if necessary. |
590 * |
644 * |
591 * @since 1.5.0 |
645 * @since 1.5.0 |
592 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
646 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
593 * |
647 * |
594 * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the excerpt. |
648 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the excerpt. |
595 * Default current comment. |
649 * Default current comment. |
596 * @return string The possibly truncated comment excerpt. |
650 * @return string The possibly truncated comment excerpt. |
597 */ |
651 */ |
598 function get_comment_excerpt( $comment_ID = 0 ) { |
652 function get_comment_excerpt( $comment_id = 0 ) { |
599 $comment = get_comment( $comment_ID ); |
653 $comment = get_comment( $comment_id ); |
600 |
654 |
601 if ( ! post_password_required( $comment->comment_post_ID ) ) { |
655 if ( ! post_password_required( $comment->comment_post_ID ) ) { |
602 $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) ); |
656 $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) ); |
603 } else { |
657 } else { |
604 $comment_text = __( 'Password protected' ); |
658 $comment_text = __( 'Password protected' ); |
614 * |
668 * |
615 * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt. |
669 * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt. |
616 */ |
670 */ |
617 $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length ); |
671 $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length ); |
618 |
672 |
619 $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' ); |
673 $comment_excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' ); |
620 |
674 |
621 /** |
675 /** |
622 * Filters the retrieved comment excerpt. |
676 * Filters the retrieved comment excerpt. |
623 * |
677 * |
624 * @since 1.5.0 |
678 * @since 1.5.0 |
625 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added. |
679 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. |
626 * |
680 * |
627 * @param string $excerpt The comment excerpt text. |
681 * @param string $comment_excerpt The comment excerpt text. |
628 * @param string $comment_ID The comment ID as a numeric string. |
682 * @param string $comment_id The comment ID as a numeric string. |
629 * @param WP_Comment $comment The comment object. |
683 * @param WP_Comment $comment The comment object. |
630 */ |
684 */ |
631 return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment ); |
685 return apply_filters( 'get_comment_excerpt', $comment_excerpt, $comment->comment_ID, $comment ); |
632 } |
686 } |
633 |
687 |
634 /** |
688 /** |
635 * Displays the excerpt of the current comment. |
689 * Displays the excerpt of the current comment. |
636 * |
690 * |
637 * @since 1.2.0 |
691 * @since 1.2.0 |
638 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
692 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
639 * |
693 * |
640 * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the excerpt. |
694 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to print the excerpt. |
641 * Default current comment. |
695 * Default current comment. |
642 */ |
696 */ |
643 function comment_excerpt( $comment_ID = 0 ) { |
697 function comment_excerpt( $comment_id = 0 ) { |
644 $comment = get_comment( $comment_ID ); |
698 $comment = get_comment( $comment_id ); |
699 |
|
645 $comment_excerpt = get_comment_excerpt( $comment ); |
700 $comment_excerpt = get_comment_excerpt( $comment ); |
646 |
701 |
647 /** |
702 /** |
648 * Filters the comment excerpt for display. |
703 * Filters the comment excerpt for display. |
649 * |
704 * |
650 * @since 1.2.0 |
705 * @since 1.2.0 |
651 * @since 4.1.0 The `$comment_ID` parameter was added. |
706 * @since 4.1.0 The `$comment_id` parameter was added. |
652 * |
707 * |
653 * @param string $comment_excerpt The comment excerpt text. |
708 * @param string $comment_excerpt The comment excerpt text. |
654 * @param string $comment_ID The comment ID as a numeric string. |
709 * @param string $comment_id The comment ID as a numeric string. |
655 */ |
710 */ |
656 echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID ); |
711 echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID ); |
657 } |
712 } |
658 |
713 |
659 /** |
714 /** |
662 * @since 1.5.0 |
717 * @since 1.5.0 |
663 * |
718 * |
664 * @return string The comment ID as a numeric string. |
719 * @return string The comment ID as a numeric string. |
665 */ |
720 */ |
666 function get_comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
721 function get_comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
667 $comment = get_comment(); |
722 $comment = get_comment(); |
668 $comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : '0'; |
723 |
724 $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : '0'; |
|
669 |
725 |
670 /** |
726 /** |
671 * Filters the returned comment ID. |
727 * Filters the returned comment ID. |
672 * |
728 * |
673 * @since 1.5.0 |
729 * @since 1.5.0 |
674 * @since 4.1.0 The `$comment` parameter was added. |
730 * @since 4.1.0 The `$comment` parameter was added. |
675 * |
731 * |
676 * @param string $comment_ID The current comment ID as a numeric string. |
732 * @param string $comment_id The current comment ID as a numeric string. |
677 * @param WP_Comment $comment The comment object. |
733 * @param WP_Comment $comment The comment object. |
678 */ |
734 */ |
679 return apply_filters( 'get_comment_ID', $comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase |
735 return apply_filters( 'get_comment_ID', $comment_id, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase |
680 } |
736 } |
681 |
737 |
682 /** |
738 /** |
683 * Displays the comment ID of the current comment. |
739 * Displays the comment ID of the current comment. |
684 * |
740 * |
697 * @see get_page_of_comment() |
753 * @see get_page_of_comment() |
698 * |
754 * |
699 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
755 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
700 * @global bool $in_comment_loop |
756 * @global bool $in_comment_loop |
701 * |
757 * |
702 * @param WP_Comment|int|null $comment Comment to retrieve. Default current comment. |
758 * @param WP_Comment|int|null $comment Optional. Comment to retrieve. Default current comment. |
703 * @param array $args { |
759 * @param array $args { |
704 * An array of optional arguments to override the defaults. |
760 * An array of optional arguments to override the defaults. |
705 * |
761 * |
706 * @type string $type Passed to get_page_of_comment(). |
762 * @type string $type Passed to get_page_of_comment(). |
707 * @type int $page Current page of comments, for calculating comment pagination. |
763 * @type int $page Current page of comments, for calculating comment pagination. |
728 'page' => '', |
784 'page' => '', |
729 'per_page' => '', |
785 'per_page' => '', |
730 'max_depth' => '', |
786 'max_depth' => '', |
731 'cpage' => null, |
787 'cpage' => null, |
732 ); |
788 ); |
733 $args = wp_parse_args( $args, $defaults ); |
789 |
734 |
790 $args = wp_parse_args( $args, $defaults ); |
735 $link = get_permalink( $comment->comment_post_ID ); |
791 |
792 $comment_link = get_permalink( $comment->comment_post_ID ); |
|
736 |
793 |
737 // The 'cpage' param takes precedence. |
794 // The 'cpage' param takes precedence. |
738 if ( ! is_null( $args['cpage'] ) ) { |
795 if ( ! is_null( $args['cpage'] ) ) { |
739 $cpage = $args['cpage']; |
796 $cpage = $args['cpage']; |
740 |
797 |
770 } |
827 } |
771 |
828 |
772 if ( $cpage && get_option( 'page_comments' ) ) { |
829 if ( $cpage && get_option( 'page_comments' ) ) { |
773 if ( $wp_rewrite->using_permalinks() ) { |
830 if ( $wp_rewrite->using_permalinks() ) { |
774 if ( $cpage ) { |
831 if ( $cpage ) { |
775 $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage; |
832 $comment_link = trailingslashit( $comment_link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage; |
776 } |
833 } |
777 |
834 |
778 $link = user_trailingslashit( $link, 'comment' ); |
835 $comment_link = user_trailingslashit( $comment_link, 'comment' ); |
779 } elseif ( $cpage ) { |
836 } elseif ( $cpage ) { |
780 $link = add_query_arg( 'cpage', $cpage, $link ); |
837 $comment_link = add_query_arg( 'cpage', $cpage, $comment_link ); |
781 } |
838 } |
782 } |
839 } |
783 |
840 |
784 if ( $wp_rewrite->using_permalinks() ) { |
841 if ( $wp_rewrite->using_permalinks() ) { |
785 $link = user_trailingslashit( $link, 'comment' ); |
842 $comment_link = user_trailingslashit( $comment_link, 'comment' ); |
786 } |
843 } |
787 |
844 |
788 $link = $link . '#comment-' . $comment->comment_ID; |
845 $comment_link = $comment_link . '#comment-' . $comment->comment_ID; |
789 |
846 |
790 /** |
847 /** |
791 * Filters the returned single comment permalink. |
848 * Filters the returned single comment permalink. |
792 * |
849 * |
793 * @since 2.8.0 |
850 * @since 2.8.0 |
794 * @since 4.4.0 Added the `$cpage` parameter. |
851 * @since 4.4.0 Added the `$cpage` parameter. |
795 * |
852 * |
796 * @see get_page_of_comment() |
853 * @see get_page_of_comment() |
797 * |
854 * |
798 * @param string $link The comment permalink with '#comment-$id' appended. |
855 * @param string $comment_link The comment permalink with '#comment-$id' appended. |
799 * @param WP_Comment $comment The current comment object. |
856 * @param WP_Comment $comment The current comment object. |
800 * @param array $args An array of arguments to override the defaults. |
857 * @param array $args An array of arguments to override the defaults. |
801 * @param int $cpage The calculated 'cpage' value. |
858 * @param int $cpage The calculated 'cpage' value. |
802 */ |
859 */ |
803 return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage ); |
860 return apply_filters( 'get_comment_link', $comment_link, $comment, $args, $cpage ); |
804 } |
861 } |
805 |
862 |
806 /** |
863 /** |
807 * Retrieves the link to the current post comments. |
864 * Retrieves the link to the current post comments. |
808 * |
865 * |
809 * @since 1.5.0 |
866 * @since 1.5.0 |
810 * |
867 * |
811 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post. |
868 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
812 * @return string The link to the comments. |
869 * @return string The link to the comments. |
813 */ |
870 */ |
814 function get_comments_link( $post_id = 0 ) { |
871 function get_comments_link( $post = 0 ) { |
815 $hash = get_comments_number( $post_id ) ? '#comments' : '#respond'; |
872 $hash = get_comments_number( $post ) ? '#comments' : '#respond'; |
816 $comments_link = get_permalink( $post_id ) . $hash; |
873 $comments_link = get_permalink( $post ) . $hash; |
817 |
874 |
818 /** |
875 /** |
819 * Filters the returned post comments permalink. |
876 * Filters the returned post comments permalink. |
820 * |
877 * |
821 * @since 3.6.0 |
878 * @since 3.6.0 |
822 * |
879 * |
823 * @param string $comments_link Post comments permalink with '#comments' appended. |
880 * @param string $comments_link Post comments permalink with '#comments' appended. |
824 * @param int|WP_Post $post_id Post ID or WP_Post object. |
881 * @param int|WP_Post $post Post ID or WP_Post object. |
825 */ |
882 */ |
826 return apply_filters( 'get_comments_link', $comments_link, $post_id ); |
883 return apply_filters( 'get_comments_link', $comments_link, $post ); |
827 } |
884 } |
828 |
885 |
829 /** |
886 /** |
830 * Displays the link to the current post comments. |
887 * Displays the link to the current post comments. |
831 * |
888 * |
847 /** |
904 /** |
848 * Retrieves the amount of comments a post has. |
905 * Retrieves the amount of comments a post has. |
849 * |
906 * |
850 * @since 1.5.0 |
907 * @since 1.5.0 |
851 * |
908 * |
852 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`. |
909 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`. |
853 * @return string|int If the post exists, a numeric string representing the number of comments |
910 * @return string|int If the post exists, a numeric string representing the number of comments |
854 * the post has, otherwise 0. |
911 * the post has, otherwise 0. |
855 */ |
912 */ |
856 function get_comments_number( $post_id = 0 ) { |
913 function get_comments_number( $post = 0 ) { |
857 $post = get_post( $post_id ); |
914 $post = get_post( $post ); |
858 |
915 |
859 if ( ! $post ) { |
916 $comments_number = $post ? $post->comment_count : 0; |
860 $count = 0; |
917 $post_id = $post ? $post->ID : 0; |
861 } else { |
|
862 $count = $post->comment_count; |
|
863 $post_id = $post->ID; |
|
864 } |
|
865 |
918 |
866 /** |
919 /** |
867 * Filters the returned comment count for a post. |
920 * Filters the returned comment count for a post. |
868 * |
921 * |
869 * @since 1.5.0 |
922 * @since 1.5.0 |
870 * |
923 * |
871 * @param string|int $count A string representing the number of comments a post has, otherwise 0. |
924 * @param string|int $comments_number A string representing the number of comments a post has, otherwise 0. |
872 * @param int $post_id Post ID. |
925 * @param int $post_id Post ID. |
873 */ |
926 */ |
874 return apply_filters( 'get_comments_number', $count, $post_id ); |
927 return apply_filters( 'get_comments_number', $comments_number, $post_id ); |
875 } |
928 } |
876 |
929 |
877 /** |
930 /** |
878 * Displays the language string for the number of comments the current post has. |
931 * Displays the language string for the number of comments the current post has. |
879 * |
932 * |
880 * @since 0.71 |
933 * @since 0.71 |
881 * @since 5.4.0 The `$deprecated` parameter was changed to `$post_id`. |
934 * @since 5.4.0 The `$deprecated` parameter was changed to `$post`. |
882 * |
935 * |
883 * @param string|false $zero Optional. Text for no comments. Default false. |
936 * @param string|false $zero Optional. Text for no comments. Default false. |
884 * @param string|false $one Optional. Text for one comment. Default false. |
937 * @param string|false $one Optional. Text for one comment. Default false. |
885 * @param string|false $more Optional. Text for more than one comment. Default false. |
938 * @param string|false $more Optional. Text for more than one comment. Default false. |
886 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`. |
939 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`. |
887 */ |
940 */ |
888 function comments_number( $zero = false, $one = false, $more = false, $post_id = 0 ) { |
941 function comments_number( $zero = false, $one = false, $more = false, $post = 0 ) { |
889 echo get_comments_number_text( $zero, $one, $more, $post_id ); |
942 echo get_comments_number_text( $zero, $one, $more, $post ); |
890 } |
943 } |
891 |
944 |
892 /** |
945 /** |
893 * Displays the language string for the number of comments the current post has. |
946 * Displays the language string for the number of comments the current post has. |
894 * |
947 * |
895 * @since 4.0.0 |
948 * @since 4.0.0 |
896 * @since 5.4.0 Added the `$post_id` parameter to allow using the function outside of the loop. |
949 * @since 5.4.0 Added the `$post` parameter to allow using the function outside of the loop. |
897 * |
950 * |
898 * @param string $zero Optional. Text for no comments. Default false. |
951 * @param string $zero Optional. Text for no comments. Default false. |
899 * @param string $one Optional. Text for one comment. Default false. |
952 * @param string $one Optional. Text for one comment. Default false. |
900 * @param string $more Optional. Text for more than one comment. Default false. |
953 * @param string $more Optional. Text for more than one comment. Default false. |
901 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`. |
954 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`. |
902 * @return string Language string for the number of comments a post has. |
955 * @return string Language string for the number of comments a post has. |
903 */ |
956 */ |
904 function get_comments_number_text( $zero = false, $one = false, $more = false, $post_id = 0 ) { |
957 function get_comments_number_text( $zero = false, $one = false, $more = false, $post = 0 ) { |
905 $number = get_comments_number( $post_id ); |
958 $comments_number = get_comments_number( $post ); |
906 |
959 |
907 if ( $number > 1 ) { |
960 if ( $comments_number > 1 ) { |
908 if ( false === $more ) { |
961 if ( false === $more ) { |
909 /* translators: %s: Number of comments. */ |
962 $comments_number_text = sprintf( |
910 $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) ); |
963 /* translators: %s: Number of comments. */ |
964 _n( '%s Comment', '%s Comments', $comments_number ), |
|
965 number_format_i18n( $comments_number ) |
|
966 ); |
|
911 } else { |
967 } else { |
912 // % Comments |
968 // % Comments |
913 /* |
969 /* |
914 * translators: If comment number in your language requires declension, |
970 * translators: If comment number in your language requires declension, |
915 * translate this to 'on'. Do not translate into your own language. |
971 * translate this to 'on'. Do not translate into your own language. |
916 */ |
972 */ |
917 if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) { |
973 if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) { |
918 $text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more ); |
974 $text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more ); |
919 $text = preg_replace( '/&.+?;/', '', $text ); // Kill entities. |
975 $text = preg_replace( '/&.+?;/', '', $text ); // Remove HTML entities. |
920 $text = trim( strip_tags( $text ), '% ' ); |
976 $text = trim( strip_tags( $text ), '% ' ); |
921 |
977 |
922 // Replace '% Comments' with a proper plural form. |
978 // Replace '% Comments' with a proper plural form. |
923 if ( $text && ! preg_match( '/[0-9]+/', $text ) && false !== strpos( $more, '%' ) ) { |
979 if ( $text && ! preg_match( '/[0-9]+/', $text ) && str_contains( $more, '%' ) ) { |
924 /* translators: %s: Number of comments. */ |
980 /* translators: %s: Number of comments. */ |
925 $new_text = _n( '%s Comment', '%s Comments', $number ); |
981 $new_text = _n( '%s Comment', '%s Comments', $comments_number ); |
926 $new_text = trim( sprintf( $new_text, '' ) ); |
982 $new_text = trim( sprintf( $new_text, '' ) ); |
927 |
983 |
928 $more = str_replace( $text, $new_text, $more ); |
984 $more = str_replace( $text, $new_text, $more ); |
929 if ( false === strpos( $more, '%' ) ) { |
985 if ( ! str_contains( $more, '%' ) ) { |
930 $more = '% ' . $more; |
986 $more = '% ' . $more; |
931 } |
987 } |
932 } |
988 } |
933 } |
989 } |
934 |
990 |
935 $output = str_replace( '%', number_format_i18n( $number ), $more ); |
991 $comments_number_text = str_replace( '%', number_format_i18n( $comments_number ), $more ); |
936 } |
992 } |
937 } elseif ( 0 == $number ) { |
993 } elseif ( 0 == $comments_number ) { |
938 $output = ( false === $zero ) ? __( 'No Comments' ) : $zero; |
994 $comments_number_text = ( false === $zero ) ? __( 'No Comments' ) : $zero; |
939 } else { // Must be one. |
995 } else { // Must be one. |
940 $output = ( false === $one ) ? __( '1 Comment' ) : $one; |
996 $comments_number_text = ( false === $one ) ? __( '1 Comment' ) : $one; |
941 } |
997 } |
998 |
|
942 /** |
999 /** |
943 * Filters the comments count for display. |
1000 * Filters the comments count for display. |
944 * |
1001 * |
945 * @since 1.5.0 |
1002 * @since 1.5.0 |
946 * |
1003 * |
947 * @see _n() |
1004 * @see _n() |
948 * |
1005 * |
949 * @param string $output A translatable string formatted based on whether the count |
1006 * @param string $comments_number_text A translatable string formatted based on whether the count |
950 * is equal to 0, 1, or 1+. |
1007 * is equal to 0, 1, or 1+. |
951 * @param int $number The number of post comments. |
1008 * @param int $comments_number The number of post comments. |
952 */ |
1009 */ |
953 return apply_filters( 'comments_number', $output, $number ); |
1010 return apply_filters( 'comments_number', $comments_number_text, $comments_number ); |
954 } |
1011 } |
955 |
1012 |
956 /** |
1013 /** |
957 * Retrieves the text of the current comment. |
1014 * Retrieves the text of the current comment. |
958 * |
1015 * |
959 * @since 1.5.0 |
1016 * @since 1.5.0 |
960 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
1017 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
961 * @since 5.4.0 Added 'In reply to %s.' prefix to child comments in comments feed. |
1018 * @since 5.4.0 Added 'In reply to %s.' prefix to child comments in comments feed. |
962 * |
1019 * |
963 * @see Walker_Comment::comment() |
1020 * @see Walker_Comment::comment() |
964 * |
1021 * |
965 * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the text. |
1022 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the text. |
966 * Default current comment. |
1023 * Default current comment. |
967 * @param array $args Optional. An array of arguments. Default empty array. |
1024 * @param array $args Optional. An array of arguments. Default empty array. |
968 * @return string The comment content. |
1025 * @return string The comment content. |
969 */ |
1026 */ |
970 function get_comment_text( $comment_ID = 0, $args = array() ) { |
1027 function get_comment_text( $comment_id = 0, $args = array() ) { |
971 $comment = get_comment( $comment_ID ); |
1028 $comment = get_comment( $comment_id ); |
972 |
1029 |
973 $comment_content = $comment->comment_content; |
1030 $comment_text = $comment->comment_content; |
974 |
1031 |
975 if ( is_comment_feed() && $comment->comment_parent ) { |
1032 if ( is_comment_feed() && $comment->comment_parent ) { |
976 $parent = get_comment( $comment->comment_parent ); |
1033 $parent = get_comment( $comment->comment_parent ); |
977 if ( $parent ) { |
1034 if ( $parent ) { |
978 $parent_link = esc_url( get_comment_link( $parent ) ); |
1035 $parent_link = esc_url( get_comment_link( $parent ) ); |
979 $name = get_comment_author( $parent ); |
1036 $name = get_comment_author( $parent ); |
980 |
1037 |
981 $comment_content = sprintf( |
1038 $comment_text = sprintf( |
982 /* translators: %s: Comment link. */ |
1039 /* translators: %s: Comment link. */ |
983 ent2ncr( __( 'In reply to %s.' ) ), |
1040 ent2ncr( __( 'In reply to %s.' ) ), |
984 '<a href="' . $parent_link . '">' . $name . '</a>' |
1041 '<a href="' . $parent_link . '">' . $name . '</a>' |
985 ) . "\n\n" . $comment_content; |
1042 ) . "\n\n" . $comment_text; |
986 } |
1043 } |
987 } |
1044 } |
988 |
1045 |
989 /** |
1046 /** |
990 * Filters the text of a comment. |
1047 * Filters the text of a comment. |
991 * |
1048 * |
992 * @since 1.5.0 |
1049 * @since 1.5.0 |
993 * |
1050 * |
994 * @see Walker_Comment::comment() |
1051 * @see Walker_Comment::comment() |
995 * |
1052 * |
996 * @param string $comment_content Text of the comment. |
1053 * @param string $comment_text Text of the comment. |
997 * @param WP_Comment $comment The comment object. |
1054 * @param WP_Comment $comment The comment object. |
998 * @param array $args An array of arguments. |
1055 * @param array $args An array of arguments. |
999 */ |
1056 */ |
1000 return apply_filters( 'get_comment_text', $comment_content, $comment, $args ); |
1057 return apply_filters( 'get_comment_text', $comment_text, $comment, $args ); |
1001 } |
1058 } |
1002 |
1059 |
1003 /** |
1060 /** |
1004 * Displays the text of the current comment. |
1061 * Displays the text of the current comment. |
1005 * |
1062 * |
1006 * @since 0.71 |
1063 * @since 0.71 |
1007 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
1064 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
1008 * |
1065 * |
1009 * @see Walker_Comment::comment() |
1066 * @see Walker_Comment::comment() |
1010 * |
1067 * |
1011 * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the text. |
1068 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to print the text. |
1012 * Default current comment. |
1069 * Default current comment. |
1013 * @param array $args Optional. An array of arguments. Default empty array. |
1070 * @param array $args Optional. An array of arguments. Default empty array. |
1014 */ |
1071 */ |
1015 function comment_text( $comment_ID = 0, $args = array() ) { |
1072 function comment_text( $comment_id = 0, $args = array() ) { |
1016 $comment = get_comment( $comment_ID ); |
1073 $comment = get_comment( $comment_id ); |
1017 |
1074 |
1018 $comment_text = get_comment_text( $comment, $args ); |
1075 $comment_text = get_comment_text( $comment, $args ); |
1076 |
|
1019 /** |
1077 /** |
1020 * Filters the text of a comment to be displayed. |
1078 * Filters the text of a comment to be displayed. |
1021 * |
1079 * |
1022 * @since 1.2.0 |
1080 * @since 1.2.0 |
1023 * |
1081 * |
1024 * @see Walker_Comment::comment() |
1082 * @see Walker_Comment::comment() |
1025 * |
1083 * |
1026 * @param string $comment_text Text of the current comment. |
1084 * @param string $comment_text Text of the comment. |
1027 * @param WP_Comment|null $comment The comment object. Null if not found. |
1085 * @param WP_Comment|null $comment The comment object. Null if not found. |
1028 * @param array $args An array of arguments. |
1086 * @param array $args An array of arguments. |
1029 */ |
1087 */ |
1030 echo apply_filters( 'comment_text', $comment_text, $comment, $args ); |
1088 echo apply_filters( 'comment_text', $comment_text, $comment, $args ); |
1031 } |
1089 } |
1032 |
1090 |
1033 /** |
1091 /** |
1034 * Retrieves the comment time of the current comment. |
1092 * Retrieves the comment time of the current comment. |
1035 * |
1093 * |
1036 * @since 1.5.0 |
1094 * @since 1.5.0 |
1037 * |
1095 * @since 6.2.0 Added the `$comment_id` parameter. |
1038 * @param string $format Optional. PHP time format. Defaults to the 'time_format' option. |
1096 * |
1039 * @param bool $gmt Optional. Whether to use the GMT date. Default false. |
1097 * @param string $format Optional. PHP date format. Defaults to the 'time_format' option. |
1040 * @param bool $translate Optional. Whether to translate the time (for use in feeds). |
1098 * @param bool $gmt Optional. Whether to use the GMT date. Default false. |
1041 * Default true. |
1099 * @param bool $translate Optional. Whether to translate the time (for use in feeds). |
1100 * Default true. |
|
1101 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the time. |
|
1102 * Default current comment. |
|
1042 * @return string The formatted time. |
1103 * @return string The formatted time. |
1043 */ |
1104 */ |
1044 function get_comment_time( $format = '', $gmt = false, $translate = true ) { |
1105 function get_comment_time( $format = '', $gmt = false, $translate = true, $comment_id = 0 ) { |
1045 $comment = get_comment(); |
1106 $comment = get_comment( $comment_id ); |
1107 |
|
1108 if ( null === $comment ) { |
|
1109 return ''; |
|
1110 } |
|
1046 |
1111 |
1047 $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date; |
1112 $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date; |
1048 |
1113 |
1049 $_format = ! empty( $format ) ? $format : get_option( 'time_format' ); |
1114 $_format = ! empty( $format ) ? $format : get_option( 'time_format' ); |
1050 |
1115 |
1051 $date = mysql2date( $_format, $comment_date, $translate ); |
1116 $comment_time = mysql2date( $_format, $comment_date, $translate ); |
1052 |
1117 |
1053 /** |
1118 /** |
1054 * Filters the returned comment time. |
1119 * Filters the returned comment time. |
1055 * |
1120 * |
1056 * @since 1.5.0 |
1121 * @since 1.5.0 |
1057 * |
1122 * |
1058 * @param string|int $date The comment time, formatted as a date string or Unix timestamp. |
1123 * @param string|int $comment_time The comment time, formatted as a date string or Unix timestamp. |
1059 * @param string $format PHP date format. |
1124 * @param string $format PHP date format. |
1060 * @param bool $gmt Whether the GMT date is in use. |
1125 * @param bool $gmt Whether the GMT date is in use. |
1061 * @param bool $translate Whether the time is translated. |
1126 * @param bool $translate Whether the time is translated. |
1062 * @param WP_Comment $comment The comment object. |
1127 * @param WP_Comment $comment The comment object. |
1063 */ |
1128 */ |
1064 return apply_filters( 'get_comment_time', $date, $format, $gmt, $translate, $comment ); |
1129 return apply_filters( 'get_comment_time', $comment_time, $format, $gmt, $translate, $comment ); |
1065 } |
1130 } |
1066 |
1131 |
1067 /** |
1132 /** |
1068 * Displays the comment time of the current comment. |
1133 * Displays the comment time of the current comment. |
1069 * |
1134 * |
1070 * @since 0.71 |
1135 * @since 0.71 |
1071 * |
1136 * @since 6.2.0 Added the `$comment_id` parameter. |
1072 * @param string $format Optional. PHP time format. Defaults to the 'time_format' option. |
1137 * |
1073 */ |
1138 * @param string $format Optional. PHP time format. Defaults to the 'time_format' option. |
1074 function comment_time( $format = '' ) { |
1139 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to print the time. |
1075 echo get_comment_time( $format ); |
1140 * Default current comment. |
1141 */ |
|
1142 function comment_time( $format = '', $comment_id = 0 ) { |
|
1143 echo get_comment_time( $format, false, true, $comment_id ); |
|
1076 } |
1144 } |
1077 |
1145 |
1078 /** |
1146 /** |
1079 * Retrieves the comment type of the current comment. |
1147 * Retrieves the comment type of the current comment. |
1080 * |
1148 * |
1081 * @since 1.5.0 |
1149 * @since 1.5.0 |
1082 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. |
1150 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. |
1083 * |
1151 * |
1084 * @param int|WP_Comment $comment_ID Optional. WP_Comment or ID of the comment for which to get the type. |
1152 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the type. |
1085 * Default current comment. |
1153 * Default current comment. |
1086 * @return string The comment type. |
1154 * @return string The comment type. |
1087 */ |
1155 */ |
1088 function get_comment_type( $comment_ID = 0 ) { |
1156 function get_comment_type( $comment_id = 0 ) { |
1089 $comment = get_comment( $comment_ID ); |
1157 $comment = get_comment( $comment_id ); |
1090 |
1158 |
1091 if ( '' === $comment->comment_type ) { |
1159 if ( '' === $comment->comment_type ) { |
1092 $comment->comment_type = 'comment'; |
1160 $comment->comment_type = 'comment'; |
1093 } |
1161 } |
1094 |
1162 |
1095 /** |
1163 /** |
1096 * Filters the returned comment type. |
1164 * Filters the returned comment type. |
1097 * |
1165 * |
1098 * @since 1.5.0 |
1166 * @since 1.5.0 |
1099 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added. |
1167 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. |
1100 * |
1168 * |
1101 * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'. |
1169 * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'. |
1102 * @param string $comment_ID The comment ID as a numeric string. |
1170 * @param string $comment_id The comment ID as a numeric string. |
1103 * @param WP_Comment $comment The comment object. |
1171 * @param WP_Comment $comment The comment object. |
1104 */ |
1172 */ |
1105 return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment ); |
1173 return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment ); |
1106 } |
1174 } |
1107 |
1175 |
1148 * |
1216 * |
1149 * @return string The trackback URL after being filtered. |
1217 * @return string The trackback URL after being filtered. |
1150 */ |
1218 */ |
1151 function get_trackback_url() { |
1219 function get_trackback_url() { |
1152 if ( get_option( 'permalink_structure' ) ) { |
1220 if ( get_option( 'permalink_structure' ) ) { |
1153 $tb_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' ); |
1221 $trackback_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' ); |
1154 } else { |
1222 } else { |
1155 $tb_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID(); |
1223 $trackback_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID(); |
1156 } |
1224 } |
1157 |
1225 |
1158 /** |
1226 /** |
1159 * Filters the returned trackback URL. |
1227 * Filters the returned trackback URL. |
1160 * |
1228 * |
1161 * @since 2.2.0 |
1229 * @since 2.2.0 |
1162 * |
1230 * |
1163 * @param string $tb_url The trackback URL. |
1231 * @param string $trackback_url The trackback URL. |
1164 */ |
1232 */ |
1165 return apply_filters( 'trackback_url', $tb_url ); |
1233 return apply_filters( 'trackback_url', $trackback_url ); |
1166 } |
1234 } |
1167 |
1235 |
1168 /** |
1236 /** |
1169 * Displays the current post's trackback URL. |
1237 * Displays the current post's trackback URL. |
1170 * |
1238 * |
1233 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
1301 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
1234 * Conditional Tags} article in the Theme Developer Handbook. |
1302 * Conditional Tags} article in the Theme Developer Handbook. |
1235 * |
1303 * |
1236 * @since 1.5.0 |
1304 * @since 1.5.0 |
1237 * |
1305 * |
1238 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post. |
1306 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. |
1239 * @return bool True if the comments are open. |
1307 * @return bool True if the comments are open. |
1240 */ |
1308 */ |
1241 function comments_open( $post_id = null ) { |
1309 function comments_open( $post = null ) { |
1242 |
1310 $_post = get_post( $post ); |
1243 $_post = get_post( $post_id ); |
1311 |
1244 |
1312 $post_id = $_post ? $_post->ID : 0; |
1245 $post_id = $_post ? $_post->ID : 0; |
1313 $comments_open = ( $_post && ( 'open' === $_post->comment_status ) ); |
1246 $open = ( $_post && ( 'open' === $_post->comment_status ) ); |
|
1247 |
1314 |
1248 /** |
1315 /** |
1249 * Filters whether the current post is open for comments. |
1316 * Filters whether the current post is open for comments. |
1250 * |
1317 * |
1251 * @since 2.5.0 |
1318 * @since 2.5.0 |
1252 * |
1319 * |
1253 * @param bool $open Whether the current post is open for comments. |
1320 * @param bool $comments_open Whether the current post is open for comments. |
1254 * @param int $post_id The post ID. |
1321 * @param int $post_id The post ID. |
1255 */ |
1322 */ |
1256 return apply_filters( 'comments_open', $open, $post_id ); |
1323 return apply_filters( 'comments_open', $comments_open, $post_id ); |
1257 } |
1324 } |
1258 |
1325 |
1259 /** |
1326 /** |
1260 * Determines whether the current post is open for pings. |
1327 * Determines whether the current post is open for pings. |
1261 * |
1328 * |
1263 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
1330 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
1264 * Conditional Tags} article in the Theme Developer Handbook. |
1331 * Conditional Tags} article in the Theme Developer Handbook. |
1265 * |
1332 * |
1266 * @since 1.5.0 |
1333 * @since 1.5.0 |
1267 * |
1334 * |
1268 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post. |
1335 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. |
1269 * @return bool True if pings are accepted |
1336 * @return bool True if pings are accepted |
1270 */ |
1337 */ |
1271 function pings_open( $post_id = null ) { |
1338 function pings_open( $post = null ) { |
1272 |
1339 $_post = get_post( $post ); |
1273 $_post = get_post( $post_id ); |
1340 |
1274 |
1341 $post_id = $_post ? $_post->ID : 0; |
1275 $post_id = $_post ? $_post->ID : 0; |
1342 $pings_open = ( $_post && ( 'open' === $_post->ping_status ) ); |
1276 $open = ( $_post && ( 'open' === $_post->ping_status ) ); |
|
1277 |
1343 |
1278 /** |
1344 /** |
1279 * Filters whether the current post is open for pings. |
1345 * Filters whether the current post is open for pings. |
1280 * |
1346 * |
1281 * @since 2.5.0 |
1347 * @since 2.5.0 |
1282 * |
1348 * |
1283 * @param bool $open Whether the current post is open for pings. |
1349 * @param bool $pings_open Whether the current post is open for pings. |
1284 * @param int $post_id The post ID. |
1350 * @param int $post_id The post ID. |
1285 */ |
1351 */ |
1286 return apply_filters( 'pings_open', $open, $post_id ); |
1352 return apply_filters( 'pings_open', $pings_open, $post_id ); |
1287 } |
1353 } |
1288 |
1354 |
1289 /** |
1355 /** |
1290 * Displays form token for unfiltered comments. |
1356 * Displays form token for unfiltered comments. |
1291 * |
1357 * |
1304 $post = get_post(); |
1370 $post = get_post(); |
1305 $post_id = $post ? $post->ID : 0; |
1371 $post_id = $post ? $post->ID : 0; |
1306 |
1372 |
1307 if ( current_user_can( 'unfiltered_html' ) ) { |
1373 if ( current_user_can( 'unfiltered_html' ) ) { |
1308 wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false ); |
1374 wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false ); |
1309 echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n"; |
1375 wp_print_inline_script_tag( "(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();" ); |
1310 } |
1376 } |
1311 } |
1377 } |
1312 |
1378 |
1313 /** |
1379 /** |
1314 * Loads the comment template specified in $file. |
1380 * Loads the comment template specified in $file. |
1319 * Uses the WordPress database object to query for the comments. The comments |
1385 * Uses the WordPress database object to query for the comments. The comments |
1320 * are passed through the {@see 'comments_array'} filter hook with the list of comments |
1386 * are passed through the {@see 'comments_array'} filter hook with the list of comments |
1321 * and the post ID respectively. |
1387 * and the post ID respectively. |
1322 * |
1388 * |
1323 * The `$file` path is passed through a filter hook called {@see 'comments_template'}, |
1389 * The `$file` path is passed through a filter hook called {@see 'comments_template'}, |
1324 * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path |
1390 * which includes the template directory and $file combined. Tries the $filtered path |
1325 * first and if it fails it will require the default comment template from the |
1391 * first and if it fails it will require the default comment template from the |
1326 * default theme. If either does not exist, then the WordPress process will be |
1392 * default theme. If either does not exist, then the WordPress process will be |
1327 * halted. It is advised for that reason, that the default theme is not deleted. |
1393 * halted. It is advised for that reason, that the default theme is not deleted. |
1328 * |
1394 * |
1329 * Will not try to get the comments if the post has none. |
1395 * Will not try to get the comments if the post has none. |
1330 * |
1396 * |
1331 * @since 1.5.0 |
1397 * @since 1.5.0 |
1332 * |
1398 * |
1333 * @global WP_Query $wp_query WordPress Query object. |
1399 * @global WP_Query $wp_query WordPress Query object. |
1334 * @global WP_Post $post Global post object. |
1400 * @global WP_Post $post Global post object. |
1335 * @global wpdb $wpdb WordPress database abstraction object. |
1401 * @global wpdb $wpdb WordPress database abstraction object. |
1336 * @global int $id |
1402 * @global int $id |
1337 * @global WP_Comment $comment Global comment object. |
1403 * @global WP_Comment $comment Global comment object. |
1338 * @global string $user_login |
1404 * @global string $user_login |
1339 * @global string $user_identity |
1405 * @global string $user_identity |
1340 * @global bool $overridden_cpage |
1406 * @global bool $overridden_cpage |
1341 * @global bool $withcomments |
1407 * @global bool $withcomments |
1408 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory. |
|
1409 * @global string $wp_template_path Path to current theme's template directory. |
|
1342 * |
1410 * |
1343 * @param string $file Optional. The file to load. Default '/comments.php'. |
1411 * @param string $file Optional. The file to load. Default '/comments.php'. |
1344 * @param bool $separate_comments Optional. Whether to separate the comments by comment type. |
1412 * @param bool $separate_comments Optional. Whether to separate the comments by comment type. |
1345 * Default false. |
1413 * Default false. |
1346 */ |
1414 */ |
1347 function comments_template( $file = '/comments.php', $separate_comments = false ) { |
1415 function comments_template( $file = '/comments.php', $separate_comments = false ) { |
1348 global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage; |
1416 global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage, $wp_stylesheet_path, $wp_template_path; |
1349 |
1417 |
1350 if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) { |
1418 if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) { |
1351 return; |
1419 return; |
1352 } |
1420 } |
1353 |
1421 |
1378 * The URL of the current comment author escaped for use in attributes. |
1446 * The URL of the current comment author escaped for use in attributes. |
1379 */ |
1447 */ |
1380 $comment_author_url = esc_url( $commenter['comment_author_url'] ); |
1448 $comment_author_url = esc_url( $commenter['comment_author_url'] ); |
1381 |
1449 |
1382 $comment_args = array( |
1450 $comment_args = array( |
1383 'orderby' => 'comment_date_gmt', |
1451 'orderby' => 'comment_date_gmt', |
1384 'order' => 'ASC', |
1452 'order' => 'ASC', |
1385 'status' => 'approve', |
1453 'status' => 'approve', |
1386 'post_id' => $post->ID, |
1454 'post_id' => $post->ID, |
1387 'no_found_rows' => false, |
1455 'no_found_rows' => false, |
1388 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance. |
|
1389 ); |
1456 ); |
1390 |
1457 |
1391 if ( get_option( 'thread_comments' ) ) { |
1458 if ( get_option( 'thread_comments' ) ) { |
1392 $comment_args['hierarchical'] = 'threaded'; |
1459 $comment_args['hierarchical'] = 'threaded'; |
1393 } else { |
1460 } else { |
1454 */ |
1521 */ |
1455 $top_level_args = apply_filters( 'comments_template_top_level_query_args', $top_level_args ); |
1522 $top_level_args = apply_filters( 'comments_template_top_level_query_args', $top_level_args ); |
1456 |
1523 |
1457 $top_level_count = $top_level_query->query( $top_level_args ); |
1524 $top_level_count = $top_level_query->query( $top_level_args ); |
1458 |
1525 |
1459 $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page; |
1526 $comment_args['offset'] = ( (int) ceil( $top_level_count / $per_page ) - 1 ) * $per_page; |
1460 } |
1527 } |
1461 } |
1528 } |
1462 |
1529 |
1463 /** |
1530 /** |
1464 * Filters the arguments used to query comments in comments_template(). |
1531 * Filters the arguments used to query comments in comments_template(). |
1513 * Filters the comments array. |
1580 * Filters the comments array. |
1514 * |
1581 * |
1515 * @since 2.1.0 |
1582 * @since 2.1.0 |
1516 * |
1583 * |
1517 * @param array $comments Array of comments supplied to the comments template. |
1584 * @param array $comments Array of comments supplied to the comments template. |
1518 * @param int $post_ID Post ID. |
1585 * @param int $post_id Post ID. |
1519 */ |
1586 */ |
1520 $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID ); |
1587 $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID ); |
1521 |
1588 |
1522 $comments = &$wp_query->comments; |
1589 $comments = &$wp_query->comments; |
1523 $wp_query->comment_count = count( $wp_query->comments ); |
1590 $wp_query->comment_count = count( $wp_query->comments ); |
1539 |
1606 |
1540 if ( ! defined( 'COMMENTS_TEMPLATE' ) ) { |
1607 if ( ! defined( 'COMMENTS_TEMPLATE' ) ) { |
1541 define( 'COMMENTS_TEMPLATE', true ); |
1608 define( 'COMMENTS_TEMPLATE', true ); |
1542 } |
1609 } |
1543 |
1610 |
1544 $theme_template = STYLESHEETPATH . $file; |
1611 $theme_template = trailingslashit( $wp_stylesheet_path ) . $file; |
1545 |
1612 |
1546 /** |
1613 /** |
1547 * Filters the path to the theme template file used for the comments template. |
1614 * Filters the path to the theme template file used for the comments template. |
1548 * |
1615 * |
1549 * @since 1.5.1 |
1616 * @since 1.5.1 |
1552 */ |
1619 */ |
1553 $include = apply_filters( 'comments_template', $theme_template ); |
1620 $include = apply_filters( 'comments_template', $theme_template ); |
1554 |
1621 |
1555 if ( file_exists( $include ) ) { |
1622 if ( file_exists( $include ) ) { |
1556 require $include; |
1623 require $include; |
1557 } elseif ( file_exists( TEMPLATEPATH . $file ) ) { |
1624 } elseif ( file_exists( trailingslashit( $wp_template_path ) . $file ) ) { |
1558 require TEMPLATEPATH . $file; |
1625 require trailingslashit( $wp_template_path ) . $file; |
1559 } else { // Backward compat code will be removed in a future release. |
1626 } else { // Backward compat code will be removed in a future release. |
1560 require ABSPATH . WPINC . '/theme-compat/comments.php'; |
1627 require ABSPATH . WPINC . '/theme-compat/comments.php'; |
1561 } |
1628 } |
1562 } |
1629 } |
1563 |
1630 |
1571 * @param false|string $more Optional. String to display when there are more than one comment. Default false. |
1638 * @param false|string $more Optional. String to display when there are more than one comment. Default false. |
1572 * @param string $css_class Optional. CSS class to use for comments. Default empty. |
1639 * @param string $css_class Optional. CSS class to use for comments. Default empty. |
1573 * @param false|string $none Optional. String to display when comments have been turned off. Default false. |
1640 * @param false|string $none Optional. String to display when comments have been turned off. Default false. |
1574 */ |
1641 */ |
1575 function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) { |
1642 function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) { |
1576 $post_id = get_the_ID(); |
1643 $post_id = get_the_ID(); |
1577 $post_title = get_the_title(); |
1644 $post_title = get_the_title(); |
1578 $number = get_comments_number( $post_id ); |
1645 $comments_number = get_comments_number( $post_id ); |
1579 |
1646 |
1580 if ( false === $zero ) { |
1647 if ( false === $zero ) { |
1581 /* translators: %s: Post title. */ |
1648 /* translators: %s: Post title. */ |
1582 $zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $post_title ); |
1649 $zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $post_title ); |
1583 } |
1650 } |
1587 $one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $post_title ); |
1654 $one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $post_title ); |
1588 } |
1655 } |
1589 |
1656 |
1590 if ( false === $more ) { |
1657 if ( false === $more ) { |
1591 /* translators: 1: Number of comments, 2: Post title. */ |
1658 /* translators: 1: Number of comments, 2: Post title. */ |
1592 $more = _n( '%1$s Comment<span class="screen-reader-text"> on %2$s</span>', '%1$s Comments<span class="screen-reader-text"> on %2$s</span>', $number ); |
1659 $more = _n( |
1593 $more = sprintf( $more, number_format_i18n( $number ), $post_title ); |
1660 '%1$s Comment<span class="screen-reader-text"> on %2$s</span>', |
1661 '%1$s Comments<span class="screen-reader-text"> on %2$s</span>', |
|
1662 $comments_number |
|
1663 ); |
|
1664 $more = sprintf( $more, number_format_i18n( $comments_number ), $post_title ); |
|
1594 } |
1665 } |
1595 |
1666 |
1596 if ( false === $none ) { |
1667 if ( false === $none ) { |
1597 /* translators: %s: Post title. */ |
1668 /* translators: %s: Post title. */ |
1598 $none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $post_title ); |
1669 $none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $post_title ); |
1599 } |
1670 } |
1600 |
1671 |
1601 if ( 0 == $number && ! comments_open() && ! pings_open() ) { |
1672 if ( 0 == $comments_number && ! comments_open() && ! pings_open() ) { |
1602 echo '<span' . ( ( ! empty( $css_class ) ) ? ' class="' . esc_attr( $css_class ) . '"' : '' ) . '>' . $none . '</span>'; |
1673 printf( |
1674 '<span%1$s>%2$s</span>', |
|
1675 ! empty( $css_class ) ? ' class="' . esc_attr( $css_class ) . '"' : '', |
|
1676 $none |
|
1677 ); |
|
1603 return; |
1678 return; |
1604 } |
1679 } |
1605 |
1680 |
1606 if ( post_password_required() ) { |
1681 if ( post_password_required() ) { |
1607 _e( 'Enter your password to view comments.' ); |
1682 _e( 'Enter your password to view comments.' ); |
1608 return; |
1683 return; |
1609 } |
1684 } |
1610 |
1685 |
1611 echo '<a href="'; |
1686 if ( 0 == $comments_number ) { |
1612 if ( 0 == $number ) { |
|
1613 $respond_link = get_permalink() . '#respond'; |
1687 $respond_link = get_permalink() . '#respond'; |
1614 /** |
1688 /** |
1615 * Filters the respond link when a post has no comments. |
1689 * Filters the respond link when a post has no comments. |
1616 * |
1690 * |
1617 * @since 4.4.0 |
1691 * @since 4.4.0 |
1618 * |
1692 * |
1619 * @param string $respond_link The default response link. |
1693 * @param string $respond_link The default response link. |
1620 * @param int $post_id The post ID. |
1694 * @param int $post_id The post ID. |
1621 */ |
1695 */ |
1622 echo apply_filters( 'respond_link', $respond_link, $post_id ); |
1696 $comments_link = apply_filters( 'respond_link', $respond_link, $post_id ); |
1623 } else { |
1697 } else { |
1624 comments_link(); |
1698 $comments_link = get_comments_link(); |
1625 } |
1699 } |
1626 echo '"'; |
1700 |
1627 |
1701 $link_attributes = ''; |
1628 if ( ! empty( $css_class ) ) { |
1702 |
1629 echo ' class="' . $css_class . '" '; |
|
1630 } |
|
1631 |
|
1632 $attributes = ''; |
|
1633 /** |
1703 /** |
1634 * Filters the comments link attributes for display. |
1704 * Filters the comments link attributes for display. |
1635 * |
1705 * |
1636 * @since 2.5.0 |
1706 * @since 2.5.0 |
1637 * |
1707 * |
1638 * @param string $attributes The comments link attributes. Default empty. |
1708 * @param string $link_attributes The comments link attributes. Default empty. |
1639 */ |
1709 */ |
1640 echo apply_filters( 'comments_popup_link_attributes', $attributes ); |
1710 $link_attributes = apply_filters( 'comments_popup_link_attributes', $link_attributes ); |
1641 |
1711 |
1642 echo '>'; |
1712 printf( |
1643 comments_number( $zero, $one, $more ); |
1713 '<a href="%1$s"%2$s%3$s>%4$s</a>', |
1644 echo '</a>'; |
1714 esc_url( $comments_link ), |
1715 ! empty( $css_class ) ? ' class="' . $css_class . '" ' : '', |
|
1716 $link_attributes, |
|
1717 get_comments_number_text( $zero, $one, $more ) |
|
1718 ); |
|
1645 } |
1719 } |
1646 |
1720 |
1647 /** |
1721 /** |
1648 * Retrieves HTML content for reply to comment link. |
1722 * Retrieves HTML content for reply to comment link. |
1649 * |
1723 * |
1665 * @type int $depth The depth of the new comment. Must be greater than 0 and less than the value |
1739 * @type int $depth The depth of the new comment. Must be greater than 0 and less than the value |
1666 * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0. |
1740 * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0. |
1667 * @type string $before The text or HTML to add before the reply link. Default empty. |
1741 * @type string $before The text or HTML to add before the reply link. Default empty. |
1668 * @type string $after The text or HTML to add after the reply link. Default empty. |
1742 * @type string $after The text or HTML to add after the reply link. Default empty. |
1669 * } |
1743 * } |
1670 * @param int|WP_Comment $comment Comment being replied to. Default current comment. |
1744 * @param int|WP_Comment $comment Optional. Comment being replied to. Default current comment. |
1671 * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on. |
1745 * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on. |
1672 * Default current post. |
1746 * Default current post. |
1673 * @return string|false|null Link to show comment form, if successful. False, if comments are closed. |
1747 * @return string|false|null Link to show comment form, if successful. False, if comments are closed. |
1674 */ |
1748 */ |
1675 function get_comment_reply_link( $args = array(), $comment = null, $post = null ) { |
1749 function get_comment_reply_link( $args = array(), $comment = null, $post = null ) { |
1676 $defaults = array( |
1750 $defaults = array( |
1742 ); |
1816 ); |
1743 |
1817 |
1744 $data_attribute_string = ''; |
1818 $data_attribute_string = ''; |
1745 |
1819 |
1746 foreach ( $data_attributes as $name => $value ) { |
1820 foreach ( $data_attributes as $name => $value ) { |
1747 $data_attribute_string .= " data-${name}=\"" . esc_attr( $value ) . '"'; |
1821 $data_attribute_string .= " data-{$name}=\"" . esc_attr( $value ) . '"'; |
1748 } |
1822 } |
1749 |
1823 |
1750 $data_attribute_string = trim( $data_attribute_string ); |
1824 $data_attribute_string = trim( $data_attribute_string ); |
1751 |
1825 |
1752 $link = sprintf( |
1826 $link = sprintf( |
1765 esc_attr( sprintf( $args['reply_to_text'], get_comment_author( $comment ) ) ), |
1839 esc_attr( sprintf( $args['reply_to_text'], get_comment_author( $comment ) ) ), |
1766 $args['reply_text'] |
1840 $args['reply_text'] |
1767 ); |
1841 ); |
1768 } |
1842 } |
1769 |
1843 |
1844 $comment_reply_link = $args['before'] . $link . $args['after']; |
|
1845 |
|
1770 /** |
1846 /** |
1771 * Filters the comment reply link. |
1847 * Filters the comment reply link. |
1772 * |
1848 * |
1773 * @since 2.7.0 |
1849 * @since 2.7.0 |
1774 * |
1850 * |
1775 * @param string $link The HTML markup for the comment reply link. |
1851 * @param string $comment_reply_link The HTML markup for the comment reply link. |
1776 * @param array $args An array of arguments overriding the defaults. |
1852 * @param array $args An array of arguments overriding the defaults. |
1777 * @param WP_Comment $comment The object of the comment being replied. |
1853 * @param WP_Comment $comment The object of the comment being replied. |
1778 * @param WP_Post $post The WP_Post object. |
1854 * @param WP_Post $post The WP_Post object. |
1779 */ |
1855 */ |
1780 return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post ); |
1856 return apply_filters( 'comment_reply_link', $comment_reply_link, $args, $comment, $post ); |
1781 } |
1857 } |
1782 |
1858 |
1783 /** |
1859 /** |
1784 * Displays the HTML content for reply to comment link. |
1860 * Displays the HTML content for reply to comment link. |
1785 * |
1861 * |
1786 * @since 2.7.0 |
1862 * @since 2.7.0 |
1787 * |
1863 * |
1788 * @see get_comment_reply_link() |
1864 * @see get_comment_reply_link() |
1789 * |
1865 * |
1790 * @param array $args Optional. Override default options. Default empty array. |
1866 * @param array $args Optional. Override default options. Default empty array. |
1791 * @param int|WP_Comment $comment Comment being replied to. Default current comment. |
1867 * @param int|WP_Comment $comment Optional. Comment being replied to. Default current comment. |
1792 * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on. |
1868 * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on. |
1793 * Default current post. |
1869 * Default current post. |
1794 */ |
1870 */ |
1795 function comment_reply_link( $args = array(), $comment = null, $post = null ) { |
1871 function comment_reply_link( $args = array(), $comment = null, $post = null ) { |
1796 echo get_comment_reply_link( $args, $comment, $post ); |
1872 echo get_comment_reply_link( $args, $comment, $post ); |
1797 } |
1873 } |
1856 get_permalink( $post->ID ) . '#' . $args['respond_id'], |
1932 get_permalink( $post->ID ) . '#' . $args['respond_id'], |
1857 $onclick, |
1933 $onclick, |
1858 $args['reply_text'] |
1934 $args['reply_text'] |
1859 ); |
1935 ); |
1860 } |
1936 } |
1861 $formatted_link = $args['before'] . $link . $args['after']; |
1937 |
1938 $post_reply_link = $args['before'] . $link . $args['after']; |
|
1862 |
1939 |
1863 /** |
1940 /** |
1864 * Filters the formatted post comments link HTML. |
1941 * Filters the formatted post comments link HTML. |
1865 * |
1942 * |
1866 * @since 2.7.0 |
1943 * @since 2.7.0 |
1867 * |
1944 * |
1868 * @param string $formatted The HTML-formatted post comments link. |
1945 * @param string $post_reply_link The HTML-formatted post comments link. |
1869 * @param int|WP_Post $post The post ID or WP_Post object. |
1946 * @param int|WP_Post $post The post ID or WP_Post object. |
1870 */ |
1947 */ |
1871 return apply_filters( 'post_comments_link', $formatted_link, $post ); |
1948 return apply_filters( 'post_comments_link', $post_reply_link, $post ); |
1872 } |
1949 } |
1873 |
1950 |
1874 /** |
1951 /** |
1875 * Displays the HTML content for reply to post link. |
1952 * Displays the HTML content for reply to post link. |
1876 * |
1953 * |
1877 * @since 2.7.0 |
1954 * @since 2.7.0 |
1878 * |
1955 * |
1879 * @see get_post_reply_link() |
1956 * @see get_post_reply_link() |
1880 * |
1957 * |
1881 * @param array $args Optional. Override default options. Default empty array. |
1958 * @param array $args Optional. Override default options. Default empty array. |
1882 * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on. |
1959 * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on. |
1883 * Default current post. |
1960 * Default current post. |
1884 */ |
1961 */ |
1885 function post_reply_link( $args = array(), $post = null ) { |
1962 function post_reply_link( $args = array(), $post = null ) { |
1886 echo get_post_reply_link( $args, $post ); |
1963 echo get_post_reply_link( $args, $post ); |
1887 } |
1964 } |
1888 |
1965 |
1889 /** |
1966 /** |
1890 * Retrieves HTML content for cancel comment reply link. |
1967 * Retrieves HTML content for cancel comment reply link. |
1891 * |
1968 * |
1892 * @since 2.7.0 |
1969 * @since 2.7.0 |
1893 * |
1970 * @since 6.2.0 Added the `$post` parameter. |
1894 * @param string $text Optional. Text to display for cancel reply link. If empty, |
1971 * |
1972 * @param string $link_text Optional. Text to display for cancel reply link. If empty, |
|
1973 * defaults to 'Click here to cancel reply'. Default empty. |
|
1974 * @param int|WP_Post|null $post Optional. The post the comment thread is being |
|
1975 * displayed for. Defaults to the current global post. |
|
1976 * @return string |
|
1977 */ |
|
1978 function get_cancel_comment_reply_link( $link_text = '', $post = null ) { |
|
1979 if ( empty( $link_text ) ) { |
|
1980 $link_text = __( 'Click here to cancel reply.' ); |
|
1981 } |
|
1982 |
|
1983 $post = get_post( $post ); |
|
1984 $reply_to_id = $post ? _get_comment_reply_id( $post->ID ) : 0; |
|
1985 $link_style = 0 !== $reply_to_id ? '' : ' style="display:none;"'; |
|
1986 $link_url = esc_url( remove_query_arg( array( 'replytocom', 'unapproved', 'moderation-hash' ) ) ) . '#respond'; |
|
1987 |
|
1988 $cancel_comment_reply_link = sprintf( |
|
1989 '<a rel="nofollow" id="cancel-comment-reply-link" href="%1$s"%2$s>%3$s</a>', |
|
1990 $link_url, |
|
1991 $link_style, |
|
1992 $link_text |
|
1993 ); |
|
1994 |
|
1995 /** |
|
1996 * Filters the cancel comment reply link HTML. |
|
1997 * |
|
1998 * @since 2.7.0 |
|
1999 * |
|
2000 * @param string $cancel_comment_reply_link The HTML-formatted cancel comment reply link. |
|
2001 * @param string $link_url Cancel comment reply link URL. |
|
2002 * @param string $link_text Cancel comment reply link text. |
|
2003 */ |
|
2004 return apply_filters( 'cancel_comment_reply_link', $cancel_comment_reply_link, $link_url, $link_text ); |
|
2005 } |
|
2006 |
|
2007 /** |
|
2008 * Displays HTML content for cancel comment reply link. |
|
2009 * |
|
2010 * @since 2.7.0 |
|
2011 * |
|
2012 * @param string $link_text Optional. Text to display for cancel reply link. If empty, |
|
1895 * defaults to 'Click here to cancel reply'. Default empty. |
2013 * defaults to 'Click here to cancel reply'. Default empty. |
1896 * @return string |
2014 */ |
1897 */ |
2015 function cancel_comment_reply_link( $link_text = '' ) { |
1898 function get_cancel_comment_reply_link( $text = '' ) { |
2016 echo get_cancel_comment_reply_link( $link_text ); |
1899 if ( empty( $text ) ) { |
|
1900 $text = __( 'Click here to cancel reply.' ); |
|
1901 } |
|
1902 |
|
1903 $style = isset( $_GET['replytocom'] ) ? '' : ' style="display:none;"'; |
|
1904 $link = esc_html( remove_query_arg( array( 'replytocom', 'unapproved', 'moderation-hash' ) ) ) . '#respond'; |
|
1905 |
|
1906 $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>'; |
|
1907 |
|
1908 /** |
|
1909 * Filters the cancel comment reply link HTML. |
|
1910 * |
|
1911 * @since 2.7.0 |
|
1912 * |
|
1913 * @param string $formatted_link The HTML-formatted cancel comment reply link. |
|
1914 * @param string $link Cancel comment reply link URL. |
|
1915 * @param string $text Cancel comment reply link text. |
|
1916 */ |
|
1917 return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text ); |
|
1918 } |
|
1919 |
|
1920 /** |
|
1921 * Displays HTML content for cancel comment reply link. |
|
1922 * |
|
1923 * @since 2.7.0 |
|
1924 * |
|
1925 * @param string $text Optional. Text to display for cancel reply link. If empty, |
|
1926 * defaults to 'Click here to cancel reply'. Default empty. |
|
1927 */ |
|
1928 function cancel_comment_reply_link( $text = '' ) { |
|
1929 echo get_cancel_comment_reply_link( $text ); |
|
1930 } |
2017 } |
1931 |
2018 |
1932 /** |
2019 /** |
1933 * Retrieves hidden input HTML for replying to comments. |
2020 * Retrieves hidden input HTML for replying to comments. |
1934 * |
2021 * |
1935 * @since 3.0.0 |
2022 * @since 3.0.0 |
1936 * |
2023 * @since 6.2.0 Renamed `$post_id` to `$post` and added WP_Post support. |
1937 * @param int $post_id Optional. Post ID. Defaults to the current post ID. |
2024 * |
2025 * @param int|WP_Post|null $post Optional. The post the comment is being displayed for. |
|
2026 * Defaults to the current global post. |
|
1938 * @return string Hidden input HTML for replying to comments. |
2027 * @return string Hidden input HTML for replying to comments. |
1939 */ |
2028 */ |
1940 function get_comment_id_fields( $post_id = 0 ) { |
2029 function get_comment_id_fields( $post = null ) { |
1941 if ( empty( $post_id ) ) { |
2030 $post = get_post( $post ); |
1942 $post_id = get_the_ID(); |
2031 if ( ! $post ) { |
1943 } |
2032 return ''; |
1944 |
2033 } |
1945 $reply_to_id = isset( $_GET['replytocom'] ) ? (int) $_GET['replytocom'] : 0; |
2034 |
1946 $result = "<input type='hidden' name='comment_post_ID' value='$post_id' id='comment_post_ID' />\n"; |
2035 $post_id = $post->ID; |
1947 $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$reply_to_id' />\n"; |
2036 $reply_to_id = _get_comment_reply_id( $post_id ); |
2037 |
|
2038 $comment_id_fields = "<input type='hidden' name='comment_post_ID' value='$post_id' id='comment_post_ID' />\n"; |
|
2039 $comment_id_fields .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$reply_to_id' />\n"; |
|
1948 |
2040 |
1949 /** |
2041 /** |
1950 * Filters the returned comment ID fields. |
2042 * Filters the returned comment ID fields. |
1951 * |
2043 * |
1952 * @since 3.0.0 |
2044 * @since 3.0.0 |
1953 * |
2045 * |
1954 * @param string $result The HTML-formatted hidden ID field comment elements. |
2046 * @param string $comment_id_fields The HTML-formatted hidden ID field comment elements. |
1955 * @param int $post_id The post ID. |
2047 * @param int $post_id The post ID. |
1956 * @param int $reply_to_id The ID of the comment being replied to. |
2048 * @param int $reply_to_id The ID of the comment being replied to. |
1957 */ |
2049 */ |
1958 return apply_filters( 'comment_id_fields', $result, $post_id, $reply_to_id ); |
2050 return apply_filters( 'comment_id_fields', $comment_id_fields, $post_id, $reply_to_id ); |
1959 } |
2051 } |
1960 |
2052 |
1961 /** |
2053 /** |
1962 * Outputs hidden input HTML for replying to comments. |
2054 * Outputs hidden input HTML for replying to comments. |
1963 * |
2055 * |
1965 * and `comment_parent` values for threaded comments. |
2057 * and `comment_parent` values for threaded comments. |
1966 * |
2058 * |
1967 * This tag must be within the `<form>` section of the `comments.php` template. |
2059 * This tag must be within the `<form>` section of the `comments.php` template. |
1968 * |
2060 * |
1969 * @since 2.7.0 |
2061 * @since 2.7.0 |
2062 * @since 6.2.0 Renamed `$post_id` to `$post` and added WP_Post support. |
|
1970 * |
2063 * |
1971 * @see get_comment_id_fields() |
2064 * @see get_comment_id_fields() |
1972 * |
2065 * |
1973 * @param int $post_id Optional. Post ID. Defaults to the current post ID. |
2066 * @param int|WP_Post|null $post Optional. The post the comment is being displayed for. |
1974 */ |
2067 * Defaults to the current global post. |
1975 function comment_id_fields( $post_id = 0 ) { |
2068 */ |
1976 echo get_comment_id_fields( $post_id ); |
2069 function comment_id_fields( $post = null ) { |
2070 echo get_comment_id_fields( $post ); |
|
1977 } |
2071 } |
1978 |
2072 |
1979 /** |
2073 /** |
1980 * Displays text based on comment reply status. |
2074 * Displays text based on comment reply status. |
1981 * |
2075 * |
1983 * |
2077 * |
1984 * @internal The $comment global must be present to allow template tags access to the current |
2078 * @internal The $comment global must be present to allow template tags access to the current |
1985 * comment. See https://core.trac.wordpress.org/changeset/36512. |
2079 * comment. See https://core.trac.wordpress.org/changeset/36512. |
1986 * |
2080 * |
1987 * @since 2.7.0 |
2081 * @since 2.7.0 |
2082 * @since 6.2.0 Added the `$post` parameter. |
|
1988 * |
2083 * |
1989 * @global WP_Comment $comment Global comment object. |
2084 * @global WP_Comment $comment Global comment object. |
1990 * |
2085 * |
1991 * @param string|false $no_reply_text Optional. Text to display when not replying to a comment. |
2086 * @param string|false $no_reply_text Optional. Text to display when not replying to a comment. |
1992 * Default false. |
2087 * Default false. |
1993 * @param string|false $reply_text Optional. Text to display when replying to a comment. |
2088 * @param string|false $reply_text Optional. Text to display when replying to a comment. |
1994 * Default false. Accepts "%s" for the author of the comment |
2089 * Default false. Accepts "%s" for the author of the comment |
1995 * being replied to. |
2090 * being replied to. |
1996 * @param bool $link_to_parent Optional. Boolean to control making the author's name a link |
2091 * @param bool $link_to_parent Optional. Boolean to control making the author's name a link |
1997 * to their comment. Default true. |
2092 * to their comment. Default true. |
1998 */ |
2093 * @param int|WP_Post|null $post Optional. The post that the comment form is being displayed for. |
1999 function comment_form_title( $no_reply_text = false, $reply_text = false, $link_to_parent = true ) { |
2094 * Defaults to the current global post. |
2095 */ |
|
2096 function comment_form_title( $no_reply_text = false, $reply_text = false, $link_to_parent = true, $post = null ) { |
|
2000 global $comment; |
2097 global $comment; |
2001 |
2098 |
2002 if ( false === $no_reply_text ) { |
2099 if ( false === $no_reply_text ) { |
2003 $no_reply_text = __( 'Leave a Reply' ); |
2100 $no_reply_text = __( 'Leave a Reply' ); |
2004 } |
2101 } |
2006 if ( false === $reply_text ) { |
2103 if ( false === $reply_text ) { |
2007 /* translators: %s: Author of the comment being replied to. */ |
2104 /* translators: %s: Author of the comment being replied to. */ |
2008 $reply_text = __( 'Leave a Reply to %s' ); |
2105 $reply_text = __( 'Leave a Reply to %s' ); |
2009 } |
2106 } |
2010 |
2107 |
2011 $reply_to_id = isset( $_GET['replytocom'] ) ? (int) $_GET['replytocom'] : 0; |
2108 $post = get_post( $post ); |
2012 |
2109 if ( ! $post ) { |
2013 if ( 0 == $reply_to_id ) { |
|
2014 echo $no_reply_text; |
2110 echo $no_reply_text; |
2111 return; |
|
2112 } |
|
2113 |
|
2114 $reply_to_id = _get_comment_reply_id( $post->ID ); |
|
2115 |
|
2116 if ( 0 === $reply_to_id ) { |
|
2117 echo $no_reply_text; |
|
2118 return; |
|
2119 } |
|
2120 |
|
2121 // Sets the global so that template tags can be used in the comment form. |
|
2122 $comment = get_comment( $reply_to_id ); |
|
2123 |
|
2124 if ( $link_to_parent ) { |
|
2125 $comment_author = sprintf( |
|
2126 '<a href="#comment-%1$s">%2$s</a>', |
|
2127 get_comment_ID(), |
|
2128 get_comment_author( $reply_to_id ) |
|
2129 ); |
|
2015 } else { |
2130 } else { |
2016 // Sets the global so that template tags can be used in the comment form. |
2131 $comment_author = get_comment_author( $reply_to_id ); |
2017 $comment = get_comment( $reply_to_id ); |
2132 } |
2018 |
2133 |
2019 if ( $link_to_parent ) { |
2134 printf( $reply_text, $comment_author ); |
2020 $author = '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author( $comment ) . '</a>'; |
2135 } |
2021 } else { |
2136 |
2022 $author = get_comment_author( $comment ); |
2137 /** |
2023 } |
2138 * Gets the comment's reply to ID from the $_GET['replytocom']. |
2024 |
2139 * |
2025 printf( $reply_text, $author ); |
2140 * @since 6.2.0 |
2026 } |
2141 * |
2142 * @access private |
|
2143 * |
|
2144 * @param int|WP_Post $post The post the comment is being displayed for. |
|
2145 * Defaults to the current global post. |
|
2146 * @return int Comment's reply to ID. |
|
2147 */ |
|
2148 function _get_comment_reply_id( $post = null ) { |
|
2149 $post = get_post( $post ); |
|
2150 |
|
2151 if ( ! $post || ! isset( $_GET['replytocom'] ) || ! is_numeric( $_GET['replytocom'] ) ) { |
|
2152 return 0; |
|
2153 } |
|
2154 |
|
2155 $reply_to_id = (int) $_GET['replytocom']; |
|
2156 |
|
2157 /* |
|
2158 * Validate the comment. |
|
2159 * Bail out if it does not exist, is not approved, or its |
|
2160 * `comment_post_ID` does not match the given post ID. |
|
2161 */ |
|
2162 $comment = get_comment( $reply_to_id ); |
|
2163 |
|
2164 if ( |
|
2165 ! $comment instanceof WP_Comment || |
|
2166 0 === (int) $comment->comment_approved || |
|
2167 $post->ID !== (int) $comment->comment_post_ID |
|
2168 ) { |
|
2169 return 0; |
|
2170 } |
|
2171 |
|
2172 return $reply_to_id; |
|
2027 } |
2173 } |
2028 |
2174 |
2029 /** |
2175 /** |
2030 * Displays a list of comments. |
2176 * Displays a list of comments. |
2031 * |
2177 * |
2032 * Used in the comments.php template to list comments for a particular post. |
2178 * Used in the comments.php template to list comments for a particular post. |
2033 * |
2179 * |
2034 * @since 2.7.0 |
2180 * @since 2.7.0 |
2035 * |
2181 * |
2036 * @see WP_Query->comments |
2182 * @see WP_Query::$comments |
2037 * |
2183 * |
2038 * @global WP_Query $wp_query WordPress Query object. |
2184 * @global WP_Query $wp_query WordPress Query object. |
2039 * @global int $comment_alt |
2185 * @global int $comment_alt |
2040 * @global int $comment_depth |
2186 * @global int $comment_depth |
2041 * @global int $comment_thread_alt |
2187 * @global int $comment_thread_alt |
2062 * @type string $format How to format the comments list. Accepts 'html5', 'xhtml'. |
2208 * @type string $format How to format the comments list. Accepts 'html5', 'xhtml'. |
2063 * Default 'html5' if the theme supports it. |
2209 * Default 'html5' if the theme supports it. |
2064 * @type bool $short_ping Whether to output short pings. Default false. |
2210 * @type bool $short_ping Whether to output short pings. Default false. |
2065 * @type bool $echo Whether to echo the output or return it. Default true. |
2211 * @type bool $echo Whether to echo the output or return it. Default true. |
2066 * } |
2212 * } |
2067 * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. |
2213 * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Default null. |
2068 * @return void|string Void if 'echo' argument is true, or no comments to list. |
2214 * @return void|string Void if 'echo' argument is true, or no comments to list. |
2069 * Otherwise, HTML list of comments. |
2215 * Otherwise, HTML list of comments. |
2070 */ |
2216 */ |
2071 function wp_list_comments( $args = array(), $comments = null ) { |
2217 function wp_list_comments( $args = array(), $comments = null ) { |
2072 global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop; |
2218 global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop; |
2239 |
2385 |
2240 if ( null === $parsed_args['reverse_top_level'] ) { |
2386 if ( null === $parsed_args['reverse_top_level'] ) { |
2241 $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) ); |
2387 $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) ); |
2242 } |
2388 } |
2243 |
2389 |
2244 wp_queue_comments_for_comment_meta_lazyload( $_comments ); |
|
2245 |
|
2246 if ( empty( $parsed_args['walker'] ) ) { |
2390 if ( empty( $parsed_args['walker'] ) ) { |
2247 $walker = new Walker_Comment; |
2391 $walker = new Walker_Comment(); |
2248 } else { |
2392 } else { |
2249 $walker = $parsed_args['walker']; |
2393 $walker = $parsed_args['walker']; |
2250 } |
2394 } |
2251 |
2395 |
2252 $output = $walker->paged_walk( $_comments, $parsed_args['max_depth'], $parsed_args['page'], $parsed_args['per_page'], $parsed_args ); |
2396 $output = $walker->paged_walk( $_comments, $parsed_args['max_depth'], $parsed_args['page'], $parsed_args['per_page'], $parsed_args ); |
2292 * @type string $url Comment author URL field HTML. |
2436 * @type string $url Comment author URL field HTML. |
2293 * @type string $cookies Comment cookie opt-in field HTML. |
2437 * @type string $cookies Comment cookie opt-in field HTML. |
2294 * } |
2438 * } |
2295 * @type string $comment_field The comment textarea field HTML. |
2439 * @type string $comment_field The comment textarea field HTML. |
2296 * @type string $must_log_in HTML element for a 'must be logged in to comment' message. |
2440 * @type string $must_log_in HTML element for a 'must be logged in to comment' message. |
2297 * @type string $logged_in_as HTML element for a 'logged in as [user]' message. |
2441 * @type string $logged_in_as The HTML for the 'logged in as [user]' message, the Edit profile link, |
2442 * and the Log out link. |
|
2298 * @type string $comment_notes_before HTML element for a message displayed before the comment fields |
2443 * @type string $comment_notes_before HTML element for a message displayed before the comment fields |
2299 * if the user is not logged in. |
2444 * if the user is not logged in. |
2300 * Default 'Your email address will not be published.'. |
2445 * Default 'Your email address will not be published.'. |
2301 * @type string $comment_notes_after HTML element for a message displayed after the textarea field. |
2446 * @type string $comment_notes_after HTML element for a message displayed after the textarea field. |
2302 * @type string $action The comment form element action attribute. Default '/wp-comments-post.php'. |
2447 * @type string $action The comment form element action attribute. Default '/wp-comments-post.php'. |
2322 * @type string $submit_field HTML format for the markup surrounding the Submit button and comment hidden |
2467 * @type string $submit_field HTML format for the markup surrounding the Submit button and comment hidden |
2323 * fields. Default: '<p class="form-submit">%1$s %2$s</p>', where %1$s is the |
2468 * fields. Default: '<p class="form-submit">%1$s %2$s</p>', where %1$s is the |
2324 * submit button markup and %2$s is the comment hidden fields. |
2469 * submit button markup and %2$s is the comment hidden fields. |
2325 * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'. |
2470 * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'. |
2326 * } |
2471 * } |
2327 * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post. |
2472 * @param int|WP_Post $post Optional. Post ID or WP_Post object to generate the form for. Default current post. |
2328 */ |
2473 */ |
2329 function comment_form( $args = array(), $post_id = null ) { |
2474 function comment_form( $args = array(), $post = null ) { |
2330 if ( null === $post_id ) { |
2475 $post = get_post( $post ); |
2331 $post_id = get_the_ID(); |
2476 |
2332 } |
2477 // Exit the function if the post is invalid or comments are closed. |
2333 |
2478 if ( ! $post || ! comments_open( $post ) ) { |
2334 // Exit the function when comments for the post are closed. |
|
2335 if ( ! comments_open( $post_id ) ) { |
|
2336 /** |
2479 /** |
2337 * Fires after the comment form if comments are closed. |
2480 * Fires after the comment form if comments are closed. |
2481 * |
|
2482 * For backward compatibility, this action also fires if comment_form() |
|
2483 * is called with an invalid post object or ID. |
|
2338 * |
2484 * |
2339 * @since 3.0.0 |
2485 * @since 3.0.0 |
2340 */ |
2486 */ |
2341 do_action( 'comment_form_comments_closed' ); |
2487 do_action( 'comment_form_comments_closed' ); |
2342 |
2488 |
2343 return; |
2489 return; |
2344 } |
2490 } |
2345 |
2491 |
2492 $post_id = $post->ID; |
|
2346 $commenter = wp_get_current_commenter(); |
2493 $commenter = wp_get_current_commenter(); |
2347 $user = wp_get_current_user(); |
2494 $user = wp_get_current_user(); |
2348 $user_identity = $user->exists() ? $user->display_name : ''; |
2495 $user_identity = $user->exists() ? $user->display_name : ''; |
2349 |
2496 |
2350 $args = wp_parse_args( $args ); |
2497 $args = wp_parse_args( $args ); |
2357 |
2504 |
2358 // Define attributes in HTML5 or XHTML syntax. |
2505 // Define attributes in HTML5 or XHTML syntax. |
2359 $required_attribute = ( $html5 ? ' required' : ' required="required"' ); |
2506 $required_attribute = ( $html5 ? ' required' : ' required="required"' ); |
2360 $checked_attribute = ( $html5 ? ' checked' : ' checked="checked"' ); |
2507 $checked_attribute = ( $html5 ? ' checked' : ' checked="checked"' ); |
2361 |
2508 |
2362 // Identify required fields visually. |
2509 // Identify required fields visually and create a message about the indicator. |
2363 $required_indicator = ' <span class="required" aria-hidden="true">*</span>'; |
2510 $required_indicator = ' ' . wp_required_field_indicator(); |
2511 $required_text = ' ' . wp_required_field_message(); |
|
2364 |
2512 |
2365 $fields = array( |
2513 $fields = array( |
2366 'author' => sprintf( |
2514 'author' => sprintf( |
2367 '<p class="comment-form-author">%s %s</p>', |
2515 '<p class="comment-form-author">%s %s</p>', |
2368 sprintf( |
2516 sprintf( |
2369 '<label for="author">%s%s</label>', |
2517 '<label for="author">%s%s</label>', |
2370 __( 'Name' ), |
2518 __( 'Name' ), |
2371 ( $req ? $required_indicator : '' ) |
2519 ( $req ? $required_indicator : '' ) |
2372 ), |
2520 ), |
2373 sprintf( |
2521 sprintf( |
2374 '<input id="author" name="author" type="text" value="%s" size="30" maxlength="245"%s />', |
2522 '<input id="author" name="author" type="text" value="%s" size="30" maxlength="245" autocomplete="name"%s />', |
2375 esc_attr( $commenter['comment_author'] ), |
2523 esc_attr( $commenter['comment_author'] ), |
2376 ( $req ? $required_attribute : '' ) |
2524 ( $req ? $required_attribute : '' ) |
2377 ) |
2525 ) |
2378 ), |
2526 ), |
2379 'email' => sprintf( |
2527 'email' => sprintf( |
2382 '<label for="email">%s%s</label>', |
2530 '<label for="email">%s%s</label>', |
2383 __( 'Email' ), |
2531 __( 'Email' ), |
2384 ( $req ? $required_indicator : '' ) |
2532 ( $req ? $required_indicator : '' ) |
2385 ), |
2533 ), |
2386 sprintf( |
2534 sprintf( |
2387 '<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes"%s />', |
2535 '<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes" autocomplete="email"%s />', |
2388 ( $html5 ? 'type="email"' : 'type="text"' ), |
2536 ( $html5 ? 'type="email"' : 'type="text"' ), |
2389 esc_attr( $commenter['comment_author_email'] ), |
2537 esc_attr( $commenter['comment_author_email'] ), |
2390 ( $req ? $required_attribute : '' ) |
2538 ( $req ? $required_attribute : '' ) |
2391 ) |
2539 ) |
2392 ), |
2540 ), |
2395 sprintf( |
2543 sprintf( |
2396 '<label for="url">%s</label>', |
2544 '<label for="url">%s</label>', |
2397 __( 'Website' ) |
2545 __( 'Website' ) |
2398 ), |
2546 ), |
2399 sprintf( |
2547 sprintf( |
2400 '<input id="url" name="url" %s value="%s" size="30" maxlength="200" />', |
2548 '<input id="url" name="url" %s value="%s" size="30" maxlength="200" autocomplete="url" />', |
2401 ( $html5 ? 'type="url"' : 'type="text"' ), |
2549 ( $html5 ? 'type="url"' : 'type="text"' ), |
2402 esc_attr( $commenter['comment_author_url'] ) |
2550 esc_attr( $commenter['comment_author_url'] ) |
2403 ) |
2551 ) |
2404 ), |
2552 ), |
2405 ); |
2553 ); |
2422 // Ensure that the passed fields include cookies consent. |
2570 // Ensure that the passed fields include cookies consent. |
2423 if ( isset( $args['fields'] ) && ! isset( $args['fields']['cookies'] ) ) { |
2571 if ( isset( $args['fields'] ) && ! isset( $args['fields']['cookies'] ) ) { |
2424 $args['fields']['cookies'] = $fields['cookies']; |
2572 $args['fields']['cookies'] = $fields['cookies']; |
2425 } |
2573 } |
2426 } |
2574 } |
2427 |
|
2428 $required_text = sprintf( |
|
2429 /* translators: %s: Asterisk symbol (*). */ |
|
2430 ' <span class="required-field-message" aria-hidden="true">' . __( 'Required fields are marked %s' ) . '</span>', |
|
2431 trim( $required_indicator ) |
|
2432 ); |
|
2433 |
2575 |
2434 /** |
2576 /** |
2435 * Filters the default comment form fields. |
2577 * Filters the default comment form fields. |
2436 * |
2578 * |
2437 * @since 3.0.0 |
2579 * @since 3.0.0 |
2461 ) |
2603 ) |
2462 ), |
2604 ), |
2463 'logged_in_as' => sprintf( |
2605 'logged_in_as' => sprintf( |
2464 '<p class="logged-in-as">%s%s</p>', |
2606 '<p class="logged-in-as">%s%s</p>', |
2465 sprintf( |
2607 sprintf( |
2466 /* translators: 1: Edit user link, 2: Accessibility text, 3: User name, 4: Logout URL. */ |
2608 /* translators: 1: User name, 2: Edit user link, 3: Logout URL. */ |
2467 __( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ), |
2609 __( 'Logged in as %1$s. <a href="%2$s">Edit your profile</a>. <a href="%3$s">Log out?</a>' ), |
2610 $user_identity, |
|
2468 get_edit_user_link(), |
2611 get_edit_user_link(), |
2469 /* translators: %s: User name. */ |
|
2470 esc_attr( sprintf( __( 'Logged in as %s. Edit your profile.' ), $user_identity ) ), |
|
2471 $user_identity, |
|
2472 /** This filter is documented in wp-includes/link-template.php */ |
2612 /** This filter is documented in wp-includes/link-template.php */ |
2473 wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) ) |
2613 wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) ) |
2474 ), |
2614 ), |
2475 $required_text |
2615 $required_text |
2476 ), |
2616 ), |
2517 |
2657 |
2518 // Ensure that the filtered arguments contain all required default values. |
2658 // Ensure that the filtered arguments contain all required default values. |
2519 $args = array_merge( $defaults, $args ); |
2659 $args = array_merge( $defaults, $args ); |
2520 |
2660 |
2521 // Remove `aria-describedby` from the email field if there's no associated description. |
2661 // Remove `aria-describedby` from the email field if there's no associated description. |
2522 if ( isset( $args['fields']['email'] ) && false === strpos( $args['comment_notes_before'], 'id="email-notes"' ) ) { |
2662 if ( isset( $args['fields']['email'] ) && ! str_contains( $args['comment_notes_before'], 'id="email-notes"' ) ) { |
2523 $args['fields']['email'] = str_replace( |
2663 $args['fields']['email'] = str_replace( |
2524 ' aria-describedby="email-notes"', |
2664 ' aria-describedby="email-notes"', |
2525 '', |
2665 '', |
2526 $args['fields']['email'] |
2666 $args['fields']['email'] |
2527 ); |
2667 ); |
2536 ?> |
2676 ?> |
2537 <div id="respond" class="<?php echo esc_attr( $args['class_container'] ); ?>"> |
2677 <div id="respond" class="<?php echo esc_attr( $args['class_container'] ); ?>"> |
2538 <?php |
2678 <?php |
2539 echo $args['title_reply_before']; |
2679 echo $args['title_reply_before']; |
2540 |
2680 |
2541 comment_form_title( $args['title_reply'], $args['title_reply_to'] ); |
2681 comment_form_title( $args['title_reply'], $args['title_reply_to'], true, $post_id ); |
2542 |
2682 |
2543 if ( get_option( 'thread_comments' ) ) { |
2683 if ( get_option( 'thread_comments' ) ) { |
2544 echo $args['cancel_reply_before']; |
2684 echo $args['cancel_reply_before']; |
2545 |
2685 |
2546 cancel_comment_reply_link( $args['cancel_reply_link'] ); |
2686 cancel_comment_reply_link( $args['cancel_reply_link'] ); |
2582 /** |
2722 /** |
2583 * Filters the 'logged in' message for the comment form for display. |
2723 * Filters the 'logged in' message for the comment form for display. |
2584 * |
2724 * |
2585 * @since 3.0.0 |
2725 * @since 3.0.0 |
2586 * |
2726 * |
2587 * @param string $args_logged_in The logged-in-as HTML-formatted message. |
2727 * @param string $args_logged_in The HTML for the 'logged in as [user]' message, |
2728 * the Edit profile link, and the Log out link. |
|
2588 * @param array $commenter An array containing the comment author's |
2729 * @param array $commenter An array containing the comment author's |
2589 * username, email, and URL. |
2730 * username, email, and URL. |
2590 * @param string $user_identity If the commenter is a registered user, |
2731 * @param string $user_identity If the commenter is a registered user, |
2591 * the display name, blank otherwise. |
2732 * the display name, blank otherwise. |
2592 */ |
2733 */ |