/
blocks.php
439 lines (384 loc) · 12.1 KB
/
blocks.php
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
namespace WPOrg_Learn\Blocks;
use Error;
use Sensei_Lesson;
use Sensei_Utils;
use Sensei_Reports_Overview_Service_Courses;
use function WordPressdotorg\Locales\get_locale_name_from_code;
use function WPOrg_Learn\{get_build_path, get_build_url, get_views_path};
use function WPOrg_Learn\Form\render_workshop_application_form;
use function WPOrg_Learn\Post_Meta\get_workshop_duration;
defined( 'WPINC' ) || die();
/**
* Actions and filters.
*/
add_action( 'init', __NAMESPACE__ . '\register_types' );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_block_style_assets' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_block_style_assets' );
/**
* Register block types.
*
* @return void
*/
function register_types() {
register_lesson_plan_actions();
register_lesson_plan_details();
register_course_data();
register_workshop_details();
register_workshop_application_form();
}
/**
* Register Lesson Plan Actions block type and related assets.
*
* @throws Error If the build files are not found.
*/
function register_lesson_plan_actions() {
$script_asset_path = get_build_path() . 'lesson-plan-actions.asset.php';
if ( ! is_readable( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "wporg-learn/lesson-plan-actions" block first.'
);
}
$script_asset = require $script_asset_path;
wp_register_script(
'lesson-plan-actions-editor-script',
get_build_url() . 'lesson-plan-actions.js',
$script_asset['dependencies'],
$script_asset['version'],
true
);
wp_register_style(
'lesson-plan-actions-style',
get_build_url() . 'style-lesson-plan-actions.css',
array(),
filemtime( get_build_path() . 'style-lesson-plan-actions.css' )
);
register_block_type( 'wporg-learn/lesson-plan-actions', array(
'editor_script' => 'lesson-plan-actions-editor-script',
'style' => 'lesson-plan-actions-style',
'render_callback' => __NAMESPACE__ . '\lesson_plan_actions_render_callback',
) );
}
/**
* Render the block content (html) on the frontend of the site.
*
* @param array $attributes
* @param string $content
* @return string HTML output used by the block
*/
function lesson_plan_actions_render_callback( $attributes, $content ) {
if ( get_post_type() !== 'lesson-plan' ) {
return;
}
$post = get_post();
ob_start();
require get_views_path() . 'block-lesson-plan-actions.php';
return ob_get_clean();
}
/**
* Register Lesson Plan Details block type and related assets.
*
* @throws Error If the build files are not found.
*/
function register_lesson_plan_details() {
$script_asset_path = get_build_path() . 'lesson-plan-details.asset.php';
if ( ! is_readable( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "wporg-learn/lesson-plan-details" block first.'
);
}
$script_asset = require $script_asset_path;
wp_register_script(
'lesson-plan-details-editor-script',
get_build_url() . 'lesson-plan-details.js',
$script_asset['dependencies'],
$script_asset['version'],
true,
);
wp_register_style(
'lesson-plan-details-style',
get_build_url() . 'style-lesson-plan-details.css',
array(),
filemtime( get_build_path() . 'style-lesson-plan-details.css' )
);
register_block_type( 'wporg-learn/lesson-plan-details', array(
'editor_script' => 'lesson-plan-details-editor-script',
'style' => 'lesson-plan-details-style',
'render_callback' => __NAMESPACE__ . '\lesson_plan_details_render_callback',
) );
}
/**
* Render the block content (html) on the frontend of the site.
*
* @param array $attributes
* @param string $content
* @return string HTML output used by the block
*/
function lesson_plan_details_render_callback( $attributes, $content ) {
if ( get_post_type() !== 'lesson-plan' ) {
return;
}
$details = wporg_learn_get_lesson_plan_taxonomy_data( get_the_ID(), 'single' );
ob_start();
require get_views_path() . 'block-lesson-plan-details.php';
return ob_get_clean();
}
/**
* Register Course Data block type and related assets.
*
* @throws Error If the build files are not found.
*/
function register_course_data() {
$script_asset_path = get_build_path() . 'course-data.asset.php';
if ( ! is_readable( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "wporg-learn/course-data" block first.'
);
}
$script_asset = require $script_asset_path;
wp_register_script(
'course-data-editor-script',
get_build_url() . 'course-data.js',
$script_asset['dependencies'],
$script_asset['version'],
true,
);
wp_register_style(
'course-data-style',
get_build_url() . 'style-course-data.css',
array(),
filemtime( get_build_path() . 'style-course-data.css' )
);
register_block_type( 'wporg-learn/course-data', array(
'editor_script' => 'course-data-editor-script',
'style' => 'course-data-style',
'render_callback' => __NAMESPACE__ . '\course_data_render_callback',
) );
}
/**
* Render the block content (html) on the frontend of the site.
*
* @param array $attributes
* @param string $content
* @return string HTML output used by the block
*/
function course_data_render_callback( $attributes, $content ) {
if ( get_post_type() !== 'course' ) {
return;
}
$course_service = new Sensei_Reports_Overview_Service_Courses();
$post = get_post();
$course_id = $post->ID;
// Get the total number of learners enrolled in the course
$learners = Sensei_Utils::sensei_check_for_activity(
array(
'type' => 'sensei_course_status',
'status' => 'in-progress',
'post__in' => $course_id,
)
);
// Get the average grade scross all learners
$average_grade = round( $course_service->get_courses_average_grade( array( $course_id ) ), 0 );
// Get the average number of days it takes to complete a course
$average_days = $course_service->get_average_days_to_completion( array( $course_id ) );
// Set up array of data to be used
$data = array(
'learners' => array(
'label' => __( 'Enrolled learners', 'wporg-learn' ),
'value' => $learners,
),
'grade' => array(
'label' => __( 'Average final grade', 'wporg-learn' ),
'value' => $average_grade . '%',
),
'days' => array(
'label' => __( 'Average days to completion', 'wporg-learn' ),
'value' => $average_days,
),
);
ob_start();
require get_views_path() . 'block-course-data.php';
return ob_get_clean();
}
/**
* Register Workshop Details block type and related assets.
*
* @throws Error If the build files are not found.
*/
function register_workshop_details() {
$script_asset_path = get_build_path() . 'workshop-details.asset.php';
if ( ! is_readable( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "wporg-learn/workshop-details" block first.'
);
}
$script_asset = require $script_asset_path;
wp_register_script(
'workshop-details-editor-script',
get_build_url() . 'workshop-details.js',
$script_asset['dependencies'],
$script_asset['version'],
true,
);
wp_register_style(
'workshop-details-style',
get_build_url() . 'style-workshop-details.css',
array(),
filemtime( get_build_path() . 'style-workshop-details.css' )
);
register_block_type( 'wporg-learn/workshop-details', array(
'editor_script' => 'workshop-details-editor-script',
'style' => 'workshop-details-style',
'render_callback' => __NAMESPACE__ . '\workshop_details_render_callback',
) );
}
/**
* Render the block content (html) on the frontend of the site.
*
* @param array $attributes
* @param string $content
* @return string HTML output used by the block
*/
function workshop_details_render_callback( $attributes, $content ) {
if ( get_post_type() !== 'wporg_workshop' ) {
return;
}
$post = get_post();
$topic_ids = wp_get_post_terms( $post->ID, 'topic', array( 'fields' => 'ids' ) );
$level = wp_get_post_terms( $post->ID, 'level', array( 'fields' => 'names' ) );
$captions = get_post_meta( $post->ID, 'video_caption_language' );
$version_ids = wp_get_post_terms( $post->ID, 'wporg_wp_version', array( 'fields' => 'ids' ) );
$version_names = wp_get_post_terms( $post->ID, 'wporg_wp_version', array( 'fields' => 'names' ) );
$topic_names = array();
foreach ( $topic_ids as $id ) {
$topic_names[] = get_term( $id )->name;
}
$fields = array(
'length' => array(
'label' => __( 'Length', 'wporg-learn' ),
'param' => array(),
'value' => array( get_workshop_duration( $post, 'string' ) ),
),
'topic' => array(
'label' => __( 'Topic', 'wporg-learn' ),
'param' => $topic_ids,
'value' => $topic_names,
),
'wp_version' => array(
'label' => __( 'Related Version', 'wporg-learn' ),
'param' => $version_ids,
'value' => $version_names,
),
'level' => array(
'label' => __( 'Level', 'wporg-learn' ),
'param' => array(),
'value' => $level,
),
'language' => array(
'label' => __( 'Language', 'wporg-learn' ),
'param' => array( $post->language ),
'value' => array( esc_html( get_locale_name_from_code( $post->language, 'native' ) ) ),
),
'captions' => array(
'label' => __( 'Subtitles', 'wporg-learn' ),
'param' => $captions,
'value' => array_map(
function( $caption_lang ) {
return esc_html( get_locale_name_from_code( $caption_lang, 'native' ) );
},
$captions
),
),
);
// Remove fields with empty values.
$fields = array_filter( $fields, function( $data ) {
return $data['value'];
} );
$lesson_id = get_post_meta( $post->ID, 'linked_lesson_id', true );
$quiz_url = '';
if ( $lesson_id && Sensei_Lesson::lesson_quiz_has_questions( $lesson_id ) ) {
$quiz_id = Sensei()->lesson->lesson_quizzes( $lesson_id );
if ( $quiz_id ) {
$quiz_url = get_permalink( $quiz_id );
}
}
ob_start();
require get_views_path() . 'block-workshop-details.php';
return ob_get_clean();
}
/**
* Register Workshop Application Form block type and related assets.
*
* @throws Error If the build files are not found.
*/
function register_workshop_application_form() {
$script_asset_path = get_build_path() . 'workshop-application-form.asset.php';
if ( ! is_readable( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "wporg-learn/workshop-application-form" block first.'
);
}
$script_asset = require $script_asset_path;
wp_register_script(
'workshop-application-form-editor-script',
get_build_url() . 'workshop-application-form.js',
$script_asset['dependencies'],
$script_asset['version']
);
$script_asset_path = get_build_path() . 'form.asset.php';
if ( ! is_readable( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` first.'
);
}
$script_asset = require $script_asset_path;
wp_register_script(
'workshop-application-form-script',
get_build_url() . 'form.js',
array_merge( $script_asset['dependencies'], array( 'jquery', 'select2' ) ),
$script_asset['version'],
true
);
register_block_type( 'wporg-learn/workshop-application-form', array(
'editor_script' => 'workshop-application-form-editor-script',
'script' => 'workshop-application-form-script',
'style' => 'select2',
'render_callback' => __NAMESPACE__ . '\workshop_application_form_render_callback',
) );
}
/**
* Render the Workshop Application Form block markup.
*
* @return string
*/
function workshop_application_form_render_callback() {
return render_workshop_application_form();
}
/**
* Enqueue scripts and stylesheets for custom block styles.
*
* @throws Error If the build files are not found.
*/
function enqueue_block_style_assets() {
if ( is_admin() ) {
$script_asset_path = get_build_path() . 'block-styles.asset.php';
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for block styles first.'
);
}
$script_asset = require $script_asset_path;
wp_enqueue_script(
'wporg-learn-block-styles',
get_build_url() . 'block-styles.js',
$script_asset['dependencies'],
$script_asset['version']
);
}
wp_enqueue_style(
'wporg-learn-block-styles',
get_build_url() . 'style-block-styles.css',
array(),
filemtime( get_build_path() . 'style-block-styles.css' )
);
}