|
7 | 7 |
|
8 | 8 | use Activitypub\Blocks;
|
9 | 9 | use Activitypub\Collection\Actors;
|
| 10 | +use Activitypub\Collection\Followers; |
10 | 11 |
|
11 | 12 | /* @var array $attributes Block attributes. */
|
12 | 13 | $attributes = wp_parse_args( $attributes );
|
13 | 14 |
|
| 15 | +/* @var WP_Block $block Parsed block.*/ |
| 16 | +$block = $block ?? null; |
| 17 | + |
| 18 | +/* @var string $content Inner blocks content. */ |
| 19 | +$content = $content ?? ''; |
| 20 | + |
14 | 21 | // 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 | +} |
18 | 27 |
|
19 | 28 | // Generate a unique ID for the block.
|
20 | 29 | $block_id = 'activitypub-follow-me-block-' . wp_unique_id();
|
21 | 30 |
|
22 | 31 | // Get block style information.
|
23 | 32 | $style = wp_get_global_styles();
|
24 | 33 | $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(); |
33 | 35 |
|
34 | 36 | // Set up the Interactivity API state.
|
35 | 37 | wp_interactivity_state(
|
|
47 | 49 | );
|
48 | 50 |
|
49 | 51 | // 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', |
57 | 57 | );
|
| 58 | +if ( isset( $attributes['buttonOnly'] ) ) { |
| 59 | + $wrapper_attributes['class'] .= ' is-style-button-only'; |
| 60 | +} |
58 | 61 |
|
59 | 62 | $wrapper_context = wp_interactivity_data_wp_context(
|
60 | 63 | array(
|
|
65 | 68 | 'errorMessage' => '',
|
66 | 69 | 'copyButtonText' => __( 'Copy', 'activitypub' ),
|
67 | 70 | 'userId' => $user_id,
|
68 |
| - 'buttonOnly' => $button_only, |
69 | 71 | 'buttonStyle' => $button_style,
|
70 | 72 | 'backgroundColor' => $background_color,
|
71 | 73 | 'webfinger' => '@' . $actor->get_webfinger(),
|
|
75 | 77 | )
|
76 | 78 | );
|
77 | 79 |
|
78 |
| -/* @var string $content Inner blocks content. */ |
79 | 80 | if ( empty( $content ) ) {
|
80 | 81 | $button_text = $attributes['buttonText'] ?? __( 'Follow', 'activitypub' );
|
81 | 82 | $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' ) ); |
82 | 85 | }
|
| 86 | + |
83 | 87 | $content = Blocks::add_directions(
|
84 | 88 | $content,
|
85 | 89 | array( 'class_name' => 'wp-element-button' ),
|
|
92 | 96 | )
|
93 | 97 | );
|
94 | 98 |
|
| 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 | + |
95 | 107 | ?>
|
96 | 108 | <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 ?> |
98 | 110 | <?php echo $wrapper_context; // phpcs:ignore WordPress.Security.EscapeOutput ?>
|
99 | 111 | >
|
100 | 112 | <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"> |
102 | 118 | <img
|
103 | 119 | class="activitypub-profile__avatar"
|
104 | 120 | src="<?php echo esc_url( $actor->get_icon()['url'] ); ?>"
|
105 | 121 | alt="<?php echo esc_attr( $actor->get_name() ); ?>"
|
106 | 122 | />
|
| 123 | + |
107 | 124 | <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 ?> |
112 | 131 |
|
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> |
114 | 164 | </div>
|
115 | 165 |
|
116 | 166 | <?php
|
|
0 commit comments