11import {
2+ FieldPanel ,
23 RadioButtonControl ,
34 SearchableMulticheckControl ,
45} from '@content-control/components' ;
@@ -11,32 +12,153 @@ import {
1112} from '@wordpress/components' ;
1213import { __ } from '@wordpress/i18n' ;
1314import { search } from '@wordpress/icons' ;
15+ import { addFilter } from '@wordpress/hooks' ;
16+ import { type Restriction } from '@content-control/core-data' ;
1417
18+ import useFields from '../use-fields' ;
1519import { userStatusOptions } from '../options' ;
1620
1721import type { EditTabProps } from '.' ;
1822
1923/* Global Var Imports */
2024const { userRoles } = contentControlSettingsPage ;
2125
22- const GeneralTab = ( {
23- values,
24- updateValues,
25- updateSettings,
26- } : EditTabProps ) => {
27- const { settings } = values ;
26+ addFilter (
27+ 'contentControl.restrictionEditor.tabFields' ,
28+ 'content-control' ,
29+ (
30+ fields : Record <
31+ string ,
32+ { id : string ; priority : number ; component : React . JSX . Element } [ ]
33+ > ,
34+ settings : Restriction [ 'settings' ] ,
35+ updateSettings : (
36+ settings : Partial < Restriction [ 'settings' ] >
37+ ) => void
38+ ) => {
39+ // ** TODO REVIEW - This is here to ensure old data does not throw errors.
40+ // ** It may be that if we have dedicated migration routine this can be removed.
41+ let cleanedRoles : string [ ] = [ ] ;
2842
29- // ** TODO REVIEW - This is here to ensure old data does not throw errors.
30- // ** It may be that if we have dedicated migration routine this can be removed.
31- let cleanedRoles : string [ ] = [ ] ;
43+ if ( Array . isArray ( settings . userRoles ) ) {
44+ cleanedRoles = settings . userRoles ;
45+ } else if ( typeof settings . userRoles === 'object' ) {
46+ cleanedRoles = Object . entries ( settings . userRoles ) . map (
47+ ( [ value ] ) => value
48+ ) ;
49+ }
50+ return {
51+ ...fields ,
52+ general : [
53+ {
54+ id : 'userStatus' ,
55+ priority : 3 ,
56+ component : (
57+ < FieldPanel
58+ title = { __ ( 'User Status' , 'content-control' ) }
59+ >
60+ < RadioButtonControl
61+ label = { __ (
62+ 'Who can see this content?' ,
63+ 'content-control'
64+ ) }
65+ value = { settings . userStatus }
66+ onChange = { ( newUserStatus ) =>
67+ updateSettings ( {
68+ userStatus : newUserStatus ,
69+ } )
70+ }
71+ options = { userStatusOptions ( ) }
72+ className = "userStatus-field"
73+ />
3274
33- if ( Array . isArray ( settings . userRoles ) ) {
34- cleanedRoles = settings . userRoles ;
35- } else if ( typeof settings . userRoles === 'object' ) {
36- cleanedRoles = Object . entries ( settings . userRoles ) . map (
37- ( [ value ] ) => value
38- ) ;
75+ { 'logged_in' === settings . userStatus && (
76+ < >
77+ < SelectControl
78+ label = { __ (
79+ 'User Role' ,
80+ 'content-control'
81+ ) }
82+ value = { settings . roleMatch ?? 'any' }
83+ options = { [
84+ {
85+ label : __ (
86+ 'Any' ,
87+ 'content-control'
88+ ) ,
89+ value : 'any' ,
90+ } ,
91+ {
92+ label : __ (
93+ 'Matching' ,
94+ 'content-control'
95+ ) ,
96+ value : 'match' ,
97+ } ,
98+ {
99+ label : __ (
100+ 'Excluding' ,
101+ 'content-control'
102+ ) ,
103+ value : 'exclude' ,
104+ } ,
105+ ] }
106+ onChange = { ( newRoleMatch ) =>
107+ updateSettings ( {
108+ roleMatch : newRoleMatch as
109+ | 'any'
110+ | 'match'
111+ | 'exclude' ,
112+ } )
113+ }
114+ className = "is-large roleMatch-field"
115+ />
116+
117+ { 'any' !== settings . roleMatch && (
118+ < SearchableMulticheckControl
119+ label = {
120+ 'exclude' === settings . roleMatch
121+ ? __ (
122+ 'Roles to exclude' ,
123+ 'content-control'
124+ )
125+ : __ (
126+ 'Roles to include' ,
127+ 'content-control'
128+ )
129+ }
130+ searchIcon = { search }
131+ placeholder = { __ (
132+ 'Search roles…' ,
133+ 'content-control'
134+ ) }
135+ className = "is-large userRoles-field"
136+ value = { cleanedRoles }
137+ onChange = { ( roles ) =>
138+ updateSettings ( {
139+ userRoles : roles ,
140+ } )
141+ }
142+ options = { Object . entries (
143+ userRoles
144+ ) . map ( ( [ value , label ] ) => ( {
145+ value,
146+ label,
147+ } ) ) }
148+ />
149+ ) }
150+ </ >
151+ ) }
152+ </ FieldPanel >
153+ ) ,
154+ } ,
155+ ] ,
156+ } ;
39157 }
158+ ) ;
159+
160+ const GeneralTab = ( { values, updateValues } : EditTabProps ) => {
161+ const { getTabFields } = useFields ( ) ;
40162
41163 const descriptionRowEst = values . description . length / 80 ;
42164 const descriptionRows = clamp ( descriptionRowEst , 1 , 5 ) ;
@@ -74,79 +196,9 @@ const GeneralTab = ( {
74196 </ Notice >
75197 ) }
76198
77- < RadioButtonControl
78- label = { __ ( 'Who can see this content?' , 'content-control' ) }
79- value = { settings . userStatus }
80- onChange = { ( newUserStatus ) =>
81- updateSettings ( { userStatus : newUserStatus } )
82- }
83- options = { userStatusOptions }
84- className = "userStatus-field"
85- />
86-
87- { 'logged_in' === settings . userStatus && (
88- < >
89- < SelectControl
90- label = { __ ( 'User Role' , 'content-control' ) }
91- value = { settings . roleMatch ?? 'any' }
92- options = { [
93- {
94- label : __ ( 'Any' , 'content-control' ) ,
95- value : 'any' ,
96- } ,
97- {
98- label : __ ( 'Matching' , 'content-control' ) ,
99- value : 'match' ,
100- } ,
101- {
102- label : __ ( 'Excluding' , 'content-control' ) ,
103- value : 'exclude' ,
104- } ,
105- ] }
106- onChange = { ( newRoleMatch ) =>
107- updateSettings ( {
108- roleMatch : newRoleMatch as
109- | 'any'
110- | 'match'
111- | 'exclude' ,
112- } )
113- }
114- className = "is-large roleMatch-field"
115- />
116-
117- { 'any' !== settings . roleMatch && (
118- < SearchableMulticheckControl
119- label = {
120- 'exclude' === settings . roleMatch
121- ? __ (
122- 'Roles to exclude' ,
123- 'content-control'
124- )
125- : __ (
126- 'Roles to include' ,
127- 'content-control'
128- )
129- }
130- searchIcon = { search }
131- placeholder = { __ (
132- 'Search roles…' ,
133- 'content-control'
134- ) }
135- className = "is-large userRoles-field"
136- value = { cleanedRoles }
137- onChange = { ( roles ) =>
138- updateSettings ( { userRoles : roles } )
139- }
140- options = { Object . entries ( userRoles ) . map (
141- ( [ value , label ] ) => ( {
142- value,
143- label,
144- } )
145- ) }
146- />
147- ) }
148- </ >
149- ) }
199+ { getTabFields ( 'general' ) . map ( ( field ) => (
200+ < div key = { field . id } > { field . component } </ div >
201+ ) ) }
150202 </ div >
151203 ) ;
152204} ;
0 commit comments