Skip to content

Commit

Permalink
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwiebe committed Sep 20, 2024
2 parents f5a55b4 + 18da65f commit d022392
Show file tree
Hide file tree
Showing 20 changed files with 246 additions and 37 deletions.
46 changes: 43 additions & 3 deletions assets/js/activitypub-header-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,42 @@
$headerImagePreview = $( '#activitypub-header-image-preview' ),
$hiddenDataField = $( '#activitypub_header_image' ),
$removeButton = $( '#activitypub-remove-header-image' ),
frame;
frame,
ImageCropperNoCustomizer;

/**
* We register our own handler because the Core one invokes the Customizer, which fails the request unnecessarily
* for users who don't have the 'customize' capability.
* See https://github.com/Automattic/wordpress-activitypub/issues/846
*/
ImageCropperNoCustomizer = wp.media.controller.CustomizeImageCropper.extend( {
doCrop: function( attachment ) {
var cropDetails = attachment.get( 'cropDetails' ),
control = this.get( 'control' ),
ratio = cropDetails.width / cropDetails.height;

// Use crop measurements when flexible in both directions.
if ( control.params.flex_width && control.params.flex_height ) {
cropDetails.dst_width = cropDetails.width;
cropDetails.dst_height = cropDetails.height;

// Constrain flexible side based on image ratio and size of the fixed side.
} else {
cropDetails.dst_width = control.params.flex_width ? control.params.height * ratio : control.params.width;
cropDetails.dst_height = control.params.flex_height ? control.params.width / ratio : control.params.height;
}

return wp.ajax.post( 'crop-image', {
// where wp_customize: 'on' would be in Core, for no good reason I understand.
nonce: attachment.get( 'nonces' ).edit,
id: attachment.get( 'id' ),
context: control.id,
cropDetails: cropDetails
} );
}
} );



/**
* Calculate image selection options based on the attachment dimensions.
Expand Down Expand Up @@ -73,6 +108,11 @@
*/
$chooseButton.on( 'click', function () {
var $el = $( this );
var userId = $el.data( 'userId' );
var mediaQuery = { type: 'image' };
if ( userId ) {
mediaQuery.author = userId;
}

// Create the media frame.
frame = wp.media( {
Expand All @@ -86,12 +126,12 @@
states: [
new wp.media.controller.Library( {
title: $el.data( 'choose-text' ),
library: wp.media.query( { type: 'image' } ),
library: wp.media.query( mediaQuery ),
date: false,
suggestedWidth: $el.data( 'size' ),
suggestedHeight: $el.data( 'size' ),
} ),
new wp.media.controller.CustomizeImageCropper( {
new ImageCropperNoCustomizer( {
control: {
params: {
width: $el.data( 'size' ),
Expand Down
4 changes: 4 additions & 0 deletions build/follow-me/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"default": "site"
}
},
"usesContext": [
"postType",
"postId"
],
"editorScript": "file:./index.js",
"viewScript": "file:./view.js",
"style": [
Expand Down
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-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'ef5b5c7e3b4b9ceeb7dd');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'f33252a2026002ddb56c');
3 changes: 2 additions & 1 deletion build/follow-me/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/follow-me/view.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'ec1f2f8a193d8d386abc');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'ab8c0dad126bb0a61ed6');
2 changes: 1 addition & 1 deletion build/follow-me/view.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions build/followers/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
]
}
},
"usesContext": [
"postType",
"postId"
],
"styles": [
{
"name": "default",
Expand Down
2 changes: 1 addition & 1 deletion build/followers/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-data', 'wp-element', 'wp-i18n', 'wp-primitives', 'wp-url'), 'version' => 'aaa7aa378fccecb81d65');
<?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', 'wp-url'), 'version' => '28a5bef9295566598f5c');
7 changes: 4 additions & 3 deletions build/followers/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/followers/view.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => 'fa8a5fc56fc73c38ebad');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => '111b88843c05346aadbf');
6 changes: 3 additions & 3 deletions build/followers/view.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 67 additions & 7 deletions includes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,56 @@ public static function register_blocks() {
);
}

/**
* Get the user ID from a user string.
*
* @param string $user_string The user string. Can be a user ID, 'site', or 'inherit'.
* @return int|null The user ID, or null if the 'inherit' string is not supported in this context.
*/
private static function get_user_id( $user_string ) {
if ( is_numeric( $user_string ) ) {
return absint( $user_string );
}
// any other non-numeric falls back to 0, including the `site` string used in the UI
return 0;

// if the user string is 'site', return the Blog User ID.
if ( 'site' === $user_string ) {
return User_Collection::BLOG_USER_ID;
}

// The only other value should be 'inherit', which means to use the query context to determine the User.
if ( 'inherit' !== $user_string ) {
return null;
}

// For a homepage/front page, if the Blog User is active, use it.
if ( ( is_front_page() || is_home() ) && ! is_user_type_disabled( 'blog' ) ) {
return User_Collection::BLOG_USER_ID;
}

// If we're in a loop, use the post author
$author_id = get_the_author_meta( 'ID' );
if ( $author_id ) {
return $author_id;
}

// For other pages, the queried object will clue us in.
$queried_object = get_queried_object();
if ( ! $queried_object ) {
return null;
}

// If we're on a user archive page, use that user's ID.
if ( is_a( $queried_object, 'WP_User' ) ) {
return $queried_object->ID;
}

// For a single post, use the post author's ID.
if ( is_a( $queried_object, 'WP_Post' ) ) {
return get_the_author_meta( 'ID' );
}

// We won't properly account for some conditions, like tag archives.
return null;
}

/**
Expand All @@ -93,13 +137,21 @@ protected static function filter_array_by_keys( $array, $keys ) {
public static function render_follow_me_block( $attrs ) {
$user_id = self::get_user_id( $attrs['selectedUser'] );
$user = User_Collection::get_by_id( $user_id );
if ( ! is_wp_error( $user ) ) {
$attrs['profileData'] = self::filter_array_by_keys(
$user->to_array(),
array( 'icon', 'name', 'webfinger' )
);
if ( is_wp_error( $user ) ) {
if ( 'inherit' === $attrs['selectedUser'] ) {
// If the user is 'inherit' and we couldn't determine the user, don't render anything.
return '<!-- Follow Me block: `inherit` mode does not display on this type of page -->';
} else {
// If the user is a specific ID and we couldn't find it, render an error message.
return '<!-- Follow Me block: user not found -->';
}
}

$attrs['profileData'] = self::filter_array_by_keys(
$user->to_array(),
array( 'icon', 'name', 'webfinger' )
);

// add `@` prefix if it's missing
if ( '@' !== substr( $attrs['profileData']['webfinger'], 0, 1 ) ) {
$attrs['profileData']['webfinger'] = '@' . $attrs['profileData']['webfinger'];
Expand All @@ -118,6 +170,14 @@ public static function render_follow_me_block( $attrs ) {

public static function render_follower_block( $attrs ) {
$followee_user_id = self::get_user_id( $attrs['selectedUser'] );
if ( is_null( $followee_user_id ) ) {
return '<!-- Followers block: `inherit` mode does not display on this type of page -->';
}
$user = User_Collection::get_by_id( $followee_user_id );
if ( is_wp_error( $user ) ) {
return '<!-- Followers block: `' . $followee_user_id . '` not an active ActivityPub user -->';
}

$per_page = absint( $attrs['per_page'] );
$follower_data = Followers::get_followers_with_count( $followee_user_id, $per_page );

Expand Down
Loading

0 comments on commit d022392

Please sign in to comment.