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