index.js
5.29 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
/**
* LearnDash Block ld-course-progress
*
* @since 2.5.9
* @package LearnDash
*/
/**
* LearnDash block functions
*/
import {
ldlms_get_post_edit_meta,
ldlms_get_custom_label,
} from '../ldlms.js';
/**
* Internal block libraries
*/
import { __, _x, sprintf} from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { InspectorControls } from '@wordpress/block-editor';
import { PanelBody, TextControl, ToggleControl, PanelRow } from '@wordpress/components';
import ServerSideRender from '@wordpress/server-side-render';
import { useMemo } from "@wordpress/element";
const el = wp.element.createElement;
const iconEl = el('svg', { width: 300, height: 300, viewBox: '0 0 50 10' },
el('path', { d: "M47.1,0h-44c-1.7,0-3,1.3-3,3v4c0,1.7,1.3,3,3,3h44c1.7,0,3-1.3,3-3V3C50.1,1.3,48.7,0,47.1,0z M48.1,7c0,0.6-0.4,1-1,1h-12 V2h12c0.6,0,1,0.4,1,1V7z" } )
);
const block_key = 'learndash/ld-course-progress';
const block_title = sprintf(
// translators: placeholder: Course.
_x('LearnDash %s Progress', 'placeholders: Course', 'learndash'), ldlms_get_custom_label('course')
);
registerBlockType(
block_key,
{
title: block_title,
description: sprintf(
// translators: placeholder: Course.
_x('This block displays users progress bar for the %s.', 'placeholders: Course', 'learndash'), ldlms_get_custom_label('course') ),
icon: iconEl,
category: 'learndash-blocks',
example: {
attributes: {
example_show: 1,
},
},
supports: {
customClassName: false,
},
attributes: {
course_id: {
type: 'string',
default: ''
},
user_id: {
type: 'string',
default: ''
},
preview_show: {
type: 'boolean',
default: 1
},
preview_user_id: {
type: 'string',
},
example_show: {
type: 'boolean',
default: 0
},
editing_post_meta: {
type: 'object'
}
},
edit: props => {
let { attributes: { course_id }, className } = props;
const { attributes: { user_id, preview_show, preview_user_id, example_show },
setAttributes } = props;
const inspectorControls = (
<InspectorControls key="controls">
<PanelBody title={__("Settings", "learndash")}>
<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 });
}
}}
/>
<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 });
}
}}
/>
</PanelBody>
<PanelBody title={__("Preview", "learndash")} initialOpen={false}>
<ToggleControl
label={__("Show Preview", "learndash")}
checked={!!preview_show}
onChange={(preview_show) => setAttributes({ preview_show })}
/>
<PanelRow className="learndash-block-error-message">
{__("Preview settings are not saved.", "learndash")}
</PanelRow>
<TextControl
label={__("Preview User ID", "learndash")}
help={__("Enter a User ID to test preview", "learndash")}
value={preview_user_id || ""}
type={"number"}
onChange={function (preview_new_user_id) {
if (preview_new_user_id != "" && preview_new_user_id < 0) {
setAttributes({ preview_user_id: "0" });
} else {
setAttributes({ preview_user_id: preview_new_user_id });
}
}}
/>
</PanelBody>
</InspectorControls>
);
function get_default_message() {
return sprintf(
// translators: placeholder: block_title.
_x('%s block output shown here', 'placeholder: block_title', 'learndash'), block_title
);
}
function empty_response_placeholder_function(props) {
return get_default_message();
}
function do_serverside_render(attributes) {
if ( attributes.preview_show == true ) {
// We add the meta so the server knowns what is being edited.
attributes.editing_post_meta = ldlms_get_post_edit_meta();
return <ServerSideRender
block={block_key}
attributes={attributes}
key={block_key}
EmptyResponsePlaceholder={ empty_response_placeholder_function }
/>
} else {
return get_default_message();
}
}
return [
inspectorControls,
useMemo(() => do_serverside_render(props.attributes), [props.attributes]),
];
},
save: props => {
delete (props.attributes.example_show);
delete (props.attributes.editing_post_meta);
}
},
);