Skip to content

Follow Me: Improve MU compatibility #1777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/follow-me/index.asset.php
Original file line number Diff line number Diff line change
@@ -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' => 'd10a4c1f7765f3cb91d3');
<?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' => '4d0ba0d6e2cbe2df8df7');
4 changes: 2 additions & 2 deletions build/follow-me/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/follow-me/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
$has_header = ! empty( $header_image['url'] ) && str_contains( $attributes['className'] ?? '', 'is-style-profile' );

$stats = array(
'posts' => count_user_posts( $user_id, 'post', true ),
'posts' => $user_id ? count_user_posts( $user_id, 'post', true ) : (int) wp_count_posts()->publish,
'followers' => Followers::count_followers( $user_id ),
);

Expand Down
2 changes: 1 addition & 1 deletion build/reply/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives', 'wp-url'), 'version' => '78dbc26b5e405051df4a');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives', 'wp-url'), 'version' => 'e4cc0f6ffd681f4d678d');
2 changes: 1 addition & 1 deletion build/reply/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/follow-me/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"name": "activitypub/follow-me",
"apiVersion": 3,
"version": "2.1.0",
"version": "2.2.0",
"title": "Follow me on the Fediverse",
"category": "widgets",
"description": "Display your Fediverse profile so that visitors can follow you.",
Expand Down
64 changes: 62 additions & 2 deletions src/follow-me/deprecation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classnames from 'classnames';
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

/**
* The block supports for the Follow Me block in version 1.
Expand Down Expand Up @@ -45,6 +46,7 @@ function migrateButtonOnly( { buttonOnly = false, className = '', ...newAttribut

return newAttributes;
}

/**
* Deprecation for the Follow Me block to use a core button block instead of the custom button.
* This handles the migration of the buttonText and buttonSize attributes to the innerBlock.
Expand Down Expand Up @@ -88,7 +90,6 @@ const v1 = {
*/
migrate( { buttonText, ...newAttributes } ) {
const buttonBlock = createBlock( 'core/button', {
tagName: 'button',
text: buttonText,
} );

Expand Down Expand Up @@ -147,4 +148,63 @@ const v2 = {
},
};

export default [ v2, v1 ];
/**
* Deprecation for the Follow Me block.
* Handles the case where the button HTML is stripped due to unfiltered_html capability restrictions.
*/
const v3 = {
attributes: {
selectedUser: {
type: 'string',
default: 'site',
},
},

supports: v2BlockSupports,

/**
* Checks if the block is eligible for migration.
*
* @param {Object} attributes The block attributes.
* @param {array} innerBlocks The inner blocks.
*
* @return {boolean} Whether the block is eligible for migration.
*/
isEligible( attributes, innerBlocks ) {
return ! innerBlocks[ 0 ].isValid && 'button' === innerBlocks[ 0 ].attributes.tagName;
},

/**
* Migrates the Follow Me block to fix the broken button.
*
* @param {Object} attributes The block attributes.
* @param {array} innerBlocks The inner blocks.
*
* @return {[Object, Array]} An array with the new block attributes and inner blocks.
*/
migrate( attributes, innerBlocks ) {
const { tagName, text, ...buttonAttributes } = innerBlocks[ 0 ].attributes;

const textMatch = innerBlocks[ 0 ].originalContent.match( /<div class="wp-block-button">(.*?)<\/div>/ );
const buttonText = textMatch ? textMatch[ 1 ] : __( 'Follow', 'activitypub' );

// Create a proper button block with the correct structure and the extracted text
const buttonBlock = createBlock( 'core/button', { buttonAttributes, text: buttonText } );

return [ attributes, [ buttonBlock ] ];
},

/**
* Save function for the Follow Me block.
*
* @return {JSX.Element} React element to save.
*/
save() {
const blockProps = useBlockProps.save();
const innerBlocksProps = useInnerBlocksProps.save( blockProps );

return <div { ...innerBlocksProps } />;
},
};

export default [ v3, v2, v1 ];
42 changes: 24 additions & 18 deletions src/follow-me/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ function fetchProfile( userId ) {
* @return {JSX.Element} Profile component.
*/
function EditorProfile( { profile, className, innerBlocksProps } ) {
const { webfinger, avatar, name, image, summary, followers, posts } = profile;
const { webfinger, avatar, name, image, summary, followersCount, postsCount } = profile;

// Ensure we're checking for the right className format
const isButtonOnly = className && className.includes( 'is-style-button-only' );

// Stats for the editor preview - use real followers count if available
const stats = {
posts: posts || 17,
followers: followers || 0,
posts: postsCount || 0,
followers: followersCount || 0,
};

return (
Expand Down Expand Up @@ -142,7 +142,7 @@ export default function Edit( { attributes, setAttributes, context: { postType,
const [ profile, setProfile ] = useState( getNormalizedProfile( DEFAULT_PROFILE_DATA ) );
const userId = selectedUser === 'site' ? 0 : selectedUser;

const TEMPLATE = [ [ 'core/button', { text: __( 'Follow', 'activitypub' ), tagName: 'button' } ] ];
const TEMPLATE = [ [ 'core/button', { text: __( 'Follow', 'activitypub' ) } ] ];

const innerBlocksProps = useInnerBlocksProps(
{},
Expand Down Expand Up @@ -182,23 +182,29 @@ export default function Edit( { attributes, setAttributes, context: { postType,
const { pathname: path } = new URL( data.followers );

apiFetch( { path: path.replace( 'wp-json/', '' ) } )
.then( ( followers ) => {
const followersCount = followers?.totalItems || 0;

// Update the profile with followers counts.
setProfile( ( prevProfile ) => ( { ...prevProfile, followers: followersCount } ) );
.then( ( { totalItems = 0 } ) => {
setProfile( ( prevProfile ) => ( { ...prevProfile, followersCount: totalItems } ) );
} )
.catch( () => {} );
} catch ( e ) {
// If URL parsing fails, just continue without fetching followers.
}
}

apiFetch( { path: `/wp/v2/users/${ effectiveUserId }/?context=activitypub` } )
.then( ( { post_count } ) => {
setProfile( ( prevProfile ) => ( { ...prevProfile, posts: post_count } ) );
} )
.catch( () => {} );
if ( effectiveUserId ) {
apiFetch( { path: `/wp/v2/users/${ effectiveUserId }/?context=activitypub` } )
.then( ( { post_count } ) => {
setProfile( ( prevProfile ) => ( { ...prevProfile, postsCount: post_count } ) );
} )
.catch( () => {} );
} else {
const { namespace } = useOptions();
apiFetch( { path: `/${ namespace }/nodeinfo/2.0` } )
.then( ( { usage: { localPosts } } ) => {
setProfile( ( prevProfile ) => ( { ...prevProfile, postsCount: localPosts } ) );
} )
.catch( () => {} );
}
} )
.catch( () => {} );
}, [ userId, authorId, isInheritMode ] );
Expand All @@ -217,8 +223,8 @@ export default function Edit( { attributes, setAttributes, context: { postType,
return (
<div { ...blockProps }>
<InspectorControls key="activitypub-follow-me">
<PanelBody title={ __( 'Follow Me Options', 'activitypub' ) }>
{ usersOptions.length > 1 && (
{ usersOptions.length > 1 && (
<PanelBody title={ __( 'Follow Me Options', 'activitypub' ) }>
<SelectControl
label={ __( 'Select User', 'activitypub' ) }
value={ attributes.selectedUser }
Expand All @@ -227,8 +233,8 @@ export default function Edit( { attributes, setAttributes, context: { postType,
__next40pxDefaultSize
__nextHasNoMarginBottom
/>
) }
</PanelBody>
</PanelBody>
) }
</InspectorControls>

{ isInheritMode && ! authorId ? (
Expand Down
2 changes: 1 addition & 1 deletion src/follow-me/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
$has_header = ! empty( $header_image['url'] ) && str_contains( $attributes['className'] ?? '', 'is-style-profile' );

$stats = array(
'posts' => count_user_posts( $user_id, 'post', true ),
'posts' => $user_id ? count_user_posts( $user_id, 'post', true ) : (int) wp_count_posts()->publish,
'followers' => Followers::count_followers( $user_id ),
);

Expand Down
Loading