-
Notifications
You must be signed in to change notification settings - Fork 385
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 support for comments #871
Changes from 15 commits
7ec60e4
eed214f
4067b59
a8d2ad3
98874ba
4f6dcd6
317f90f
451db91
0541686
2c8575e
ac0cc53
2741fdf
ebffaae
9790be3
b425b19
b7c5963
94aee02
d0fc6c5
6b660b3
69d1342
4200d9e
366bef3
7b63836
8b289c1
15d327d
561189b
a1bf8d3
b40e932
dc1f9c3
187e136
82e8021
d4fd8bc
bbabbd7
0fd1ab5
9f99abe
9216e4e
fafa679
9536794
74ae3b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* AMP Rest Functions | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Register the comments REST endpoint. | ||
* | ||
* @since 0.7 | ||
*/ | ||
function amp_register_endpoints() { | ||
|
||
register_rest_route( 'amp/v1', '/comments/(?P<id>\d+)', array( | ||
'methods' => 'GET', | ||
'callback' => 'amp_get_comments', | ||
) ); | ||
|
||
} | ||
|
||
/** | ||
* Get comments for the Post and Parent | ||
* | ||
* @since 0.7 | ||
* @param WP_REST_Request $request The REST request. | ||
* | ||
* @return array The array of comments. | ||
*/ | ||
function amp_get_comments( $request ) { | ||
|
||
$id = $request->get_param( 'id' ); | ||
$return = array( | ||
'items' => array( | ||
'comment' => amp_get_comments_recursive( $id, 0 ), | ||
), | ||
); | ||
|
||
return $return; | ||
} | ||
|
||
/** | ||
* Recursively get comments for the Post and Parent | ||
* | ||
* @since 0.7 | ||
* @param int $id The post ID. | ||
* @param int $parent The comment parent. | ||
* | ||
* @return array The array of comments. | ||
*/ | ||
function amp_get_comments_recursive( $id, $parent ) { | ||
|
||
$comments = get_comments( array( | ||
'post_ID' => $id, | ||
'parent' => $parent, | ||
'order' => 'ASC', | ||
) ); | ||
|
||
$return = array(); | ||
foreach ( $comments as $comment ) { | ||
$GLOBALS['comment'] = $comment; // WPCS: override ok. | ||
$comment->comment_date = get_comment_date(); | ||
$comment->comment_time = get_comment_time(); | ||
$comment->comment = amp_get_comments_recursive( $id, $comment->comment_ID ); | ||
$return[] = $comment; | ||
} | ||
|
||
return $return; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?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; | ||
|
||
/** | ||
* Starts the element output. | ||
* | ||
* @since 2.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 ) { | ||
$output .= '{{#comment}}'; | ||
parent::start_el( $output, $comment, $depth, $args, $id ); | ||
} | ||
/** | ||
* Ends the element output, if needed. | ||
* | ||
* @since 2.7.0 | ||
* | ||
* @see Walker::end_el() | ||
* @see wp_list_comments() | ||
* | ||
* @param string $output Used to append additional content. Passed by reference. | ||
* @param WP_Comment $comment The current comment object. Default current comment. | ||
* @param int $depth Optional. Depth of the current comment. Default 0. | ||
* @param array $args Optional. An array of arguments. Default empty array. | ||
*/ | ||
public function end_el( &$output, $comment, $depth = 0, $args = array() ) { | ||
if ( ! empty( $args['end-callback'] ) ) { | ||
ob_start(); | ||
call_user_func( $args['end-callback'], $comment, $args, $depth ); | ||
$output .= ob_get_clean(); | ||
} else { | ||
if ( 'div' === $args['style'] ) { | ||
$output .= "</div><!-- #comment-## -->\n"; | ||
} else { | ||
$output .= "</li><!-- #comment-## -->\n"; | ||
} | ||
} | ||
$output .= '{{/comment}}'; | ||
} | ||
|
||
/** | ||
* Output amp-list template code and place holder for comments. | ||
* | ||
* @see Walker::paged_walk() | ||
* @param array $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 ''; | ||
} | ||
|
||
$args = array_slice( func_get_args(), 4 ); | ||
if ( ! empty( $args[0]['comments_template_placeholder'] ) ) { | ||
return $args[0]['comments_template_placeholder']; | ||
} | ||
|
||
$template = '<comments-template>'; | ||
$template .= parent::paged_walk( $elements, $max_depth, $page_num, $per_page, $args[0] ); | ||
$template .= '</comments-template>'; | ||
|
||
$url = get_rest_url( get_current_blog_id(), 'amp/v1/comments/' . get_the_ID() ); | ||
if ( strpos( $url, 'http:' ) === 0 ) { | ||
$url = substr( $url, 5 ); | ||
} | ||
// @todo Identify arguments and make filterable/settable. | ||
$template .= '<amp-list src="' . esc_attr( $url ) . '" height="400" single-item="true" layout="fixed-height">'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about calling the actual comments walker here too and provide the content as placeholder/fallback content? Maybe that is overkill. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not a bad idea. However, it would increase page weight. But setting some kind of fallback is great idea. |
||
$template .= '<template type="amp-mustache"></template>'; | ||
$template .= '</amp-list>'; | ||
|
||
return $template; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this will have performance problems, as it could result in a lot of DB queries. If you look at
wp_list_comments()
I believe you can see it gets a flat list of all comments for the post, and then it passes it intoWalker::paged_walk()
which then goes about identifying which comments are children of other comments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is,
AMP_Comment_Walker:: paged_walk()
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter Agreed. Can revise it to be more efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter how about this? b7c5963