1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Editor Notes Block. |
4
|
|
|
* |
5
|
|
|
* @since 8.x |
6
|
|
|
* |
7
|
|
|
* @package Jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack\Extensions\Editor_Notes; |
11
|
|
|
|
12
|
|
|
use Jetpack_Gutenberg; |
13
|
|
|
|
14
|
|
|
const FEATURE_NAME = 'editor-notes'; |
15
|
|
|
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Registers the block for use in Gutenberg |
19
|
|
|
* This is done via an action so that we can disable |
20
|
|
|
* registration if we need to. |
21
|
|
|
*/ |
22
|
|
|
function register_block() { |
23
|
|
|
register_post_meta( |
24
|
|
|
'post', |
25
|
|
|
'jetpack-editor-notes', |
26
|
|
|
array( |
27
|
|
|
'single' => true, |
28
|
|
|
'type' => 'array', |
29
|
|
|
'show_in_rest' => array( |
30
|
|
|
'schema' => array( |
31
|
|
|
'type' => 'array', |
32
|
|
|
'items' => array( |
33
|
|
|
'type' => 'object', |
34
|
|
|
'properties' => array( |
35
|
|
|
'id' => array( |
36
|
|
|
'type' => 'number', |
37
|
|
|
), |
38
|
|
|
'blocks' => array( |
39
|
|
|
'type' => 'string', |
40
|
|
|
), |
41
|
|
|
), |
42
|
|
|
), |
43
|
|
|
), |
44
|
|
|
), |
45
|
|
|
'auth_callback' => 'is_user_allowed', |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
jetpack_register_block( |
50
|
|
|
BLOCK_NAME, |
51
|
|
|
array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
add_action( 'init', __NAMESPACE__ . '\register_block' ); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Editor Notes block registration/dependency declaration. |
58
|
|
|
* |
59
|
|
|
* @param array $attr Array containing the Editor Notes block attributes. |
60
|
|
|
* @param string $content String containing the Editor Notes block content. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
function load_assets( $attr, $content ) { |
65
|
|
|
if ( ! is_user_allowed() || empty( $attr['noteId'] ) ) { |
66
|
|
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
70
|
|
|
|
71
|
|
|
$notes = get_post_meta( get_the_ID(), 'jetpack-editor-notes', true ); |
72
|
|
|
$blocks = ''; |
73
|
|
|
foreach ( $notes as $n ) { |
74
|
|
|
if ( (int) $n['id'] === $attr['noteId'] ) { |
75
|
|
|
$blocks = $n['blocks']; |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return sprintf( |
81
|
|
|
'<div class="%1$s"><p class="wp-block-jetpack-editor-notes__label">%2$s</p>%3$s</div>', |
82
|
|
|
esc_attr( Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr ) ), |
83
|
|
|
esc_html__( 'Editor Notes', 'jetpack' ), |
84
|
|
|
$blocks |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check if the current user is allowed to see the Editor Notes. |
90
|
|
|
* |
91
|
|
|
* @return boolean |
92
|
|
|
*/ |
93
|
|
|
function is_user_allowed() { |
94
|
|
|
return current_user_can( 'edit_others_posts' ); |
95
|
|
|
} |
96
|
|
|
|