Skip to content

Commit 067d166

Browse files
committed
Editor: Add changes for new Comments Query Loop blocks
Backports changes from Gutenberg to add functions required by Comment Query Loop and related blocks. Related unit tests depends on the new blocks and they will get added seperately. Props darerodz, timothyblynjacobs. See #55505. git-svn-id: https://develop.svn.wordpress.org/trunk@53138 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 79f2fb4 commit 067d166

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

src/wp-includes/block-editor.php

+17
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
396396

397397
$editor_settings['localAutosaveInterval'] = 15;
398398

399+
$editor_settings['__experimentalDiscussionSettings'] = array(
400+
'commentOrder' => get_option( 'comment_order' ),
401+
'commentsPerPage' => get_option( 'comments_per_page' ),
402+
'defaultCommentsPage' => get_option( 'default_comments_page' ),
403+
'pageComments' => get_option( 'page_comments' ),
404+
'threadComments' => get_option( 'thread_comments' ),
405+
'threadCommentsDepth' => get_option( 'thread_comments_depth' ),
406+
'avatarURL' => get_avatar_url(
407+
'',
408+
array(
409+
'size' => 96,
410+
'force_default' => true,
411+
'default' => get_option( 'avatar_default' ),
412+
)
413+
),
414+
);
415+
399416
/**
400417
* Filters the settings to pass to the block editor for all editor type.
401418
*

src/wp-includes/blocks.php

+93
Original file line numberDiff line numberDiff line change
@@ -1358,3 +1358,96 @@ function _wp_multiple_block_styles( $metadata ) {
13581358
return $metadata;
13591359
}
13601360
add_filter( 'block_type_metadata', '_wp_multiple_block_styles' );
1361+
1362+
/**
1363+
* Helper function that constructs a comment query vars array from the passed
1364+
* block properties.
1365+
*
1366+
* It's used with the Comment Query Loop inner blocks.
1367+
*
1368+
* @since 6.0.0
1369+
*
1370+
* @param WP_Block $block Block instance.
1371+
*
1372+
* @return array Returns the comment query parameters to use with the
1373+
* WP_Comment_Query constructor.
1374+
*/
1375+
function build_comment_query_vars_from_block( $block ) {
1376+
1377+
$comment_args = array(
1378+
'orderby' => 'comment_date_gmt',
1379+
'order' => 'ASC',
1380+
'status' => 'approve',
1381+
'no_found_rows' => false,
1382+
'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
1383+
);
1384+
1385+
if ( ! empty( $block->context['postId'] ) ) {
1386+
$comment_args['post_id'] = (int) $block->context['postId'];
1387+
}
1388+
1389+
if ( get_option( 'thread_comments' ) ) {
1390+
$comment_args['hierarchical'] = 'threaded';
1391+
} else {
1392+
$comment_args['hierarchical'] = false;
1393+
}
1394+
1395+
$per_page = get_option( 'comments_per_page' );
1396+
$default_page = get_option( 'default_comments_page' );
1397+
1398+
if ( $per_page > 0 ) {
1399+
$comment_args['number'] = $per_page;
1400+
1401+
$page = (int) get_query_var( 'cpage' );
1402+
if ( $page ) {
1403+
$comment_args['paged'] = $page;
1404+
} elseif ( 'oldest' === $default_page ) {
1405+
$comment_args['paged'] = 1;
1406+
} elseif ( 'newest' === $default_page ) {
1407+
$comment_args['paged'] = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
1408+
}
1409+
// Set the `cpage` query var to ensure the previous and next pagination links are correct
1410+
// when inheriting the Discussion Settings.
1411+
if ( 0 === $page && isset( $comment_args['paged'] ) && $comment_args['paged'] > 0 ) {
1412+
set_query_var( 'cpage', $comment_args['paged'] );
1413+
}
1414+
}
1415+
1416+
return $comment_args;
1417+
}
1418+
1419+
/**
1420+
* Helper function that returns the proper pagination arrow html for
1421+
* `CommentsPaginationNext` and `CommentsPaginationPrevious` blocks based on the
1422+
* provided `paginationArrow` from `CommentsPagination` context.
1423+
*
1424+
* It's used in CommentsPaginationNext and CommentsPaginationPrevious blocks.
1425+
*
1426+
* @since 6.0.0
1427+
*
1428+
* @param WP_Block $block Block instance.
1429+
* @param string $pagination_type Type of the arrow we will be rendering.
1430+
* Default 'next'. Accepts 'next' or 'previous'.
1431+
*
1432+
* @return string|null Returns the constructed WP_Query arguments.
1433+
*/
1434+
function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) {
1435+
$arrow_map = array(
1436+
'none' => '',
1437+
'arrow' => array(
1438+
'next' => '',
1439+
'previous' => '',
1440+
),
1441+
'chevron' => array(
1442+
'next' => '»',
1443+
'previous' => '«',
1444+
),
1445+
);
1446+
if ( ! empty( $block->context['comments/paginationArrow'] ) && ! empty( $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ] ) ) {
1447+
$arrow_attribute = $block->context['comments/paginationArrow'];
1448+
$arrow = $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ];
1449+
$arrow_classes = "wp-block-comments-pagination-$pagination_type-arrow is-arrow-$arrow_attribute";
1450+
return "<span class='$arrow_classes'>$arrow</span>";
1451+
}
1452+
return null;
1453+
}

src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,8 @@ protected function prepare_links( $comment ) {
11961196
$rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) );
11971197

11981198
$links['children'] = array(
1199-
'href' => $rest_url,
1199+
'href' => $rest_url,
1200+
'embedded' => true,
12001201
);
12011202
}
12021203

0 commit comments

Comments
 (0)