PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.0
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.0
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / assets / misc / gutenberg / block-content-restriction / src / index.js

index.js in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.16.0, at assets/misc/gutenberg/block-content-restriction/src/index.js

156 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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