-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensei-lesson-info-block.php
60 lines (51 loc) · 1.48 KB
/
sensei-lesson-info-block.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
<?php
/**
* Plugin Name: Sensei Lesson Info Block
* Description: Add Lesson Info Gutenberg Block to Sensei Lessons.
* Version: 0.0.1
* Author: Automattic
*/
class Sensei_Lesson_Info_Block {
private static $_instance = null;
public static function get_instance() {
self::$_instance = new Sensei_Lesson_Info_Block();
}
private function __construct() {
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_js' ) );
}
public function rest_api_init() {
register_rest_field( 'lesson', 'meta', array(
'get_callback' => function( $lesson_arr ) {
$meta_keys = array( '_lesson_length', '_lesson_complexity' );
$response = array();
foreach ( $meta_keys as $key ) {
$response[ $key ] = get_post_meta( $lesson_arr['id'], $key, true );
}
return $response;
},
'update_callback' => function( $meta, $lesson ) {
$meta_keys = array( '_lesson_length', '_lesson_complexity' );
foreach ( $meta_keys as $key ) {
if ( isset( $meta[ $key ] ) ) {
update_post_meta( $lesson->ID, $key, $meta[ $key ] );
}
}
}
) );
}
public function enqueue_block_js() {
// Only enqueue on CPT Page.
global $post;
if ( 'lesson' === $post->post_type ) {
wp_enqueue_script(
'sensei_lesson_info_block',
plugins_url( 'script.js', __FILE__ ),
array( 'wp-blocks', 'wp-element' ),
'0.0.1',
false
);
}
}
}
Sensei_Lesson_Info_Block::get_instance();