Skip to content

Commit

Permalink
add offset option to recent news block. #260
Browse files Browse the repository at this point in the history
  • Loading branch information
spelkey-ucd committed Nov 3, 2023
1 parent e61fae9 commit b6e4af9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/editor/lib/blocks/ucd-theme-recent-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ export default ( props ) => {
// using block attributes, construct and do api query for posts
const queryParams = (() => {
const q = {
per_page: attributes.postCt
per_page: attributes.postCt,
};
if ( attributes.offset ) q.offset = attributes.offset;
if ( attributes.author ) q.author = attributes.author;
if ( attributes.search ) q.search = attributes.search;

for (const tax in attributes.terms) {
const v = attributes.terms[tax].join(",");
if ( !v ) continue;
Expand Down Expand Up @@ -56,7 +57,7 @@ export default ( props ) => {
// set up term picker
const onTermChange = ( v ) => {
const terms = {
...attributes.terms,
...attributes.terms,
[ v.taxonomy ]: v.terms
};
setAttributes({terms})
Expand Down Expand Up @@ -90,7 +91,7 @@ export default ( props ) => {
if ( post ){
p.href = post.link;
p.title = decodeEntities(post.title.rendered);

let postExcerpt = post.excerpt.rendered.replace(/(<([^>]+)>)/gi, "").replace(" [&hellip;]", "...");
postExcerpt = decodeEntities(postExcerpt).replace(/(?:\r\n|\r|\n)/g, '');
p.excerpt = postExcerpt;
Expand Down Expand Up @@ -151,33 +152,40 @@ export default ( props ) => {
</${BlockControls}>
<${InspectorControls}>
<${PanelBody} title="Query Filters">
<${AuthorPicker}
<${AuthorPicker}
value=${attributes.author}
onChange=${(author) => setAttributes({author})}
/>
${taxonomies.map(t => html`
<${TermPicker}
key=${t}
<${TermPicker}
key=${t}
onChange=${onTermChange}
value=${attributes.terms[t]}
taxonomy=${t}/>
`)}
<${DebouncedText}
<${DebouncedText}
label="Keyword"
value=${attributes.search}
onChange=${(search) => setAttributes({search})}
/>
</${PanelBody}>
<${PanelBody} title="Display">
<${RangeControl}
<${RangeControl}
label="Number of posts"
value=${attributes.postCt}
onChange=${(postCt) => setAttributes({postCt})}
min=${1}
max=${20}
/>
<${SelectControl}
<${RangeControl}
label="Post offset"
value=${attributes.offset}
onChange=${(offset) => setAttributes({offset})}
min=${0}
help="Number of posts to skip before displaying results"
/>
<${SelectControl}
label='Template'
value=${attributes.template}
options=${templateOptions}
Expand All @@ -198,4 +206,4 @@ export default ( props ) => {
</div>
`;
};
};
6 changes: 5 additions & 1 deletion src/editor/lib/blocks/ucd-theme-recent-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const settings = {
type: "number",
default: 5
},
offset: {
type: "number",
default: 0
},
author: {
type: 'string',
default: ''
Expand Down Expand Up @@ -55,4 +59,4 @@ const settings = {
edit: Edit,
};

export default { name, settings };
export default { name, settings };
1 change: 1 addition & 0 deletions theme/includes/classes/block-transformations.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static function getPosts($attrs=array()){
if ( array_key_exists('search', $attrs) ) $args['s'] = $attrs['search'];
if ( array_key_exists('orderBy', $attrs) ) $args['orderby'] = $attrs['orderBy'];
if ( array_key_exists('order', $attrs) ) $args['order'] = $attrs['order'];
if ( array_key_exists('offset', $attrs) ) $args['offset'] = $attrs['offset'];
if ( array_key_exists('postCt', $attrs) ) {
$args['posts_per_page'] = $attrs['postCt'];
} else {
Expand Down

0 comments on commit b6e4af9

Please sign in to comment.