From 6f7c5b573a6023c2f2ce3eecd0d6b169ce4e964c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Fri, 8 Apr 2022 16:29:12 +0200 Subject: [PATCH] Block Library: Mark new blocks to be included in WordPress core (#40186) * Block Library: Mark new blocks to be included in WordPress core * Move more code for Comment Template inner blocks to compat directory * Deprecate the Comment Author Avatar block * Include two more functions used by Comments block --- docs/reference-guides/core-blocks.md | 4 +- lib/compat/wordpress-6.0/blocks.php | 148 ++++++++++++++++++ lib/experimental/blocks.php | 148 ------------------ .../src/comment-author-avatar/block.json | 4 +- packages/block-library/src/index.js | 30 ++-- .../block-library/src/post-comment/edit.js | 4 +- 6 files changed, 169 insertions(+), 169 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 0bae5b5e25af9f..f739b8b89d7125 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -107,9 +107,9 @@ Display content in multiple columns, with blocks added to each column. ([Source] - **Supports:** align (full, wide), anchor, color (background, gradients, link, text), spacing (blockGap, margin, padding), ~~html~~ - **Attributes:** isStackedOnMobile, verticalAlignment -## Comment Author Avatar +## Comment Author Avatar (deprecated) -Displays the avatar of the comment's author. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/comment-author-avatar)) +This block is deprecated. Please use the Avatar block instead. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/comment-author-avatar)) - **Name:** core/comment-author-avatar - **Category:** theme diff --git a/lib/compat/wordpress-6.0/blocks.php b/lib/compat/wordpress-6.0/blocks.php index ab4a9d0dbd7faa..06ad86207b7ed4 100644 --- a/lib/compat/wordpress-6.0/blocks.php +++ b/lib/compat/wordpress-6.0/blocks.php @@ -277,3 +277,151 @@ function gutenberg_block_type_metadata_multiple_view_scripts( $metadata ) { return $metadata; } add_filter( 'block_type_metadata', 'gutenberg_block_type_metadata_multiple_view_scripts' ); + +if ( ! function_exists( 'build_comment_query_vars_from_block' ) ) { + /** + * 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; + } + + if ( get_option( 'page_comments' ) === '1' || get_option( 'page_comments' ) === true ) { + $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; + } +} + +if ( ! function_exists( 'get_comments_pagination_arrow' ) ) { + /** + * 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; + } +} + +/** + * Workaround for getting discussion settings as block editor settings + * so any user can access to them without needing to be an admin. + * + * @param array $settings Default editor settings. + * + * @return array Filtered editor settings. + */ +function gutenberg_extend_block_editor_settings_with_discussion_settings( $settings ) { + + $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' ), + ) + ), + ); + + return $settings; +} +add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_settings_with_discussion_settings' ); + +/** + * Mark the `children` attr of comments as embeddable so they can be included in + * REST API responses without additional requests. + * + * @return void + */ +function gutenberg_rest_comment_set_children_as_embeddable() { + add_filter( + 'rest_prepare_comment', + function ( $response ) { + $links = $response->get_links(); + if ( isset( $links['children'] ) ) { + $href = $links['children'][0]['href']; + $response->remove_link( 'children', $href ); + $response->add_link( 'children', $href, array( 'embeddable' => true ) ); + } + return $response; + } + ); +} +add_action( 'rest_api_init', 'gutenberg_rest_comment_set_children_as_embeddable' ); diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index c7b8e86abe6837..b9ac0a6c715830 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -5,154 +5,6 @@ * @package gutenberg */ -if ( ! function_exists( 'build_comment_query_vars_from_block' ) ) { - /** - * Helper function that constructs a comment query vars array from the passed block properties. - * - * It's used with the Comment Query Loop inner blocks. - * - * @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; - } - - if ( get_option( 'page_comments' ) === '1' || get_option( 'page_comments' ) === true ) { - $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; - } -} - -if ( ! function_exists( 'get_comments_pagination_arrow' ) ) { - /** - * 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. - * - * @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; - } -} - -if ( ! function_exists( 'extend_block_editor_settings_with_discussion_settings' ) ) { - /** - * Workaround for getting discussion settings as block editor settings - * so any user can access to them without needing to be an admin. - * - * @param array $settings Default editor settings. - * - * @return array Filtered editor settings. - */ - function extend_block_editor_settings_with_discussion_settings( $settings ) { - - $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' ), - ) - ), - ); - - return $settings; - } -} -add_filter( 'block_editor_settings_all', 'extend_block_editor_settings_with_discussion_settings' ); - -if ( ! function_exists( 'gutenberg_rest_comment_set_children_as_embeddable' ) ) { - /** - * Mark the `children` attr of comments as embeddable so they can be included in - * REST API responses without additional requests. - * - * @return void - */ - function gutenberg_rest_comment_set_children_as_embeddable() { - add_filter( - 'rest_prepare_comment', - function ( $response ) { - $links = $response->get_links(); - if ( isset( $links['children'] ) ) { - $href = $links['children'][0]['href']; - $response->remove_link( 'children', $href ); - $response->add_link( 'children', $href, array( 'embeddable' => true ) ); - } - return $response; - } - ); - } -} -add_action( 'rest_api_init', 'gutenberg_rest_comment_set_children_as_embeddable' ); - /** * Returns whether the quote v2 is enabled by the user. * diff --git a/packages/block-library/src/comment-author-avatar/block.json b/packages/block-library/src/comment-author-avatar/block.json index 7e538aeb99223c..b2a469090d96ba 100644 --- a/packages/block-library/src/comment-author-avatar/block.json +++ b/packages/block-library/src/comment-author-avatar/block.json @@ -2,10 +2,10 @@ "$schema": "https://schemas.wp.org/trunk/block.json", "apiVersion": 2, "name": "core/comment-author-avatar", - "title": "Comment Author Avatar", + "title": "Comment Author Avatar (deprecated)", "category": "theme", "ancestor": [ "core/comment-template" ], - "description": "Displays the avatar of the comment's author.", + "description": "This block is deprecated. Please use the Avatar block instead.", "textdomain": "default", "attributes": { "width": { diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 321a0505eaf52c..939dfca31c695a 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -191,6 +191,7 @@ export const __experimentalGetCoreBlocks = () => [ siteTagline, query, templatePart, + avatar, postTitle, postExcerpt, postFeaturedImage, @@ -204,7 +205,21 @@ export const __experimentalGetCoreBlocks = () => [ queryPaginationNext, queryPaginationNumbers, queryPaginationPrevious, + queryNoResults, + readMore, + commentAuthorName, + commentContent, + commentDate, + commentEditLink, + commentReplyLink, + commentTemplate, + commentsQueryLoop, + commentsPagination, + commentsPaginationNext, + commentsPaginationNumbers, + commentsPaginationPrevious, postComments, + homeLink, logInOut, termDescription, queryTitle, @@ -252,31 +267,16 @@ export const __experimentalRegisterExperimentalCoreBlocks = process.env ? ( { enableFSEBlocks } = {} ) => { [ // Experimental blocks. - avatar, - homeLink, postAuthorName, - queryNoResults, // Full Site Editing blocks. ...( enableFSEBlocks ? [ commentAuthorAvatar, - commentAuthorName, - commentContent, - commentDate, - commentEditLink, - commentReplyLink, - commentTemplate, - commentsQueryLoop, - commentsPagination, - commentsPaginationNext, - commentsPaginationNumbers, - commentsPaginationPrevious, navigationArea, postComment, postCommentsCount, postCommentsForm, postCommentsLink, - readMore, ] : [] ), ].forEach( registerBlock ); diff --git a/packages/block-library/src/post-comment/edit.js b/packages/block-library/src/post-comment/edit.js index e78ecd47bab160..7a012042dcfab9 100644 --- a/packages/block-library/src/post-comment/edit.js +++ b/packages/block-library/src/post-comment/edit.js @@ -8,7 +8,7 @@ import { blockDefault } from '@wordpress/icons'; import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; const ALLOWED_BLOCKS = [ - 'core/comment-author-avatar', + 'core/avatar', 'core/comment-author-name', 'core/comment-content', 'core/comment-date', @@ -16,7 +16,7 @@ const ALLOWED_BLOCKS = [ 'core/comment-reply-link', ]; const TEMPLATE = [ - [ 'core/comment-author-avatar' ], + [ 'core/avatar' ], [ 'core/comment-author-name' ], [ 'core/comment-date' ], [ 'core/comment-content' ],