-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5a2464
commit b035623
Showing
12 changed files
with
540 additions
and
11 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,44 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 3, | ||
"name": "knowledgebase/articles", | ||
"version": "2.3.0", | ||
"title": "Knowledge Base Articles", | ||
"category": "design", | ||
"icon": "book", | ||
"keywords": [ | ||
"knowledgbase", | ||
"articles", | ||
"kb" | ||
], | ||
"description": "Display the Knowledge Base Articles", | ||
"supports": { | ||
"html": false | ||
}, | ||
"attributes": { | ||
"className": { | ||
"type": "string", | ||
"default": "" | ||
}, | ||
"limit": { | ||
"type": "string", | ||
"default": "" | ||
}, | ||
"showExcerpt": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"termID": { | ||
"type": "string", | ||
"default": "" | ||
}, | ||
"title": { | ||
"type": "string", | ||
"default": "" | ||
} | ||
}, | ||
"example": {}, | ||
"textdomain": "knowledgebase", | ||
"editorScript": "file:./index.js", | ||
"editorStyle": "file:./index.css" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-i18n', 'wp-server-side-render'), 'version' => 'af27dffe54569e88bed6'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 3, | ||
"name": "knowledgebase/articles", | ||
"version": "2.3.0", | ||
"title": "Knowledge Base Articles", | ||
"category": "design", | ||
"icon": "book", | ||
"keywords": ["knowledgbase", "articles", "kb"], | ||
"description": "Display the Knowledge Base Articles", | ||
"supports": { | ||
"html": false | ||
}, | ||
"attributes": { | ||
"className": { | ||
"type": "string", | ||
"default": "" | ||
}, | ||
"limit": { | ||
"type": "string", | ||
"default": "" | ||
}, | ||
"showExcerpt": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"termID": { | ||
"type": "string", | ||
"default": "" | ||
}, | ||
"title": { | ||
"type": "string", | ||
"default": "" | ||
} | ||
}, | ||
"example": {}, | ||
"textdomain": "knowledgebase", | ||
"editorScript": "file:./index.js", | ||
"editorStyle": "file:./index.css" | ||
} |
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,145 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import ServerSideRender from '@wordpress/server-side-render'; | ||
import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
import { | ||
Disabled, | ||
ComboboxControl, | ||
ToggleControl, | ||
PanelBody, | ||
PanelRow, | ||
Spinner, | ||
TextControl, | ||
Notice, | ||
} from '@wordpress/components'; | ||
|
||
import './editor.scss'; | ||
|
||
export default function Edit({ attributes, setAttributes }) { | ||
const { termID, limit, showExcerpt } = attributes; | ||
|
||
const blockProps = useBlockProps(); | ||
|
||
const { terms, hasResolved, error } = useSelect((select) => { | ||
const query = { per_page: -1 }; | ||
const selectorArgs = ['taxonomy', 'wzkb_category', query]; | ||
|
||
try { | ||
return { | ||
terms: select(coreStore).getEntityRecords(...selectorArgs), | ||
hasResolved: select(coreStore).hasFinishedResolution( | ||
'getEntityRecords', | ||
selectorArgs | ||
), | ||
error: null, | ||
}; | ||
} catch (fetchError) { | ||
return { | ||
terms: [], | ||
hasResolved: true, | ||
error: fetchError, | ||
}; | ||
} | ||
}, []); | ||
|
||
const termOptions = | ||
terms?.map((term) => ({ | ||
label: `${term.name} (#${term.id})`, | ||
value: term.id.toString(), | ||
})) || []; | ||
|
||
const handleLimitChange = (newLimit) => { | ||
const parsedLimit = parseInt(newLimit, 10); | ||
setAttributes({ | ||
limit: isNaN(parsedLimit) || parsedLimit <= 0 ? 5 : parsedLimit, | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<InspectorControls> | ||
{error && ( | ||
<Notice status="error" isDismissible={false}> | ||
{__( | ||
'Error loading categories. Please try again.', | ||
'knowledgebase' | ||
)} | ||
</Notice> | ||
)} | ||
|
||
<PanelBody | ||
title={__( | ||
'Knowledge Base Articles Settings', | ||
'knowledgebase' | ||
)} | ||
initialOpen={true} | ||
> | ||
<PanelRow> | ||
{!hasResolved ? ( | ||
<Spinner /> | ||
) : ( | ||
<ComboboxControl | ||
label={__( | ||
'Select Knowledge Base Section', | ||
'knowledgebase' | ||
)} | ||
value={termID} | ||
onChange={(value) => | ||
setAttributes({ termID: value }) | ||
} | ||
options={termOptions} | ||
help={__( | ||
'Search and select a knowledge base section', | ||
'knowledgebase' | ||
)} | ||
/> | ||
)} | ||
</PanelRow> | ||
<PanelRow> | ||
<TextControl | ||
label={__('Number of posts', 'knowledgebase')} | ||
value={limit} | ||
type="number" | ||
min="1" | ||
onChange={handleLimitChange} | ||
help={__( | ||
'Enter the number of posts to display (default: 5)', | ||
'knowledgebase' | ||
)} | ||
/> | ||
</PanelRow> | ||
<PanelRow> | ||
<ToggleControl | ||
label={__('Show excerpt', 'knowledgebase')} | ||
help={ | ||
showExcerpt | ||
? __( | ||
'Excerpt is displayed', | ||
'knowledgebase' | ||
) | ||
: __('No excerpt shown', 'knowledgebase') | ||
} | ||
checked={showExcerpt} | ||
onChange={() => | ||
setAttributes({ | ||
showExcerpt: !showExcerpt, | ||
}) | ||
} | ||
/> | ||
</PanelRow> | ||
</PanelBody> | ||
</InspectorControls> | ||
|
||
<div {...blockProps}> | ||
<Disabled> | ||
<ServerSideRender | ||
block="knowledgebase/articles" | ||
attributes={attributes} | ||
/> | ||
</Disabled> | ||
</div> | ||
</> | ||
); | ||
} |
Oops, something went wrong.