ldlms.js
4.95 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
import {
PanelBody,
TextControl,
} from "@wordpress/components";
/**
* LearnDash Block Functions
*
* This is a collection of common functions used within the LeanDash blocks
*
* @since 2.5.9
* @package LearnDash
*/
import { __, _x } from "@wordpress/i18n";
import '../../../src/assets/js/main';
const ldlms_get_custom_label = learndash.customLabel.get,
ldlms_get_custom_label_lower = learndash.customLabel.getLower,
ldlms_get_custom_label_slug = learndash.customLabel.getSlug;
/**
* Will retrieve meta information about the post being edited. For now
* this is only loaded on post edit screen for Gutenberg. So no checks
* are made to ensure that a post is being edited.
*
* @param {string} token Token to return from meta array. If not provided will array is returned.
*/
export function ldlms_get_post_edit_meta(token = "") {
if (token !== "") {
if (typeof ldlms_settings.meta.post[token] !== "undefined") {
return ldlms_settings.meta.post[token];
}
}
if (typeof ldlms_settings.meta.post !== "undefined") {
return ldlms_settings.meta.post;
}
}
/**
* Will retrieve meta information about the post being edited. For now
* this is only loaded on post edit screen for Gutenberg. So no checks
* are made to ensure that a post is being edited.
*
* @param {string} token Token to return from meta array. If not provided will array is returned.
* @param {any} default_value
*/
export function ldlms_get_setting(token = "", default_value) {
if (token !== "" && typeof ldlms_settings.settings[token] !== "undefined") {
return ldlms_settings.settings[token];
}
return default_value;
}
export {
ldlms_get_custom_label,
ldlms_get_custom_label_lower,
ldlms_get_custom_label_slug
};
/**
* Will retrieve meta information about the post being edited. For now
* this is only loaded on post edit screen for Gutenberg. So no checks
* are made to ensure that a post is being edited.
*
* @param {string} token Token to return from meta array. If not provided will array is returned.
*/
export function ldlms_get_per_page(token = "") {
if (token !== "") {
if (typeof ldlms_settings.settings.per_page[token] !== "undefined") {
return ldlms_settings.settings.per_page[token];
}
} else if (typeof ldlms_settings.meta.posts_per_page !== "undefined") {
return ldlms_settings.meta.posts_per_page;
}
}
/**
* Returns integer value for variable.
*
* @param {any} var_value Variable to determine integer from.
* @return {number} value of zero.
*/
export function ldlms_get_integer_value(var_value) {
if (typeof var_value === "undefined") {
return 0;
}
const var_value_tmp = parseInt(var_value);
if (isNaN(var_value_tmp)) {
return 0;
}
return var_value_tmp;
}
/**
* Retrieve the active template key/slug.
*
* @since 4.0.0
*/
export function ldlms_templates_get_active_key() {
if (typeof ldlms_settings.templates.active !== "undefined") {
return ldlms_settings.templates.active;
}
return "";
}
/**
* Retrieve the active template name.
*
* @since 4.0.0
*/
export function ldlms_templates_get_active_name() {
if (typeof ldlms_settings.templates.list !== "undefined") {
let active_template = ldlms_templates_get_active_key();
if (typeof active_template !== "undefined" && active_template !== "") {
if (
typeof ldlms_settings.templates.list[active_template] !== "undefined"
) {
return ldlms_settings.templates.list[active_template];
}
}
}
return "";
}
/**
* Retrieve the Legacy template not supported message block panel.
*
* @since 4.0.0
*/
export function ldlms_get_block_legacy_support_panel() {
let message_text = ldlms_get_legacy_not_supported_message();
if (message_text !== "") {
return (
<PanelBody title={__("Warning", "learndash")} opened={true}>
<TextControl
help={message_text}
value={""}
type={"hidden"}
className={"notice notice-error"}
/>
</PanelBody>
);
}
return "";
}
/**
* Retrieve the Legacy template not supported message text.
*
* @since 4.0.0
*/
export function ldlms_get_legacy_not_supported_message() {
let active_template_key = ldlms_templates_get_active_key();
if (active_template_key == "legacy") {
let active_template_name = ldlms_templates_get_active_name();
return sprintf(
// translators: placeholder: current template name.
_x(
'The current LearnDash template "%s" does not support this block. Please select a different template.',
"placeholder: current template name",
"learndash"
),
active_template_name
);
}
return "";
}
/**
* Returns the post type slug for a given token.
*
* @since 4.0.0
*
* @param {string} token The post_type common key like 'course', 'lesson', 'quiz', etc.
* @return {string} The post_type slug like 'sfwd-courses', 'ld-exam'. Empty string if not found.
*/
export function ldlms_get_post_type_slug(token = "") {
if (typeof ldlms_settings.post_types !== "undefined" && token !== "") {
if (typeof ldlms_settings.post_types[token] !== "undefined") {
return ldlms_settings.post_types[token];
}
}
return "";
}