| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
const { __ } = wp.i18n; |
| 3 |
|
| 4 |
/** |
| 5 |
* Loading Groups |
| 6 |
*/ |
| 7 |
export function getGroupsFromApi(_this) { |
| 8 |
// retrieve the groups |
| 9 |
apiFetch({ path: _this.groupsEndPoint }) |
| 10 |
.then((groups) => { |
| 11 |
if (groups) { |
| 12 |
_this.setState({ groups }); |
| 13 |
} |
| 14 |
}) |
| 15 |
.catch((error) => { |
| 16 |
if (_this.isStillMounted && fetchRequest === _this.currentFetchRequest) { |
| 17 |
_this.setState({ |
| 18 |
response: { |
| 19 |
error: true, |
| 20 |
errorMsg: error.message, |
| 21 |
}, |
| 22 |
}); |
| 23 |
} |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Loading Taxonomies (own REST API endpoint) |
| 29 |
*/ |
| 30 |
export function getTaxonomiesFromApi(_this) { |
| 31 |
// retrieve the taxonomies |
| 32 |
apiFetch({ path: _this.taxonomiesEndPoint }) |
| 33 |
.then((taxonomies) => { |
| 34 |
if (taxonomies) { |
| 35 |
_this.setState({ taxonomies }); |
| 36 |
} |
| 37 |
}) |
| 38 |
.catch((error) => { |
| 39 |
if (_this.isStillMounted && fetchRequest === _this.currentFetchRequest) { |
| 40 |
_this.setState({ |
| 41 |
response: { |
| 42 |
error: true, |
| 43 |
errorMsg: error.message, |
| 44 |
}, |
| 45 |
}); |
| 46 |
} |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Loading Posts |
| 52 |
*/ |
| 53 |
export function getPostsFromApi(_this) { |
| 54 |
apiFetch({ path: '/wp/v2/posts?per_page=26' }) |
| 55 |
.then((fullPosts) => { |
| 56 |
if (fullPosts) { |
| 57 |
let posts = [ |
| 58 |
{ value: -1, label: __('off') }, |
| 59 |
{ value: 0, label: __('[use this post]') }, |
| 60 |
]; |
| 61 |
fullPosts.forEach((fullPost) => { |
| 62 |
posts.push({ |
| 63 |
value: fullPost.id, |
| 64 |
label: htmlDecode(fullPost.title.rendered), |
| 65 |
}); |
| 66 |
}); |
| 67 |
if (posts[27] !== undefined) { |
| 68 |
posts[27] = { |
| 69 |
value: -2, |
| 70 |
label: __('[list cut off after 25 posts]'), |
| 71 |
}; |
| 72 |
} |
| 73 |
_this.setState({ posts }); |
| 74 |
} |
| 75 |
}) |
| 76 |
.catch((error) => { |
| 77 |
if (_this.isStillMounted && fetchRequest === _this.currentFetchRequest) { |
| 78 |
_this.setState({ |
| 79 |
response: { |
| 80 |
error: true, |
| 81 |
errorMsg: error.message, |
| 82 |
}, |
| 83 |
}); |
| 84 |
} |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
function htmlDecode(input) { |
| 89 |
var e = document.createElement('textarea'); |
| 90 |
e.innerHTML = input; |
| 91 |
return e.childNodes.length === 0 ? '' : e.childNodes[0].nodeValue; |
| 92 |
} |
| 93 |
|