GenerateBgImage.js
5 years ago
asyncHooks.js
5 years ago
capitalize.js
5 years ago
color-code-to-class.js
5 years ago
color-slug-to-color-code.js
4 years ago
common.scss
4 years ago
convert-to-grid.js
5 years ago
default-color-palette.js
5 years ago
delete-from-array.js
5 years ago
depModules.js
5 years ago
example-data.js
5 years ago
fixBrokenUnicode.js
5 years ago
font-awesome-new.js
5 years ago
font-awesome.js
5 years ago
formatNumCol.js
5 years ago
hex-to-rgba.js
5 years ago
hiddenNewBlock.js
5 years ago
hooks.js
5 years ago
is-hex-color.js
4 years ago
is-not-json.js
5 years ago
multi-array-flaten.js
5 years ago
removeProperty.js
5 years ago
replaceClientId.js
5 years ago
taxonomy-utils.js
5 years ago
to-number.js
5 years ago
hooks.js
91 lines
| 1 | import { useSelect } from '@wordpress/data'; |
| 2 | |
| 3 | export const usePostTypes = () => { |
| 4 | return useSelect((select) => { |
| 5 | return select('core').getPostTypes({ per_page: -1 }) || []; |
| 6 | }, []); |
| 7 | }; |
| 8 | |
| 9 | export const usePostType = (postType) => { |
| 10 | return useSelect( |
| 11 | (select) => { |
| 12 | return select('core').getPostType(postType) || {}; |
| 13 | }, |
| 14 | [postType] |
| 15 | ); |
| 16 | }; |
| 17 | |
| 18 | export const useTaxonomies = () => { |
| 19 | return useSelect((select) => { |
| 20 | return select('core').getTaxonomies({ per_page: -1 }) || []; |
| 21 | }, []); |
| 22 | }; |
| 23 | |
| 24 | export const usePostTypeTaxonomies = (postType) => { |
| 25 | return useSelect( |
| 26 | (select) => { |
| 27 | return (select('core').getTaxonomies() || []).filter((taxonomy) => { |
| 28 | const postTypeTaxonomies = postType.taxonomies || []; |
| 29 | return postTypeTaxonomies.includes(taxonomy.slug); |
| 30 | }); |
| 31 | }, |
| 32 | [postType] |
| 33 | ); |
| 34 | }; |
| 35 | |
| 36 | export const useTermsGroupbyTaxnomy = (taxonomies) => { |
| 37 | return useSelect( |
| 38 | (select) => { |
| 39 | const obj = {}; |
| 40 | for (const taxonomy of taxonomies) { |
| 41 | obj[taxonomy.rest_base] = |
| 42 | select('core').getEntityRecords('taxonomy', taxonomy.slug, { |
| 43 | per_page: -1, |
| 44 | }) || []; |
| 45 | } |
| 46 | return obj; |
| 47 | }, |
| 48 | [taxonomies] |
| 49 | ); |
| 50 | }; |
| 51 | |
| 52 | export const usePosts = (postType, query) => { |
| 53 | return useSelect( |
| 54 | (select) => { |
| 55 | return ( |
| 56 | select('core').getEntityRecords( |
| 57 | 'postType', |
| 58 | postType.slug, |
| 59 | query |
| 60 | ) || [] |
| 61 | ); |
| 62 | }, |
| 63 | [postType, query] |
| 64 | ); |
| 65 | }; |
| 66 | |
| 67 | export const useCurrentPostId = () => { |
| 68 | return useSelect((select) => { |
| 69 | return select('core/editor').getCurrentPostId(); |
| 70 | }); |
| 71 | }; |
| 72 | |
| 73 | export const useCurrentPostType = () => { |
| 74 | return useSelect((select) => { |
| 75 | return select('core/editor').getCurrentPostType(); |
| 76 | }); |
| 77 | }; |
| 78 | |
| 79 | export const useCurrentBlocks = () => { |
| 80 | return useSelect((select) => { |
| 81 | return select('core/block-editor').getBlocks(); |
| 82 | }); |
| 83 | }; |
| 84 | |
| 85 | export const useBlocksByName = (blockName) => |
| 86 | // eslint-disable-next-line no-shadow |
| 87 | useSelect((select) => { |
| 88 | const { getBlocks } = select('core/block-editor'); |
| 89 | return getBlocks().filter((block) => block.name === blockName); |
| 90 | }, []); |
| 91 |