-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2986 from 10up/feature/2972
Add Comments block.
- Loading branch information
Showing
10 changed files
with
587 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { InspectorControls, RichText, useBlockProps } from '@wordpress/block-editor'; | ||
import { CheckboxControl, PanelBody } from '@wordpress/components'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Window dependencies. | ||
*/ | ||
const { searchablePostTypes } = window.epComments; | ||
|
||
/** | ||
* Edit component. | ||
* | ||
* @param {object} props Component props. | ||
* @param {object} props.attributes Block attributes. | ||
* @param {Function} props.setAttributes Block attribute setter. | ||
* @returns {Function} Component. | ||
*/ | ||
export default ({ attributes, setAttributes }) => { | ||
const { label, postTypes } = attributes; | ||
|
||
const blockProps = useBlockProps({ | ||
className: 'ep-widget-search-comments', | ||
}); | ||
|
||
const allSelected = useMemo(() => { | ||
return postTypes.length === 0; | ||
}, [postTypes]); | ||
|
||
/** | ||
* Handle checking a post type option. | ||
* | ||
* @param {string} postType Post type slug. | ||
* @param {boolean} checked Whether post type is selected. | ||
* @returns {void} | ||
*/ | ||
const onChange = (postType, checked) => { | ||
const newPostTypes = checked | ||
? [...postTypes, postType] | ||
: postTypes.filter((v) => v !== postType); | ||
|
||
setAttributes({ postTypes: newPostTypes }); | ||
}; | ||
|
||
/** | ||
* Handle selecting all post types. | ||
* | ||
* @param {boolean} checked Whether all has been selected. | ||
* @returns {void} | ||
*/ | ||
const onSelectAll = (checked) => { | ||
if (checked) { | ||
setAttributes({ postTypes: [] }); | ||
} else { | ||
setAttributes({ postTypes: Object.keys(searchablePostTypes) }); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<div {...blockProps}> | ||
<RichText | ||
aria-label={__('Label text')} | ||
placeholder={__('Add label…')} | ||
withoutInteractiveFormatting | ||
value={label} | ||
onChange={(html) => setAttributes({ label: html })} | ||
/> | ||
<input | ||
autoComplete="off" | ||
className="ep-widget-search-comments-input" | ||
disabled | ||
type="search" | ||
/> | ||
</div> | ||
<InspectorControls> | ||
<PanelBody title={__('Search settings', 'elasticpress')}> | ||
<CheckboxControl | ||
checked={allSelected} | ||
label={__('Search all comments', 'elasticpress')} | ||
onChange={onSelectAll} | ||
/> | ||
{Object.entries(searchablePostTypes).map(([postType, postTypeLabel]) => { | ||
const label = sprintf( | ||
/* translators: %s: Post type label, plural. */ | ||
__('Search comments on %s', 'elasticpress'), | ||
postTypeLabel, | ||
); | ||
|
||
return ( | ||
<CheckboxControl | ||
checked={postTypes.includes(postType)} | ||
indeterminate={allSelected} | ||
label={label} | ||
onChange={(checked) => onChange(postType, checked)} | ||
/> | ||
); | ||
})} | ||
</PanelBody> | ||
</InspectorControls> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"title": "Search Comments (ElasticPress)", | ||
"icon": "search", | ||
"textdomain": "elasticpress", | ||
"name": "elasticpress/comments", | ||
"category": "widgets", | ||
"attributes": { | ||
"label": { | ||
"default": "Search comments", | ||
"type": "string" | ||
}, | ||
"postTypes": { | ||
"default": [], | ||
"type": "array" | ||
} | ||
}, | ||
"editorScript": "elasticpress-comments-editor-script", | ||
"script": "elasticpress-comments", | ||
"style": "elasticpress-comments" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies. | ||
*/ | ||
import Edit from './Edit'; | ||
import block from './block.json'; | ||
import transforms from './transforms'; | ||
|
||
registerBlockType(block, { | ||
edit: (props) => <Edit {...props} />, | ||
save: () => {}, | ||
transforms, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { createBlock } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Facet widget block transforms. | ||
*/ | ||
export default { | ||
from: [ | ||
{ | ||
type: 'block', | ||
blocks: ['core/legacy-widget'], | ||
isMatch: ({ idBase }) => idBase === 'ep-comments', | ||
transform: ({ instance }) => { | ||
const { title = null, post_type: postTypes } = instance.raw; | ||
|
||
if (!title) { | ||
return createBlock('elasticpress/comments', { postTypes }); | ||
} | ||
|
||
return [ | ||
createBlock('core/heading', { content: title }), | ||
createBlock('elasticpress/comments', { postTypes }), | ||
]; | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.