-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from rosswintle/1-7-0-update
- Loading branch information
Showing
17 changed files
with
1,872 additions
and
239 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export default class Acf { | ||
|
||
constructor() { | ||
|
||
} | ||
|
||
/** | ||
* Returns true if the code detects that ACF is installed | ||
* (only works back-end). | ||
* | ||
* @returns {boolean} | ||
*/ | ||
isAcfInstalled() { | ||
return null !== document.getElementById('toplevel_page_edit-post_type-acf-field-group') | ||
} | ||
|
||
/** | ||
* Returns the URL of the ACF screen in the backend. | ||
* (TODO: Store this somewhere?) | ||
* | ||
* @returns {string} | ||
*/ | ||
acfScreenUrl() { | ||
const elem = /** @type {HTMLAnchorElement} */(document.getElementById('toplevel_page_edit-post_type-acf-field-group')?.querySelector('a.menu-top')); | ||
return elem?.href; | ||
} | ||
|
||
/** | ||
* Gets the field groups from the ACF screen | ||
* Returns a promise that resolves to an array of objects like: | ||
* { | ||
* label: <label> | ||
* link: <url> | ||
* } | ||
* | ||
* @returns {Promise} | ||
*/ | ||
async getFieldGroups() { | ||
const response = await fetch( | ||
this.acfScreenUrl(), | ||
{ | ||
"headers": { | ||
'X-Requested-With': 'XMLHttpRequest', | ||
'Cache-Control': 'no-cache' | ||
}, | ||
"method": "GET", | ||
"credentials": 'include', | ||
"cache": "no-cache", | ||
"mode": "cors" | ||
} | ||
) | ||
const text = await response.text() | ||
const dom = document.createRange().createContextualFragment(text) | ||
const items = /** @type {NodeListOf<HTMLAnchorElement>} */( | ||
dom.querySelectorAll('tr.type-acf-field-group a.row-title') | ||
) | ||
return Array.from(items).map( | ||
item => { | ||
return { label: item.innerText, link: item.href } | ||
}, | ||
items | ||
) | ||
} | ||
|
||
} |
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
Oops, something went wrong.