Skip to content

Commit

Permalink
Block Library: Mark new blocks to be included in WordPress core (#40186)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gziolo committed Apr 8, 2022
1 parent 740445e commit 6f7c5b5
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 169 deletions.
4 changes: 2 additions & 2 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
148 changes: 148 additions & 0 deletions lib/compat/wordpress-6.0/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<span class='$arrow_classes'>$arrow</span>";
}
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' );
148 changes: 0 additions & 148 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<span class='$arrow_classes'>$arrow</span>";
}
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.
*
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/comment-author-avatar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
30 changes: 15 additions & 15 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export const __experimentalGetCoreBlocks = () => [
siteTagline,
query,
templatePart,
avatar,
postTitle,
postExcerpt,
postFeaturedImage,
Expand All @@ -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,
Expand Down Expand Up @@ -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 );
Expand Down
Loading

0 comments on commit 6f7c5b5

Please sign in to comment.