index.js
6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* LearnDash Block ld-student
*
* @since 2.5.9
* @package LearnDash
*/
/**
* LearnDash block functions
*/
import {
ldlms_get_post_edit_meta,
ldlms_get_custom_label,
ldlms_get_integer_value
} from '../ldlms.js';
/**
* Internal block libraries
*/
import { __, _x, sprintf } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { InnerBlocks, InspectorControls } from '@wordpress/block-editor';
import {
PanelBody,
SelectControl,
TextControl,
ToggleControl,
} from "@wordpress/components";
const block_key = 'learndash/ld-student';
const block_title = __('LearnDash Student', 'learndash');
registerBlockType(block_key, {
title: block_title,
description: sprintf(
// translators: placeholder: Course.
_x(
"This block shows the content if the user is enrolled in the %s.",
"placeholders: Course",
"learndash"
),
ldlms_get_custom_label("course")
),
icon: "welcome-learn-more",
category: "learndash-blocks",
supports: {
customClassName: false,
},
attributes: {
display_type: {
type: "string",
default: "",
},
course_id: {
type: "string",
default: "",
},
group_id: {
type: "string",
default: "",
},
user_id: {
type: "string",
default: "",
},
autop: {
type: "boolean",
default: true,
},
},
edit: (props) => {
const {
attributes: { display_type, course_id, group_id, user_id, autop },
className,
setAttributes,
} = props;
var display_type_control;
var post_id_controls;
display_type_control = (
<SelectControl
key="display_type"
label={__("Display Type", "learndash")}
value={display_type}
options={[
{
label: __("Select a Display Type", "learndash"),
value: "",
},
{
label: ldlms_get_custom_label("course"),
value: "sfwd-courses",
},
{
label: ldlms_get_custom_label("group"),
value: "groups",
},
]}
help={sprintf(
// translators: placeholders: Course, Group.
_x(
"Leave blank to show the default %1$s or %2$s content table.",
"placeholders: Course, Group",
"learndash"
),
ldlms_get_custom_label("course"),
ldlms_get_custom_label("group")
)}
onChange={(display_type) => setAttributes({ display_type })}
/>
);
if ("sfwd-courses" === display_type) {
setAttributes({ group_id: "" });
post_id_controls = (
<TextControl
label={sprintf(
// translators: placeholder: Course.
_x("%s ID", "placeholder: Course", "learndash"),
ldlms_get_custom_label("course")
)}
help={sprintf(
// translators: placeholders: Course, Course.
_x(
"Enter single %1$s ID. Leave blank if used within a %2$s.",
"placeholders: Course, Course",
"learndash"
),
ldlms_get_custom_label("course"),
ldlms_get_custom_label("course")
)}
value={course_id || ""}
type={'number'}
onChange={ function( new_course_id ) {
if ( new_course_id != "" && new_course_id < 0 ) {
setAttributes({ course_id: "0" });
} else {
setAttributes({ course_id: new_course_id });
}
}} />
);
} else if ("groups" === display_type) {
setAttributes({ course_id: "" });
post_id_controls = (
<TextControl
label={sprintf(
// translators: placeholder: Group.
_x("%s ID", "placeholder: Group", "learndash"),
ldlms_get_custom_label("group")
)}
help={sprintf(
// translators: placeholders: Group, Group.
_x(
"Enter single %1$s ID. Leave blank if used within a %2$s.",
"placeholders: Group, Group",
"learndash"
),
ldlms_get_custom_label("group"),
ldlms_get_custom_label("group")
)}
value={group_id || ""}
type={'number'}
onChange={ function( new_group_id ) {
if ( new_group_id != "" && new_group_id < 0 ) {
setAttributes({ group_id: "0" });
} else {
setAttributes({ group_id: new_group_id });
}
}} />
);
}
const inspectorControls = (
<InspectorControls key="controls">
<PanelBody title={__("Settings", "learndash")}>
{display_type_control}
{post_id_controls}
<TextControl
label={__("User ID", "learndash")}
help={__(
"Enter specific User ID. Leave blank for current User.",
"learndash"
)}
value={user_id || ""}
type={'number'}
onChange={ function( new_user_id ) {
if ( new_user_id != "" && new_user_id < 0 ) {
setAttributes({ user_id: "0" });
} else {
setAttributes({ user_id: new_user_id });
}
}} />
<ToggleControl
label={__("Auto Paragraph", "learndash")}
checked={!!autop}
onChange={(autop) => setAttributes({ autop })}
/>
</PanelBody>
</InspectorControls>
);
let preview_display_type = display_type;
if (preview_display_type === "") {
let editing_post_meta = ldlms_get_post_edit_meta();
if ("undefined" !== typeof editing_post_meta.post_type) {
if (editing_post_meta.post_type === "sfwd-courses") {
preview_display_type = "sfwd-courses";
} else if (editing_post_meta.post_type === "groups") {
preview_display_type = "groups";
}
}
}
let ld_block_error_message = "";
if ("sfwd-courses" === preview_display_type) {
let preview_course_id = ldlms_get_integer_value(course_id);
if (preview_course_id === 0) {
preview_course_id = ldlms_get_post_edit_meta("course_id");
preview_course_id = ldlms_get_integer_value(preview_course_id);
if (preview_course_id == 0) {
ld_block_error_message = sprintf(
// translators: placeholders: Course, Course.
_x(
"%1$s ID is required when not used within a %2$s.",
"placeholders: Course, Course",
"learndash"
),
ldlms_get_custom_label("course"),
ldlms_get_custom_label("course")
);
}
}
} else if ("groups" === preview_display_type) {
let preview_group_id = ldlms_get_integer_value(group_id);
if (preview_group_id === 0) {
preview_group_id = ldlms_get_post_edit_meta("post_id");
preview_group_id = ldlms_get_integer_value(preview_group_id);
if (preview_group_id == 0) {
ld_block_error_message = sprintf(
// translators: placeholders: Group, Group.
_x(
"%1$s ID is required when not used within a %2$s.",
"placeholders: Group, Group",
"learndash"
),
ldlms_get_custom_label("group"),
ldlms_get_custom_label("group")
);
}
}
}
if (ld_block_error_message.length) {
ld_block_error_message = (
<span className="learndash-block-error-message">
{ld_block_error_message}
</span>
);
}
const outputBlock = (
<div className={className} key="learndash/ld-student">
<span className="learndash-inner-header">{block_title}</span>
<div className="learndash-block-inner">
{ld_block_error_message}
<InnerBlocks />
</div>
</div>
);
return [inspectorControls, outputBlock];
},
save: (props) => {
return <InnerBlocks.Content />;
},
});