Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions newspack-theme/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}

$discussion = newspack_get_discussion_data();
$collapse_comments = get_theme_mod( 'collapse_comments', false );
$collapse_comments = get_theme_mod( 'collapse_comments', '' );
$on_first_page = true;
$comments_collapsed = false;
$url_end = '';
Expand All @@ -34,9 +34,14 @@
$on_first_page = false;
}

// Collapse comments if that's set, if there's more than one, and if we're on the first page:
if ( $collapse_comments && 1 < (int) $discussion->responses && $on_first_page ) {
$comments_collapsed = true;
// Collapse comments based on the setting and if we're on the first page:
if ( $collapse_comments && $on_first_page ) {
if ( 'always' === $collapse_comments ) {
$comments_collapsed = true;
} elseif ( ( true === $collapse_comments || 'more_than_one' === $collapse_comments ) && 1 < (int) $discussion->responses ) {
// Backward compat: boolean true treated same as 'more_than_one'.
$comments_collapsed = true;
}
}
?>

Expand Down Expand Up @@ -154,8 +159,20 @@
else :

// Show comment form.
newspack_comment_form( true );
if ( $comments_collapsed ) :
?>
<div id="comments-wrapper" class="comments-wrapper comments-hide" [class]="showComments ? 'comments-wrapper' : 'comments-wrapper comments-hide'">
<?php endif; ?>

<?php newspack_comment_form( true ); ?>

<?php if ( $comments_collapsed ) : ?>
</div><!-- .comments-wrapper -->
<button class="comments-toggle" id="comments-toggle" on="tap:AMP.setState({showComments: !showComments})">
<?php echo wp_kses( newspack_get_icon_svg( 'chevron_left', 24 ), newspack_sanitize_svgs() ); ?><span [text]="showComments ? '<?php esc_html_e( 'Collapse comments', 'newspack-theme' ); ?>' : '<?php esc_html_e( 'Expand comments', 'newspack-theme' ); ?>'"><?php esc_html_e( 'Expand comments', 'newspack-theme' ); ?></span>
</button>
<?php endif; ?>

<?php endif; // End have_comments check. ?>

endif; // End have_comments check.
?>
</div><!-- #comments -->
27 changes: 23 additions & 4 deletions newspack-theme/inc/customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1205,17 +1205,22 @@ function newspack_customize_register( $wp_customize ) {
$wp_customize->add_setting(
'collapse_comments',
array(
'default' => false,
'sanitize_callback' => 'newspack_sanitize_checkbox',
'default' => '',
'sanitize_callback' => 'newspack_sanitize_select',
)
);
$wp_customize->add_control(
'collapse_comments',
array(
'type' => 'checkbox',
'type' => 'select',
'label' => esc_html__( 'Collapse Comments', 'newspack-theme' ),
'description' => esc_html__( 'When using WordPress\'s default comments, checking this option will collapse the comments section when there is more than one comment, and display a button to expand.', 'newspack-theme' ),
'description' => esc_html__( 'When using WordPress\'s default comments, choose when to collapse the comments section and display a button to expand.', 'newspack-theme' ),
'section' => 'comments_options',
'choices' => array(
'' => esc_html__( 'Don\'t collapse', 'newspack-theme' ),
'more_than_one' => esc_html__( 'Collapse when more than one comment', 'newspack-theme' ),
'always' => esc_html__( 'Always collapse', 'newspack-theme' ),
),
)
);

Expand Down Expand Up @@ -1628,6 +1633,20 @@ function newspack_sanitize_checkbox( $input ) {
}
}

/**
* Sanitize select input.
*
* @param string $input Value to sanitize.
* @param object $setting Setting instance.
*
* @return string Sanitized value.
*/
function newspack_sanitize_select( $input, $setting ) {
$input = sanitize_key( $input );
$choices = $setting->manager->get_control( $setting->id )->choices;
return ( array_key_exists( $input, $choices ) ) ? $input : $setting->default;
}

/**
* Sanitize author avatar image size.
*
Expand Down
Loading