Skip to content

Commit cac552f

Browse files
authored
Add Extra Fields block for ActivityPub profiles (#2439)
1 parent 9e61e83 commit cac552f

File tree

16 files changed

+1542
-3
lines changed

16 files changed

+1542
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Added a new Fediverse Extra Fields block to display ActivityPub extra fields, featuring compact, stacked, and card layouts with flexible user selection options.

assets/css/activitypub-welcome.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,19 @@
223223
padding: 15px;
224224
}
225225

226-
.profile-field {
226+
.extra-field {
227227
margin-bottom: 15px;
228228
}
229229

230-
.profile-field label {
230+
.extra-field label {
231231
display: block;
232232
margin-bottom: 5px;
233233
font-weight: 500;
234234
font-size: 13px;
235235
color: #646970;
236236
}
237237

238-
.profile-field input {
238+
.extra-field input {
239239
width: 100%;
240240
padding: 8px;
241241
font-size: 13px;

build/extra-fields/block.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"name": "activitypub/extra-fields",
4+
"apiVersion": 3,
5+
"version": "1.0.0",
6+
"title": "Fediverse Extra Fields",
7+
"category": "widgets",
8+
"description": "Display extra fields from ActivityPub user profiles",
9+
"textdomain": "activitypub",
10+
"icon": "list-view",
11+
"supports": {
12+
"html": false,
13+
"align": [
14+
"wide",
15+
"full"
16+
],
17+
"color": {
18+
"gradients": true,
19+
"link": true,
20+
"__experimentalDefaultControls": {
21+
"background": true,
22+
"text": true,
23+
"link": true
24+
}
25+
},
26+
"__experimentalBorder": {
27+
"radius": true,
28+
"width": true,
29+
"color": true,
30+
"style": true
31+
},
32+
"shadow": true,
33+
"typography": {
34+
"fontSize": true,
35+
"__experimentalDefaultControls": {
36+
"fontSize": true
37+
}
38+
}
39+
},
40+
"styles": [
41+
{
42+
"name": "compact",
43+
"label": "Compact",
44+
"isDefault": true
45+
},
46+
{
47+
"name": "stacked",
48+
"label": "Stacked"
49+
},
50+
{
51+
"name": "cards",
52+
"label": "Cards"
53+
}
54+
],
55+
"attributes": {
56+
"selectedUser": {
57+
"type": "string",
58+
"default": "blog"
59+
},
60+
"maxFields": {
61+
"type": "number",
62+
"default": 0
63+
}
64+
},
65+
"usesContext": [
66+
"postType",
67+
"postId"
68+
],
69+
"editorScript": "file:./index.js",
70+
"style": "file:./style-index.css",
71+
"render": "file:./render.php"
72+
}

build/extra-fields/index.asset.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '6f7cc2955f584ad618ba');

build/extra-fields/index.js

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

build/extra-fields/render.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Server-side rendering for the Extra Fields block.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
use Activitypub\Blocks;
9+
use Activitypub\Collection\Extra_Fields;
10+
11+
use function Activitypub\is_activitypub_request;
12+
13+
if ( is_activitypub_request() || is_feed() ) {
14+
return '';
15+
}
16+
17+
/**
18+
* Render callback for the Extra Fields block.
19+
*
20+
* @var array $attributes Block attributes.
21+
*/
22+
$attributes = wp_parse_args( $attributes );
23+
$user_id = Blocks::get_user_id( $attributes['selectedUser'] ?? 'blog' );
24+
25+
// If user ID couldn't be determined, return empty.
26+
if ( null === $user_id ) {
27+
return '';
28+
}
29+
30+
// Get extra fields for this user.
31+
$fields = Extra_Fields::get_actor_fields( $user_id );
32+
33+
// Apply max fields limit if set.
34+
$max_fields = $attributes['maxFields'] ?? 0;
35+
if ( $max_fields > 0 && count( $fields ) > $max_fields ) {
36+
$fields = array_slice( $fields, 0, $max_fields );
37+
}
38+
39+
// Return empty on frontend if no fields (hide block).
40+
if ( empty( $fields ) ) {
41+
return '';
42+
}
43+
44+
// Get block wrapper attributes.
45+
$wrapper_attributes = get_block_wrapper_attributes(
46+
array(
47+
'class' => 'activitypub-extra-fields-block-wrapper',
48+
)
49+
);
50+
51+
// Extract background color for cards style.
52+
$background_color = '';
53+
$card_style = '';
54+
55+
// Check if this is the cards style by looking at className attribute or wrapper classes.
56+
$is_cards_style = ( isset( $attributes['className'] ) && str_contains( $attributes['className'], 'is-style-cards' ) )
57+
|| str_contains( $wrapper_attributes, 'is-style-cards' );
58+
59+
if ( $is_cards_style ) {
60+
// Check for background color in various formats.
61+
if ( isset( $attributes['backgroundColor'] ) ) {
62+
$background_color = sprintf( 'var(--wp--preset--color--%s)', $attributes['backgroundColor'] );
63+
} elseif ( isset( $attributes['style']['color']['background'] ) ) {
64+
$background_color = $attributes['style']['color']['background'];
65+
}
66+
67+
if ( $background_color ) {
68+
$card_style = sprintf( ' style="background-color: %s;"', esc_attr( $background_color ) );
69+
}
70+
}
71+
?>
72+
<div <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
73+
<dl class="activitypub-extra-fields">
74+
<?php foreach ( $fields as $field ) : ?>
75+
<div class="activitypub-extra-field"<?php echo $card_style; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
76+
<dt><?php echo esc_html( $field->post_title ); ?></dt>
77+
<dd><?php echo Extra_Fields::get_formatted_content( $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></dd>
78+
</div>
79+
<?php endforeach; ?>
80+
</dl>
81+
</div>

build/extra-fields/style-index-rtl.css

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

build/extra-fields/style-index.css

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

includes/class-blocks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public static function enqueue_editor_assets() {
4242
'blog' => ! is_user_type_disabled( 'blog' ),
4343
'users' => ! is_user_type_disabled( 'user' ),
4444
),
45+
'profileUrls' => array(
46+
'user' => \admin_url( 'profile.php#activitypub' ),
47+
'blog' => \admin_url( 'options-general.php?page=activitypub&tab=blog-profile' ),
48+
),
4549
);
4650
wp_localize_script( 'wp-editor', '_activityPubOptions', $data );
4751

@@ -76,6 +80,7 @@ public static function handle_in_reply_to_get_param() {
7680
* Register the blocks.
7781
*/
7882
public static function register_blocks() {
83+
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/extra-fields' );
7984
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/follow-me' );
8085
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/followers' );
8186
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/reactions' );

0 commit comments

Comments
 (0)