| 1 |
|
| 2 |
import { Fragment } from "react"; |
| 3 |
import { __ } from "@wordpress/i18n"; |
| 4 |
import { decodeEntities } from "@wordpress/html-entities"; |
| 5 |
import { |
| 6 |
BaseControl, |
| 7 |
CheckboxControl, |
| 8 |
SelectControl, |
| 9 |
TextareaControl, |
| 10 |
ToggleControl, |
| 11 |
__experimentalInputControl as InputControl, |
| 12 |
__experimentalToggleGroupControl as ToggleGroupControl, |
| 13 |
__experimentalToggleGroupControlOption as ToggleGroupControlOption, |
| 14 |
} from "@wordpress/components"; |
| 15 |
|
| 16 |
export function getRestrictionHelpMessage(wppbContentRestriction, userRoles) { |
| 17 |
let helpMessage = ""; |
| 18 |
let rolesSelected = false; |
| 19 |
|
| 20 |
switch (wppbContentRestriction.display_to) { |
| 21 |
case "all": |
| 22 |
helpMessage = __( |
| 23 |
"This content is not restricted and can be seen by all users.", |
| 24 |
"profile-builder", |
| 25 |
); |
| 26 |
break; |
| 27 |
case "": |
| 28 |
helpMessage = __( |
| 29 |
"This content is restricted and can only be seen by logged in users", |
| 30 |
"profile-builder", |
| 31 |
); |
| 32 |
if ( |
| 33 |
wppbContentRestriction.user_roles && |
| 34 |
wppbContentRestriction.user_roles.length !== 0 |
| 35 |
) { |
| 36 |
rolesSelected = true; |
| 37 |
|
| 38 |
helpMessage += __( |
| 39 |
" that have the following user roles: ", |
| 40 |
"profile-builder", |
| 41 |
); |
| 42 |
wppbContentRestriction.user_roles.map((slug) => { |
| 43 |
userRoles.map((userRole) => { |
| 44 |
if (userRole.slug === slug) { |
| 45 |
helpMessage += userRole.name + ", "; |
| 46 |
} |
| 47 |
}); |
| 48 |
}); |
| 49 |
helpMessage = helpMessage.slice(0, -2); |
| 50 |
} |
| 51 |
if ( |
| 52 |
wppbContentRestriction.users_ids && |
| 53 |
wppbContentRestriction.users_ids.length !== 0 |
| 54 |
) { |
| 55 |
if (rolesSelected) { |
| 56 |
helpMessage += __(" and", "profile-builder"); |
| 57 |
} |
| 58 |
helpMessage += __( |
| 59 |
" that have the following user IDs: ", |
| 60 |
"profile-builder", |
| 61 |
); |
| 62 |
helpMessage += wppbContentRestriction.users_ids; |
| 63 |
} |
| 64 |
helpMessage += "."; |
| 65 |
break; |
| 66 |
case "not_logged_in": |
| 67 |
helpMessage = __( |
| 68 |
"This content is restricted and can only be seen by logged out users.", |
| 69 |
"profile-builder", |
| 70 |
); |
| 71 |
break; |
| 72 |
default: |
| 73 |
helpMessage = __("Please select an option.", "profile-builder"); |
| 74 |
} |
| 75 |
|
| 76 |
return helpMessage; |
| 77 |
} |
| 78 |
|
| 79 |
export default function WPPBBlockContentRestrictionControlsCommon(props) { |
| 80 |
const { name, attributes, setAttributes } = props; |
| 81 |
|
| 82 |
// Abort if content restriction is not enabled or if the block type does not have the wppbContentRestriction attribute registered |
| 83 |
if (!("wppbContentRestriction" in attributes)) { |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
const { wppbContentRestriction } = attributes; |
| 88 |
|
| 89 |
const userRoles = JSON.parse(wppbBlockEditorData.userRoles); |
| 90 |
const contentRestrictionActivated = JSON.parse( |
| 91 |
wppbBlockEditorData.content_restriction_activated, |
| 92 |
); |
| 93 |
|
| 94 |
// Abort if content restriction is not enabled |
| 95 |
if (!contentRestrictionActivated) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
|
| 99 |
// Check if this is one of the Content Restriction blocks so that the 'All Users' option can be hidden |
| 100 |
let contentRestrictionBlock = false; |
| 101 |
if ( |
| 102 |
[ |
| 103 |
"wppb/content-restriction-start", |
| 104 |
"wppb/content-restriction-end", |
| 105 |
].includes(name) |
| 106 |
) { |
| 107 |
contentRestrictionBlock = true; |
| 108 |
} |
| 109 |
|
| 110 |
let helpMessage = getRestrictionHelpMessage(wppbContentRestriction, userRoles); |
| 111 |
return ( |
| 112 |
<> |
| 113 |
<p>{helpMessage}</p> |
| 114 |
<br /> |
| 115 |
<ToggleGroupControl |
| 116 |
isBlock |
| 117 |
label={__("Show content to", "profile-builder")} |
| 118 |
value={wppbContentRestriction.display_to} |
| 119 |
onChange={(value) => |
| 120 |
setAttributes({ |
| 121 |
wppbContentRestriction: { |
| 122 |
...wppbContentRestriction, |
| 123 |
display_to: value, |
| 124 |
}, |
| 125 |
}) |
| 126 |
} |
| 127 |
> |
| 128 |
{!contentRestrictionBlock && ( |
| 129 |
<ToggleGroupControlOption |
| 130 |
label={__("All Users", "profile-builder")} |
| 131 |
value="all" |
| 132 |
/> |
| 133 |
)} |
| 134 |
<ToggleGroupControlOption |
| 135 |
label={__("Logged In Users", "profile-builder")} |
| 136 |
value="" |
| 137 |
/> |
| 138 |
<ToggleGroupControlOption |
| 139 |
label={__("Logged Out Users", "profile-builder")} |
| 140 |
value="not_logged_in" |
| 141 |
/> |
| 142 |
</ToggleGroupControl> |
| 143 |
{wppbContentRestriction.display_to == "all" && <p></p>} |
| 144 |
{wppbContentRestriction.display_to == "" && ( |
| 145 |
<div> |
| 146 |
<BaseControl |
| 147 |
label={__("User Roles", "profile-builder")} |
| 148 |
help={__( |
| 149 |
"The desired valid user roles. Select none for all roles to be valid.", |
| 150 |
"profile-builder", |
| 151 |
)} |
| 152 |
> |
| 153 |
<div style={{ maxHeight: '380px', overflowY: 'auto', border: '1px solid #8c8f94', padding: '8px 12px', borderRadius: '2px', marginBottom: '16px', backgroundColor: '#fff' }}> |
| 154 |
<style> |
| 155 |
{` |
| 156 |
.wppb-user-role-checkbox { |
| 157 |
margin-bottom: 4px !important; |
| 158 |
} |
| 159 |
.wppb-user-role-checkbox:last-child { |
| 160 |
margin-bottom: 0 !important; |
| 161 |
} |
| 162 |
.wppb-user-role-checkbox .components-checkbox-control__label { |
| 163 |
font-size: 13px !important; |
| 164 |
line-height: 1.4 !important; |
| 165 |
} |
| 166 |
.wppb-user-role-checkbox .components-checkbox-control__input-container { |
| 167 |
margin-right: 8px !important; |
| 168 |
} |
| 169 |
`} |
| 170 |
</style> |
| 171 |
{userRoles?.map((userRole) => { |
| 172 |
const isChecked = wppbContentRestriction.user_roles ? wppbContentRestriction.user_roles.includes(userRole.slug) : false; |
| 173 |
return ( |
| 174 |
<CheckboxControl |
| 175 |
key={userRole.slug} |
| 176 |
label={decodeEntities(userRole.name)} |
| 177 |
checked={isChecked} |
| 178 |
className="wppb-user-role-checkbox" |
| 179 |
onChange={(checked) => { |
| 180 |
const currentRoles = wppbContentRestriction.user_roles ? [...wppbContentRestriction.user_roles] : []; |
| 181 |
let newRoles; |
| 182 |
if (checked) { |
| 183 |
newRoles = [...currentRoles, userRole.slug]; |
| 184 |
} else { |
| 185 |
newRoles = currentRoles.filter(r => r !== userRole.slug); |
| 186 |
} |
| 187 |
setAttributes({ |
| 188 |
wppbContentRestriction: { |
| 189 |
...wppbContentRestriction, |
| 190 |
user_roles: newRoles, |
| 191 |
}, |
| 192 |
}); |
| 193 |
}} |
| 194 |
/> |
| 195 |
); |
| 196 |
})} |
| 197 |
</div> |
| 198 |
</BaseControl> |
| 199 |
<BaseControl label={__("User IDs", "profile-builder")}> |
| 200 |
<InputControl |
| 201 |
help={__( |
| 202 |
"A comma-separated list of user IDs.", |
| 203 |
"profile-builder", |
| 204 |
)} |
| 205 |
value={wppbContentRestriction.users_ids} |
| 206 |
onChange={(value) => |
| 207 |
setAttributes({ |
| 208 |
wppbContentRestriction: { |
| 209 |
...wppbContentRestriction, |
| 210 |
users_ids: value, |
| 211 |
}, |
| 212 |
}) |
| 213 |
} |
| 214 |
className="components-input-control__input" |
| 215 |
/> |
| 216 |
</BaseControl> |
| 217 |
<Fragment> |
| 218 |
<ToggleControl |
| 219 |
label={__( |
| 220 |
"Enable Custom Message", |
| 221 |
"profile-builder", |
| 222 |
)} |
| 223 |
checked={ |
| 224 |
wppbContentRestriction.enable_message_logged_in |
| 225 |
? wppbContentRestriction.enable_message_logged_in |
| 226 |
: false |
| 227 |
} |
| 228 |
onChange={() => |
| 229 |
setAttributes({ |
| 230 |
wppbContentRestriction: { |
| 231 |
...wppbContentRestriction, |
| 232 |
enable_message_logged_in: !wppbContentRestriction.enable_message_logged_in, |
| 233 |
}, |
| 234 |
}) |
| 235 |
} |
| 236 |
/> |
| 237 |
{wppbContentRestriction.enable_message_logged_in && ( |
| 238 |
<TextareaControl |
| 239 |
help={__( |
| 240 |
"Custom message for logged-in users.", |
| 241 |
"profile-builder", |
| 242 |
)} |
| 243 |
value={wppbContentRestriction.message_logged_in} |
| 244 |
onChange={(value) => |
| 245 |
setAttributes({ |
| 246 |
wppbContentRestriction: { |
| 247 |
...wppbContentRestriction, |
| 248 |
message_logged_in: value, |
| 249 |
}, |
| 250 |
}) |
| 251 |
} |
| 252 |
/> |
| 253 |
)} |
| 254 |
</Fragment> |
| 255 |
</div> |
| 256 |
)} |
| 257 |
{wppbContentRestriction.display_to == "not_logged_in" && ( |
| 258 |
<Fragment> |
| 259 |
<ToggleControl |
| 260 |
label={__("Enable Custom Message", "profile-builder")} |
| 261 |
checked={ |
| 262 |
wppbContentRestriction.enable_message_logged_out |
| 263 |
? wppbContentRestriction.enable_message_logged_out |
| 264 |
: false |
| 265 |
} |
| 266 |
onChange={() => |
| 267 |
setAttributes({ |
| 268 |
wppbContentRestriction: { |
| 269 |
...wppbContentRestriction, |
| 270 |
enable_message_logged_out: !wppbContentRestriction.enable_message_logged_out, |
| 271 |
}, |
| 272 |
}) |
| 273 |
} |
| 274 |
/> |
| 275 |
{wppbContentRestriction.enable_message_logged_out && ( |
| 276 |
<TextareaControl |
| 277 |
help={__( |
| 278 |
"Custom message for logged-out users", |
| 279 |
"profile-builder", |
| 280 |
)} |
| 281 |
value={wppbContentRestriction.message_logged_out} |
| 282 |
onChange={(value) => |
| 283 |
setAttributes({ |
| 284 |
wppbContentRestriction: { |
| 285 |
...wppbContentRestriction, |
| 286 |
message_logged_out: value, |
| 287 |
}, |
| 288 |
}) |
| 289 |
} |
| 290 |
/> |
| 291 |
)} |
| 292 |
</Fragment> |
| 293 |
)} |
| 294 |
</> |
| 295 |
); |
| 296 |
} |
| 297 |
|