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