Skip to content
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

Block Library: Mark new blocks to be included in WordPress core #40186

Merged
merged 4 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c4rl0sbr4v0 and @DAreRodz - is that still necessary for the Comment Template block?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need that function in order to show Comment Replies on the editor. Attached a video of what happens if we remove it.

removing_rest_extension.mov

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you for the confirmation 👍🏻

'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