forked from Automattic/Co-Authors-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-cap-block-coauthor-display-name.php
129 lines (115 loc) · 2.89 KB
/
class-cap-block-coauthor-display-name.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
<?php
/**
* Co-Author Display Name Block
*
* @package Co-Authors Plus
*/
/**
* CAP Block CoAuthor Display Name
*/
class CAP_Block_CoAuthor_Display_Name {
/**
* Construct
*/
public function __construct() {
add_action( 'init', array( __CLASS__, 'register_block' ) );
add_action( 'render_block_context', array( __CLASS__, 'provide_author_archive_context' ), 10, 3 );
}
/**
* Register Block
*/
public static function register_block() : void {
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => array( __CLASS__, 'render_block' ),
)
);
}
/**
* Render Block
*
* @param array $attributes
* @param string $content
* @param WP_Block $block
* @return string
*/
public static function render_block( array $attributes, string $content, WP_Block $block ) : string {
$author = $block->context['cap/author'] ?? array();
if ( empty( $author ) ) {
return '';
}
$display_name = $author['display_name'] ?? '';
if ( '' === $display_name ) {
return '';
}
$link = $author['link'] ?? '';
$is_link = $attributes['isLink'] ?? false;
$rel = $attributes['rel'] ?? '';
if ( $is_link && '' !== $link ) {
$inner_content = sprintf(
'<a rel="%s" href="%s" title="%s">%s</a>',
$rel,
$link,
sprintf( __( 'Posts by %s', 'co-authors-plus' ), $display_name ),
$display_name
);
} else {
$inner_content = $display_name;
}
return sprintf(
'<p %s>%s</p>',
get_block_wrapper_attributes(
self::get_custom_block_wrapper_attributes( $attributes )
),
$inner_content
);
}
/**
* Provide Author Archive Context
*
* @param array $context,
* @param array $parsed_block
* @param null|WP_Block $parent_block
* @return array
*/
public static function provide_author_archive_context( array $context, array $parsed_block, ?WP_Block $parent_block ) : array {
if ( ! is_author() ) {
return $context;
}
if ( null === $parsed_block['blockName'] ) {
return $context;
}
// author if you do an individual piece of a coauthor outside of a coauthor template.
if ( 'cap/coauthor-' === substr( $parsed_block['blockName'], 0, 13 ) && ( ! array_key_exists( 'cap/author', $context ) || empty( $context['cap/author'] ) ) ) {
return array(
'cap/author' => rest_get_server()->dispatch(
WP_REST_Request::from_url(
home_url(
sprintf(
'/wp-json/coauthor-blocks/v1/coauthor/%s',
get_query_var( 'author_name' )
)
)
)
)->get_data()
);
}
return $context;
}
/**
* Get Custom Block Wrapper Attributes
*
* @param array $attributes
* @return array
*/
public static function get_custom_block_wrapper_attributes( array $attributes ) : array {
$text_align = $attributes['textAlign'] ?? '';
if ( empty( $text_align ) ) {
return array();
}
return array(
'class' => esc_attr( "has-text-align-{$text_align}" )
);
}
}