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