From 70ee6702962fb856b97ff61319b7815ac4174023 Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+c4rl0sbr4v0@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:03:58 +0200 Subject: [PATCH] Block Library: Add new Edit Comment Link block (#35965) * Add edit post comment block * Replace spaces with tabs * Rename label for consistency * Quote the href attribute * Don't assume roles will have specific capabilities Co-authored-by: Carolina Nymark * Update the block title * Add fixture * Move fixtures * Apply cs * Add edit post comment block * Replace spaces with tabs * Rename label for consistency * Quote the href attribute * Don't assume roles will have specific capabilities Co-authored-by: Carolina Nymark * Update the block title * Add fixture * Move fixtures * Apply cs * Update block to be similar to other comment blocks * Escape attributes and format php Co-authored-by: Diede Exterkate Co-authored-by: Carolina Nymark Co-authored-by: Carlos Bravo --- lib/blocks.php | 1 + packages/block-library/src/index.js | 2 + .../src/post-comment-author/index.php | 2 +- .../src/post-comment-edit/block.json | 33 ++++++++++++ .../src/post-comment-edit/edit.js | 42 +++++++++++++++ .../src/post-comment-edit/index.js | 18 +++++++ .../src/post-comment-edit/index.php | 51 +++++++++++++++++++ .../blocks/core__post-comment-edit.html | 1 + .../blocks/core__post-comment-edit.json | 31 +++++++++++ .../core__post-comment-edit.parsed.json | 30 +++++++++++ .../core__post-comment-edit.serialized.html | 1 + 11 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 packages/block-library/src/post-comment-edit/block.json create mode 100644 packages/block-library/src/post-comment-edit/edit.js create mode 100644 packages/block-library/src/post-comment-edit/index.js create mode 100644 packages/block-library/src/post-comment-edit/index.php create mode 100644 test/integration/fixtures/blocks/core__post-comment-edit.html create mode 100644 test/integration/fixtures/blocks/core__post-comment-edit.json create mode 100644 test/integration/fixtures/blocks/core__post-comment-edit.parsed.json create mode 100644 test/integration/fixtures/blocks/core__post-comment-edit.serialized.html diff --git a/lib/blocks.php b/lib/blocks.php index 752675134af85b..9e0d63a0c6bf00 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -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', diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index b491765b98dfcf..77095d01063ca4 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -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'; @@ -245,6 +246,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = postCommentAuthorAvatar, postCommentContent, postCommentDate, + postCommentEdit, postComments, postCommentsCount, postCommentsForm, diff --git a/packages/block-library/src/post-comment-author/index.php b/packages/block-library/src/post-comment-author/index.php index 1f4a9313e14b66..fb5cc4c43871fa 100644 --- a/packages/block-library/src/post-comment-author/index.php +++ b/packages/block-library/src/post-comment-author/index.php @@ -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( '%3s', $link, $attributes['linkTarget'], $comment_author ); + $comment_author = sprintf( '%3s', esc_url( $link ), esc_attr( $attributes['linkTarget'] ), $comment_author ); } return sprintf( diff --git a/packages/block-library/src/post-comment-edit/block.json b/packages/block-library/src/post-comment-edit/block.json new file mode 100644 index 00000000000000..a838989efd98ea --- /dev/null +++ b/packages/block-library/src/post-comment-edit/block.json @@ -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 + } + } +} diff --git a/packages/block-library/src/post-comment-edit/edit.js b/packages/block-library/src/post-comment-edit/edit.js new file mode 100644 index 00000000000000..320507a373378a --- /dev/null +++ b/packages/block-library/src/post-comment-edit/edit.js @@ -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 = ( + + + + setAttributes( { + linkTarget: value ? '_blank' : '_self', + } ) + } + checked={ linkTarget === '_blank' } + /> + + + ); + + return ( + <> + { inspectorControls } + + + ); +} diff --git a/packages/block-library/src/post-comment-edit/index.js b/packages/block-library/src/post-comment-edit/index.js new file mode 100644 index 00000000000000..59a1f84ed88542 --- /dev/null +++ b/packages/block-library/src/post-comment-edit/index.js @@ -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, +}; diff --git a/packages/block-library/src/post-comment-edit/index.php b/packages/block-library/src/post-comment-edit/index.php new file mode 100644 index 00000000000000..880ccdcdb22148 --- /dev/null +++ b/packages/block-library/src/post-comment-edit/index.php @@ -0,0 +1,51 @@ +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( + '', + 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' ); diff --git a/test/integration/fixtures/blocks/core__post-comment-edit.html b/test/integration/fixtures/blocks/core__post-comment-edit.html new file mode 100644 index 00000000000000..48e9c7c4034661 --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-comment-edit.html @@ -0,0 +1 @@ + diff --git a/test/integration/fixtures/blocks/core__post-comment-edit.json b/test/integration/fixtures/blocks/core__post-comment-edit.json new file mode 100644 index 00000000000000..fae0501ba14c16 --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-comment-edit.json @@ -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": "" + } +] diff --git a/test/integration/fixtures/blocks/core__post-comment-edit.parsed.json b/test/integration/fixtures/blocks/core__post-comment-edit.parsed.json new file mode 100644 index 00000000000000..dfa7123204e54f --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-comment-edit.parsed.json @@ -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": [] + } +] diff --git a/test/integration/fixtures/blocks/core__post-comment-edit.serialized.html b/test/integration/fixtures/blocks/core__post-comment-edit.serialized.html new file mode 100644 index 00000000000000..156b45b00efdfa --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-comment-edit.serialized.html @@ -0,0 +1 @@ +