-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tags support in Query block (#25005)
* add tags support in Query block * refactor + tests for getTermsInfo * fetch more terms to show * add typedefs and jsdoc at getTermsInfo * fix add multiple terms * set $query properly
- Loading branch information
1 parent
6b95e0d
commit 9ced21b
Showing
11 changed files
with
190 additions
and
42 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
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
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
"pages": 1, | ||
"offset": 0, | ||
"categoryIds": [], | ||
"tagIds": [], | ||
"order": "desc", | ||
"orderBy": "date" | ||
} | ||
|
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,5 @@ | ||
export const MAX_FETCHED_TERMS = 100; | ||
|
||
export default { | ||
MAX_FETCHED_TERMS, | ||
}; |
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,21 @@ | ||
export const terms = [ | ||
{ | ||
count: 2, | ||
id: 4, | ||
meta: [], | ||
name: 'nba', | ||
parent: 0, | ||
slug: 'nba', | ||
taxonomy: 'category', | ||
}, | ||
{ | ||
count: 0, | ||
id: 11, | ||
link: 'http://localhost:8888/?tag=featured', | ||
name: 'featured', | ||
slug: 'featured', | ||
taxonomy: 'post_tag', | ||
}, | ||
]; | ||
|
||
export default { terms }; |
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,30 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { terms } from './fixtures'; | ||
import { getTermsInfo } from '../utils'; | ||
|
||
describe( 'Query block utils', () => { | ||
describe( 'getTermsInfo', () => { | ||
it( 'should return an empty object when no terms provided', () => { | ||
expect( getTermsInfo() ).toEqual( { terms: undefined } ); | ||
} ); | ||
it( 'should return proper terms info object', () => { | ||
expect( getTermsInfo( terms ) ).toEqual( | ||
expect.objectContaining( { | ||
mapById: expect.objectContaining( { | ||
'4': expect.objectContaining( { name: 'nba' } ), | ||
'11': expect.objectContaining( { | ||
name: 'featured', | ||
} ), | ||
} ), | ||
mapByName: expect.objectContaining( { | ||
nba: expect.objectContaining( { id: 4 } ), | ||
featured: expect.objectContaining( { id: 11 } ), | ||
} ), | ||
names: expect.arrayContaining( [ 'nba', 'featured' ] ), | ||
} ) | ||
); | ||
} ); | ||
} ); | ||
} ); |
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,47 @@ | ||
/** | ||
* WordPress term object from REST API. | ||
* Categories ref: https://developer.wordpress.org/rest-api/reference/categories/ | ||
* Tags ref: https://developer.wordpress.org/rest-api/reference/tags/ | ||
* | ||
* @typedef {Object} WPTerm | ||
* @property {number} id Unique identifier for the term. | ||
* @property {number} count Number of published posts for the term. | ||
* @property {string} description HTML description of the term. | ||
* @property {string} link URL of the term. | ||
* @property {string} name HTML title for the term. | ||
* @property {string} slug An alphanumeric identifier for the term unique to its type. | ||
* @property {string} taxonomy Type attribution for the term. | ||
* @property {Object} meta Meta fields | ||
* @property {number} [parent] The parent term ID. | ||
*/ | ||
|
||
/** | ||
* The object used in Query block that contains info and helper mappings | ||
* from an array of WPTerm. | ||
* | ||
* @typedef {Object} QueryTermsInfo | ||
* @property {WPTerm[]} terms The array of terms. | ||
* @property {Object<string, WPTerm>} mapById Object mapping with the term id as key and the term as value. | ||
* @property {Object<string, WPTerm>} mapByName Object mapping with the term name as key and the term as value. | ||
* @property {string[]} names Array with the terms' names. | ||
*/ | ||
|
||
/** | ||
* Returns a helper object with mapping from WPTerms. | ||
* | ||
* @param {WPTerm[]} terms The terms to extract of helper object. | ||
* @return {QueryTermsInfo} The object with the terms information. | ||
*/ | ||
export const getTermsInfo = ( terms ) => ( { | ||
terms, | ||
...terms?.reduce( | ||
( accumulator, term ) => { | ||
const { mapById, mapByName, names } = accumulator; | ||
mapById[ term.id ] = term; | ||
mapByName[ term.name ] = term; | ||
names.push( term.name ); | ||
return accumulator; | ||
}, | ||
{ mapById: {}, mapByName: {}, names: [] } | ||
), | ||
} ); |
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"pages": 1, | ||
"offset": 0, | ||
"categoryIds": [], | ||
"tagIds": [], | ||
"order": "desc", | ||
"orderBy": "date" | ||
} | ||
|
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