| 1 |
import { assign, has } from "lodash"; |
| 2 |
|
| 3 |
import { addFilter } from "@wordpress/hooks"; |
| 4 |
import { createHigherOrderComponent } from "@wordpress/compose"; |
| 5 |
import { __ } from "@wordpress/i18n"; |
| 6 |
import { InspectorControls } from "@wordpress/block-editor"; |
| 7 |
import { PanelBody } from "@wordpress/components"; |
| 8 |
|
| 9 |
import WPPBBlockContentRestrictionControlsCommon from "./controls.js"; |
| 10 |
|
| 11 |
/** |
| 12 |
* Add the content restriction inspector controls in the editor |
| 13 |
*/ |
| 14 |
function WPPBBlockContentRestrictionControls(props) { |
| 15 |
const { name, attributes, setAttributes } = props; |
| 16 |
const { wppbContentRestriction } = attributes; |
| 17 |
|
| 18 |
const contentRestrictionActivated = JSON.parse( |
| 19 |
wppbBlockEditorData.content_restriction_activated, |
| 20 |
); |
| 21 |
|
| 22 |
// Abort if content restriction is not enabled, if the block type does not have the |
| 23 |
// wppbContentRestriction attribute registered or if the block is one of the Content Restriction blocks |
| 24 |
if ( |
| 25 |
!contentRestrictionActivated || |
| 26 |
!has(attributes, "wppbContentRestriction") || |
| 27 |
[ |
| 28 |
"wppb/content-restriction-start", |
| 29 |
"wppb/content-restriction-end", |
| 30 |
"wppb/content-restriction-start", |
| 31 |
"wppb/content-restriction-start", |
| 32 |
].includes(name) |
| 33 |
) { |
| 34 |
return null; |
| 35 |
} |
| 36 |
|
| 37 |
return ( |
| 38 |
<InspectorControls> |
| 39 |
<PanelBody |
| 40 |
title={__( |
| 41 |
"Profile Builder Content Restriction", |
| 42 |
"profile-builder", |
| 43 |
)} |
| 44 |
className="profile-builder-content-restriction-settings" |
| 45 |
initialOpen={wppbContentRestriction.panel_open} |
| 46 |
onToggle={(value) => |
| 47 |
setAttributes({ |
| 48 |
wppbContentRestriction: assign( |
| 49 |
{ ...wppbContentRestriction }, |
| 50 |
{ panel_open: !wppbContentRestriction.panel_open }, |
| 51 |
), |
| 52 |
}) |
| 53 |
} |
| 54 |
> |
| 55 |
<WPPBBlockContentRestrictionControlsCommon {...props} /> |
| 56 |
</PanelBody> |
| 57 |
</InspectorControls> |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Add the content restriction settings attribute |
| 63 |
*/ |
| 64 |
function WPPBContentRestrictionAttributes(settings) { |
| 65 |
let contentRestrictionAttributes = { |
| 66 |
wppbContentRestriction: { |
| 67 |
type: "object", |
| 68 |
properties: { |
| 69 |
user_roles: { |
| 70 |
type: "array", |
| 71 |
}, |
| 72 |
users_ids: { |
| 73 |
type: "string", |
| 74 |
}, |
| 75 |
display_to: { |
| 76 |
type: "string", |
| 77 |
}, |
| 78 |
enable_message_logged_in: { |
| 79 |
type: "bool", |
| 80 |
}, |
| 81 |
enable_message_logged_out: { |
| 82 |
type: "bool", |
| 83 |
}, |
| 84 |
message_logged_in: { |
| 85 |
type: "string", |
| 86 |
}, |
| 87 |
message_logged_out: { |
| 88 |
type: "string", |
| 89 |
}, |
| 90 |
panel_open: { |
| 91 |
type: "bool", |
| 92 |
}, |
| 93 |
}, |
| 94 |
default: { |
| 95 |
user_roles: [], |
| 96 |
users_ids: "", |
| 97 |
display_to: "all", |
| 98 |
enable_message_logged_in: false, |
| 99 |
enable_message_logged_out: false, |
| 100 |
message_logged_in: "", |
| 101 |
message_logged_out: "", |
| 102 |
panel_open: false, |
| 103 |
}, |
| 104 |
}, |
| 105 |
}; |
| 106 |
|
| 107 |
// The Content Restriction Start block should not have an 'All Users' option |
| 108 |
if (settings.attributes.wppb_content_restriction_block_start) { |
| 109 |
contentRestrictionAttributes.wppbContentRestriction.default.display_to = |
| 110 |
""; |
| 111 |
} |
| 112 |
|
| 113 |
// Do not add the content restriction settings attribute for these blocks |
| 114 |
if ( |
| 115 |
settings.attributes.wppb_content_restriction_block_end || |
| 116 |
settings.attributes.pms_content_restriction_block_start || |
| 117 |
settings.attributes.pms_content_restriction_block_end |
| 118 |
) { |
| 119 |
return settings; |
| 120 |
} |
| 121 |
|
| 122 |
settings.attributes = assign( |
| 123 |
settings.attributes, |
| 124 |
contentRestrictionAttributes, |
| 125 |
); |
| 126 |
return settings; |
| 127 |
} |
| 128 |
addFilter( |
| 129 |
"blocks.registerBlockType", |
| 130 |
"profile-builder/attributes", |
| 131 |
WPPBContentRestrictionAttributes, |
| 132 |
); |
| 133 |
|
| 134 |
/** |
| 135 |
* Filter the block edit object and add content restriction controls |
| 136 |
*/ |
| 137 |
const blockWPPBContentRestrictionControls = createHigherOrderComponent( |
| 138 |
(BlockEdit) => { |
| 139 |
return (props) => { |
| 140 |
return ( |
| 141 |
<> |
| 142 |
<BlockEdit {...props} /> |
| 143 |
<WPPBBlockContentRestrictionControls {...props} /> |
| 144 |
</> |
| 145 |
); |
| 146 |
}; |
| 147 |
}, |
| 148 |
"blockWPPBContentRestrictionControls", |
| 149 |
); |
| 150 |
addFilter( |
| 151 |
"editor.BlockEdit", |
| 152 |
"profile-builder/inspector-controls", |
| 153 |
blockWPPBContentRestrictionControls, |
| 154 |
100, // above Advanced controls |
| 155 |
); |
| 156 |
|