index.js
4.02 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
/**
* LearnDash Block ld-course-notstarted
*
* @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, TextControl, ToggleControl } from '@wordpress/components';
const block_title = sprintf(
// translators: placeholder: Course.
_x('LearnDash %s Not Started', 'placeholder: Course', 'learndash'), ldlms_get_custom_label('course')
);
const block_key = 'learndash/ld-course-notstarted';
registerBlockType(
block_key,
{
title: block_title,
description: sprintf(
// translators: placeholder: Course.
_x('This block shows the content if the user is enrolled into the %s but not yet started.', 'placeholder: Course', 'learndash'), ldlms_get_custom_label('course') ),
icon: 'star-empty',
category: 'learndash-blocks',
supports: {
customClassName: false,
},
attributes: {
course_id: {
type: 'string',
default: '',
},
user_id: {
type: 'string',
default: '',
},
autop: {
type: 'boolean',
default: true
},
},
edit: props => {
const { attributes: { course_id, user_id, autop }, className, 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 });
}
}} />
<ToggleControl
label={__('Auto Paragraph', 'learndash')}
checked={!!autop}
onChange={autop => setAttributes({ autop })}
/>
</PanelBody>
</InspectorControls>
);
let ld_block_error_message = '';
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'));
}
}
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={block_key}>
<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 />
);
}
},
);