From f3fc52f959911868f847fd3b8d40d298f6e264a7 Mon Sep 17 00:00:00 2001 From: gziolo Date: Mon, 11 Apr 2022 15:22:09 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@53138 git-svn-id: http://core.svn.wordpress.org/trunk@52727 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-editor.php | 17 ++++ wp-includes/blocks.php | 93 +++++++++++++++++++ .../class-wp-rest-comments-controller.php | 3 +- wp-includes/version.php | 2 +- 4 files changed, 113 insertions(+), 2 deletions(-) diff --git a/wp-includes/block-editor.php b/wp-includes/block-editor.php index c5cb180117..9fe559fc41 100644 --- a/wp-includes/block-editor.php +++ b/wp-includes/block-editor.php @@ -396,6 +396,23 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex $editor_settings['localAutosaveInterval'] = 15; + $editor_settings['__experimentalDiscussionSettings'] = array( + 'commentOrder' => get_option( 'comment_order' ), + 'commentsPerPage' => get_option( 'comments_per_page' ), + 'defaultCommentsPage' => get_option( 'default_comments_page' ), + 'pageComments' => get_option( 'page_comments' ), + 'threadComments' => get_option( 'thread_comments' ), + 'threadCommentsDepth' => get_option( 'thread_comments_depth' ), + 'avatarURL' => get_avatar_url( + '', + array( + 'size' => 96, + 'force_default' => true, + 'default' => get_option( 'avatar_default' ), + ) + ), + ); + /** * Filters the settings to pass to the block editor for all editor type. * diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 45e36fc1dc..72f0198626 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -1358,3 +1358,96 @@ function _wp_multiple_block_styles( $metadata ) { return $metadata; } add_filter( 'block_type_metadata', '_wp_multiple_block_styles' ); + +/** + * Helper function that constructs a comment query vars array from the passed + * block properties. + * + * It's used with the Comment Query Loop inner blocks. + * + * @since 6.0.0 + * + * @param WP_Block $block Block instance. + * + * @return array Returns the comment query parameters to use with the + * WP_Comment_Query constructor. + */ +function build_comment_query_vars_from_block( $block ) { + + $comment_args = array( + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'no_found_rows' => false, + 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance. + ); + + if ( ! empty( $block->context['postId'] ) ) { + $comment_args['post_id'] = (int) $block->context['postId']; + } + + if ( get_option( 'thread_comments' ) ) { + $comment_args['hierarchical'] = 'threaded'; + } else { + $comment_args['hierarchical'] = false; + } + + $per_page = get_option( 'comments_per_page' ); + $default_page = get_option( 'default_comments_page' ); + + if ( $per_page > 0 ) { + $comment_args['number'] = $per_page; + + $page = (int) get_query_var( 'cpage' ); + if ( $page ) { + $comment_args['paged'] = $page; + } elseif ( 'oldest' === $default_page ) { + $comment_args['paged'] = 1; + } elseif ( 'newest' === $default_page ) { + $comment_args['paged'] = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages; + } + // Set the `cpage` query var to ensure the previous and next pagination links are correct + // when inheriting the Discussion Settings. + if ( 0 === $page && isset( $comment_args['paged'] ) && $comment_args['paged'] > 0 ) { + set_query_var( 'cpage', $comment_args['paged'] ); + } + } + + return $comment_args; +} + +/** + * Helper function that returns the proper pagination arrow html for + * `CommentsPaginationNext` and `CommentsPaginationPrevious` blocks based on the + * provided `paginationArrow` from `CommentsPagination` context. + * + * It's used in CommentsPaginationNext and CommentsPaginationPrevious blocks. + * + * @since 6.0.0 + * + * @param WP_Block $block Block instance. + * @param string $pagination_type Type of the arrow we will be rendering. + * Default 'next'. Accepts 'next' or 'previous'. + * + * @return string|null Returns the constructed WP_Query arguments. + */ +function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) { + $arrow_map = array( + 'none' => '', + 'arrow' => array( + 'next' => '→', + 'previous' => '←', + ), + 'chevron' => array( + 'next' => '»', + 'previous' => '«', + ), + ); + if ( ! empty( $block->context['comments/paginationArrow'] ) && ! empty( $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ] ) ) { + $arrow_attribute = $block->context['comments/paginationArrow']; + $arrow = $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ]; + $arrow_classes = "wp-block-comments-pagination-$pagination_type-arrow is-arrow-$arrow_attribute"; + return "$arrow"; + } + return null; +} diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 6e2daa829a..c2d9e87635 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -1196,7 +1196,8 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { $rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) ); $links['children'] = array( - 'href' => $rest_url, + 'href' => $rest_url, + 'embedded' => true, ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index fd1a6e1631..2bb6e23f62 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.0-alpha-53137'; +$wp_version = '6.0-alpha-53138'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.