Skip to content
Closed
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 @@ -73,6 +73,7 @@ function gutenberg_reregister_core_block_types() {
'post-comment-author.php' => 'core/post-comment-author',
'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 @@ -82,6 +82,7 @@ import * as postComment from './post-comment';
import * as postCommentAuthor from './post-comment-author';
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 @@ -243,6 +244,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
postCommentAuthor,
postCommentContent,
postCommentDate,
postCommentEdit,
postComments,
postCommentsCount,
postCommentsForm,
Expand Down
24 changes: 24 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,24 @@
{
"apiVersion": 2,
"name": "core/post-comment-edit",
"category": "design",
"usesContext": [ "commentId" ],
"parent": [
"core/post-comment"
],
"attributes": {
"openInNewTab": {
"type": "boolean",
"default": false
}
Comment on lines +10 to +13

Copy link
Copy Markdown
Contributor

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

},
"supports": {
"html": false,
"color": {
"link": true,
"text": false
},
"fontSize": true,
"lineHeight": true
Comment on lines +21 to +22

@cbravobernal cbravobernal Oct 20, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When I tested on my local site, it appeared a notice telling: fontSize support is now declared under supports.typography.fontSize

Using this new approach should solve it.

	"typography": {
			"fontSize": true,
			"lineHeight": true,
			"__experimentalFontFamily": true,
			"__experimentalFontWeight": true,
			"__experimentalFontStyle": true,
			"__experimentalTextTransform": true,
			"__experimentalLetterSpacing": true
		}

}
}
31 changes: 31 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,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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Using the same approach as site logo block could be:

https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/site-logo/edit.js#L278-L286

					<ToggleControl
								label={ __( 'Open in new tab' ) }
								onChange={ ( value ) =>
									setAttributes( {
										linkTarget: value ? '_blank' : '_self',
									} )
								}
								checked={ linkTarget === '_blank' }
							/>

</PanelBody>
</InspectorControls>
<div { ...useBlockProps() }>
<a href="#edit-comment-pseudo-link">{ __( 'Edit' ) }</a>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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>
</>
);
}
23 changes: 23 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,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' ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The title and description should be in block.json so it gets exposed also on the server.

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,
};
52 changes: 52 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,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 '';
}

Comment thread
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 attributes['linkTarget'] in order to work on the frontend.


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 /-->
12 changes: 12 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,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 /-->