Repository navigation
Add edit post comment block #30671
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
Add edit post comment block #30671
Changes from all commits
ff83366
9d95905
f07e572
9f0e7cc
5f7fcbd
be8fc6f
a08e66a
ef1fdb7
ca9838e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "apiVersion": 2, | ||
| "name": "core/post-comment-edit", | ||
| "category": "design", | ||
| "usesContext": [ "commentId" ], | ||
| "parent": [ | ||
| "core/post-comment" | ||
| ], | ||
| "attributes": { | ||
| "openInNewTab": { | ||
| "type": "boolean", | ||
| "default": false | ||
| } | ||
| }, | ||
| "supports": { | ||
| "html": false, | ||
| "color": { | ||
| "link": true, | ||
| "text": false | ||
| }, | ||
| "fontSize": true, | ||
| "lineHeight": true | ||
|
Comment on lines
+21
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I tested on my local site, it appeared a notice telling: Using this new approach should solve it. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; | ||
| import { PanelBody, ToggleControl } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| export default function Edit( { attributes, setAttributes } ) { | ||
| const { openInNewTab } = attributes; | ||
|
|
||
| return ( | ||
| <> | ||
| <InspectorControls> | ||
| <PanelBody title={ __( 'Link settings' ) }> | ||
| <ToggleControl | ||
| label={ __( 'Open in new tab' ) } | ||
| checked={ openInNewTab } | ||
| onChange={ () => | ||
| setAttributes( { | ||
| openInNewTab: ! openInNewTab, | ||
| } ) | ||
| } | ||
| /> | ||
|
Comment on lines
+15
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the same approach as site logo block could be: |
||
| </PanelBody> | ||
| </InspectorControls> | ||
| <div { ...useBlockProps() }> | ||
| <a href="#edit-comment-pseudo-link">{ __( 'Edit' ) }</a> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clicking the link in the editor seems to cause an unusual crash and some console errors, not sure what's happening, but it's probably unrelated to this PR. Thought I'd flag it though.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch! It could cause some pretty nasty behaviour. I've created an issue for that: #30967 I wouldn't know how to adres this in the scope of this block though besides removing the href. But removing the href has its own issues. |
||
| </div> | ||
| </> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __, _x } from '@wordpress/i18n'; | ||
| 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 = { | ||
| title: _x( 'Post Comment Edit Link', 'block title' ), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The title and description should be in |
||
| description: __( | ||
| 'Displays a link to edit the comment in the WordPress Dashboard. This link is only visible to users with the edit comment capability.' | ||
| ), | ||
| icon, | ||
| edit, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?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 ''; | ||
| } | ||
|
|
||
|
diedexx marked this conversation as resolved.
Resolved
|
||
| $edit_comment_link = get_edit_comment_link( $block->context['commentId'] ); | ||
| $open_in_new_tab = true === $attributes['openInNewTab']; | ||
|
|
||
| $link_atts = ''; | ||
|
|
||
| if ( $open_in_new_tab ) { | ||
| $link_atts .= 'target="_blank"'; | ||
| } | ||
|
Comment on lines
+27
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we change to the other blocks approach. We have to change this into something like |
||
|
|
||
| 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 /--> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [ | ||
| { | ||
| "clientId": "_clientId_0", | ||
| "name": "core/post-comment-edit", | ||
| "isValid": true, | ||
| "attributes": { | ||
| "openInNewTab": false | ||
| }, | ||
| "innerBlocks": [], | ||
| "originalContent": "" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [ | ||
| { | ||
| "blockName": "core/post-comment-edit", | ||
| "attrs": {}, | ||
| "innerBlocks": [], | ||
| "innerHTML": "", | ||
| "innerContent": [] | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!-- wp:post-comment-edit /--> |
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 similar approaches already on production blocks. Maybe we could change this one in order to keep consistency between them!
For example:
https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/site-logo/block.json#L19-L22