| 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> |
| 82 |
|