Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active June 5, 2018 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/8d9e750f88d4bed86ee2ea3b5dafab43 to your computer and use it in GitHub Desktop.
Save westonruter/8d9e750f88d4bed86ee2ea3b5dafab43 to your computer and use it in GitHub Desktop.
<?php
/**
* The template for displaying comments
*
* The area of the page that contains both current comments
* and the comment form.
*
* @package Twenty_Fifteen_AMP
*/
// Include regular comments template if not AMP.
if ( ! twentyfifteen_is_amp() ) {
require trailingslashit( get_template_directory() ) . basename( __FILE__ );
return;
}
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
?>
<div id="comments" class="comments-area">
<?php if ( have_comments() ) : ?>
<h2 class="comments-title">
<?php
$comments_number = get_comments_number();
if ( '1' === $comments_number ) {
/* translators: %s: post title */
echo esc_html( sprintf( _x( 'One thought on &ldquo;%s&rdquo;', 'comments title', 'twentyfifteen' ), get_the_title() ) );
} else {
echo esc_html( sprintf(
/* translators: 1: number of comments, 2: post title */
_nx(
'%1$s thought on &ldquo;%2$s&rdquo;',
'%1$s thoughts on &ldquo;%2$s&rdquo;',
$comments_number,
'comments title',
'twentyfifteen'
),
number_format_i18n( $comments_number ),
get_the_title()
) );
}
?>
</h2>
<?php twentyfifteen_comment_nav(); ?>
<amp-live-list
id="amp-live-comments-list-<?php the_ID(); ?>"
<?php echo ( 'asc' === get_option( 'comment_order' ) ) ? ' sort="ascending" ' : ''; ?>
data-poll-interval="<?php echo esc_attr( 15000 ); ?>"
data-max-items-per-page="<?php echo esc_attr( get_option( 'page_comments' ) ? get_option( 'comments_per_page' ) : 10000 ); ?>"
>
<ol items class="comment-list">
<?php
wp_list_comments( array(
'style' => 'ol',
'short_ping' => true,
'avatar_size' => 56,
) );
?>
</ol><!-- .comment-list -->
<?php twentyfifteen_comment_nav( array( 'is_amp_live_list_pagination' => true ) ); ?>
<p update>
<button class="button" on="tap:amp-live-comments-list-<?php the_ID(); ?>.update"><?php esc_html_e( 'New comment(s)', '_s' ); ?></button>
</p>
</amp-live-list>
<?php endif; // End of have_comments() check. ?>
<?php if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) : ?>
<p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'twentyfifteen' ); ?></p>
<?php endif; ?>
<?php comment_form(); ?>
</div><!-- .comments-area -->
<?php
/**
* Twenty Fifteen AMP Functions.
*
* @package Twenty_Fifteen_AMP
*/
/**
* Return whether AMP endpoint.
*
* @return bool Whether AMP.
*/
function twentyfifteen_is_amp() {
return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
}
/**
* Determine whether the sidebar (secondary) is present.
*
* This replaces the conditional that was in the parent theme's sidebar.php.
*
* @return bool Whether sidebar is present.
*/
function twentyfifteen_has_secondary_sidebar() {
return has_nav_menu( 'primary' ) || has_nav_menu( 'social' ) || is_active_sidebar( 'sidebar-1' );
}
/**
* Add pagination reference point attribute for amp-live-list when theme supports AMP.
*
* This is used by the navigation_markup_template filter in the comments template.
*
* @link https://www.ampproject.org/docs/reference/components/amp-live-list#pagination
* @todo AMP should allow nav instead of element with [pagination] attribute.
*
* @param string $markup Navigation markup.
* @return string Markup.
*/
function twentyfifteen_add_amp_live_list_pagination_attribute( $markup ) {
return preg_replace( '/(\s*<[a-z0-9_-]+)/i', '$1 pagination ', $markup, 1 );
}
/**
* Display navigation to next/previous comments when applicable.
*
* As of WordPress 4.4 we can now use `the_comments_navigation()` here.
*
* @todo AMP should allow a nav in place of element with [pagination] attribute.
* @param array $args {
* Args.
*
* @type bool $is_amp_live_list_pagination Is the pagination for amp-live-list.
* }
*/
function twentyfifteen_comment_nav( $args = array( 'is_amp_live_list_pagination' => false ) ) {
if ( ! empty( $args['is_amp_live_list_pagination'] ) ) {
add_filter( 'navigation_markup_template', 'twentyfifteen_add_amp_live_list_pagination_attribute' );
}
the_comments_navigation();
if ( ! empty( $args['is_amp_live_list_pagination'] ) ) {
remove_filter( 'navigation_markup_template', 'twentyfifteen_add_amp_live_list_pagination_attribute' );
}
}
/**
* Add AMP theme support.
*/
function twentyfifteen_amp_after_setup_theme() {
if ( defined( 'AMP__VERSION' ) && version_compare( strtok( AMP__VERSION, '-' ), '1.0' ) ) {
add_action( 'admin_notices', 'twentyfifteen_amp_version_required_admin_notoce' );
return;
}
add_post_type_support( 'page', 'amp' );
// To opt-in for paired mode instead of Native AMP, add 'template_dir' => './' to the following amp theme support args.
add_theme_support( 'amp', array(
'comments_live_list' => true,
) );
}
add_action( 'after_setup_theme', 'twentyfifteen_amp_after_setup_theme' );
/**
* Show notice when the required AMP version is not installed.
*/
function twentyfifteen_amp_version_required_admin_notoce() {
?>
<div class="notice notice-warning">
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %1$s is the current plugin version */
__( 'The Twenty Fifteen Amplified theme requires AMP plugin v1.0 (76e1657), but you have %1$s. Please update to use with <code>amp</code> theme support.', 'twentyfifteen-amp' ),
esc_html( AMP__VERSION )
)
);
?>
</p>
</div>
<?php
}
/**
* Enqueue CSS from parent theme and this child theme.
*
* @see twentyfifteen_scripts()
*/
function twentyfifteen_amp_scripts() {
$parent_stylesheet = wp_styles()->registered[ get_template() . '-style' ];
$parent_stylesheet->src = get_template_directory_uri() . '/style.css';
$parent_stylesheet->ver = wp_get_theme( get_template() )->get( 'Version' );
wp_enqueue_style(
get_stylesheet() . '-style',
get_stylesheet_directory_uri() . '/style.css',
array( get_template() . '-style' ),
wp_get_theme( get_stylesheet() )->get( 'Version' )
);
}
add_action( 'wp_enqueue_scripts', 'twentyfifteen_amp_scripts', 20 );
/*
Theme Name: Twenty Fifteen Amplified
Author: XWP
Description: AMP child theme of Twenty Fifteen.
Version: 0.1
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Template: twentyfifteen
*/
html[amp] #commentform [submit-success] p {
margin-top: 1em;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment