22 * Locations Section Component
33 *
44 * Handles location rules for where the fieldset should appear.
5+ * Fetches actual templates, categories, and post formats from WordPress
6+ * via the localized `window.openfieldsAdmin` data OR the REST API.
57 *
68 * @package OpenFields
79 */
810
11+ import { useEffect , useState } from 'react' ;
912import { Button } from '../../../components/ui/button' ;
1013import { Card } from '../../../components/ui/card' ;
1114import {
@@ -23,10 +26,72 @@ interface LocationsSectionProps {
2326 onLocationGroupsChange : ( groups : LocationGroup [ ] ) => void ;
2427}
2528
29+ interface OptionItem {
30+ name : string ;
31+ label : string ;
32+ }
33+
2634export function LocationsSection ( {
2735 locationGroups,
2836 onLocationGroupsChange,
2937} : LocationsSectionProps ) {
38+ // State for dynamically fetched location options
39+ const [ pageTemplates , setPageTemplates ] = useState < OptionItem [ ] > ( [ ] ) ;
40+ const [ categories , setCategories ] = useState < OptionItem [ ] > ( [ ] ) ;
41+ const [ postFormats , setPostFormats ] = useState < OptionItem [ ] > ( [ ] ) ;
42+
43+ // Load location options from WordPress localized data or REST API
44+ useEffect ( ( ) => {
45+ // Page templates: from localized data or REST API
46+ const wpTemplates = window . openfieldsAdmin ?. pageTemplates ;
47+ if ( wpTemplates && wpTemplates . length > 0 ) {
48+ setPageTemplates ( wpTemplates ) ;
49+ } else {
50+ // Fallback: fetch from REST API
51+ const restUrl = window . openfieldsAdmin ?. restUrl || '/wp-json/codeideal-open-fields/v1' ;
52+ const nonce = window . openfieldsAdmin ?. nonce || '' ;
53+ fetch ( `${ restUrl } /locations/types` , {
54+ headers : { 'X-WP-Nonce' : nonce } ,
55+ } )
56+ . then ( ( res ) => res . json ( ) )
57+ . then ( ( types : { key : string ; label : string ; options : { value : string ; label : string } [ ] } [ ] ) => {
58+ const templateType = types . find ( ( t ) => t . key === 'page_template' ) ;
59+ if ( templateType ?. options ) {
60+ setPageTemplates (
61+ templateType . options . map ( ( o ) => ( { name : o . value , label : o . label } ) )
62+ ) ;
63+ }
64+ const catType = types . find ( ( t ) => t . key === 'post_category' ) ;
65+ if ( catType ?. options ) {
66+ setCategories (
67+ catType . options . map ( ( o ) => ( { name : o . value , label : o . label } ) )
68+ ) ;
69+ }
70+ const formatType = types . find ( ( t ) => t . key === 'post_format' ) ;
71+ if ( formatType ?. options ) {
72+ setPostFormats (
73+ formatType . options . map ( ( o ) => ( { name : o . value , label : o . label } ) )
74+ ) ;
75+ }
76+ } )
77+ . catch ( ( ) => {
78+ // Fallback defaults
79+ setPageTemplates ( [ { name : 'default' , label : 'Default Template' } ] ) ;
80+ } ) ;
81+ }
82+
83+ // Categories: from localized data
84+ const wpCategories = window . openfieldsAdmin ?. categories ;
85+ if ( wpCategories && wpCategories . length > 0 ) {
86+ setCategories ( wpCategories ) ;
87+ }
88+
89+ // Post formats: from localized data
90+ const wpFormats = window . openfieldsAdmin ?. postFormats ;
91+ if ( wpFormats && wpFormats . length > 0 ) {
92+ setPostFormats ( wpFormats ) ;
93+ }
94+ } , [ ] ) ;
3095 // Update a rule in a group
3196 const handleUpdateRule = (
3297 groupIndex : number ,
@@ -91,8 +156,8 @@ export function LocationsSection({
91156 ] ) ;
92157 } ;
93158
94- // Get value options based on rule type
95- const getValueOptions = ( ruleType : string ) => {
159+ // Get value options based on rule type — uses dynamic data from WordPress
160+ const getValueOptions = ( ruleType : string ) : OptionItem [ ] => {
96161 switch ( ruleType ) {
97162 case 'post_type' :
98163 return (
@@ -117,10 +182,13 @@ export function LocationsSection({
117182 ]
118183 ) ;
119184 case 'page_template' :
120- return [
121- { name : 'default' , label : 'Default Template' } ,
122- { name : 'full-width' , label : 'Full Width' } ,
123- ] ;
185+ return pageTemplates . length > 0
186+ ? pageTemplates
187+ : [ { name : 'default' , label : 'Default Template' } ] ;
188+ case 'post_category' :
189+ return categories ;
190+ case 'post_format' :
191+ return postFormats ;
124192 default :
125193 return [ ] ;
126194 }
@@ -173,6 +241,12 @@ export function LocationsSection({
173241 < SelectItem value = "page_template" >
174242 Page Template
175243 </ SelectItem >
244+ < SelectItem value = "post_category" >
245+ Post Category
246+ </ SelectItem >
247+ < SelectItem value = "post_format" >
248+ Post Format
249+ </ SelectItem >
176250 < SelectItem value = "taxonomy" > Taxonomy</ SelectItem >
177251 < SelectItem value = "user_role" > User Role</ SelectItem >
178252 </ SelectContent >
0 commit comments