This repository has been archived on 2020-05-08. You can view files and clone it, but cannot push or open issues or pull requests.
understrap/inc/custom-comments.php

107 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2016-11-21 16:25:56 +00:00
/**
* Comment layout
2016-11-21 16:25:56 +00:00
*
* @package understrap
*/
2019-06-20 08:57:12 +00:00
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
2018-09-10 21:59:04 +00:00
// Add Bootstrap classes to comment form fields.
2017-11-27 08:02:16 +00:00
add_filter( 'comment_form_default_fields', 'understrap_bootstrap_comment_form_fields' );
if ( ! function_exists( 'understrap_bootstrap_comment_form_fields' ) ) {
/**
* Add Bootstrap classes to WP's comment form default fields.
*
* @param array $fields {
* Default comment fields.
*
* @type string $author Comment author field HTML.
* @type string $email Comment author email field HTML.
* @type string $url Comment author URL field HTML.
* @type string $cookies Comment cookie opt-in field HTML.
* }
*
* @return array
*/
function understrap_bootstrap_comment_form_fields( $fields ) {
$replace = array(
'<p class="' => '<div class="form-group ',
'<input' => '<input class="form-control" ',
'</p>' => '</div>',
);
if ( isset( $fields['author'] ) ) {
$fields['author'] = strtr( $fields['author'], $replace );
}
if ( isset( $fields['email'] ) ) {
$fields['email'] = strtr( $fields['email'], $replace );
}
if ( isset( $fields['url'] ) ) {
$fields['url'] = strtr( $fields['url'], $replace );
}
$replace = array(
'<p class="' => '<div class="form-group form-check ',
'<input' => '<input class="form-check-input" ',
'<label' => '<label class="form-check-label" ',
'</p>' => '</div>',
);
if ( isset( $fields['cookies'] ) ) {
$fields['cookies'] = strtr( $fields['cookies'], $replace );
}
return $fields;
}
} // End of if function_exists( 'understrap_bootstrap_comment_form_fields' )
2016-11-21 16:25:56 +00:00
// Add Bootstrap classes to comment form submit button and comment field.
2017-11-27 08:02:16 +00:00
add_filter( 'comment_form_defaults', 'understrap_bootstrap_comment_form' );
2016-11-21 16:25:56 +00:00
if ( ! function_exists( 'understrap_bootstrap_comment_form' ) ) {
/**
2020-04-18 22:34:08 +00:00
* Adds Bootstrap classes to comment form submit button and comment field.
*
* @param string[] $args Comment form arguments and fields.
*
* @return string[]
*/
function understrap_bootstrap_comment_form( $args ) {
$replace = array(
'<p class="' => '<div class="form-group ',
'<textarea' => '<textarea class="form-control" ',
'</p>' => '</div>',
);
if ( isset( $args['comment_field'] ) ) {
$args['comment_field'] = strtr( $args['comment_field'], $replace );
}
if ( isset( $args['class_submit'] ) ) {
$args['class_submit'] = 'btn btn-secondary';
}
return $args;
}
} // End of if function_exists( 'understrap_bootstrap_comment_form' ).
// Add note if comments are closed.
add_action( 'comment_form_comments_closed', 'understrap_comment_form_comments_closed' );
if ( ! function_exists( 'understrap_comment_form_comments_closed' ) ) {
/**
* Displays a note that comments are closed if comments are closed and there are comments.
*/
function understrap_comment_form_comments_closed() {
if ( get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) {
?>
<p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'understrap' ); ?></p>
<?php
}
}
} // End of if function_exists( 'understrap_comment_form_comments_closed' ).