index.js
3.55 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
/**
* LearnDash Block ld-group
*
* @since 2.5.9
* @package LearnDash
*/
/**
* LearnDash block functions
*/
import {
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_key = 'learndash/ld-group';
const block_title = sprintf(
// translators: placeholder: Group.
_x('LearnDash %s', 'placeholder: Group', 'learndash'), ldlms_get_custom_label('group')
);
registerBlockType(
block_key,
{
title: block_title,
description: sprintf(
// translators: placeholder: Group.
_x( 'This block shows the content if the user is enrolled into the %s.', 'placeholder: Group', 'learndash'), ldlms_get_custom_label('group')),
icon: 'groups',
category: 'learndash-blocks',
supports: {
customClassName: false,
},
attributes: {
group_id: {
type: 'string',
},
user_id: {
type: 'string',
default: '',
},
autop: {
type: 'boolean',
default: true
},
},
edit: props => {
const { attributes: { group_id, user_id, autop }, className, setAttributes } = props;
const inspectorControls = (
<InspectorControls key="controls">
<PanelBody
title={__('Settings', 'learndash')}
>
<TextControl
label={sprintf(
// translators: placeholder: Group.
_x('%s ID', 'placeholder: Group', 'learndash'), ldlms_get_custom_label('group'))}
help={sprintf(
// translators: placeholder: Group.
_x('%s ID (required)', 'placeholder: Group', 'learndash'), 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 });
}
}} />
<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_group_id = ldlms_get_integer_value(group_id);
if (preview_group_id == 0) {
ld_block_error_message = sprintf(
// translators: placeholder: Group.
_x('%s ID is required.', 'placeholder: Group', 'learndash'), 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-group'>
<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 />
);
}
},
);