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: Add new Edit Comment Link block #35965

Merged
merged 20 commits into from
Oct 27, 2021
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
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function gutenberg_reregister_core_block_types() {
'post-comment-author-avatar.php' => 'core/post-comment-author-avatar',
'post-comment-content.php' => 'core/post-comment-content',
'post-comment-date.php' => 'core/post-comment-date',
'post-comment-edit.php' => 'core/post-comment-edit',
'post-comments.php' => 'core/post-comments',
'post-comments-count.php' => 'core/post-comments-count',
'post-comments-form.php' => 'core/post-comments-form',
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import * as postCommentAuthor from './post-comment-author';
import * as postCommentAuthorAvatar from './post-comment-author-avatar';
import * as postCommentContent from './post-comment-content';
import * as postCommentDate from './post-comment-date';
import * as postCommentEdit from './post-comment-edit';
import * as postComments from './post-comments';
import * as postCommentsCount from './post-comments-count';
import * as postCommentsForm from './post-comments-form';
Expand Down Expand Up @@ -245,6 +246,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
postCommentAuthorAvatar,
postCommentContent,
postCommentDate,
postCommentEdit,
postComments,
postCommentsCount,
postCommentsForm,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-comment-author/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function render_block_core_post_comment_author( $attributes, $content, $block )
$link = get_comment_author_url( $comment );

if ( ! empty( $attributes['isLink'] ) && ! empty( $attributes['linkTarget'] ) ) {
$comment_author = sprintf( '<a rel="external nofollow ugc" href="%1s" target="%2s" >%3s</a>', $link, $attributes['linkTarget'], $comment_author );
$comment_author = sprintf( '<a rel="external nofollow ugc" href="%1s" target="%2s" >%3s</a>', esc_url( $link ), esc_attr( $attributes['linkTarget'] ), $comment_author );
}

return sprintf(
Expand Down
33 changes: 33 additions & 0 deletions packages/block-library/src/post-comment-edit/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"apiVersion": 2,
"name": "core/post-comment-edit",
"title": "Post Comment Edit Link",
"category": "design",
"parent": [ "core/post-comment" ],
"description": "Displays a link to edit the comment in the WordPress Dashboard. This link is only visible to users with the edit comment capability.",
"textdomain": "default",
"usesContext": [ "commentId" ],
"attributes": {
"linkTarget": {
"type": "string",
"default": "_self"
}
},
"supports": {
"html": false,
"color": {
"link": true,
"gradients": true,
"text": false
},
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalLetterSpacing": true
}
}
}
42 changes: 42 additions & 0 deletions packages/block-library/src/post-comment-edit/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export default function Edit( {
attributes: { className, linkTarget },
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the className attribute passed always passed to the blocks's edit method in a way that I'm not aware of? Otherwise, I think you won't need it here because it's not defined in the block.json

Copy link
Member

Choose a reason for hiding this comment

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

There are two features enabled by default for every block:
https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-supports.md#classname
https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-supports.md#customclassname

The second one injects a custom class name using a className attribute. I think that the handling is now automated with useBlockProps so you no longer need to explicitly pass it to useBlockProps, but it was a requirement for blocks in the past. I think we should remove className handling here to avoid confusion.

Copy link
Member

@gziolo gziolo Oct 28, 2021

Choose a reason for hiding this comment

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

I need to test it because I see the same usage as here also in other blocks. It's very inconsistent across blocks.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we can safely remove the className handling from here. It is redundant. Well, it doesn't break anything. I'm opening a PR soon with more changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh, I see! Thanks, Greg!

setAttributes,
} ) {
const blockProps = useBlockProps( { className } );
const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Open in new tab' ) }
onChange={ ( value ) =>
setAttributes( {
linkTarget: value ? '_blank' : '_self',
} )
}
checked={ linkTarget === '_blank' }
/>
</PanelBody>
</InspectorControls>
);

return (
<>
{ inspectorControls }
<div { ...blockProps }>
<a
href="#edit-comment-pseudo-link"
onClick={ ( event ) => event.preventDefault() }
>
{ __( 'Edit' ) }
</a>
</div>
</>
);
}
18 changes: 18 additions & 0 deletions packages/block-library/src/post-comment-edit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* WordPress dependencies
*/
import { edit as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';

const { name } = metadata;
export { metadata, name };

export const settings = {
icon,
edit,
};
51 changes: 51 additions & 0 deletions packages/block-library/src/post-comment-edit/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Server-side rendering of the `core/post-comment-edit` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-comment-edit` block on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Return the post comment's date.
*/
function render_block_core_post_comment_edit( $attributes, $content, $block ) {
if ( ! isset( $block->context['commentId'] ) || ! current_user_can( 'edit_comment', $block->context['commentId'] ) ) {
return '';
}

$edit_comment_link = get_edit_comment_link( $block->context['commentId'] );

$link_atts = '';

if ( ! empty( $attributes['linkTarget'] ) ) {
$link_atts .= sprintf( 'target="%s"', esc_attr( $attributes['linkTarget'] ) );
}

return sprintf(
'<div %1$s><a href="%2$s" %3$s>%4$s</a></div>',
get_block_wrapper_attributes(),
esc_url( $edit_comment_link ),
$link_atts,
esc_html__( 'Edit' )
);
}

/**
* Registers the `core/post-comment-edit` block on the server.
*/
function register_block_core_post_comment_edit() {
register_block_type_from_metadata(
__DIR__ . '/post-comment-edit',
array(
'render_callback' => 'render_block_core_post_comment_edit',
)
);
}

add_action( 'init', 'register_block_core_post_comment_edit' );
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-comment-edit {"linkTarget":"_blank","style":{"typography":{"fontStyle":"normal","fontWeight":"700","lineHeight":"2","textTransform":"uppercase","letterSpacing":"13px"},"elements":{"link":{"color":{"text":"#ed1717"}}}},"backgroundColor":"recommended-color-03","fontSize":"extra-large","fontFamily":"system-monospace"} /-->
31 changes: 31 additions & 0 deletions test/integration/fixtures/blocks/core__post-comment-edit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-comment-edit",
"isValid": true,
"attributes": {
"linkTarget": "_blank",
"backgroundColor": "recommended-color-03",
"fontFamily": "system-monospace",
"fontSize": "extra-large",
"style": {
"typography": {
"fontStyle": "normal",
"fontWeight": "700",
"lineHeight": "2",
"textTransform": "uppercase",
"letterSpacing": "13px"
},
"elements": {
"link": {
"color": {
"text": "#ed1717"
}
}
}
}
},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"blockName": "core/post-comment-edit",
"attrs": {
"linkTarget": "_blank",
"style": {
"typography": {
"fontStyle": "normal",
"fontWeight": "700",
"lineHeight": "2",
"textTransform": "uppercase",
"letterSpacing": "13px"
},
"elements": {
"link": {
"color": {
"text": "#ed1717"
}
}
}
},
"backgroundColor": "recommended-color-03",
"fontSize": "extra-large",
"fontFamily": "system-monospace"
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-comment-edit {"linkTarget":"_blank","backgroundColor":"recommended-color-03","fontFamily":"system-monospace","fontSize":"extra-large","style":{"typography":{"fontStyle":"normal","fontWeight":"700","lineHeight":"2","textTransform":"uppercase","letterSpacing":"13px"},"elements":{"link":{"color":{"text":"#ed1717"}}}}} /-->