Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getAllTerms,
deleteAllTerms,
getTermBySlug,
createTerm,
} from './taxonomies/index.js';
import { fillInXliffExportForm, getXliffRegex } from './xliff/index.js';
import { getDownload, getStringFromFile } from './downloads/index.js';
Expand All @@ -30,6 +31,7 @@ export {
getAllTerms,
deleteAllTerms,
getTermBySlug,
createTerm,
fillInXliffExportForm,
getDownload,
getXliffRegex,
Expand Down
35 changes: 35 additions & 0 deletions src/taxonomies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ export const getTermBySlug = async ( requestUtils, taxonomy, slug ) => {
} );
};

let termCounter = 0;

/**
* Creates a term.
*
* @param {RequestUtils} requestUtils Gutenberg request utils object.
* @param {string} taxonomy Taxonomy slug used in related REST endpoint (e.g., 'categories', 'tags').
* @param {Object} [args = {}] Additional arguments to pass to the request.
* @param {string} [args.name] Term name (required).
* @param {string} [args.description] Term description.
* @param {string} [args.slug] Term slug.
* @param {number} [args.parent] Parent term ID.
* @param {string} [args.lang] Language slug.
* @return {Promise} Request promise.
*/
export const createTerm = async (
requestUtils,
taxonomy = 'categories',
args = {}
) => {
const defaultArgs = {
...args,
};

if ( ! defaultArgs.name ) {
defaultArgs.name = `Term ${ ++termCounter }`;
}
Comment on lines +64 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add it directly in defaultArgs definition, right before the spread operator. Am I right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not if we want to increment the counter only when needed. Putting it in defaultArgs would increment the counter even when args.name is provided, wasting numbers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see, my bad.


return requestUtils.rest( {
method: 'POST',
path: `${ BASE_PATH }/${ taxonomy }`,
data: defaultArgs,
} );
};

/**
* Deletes all terms.
*
Expand Down