| 1 |
import { useEffect, useState } from "@wordpress/element"; |
| 2 |
import { jsonStringify } from "../blocks/shared/helpFn"; |
| 3 |
import { filterDndSelectValues, filteredTaxonomiesValues, queryFn } from "../controls/controls"; |
| 4 |
import { useSelect } from "@wordpress/data"; |
| 5 |
|
| 6 |
const useMetaData = (attributes, editSite = "frontend") => { |
| 7 |
const { |
| 8 |
postType, |
| 9 |
uniqueId, |
| 10 |
multiplePostType, |
| 11 |
quickQuery, |
| 12 |
postLimit, |
| 13 |
offset, |
| 14 |
taxonomies, |
| 15 |
categories, |
| 16 |
filterByAuthor, |
| 17 |
filterByDate, |
| 18 |
filterByKeyword, |
| 19 |
filterByCustomFields, |
| 20 |
orderBy, |
| 21 |
orderDirection, |
| 22 |
includeOnlyPost, |
| 23 |
excludePost, |
| 24 |
excludeTerm, |
| 25 |
excludeAuthor, |
| 26 |
excludeStickyPosts, |
| 27 |
excludeCurrentPosts, |
| 28 |
excludeProtectedPosts, |
| 29 |
excludeChildrenPosts, |
| 30 |
excludePostWithoutImagePosts, |
| 31 |
termId, |
| 32 |
filterProduct, |
| 33 |
relation, |
| 34 |
specificDate, |
| 35 |
specificMonth, |
| 36 |
specificYear, |
| 37 |
specificPeriodAfter, |
| 38 |
specificPeriodBefore, |
| 39 |
specificDateBefore, |
| 40 |
specificDateAfter, |
| 41 |
keywordSearch, |
| 42 |
currentPage, |
| 43 |
itemsPerPage, |
| 44 |
blockName, |
| 45 |
displayAdvertisement, |
| 46 |
page_id, |
| 47 |
excludeDateAfter, |
| 48 |
excludeDateBefore, |
| 49 |
} = attributes; |
| 50 |
|
| 51 |
// const postId = page_id ? page_id : useSelect((select) => select("core/editor")?.getCurrentPostId()); |
| 52 |
const postId = 0; |
| 53 |
|
| 54 |
const [metaData, setMetaData] = useState({ |
| 55 |
authors: [], |
| 56 |
authorList: [], |
| 57 |
allTaxonomies: [], |
| 58 |
postCount: {}, |
| 59 |
imageSizes: [], |
| 60 |
allPostTypes: [], |
| 61 |
allMetaKeys: [], |
| 62 |
}); |
| 63 |
|
| 64 |
const filteredTaxonomies = filteredTaxonomiesValues(taxonomies); |
| 65 |
const filteredIncludedPost = filterDndSelectValues(includeOnlyPost); |
| 66 |
|
| 67 |
const dependencies = { |
| 68 |
postType, |
| 69 |
blockName, |
| 70 |
quickQuery, |
| 71 |
postLimit, |
| 72 |
offset, |
| 73 |
categories, |
| 74 |
filterByAuthor, |
| 75 |
filterByDate, |
| 76 |
filterByKeyword, |
| 77 |
filterByCustomFields, |
| 78 |
orderBy, |
| 79 |
orderDirection, |
| 80 |
excludePost, |
| 81 |
excludeTerm, |
| 82 |
excludeAuthor, |
| 83 |
excludeStickyPosts, |
| 84 |
excludeCurrentPosts, |
| 85 |
excludeProtectedPosts, |
| 86 |
excludeChildrenPosts, |
| 87 |
excludePostWithoutImagePosts, |
| 88 |
termId, |
| 89 |
filterProduct, |
| 90 |
relation, |
| 91 |
specificDate, |
| 92 |
specificMonth, |
| 93 |
specificYear, |
| 94 |
specificPeriodAfter, |
| 95 |
specificPeriodBefore, |
| 96 |
specificDateBefore, |
| 97 |
specificDateAfter, |
| 98 |
keywordSearch, |
| 99 |
currentPage, |
| 100 |
blockName, |
| 101 |
displayAdvertisement, |
| 102 |
multiplePostType, |
| 103 |
postId, |
| 104 |
page_id, |
| 105 |
excludeDateAfter, |
| 106 |
editSite, |
| 107 |
excludeDateBefore, |
| 108 |
taxonomies: filteredTaxonomies, |
| 109 |
includeOnlyPost: filteredIncludedPost, |
| 110 |
}; |
| 111 |
|
| 112 |
const queryData = { |
| 113 |
...dependencies, |
| 114 |
}; |
| 115 |
|
| 116 |
const data = new FormData(); |
| 117 |
// const queryData = { postType, multiplePostType }; |
| 118 |
data.append("nonce", sp_smart_post_block_localize.ajaxNonce); |
| 119 |
data.append("action", "sp_smart_post_block_meta_data_query"); |
| 120 |
data.append("metaQueryData", jsonStringify(queryData)); |
| 121 |
|
| 122 |
const fetchMetaData = async () => { |
| 123 |
try { |
| 124 |
const apiData = await queryFn(data); |
| 125 |
const { |
| 126 |
authors, |
| 127 |
taxonomies, |
| 128 |
author_list, |
| 129 |
post_count, |
| 130 |
image_sizes, |
| 131 |
all_post_type_list, |
| 132 |
all_meta_field_list, |
| 133 |
} = apiData; |
| 134 |
setMetaData({ |
| 135 |
authors, |
| 136 |
authorList: author_list, |
| 137 |
allTaxonomies: taxonomies, |
| 138 |
postCount: post_count, |
| 139 |
imageSizes: image_sizes, |
| 140 |
allPostTypes: all_post_type_list ? all_post_type_list : {}, |
| 141 |
allMetaKeys: all_meta_field_list, |
| 142 |
}); |
| 143 |
} catch (error) { |
| 144 |
console.log("Error fetching metadata:", error.message); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
useEffect(() => { |
| 149 |
const allTemplatesRoute = |
| 150 |
window.location.search.includes("p=%2Ftemplate") || window.location.search.includes("p=/template"); |
| 151 |
if (!allTemplatesRoute) { |
| 152 |
fetchMetaData(); |
| 153 |
} |
| 154 |
}, [jsonStringify(queryData)]); |
| 155 |
return metaData; |
| 156 |
}; |
| 157 |
|
| 158 |
export default useMetaData; |
| 159 |
|