Skip to content

Commit 57c0823

Browse files
authored
Follow Me: Introduce Author Card style (#1753)
1 parent c3c0897 commit 57c0823

File tree

13 files changed

+529
-142
lines changed

13 files changed

+529
-142
lines changed

build/follow-me/block.json

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"$schema": "https://schemas.wp.org/trunk/block.json",
33
"name": "activitypub/follow-me",
44
"apiVersion": 3,
5-
"version": "2.0.0",
5+
"version": "2.1.0",
66
"title": "Follow me on the Fediverse",
77
"category": "widgets",
88
"description": "Display your Fediverse profile so that visitors can follow you.",
99
"textdomain": "activitypub",
1010
"icon": "groups",
1111
"example": {
1212
"attributes": {
13-
"buttonOnly": false
13+
"className": "is-style-default"
1414
}
1515
},
1616
"supports": {
@@ -31,6 +31,7 @@
3131
"color": true,
3232
"style": true
3333
},
34+
"shadow": true,
3435
"typography": {
3536
"fontSize": true,
3637
"__experimentalDefaultControls": {
@@ -43,14 +44,25 @@
4344
]
4445
}
4546
},
47+
"styles": [
48+
{
49+
"name": "default",
50+
"label": "Default",
51+
"isDefault": true
52+
},
53+
{
54+
"name": "button-only",
55+
"label": "Button"
56+
},
57+
{
58+
"name": "profile",
59+
"label": "Profile"
60+
}
61+
],
4662
"attributes": {
4763
"selectedUser": {
4864
"type": "string",
4965
"default": "site"
50-
},
51-
"buttonOnly": {
52-
"type": "boolean",
53-
"default": false
5466
}
5567
},
5668
"usesContext": [

build/follow-me/index.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'e538adbcb1ecf68ba3cc');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'e4b8de35c5aa8ae7bbe7');

build/follow-me/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/follow-me/render.php

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@
77

88
use Activitypub\Blocks;
99
use Activitypub\Collection\Actors;
10+
use Activitypub\Collection\Followers;
1011

1112
/* @var array $attributes Block attributes. */
1213
$attributes = wp_parse_args( $attributes );
1314

15+
/* @var WP_Block $block Parsed block.*/
16+
$block = $block ?? null;
17+
18+
/* @var string $content Inner blocks content. */
19+
$content = $content ?? '';
20+
1421
// Get the user ID from the selected user attribute.
15-
$selected_user = $attributes['selectedUser'] ?? 'site';
16-
$user_id = Blocks::get_user_id( $selected_user );
17-
$button_only = $attributes['buttonOnly'] ?? false;
22+
$user_id = Blocks::get_user_id( $attributes['selectedUser'] ?? 'site' );
23+
$actor = Actors::get_by_id( $user_id );
24+
if ( is_wp_error( $actor ) ) {
25+
return;
26+
}
1827

1928
// Generate a unique ID for the block.
2029
$block_id = 'activitypub-follow-me-block-' . wp_unique_id();
2130

2231
// Get block style information.
2332
$style = wp_get_global_styles();
2433
$background_color = $attributes['backgroundColor'] ?? $style['color']['background'] ?? '';
25-
26-
// Get button style from block attributes.
27-
$button_style = $attributes['style'] ?? array();
28-
29-
$actor = Actors::get_by_id( $user_id );
30-
if ( is_wp_error( $actor ) ) {
31-
return;
32-
}
34+
$button_style = $attributes['style'] ?? array();
3335

3436
// Set up the Interactivity API state.
3537
wp_interactivity_state(
@@ -47,14 +49,15 @@
4749
);
4850

4951
// Add the block wrapper attributes.
50-
$wrapper_attributes = get_block_wrapper_attributes(
51-
array(
52-
'id' => $block_id,
53-
'class' => 'activitypub-follow-me-block-wrapper',
54-
'data-wp-interactive' => 'activitypub/follow-me',
55-
'data-wp-init' => 'callbacks.initButtonStyles',
56-
)
52+
$wrapper_attributes = array(
53+
'id' => $block_id,
54+
'class' => 'activitypub-follow-me-block-wrapper',
55+
'data-wp-interactive' => 'activitypub/follow-me',
56+
'data-wp-init' => 'callbacks.initButtonStyles',
5757
);
58+
if ( isset( $attributes['buttonOnly'] ) ) {
59+
$wrapper_attributes['class'] .= ' is-style-button-only';
60+
}
5861

5962
$wrapper_context = wp_interactivity_data_wp_context(
6063
array(
@@ -65,7 +68,6 @@
6568
'errorMessage' => '',
6669
'copyButtonText' => __( 'Copy', 'activitypub' ),
6770
'userId' => $user_id,
68-
'buttonOnly' => $button_only,
6971
'buttonStyle' => $button_style,
7072
'backgroundColor' => $background_color,
7173
'webfinger' => '@' . $actor->get_webfinger(),
@@ -75,11 +77,13 @@
7577
)
7678
);
7779

78-
/* @var string $content Inner blocks content. */
7980
if ( empty( $content ) ) {
8081
$button_text = $attributes['buttonText'] ?? __( 'Follow', 'activitypub' );
8182
$content = '<div class="wp-block-button"><button class="wp-block-button__link wp-element-button">' . esc_html( $button_text ) . '</button></div>';
83+
} else {
84+
$content = implode( PHP_EOL, wp_list_pluck( $block->parsed_block['innerBlocks'], 'innerHTML' ) );
8285
}
86+
8387
$content = Blocks::add_directions(
8488
$content,
8589
array( 'class_name' => 'wp-element-button' ),
@@ -92,25 +96,71 @@
9296
)
9397
);
9498

99+
$header_image = $actor->get_image();
100+
$has_header = ! empty( $header_image['url'] ) && str_contains( $attributes['className'] ?? '', 'is-style-profile' );
101+
102+
$stats = array(
103+
'posts' => count_user_posts( $user_id, 'post', true ),
104+
'followers' => Followers::count_followers( $user_id ),
105+
);
106+
95107
?>
96108
<div
97-
<?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput ?>
109+
<?php echo get_block_wrapper_attributes( $wrapper_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
98110
<?php echo $wrapper_context; // phpcs:ignore WordPress.Security.EscapeOutput ?>
99111
>
100112
<div class="activitypub-profile">
101-
<?php if ( ! $button_only ) : ?>
113+
<?php if ( $has_header ) : ?>
114+
<div class="activitypub-profile__header" style="background-image: url('<?php echo esc_url( $header_image['url'] ); ?>');"></div>
115+
<?php endif; ?>
116+
117+
<div class="activitypub-profile__body">
102118
<img
103119
class="activitypub-profile__avatar"
104120
src="<?php echo esc_url( $actor->get_icon()['url'] ); ?>"
105121
alt="<?php echo esc_attr( $actor->get_name() ); ?>"
106122
/>
123+
107124
<div class="activitypub-profile__content">
108-
<div class="activitypub-profile__name"><?php echo esc_html( $actor->get_name() ); ?></div>
109-
<div class="activitypub-profile__handle"><?php echo esc_html( '@' . $actor->get_webfinger() ); ?></div>
110-
</div>
111-
<?php endif; ?>
125+
<div class="activitypub-profile__info">
126+
<div class="activitypub-profile__name"><?php echo esc_html( $actor->get_name() ); ?></div>
127+
<div class="activitypub-profile__handle"><?php echo esc_html( '@' . $actor->get_webfinger() ); ?></div>
128+
</div>
129+
130+
<?php echo $content; // phpcs:ignore WordPress.Security.EscapeOutput ?>
112131

113-
<?php echo $content; // phpcs:ignore WordPress.Security.EscapeOutput ?>
132+
<?php if ( $actor->get_summary() ) : ?>
133+
<div class="activitypub-profile__bio">
134+
<?php echo wp_kses_post( $actor->get_summary() ); ?>
135+
</div>
136+
<?php endif; ?>
137+
138+
<div class="activitypub-profile__stats">
139+
<?php if ( null !== $stats['posts'] ) : ?>
140+
<div>
141+
<?php
142+
printf(
143+
/* translators: %s: Number of posts */
144+
esc_html( _n( '%s post', '%s posts', (int) $stats['posts'], 'activitypub' ) ),
145+
'<strong>' . esc_html( number_format_i18n( $stats['posts'] ) ) . '</strong>'
146+
);
147+
?>
148+
</div>
149+
<?php endif; ?>
150+
<?php if ( null !== $stats['followers'] ) : ?>
151+
<div>
152+
<?php
153+
printf(
154+
/* translators: %s: Number of followers */
155+
esc_html( _n( '%s follower', '%s followers', (int) $stats['followers'], 'activitypub' ) ),
156+
'<strong>' . esc_html( number_format_i18n( $stats['followers'] ) ) . '</strong>'
157+
);
158+
?>
159+
</div>
160+
<?php endif; ?>
161+
</div>
162+
</div>
163+
</div>
114164
</div>
115165

116166
<?php

build/follow-me/style-index-rtl.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)