Skip to content

Commit

Permalink
Add placeholder to Post Comments block (#40484)
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Apr 21, 2022
1 parent 2ce8e8d commit 9b1a8fc
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 57 deletions.
15 changes: 8 additions & 7 deletions lib/compat/wordpress-6.0/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,14 @@ function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) {
function gutenberg_extend_block_editor_settings_with_discussion_settings( $settings ) {

$settings['__experimentalDiscussionSettings'] = array(
'commentOrder' => get_option( 'comment_order' ),
'commentsPerPage' => get_option( 'comments_per_page' ),
'defaultCommentsPage' => get_option( 'default_comments_page' ),
'pageComments' => get_option( 'page_comments' ),
'threadComments' => get_option( 'thread_comments' ),
'threadCommentsDepth' => get_option( 'thread_comments_depth' ),
'avatarURL' => get_avatar_url(
'commentOrder' => get_option( 'comment_order' ),
'commentsPerPage' => get_option( 'comments_per_page' ),
'defaultCommentsPage' => get_option( 'default_comments_page' ),
'pageComments' => get_option( 'page_comments' ),
'threadComments' => get_option( 'thread_comments' ),
'threadCommentsDepth' => get_option( 'thread_comments_depth' ),
'defaultCommentStatus' => get_option( 'default_comment_status' ),
'avatarURL' => get_avatar_url(
'',
array(
'size' => 96,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
@import "./query-pagination/editor.scss";
@import "./query-pagination-numbers/editor.scss";
@import "./post-featured-image/editor.scss";
@import "./post-comments/editor.scss";

:root .editor-styles-wrapper {
@include background-colors-deprecated();
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
"wp-block-post-comments",
"wp-block-buttons",
"wp-block-button"
]
],
"editorStyle": "wp-block-post-comments-editor"
}
240 changes: 196 additions & 44 deletions packages/block-library/src/post-comments/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,84 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import {
AlignmentControl,
BlockControls,
Warning,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { RawHTML } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';

function PostCommentsDisplay( { postId } ) {
return useSelect(
( select ) => {
const comments = select( coreStore ).getEntityRecords(
'root',
'comment',
{
post: postId,
}
);
// TODO: "No Comments" placeholder should be editable.
return comments && comments.length
? comments.map( ( comment ) => (
<RawHTML
className="wp-block-post-comments__comment"
key={ comment.id }
>
{ comment.content.rendered }
</RawHTML>
) )
: __( 'No comments.' );
},
[ postId ]
);
}
import { __, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { __experimentalUseDisabled as useDisabled } from '@wordpress/compose';

export default function PostCommentsEdit( {
attributes,
attributes: { textAlign },
setAttributes,
context,
context: { postType, postId },
} ) {
const { postType, postId } = context;
const { textAlign } = attributes;
let [ postTitle ] = useEntityProp( 'postType', postType, 'title', postId );
postTitle = postTitle || __( 'Post Title' );

const [ commentStatus ] = useEntityProp(
'postType',
postType,
'comment_status',
postId
);

const { avatarURL, defaultCommentStatus } = useSelect(
( select ) =>
select( blockEditorStore ).getSettings()
.__experimentalDiscussionSettings
);

const isSiteEditor = postType === undefined || postId === undefined;

const postTypeSupportsComments = useSelect( ( select ) =>
postType
? !! select( coreStore ).getPostType( postType )?.supports.comments
: false
);

let warning = __(
'Post Comments block: This is just a placeholder, not a real comment. The final styling may differ because it also depends on the current theme. For better compatibility with the Block Editor, please consider replacing this block with the "Comments Query Loop" block.'
);
let showPlacholder = true;

if ( ! isSiteEditor && 'open' !== commentStatus ) {
if ( 'closed' === commentStatus ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments block: Comments to this %s are not allowed.'
),
postType
);
showPlacholder = false;
} else if ( ! postTypeSupportsComments ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments block: Comments for this post type (%s) are not enabled.'
),
postType
);
showPlacholder = false;
} else if ( 'open' !== defaultCommentStatus ) {
warning = __( 'Post Comments block: Comments are not enabled.' );
showPlacholder = false;
}
}

const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

if ( ! postType || ! postId ) {
return (
<div { ...blockProps }>
<Warning>
{ __( 'Post comments block: no post found.' ) }
</Warning>
</div>
);
}
const disabledRef = useDisabled();

return (
<>
Expand All @@ -78,7 +97,140 @@ export default function PostCommentsEdit( {
</BlockControls>

<div { ...blockProps }>
<PostCommentsDisplay postId={ postId } />
<Warning>{ warning }</Warning>

{ showPlacholder && (
<div
className="wp-block-post-comments__placeholder"
ref={ disabledRef }
>
<h3>
{ __( 'One response to' ) }{ postTitle }
</h3>

<div className="navigation">
<div className="alignleft">
<a href="#top">« { __( 'Older Comments' ) }</a>
</div>
<div className="alignright">
<a href="#top">{ __( 'Newer Comments' ) } »</a>
</div>
</div>

<ol className="commentlist">
<li className="comment even thread-even depth-1">
<article className="comment-body">
<footer className="comment-meta">
<div className="comment-author vcard">
<img
alt="Commenter Avatar"
src={ avatarURL }
className="avatar avatar-32 photo"
height="32"
width="32"
loading="lazy"
/>
<b className="fn">
<a href="#top" className="url">
{ __(
'A WordPress Commenter'
) }
</a>
</b>{ ' ' }
<span className="says">
{ __( 'says' ) }:
</span>
</div>

<div className="comment-metadata">
<a href="#top">
<time dateTime="2000-01-01T00:00:00+00:00">
{ __(
'January 1, 2000 at 00:00 am'
) }
</time>
</a>{ ' ' }
<span className="edit-link">
<a
className="comment-edit-link"
href="#top"
>
{ __( 'Edit' ) }
</a>
</span>
</div>
</footer>

<div className="comment-content">
<p>
{ __( 'Hi, this is a comment.' ) }
<br />
{ __(
'To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.'
) }
<br />
{ __(
'Commenter avatars come from'
) }{ ' ' }
<a href="https://gravatar.com/">
Gravatar
</a>
.
</p>
</div>

<div className="reply">
<a
className="comment-reply-link"
href="#top"
aria-label="Reply to A WordPress Commenter"
>
{ __( 'Reply' ) }
</a>
</div>
</article>
</li>
</ol>

<div className="navigation">
<div className="alignleft">
<a href="#top">« { __( 'Older Comments' ) }</a>
</div>
<div className="alignright">
<a href="#top">{ __( 'Newer Comments' ) } »</a>
</div>
</div>

<div className="comment-respond">
<h3 className="comment-reply-title">
{ __( 'Leave a Reply' ) }
</h3>

<form className="comment-form" noValidate>
<p className="comment-form-comment">
<label htmlFor="comment">
{ __( 'Comment' ) }{ ' ' }
<span className="required">*</span>
</label>
<textarea
name="comment"
cols="45"
rows="8"
required
/>
</p>
<p className="form-submit wp-block-button">
<input
name="submit"
type="submit"
className="submit wp-block-button__link"
value={ __( 'Post Comment' ) }
/>
</p>
</form>
</div>
</div>
) }
</div>
</>
);
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/post-comments/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.wp-block-post-comments__placeholder * {
pointer-events: none;
}
6 changes: 1 addition & 5 deletions packages/block-library/src/post-comments/style.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
.wp-block-post-comments {
// Remove extraneous top padding added to the first heading of the block.
> h3:first-of-type {
margin-top: 0;
}

.commentlist {
clear: both;
list-style: none;
margin: 0;
padding: 0;
Expand Down

0 comments on commit 9b1a8fc

Please sign in to comment.