Utility Functions
The pmc-gutenberg plugin has several service (opens in a new tab)
and util (opens in a new tab) functions
available to the codebase to help with data transformation, to house frequently-used functions, or work
with WordPress data effectively.
Each service or utility function MUST have a corresponding jest (opens in a new tab) unit test to accompany it.
We should be adding READMEs for each service or util function if it's not immediatly obvious how the function is intended to work, though historically this has been ignored.
What's the Differnce?
- 👉 service function
Something that services the codebase. It may be unique to this feature or work with data in a very specific format or extend some aspect of the codebase. For example, a function that takes a
WP_Postobject and returns an new object with formatted or manipulated data.
- 👉 utility (util) function
A function that could be pulled from the codebase, dropped into another, and it would function as expected. Data format, feature, or dependency agnostic. For example, a function that consumes an array and returns non-even values.
checkIfPostHasTerm
Helper to check if the post has term with the mentioned slug assigned or not.
termSlug: string — Slug of the post term.
commaSeparate
Converts array to comma-separated string, separated by and for two or ending the last item with and and an Oxford comma.
array: Array — Array of strings.
getStatusColor
Given a status, return a color string.
status: string — Status string.
isSameValueName
Determines if term name is the same.
termA: string — Term name a to compare.termB: string — Term name b to compare.
isValidHref
Check for issues with the provided href.
href: string — The href.
isValidURL
Check if a given string is a valid URL.
str: string — String to check.
namesToIds
Convert entity names to ids.
names: Array — Array of names.entities: Array — Array of entity objects.
parseShortcode
Helper to parse shortcodes.
shortcode: string — Shortcode meta value.
stripHTMLTags
Helper to strip HTML tags for a given string.
data: string — HTML content.
unescapeString
Replace apostrophe character with regular one.
arg: string — String to unescape.
unescapeTerm
Un-escapes term name.
term: Object — Term object.
unescapeTerms
Un-escape term name for each in array of objects.
terms: Array — Array of term objects.
getKey
Get prefixed key.
_id: string|number — Value to prefix.
useLinkInstanceKey
Builds a unique link control key for the given object reference.
instance: Object — An unique object reference specific to this link control instance.