utils.js
69 lines
| 1 | const {useSelect} = wp.data; |
| 2 | import { __ } from '@wordpress/i18n' |
| 3 | |
| 4 | /** |
| 5 | * Get array of form options for a select control |
| 6 | * |
| 7 | * @since 2.9.0 |
| 8 | * @return {array} Array of options for a select control |
| 9 | */ |
| 10 | export const useFormOptions = () => { |
| 11 | const formOptions = useSelect((select) => { |
| 12 | const records = select('core').getEntityRecords('postType', 'give_forms'); |
| 13 | if (records) { |
| 14 | return records.map((record) => { |
| 15 | return { |
| 16 | label: record.title.rendered ? record.title.rendered : __('(no title)'), |
| 17 | value: record.id, |
| 18 | }; |
| 19 | }); |
| 20 | } |
| 21 | return []; |
| 22 | }, []); |
| 23 | return formOptions; |
| 24 | }; |
| 25 | |
| 26 | /** |
| 27 | * Get array of form tag options for a select control |
| 28 | * |
| 29 | * @since 2.9.0 |
| 30 | * @return {array} Array of options for a select control |
| 31 | */ |
| 32 | export const useTagOptions = () => { |
| 33 | const tagOptions = useSelect((select) => { |
| 34 | const records = select('core').getEntityRecords('taxonomy', 'give_forms_tag', {per_page: 100}); |
| 35 | if (records) { |
| 36 | return records.map((record) => { |
| 37 | return { |
| 38 | label: record.name ? record.name : __('(no title)'), |
| 39 | value: record.id, |
| 40 | }; |
| 41 | }); |
| 42 | } |
| 43 | return []; |
| 44 | }, []); |
| 45 | return tagOptions; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Get array of form category options for a select control |
| 50 | * |
| 51 | * @since 2.9.0 |
| 52 | * @return {array} Array of options for a select control |
| 53 | */ |
| 54 | export const useCategoryOptions = () => { |
| 55 | const categoryOptions = useSelect((select) => { |
| 56 | const records = select('core').getEntityRecords('taxonomy', 'give_forms_category', {per_page: 100}); |
| 57 | if (records) { |
| 58 | return records.map((record) => { |
| 59 | return { |
| 60 | label: record.name ? record.name : __('(no title)'), |
| 61 | value: record.id, |
| 62 | }; |
| 63 | }); |
| 64 | } |
| 65 | return []; |
| 66 | }, []); |
| 67 | return categoryOptions; |
| 68 | }; |
| 69 |