暫無描述

use-search-options.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * External dependencies
  3. */
  4. import { useMemo } from 'react';
  5. /**
  6. * WordPress dependencies
  7. */
  8. import { useEntityProp } from '@wordpress/core-data';
  9. /**
  10. * Fetches values and setters for various search configuration values.
  11. *
  12. * @returns {object} values and setters
  13. */
  14. export default function useSearchOptions() {
  15. const [ theme, setTheme ] = useEntityProp( 'root', 'site', 'jetpack_search_color_theme' );
  16. const [ resultFormat, setResultFormat ] = useEntityProp(
  17. 'root',
  18. 'site',
  19. 'jetpack_search_result_format'
  20. );
  21. const [ sort, setSort ] = useEntityProp( 'root', 'site', 'jetpack_search_default_sort' );
  22. const [ trigger, setTrigger ] = useEntityProp( 'root', 'site', 'jetpack_search_overlay_trigger' );
  23. const [ color, setColor ] = useEntityProp( 'root', 'site', 'jetpack_search_highlight_color' );
  24. const [ sortEnabled, setSortEnabled ] = useEntityProp(
  25. 'root',
  26. 'site',
  27. 'jetpack_search_enable_sort'
  28. );
  29. const [ infiniteScroll, setInfiniteScroll ] = useEntityProp(
  30. 'root',
  31. 'site',
  32. 'jetpack_search_inf_scroll'
  33. );
  34. const [ showLogo, setShowLogo ] = useEntityProp(
  35. 'root',
  36. 'site',
  37. 'jetpack_search_show_powered_by'
  38. );
  39. const [ excludedPostTypesCsv, setExcludedPostTypesCsv ] = useEntityProp(
  40. 'root',
  41. 'site',
  42. 'jetpack_search_excluded_post_types'
  43. );
  44. // Excluded Post Types is stored as a CSV string in site options. Convert into array of strings.
  45. // Caveat: csv can be an empty string, which can produces [ '' ] if only csv.split is used.
  46. const excludedPostTypes = useMemo(
  47. () => excludedPostTypesCsv?.split( ',' ).filter( type => type?.length > 0 ),
  48. [ excludedPostTypesCsv ]
  49. );
  50. const setExcludedPostTypes = postTypesArr => setExcludedPostTypesCsv( postTypesArr.join( ',' ) );
  51. return {
  52. color,
  53. excludedPostTypes,
  54. infiniteScroll,
  55. resultFormat,
  56. setColor,
  57. setExcludedPostTypes,
  58. setInfiniteScroll,
  59. setResultFormat,
  60. setShowLogo,
  61. setSort,
  62. setSortEnabled,
  63. setTheme,
  64. setTrigger,
  65. showLogo,
  66. sort,
  67. sortEnabled,
  68. theme,
  69. trigger,
  70. };
  71. }