utils/wp-api/utils
Functions
Section titled “Functions”apiEndpoint()
Section titled “apiEndpoint()”function apiEndpoint( endpoint: string, type: "categories" | "tags" | "posts" | "pages" | "media" | "settings", path?: string): URL
Defined in: studiocms/packages/studiocms_devapps/src/utils/wp-api/utils.ts:165
Constructs a WordPress API endpoint URL based on the provided parameters.
Parameters
Section titled “Parameters”endpoint
Section titled “endpoint”string
The base URL of the WordPress website.
The type of resource to access. Can be ‘posts’, ‘pages’, ‘media’, ‘categories’, ‘tags’, or ‘settings’.
"categories"
| "tags"
| "posts"
| "pages"
| "media"
| "settings"
string
An optional path to append to the endpoint.
Returns
Section titled “Returns”URL
The constructed URL object pointing to the desired API endpoint.
Throws
Section titled “Throws”If the endpoint
argument is missing.
cleanUpHtml()
Section titled “cleanUpHtml()”function cleanUpHtml(html: string): any
Defined in: studiocms/packages/studiocms_devapps/src/utils/wp-api/utils.ts:25
Cleans up the provided HTML string by removing certain attributes from images and modifying specific elements related to WordPress polls.
Parameters
Section titled “Parameters”string
The HTML string to be cleaned up.
Returns
Section titled “Returns”any
The cleaned-up HTML string.
downloadAndUpdateImages()
Section titled “downloadAndUpdateImages()”function downloadAndUpdateImages(html: string, pathToFolder: string): Promise<any>
Defined in: studiocms/packages/studiocms_devapps/src/utils/wp-api/utils.ts:140
Downloads and updates the image sources in the provided HTML string.
This function parses the given HTML string, finds all image elements, downloads the images to the specified folder, and updates the image sources to point to the downloaded images.
Parameters
Section titled “Parameters”string
The HTML string containing image elements to be processed.
pathToFolder
Section titled “pathToFolder”string
The path to the folder where images should be downloaded.
Returns
Section titled “Returns”Promise
<any
>
A promise that resolves to the updated HTML string with new image sources.
downloadImage()
Section titled “downloadImage()”function downloadImage(imageUrl: string | URL, destination: string | URL): Promise<boolean>
Defined in: studiocms/packages/studiocms_devapps/src/utils/wp-api/utils.ts:51
Downloads an image from the specified URL and saves it to the given destination.
Parameters
Section titled “Parameters”imageUrl
Section titled “imageUrl”The URL of the image to download.
string
| URL
destination
Section titled “destination”The file path where the image should be saved.
string
| URL
Returns
Section titled “Returns”Promise
<boolean
>
- A promise that resolves to true if the image was successfully downloaded, or false if the download failed or the file already exists.
Throws
Section titled “Throws”- Throws an error if there is an issue with the fetch request or file writing.
downloadPostImage()
Section titled “downloadPostImage()”function downloadPostImage(src: string, pathToFolder: string): Promise<undefined | string>
Defined in: studiocms/packages/studiocms_devapps/src/utils/wp-api/utils.ts:102
Downloads an image from the given source URL and saves it to the specified folder.
Parameters
Section titled “Parameters”string
The URL of the image to download.
pathToFolder
Section titled “pathToFolder”string
The path to the folder where the image should be saved.
Returns
Section titled “Returns”Promise
<undefined
| string
>
The file name of the downloaded image if successful, otherwise undefined
.
Remarks
Section titled “Remarks”- If the
src
orpathToFolder
parameters are not provided, the function will return immediately. - If the specified folder does not exist, it will be created recursively.
- If the image already exists in the specified folder, the function will log a message and skip the download.
- If the image download fails, the source URL will be added to the
imagesNotDownloaded
array.
fetchAll()
Section titled “fetchAll()”function fetchAll( url: URL, page: number,results: any[]): Promise<any[]>
Defined in: studiocms/packages/studiocms_devapps/src/utils/wp-api/utils.ts:194
Fetch all pages for a paginated WP endpoint.
Parameters
Section titled “Parameters”URL
number
= 1
results
Section titled “results”any
[] = []
Returns
Section titled “Returns”Promise
<any
[]>
stripHtml()
Section titled “stripHtml()”function stripHtml(string: string): string
Defined in: studiocms/packages/studiocms_devapps/src/utils/wp-api/utils.ts:14
Removes all HTML tags from a given string.
Parameters
Section titled “Parameters”string
Section titled “string”string
The input string containing HTML tags.
Returns
Section titled “Returns”string
The input string with all HTML tags removed.