Skip to content

Commit

Permalink
Add changes from previous merge attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Jun 19, 2022
1 parent 862ec61 commit f35d33b
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 36 deletions.
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function gutenberg_reregister_core_block_types() {
'comments-pagination-numbers.php' => 'core/comments-pagination-numbers',
'comments-pagination-previous.php' => 'core/comments-pagination-previous',
'comments-title.php' => 'core/comments-title',
'comments.php' => 'core/comments-query-loop',
'file.php' => 'core/file',
'home-link.php' => 'core/home-link',
'image.php' => 'core/image',
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
}
}
},
"editorStyle": "wp-block-comments-editor"
"editorStyle": "wp-block-comments-editor",
"usesContext": [ "postId", "postType" ]
}
129 changes: 129 additions & 0 deletions packages/block-library/src/comments/edit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
Warning,
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import CommentsInspectorControls from './comments-inspector-controls';
import Placeholder from './placeholder';

export default function PostCommentsEdit( {
attributes,
setAttributes,
context: { postType, postId },
clientId,
} ) {
const { tagName: TagName, textAlign } = attributes;

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

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

const innerBlocks = useSelect( ( select ) =>
select( blockEditorStore ).getBlocks( clientId )
);

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,
} ),
} );
const innerBlocksProps = useInnerBlocksProps( blockProps );

if ( innerBlocks.length > 0 ) {
return (
<>
<CommentsInspectorControls
attributes={ attributes }
setAttributes={ setAttributes }
/>
<TagName { ...innerBlocksProps } />
</>
);
}

return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>

<div { ...blockProps }>
<Warning>{ warning }</Warning>

{ showPlacholder && (
<Placeholder postId={ postId } postType={ postType } />
) }
</div>
</>
);
}
121 changes: 121 additions & 0 deletions packages/block-library/src/comments/edit/placeholder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* WordPress dependencies
*/
import { store as blockEditorStore } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { useDisabled } from '@wordpress/compose';

/**
* Internal dependencies
*/
import CommentsForm from '../../post-comments-form/form';

export default function PostCommentsPlaceholder( { postType, postId } ) {
let [ postTitle ] = useEntityProp( 'postType', postType, 'title', postId );
postTitle = postTitle || __( 'Post Title' );

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

const disabledRef = useDisabled();

return (
<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>

<CommentsForm />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**
* WordPress dependencies
*/
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import CommentsInspectorControls from './edit/comments-inspector-controls';

const TEMPLATE = [
[ 'core/comments-title' ],
[
Expand Down Expand Up @@ -68,21 +58,4 @@ const TEMPLATE = [
[ 'core/post-comments-form' ],
];

export default function CommentsEdit( { attributes, setAttributes } ) {
const { tagName: TagName } = attributes;

const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
} );

return (
<>
<CommentsInspectorControls
attributes={ attributes }
setAttributes={ setAttributes }
/>
<TagName { ...innerBlocksProps } />
</>
);
}
export default TEMPLATE;
9 changes: 9 additions & 0 deletions packages/block-library/src/comments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { postComments as icon } from '@wordpress/icons';
import metadata from './block.json';
import edit from './edit';
import save from './save';
import TEMPLATE from './edit/template';

const { name } = metadata;
export { metadata, name };
Expand All @@ -17,4 +18,12 @@ export const settings = {
icon,
edit,
save,
variations: [
{
name: 'default',
isDefault: true,
innerBlocks: TEMPLATE,
scope: [ 'inserter' ],
},
],
};
Loading

0 comments on commit f35d33b

Please sign in to comment.