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