-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9fe029e
Add edit post comment block
diedexx b66e2e3
Replace spaces with tabs
diedexx fac42fa
Rename label for consistency
diedexx de3d355
Quote the href attribute
diedexx b10c9ff
Don't assume roles will have specific capabilities
diedexx e46b2e5
Update the block title
diedexx 5c196e2
Add fixture
diedexx dc8f0a5
Move fixtures
diedexx fe24e9c
Apply cs
diedexx bd2c3b1
Add edit post comment block
diedexx ed17023
Replace spaces with tabs
diedexx 7b2d324
Rename label for consistency
diedexx 6d3cddc
Quote the href attribute
diedexx cba01f8
Don't assume roles will have specific capabilities
diedexx 1185f5f
Update the block title
diedexx 7be69b5
Add fixture
diedexx 85498c2
Move fixtures
diedexx 4fca688
Apply cs
diedexx 1299e3b
Update block to be similar to other comment blocks
cd8c33a
Escape attributes and format php
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
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> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
test/integration/fixtures/blocks/core__post-comment-edit.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "" | ||
} | ||
] |
30 changes: 30 additions & 0 deletions
30
test/integration/fixtures/blocks/core__post-comment-edit.parsed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/fixtures/blocks/core__post-comment-edit.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}}}}} /--> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theblock.json
There was a problem hiding this comment.
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 withuseBlockProps
so you no longer need to explicitly pass it touseBlockProps
, but it was a requirement for blocks in the past. I think we should removeclassName
handling here to avoid confusion.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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!