| 1 |
<?php |
| 2 |
/** |
| 3 |
* Comments functions |
| 4 |
* |
| 5 |
* Theme-side tweaks to the WordPress comment form and comment output: |
| 6 |
* - Re-labels the comment form title with a live approved-comment count. |
| 7 |
* - Swaps the submit button's CSS class to the theme's outline-button style. |
| 8 |
* - Forces links inside comment text to open in a new, safely-rel'd tab. |
| 9 |
* |
| 10 |
* All three are registered as filters on core comment hooks. |
| 11 |
* |
| 12 |
* @package wp-stream |
| 13 |
*/ |
| 14 |
|
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
// Replace the comment form title with one that includes the comment count. |
| 22 |
add_filter( 'comment_form_defaults', 'wpstream_theme_custom_comment_title', 20 ); |
| 23 |
|
| 24 |
if ( ! function_exists( 'wpstream_theme_custom_comment_title' ) ) { |
| 25 |
/** |
| 26 |
* Customizes the comment section title to display the total number of comments. |
| 27 |
* |
| 28 |
* This function modifies the title of the comment section to display the total number |
| 29 |
* of approved comments for the current post. |
| 30 |
* |
| 31 |
* @param array $defaults The default comment section arguments. |
| 32 |
* @return array The modified comment section arguments. |
| 33 |
*/ |
| 34 |
function wpstream_theme_custom_comment_title( $defaults ) { |
| 35 |
// Current post whose comments the form belongs to. |
| 36 |
$post_id = get_the_ID(); |
| 37 |
// Fetch the comment tallies (approved, pending, spam, etc.) for this post. |
| 38 |
$comments_count = wp_count_comments( $post_id ); |
| 39 |
// We only surface the approved-comment count in the title. |
| 40 |
$total_comments = $comments_count->approved; |
| 41 |
// translators: %d is replaced with the total number of comments. |
| 42 |
// Build the "Comments (N)" heading. |
| 43 |
$title = sprintf( __( 'Comments (%d)', 'hello-wpstream' ), $total_comments ); |
| 44 |
|
| 45 |
// Override the default reply/title text shown above the comment form. |
| 46 |
$defaults['title_reply'] = $title; |
| 47 |
|
| 48 |
// Return the (possibly) modified defaults back to WordPress. |
| 49 |
return $defaults; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
// Restyle the comment form's submit button via its CSS class. |
| 55 |
add_filter( 'comment_form_defaults', 'wpstream_theme_comment_button' ); |
| 56 |
|
| 57 |
if ( ! function_exists( 'wpstream_theme_comment_button' ) ) { |
| 58 |
/** |
| 59 |
* Modify the class of the comment submit button. |
| 60 |
* |
| 61 |
* @param array $args The arguments for the comment form submit button. |
| 62 |
* @return array Modified arguments for the comment form submit button. |
| 63 |
*/ |
| 64 |
function wpstream_theme_comment_button( $args ) { |
| 65 |
// Apply the theme's outline-button styling to the submit control. |
| 66 |
$args['class_submit'] = 'btn-outline'; // since WP 4.1. |
| 67 |
// Hand the adjusted args back to the comment form. |
| 68 |
return $args; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Rewrite anchors in rendered comment text so they open in a new tab. |
| 73 |
add_filter( 'comment_text', 'wpstream_bs_comment_links_in_new_tab' ); |
| 74 |
|
| 75 |
if ( ! function_exists( 'wpstream_bs_comment_links_in_new_tab' ) ) { |
| 76 |
/** |
| 77 |
* Open comment author links in new tab. |
| 78 |
* |
| 79 |
* @param string $text The comment text. |
| 80 |
* @return string Modified comment text with links opened in new tab. |
| 81 |
*/ |
| 82 |
function wpstream_bs_comment_links_in_new_tab( $text ) { |
| 83 |
// Inject target/rel attributes on every opening anchor tag in the text. |
| 84 |
// nofollow noopener noreferrer keeps outbound comment links safe. |
| 85 |
return str_replace( '<a', '<a target="_blank" rel="nofollow noopener noreferrer"', $text ); |
| 86 |
} |
| 87 |
} |
| 88 |
|