| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Comment template functions 4 * 5 * These functions are meant to live inside of the WordPress loop. 6 * 7 * @package WordPress 8 * @subpackage Template 9 */ 10 11 /** 12 * Retrieves the author of the current comment. 13 * 14 * If the comment has an empty comment_author field, then 'Anonymous' person is 15 * assumed. 16 * 17 * @since 1.5.0 18 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 19 * 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. 22 * @return string The comment author 23 */ 24 function get_comment_author( $comment_id = 0 ) { 25 $comment = get_comment( $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 = '0'; 33 } 34 35 if ( empty( $comment->comment_author ) ) { 36 $user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false; 37 if ( $user ) { 38 $comment_author = $user->display_name; 39 } else { 40 $comment_author = __( 'Anonymous' ); 41 } 42 } else { 43 $comment_author = $comment->comment_author; 44 } 45 46 /** 47 * Filters the returned comment author name. 48 * 49 * @since 1.5.0 50 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. 51 * 52 * @param string $comment_author The comment author's username. 53 * @param string $comment_id The comment ID as a numeric string. 54 * @param WP_Comment $comment The comment object. 55 */ 56 return apply_filters( 'get_comment_author', $comment_author, $comment_id, $comment ); 57 } 58 59 /** 60 * Displays the author of the current comment. 61 * 62 * @since 0.71 63 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 64 * 65 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author. 66 * Default current comment. 67 */ 68 function comment_author( $comment_id = 0 ) { 69 $comment = get_comment( $comment_id ); 70 71 $comment_author = get_comment_author( $comment ); 72 73 /** 74 * Filters the comment author's name for display. 75 * 76 * @since 1.2.0 77 * @since 4.1.0 The `$comment_id` parameter was added. 78 * 79 * @param string $comment_author The comment author's username. 80 * @param string $comment_id The comment ID as a numeric string. 81 */ 82 echo apply_filters( 'comment_author', $comment_author, $comment->comment_ID ); 83 } 84 85 /** 86 * Retrieves the email of the author of the current comment. 87 * 88 * @since 1.5.0 89 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 90 * 91 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's email. 92 * Default current comment. 93 * @return string The current comment author's email 94 */ 95 function get_comment_author_email( $comment_id = 0 ) { 96 $comment = get_comment( $comment_id ); 97 98 /** 99 * Filters the comment author's returned email address. 100 * 101 * @since 1.5.0 102 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. 103 * 104 * @param string $comment_author_email The comment author's email address. 105 * @param string $comment_id The comment ID as a numeric string. 106 * @param WP_Comment $comment The comment object. 107 */ 108 return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment ); 109 } 110 111 /** 112 * Displays the email of the author of the current global $comment. 113 * 114 * Care should be taken to protect the email address and assure that email 115 * harvesters do not capture your commenter's email address. Most assume that 116 * their email address will not appear in raw form on the site. Doing so will 117 * enable anyone, including those that people don't want to get the email 118 * address and use it for their own means good and bad. 119 * 120 * @since 0.71 121 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 122 * 123 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's email. 124 * Default current comment. 125 */ 126 function comment_author_email( $comment_id = 0 ) { 127 $comment = get_comment( $comment_id ); 128 129 $comment_author_email = get_comment_author_email( $comment ); 130 131 /** 132 * Filters the comment author's email for display. 133 * 134 * @since 1.2.0 135 * @since 4.1.0 The `$comment_id` parameter was added. 136 * 137 * @param string $comment_author_email The comment author's email address. 138 * @param string $comment_id The comment ID as a numeric string. 139 */ 140 echo apply_filters( 'author_email', $comment_author_email, $comment->comment_ID ); 141 } 142 143 /** 144 * Displays the HTML email link to the author of the current comment. 145 * 146 * Care should be taken to protect the email address and assure that email 147 * harvesters do not capture your commenter's email address. Most assume that 148 * their email address will not appear in raw form on the site. Doing so will 149 * enable anyone, including those that people don't want to get the email 150 * address and use it for their own means good and bad. 151 * 152 * @since 0.71 153 * @since 4.6.0 Added the `$comment` parameter. 154 * 155 * @param string $link_text Optional. Text to display instead of the comment author's email address. 156 * Default empty. 157 * @param string $before Optional. Text or HTML to display before the email link. Default empty. 158 * @param string $after Optional. Text or HTML to display after the email link. Default empty. 159 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment. 160 */ 161 function comment_author_email_link( $link_text = '', $before = '', $after = '', $comment = null ) { 162 $link = get_comment_author_email_link( $link_text, $before, $after, $comment ); 163 if ( $link ) { 164 echo $link; 165 } 166 } 167 168 /** 169 * Returns the HTML email link to the author of the current comment. 170 * 171 * Care should be taken to protect the email address and assure that email 172 * harvesters do not capture your commenter's email address. Most assume that 173 * their email address will not appear in raw form on the site. Doing so will 174 * enable anyone, including those that people don't want to get the email 175 * address and use it for their own means good and bad. 176 * 177 * @since 2.7.0 178 * @since 4.6.0 Added the `$comment` parameter. 179 * 180 * @param string $link_text Optional. Text to display instead of the comment author's email address. 181 * Default empty. 182 * @param string $before Optional. Text or HTML to display before the email link. Default empty. 183 * @param string $after Optional. Text or HTML to display after the email link. Default empty. 184 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment. 185 * @return string HTML markup for the comment author email link. By default, the email address is obfuscated 186 * via the {@see 'comment_email'} filter with antispambot(). 187 */ 188 function get_comment_author_email_link( $link_text = '', $before = '', $after = '', $comment = null ) { 189 $comment = get_comment( $comment ); 190 191 /** 192 * Filters the comment author's email for display. 193 * 194 * Care should be taken to protect the email address and assure that email 195 * harvesters do not capture your commenter's email address. 196 * 197 * @since 1.2.0 198 * @since 4.1.0 The `$comment` parameter was added. 199 * 200 * @param string $comment_author_email The comment author's email address. 201 * @param WP_Comment $comment The comment object. 202 */ 203 $comment_author_email = apply_filters( 'comment_email', $comment->comment_author_email, $comment ); 204 205 if ( ( ! empty( $comment_author_email ) ) && ( '@' !== $comment_author_email ) ) { 206 $display = ( '' !== $link_text ) ? $link_text : $comment_author_email; 207 208 $comment_author_email_link = $before . sprintf( 209 '<a href="%1$s">%2$s</a>', 210 esc_url( 'mailto:' . $comment_author_email ), 211 esc_html( $display ) 212 ) . $after; 213 214 return $comment_author_email_link; 215 } else { 216 return ''; 217 } 218 } 219 220 /** 221 * Retrieves the HTML link to the URL of the author of the current comment. 222 * 223 * Both get_comment_author_url() and get_comment_author() rely on get_comment(), 224 * which falls back to the global comment variable if the $comment_id argument is empty. 225 * 226 * @since 1.5.0 227 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 228 * 229 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's link. 230 * Default current comment. 231 * @return string The comment author name or HTML link for author's URL. 232 */ 233 function get_comment_author_link( $comment_id = 0 ) { 234 $comment = get_comment( $comment_id ); 235 236 if ( ! empty( $comment->comment_ID ) ) { 237 $comment_id = $comment->comment_ID; 238 } elseif ( is_scalar( $comment_id ) ) { 239 $comment_id = (string) $comment_id; 240 } else { 241 $comment_id = '0'; 242 } 243 244 $comment_author_url = get_comment_author_url( $comment ); 245 $comment_author = get_comment_author( $comment ); 246 247 if ( empty( $comment_author_url ) || 'http://' === $comment_author_url ) { 248 $comment_author_link = $comment_author; 249 } else { 250 $rel_parts = array( 'ugc' ); 251 if ( ! wp_is_internal_link( $comment_author_url ) ) { 252 $rel_parts = array_merge( 253 $rel_parts, 254 array( 'external', 'nofollow' ) 255 ); 256 } 257 258 /** 259 * Filters the rel attributes of the comment author's link. 260 * 261 * @since 6.2.0 262 * 263 * @param string[] $rel_parts An array of strings representing the rel tags 264 * which will be joined into the anchor's rel attribute. 265 * @param WP_Comment $comment The comment object. 266 */ 267 $rel_parts = apply_filters( 'comment_author_link_rel', $rel_parts, $comment ); 268 269 $rel = implode( ' ', $rel_parts ); 270 $rel = esc_attr( $rel ); 271 // Empty space before 'rel' is necessary for later sprintf(). 272 $rel = ! empty( $rel ) ? sprintf( ' rel="%s"', $rel ) : ''; 273 274 $comment_author_link = sprintf( 275 '<a href="%1$s" class="url"%2$s>%3$s</a>', 276 $comment_author_url, 277 $rel, 278 $comment_author 279 ); 280 } 281 282 /** 283 * Filters the comment author's link for display. 284 * 285 * @since 1.5.0 286 * @since 4.1.0 The `$comment_author` and `$comment_id` parameters were added. 287 * 288 * @param string $comment_author_link The HTML-formatted comment author link. 289 * Empty for an invalid URL. 290 * @param string $comment_author The comment author's username. 291 * @param string $comment_id The comment ID as a numeric string. 292 */ 293 return apply_filters( 'get_comment_author_link', $comment_author_link, $comment_author, $comment_id ); 294 } 295 296 /** 297 * Displays the HTML link to the URL of the author of the current comment. 298 * 299 * @since 0.71 300 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 301 * 302 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's link. 303 * Default current comment. 304 */ 305 function comment_author_link( $comment_id = 0 ) { 306 echo get_comment_author_link( $comment_id ); 307 } 308 309 /** 310 * Retrieves the IP address of the author of the current comment. 311 * 312 * @since 1.5.0 313 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 314 * 315 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's IP address. 316 * Default current comment. 317 * @return string Comment author's IP address, or an empty string if it's not available. 318 */ 319 function get_comment_author_IP( $comment_id = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 320 $comment = get_comment( $comment_id ); 321 322 /** 323 * Filters the comment author's returned IP address. 324 * 325 * @since 1.5.0 326 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. 327 * 328 * @param string $comment_author_ip The comment author's IP address, or an empty string if it's not available. 329 * @param string $comment_id The comment ID as a numeric string. 330 * @param WP_Comment $comment The comment object. 331 */ 332 return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase 333 } 334 335 /** 336 * Displays the IP address of the author of the current comment. 337 * 338 * @since 0.71 339 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 340 * 341 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's IP address. 342 * Default current comment. 343 */ 344 function comment_author_IP( $comment_id = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 345 echo esc_html( get_comment_author_IP( $comment_id ) ); 346 } 347 348 /** 349 * Retrieves the URL of the author of the current comment, not linked. 350 * 351 * @since 1.5.0 352 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 353 * 354 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to get the author's URL. 355 * Default current comment. 356 * @return string Comment author URL, if provided, an empty string otherwise. 357 */ 358 function get_comment_author_url( $comment_id = 0 ) { 359 $comment = get_comment( $comment_id ); 360 361 $comment_author_url = ''; 362 $comment_id = 0; 363 364 if ( ! empty( $comment ) ) { 365 $comment_author_url = ( 'http://' === $comment->comment_author_url ) ? '' : $comment->comment_author_url; 366 $comment_author_url = esc_url( $comment_author_url, array( 'http', 'https' ) ); 367 368 $comment_id = $comment->comment_ID; 369 } 370 371 /** 372 * Filters the comment author's URL. 373 * 374 * @since 1.5.0 375 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. 376 * 377 * @param string $comment_author_url The comment author's URL, or an empty string. 378 * @param string|int $comment_id The comment ID as a numeric string, or 0 if not found. 379 * @param WP_Comment|null $comment The comment object, or null if not found. 380 */ 381 return apply_filters( 'get_comment_author_url', $comment_author_url, $comment_id, $comment ); 382 } 383 384 /** 385 * Displays the URL of the author of the current comment, not linked. 386 * 387 * @since 0.71 388 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 389 * 390 * @param int|WP_Comment $comment_id Optional. WP_Comment or the ID of the comment for which to print the author's URL. 391 * Default current comment. 392 */ 393 function comment_author_url( $comment_id = 0 ) { 394 $comment = get_comment( $comment_id ); 395 396 $comment_author_url = get_comment_author_url( $comment ); 397 398 /** 399 * Filters the comment author's URL for display. 400 * 401 * @since 1.2.0 402 * @since 4.1.0 The `$comment_id` parameter was added. 403 * 404 * @param string $comment_author_url The comment author's URL. 405 * @param string $comment_id The comment ID as a numeric string. 406 */ 407 echo apply_filters( 'comment_url', $comment_author_url, $comment->comment_ID ); 408 } 409 410 /** 411 * Retrieves the HTML link of the URL of the author of the current comment. 412 * 413 * $link_text parameter is only used if the URL does not exist for the comment 414 * author. If the URL does exist then the URL will be used and the $link_text 415 * will be ignored. 416 * 417 * Encapsulate the HTML link between the $before and $after. So it will appear 418 * in the order of $before, link, and finally $after. 419 * 420 * @since 1.5.0 421 * @since 4.6.0 Added the `$comment` parameter. 422 * 423 * @param string $link_text Optional. The text to display instead of the comment 424 * author's email address. Default empty. 425 * @param string $before Optional. The text or HTML to display before the email link. 426 * Default empty. 427 * @param string $after Optional. The text or HTML to display after the email link. 428 * Default empty. 429 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. 430 * Default is the current comment. 431 * @return string The HTML link between the $before and $after parameters. 432 */ 433 function get_comment_author_url_link( $link_text = '', $before = '', $after = '', $comment = 0 ) { 434 $comment_author_url = get_comment_author_url( $comment ); 435 436 $display = ( '' !== $link_text ) ? $link_text : $comment_author_url; 437 $display = str_replace( 'http://www.', '', $display ); 438 $display = str_replace( 'http://', '', $display ); 439 440 if ( str_ends_with( $display, '/' ) ) { 441 $display = substr( $display, 0, -1 ); 442 } 443 444 $comment_author_url_link = $before . sprintf( 445 '<a href="%1$s" rel="external">%2$s</a>', 446 $comment_author_url, 447 $display 448 ) . $after; 449 450 /** 451 * Filters the comment author's returned URL link. 452 * 453 * @since 1.5.0 454 * 455 * @param string $comment_author_url_link The HTML-formatted comment author URL link. 456 */ 457 return apply_filters( 'get_comment_author_url_link', $comment_author_url_link ); 458 } 459 460 /** 461 * Displays the HTML link of the URL of the author of the current comment. 462 * 463 * @since 0.71 464 * @since 4.6.0 Added the `$comment` parameter. 465 * 466 * @param string $link_text Optional. Text to display instead of the comment author's 467 * email address. Default empty. 468 * @param string $before Optional. Text or HTML to display before the email link. 469 * Default empty. 470 * @param string $after Optional. Text or HTML to display after the email link. 471 * Default empty. 472 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. 473 * Default is the current comment. 474 */ 475 function comment_author_url_link( $link_text = '', $before = '', $after = '', $comment = 0 ) { 476 echo get_comment_author_url_link( $link_text, $before, $after, $comment ); 477 } 478 479 /** 480 * Generates semantic classes for each comment element. 481 * 482 * @since 2.7.0 483 * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. 484 * 485 * @param string|string[] $css_class Optional. One or more classes to add to the class list. 486 * Default empty. 487 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default current comment. 488 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. 489 * @param bool $display Optional. Whether to print or return the output. 490 * Default true. 491 * @return string|void Comment classes if `$display` is false, nothing otherwise. 492 * @phpstan-return ( $display is true ? void : string ) 493 */ 494 function comment_class( $css_class = '', $comment = null, $post = null, $display = true ) { 495 // Separates classes with a single space, collates classes for comment DIV. 496 $css_class = 'class="' . implode( ' ', get_comment_class( $css_class, $comment, $post ) ) . '"'; 497 498 if ( $display ) { 499 echo $css_class; 500 } else { 501 return $css_class; 502 } 503 } 504 505 /** 506 * Returns the classes for the comment div as an array. 507 * 508 * @since 2.7.0 509 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 510 * 511 * @global int $comment_alt 512 * @global int $comment_depth 513 * @global int $comment_thread_alt 514 * 515 * @param string|string[] $css_class Optional. One or more classes to add to the class list. 516 * Default empty. 517 * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object. Default current comment. 518 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. 519 * @return string[] An array of classes. 520 */ 521 function get_comment_class( $css_class = '', $comment_id = null, $post = null ) { 522 global $comment_alt, $comment_depth, $comment_thread_alt; 523 524 $classes = array(); 525 526 $comment = get_comment( $comment_id ); 527 if ( ! $comment ) { 528 return $classes; 529 } 530 531 // Get the comment type (comment, trackback). 532 $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type; 533 534 // Add classes for comment authors that are registered users. 535 $user = $comment->user_id ? get_userdata( $comment->user_id ) : false; 536 if ( $user ) { 537 $classes[] = 'byuser'; 538 $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id ); 539 // For comment authors who are the author of the post. 540 $_post = get_post( $post ); 541 if ( $_post ) { 542 if ( $comment->user_id === $_post->post_author ) { 543 $classes[] = 'bypostauthor'; 544 } 545 } 546 } 547 548 if ( empty( $comment_alt ) ) { 549 $comment_alt = 0; 550 } 551 if ( empty( $comment_depth ) ) { 552 $comment_depth = 1; 553 } 554 if ( empty( $comment_thread_alt ) ) { 555 $comment_thread_alt = 0; 556 } 557 558 if ( $comment_alt % 2 ) { 559 $classes[] = 'odd'; 560 $classes[] = 'alt'; 561 } else { 562 $classes[] = 'even'; 563 } 564 565 ++$comment_alt; 566 567 // Alt for top-level comments. 568 if ( 1 === $comment_depth ) { 569 if ( $comment_thread_alt % 2 ) { 570 $classes[] = 'thread-odd'; 571 $classes[] = 'thread-alt'; 572 } else { 573 $classes[] = 'thread-even'; 574 } 575 ++$comment_thread_alt; 576 } 577 578 $classes[] = "depth-$comment_depth"; 579 580 if ( ! empty( $css_class ) ) { 581 if ( ! is_array( $css_class ) ) { 582 $css_class = preg_split( '#\s+#', $css_class ); 583 } 584 $classes = array_merge( $classes, $css_class ); 585 } 586 587 $classes = array_map( 'esc_attr', $classes ); 588 589 /** 590 * Filters the returned CSS classes for the current comment. 591 * 592 * @since 2.7.0 593 * 594 * @param string[] $classes An array of comment classes. 595 * @param string[] $css_class An array of additional classes added to the list. 596 * @param string $comment_id The comment ID as a numeric string. 597 * @param WP_Comment $comment The comment object. 598 * @param int|WP_Post $post The post ID or WP_Post object. 599 */ 600 return apply_filters( 'comment_class', $classes, $css_class, $comment->comment_ID, $comment, $post ); 601 } 602 603 /** 604 * Retrieves the comment date of the current comment. 605 * 606 * @since 1.5.0 607 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 608 * 609 * @param string $format Optional. PHP date format. Defaults to the 'date_format' option. 610 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the date. 611 * Default current comment. 612 * @return string The comment's date. 613 */ 614 function get_comment_date( $format = '', $comment_id = 0 ) { 615 $comment = get_comment( $comment_id ); 616 617 $_format = ! empty( $format ) ? $format : get_option( 'date_format' ); 618 619 $comment_date = mysql2date( $_format, $comment->comment_date ); 620 621 /** 622 * Filters the returned comment date. 623 * 624 * @since 1.5.0 625 * 626 * @param string|int $comment_date Formatted date string or Unix timestamp. 627 * @param string $format PHP date format. 628 * @param WP_Comment $comment The comment object. 629 */ 630 return apply_filters( 'get_comment_date', $comment_date, $format, $comment ); 631 } 632 633 /** 634 * Displays the comment date of the current comment. 635 * 636 * @since 0.71 637 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 638 * 639 * @param string $format Optional. PHP date format. Defaults to the 'date_format' option. 640 * @param int|WP_Comment $comment_id WP_Comment or ID of the comment for which to print the date. 641 * Default current comment. 642 */ 643 function comment_date( $format = '', $comment_id = 0 ) { 644 echo get_comment_date( $format, $comment_id ); 645 } 646 647 /** 648 * Retrieves the excerpt of the given comment. 649 * 650 * Returns a maximum of 20 words with an ellipsis appended if necessary. 651 * 652 * @since 1.5.0 653 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 654 * 655 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the excerpt. 656 * Default current comment. 657 * @return string The possibly truncated comment excerpt. 658 */ 659 function get_comment_excerpt( $comment_id = 0 ) { 660 $comment = get_comment( $comment_id ); 661 662 if ( ! post_password_required( $comment->comment_post_ID ) ) { 663 $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) ); 664 } else { 665 $comment_text = __( 'Password protected' ); 666 } 667 668 /* translators: Maximum number of words used in a comment excerpt. */ 669 $comment_excerpt_length = (int) _x( '20', 'comment_excerpt_length' ); 670 671 /** 672 * Filters the maximum number of words used in the comment excerpt. 673 * 674 * @since 4.4.0 675 * 676 * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt. 677 */ 678 $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length ); 679 680 $comment_excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' ); 681 682 /** 683 * Filters the retrieved comment excerpt. 684 * 685 * @since 1.5.0 686 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. 687 * 688 * @param string $comment_excerpt The comment excerpt text. 689 * @param string $comment_id The comment ID as a numeric string. 690 * @param WP_Comment $comment The comment object. 691 */ 692 return apply_filters( 'get_comment_excerpt', $comment_excerpt, $comment->comment_ID, $comment ); 693 } 694 695 /** 696 * Displays the excerpt of the current comment. 697 * 698 * @since 1.2.0 699 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 700 * 701 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to print the excerpt. 702 * Default current comment. 703 */ 704 function comment_excerpt( $comment_id = 0 ) { 705 $comment = get_comment( $comment_id ); 706 707 $comment_excerpt = get_comment_excerpt( $comment ); 708 709 /** 710 * Filters the comment excerpt for display. 711 * 712 * @since 1.2.0 713 * @since 4.1.0 The `$comment_id` parameter was added. 714 * 715 * @param string $comment_excerpt The comment excerpt text. 716 * @param string $comment_id The comment ID as a numeric string. 717 */ 718 echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID ); 719 } 720 721 /** 722 * Retrieves the comment ID of the current comment. 723 * 724 * @since 1.5.0 725 * 726 * @return string The comment ID as a numeric string. 727 */ 728 function get_comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 729 $comment = get_comment(); 730 731 $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : '0'; 732 733 /** 734 * Filters the returned comment ID. 735 * 736 * @since 1.5.0 737 * @since 4.1.0 The `$comment` parameter was added. 738 * 739 * @param string $comment_id The current comment ID as a numeric string. 740 * @param WP_Comment $comment The comment object. 741 */ 742 return apply_filters( 'get_comment_ID', $comment_id, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase 743 } 744 745 /** 746 * Displays the comment ID of the current comment. 747 * 748 * @since 0.71 749 */ 750 function comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 751 echo get_comment_ID(); 752 } 753 754 /** 755 * Retrieves the link to a given comment. 756 * 757 * @since 1.5.0 758 * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument. 759 * 760 * @see get_page_of_comment() 761 * 762 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 763 * @global bool $in_comment_loop 764 * 765 * @param WP_Comment|int|null $comment Optional. Comment to retrieve. Default current comment. 766 * @param array $args { 767 * An array of optional arguments to override the defaults. 768 * 769 * @type string $type Passed to get_page_of_comment(). 770 * @type int $page Current page of comments, for calculating comment pagination. 771 * @type int $per_page Per-page value for comment pagination. 772 * @type int $max_depth Passed to get_page_of_comment(). 773 * @type int|string $cpage Value to use for the comment's "comment-page" or "cpage" value. 774 * If provided, this value overrides any value calculated from `$page` 775 * and `$per_page`. 776 * } 777 * @return string The permalink to the given comment. 778 */ 779 function get_comment_link( $comment = null, $args = array() ) { 780 global $wp_rewrite, $in_comment_loop; 781 782 $comment = get_comment( $comment ); 783 784 // Back-compat. 785 if ( ! is_array( $args ) ) { 786 $args = array( 'page' => $args ); 787 } 788 789 $defaults = array( 790 'type' => 'all', 791 'page' => '', 792 'per_page' => '', 793 'max_depth' => '', 794 'cpage' => null, 795 ); 796 797 $args = wp_parse_args( $args, $defaults ); 798 799 $comment_link = get_permalink( $comment->comment_post_ID ); 800 801 // The 'cpage' param takes precedence. 802 if ( ! is_null( $args['cpage'] ) ) { 803 $cpage = $args['cpage']; 804 805 // No 'cpage' is provided, so we calculate one. 806 } else { 807 if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) { 808 $args['per_page'] = get_option( 'comments_per_page' ); 809 } 810 811 if ( empty( $args['per_page'] ) ) { 812 $args['per_page'] = 0; 813 $args['page'] = 0; 814 } 815 816 $cpage = $args['page']; 817 818 if ( '' === $cpage ) { 819 if ( ! empty( $in_comment_loop ) ) { 820 $cpage = (int) get_query_var( 'cpage' ); 821 } else { 822 // Requires a database hit, so we only do it when we can't figure out from context. 823 $cpage = get_page_of_comment( $comment->comment_ID, $args ); 824 } 825 } 826 827 /* 828 * If the default page displays the oldest comments, the permalinks for comments on the default page 829 * do not need a 'cpage' query var. 830 */ 831 if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) { 832 $cpage = ''; 833 } 834 } 835 836 if ( $cpage && get_option( 'page_comments' ) ) { 837 if ( $wp_rewrite->using_permalinks() ) { 838 $comment_link = trailingslashit( $comment_link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage; 839 } else { 840 $comment_link = add_query_arg( 'cpage', $cpage, $comment_link ); 841 } 842 } 843 844 if ( $wp_rewrite->using_permalinks() ) { 845 $comment_link = user_trailingslashit( $comment_link, 'comment' ); 846 } 847 848 $comment_link = $comment_link . '#comment-' . $comment->comment_ID; 849 850 /** 851 * Filters the returned single comment permalink. 852 * 853 * @since 2.8.0 854 * @since 4.4.0 Added the `$cpage` parameter. 855 * 856 * @see get_page_of_comment() 857 * 858 * @param string $comment_link The comment permalink with '#comment-$id' appended. 859 * @param WP_Comment $comment The current comment object. 860 * @param array $args An array of arguments to override the defaults. 861 * @param int $cpage The calculated 'cpage' value. 862 */ 863 return apply_filters( 'get_comment_link', $comment_link, $comment, $args, $cpage ); 864 } 865 866 /** 867 * Retrieves the link to the current post comments. 868 * 869 * @since 1.5.0 870 * 871 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 872 * @return string The link to the comments. 873 */ 874 function get_comments_link( $post = 0 ) { 875 $hash = get_comments_number( $post ) ? '#comments' : '#respond'; 876 $comments_link = get_permalink( $post ) . $hash; 877 878 /** 879 * Filters the returned post comments permalink. 880 * 881 * @since 3.6.0 882 * 883 * @param string $comments_link Post comments permalink with '#comments' appended. 884 * @param int|WP_Post $post Post ID or WP_Post object. 885 */ 886 return apply_filters( 'get_comments_link', $comments_link, $post ); 887 } 888 889 /** 890 * Displays the link to the current post comments. 891 * 892 * @since 0.71 893 * 894 * @param string $deprecated Not Used. 895 * @param string $deprecated_2 Not Used. 896 */ 897 function comments_link( $deprecated = '', $deprecated_2 = '' ) { 898 if ( ! empty( $deprecated ) ) { 899 _deprecated_argument( __FUNCTION__, '0.72' ); 900 } 901 if ( ! empty( $deprecated_2 ) ) { 902 _deprecated_argument( __FUNCTION__, '1.3.0' ); 903 } 904 echo esc_url( get_comments_link() ); 905 } 906 907 /** 908 * Retrieves the amount of comments a post has. 909 * 910 * @since 1.5.0 911 * 912 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`. 913 * @return string|int If the post exists, a numeric string representing the number of comments 914 * the post has, otherwise 0. 915 */ 916 function get_comments_number( $post = 0 ) { 917 $post = get_post( $post ); 918 919 $comments_number = $post ? $post->comment_count : 0; 920 $post_id = $post ? $post->ID : 0; 921 922 /** 923 * Filters the returned comment count for a post. 924 * 925 * @since 1.5.0 926 * 927 * @param string|int $comments_number A string representing the number of comments a post has, otherwise 0. 928 * @param int $post_id Post ID. 929 */ 930 return apply_filters( 'get_comments_number', $comments_number, $post_id ); 931 } 932 933 /** 934 * Displays the language string for the number of comments the current post has. 935 * 936 * @since 0.71 937 * @since 5.4.0 The `$deprecated` parameter was changed to `$post`. 938 * 939 * @param string|false $zero Optional. Text for no comments. Default false. 940 * @param string|false $one Optional. Text for one comment. Default false. 941 * @param string|false $more Optional. Text for more than one comment. Default false. 942 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`. 943 */ 944 function comments_number( $zero = false, $one = false, $more = false, $post = 0 ) { 945 echo get_comments_number_text( $zero, $one, $more, $post ); 946 } 947 948 /** 949 * Displays the language string for the number of comments the current post has. 950 * 951 * @since 4.0.0 952 * @since 5.4.0 Added the `$post` parameter to allow using the function outside of the loop. 953 * 954 * @param string|false $zero Optional. Text for no comments. Default false. 955 * @param string|false $one Optional. Text for one comment. Default false. 956 * @param string|false $more Optional. Text for more than one comment. Default false. 957 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`. 958 * @return string Language string for the number of comments a post has. 959 */ 960 function get_comments_number_text( $zero = false, $one = false, $more = false, $post = 0 ) { 961 $comments_number = (int) get_comments_number( $post ); 962 963 if ( $comments_number > 1 ) { 964 if ( false === $more ) { 965 $comments_number_text = sprintf( 966 /* translators: %s: Number of comments. */ 967 _n( '%s Comment', '%s Comments', $comments_number ), 968 number_format_i18n( $comments_number ) 969 ); 970 } else { 971 // % Comments 972 /* 973 * translators: If comment number in your language requires declension, 974 * translate this to 'on'. Do not translate into your own language. 975 */ 976 if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) { 977 $text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more ); 978 $text = preg_replace( '/&.+?;/', '', $text ); // Remove HTML entities. 979 $text = trim( strip_tags( $text ), '% ' ); 980 981 // Replace '% Comments' with a proper plural form. 982 if ( $text && ! preg_match( '/[0-9]+/', $text ) && str_contains( $more, '%' ) ) { 983 /* translators: %s: Number of comments. */ 984 $new_text = _n( '%s Comment', '%s Comments', $comments_number ); 985 $new_text = trim( sprintf( $new_text, '' ) ); 986 987 $more = str_replace( $text, $new_text, $more ); 988 if ( ! str_contains( $more, '%' ) ) { 989 $more = '% ' . $more; 990 } 991 } 992 } 993 994 $comments_number_text = str_replace( '%', number_format_i18n( $comments_number ), $more ); 995 } 996 } elseif ( 0 === $comments_number ) { 997 $comments_number_text = ( false === $zero ) ? __( 'No Comments' ) : $zero; 998 } else { // Must be one. 999 $comments_number_text = ( false === $one ) ? __( '1 Comment' ) : $one; 1000 } 1001 1002 /** 1003 * Filters the comments count for display. 1004 * 1005 * @since 1.5.0 1006 * 1007 * @see _n() 1008 * 1009 * @param string $comments_number_text A translatable string formatted based on whether the count 1010 * is equal to 0, 1, or 1+. 1011 * @param int $comments_number The number of post comments. 1012 */ 1013 return apply_filters( 'comments_number', $comments_number_text, $comments_number ); 1014 } 1015 1016 /** 1017 * Retrieves the text of the current comment. 1018 * 1019 * @since 1.5.0 1020 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 1021 * @since 5.4.0 Added 'In reply to %s.' prefix to child comments in comments feed. 1022 * 1023 * @see Walker_Comment::comment() 1024 * 1025 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the text. 1026 * Default current comment. 1027 * @param array $args Optional. An array of arguments. Default empty array. 1028 * @return string The comment content. 1029 */ 1030 function get_comment_text( $comment_id = 0, $args = array() ) { 1031 $comment = get_comment( $comment_id ); 1032 1033 $comment_text = $comment->comment_content; 1034 1035 if ( is_comment_feed() && $comment->comment_parent ) { 1036 $parent = get_comment( $comment->comment_parent ); 1037 if ( $parent ) { 1038 $parent_link = esc_url( get_comment_link( $parent ) ); 1039 $name = get_comment_author( $parent ); 1040 1041 $comment_text = sprintf( 1042 /* translators: %s: Comment link. */ 1043 ent2ncr( __( 'In reply to %s.' ) ), 1044 '<a href="' . $parent_link . '">' . $name . '</a>' 1045 ) . "\n\n" . $comment_text; 1046 } 1047 } 1048 1049 /** 1050 * Filters the text of a comment. 1051 * 1052 * @since 1.5.0 1053 * 1054 * @see Walker_Comment::comment() 1055 * 1056 * @param string $comment_text Text of the comment. 1057 * @param WP_Comment $comment The comment object. 1058 * @param array $args An array of arguments. 1059 */ 1060 return apply_filters( 'get_comment_text', $comment_text, $comment, $args ); 1061 } 1062 1063 /** 1064 * Displays the text of the current comment. 1065 * 1066 * @since 0.71 1067 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 1068 * 1069 * @see Walker_Comment::comment() 1070 * 1071 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to print the text. 1072 * Default current comment. 1073 * @param array $args Optional. An array of arguments. Default empty array. 1074 */ 1075 function comment_text( $comment_id = 0, $args = array() ) { 1076 $comment = get_comment( $comment_id ); 1077 1078 $comment_text = get_comment_text( $comment, $args ); 1079 1080 /** 1081 * Filters the text of a comment to be displayed. 1082 * 1083 * @since 1.2.0 1084 * 1085 * @see Walker_Comment::comment() 1086 * 1087 * @param string $comment_text Text of the comment. 1088 * @param WP_Comment|null $comment The comment object. Null if not found. 1089 * @param array $args An array of arguments. 1090 */ 1091 echo apply_filters( 'comment_text', $comment_text, $comment, $args ); 1092 } 1093 1094 /** 1095 * Retrieves the comment time of the current comment. 1096 * 1097 * @since 1.5.0 1098 * @since 6.2.0 Added the `$comment_id` parameter. 1099 * 1100 * @param string $format Optional. PHP date format. Defaults to the 'time_format' option. 1101 * @param bool $gmt Optional. Whether to use the GMT date. Default false. 1102 * @param bool $translate Optional. Whether to translate the time (for use in feeds). 1103 * Default true. 1104 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the time. 1105 * Default current comment. 1106 * @return string The formatted time. 1107 */ 1108 function get_comment_time( $format = '', $gmt = false, $translate = true, $comment_id = 0 ) { 1109 $comment = get_comment( $comment_id ); 1110 1111 if ( null === $comment ) { 1112 return ''; 1113 } 1114 1115 $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date; 1116 1117 $_format = ! empty( $format ) ? $format : get_option( 'time_format' ); 1118 1119 $comment_time = mysql2date( $_format, $comment_date, $translate ); 1120 1121 /** 1122 * Filters the returned comment time. 1123 * 1124 * @since 1.5.0 1125 * 1126 * @param string|int $comment_time The comment time, formatted as a date string or Unix timestamp. 1127 * @param string $format PHP date format. 1128 * @param bool $gmt Whether the GMT date is in use. 1129 * @param bool $translate Whether the time is translated. 1130 * @param WP_Comment $comment The comment object. 1131 */ 1132 return apply_filters( 'get_comment_time', $comment_time, $format, $gmt, $translate, $comment ); 1133 } 1134 1135 /** 1136 * Displays the comment time of the current comment. 1137 * 1138 * @since 0.71 1139 * @since 6.2.0 Added the `$comment_id` parameter. 1140 * 1141 * @param string $format Optional. PHP time format. Defaults to the 'time_format' option. 1142 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to print the time. 1143 * Default current comment. 1144 */ 1145 function comment_time( $format = '', $comment_id = 0 ) { 1146 echo get_comment_time( $format, false, true, $comment_id ); 1147 } 1148 1149 /** 1150 * Retrieves the comment type of the current comment. 1151 * 1152 * @since 1.5.0 1153 * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object. 1154 * 1155 * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the type. 1156 * Default current comment. 1157 * @return string The comment type. 1158 */ 1159 function get_comment_type( $comment_id = 0 ) { 1160 $comment = get_comment( $comment_id ); 1161 1162 if ( '' === $comment->comment_type ) { 1163 $comment->comment_type = 'comment'; 1164 } 1165 1166 /** 1167 * Filters the returned comment type. 1168 * 1169 * @since 1.5.0 1170 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. 1171 * 1172 * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'. 1173 * @param string $comment_id The comment ID as a numeric string. 1174 * @param WP_Comment $comment The comment object. 1175 */ 1176 return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment ); 1177 } 1178 1179 /** 1180 * Displays the comment type of the current comment. 1181 * 1182 * @since 0.71 1183 * 1184 * @param string|false $comment_text Optional. String to display for comment type. Default false. 1185 * @param string|false $trackback_text Optional. String to display for trackback type. Default false. 1186 * @param string|false $pingback_text Optional. String to display for pingback type. Default false. 1187 */ 1188 function comment_type( $comment_text = false, $trackback_text = false, $pingback_text = false ) { 1189 if ( false === $comment_text ) { 1190 $comment_text = _x( 'Comment', 'noun' ); 1191 } 1192 if ( false === $trackback_text ) { 1193 $trackback_text = __( 'Trackback' ); 1194 } 1195 if ( false === $pingback_text ) { 1196 $pingback_text = __( 'Pingback' ); 1197 } 1198 $type = get_comment_type(); 1199 switch ( $type ) { 1200 case 'trackback': 1201 echo $trackback_text; 1202 break; 1203 case 'pingback': 1204 echo $pingback_text; 1205 break; 1206 default: 1207 echo $comment_text; 1208 } 1209 } 1210 1211 /** 1212 * Retrieves the current post's trackback URL. 1213 * 1214 * There is a check to see if permalink's have been enabled and if so, will 1215 * retrieve the pretty path. If permalinks weren't enabled, the ID of the 1216 * current post is used and appended to the correct page to go to. 1217 * 1218 * @since 1.5.0 1219 * 1220 * @return string The trackback URL after being filtered. 1221 */ 1222 function get_trackback_url() { 1223 if ( get_option( 'permalink_structure' ) ) { 1224 $trackback_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' ); 1225 } else { 1226 $trackback_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID(); 1227 } 1228 1229 /** 1230 * Filters the returned trackback URL. 1231 * 1232 * @since 2.2.0 1233 * 1234 * @param string $trackback_url The trackback URL. 1235 */ 1236 return apply_filters( 'trackback_url', $trackback_url ); 1237 } 1238 1239 /** 1240 * Displays the current post's trackback URL. 1241 * 1242 * @since 0.71 1243 * @since 2.5.0 Deprecated the `$deprecated_echo` argument. 1244 * 1245 * @see get_trackback_url() 1246 * 1247 * @param true $deprecated_echo Deprecated. Use {@see get_trackback_url()}. Echo the URL or 1248 * return it. Default true. 1249 * @return string|void The trackback URL when `$deprecated_echo` is false, nothing otherwise. 1250 * @phpstan-return ( $deprecated_echo is true ? void : string ) 1251 * 1252 * @phpstan-ignore conditionalType.alwaysTrue (Typed `true` to flag the deprecated argument.) 1253 */ 1254 function trackback_url( $deprecated_echo = true ) { 1255 if ( true !== $deprecated_echo ) { 1256 _deprecated_argument( 1257 __FUNCTION__, 1258 '2.5.0', 1259 sprintf( 1260 /* translators: %s: get_trackback_url() */ 1261 __( 'Use %s instead if you do not want the value echoed.' ), 1262 '<code>get_trackback_url()</code>' 1263 ) 1264 ); 1265 } 1266 1267 if ( $deprecated_echo ) { 1268 echo get_trackback_url(); 1269 } else { 1270 return get_trackback_url(); 1271 } 1272 } 1273 1274 /** 1275 * Generates and displays the RDF for the trackback information of current post. 1276 * 1277 * Deprecated in 3.0.0, and restored in 3.0.1. 1278 * 1279 * @since 0.71 1280 * 1281 * @param int|string $deprecated Not used (Was $timezone = 0). 1282 */ 1283 function trackback_rdf( $deprecated = '' ) { 1284 if ( ! empty( $deprecated ) ) { 1285 _deprecated_argument( __FUNCTION__, '2.5.0' ); 1286 } 1287 1288 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) { 1289 return; 1290 } 1291 1292 echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 1293 xmlns:dc="http://purl.org/dc/elements/1.1/" 1294 xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> 1295 <rdf:Description rdf:about="'; 1296 the_permalink(); 1297 echo '"' . "\n"; 1298 echo ' dc:identifier="'; 1299 the_permalink(); 1300 echo '"' . "\n"; 1301 echo ' dc:title="' . str_replace( '--', '--', wptexturize( strip_tags( get_the_title() ) ) ) . '"' . "\n"; 1302 echo ' trackback:ping="' . get_trackback_url() . '"' . " />\n"; 1303 echo '</rdf:RDF>'; 1304 } 1305 1306 /** 1307 * Determines whether the current post is open for comments. 1308 * 1309 * For more information on this and similar theme functions, check out 1310 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1311 * Conditional Tags} article in the Theme Developer Handbook. 1312 * 1313 * @since 1.5.0 1314 * 1315 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. 1316 * @return bool True if the comments are open. 1317 */ 1318 function comments_open( $post = null ) { 1319 $_post = get_post( $post ); 1320 1321 $post_id = $_post ? $_post->ID : 0; 1322 $comments_open = ( $_post && ( 'open' === $_post->comment_status ) ); 1323 1324 /** 1325 * Filters whether the current post is open for comments. 1326 * 1327 * @since 2.5.0 1328 * 1329 * @param bool $comments_open Whether the current post is open for comments. 1330 * @param int $post_id The post ID. 1331 */ 1332 return apply_filters( 'comments_open', $comments_open, $post_id ); 1333 } 1334 1335 /** 1336 * Determines whether the current post is open for pings. 1337 * 1338 * For more information on this and similar theme functions, check out 1339 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1340 * Conditional Tags} article in the Theme Developer Handbook. 1341 * 1342 * @since 1.5.0 1343 * 1344 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. 1345 * @return bool True if pings are accepted 1346 */ 1347 function pings_open( $post = null ) { 1348 $_post = get_post( $post ); 1349 1350 $post_id = $_post ? $_post->ID : 0; 1351 $pings_open = ( $_post && ( 'open' === $_post->ping_status ) ); 1352 1353 /** 1354 * Filters whether the current post is open for pings. 1355 * 1356 * @since 2.5.0 1357 * 1358 * @param bool $pings_open Whether the current post is open for pings. 1359 * @param int $post_id The post ID. 1360 */ 1361 return apply_filters( 'pings_open', $pings_open, $post_id ); 1362 } 1363 1364 /** 1365 * Displays form token for unfiltered comments. 1366 * 1367 * Will only display nonce token if the current user has permissions for 1368 * unfiltered html. Won't display the token for other users. 1369 * 1370 * The function was backported to 2.0.10 and was added to versions 2.1.3 and 1371 * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in 1372 * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0. 1373 * 1374 * Backported to 2.0.10. 1375 * 1376 * @since 2.1.3 1377 */ 1378 function wp_comment_form_unfiltered_html_nonce() { 1379 $post = get_post(); 1380 $post_id = $post ? $post->ID : 0; 1381 1382 if ( current_user_can( 'unfiltered_html' ) ) { 1383 wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false ); 1384 wp_print_inline_script_tag( "(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();\n//# sourceURL=" . rawurlencode( __FUNCTION__ ) ); 1385 } 1386 } 1387 1388 /** 1389 * Loads the comment template specified in $file. 1390 * 1391 * Will not display the comments template if not on single post or page, or if 1392 * the post does not have comments. 1393 * 1394 * Uses the WordPress database object to query for the comments. The comments 1395 * are passed through the {@see 'comments_array'} filter hook with the list of comments 1396 * and the post ID respectively. 1397 * 1398 * The `$file` path is passed through a filter hook called {@see 'comments_template'}, 1399 * which includes the template directory and $file combined. Tries the $filtered path 1400 * first and if it fails it will require the default comment template from the 1401 * default theme. If either does not exist, then the WordPress process will be 1402 * halted. It is advised for that reason, that the default theme is not deleted. 1403 * 1404 * Will not try to get the comments if the post has none. 1405 * 1406 * @since 1.5.0 1407 * 1408 * @global WP_Query $wp_query WordPress Query object. 1409 * @global WP_Post $post Global post object. 1410 * @global wpdb $wpdb WordPress database abstraction object. 1411 * @global int $id 1412 * @global WP_Comment $comment Global comment object. 1413 * @global string $user_login 1414 * @global string $user_identity 1415 * @global bool $overridden_cpage 1416 * @global bool $withcomments 1417 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory. 1418 * @global string $wp_template_path Path to current theme's template directory. 1419 * 1420 * @param string $file Optional. The file to load. Default '/comments.php'. 1421 * @param bool $separate_comments Optional. Whether to separate the comments by comment type. 1422 * Default false. 1423 */ 1424 function comments_template( $file = '/comments.php', $separate_comments = false ) { 1425 global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage, $wp_stylesheet_path, $wp_template_path; 1426 1427 if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) { 1428 return; 1429 } 1430 1431 if ( empty( $file ) ) { 1432 $file = '/comments.php'; 1433 } 1434 1435 $req = get_option( 'require_name_email' ); 1436 1437 /* 1438 * Comment author information fetched from the comment cookies. 1439 */ 1440 $commenter = wp_get_current_commenter(); 1441 1442 /* 1443 * The name of the current comment author escaped for use in attributes. 1444 * Escaped by sanitize_comment_cookies(). 1445 */ 1446 $comment_author = $commenter['comment_author']; 1447 1448 /* 1449 * The email address of the current comment author escaped for use in attributes. 1450 * Escaped by sanitize_comment_cookies(). 1451 */ 1452 $comment_author_email = $commenter['comment_author_email']; 1453 1454 /* 1455 * The URL of the current comment author escaped for use in attributes. 1456 */ 1457 $comment_author_url = esc_url( $commenter['comment_author_url'] ); 1458 1459 $comment_args = array( 1460 'orderby' => 'comment_date_gmt', 1461 'order' => 'ASC', 1462 'status' => 'approve', 1463 'post_id' => $post->ID, 1464 'no_found_rows' => false, 1465 ); 1466 1467 if ( get_option( 'thread_comments' ) ) { 1468 $comment_args['hierarchical'] = 'threaded'; 1469 } else { 1470 $comment_args['hierarchical'] = false; 1471 } 1472 1473 if ( is_user_logged_in() ) { 1474 $comment_args['include_unapproved'] = array( get_current_user_id() ); 1475 } else { 1476 $unapproved_email = wp_get_unapproved_comment_author_email(); 1477 1478 if ( $unapproved_email ) { 1479 $comment_args['include_unapproved'] = array( $unapproved_email ); 1480 } 1481 } 1482 1483 $per_page = 0; 1484 if ( get_option( 'page_comments' ) ) { 1485 $per_page = (int) get_query_var( 'comments_per_page' ); 1486 if ( 0 === $per_page ) { 1487 $per_page = (int) get_option( 'comments_per_page' ); 1488 } 1489 1490 $comment_args['number'] = $per_page; 1491 $page = (int) get_query_var( 'cpage' ); 1492 1493 if ( $page ) { 1494 $comment_args['offset'] = ( $page - 1 ) * $per_page; 1495 } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) { 1496 $comment_args['offset'] = 0; 1497 } else { 1498 // If fetching the first page of 'newest', we need a top-level comment count. 1499 $top_level_query = new WP_Comment_Query(); 1500 $top_level_args = array( 1501 'count' => true, 1502 'orderby' => false, 1503 'post_id' => $post->ID, 1504 'status' => 'approve', 1505 ); 1506 1507 if ( $comment_args['hierarchical'] ) { 1508 $top_level_args['parent'] = 0; 1509 } 1510 1511 if ( isset( $comment_args['include_unapproved'] ) ) { 1512 $top_level_args['include_unapproved'] = $comment_args['include_unapproved']; 1513 } 1514 1515 /** 1516 * Filters the arguments used in the top level comments query. 1517 * 1518 * @since 5.6.0 1519 * 1520 * @see WP_Comment_Query::__construct() 1521 * 1522 * @param array $top_level_args { 1523 * The top level query arguments for the comments template. 1524 * 1525 * @type bool $count Whether to return a comment count. 1526 * @type string|array $orderby The field(s) to order by. 1527 * @type int $post_id The post ID. 1528 * @type string|array $status The comment status to limit results by. 1529 * } 1530 */ 1531 $top_level_args = apply_filters( 'comments_template_top_level_query_args', $top_level_args ); 1532 1533 $top_level_count = $top_level_query->query( $top_level_args ); 1534 1535 $comment_args['offset'] = ( (int) ceil( $top_level_count / $per_page ) - 1 ) * $per_page; 1536 } 1537 } 1538 1539 /** 1540 * Filters the arguments used to query comments in comments_template(). 1541 * 1542 * @since 4.5.0 1543 * 1544 * @see WP_Comment_Query::__construct() 1545 * 1546 * @param array $comment_args { 1547 * Array of WP_Comment_Query arguments. 1548 * 1549 * @type string|array $orderby Field(s) to order by. 1550 * @type string $order Order of results. Accepts 'ASC' or 'DESC'. 1551 * @type string $status Comment status. 1552 * @type array $include_unapproved Array of IDs or email addresses whose unapproved comments 1553 * will be included in results. 1554 * @type int $post_id ID of the post. 1555 * @type bool $no_found_rows Whether to refrain from querying for found rows. 1556 * @type bool $update_comment_meta_cache Whether to prime cache for comment meta. 1557 * @type bool|string $hierarchical Whether to query for comments hierarchically. 1558 * @type int $offset Comment offset. 1559 * @type int $number Number of comments to fetch. 1560 * } 1561 */ 1562 $comment_args = apply_filters( 'comments_template_query_args', $comment_args ); 1563 1564 $comment_query = new WP_Comment_Query( $comment_args ); 1565 $_comments = $comment_query->comments; 1566 1567 // Trees must be flattened before they're passed to the walker. 1568 if ( $comment_args['hierarchical'] ) { 1569 $comments_flat = array(); 1570 foreach ( $_comments as $_comment ) { 1571 $comments_flat[] = $_comment; 1572 $comment_children = $_comment->get_children( 1573 array( 1574 'format' => 'flat', 1575 'status' => $comment_args['status'], 1576 'orderby' => $comment_args['orderby'], 1577 ) 1578 ); 1579 1580 foreach ( $comment_children as $comment_child ) { 1581 $comments_flat[] = $comment_child; 1582 } 1583 } 1584 } else { 1585 $comments_flat = $_comments; 1586 } 1587 1588 /** 1589 * Filters the comments array. 1590 * 1591 * @since 2.1.0 1592 * 1593 * @param array $comments Array of comments supplied to the comments template. 1594 * @param int $post_id Post ID. 1595 */ 1596 $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID ); 1597 1598 $comments = &$wp_query->comments; 1599 $wp_query->comment_count = count( $wp_query->comments ); 1600 $wp_query->max_num_comment_pages = $comment_query->max_num_pages; 1601 1602 if ( $separate_comments ) { 1603 $wp_query->comments_by_type = separate_comments( $comments ); 1604 $comments_by_type = &$wp_query->comments_by_type; 1605 } else { 1606 $wp_query->comments_by_type = array(); 1607 } 1608 1609 $overridden_cpage = false; 1610 1611 if ( '' === get_query_var( 'cpage' ) && $wp_query->max_num_comment_pages > 1 ) { 1612 set_query_var( 'cpage', 'newest' === get_option( 'default_comments_page' ) ? get_comment_pages_count() : 1 ); 1613 $overridden_cpage = true; 1614 } 1615 1616 if ( ! defined( 'COMMENTS_TEMPLATE' ) ) { 1617 define( 'COMMENTS_TEMPLATE', true ); 1618 } 1619 1620 $theme_template = trailingslashit( $wp_stylesheet_path ) . $file; 1621 1622 /** 1623 * Filters the path to the theme template file used for the comments template. 1624 * 1625 * @since 1.5.1 1626 * 1627 * @param string $theme_template The path to the theme template file. 1628 */ 1629 $include = apply_filters( 'comments_template', $theme_template ); 1630 1631 if ( file_exists( $include ) ) { 1632 require $include; 1633 } elseif ( file_exists( trailingslashit( $wp_template_path ) . $file ) ) { 1634 require trailingslashit( $wp_template_path ) . $file; 1635 } else { // Backward compat code will be removed in a future release. 1636 require ABSPATH . WPINC . '/theme-compat/comments.php'; 1637 } 1638 } 1639 1640 /** 1641 * Displays the link to the comments for the current post ID. 1642 * 1643 * @since 0.71 1644 * 1645 * @param false|string $zero Optional. String to display when no comments. Default false. 1646 * @param false|string $one Optional. String to display when only one comment is available. Default false. 1647 * @param false|string $more Optional. String to display when there are more than one comment. Default false. 1648 * @param string $css_class Optional. CSS class to use for comments. Default empty. 1649 * @param false|string $none Optional. String to display when comments have been turned off. Default false. 1650 */ 1651 function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) { 1652 $post_id = get_the_ID(); 1653 $post_title = get_the_title(); 1654 $comments_number = (int) get_comments_number( $post_id ); 1655 1656 if ( false === $zero ) { 1657 /* translators: %s: Post title. */ 1658 $zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $post_title ); 1659 } 1660 1661 if ( false === $one ) { 1662 /* translators: %s: Post title. */ 1663 $one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $post_title ); 1664 } 1665 1666 if ( false === $more ) { 1667 /* translators: 1: Number of comments, 2: Post title. */ 1668 $more = _n( 1669 '%1$s Comment<span class="screen-reader-text"> on %2$s</span>', 1670 '%1$s Comments<span class="screen-reader-text"> on %2$s</span>', 1671 $comments_number 1672 ); 1673 $more = sprintf( $more, number_format_i18n( $comments_number ), $post_title ); 1674 } 1675 1676 if ( false === $none ) { 1677 /* translators: %s: Post title. */ 1678 $none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $post_title ); 1679 } 1680 1681 if ( 0 === $comments_number && ! comments_open() && ! pings_open() ) { 1682 printf( 1683 '<span%1$s>%2$s</span>', 1684 ! empty( $css_class ) ? ' class="' . esc_attr( $css_class ) . '"' : '', 1685 $none 1686 ); 1687 return; 1688 } 1689 1690 if ( post_password_required() ) { 1691 _e( 'Enter your password to view comments.' ); 1692 return; 1693 } 1694 1695 if ( 0 === $comments_number ) { 1696 $respond_link = get_permalink() . '#respond'; 1697 /** 1698 * Filters the respond link when a post has no comments. 1699 * 1700 * @since 4.4.0 1701 * 1702 * @param string $respond_link The default response link. 1703 * @param int $post_id The post ID. 1704 */ 1705 $comments_link = apply_filters( 'respond_link', $respond_link, $post_id ); 1706 } else { 1707 $comments_link = get_comments_link(); 1708 } 1709 1710 $link_attributes = ''; 1711 1712 /** 1713 * Filters the comments link attributes for display. 1714 * 1715 * @since 2.5.0 1716 * 1717 * @param string $link_attributes The comments link attributes. Default empty. 1718 */ 1719 $link_attributes = apply_filters( 'comments_popup_link_attributes', $link_attributes ); 1720 1721 printf( 1722 '<a href="%1$s"%2$s%3$s>%4$s</a>', 1723 esc_url( $comments_link ), 1724 ! empty( $css_class ) ? ' class="' . $css_class . '" ' : '', 1725 $link_attributes, 1726 get_comments_number_text( $zero, $one, $more ) 1727 ); 1728 } 1729 1730 /** 1731 * Retrieves HTML content for reply to comment link. 1732 * 1733 * @since 2.7.0 1734 * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. 1735 * 1736 * @param array $args { 1737 * Optional. Override default arguments. 1738 * 1739 * @type string $add_below The first part of the selector used to identify the comment to respond below. 1740 * The resulting value is passed as the first parameter to addComment.moveForm(), 1741 * concatenated as $add_below-$comment->comment_ID. Default 'comment'. 1742 * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter 1743 * to addComment.moveForm(), and appended to the link URL as a hash value. 1744 * Default 'respond'. 1745 * @type string $reply_text The visible text of the Reply link. Default 'Reply'. 1746 * @type string $reply_to_text The accessible name of the Reply link, using `%s` as a placeholder 1747 * for the comment author's name. Default 'Reply to %s'. 1748 * Should start with the visible `reply_text` value. 1749 * @type bool $show_reply_to_text Whether to use `reply_to_text` as visible link text. Default false. 1750 * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'. 1751 * @type int $max_depth The max depth of the comment tree. Default 0. 1752 * @type int $depth The depth of the new comment. Must be greater than 0 and less than the value 1753 * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0. 1754 * @type string $before The text or HTML to add before the reply link. Default empty. 1755 * @type string $after The text or HTML to add after the reply link. Default empty. 1756 * } 1757 * @param int|WP_Comment $comment Optional. Comment being replied to. Default current comment. 1758 * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on. 1759 * Default current post. 1760 * @return string|false|null Link to show comment form on success. False if comments are closed. Null on failure. 1761 */ 1762 function get_comment_reply_link( $args = array(), $comment = null, $post = null ) { 1763 $defaults = array( 1764 'add_below' => 'comment', 1765 'respond_id' => 'respond', 1766 /* translators: Comment reply button text. */ 1767 'reply_text' => _x( 'Reply', 'verb' ), 1768 /* translators: Comment reply button text. %s: Comment author name. */ 1769 'reply_to_text' => __( 'Reply to %s' ), 1770 'login_text' => __( 'Log in to Reply' ), 1771 'max_depth' => 0, 1772 'depth' => 0, 1773 'before' => '', 1774 'after' => '', 1775 'show_reply_to_text' => false, 1776 ); 1777 1778 $args = wp_parse_args( $args, $defaults ); 1779 1780 $args['max_depth'] = (int) $args['max_depth']; 1781 $args['depth'] = (int) $args['depth']; 1782 1783 if ( 0 === $args['depth'] || $args['max_depth'] <= $args['depth'] ) { 1784 return null; 1785 } 1786 1787 $comment = get_comment( $comment ); 1788 1789 if ( empty( $comment ) ) { 1790 return null; 1791 } 1792 1793 if ( empty( $post ) ) { 1794 $post = $comment->comment_post_ID; 1795 } 1796 1797 $post = get_post( $post ); 1798 1799 if ( ! comments_open( $post->ID ) ) { 1800 return false; 1801 } 1802 1803 if ( get_option( 'page_comments' ) ) { 1804 $permalink = str_replace( '#comment-' . $comment->comment_ID, '', get_comment_link( $comment ) ); 1805 } else { 1806 $permalink = get_permalink( $post->ID ); 1807 } 1808 1809 /** 1810 * Filters the comment reply link arguments. 1811 * 1812 * @since 4.1.0 1813 * 1814 * @param array $args Comment reply link arguments. See get_comment_reply_link() 1815 * for more information on accepted arguments. 1816 * @param WP_Comment $comment The object of the comment being replied to. 1817 * @param WP_Post $post The WP_Post object. 1818 */ 1819 $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post ); 1820 1821 if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) { 1822 $link = sprintf( 1823 '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>', 1824 esc_url( wp_login_url( get_permalink() ) ), 1825 $args['login_text'] 1826 ); 1827 } else { 1828 $data_attributes = array( 1829 'commentid' => $comment->comment_ID, 1830 'postid' => $post->ID, 1831 'belowelement' => $args['add_below'] . '-' . $comment->comment_ID, 1832 'respondelement' => $args['respond_id'], 1833 'replyto' => sprintf( $args['reply_to_text'], get_comment_author( $comment ) ), 1834 ); 1835 1836 $data_attribute_string = ''; 1837 1838 foreach ( $data_attributes as $name => $value ) { 1839 $data_attribute_string .= " data-{$name}=\"" . esc_attr( $value ) . '"'; 1840 } 1841 1842 $data_attribute_string = trim( $data_attribute_string ); 1843 1844 $reply_text = $args['show_reply_to_text'] 1845 ? sprintf( $args['reply_to_text'], get_comment_author( $comment ) ) 1846 : $args['reply_text']; 1847 1848 $aria_label = $args['show_reply_to_text'] ? '' : sprintf( $args['reply_to_text'], get_comment_author( $comment ) ); 1849 1850 $link = sprintf( 1851 '<a rel="nofollow" class="comment-reply-link" href="%s" %s%s>%s</a>', 1852 esc_url( 1853 add_query_arg( 1854 array( 1855 'replytocom' => $comment->comment_ID, 1856 'unapproved' => false, 1857 'moderation-hash' => false, 1858 ), 1859 $permalink 1860 ) 1861 ) . '#' . $args['respond_id'], 1862 $data_attribute_string, 1863 $aria_label ? ' aria-label="' . esc_attr( $aria_label ) . '"' : '', 1864 $reply_text 1865 ); 1866 } 1867 1868 $comment_reply_link = $args['before'] . $link . $args['after']; 1869 1870 /** 1871 * Filters the comment reply link. 1872 * 1873 * @since 2.7.0 1874 * 1875 * @param string $comment_reply_link The HTML markup for the comment reply link. 1876 * @param array $args An array of arguments overriding the defaults. 1877 * @param WP_Comment $comment The object of the comment being replied. 1878 * @param WP_Post $post The WP_Post object. 1879 */ 1880 return apply_filters( 'comment_reply_link', $comment_reply_link, $args, $comment, $post ); 1881 } 1882 1883 /** 1884 * Displays the HTML content for reply to comment link. 1885 * 1886 * @since 2.7.0 1887 * 1888 * @see get_comment_reply_link() 1889 * 1890 * @param array $args Optional. Override default options. Default empty array. 1891 * @param int|WP_Comment $comment Optional. Comment being replied to. Default current comment. 1892 * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on. 1893 * Default current post. 1894 */ 1895 function comment_reply_link( $args = array(), $comment = null, $post = null ) { 1896 echo get_comment_reply_link( $args, $comment, $post ); 1897 } 1898 1899 /** 1900 * Retrieves HTML content for reply to post link. 1901 * 1902 * @since 2.7.0 1903 * 1904 * @param array $args { 1905 * Optional. Override default arguments. 1906 * 1907 * @type string $add_below The first part of the selector used to identify the comment to respond below. 1908 * The resulting value is passed as the first parameter to addComment.moveForm(), 1909 * concatenated as $add_below-$comment->comment_ID. Default is 'post'. 1910 * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter 1911 * to addComment.moveForm(), and appended to the link URL as a hash value. 1912 * Default 'respond'. 1913 * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'. 1914 * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'. 1915 * @type string $before Text or HTML to add before the reply link. Default empty. 1916 * @type string $after Text or HTML to add after the reply link. Default empty. 1917 * } 1918 * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on. 1919 * Default current post. 1920 * @return string|false Link to show comment form on success. False if comments are closed. 1921 */ 1922 function get_post_reply_link( $args = array(), $post = null ) { 1923 $defaults = array( 1924 'add_below' => 'post', 1925 'respond_id' => 'respond', 1926 'reply_text' => __( 'Leave a Comment' ), 1927 'login_text' => __( 'Log in to leave a Comment' ), 1928 'before' => '', 1929 'after' => '', 1930 ); 1931 1932 $args = wp_parse_args( $args, $defaults ); 1933 1934 $post = get_post( $post ); 1935 1936 if ( ! comments_open( $post->ID ) ) { 1937 return false; 1938 } 1939 1940 if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) { 1941 $link = sprintf( 1942 '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>', 1943 wp_login_url( get_permalink() ), 1944 $args['login_text'] 1945 ); 1946 } else { 1947 $onclick = sprintf( 1948 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )', 1949 $args['add_below'], 1950 $post->ID, 1951 $args['respond_id'] 1952 ); 1953 1954 $link = sprintf( 1955 "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>", 1956 get_permalink( $post->ID ) . '#' . $args['respond_id'], 1957 $onclick, 1958 $args['reply_text'] 1959 ); 1960 } 1961 1962 $post_reply_link = $args['before'] . $link . $args['after']; 1963 1964 /** 1965 * Filters the formatted post comments link HTML. 1966 * 1967 * @since 2.7.0 1968 * 1969 * @param string $post_reply_link The HTML-formatted post comments link. 1970 * @param int|WP_Post $post The post ID or WP_Post object. 1971 */ 1972 return apply_filters( 'post_comments_link', $post_reply_link, $post ); 1973 } 1974 1975 /** 1976 * Displays the HTML content for reply to post link. 1977 * 1978 * @since 2.7.0 1979 * 1980 * @see get_post_reply_link() 1981 * 1982 * @param array $args Optional. Override default options. Default empty array. 1983 * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on. 1984 * Default current post. 1985 */ 1986 function post_reply_link( $args = array(), $post = null ) { 1987 echo get_post_reply_link( $args, $post ); 1988 } 1989 1990 /** 1991 * Retrieves HTML content for cancel comment reply link. 1992 * 1993 * @since 2.7.0 1994 * @since 6.2.0 Added the `$post` parameter. 1995 * 1996 * @param string $link_text Optional. Text to display for cancel reply link. If empty, 1997 * defaults to 'Click here to cancel reply'. Default empty. 1998 * @param int|WP_Post|null $post Optional. The post the comment thread is being 1999 * displayed for. Defaults to the current global post. 2000 * @return string 2001 */ 2002 function get_cancel_comment_reply_link( $link_text = '', $post = null ) { 2003 if ( empty( $link_text ) ) { 2004 $link_text = __( 'Click here to cancel reply.' ); 2005 } 2006 2007 $post = get_post( $post ); 2008 $reply_to_id = $post ? _get_comment_reply_id( $post->ID ) : 0; 2009 $link_style = 0 !== $reply_to_id ? '' : ' style="display:none;"'; 2010 $link_url = esc_url( remove_query_arg( array( 'replytocom', 'unapproved', 'moderation-hash' ) ) ) . '#respond'; 2011 2012 $cancel_comment_reply_link = sprintf( 2013 '<a rel="nofollow" id="cancel-comment-reply-link" href="%1$s"%2$s>%3$s</a>', 2014 $link_url, 2015 $link_style, 2016 $link_text 2017 ); 2018 2019 /** 2020 * Filters the cancel comment reply link HTML. 2021 * 2022 * @since 2.7.0 2023 * 2024 * @param string $cancel_comment_reply_link The HTML-formatted cancel comment reply link. 2025 * @param string $link_url Cancel comment reply link URL. 2026 * @param string $link_text Cancel comment reply link text. 2027 */ 2028 return apply_filters( 'cancel_comment_reply_link', $cancel_comment_reply_link, $link_url, $link_text ); 2029 } 2030 2031 /** 2032 * Displays HTML content for cancel comment reply link. 2033 * 2034 * @since 2.7.0 2035 * 2036 * @param string $link_text Optional. Text to display for cancel reply link. If empty, 2037 * defaults to 'Click here to cancel reply'. Default empty. 2038 */ 2039 function cancel_comment_reply_link( $link_text = '' ) { 2040 echo get_cancel_comment_reply_link( $link_text ); 2041 } 2042 2043 /** 2044 * Retrieves hidden input HTML for replying to comments. 2045 * 2046 * @since 3.0.0 2047 * @since 6.2.0 Renamed `$post_id` to `$post` and added WP_Post support. 2048 * 2049 * @param int|WP_Post|null $post Optional. The post the comment is being displayed for. 2050 * Defaults to the current global post. 2051 * @return string Hidden input HTML for replying to comments. 2052 */ 2053 function get_comment_id_fields( $post = null ) { 2054 $post = get_post( $post ); 2055 if ( ! $post ) { 2056 return ''; 2057 } 2058 2059 $post_id = $post->ID; 2060 $reply_to_id = _get_comment_reply_id( $post_id ); 2061 2062 $comment_id_fields = "<input type='hidden' name='comment_post_ID' value='$post_id' id='comment_post_ID' />\n"; 2063 $comment_id_fields .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$reply_to_id' />\n"; 2064 2065 /** 2066 * Filters the returned comment ID fields. 2067 * 2068 * @since 3.0.0 2069 * 2070 * @param string $comment_id_fields The HTML-formatted hidden ID field comment elements. 2071 * @param int $post_id The post ID. 2072 * @param int $reply_to_id The ID of the comment being replied to. 2073 */ 2074 return apply_filters( 'comment_id_fields', $comment_id_fields, $post_id, $reply_to_id ); 2075 } 2076 2077 /** 2078 * Outputs hidden input HTML for replying to comments. 2079 * 2080 * Adds two hidden inputs to the comment form to identify the `comment_post_ID` 2081 * and `comment_parent` values for threaded comments. 2082 * 2083 * This tag must be within the `<form>` section of the `comments.php` template. 2084 * 2085 * @since 2.7.0 2086 * @since 6.2.0 Renamed `$post_id` to `$post` and added WP_Post support. 2087 * 2088 * @see get_comment_id_fields() 2089 * 2090 * @param int|WP_Post|null $post Optional. The post the comment is being displayed for. 2091 * Defaults to the current global post. 2092 */ 2093 function comment_id_fields( $post = null ) { 2094 echo get_comment_id_fields( $post ); 2095 } 2096 2097 /** 2098 * Displays text based on comment reply status. 2099 * 2100 * Only affects users with JavaScript disabled. 2101 * 2102 * {@internal The $comment global must be present to allow template tags access to the current 2103 * comment. See https://core.trac.wordpress.org/changeset/36512.} 2104 * 2105 * @since 2.7.0 2106 * @since 6.2.0 Added the `$post` parameter. 2107 * 2108 * @global WP_Comment $comment Global comment object. 2109 * 2110 * @param string|false $no_reply_text Optional. Text to display when not replying to a comment. 2111 * Default false. 2112 * @param string|false $reply_text Optional. Text to display when replying to a comment. 2113 * Default false. Accepts "%s" for the author of the comment 2114 * being replied to. 2115 * @param bool $link_to_parent Optional. Boolean to control making the author's name a link 2116 * to their comment. Default true. 2117 * @param int|WP_Post|null $post Optional. The post that the comment form is being displayed for. 2118 * Defaults to the current global post. 2119 */ 2120 function comment_form_title( $no_reply_text = false, $reply_text = false, $link_to_parent = true, $post = null ) { 2121 global $comment; 2122 2123 if ( false === $no_reply_text ) { 2124 $no_reply_text = __( 'Leave a Reply' ); 2125 } 2126 2127 if ( false === $reply_text ) { 2128 /* translators: %s: Author of the comment being replied to. */ 2129 $reply_text = __( 'Leave a Reply to %s' ); 2130 } 2131 2132 $post = get_post( $post ); 2133 if ( ! $post ) { 2134 echo $no_reply_text; 2135 return; 2136 } 2137 2138 $reply_to_id = _get_comment_reply_id( $post->ID ); 2139 2140 if ( 0 === $reply_to_id ) { 2141 echo $no_reply_text; 2142 return; 2143 } 2144 2145 // Sets the global so that template tags can be used in the comment form. 2146 $comment = get_comment( $reply_to_id ); 2147 2148 if ( $link_to_parent ) { 2149 $comment_author = sprintf( 2150 '<a href="#comment-%1$s">%2$s</a>', 2151 get_comment_ID(), 2152 get_comment_author( $reply_to_id ) 2153 ); 2154 } else { 2155 $comment_author = get_comment_author( $reply_to_id ); 2156 } 2157 2158 printf( $reply_text, $comment_author ); 2159 } 2160 2161 /** 2162 * Gets the comment's reply to ID from the $_GET['replytocom']. 2163 * 2164 * @since 6.2.0 2165 * 2166 * @access private 2167 * 2168 * @param int|WP_Post|null $post The post the comment is being displayed for. 2169 * Defaults to the current global post. 2170 * @return int Comment's reply to ID. 2171 */ 2172 function _get_comment_reply_id( $post = null ) { 2173 $post = get_post( $post ); 2174 2175 if ( ! $post || ! isset( $_GET['replytocom'] ) || ! is_numeric( $_GET['replytocom'] ) ) { 2176 return 0; 2177 } 2178 2179 $reply_to_id = (int) $_GET['replytocom']; 2180 2181 /* 2182 * Validate the comment. 2183 * Bail out if it does not exist, is not approved, or its 2184 * `comment_post_ID` does not match the given post ID. 2185 */ 2186 $comment = get_comment( $reply_to_id ); 2187 2188 if ( 2189 ! $comment instanceof WP_Comment || 2190 0 === (int) $comment->comment_approved || 2191 $post->ID !== (int) $comment->comment_post_ID 2192 ) { 2193 return 0; 2194 } 2195 2196 return $reply_to_id; 2197 } 2198 2199 /** 2200 * Displays a list of comments. 2201 * 2202 * Used in the comments.php template to list comments for a particular post. 2203 * 2204 * @since 2.7.0 2205 * 2206 * @see WP_Query::$comments 2207 * 2208 * @global WP_Query $wp_query WordPress Query object. 2209 * @global int $comment_alt 2210 * @global int $comment_depth 2211 * @global int $comment_thread_alt 2212 * @global bool $overridden_cpage 2213 * @global bool $in_comment_loop 2214 * 2215 * @param string|array $args { 2216 * Optional. Formatting options. 2217 * 2218 * @type object $walker Instance of a Walker class to list comments. Default null. 2219 * @type int $max_depth The maximum comments depth. Default empty. 2220 * @type string $style The style of list ordering. Accepts 'ul', 'ol', or 'div'. 2221 * 'div' will result in no additional list markup. Default 'ul'. 2222 * @type callable $callback Callback function to use. Default null. 2223 * @type callable $end-callback Callback function to use at the end. Default null. 2224 * @type string $type Type of comments to list. Accepts 'all', 'comment', 2225 * 'pingback', 'trackback', 'pings'. Default 'all'. 2226 * @type int $page Page ID to list comments for. Default empty. 2227 * @type int $per_page Number of comments to list per page. Default empty. 2228 * @type int $avatar_size Height and width dimensions of the avatar size. Default 32. 2229 * @type bool $reverse_top_level Ordering of the listed comments. If true, will display 2230 * newest comments first. Default null. 2231 * @type bool $reverse_children Whether to reverse child comments in the list. Default null. 2232 * @type string $format How to format the comments list. Accepts 'html5', 'xhtml'. 2233 * Default 'html5' if the theme supports it. 2234 * @type bool $short_ping Whether to output short pings. Default false. 2235 * @type bool $echo Whether to echo the output or return it. Default true. 2236 * } 2237 * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Default null. 2238 * @return string|null|void HTML list of comments when 'echo' is false, null when there are 2239 * no comments to list. Nothing otherwise. 2240 * @phpstan-return ( 2241 * $args is array{ echo: false|0|''|'0', ... } 2242 * ? string|null 2243 * : ( $args is ''|'0'|array ? void : string|null ) 2244 * ) 2245 */ 2246 function wp_list_comments( $args = array(), $comments = null ) { 2247 global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop; 2248 2249 $in_comment_loop = true; 2250 2251 $comment_alt = 0; 2252 $comment_thread_alt = 0; 2253 $comment_depth = 1; 2254 2255 $defaults = array( 2256 'walker' => null, 2257 'max_depth' => '', 2258 'style' => 'ul', 2259 'callback' => null, 2260 'end-callback' => null, 2261 'type' => 'all', 2262 'page' => '', 2263 'per_page' => '', 2264 'avatar_size' => 32, 2265 'reverse_top_level' => null, 2266 'reverse_children' => '', 2267 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml', 2268 'short_ping' => false, 2269 'echo' => true, 2270 ); 2271 2272 $parsed_args = wp_parse_args( $args, $defaults ); 2273 2274 /** 2275 * Filters the arguments used in retrieving the comment list. 2276 * 2277 * @since 4.0.0 2278 * 2279 * @see wp_list_comments() 2280 * 2281 * @param array $parsed_args An array of arguments for displaying comments. 2282 */ 2283 $parsed_args = apply_filters( 'wp_list_comments_args', $parsed_args ); 2284 2285 // Figure out what comments we'll be looping through ($_comments). 2286 if ( null !== $comments ) { 2287 $comments = (array) $comments; 2288 if ( empty( $comments ) ) { 2289 return null; 2290 } 2291 if ( 'all' !== $parsed_args['type'] ) { 2292 $comments_by_type = separate_comments( $comments ); 2293 if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) { 2294 return null; 2295 } 2296 $_comments = $comments_by_type[ $parsed_args['type'] ]; 2297 } else { 2298 $_comments = $comments; 2299 } 2300 } else { 2301 /* 2302 * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query, 2303 * perform a separate comment query and allow Walker_Comment to paginate. 2304 */ 2305 if ( $parsed_args['page'] || $parsed_args['per_page'] ) { 2306 $current_cpage = (int) get_query_var( 'cpage' ); 2307 if ( ! $current_cpage ) { 2308 $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages; 2309 } 2310 2311 $current_per_page = (int) get_query_var( 'comments_per_page' ); 2312 if ( (int) $parsed_args['page'] !== $current_cpage || (int) $parsed_args['per_page'] !== $current_per_page ) { 2313 $comment_args = array( 2314 'post_id' => get_the_ID(), 2315 'orderby' => 'comment_date_gmt', 2316 'order' => 'ASC', 2317 'status' => 'approve', 2318 ); 2319 2320 if ( is_user_logged_in() ) { 2321 $comment_args['include_unapproved'] = array( get_current_user_id() ); 2322 } else { 2323 $unapproved_email = wp_get_unapproved_comment_author_email(); 2324 2325 if ( $unapproved_email ) { 2326 $comment_args['include_unapproved'] = array( $unapproved_email ); 2327 } 2328 } 2329 2330 $comments = get_comments( $comment_args ); 2331 2332 if ( 'all' !== $parsed_args['type'] ) { 2333 $comments_by_type = separate_comments( $comments ); 2334 if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) { 2335 return null; 2336 } 2337 2338 $_comments = $comments_by_type[ $parsed_args['type'] ]; 2339 } else { 2340 $_comments = $comments; 2341 } 2342 } 2343 2344 // Otherwise, fall back on the comments from `$wp_query->comments`. 2345 } else { 2346 if ( empty( $wp_query->comments ) ) { 2347 return null; 2348 } 2349 if ( 'all' !== $parsed_args['type'] ) { 2350 if ( empty( $wp_query->comments_by_type ) ) { 2351 $wp_query->comments_by_type = separate_comments( $wp_query->comments ); 2352 } 2353 if ( empty( $wp_query->comments_by_type[ $parsed_args['type'] ] ) ) { 2354 return null; 2355 } 2356 $_comments = $wp_query->comments_by_type[ $parsed_args['type'] ]; 2357 } else { 2358 $_comments = $wp_query->comments; 2359 } 2360 2361 if ( $wp_query->max_num_comment_pages ) { 2362 $default_comments_page = get_option( 'default_comments_page' ); 2363 $cpage = (int) get_query_var( 'cpage' ); 2364 2365 if ( 'newest' === $default_comments_page ) { 2366 $parsed_args['cpage'] = $cpage; 2367 } elseif ( 1 === $cpage ) { 2368 /* 2369 * When the first page shows the oldest comments, 2370 * post permalink is the same as the comment permalink. 2371 */ 2372 $parsed_args['cpage'] = ''; 2373 } else { 2374 $parsed_args['cpage'] = $cpage; 2375 } 2376 2377 $parsed_args['page'] = 0; 2378 $parsed_args['per_page'] = 0; 2379 } 2380 } 2381 } 2382 2383 if ( '' === $parsed_args['per_page'] && get_option( 'page_comments' ) ) { 2384 $parsed_args['per_page'] = get_query_var( 'comments_per_page' ); 2385 } 2386 2387 if ( empty( $parsed_args['per_page'] ) ) { 2388 $parsed_args['per_page'] = 0; 2389 $parsed_args['page'] = 0; 2390 } 2391 2392 if ( '' === $parsed_args['max_depth'] ) { 2393 if ( get_option( 'thread_comments' ) ) { 2394 $parsed_args['max_depth'] = get_option( 'thread_comments_depth' ); 2395 } else { 2396 $parsed_args['max_depth'] = -1; 2397 } 2398 } 2399 2400 if ( '' === $parsed_args['page'] ) { 2401 if ( empty( $overridden_cpage ) ) { 2402 $parsed_args['page'] = get_query_var( 'cpage' ); 2403 } else { 2404 $threaded = ( -1 !== (int) $parsed_args['max_depth'] ); 2405 $parsed_args['page'] = ( 'newest' === get_option( 'default_comments_page' ) ) ? get_comment_pages_count( $_comments, $parsed_args['per_page'], $threaded ) : 1; 2406 set_query_var( 'cpage', $parsed_args['page'] ); 2407 } 2408 } 2409 2410 // Validation check. 2411 $parsed_args['page'] = (int) $parsed_args['page']; 2412 $parsed_args['per_page'] = (int) $parsed_args['per_page']; 2413 if ( 0 === $parsed_args['page'] && 0 !== $parsed_args['per_page'] ) { 2414 $parsed_args['page'] = 1; 2415 } 2416 2417 if ( null === $parsed_args['reverse_top_level'] ) { 2418 $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) ); 2419 } 2420 2421 if ( empty( $parsed_args['walker'] ) ) { 2422 $walker = new Walker_Comment(); 2423 } else { 2424 $walker = $parsed_args['walker']; 2425 } 2426 2427 $output = $walker->paged_walk( $_comments, $parsed_args['max_depth'], $parsed_args['page'], $parsed_args['per_page'], $parsed_args ); 2428 2429 $in_comment_loop = false; 2430 2431 if ( $parsed_args['echo'] ) { 2432 echo $output; 2433 } else { 2434 return $output; 2435 } 2436 } 2437 2438 /** 2439 * Outputs a complete commenting form for use within a template. 2440 * 2441 * Most strings and form fields may be controlled through the `$args` array passed 2442 * into the function, while you may also choose to use the {@see 'comment_form_default_fields'} 2443 * filter to modify the array of default fields if you'd just like to add a new 2444 * one or remove a single field. All fields are also individually passed through 2445 * a filter of the {@see 'comment_form_field_$name'} where `$name` is the key used 2446 * in the array of fields. 2447 * 2448 * @since 3.0.0 2449 * @since 4.1.0 Introduced the 'class_submit' argument. 2450 * @since 4.2.0 Introduced the 'submit_button' and 'submit_fields' arguments. 2451 * @since 4.4.0 Introduced the 'class_form', 'title_reply_before', 'title_reply_after', 2452 * 'cancel_reply_before', and 'cancel_reply_after' arguments. 2453 * @since 4.5.0 The 'author', 'email', and 'url' form fields are limited to 245, 100, 2454 * and 200 characters, respectively. 2455 * @since 4.6.0 Introduced the 'action' argument. 2456 * @since 4.9.6 Introduced the 'cookies' default comment field. 2457 * @since 5.5.0 Introduced the 'class_container' argument. 2458 * @since 6.8.2 Introduced the 'novalidate' argument. 2459 * 2460 * @param array $args { 2461 * Optional. Default arguments and form fields to override. 2462 * 2463 * @type array $fields { 2464 * Default comment fields, filterable by default via the {@see 'comment_form_default_fields'} hook. 2465 * 2466 * @type string $author Comment author field HTML. 2467 * @type string $email Comment author email field HTML. 2468 * @type string $url Comment author URL field HTML. 2469 * @type string $cookies Comment cookie opt-in field HTML. 2470 * } 2471 * @type string $comment_field The comment textarea field HTML. 2472 * @type string $must_log_in HTML element for a 'must be logged in to comment' message. 2473 * @type string $logged_in_as The HTML for the 'logged in as [user]' message, the Edit profile link, 2474 * and the Log out link. 2475 * @type string $comment_notes_before HTML element for a message displayed before the comment fields 2476 * if the user is not logged in. 2477 * Default 'Your email address will not be published.'. 2478 * @type string $comment_notes_after HTML element for a message displayed after the textarea field. 2479 * @type string $action The comment form element action attribute. Default '/wp-comments-post.php'. 2480 * @type bool $novalidate Whether the novalidate attribute is added to the comment form. Default false. 2481 * @type string $id_form The comment form element id attribute. Default 'commentform'. 2482 * @type string $id_submit The comment submit element id attribute. Default 'submit'. 2483 * @type string $class_container The comment form container class attribute. Default 'comment-respond'. 2484 * @type string $class_form The comment form element class attribute. Default 'comment-form'. 2485 * @type string $class_submit The comment submit element class attribute. Default 'submit'. 2486 * @type string $name_submit The comment submit element name attribute. Default 'submit'. 2487 * @type string $title_reply The translatable 'reply' button label. Default 'Leave a Reply'. 2488 * @type string $title_reply_to The translatable 'reply-to' button label. Default 'Leave a Reply to %s', 2489 * where %s is the author of the comment being replied to. 2490 * @type string $title_reply_before HTML displayed before the comment form title. 2491 * Default: '<h3 id="reply-title" class="comment-reply-title">'. 2492 * @type string $title_reply_after HTML displayed after the comment form title. 2493 * Default: '</h3>'. 2494 * @type string $cancel_reply_before HTML displayed before the cancel reply link. 2495 * @type string $cancel_reply_after HTML displayed after the cancel reply link. 2496 * @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'. 2497 * @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'. 2498 * @type string $submit_button HTML format for the Submit button. 2499 * Default: '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />'. 2500 * @type string $submit_field HTML format for the markup surrounding the Submit button and comment hidden 2501 * fields. Default: '<p class="form-submit">%1$s %2$s</p>', where %1$s is the 2502 * submit button markup and %2$s is the comment hidden fields. 2503 * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'. 2504 * } 2505 * @param int|WP_Post $post Optional. Post ID or WP_Post object to generate the form for. Default current post. 2506 */ 2507 function comment_form( $args = array(), $post = null ) { 2508 $post = get_post( $post ); 2509 2510 // Exit the function if the post is invalid or comments are closed. 2511 if ( ! $post || ! comments_open( $post ) ) { 2512 /** 2513 * Fires after the comment form if comments are closed. 2514 * 2515 * For backward compatibility, this action also fires if comment_form() 2516 * is called with an invalid post object or ID. 2517 * 2518 * @since 3.0.0 2519 */ 2520 do_action( 'comment_form_comments_closed' ); 2521 2522 return; 2523 } 2524 2525 $post_id = $post->ID; 2526 $commenter = wp_get_current_commenter(); 2527 $user = wp_get_current_user(); 2528 $user_identity = $user->exists() ? $user->display_name : ''; 2529 2530 $args = wp_parse_args( $args ); 2531 if ( ! isset( $args['format'] ) ) { 2532 $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml'; 2533 } 2534 2535 $req = get_option( 'require_name_email' ); 2536 $html5 = 'html5' === $args['format']; 2537 2538 // Define attributes in HTML5 or XHTML syntax. 2539 $required_attribute = ( $html5 ? ' required' : ' required="required"' ); 2540 $checked_attribute = ( $html5 ? ' checked' : ' checked="checked"' ); 2541 2542 // Identify required fields visually and create a message about the indicator. 2543 $required_indicator = ' ' . wp_required_field_indicator(); 2544 $required_text = ' ' . wp_required_field_message(); 2545 2546 $fields = array( 2547 'author' => sprintf( 2548 '<p class="comment-form-author">%s %s</p>', 2549 sprintf( 2550 '<label for="author">%s%s</label>', 2551 __( 'Name' ), 2552 ( $req ? $required_indicator : '' ) 2553 ), 2554 sprintf( 2555 '<input id="author" name="author" type="text" value="%s" size="30" maxlength="245" autocomplete="name"%s />', 2556 esc_attr( $commenter['comment_author'] ), 2557 ( $req ? $required_attribute : '' ) 2558 ) 2559 ), 2560 'email' => sprintf( 2561 '<p class="comment-form-email">%s %s</p>', 2562 sprintf( 2563 '<label for="email">%s%s</label>', 2564 __( 'Email' ), 2565 ( $req ? $required_indicator : '' ) 2566 ), 2567 sprintf( 2568 '<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes" autocomplete="email"%s />', 2569 ( $html5 ? 'type="email"' : 'type="text"' ), 2570 esc_attr( $commenter['comment_author_email'] ), 2571 ( $req ? $required_attribute : '' ) 2572 ) 2573 ), 2574 'url' => sprintf( 2575 '<p class="comment-form-url">%s %s</p>', 2576 sprintf( 2577 '<label for="url">%s</label>', 2578 __( 'Website' ) 2579 ), 2580 sprintf( 2581 '<input id="url" name="url" %s value="%s" size="30" maxlength="200" autocomplete="url" />', 2582 ( $html5 ? 'type="url"' : 'type="text"' ), 2583 esc_attr( $commenter['comment_author_url'] ) 2584 ) 2585 ), 2586 ); 2587 2588 if ( has_action( 'set_comment_cookies', 'wp_set_comment_cookies' ) && get_option( 'show_comments_cookies_opt_in' ) ) { 2589 $consent = empty( $commenter['comment_author_email'] ) ? '' : $checked_attribute; 2590 2591 $fields['cookies'] = sprintf( 2592 '<p class="comment-form-cookies-consent">%s %s</p>', 2593 sprintf( 2594 '<input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"%s />', 2595 $consent 2596 ), 2597 sprintf( 2598 '<label for="wp-comment-cookies-consent">%s</label>', 2599 __( 'Save my name, email, and website in this browser for the next time I comment.' ) 2600 ) 2601 ); 2602 2603 // Ensure that the passed fields include cookies consent. 2604 if ( isset( $args['fields'] ) && ! isset( $args['fields']['cookies'] ) ) { 2605 $args['fields']['cookies'] = $fields['cookies']; 2606 } 2607 } 2608 2609 $original_fields = $fields; 2610 2611 /** 2612 * Filters the default comment form fields. 2613 * 2614 * @since 3.0.0 2615 * 2616 * @param string[] $fields Array of the default comment fields. 2617 */ 2618 $fields = apply_filters( 'comment_form_default_fields', $fields ); 2619 2620 $defaults = array( 2621 'fields' => $fields, 2622 'comment_field' => sprintf( 2623 '<p class="comment-form-comment">%s %s</p>', 2624 sprintf( 2625 '<label for="comment">%s%s</label>', 2626 _x( 'Comment', 'noun' ), 2627 $required_indicator 2628 ), 2629 '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525"' . $required_attribute . '></textarea>' 2630 ), 2631 'must_log_in' => sprintf( 2632 '<p class="must-log-in">%s</p>', 2633 sprintf( 2634 /* translators: %s: Login URL. */ 2635 __( 'You must be <a href="%s">logged in</a> to post a comment.' ), 2636 /** This filter is documented in wp-includes/link-template.php */ 2637 wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) ) 2638 ) 2639 ), 2640 'logged_in_as' => sprintf( 2641 '<p class="logged-in-as">%s%s</p>', 2642 sprintf( 2643 /* translators: 1: User name, 2: Edit user link, 3: Logout URL. */ 2644 __( 'Logged in as %1$s. <a href="%2$s">Edit your profile</a>. <a href="%3$s">Log out?</a>' ), 2645 $user_identity, 2646 get_edit_user_link(), 2647 /** This filter is documented in wp-includes/link-template.php */ 2648 wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) ) 2649 ), 2650 $required_text 2651 ), 2652 'comment_notes_before' => sprintf( 2653 '<p class="comment-notes">%s%s</p>', 2654 sprintf( 2655 '<span id="email-notes">%s</span>', 2656 __( 'Your email address will not be published.' ) 2657 ), 2658 $required_text 2659 ), 2660 'comment_notes_after' => '', 2661 'action' => site_url( '/wp-comments-post.php' ), 2662 'novalidate' => false, 2663 'id_form' => 'commentform', 2664 'id_submit' => 'submit', 2665 'class_container' => 'comment-respond', 2666 'class_form' => 'comment-form', 2667 'class_submit' => 'submit', 2668 'name_submit' => 'submit', 2669 'title_reply' => __( 'Leave a Reply' ), 2670 /* translators: %s: Author of the comment being replied to. */ 2671 'title_reply_to' => __( 'Leave a Reply to %s' ), 2672 'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title">', 2673 'title_reply_after' => '</h3>', 2674 'cancel_reply_before' => ' <small>', 2675 'cancel_reply_after' => '</small>', 2676 'cancel_reply_link' => __( 'Cancel reply' ), 2677 'label_submit' => __( 'Post Comment' ), 2678 'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />', 2679 'submit_field' => '<p class="form-submit">%1$s %2$s</p>', 2680 'format' => 'xhtml', 2681 ); 2682 2683 /** 2684 * Filters the comment form default arguments. 2685 * 2686 * Use {@see 'comment_form_default_fields'} to filter the comment fields. 2687 * 2688 * @since 3.0.0 2689 * 2690 * @param array $defaults The default comment form arguments. 2691 */ 2692 $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) ); 2693 2694 // Ensure that the filtered arguments contain all required default values. 2695 $args = array_merge( $defaults, $args ); 2696 2697 // Remove `aria-describedby` from the email field if there's no associated description. 2698 if ( isset( $args['fields']['email'] ) && ! str_contains( $args['comment_notes_before'], 'id="email-notes"' ) ) { 2699 $args['fields']['email'] = str_replace( 2700 ' aria-describedby="email-notes"', 2701 '', 2702 $args['fields']['email'] 2703 ); 2704 } 2705 2706 /** 2707 * Fires before the comment form. 2708 * 2709 * @since 3.0.0 2710 */ 2711 do_action( 'comment_form_before' ); 2712 ?> 2713 <div id="respond" class="<?php echo esc_attr( $args['class_container'] ); ?>"> 2714 <?php 2715 echo $args['title_reply_before']; 2716 2717 comment_form_title( $args['title_reply'], $args['title_reply_to'], true, $post_id ); 2718 2719 if ( get_option( 'thread_comments' ) ) { 2720 echo $args['cancel_reply_before']; 2721 2722 cancel_comment_reply_link( $args['cancel_reply_link'] ); 2723 2724 echo $args['cancel_reply_after']; 2725 } 2726 2727 echo $args['title_reply_after']; 2728 2729 if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) : 2730 2731 echo $args['must_log_in']; 2732 /** 2733 * Fires after the HTML-formatted 'must log in after' message in the comment form. 2734 * 2735 * @since 3.0.0 2736 */ 2737 do_action( 'comment_form_must_log_in_after' ); 2738 2739 else : 2740 2741 printf( 2742 '<form action="%s" method="post" id="%s" class="%s"%s>', 2743 esc_url( $args['action'] ), 2744 esc_attr( $args['id_form'] ), 2745 esc_attr( $args['class_form'] ), 2746 ( $args['novalidate'] ? ' novalidate' : '' ) 2747 ); 2748 2749 /** 2750 * Fires at the top of the comment form, inside the form tag. 2751 * 2752 * @since 3.0.0 2753 */ 2754 do_action( 'comment_form_top' ); 2755 2756 if ( is_user_logged_in() ) : 2757 2758 /** 2759 * Filters the 'logged in' message for the comment form for display. 2760 * 2761 * @since 3.0.0 2762 * 2763 * @param string $args_logged_in The HTML for the 'logged in as [user]' message, 2764 * the Edit profile link, and the Log out link. 2765 * @param array $commenter An array containing the comment author's 2766 * username, email, and URL. 2767 * @param string $user_identity If the commenter is a registered user, 2768 * the display name, blank otherwise. 2769 */ 2770 echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); 2771 2772 /** 2773 * Fires after the is_user_logged_in() check in the comment form. 2774 * 2775 * @since 3.0.0 2776 * 2777 * @param array $commenter An array containing the comment author's 2778 * username, email, and URL. 2779 * @param string $user_identity If the commenter is a registered user, 2780 * the display name, blank otherwise. 2781 */ 2782 do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); 2783 2784 else : 2785 2786 echo $args['comment_notes_before']; 2787 2788 endif; 2789 2790 // Prepare an array of all fields, including the textarea. 2791 $comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields']; 2792 2793 /** 2794 * Filters the comment form fields, including the textarea. 2795 * 2796 * @since 4.4.0 2797 * 2798 * @param array $comment_fields The comment fields. 2799 */ 2800 $comment_fields = apply_filters( 'comment_form_fields', $comment_fields ); 2801 2802 // Get an array of field names, excluding the textarea. 2803 $comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) ); 2804 2805 // Get the first and the last field name, excluding the textarea. 2806 $first_field = reset( $comment_field_keys ); 2807 $last_field = end( $comment_field_keys ); 2808 2809 foreach ( $comment_fields as $name => $field ) { 2810 2811 if ( 'comment' === $name ) { 2812 2813 /** 2814 * Filters the content of the comment textarea field for display. 2815 * 2816 * @since 3.0.0 2817 * 2818 * @param string $args_comment_field The content of the comment textarea field. 2819 */ 2820 echo apply_filters( 'comment_form_field_comment', $field ); 2821 2822 echo $args['comment_notes_after']; 2823 2824 } elseif ( ! is_user_logged_in() || ! isset( $original_fields[ $name ] ) ) { 2825 2826 if ( $first_field === $name ) { 2827 /** 2828 * Fires before the comment fields in the comment form, excluding the textarea. 2829 * 2830 * @since 3.0.0 2831 */ 2832 do_action( 'comment_form_before_fields' ); 2833 } 2834 2835 /** 2836 * Filters a comment form field for display. 2837 * 2838 * The dynamic portion of the hook name, `$name`, refers to the name 2839 * of the comment form field. 2840 * 2841 * Possible hook names include: 2842 * 2843 * - `comment_form_field_comment` 2844 * - `comment_form_field_author` 2845 * - `comment_form_field_email` 2846 * - `comment_form_field_url` 2847 * - `comment_form_field_cookies` 2848 * 2849 * @since 3.0.0 2850 * 2851 * @param string $field The HTML-formatted output of the comment form field. 2852 */ 2853 echo apply_filters( "comment_form_field_{$name}", $field ) . "\n"; 2854 2855 if ( $last_field === $name ) { 2856 /** 2857 * Fires after the comment fields in the comment form, excluding the textarea. 2858 * 2859 * @since 3.0.0 2860 */ 2861 do_action( 'comment_form_after_fields' ); 2862 } 2863 } 2864 } 2865 2866 $submit_button = sprintf( 2867 $args['submit_button'], 2868 esc_attr( $args['name_submit'] ), 2869 esc_attr( $args['id_submit'] ), 2870 esc_attr( $args['class_submit'] ), 2871 esc_attr( $args['label_submit'] ) 2872 ); 2873 2874 /** 2875 * Filters the submit button for the comment form to display. 2876 * 2877 * @since 4.2.0 2878 * 2879 * @param string $submit_button HTML markup for the submit button. 2880 * @param array $args Arguments passed to comment_form(). 2881 */ 2882 $submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args ); 2883 2884 $submit_field = sprintf( 2885 $args['submit_field'], 2886 $submit_button, 2887 get_comment_id_fields( $post_id ) 2888 ); 2889 2890 /** 2891 * Filters the submit field for the comment form to display. 2892 * 2893 * The submit field includes the submit button, hidden fields for the 2894 * comment form, and any wrapper markup. 2895 * 2896 * @since 4.2.0 2897 * 2898 * @param string $submit_field HTML markup for the submit field. 2899 * @param array $args Arguments passed to comment_form(). 2900 */ 2901 echo apply_filters( 'comment_form_submit_field', $submit_field, $args ); 2902 2903 /** 2904 * Fires at the bottom of the comment form, inside the closing form tag. 2905 * 2906 * @since 1.5.0 2907 * 2908 * @param int $post_id The post ID. 2909 */ 2910 do_action( 'comment_form', $post_id ); 2911 2912 echo '</form>'; 2913 2914 endif; 2915 ?> 2916 </div><!-- #respond --> 2917 <?php 2918 2919 /** 2920 * Fires after the comment form. 2921 * 2922 * @since 3.0.0 2923 */ 2924 do_action( 'comment_form_after' ); 2925 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Sep 15 08:20:32 2026 | Cross-referenced by PHPXref |