index.php
8.76 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* Handles all server side logic for the ld-exam Gutenberg Block.
*
* @package LearnDash
* @since 4.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'LearnDash_Gutenberg_Block' ) ) && ( ! class_exists( 'LearnDash_Gutenberg_Block_Exam' ) ) ) {
/**
* Class for handling LearnDash Exam Block
*/
class LearnDash_Gutenberg_Block_Exam extends LearnDash_Gutenberg_Block {
/**
* Array of sub_blocks used within the Exam Question block.
*
* @var array.
*/
private $sub_blocks = array();
/**
* Object constructor
*/
public function __construct() {
$this->block_slug = 'ld-exam';
$this->self_closing = false;
$this->block_attributes = array(
'ld_version' => array(
'type' => 'string',
),
);
$this->sub_blocks = array(
$this->block_base . '/ld-exam-question',
$this->block_base . '/ld-question-description',
$this->block_base . '/ld-question-answers-block',
$this->block_base . '/ld-correct-answer-message-block',
$this->block_base . '/ld-incorrect-answer-message-block',
);
$this->init();
add_filter( 'pre_render_block', array( $this, 'pre_render_block' ), 30, 3 );
}
/**
* Pre-Render filter for Block.
*
* We hook into the pre_render_block filter to prevent the default exam blocks
* from being rendered by WP.
*
* @since 4.0.0
*
* @param string|null $content The pre-rendered content. Default null.
* @param array $parsed_block The block being rendered.
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
*/
public function pre_render_block( $content, $parsed_block = array(), $parent_block = null ) {
if ( ( isset( $parsed_block['blockName'] ) ) && ( $this->block_base . '/' . $this->block_slug === $parsed_block['blockName'] ) ) {
$current_post_id = (int) get_the_ID();
$current_post_type = get_post_type( $current_post_id );
if ( learndash_get_post_type_slug( 'exam' ) === $current_post_type ) {
$atts = array(
'exam_id' => $current_post_id,
'course_id' => 0,
'user_id' => get_current_user_id(),
);
$course_id_show = (int) learndash_get_setting( $current_post_id, 'exam_challenge_course_show' );
if ( ! empty( $course_id_show ) ) {
$atts['course_id'] = $course_id_show;
}
$ld_exam_object = LDLMS_Factory_Post::exam( $atts['exam_id'], $atts );
if ( ( $ld_exam_object ) && ( is_a( $ld_exam_object, 'LDLMS_Model_Exam' ) ) ) {
$ld_exam_object->process_exam_submit();
$content = $ld_exam_object->get_front_content();
}
} else {
$content = '';
}
}
return $content;
}
/**
* Register Block for Gutenberg
*
* @since 4.0.0
*/
public function register_blocks() {
// Call our parent to register the block.
parent::register_blocks();
/**
* Register some internal blocks.
*/
register_block_type(
$this->block_base . '/ld-exam-question',
array(
'render_callback' => array( $this, 'render_block_exam_question' ),
'attributes' => $this->block_attributes,
)
);
register_block_type(
$this->block_base . '/ld-question-description',
array(
'render_callback' => array( $this, 'render_block_exam_question_description' ),
'attributes' => $this->block_attributes,
)
);
register_block_type(
$this->block_base . '/ld-question-answers-block',
array(
'render_callback' => array( $this, 'render_block_exam_question_answers' ),
'attributes' => $this->block_attributes,
)
);
register_block_type(
$this->block_base . '/ld-correct-answer-message-block',
array(
'render_callback' => array( $this, 'render_block_exam_question_correct_message' ),
'attributes' => $this->block_attributes,
)
);
register_block_type(
$this->block_base . '/ld-incorrect-answer-message-block',
array(
'render_callback' => array( $this, 'render_block_exam_question_incorrect_message' ),
'attributes' => $this->block_attributes,
)
);
}
/**
* Render Exam Question Block
*
* This function is called per the register_block_type() function above. This function will output
* the block rendered content.
*
* @since 4.0.0
*
* @param array $block_attributes The block attributes.
* @param string $block_content The block content.
* @param WP_block $block The block object.
*
* @return none The output is echoed.
*/
public function render_block( $block_attributes = array(), $block_content = '', WP_block $block = null ) {
return '';
}
/**
* Render Exam Question Block
*
* This function is called per the register_block_type() function above. This function will output
* the block rendered content.
*
* @since 4.0.0
*
* @param array $block_attributes The block attributes.
* @param string $block_content The block content.
* @param WP_block $block The block object.
*
* @return none The output is echoed.
*/
public function render_block_exam_question( $block_attributes = array(), $block_content = '', WP_block $block = null ) {
$block_attributes['learndash_question_content'] = $block_content;
$block_content = SFWD_LMS::get_template(
'exam/partials/exam_question_row.php',
$block_attributes
);
return $block_content;
}
/**
* Render Exam Question Description Block
*
* This function is called per the register_block_type() function above. This function will output
* the block rendered content.
*
* @since 4.0.0
*
* @param array $block_attributes The block attributes.
* @param string $block_content The block content.
* @param WP_block $block The block object.
*
* @return none The output is echoed.
*/
public function render_block_exam_question_description( $block_attributes = array(), $block_content = '', WP_block $block = null ) {
$block_attributes['learndash_question_description'] = $block_content;
$block_content = SFWD_LMS::get_template(
'exam/partials/exam_question_description.php',
$block_attributes
);
return $block_content;
}
/**
* Render Exam Question Answers Block
*
* This function is called per the register_block_type() function above. This function will output
* the block rendered content.
*
* @since 4.0.0
*
* @param array $block_attributes The block attributes.
* @param string $block_content The block content.
* @param WP_block $block The block object.
*
* @return none The output is echoed.
*/
public function render_block_exam_question_answers( $block_attributes = array(), $block_content = '', WP_block $block = null ) {
if ( ! isset( $block_attributes['learndash_question_answers'] ) ) {
if ( isset( $block_attributes['answers'] ) ) {
$block_attributes['learndash_question_answers'] = $block_attributes['answers'];
unset( $block_attributes['answers'] );
}
}
$block_content = SFWD_LMS::get_template(
'exam/partials/exam_question_answers.php',
$block_attributes
);
return $block_content;
}
/**
* Render Exam Question Correct Message Block
*
* This function is called per the register_block_type() function above. This function will output
* the block rendered content.
*
* @since 4.0.0
*
* @param array $block_attributes The block attributes.
* @param string $block_content The block content.
* @param WP_block $block The block object.
*
* @return none The output is echoed.
*/
public function render_block_exam_question_correct_message( $block_attributes = array(), $block_content = '', WP_block $block = null ) {
$block_attributes['learndash_question_correct_message'] = $block_content;
$block_content = SFWD_LMS::get_template(
'exam/partials/exam_question_correct_message.php',
$block_attributes
);
return $block_content;
}
/**
* Render Exam Question Incorrect Block
*
* This function is called per the register_block_type() function above. This function will output
* the block rendered content.
*
* @since 4.0.0
*
* @param array $block_attributes The block attributes.
* @param string $block_content The block content.
* @param WP_block $block The block object.
*
* @return none The output is echoed.
*/
public function render_block_exam_question_incorrect_message( $block_attributes = array(), $block_content = '', WP_block $block = null ) {
$block_attributes['learndash_question_incorrect_message'] = $block_content;
$block_content = SFWD_LMS::get_template(
'exam/partials/exam_question_incorrect_message.php',
$block_attributes
);
return $block_content;
}
// End of functions.
}
}
new LearnDash_Gutenberg_Block_Exam();