Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add amp-live-list comments #909

Merged
merged 27 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bf03e0f
add amp comment walker
DavidCramer Jan 26, 2018
9a9b1c3
add amp comment walker to autoloader
DavidCramer Jan 26, 2018
5f004e8
add comments sanitiser
DavidCramer Jan 26, 2018
82162c3
add comment walker
DavidCramer Jan 26, 2018
04eebf6
add comment and form sanitizers
DavidCramer Jan 26, 2018
80feeed
correct ID's and max items limit
DavidCramer Jan 26, 2018
396aa91
fix new line issue
DavidCramer Jan 26, 2018
1d8728a
Add form posting handler
DavidCramer Jan 26, 2018
b2577c2
rename some methods and add walker depth.
DavidCramer Jan 26, 2018
87c46b6
alternate way to get timestamp
DavidCramer Jan 26, 2018
0822a34
Merge branch 'develop' of https://github.com/Automattic/amp-wp into a…
westonruter Jan 26, 2018
dffb7ca
wip - walker changes.
DavidCramer Jan 30, 2018
98fee1b
Rework submission handling
DavidCramer Jan 30, 2018
014807d
Remove uneeded function
DavidCramer Jan 30, 2018
ae13421
merge develop
DavidCramer Jan 30, 2018
646debb
remove duplicate form sanitizer entry.
DavidCramer Jan 30, 2018
4b38a5b
remove amp markup from walker
DavidCramer Jan 30, 2018
63d83d0
Lockdown sort direction and admin notice stating unsupported.
DavidCramer Jan 30, 2018
310a74a
Opt to inline the descending-ordering comment logic since temporary
westonruter Jan 30, 2018
5ee1709
Fix script tag generation for amp-mustache as custom-template
westonruter Jan 30, 2018
8f1b37c
Implement form reset after comment submission via amp-bind and amp-state
westonruter Jan 30, 2018
82e5218
Disable comment input fields during submission
westonruter Jan 31, 2018
91b0c8d
Fix construction of attributes in comment wrapper elements
westonruter Jan 31, 2018
77dc127
Power comment replies with amp-state
westonruter Jan 31, 2018
ef0e4c3
Show who the commenter is replying to
westonruter Jan 31, 2018
28d760c
Fix setting initial replyToName state
westonruter Jan 31, 2018
8b2232f
Move function with closures out of amp.php for sake of PHP 5.2 check
westonruter Jan 31, 2018
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
10 changes: 8 additions & 2 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function amp_after_setup_theme() {
}

add_action( 'init', 'amp_init' );
add_action( 'widgets_init', 'AMP_Theme_Support::register_widgets' );
add_action( 'widgets_init', 'AMP_Theme_Support::register_widgets' ); // @todo Let this be called by AMP_Theme_Support::init().
add_action( 'init', 'AMP_Theme_Support::setup_commenting' ); // @todo Let this be called by AMP_Theme_Support::init().
add_action( 'admin_init', 'AMP_Options_Manager::register_settings' );
add_filter( 'amp_post_template_analytics', 'amp_add_custom_analytics' );
add_action( 'wp_loaded', 'amp_post_meta_box' );
Expand All @@ -102,8 +103,10 @@ function amp_after_setup_theme() {
* Init AMP.
*
* @since 0.1
* @global string $pagenow
*/
function amp_init() {
global $pagenow;

/**
* Triggers on init when AMP plugin is active.
Expand All @@ -123,7 +126,10 @@ function amp_init() {
add_filter( 'old_slug_redirect_url', 'amp_redirect_old_slug_to_new_url' );

if ( class_exists( 'Jetpack' ) && ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
require_once( AMP__DIR__ . '/jetpack-helper.php' );
require_once AMP__DIR__ . '/jetpack-helper.php';
}
if ( isset( $pagenow ) && 'wp-comments-post.php' === $pagenow ) {
amp_prepare_comment_post();
}
}

Expand Down
43 changes: 42 additions & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,56 @@ function amp_get_content_sanitizers( $post = null ) {
array(
'AMP_Style_Sanitizer' => array(),
'AMP_Img_Sanitizer' => array(),
'AMP_Form_Sanitizer' => array(),
'AMP_Comments_Sanitizer' => array(),
'AMP_Video_Sanitizer' => array(),
'AMP_Audio_Sanitizer' => array(),
'AMP_Playbuzz_Sanitizer' => array(),
'AMP_Iframe_Sanitizer' => array(
'add_placeholder' => true,
),
'AMP_Form_Sanitizer' => array(),
'AMP_Tag_And_Attribute_Sanitizer' => array(), // Note: This whitelist sanitizer must come at the end to clean up any remaining issues the other sanitizers didn't catch.
),
$post
);
}

/**
* Hook into a comment submission of an AMP XHR post request.
*
* This only runs on wp-comments-post.php.
*
* @since 0.7.0
*/
function amp_prepare_comment_post() {
if ( ! isset( $_GET['__amp_source_origin'] ) ) { // WPCS: CSRF ok. Beware of AMP_Theme_Support::purge_amp_query_vars().
return;
}

// Add amp comment hooks.
add_filter( 'comment_post_redirect', function() {
// We don't need any data, so just send a success.
wp_send_json_success();
}, PHP_INT_MAX, 2 );

// Add die handler for AMP error display.
add_filter( 'wp_die_handler', function() {
/**
* New error handler for AMP form submission.
*
* @param WP_Error|string $error The error to handle.
*/
return function( $error ) {
status_header( 400 );
if ( is_wp_error( $error ) ) {
$error = $error->get_error_message();
}
$error = strip_tags( $error, 'strong' );
wp_send_json( compact( 'error' ) );
};
} );

// Send AMP header.
$origin = esc_url_raw( wp_unslash( $_GET['__amp_source_origin'] ) ); // WPCS: CSRF ok.
header( 'AMP-Access-Control-Allow-Source-Origin: ' . $origin, true );
}
2 changes: 2 additions & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AMP_Autoloader {
*/
private static $_classmap = array(
'AMP_Theme_Support' => 'includes/class-amp-theme-support',
'AMP_Comment_Walker' => 'includes/class-amp-comment-walker',
'AMP_WP_Styles' => 'includes/class-amp-wp-styles',
'AMP_Template_Customizer' => 'includes/admin/class-amp-customizer',
'AMP_Post_Meta_Box' => 'includes/admin/class-amp-post-meta-box',
Expand Down Expand Up @@ -69,6 +70,7 @@ class AMP_Autoloader {
'AMP_Blacklist_Sanitizer' => 'includes/sanitizers/class-amp-blacklist-sanitizer',
'AMP_Iframe_Sanitizer' => 'includes/sanitizers/class-amp-iframe-sanitizer',
'AMP_Img_Sanitizer' => 'includes/sanitizers/class-amp-img-sanitizer',
'AMP_Comments_Sanitizer' => 'includes/sanitizers/class-amp-comments-sanitizer',
'AMP_Form_Sanitizer' => 'includes/sanitizers/class-amp-form-sanitizer',
'AMP_Playbuzz_Sanitizer' => 'includes/sanitizers/class-amp-playbuzz-sanitizer',
'AMP_Style_Sanitizer' => 'includes/sanitizers/class-amp-style-sanitizer',
Expand Down
121 changes: 121 additions & 0 deletions includes/class-amp-comment-walker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* Class AMP_Comment_Walker
*
* @package AMP
*/

/**
* Class AMP_Comment_Walker
*
* Walker to wrap comments in mustache tags for amp-template.
*/
class AMP_Comment_Walker extends Walker_Comment {

/**
* The original comments arguments.
*
* @since 0.7
* @var array
*/
public $args;

/**
* Holds the timestamp of the most reacent comment in a thread.
*
* @since 0.7
* @var array
*/
private $comment_thread_age = array();

/**
* Starts the element output.
*
* @since 0.7.0
*
* @see Walker::start_el()
* @see wp_list_comments()
* @global int $comment_depth
* @global WP_Comment $comment
*
* @param string $output Used to append additional content. Passed by reference.
* @param WP_Comment $comment Comment data object.
* @param int $depth Optional. Depth of the current comment in reference to parents. Default 0.
* @param array $args Optional. An array of arguments. Default empty array.
* @param int $id Optional. ID of the current comment. Default 0 (unused).
*/
public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {

$new_out = '';
parent::start_el( $new_out, $comment, $depth, $args, $id );

if ( 'div' === $args['style'] ) {
$tag = '<div';
} else {
$tag = '<li';
}
$new_tag = $tag . ' data-sort-time="' . esc_attr( strtotime( $comment->comment_date ) ) . '"';

if ( ! empty( $this->comment_thread_age[ $comment->comment_ID ] ) ) {
$new_tag .= ' data-update-time="' . esc_attr( $this->comment_thread_age[ $comment->comment_ID ] ) . '"';
}

$output .= $new_tag . substr( ltrim( $new_out ), strlen( $tag ) );

}

/**
* Output amp-list template code and place holder for comments.
*
* @since 0.7
* @see Walker::paged_walk()
*
* @param WP_Comment[] $elements List of comment Elements.
* @param int $max_depth The maximum hierarchical depth.
* @param int $page_num The specific page number, beginning with 1.
* @param int $per_page Per page counter.
*
* @return string XHTML of the specified page of elements.
*/
public function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
if ( empty( $elements ) || $max_depth < -1 ) {
return '';
}

$this->build_thread_latest_date( $elements );

$args = array_slice( func_get_args(), 4 );

return parent::paged_walk( $elements, $max_depth, $page_num, $per_page, $args[0] );
}

/**
* Find the timestamp of the latest child comment of a thread to set the updated time.
*
* @since 0.7
*
* @param WP_Comment[] $elements The list of comments to get thread times for.
* @param int $time $the timestamp to check against.
* @param bool $is_child Flag used to set the the value or return the time.
* @return int Latest time.
*/
protected function build_thread_latest_date( $elements, $time = 0, $is_child = false ) {

foreach ( $elements as $element ) {

$children = $element->get_children();
$this_time = strtotime( $element->comment_date );
if ( ! empty( $children ) ) {
$this_time = $this->build_thread_latest_date( $children, $this_time, true );
}
if ( $this_time > $time ) {
$time = $this_time;
}
if ( false === $is_child ) {
$this->comment_thread_age[ $element->comment_ID ] = $time;
}
}

return $time;
}
}
Loading