Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions block.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
"type": "string",
"default": ""
},
"customAvatarMediaId": {
"type": "integer",
"default": 0
},
"customAvatarMediaUrl": {
"type": "string",
"default": ""
},
"customTitle": {
"type": "string",
"default": ""
Expand Down
53 changes: 51 additions & 2 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,52 @@ public function __construct( array $attributes ) {
$this->attributes = $attributes;
}

/**
* Resolve the repository block avatar URL (custom image or GitHub owner avatar).
*
* Falls back to the repo owner's GitHub avatar for both user- and org-owned repos.
*/
protected function get_repo_avatar_url( object $data ): string {
$media_id = (int) ( $this->attributes['customAvatarMediaId'] ?? 0 );

if ( $media_id > 0 && wp_attachment_is_image( $media_id ) ) {
$custom_url = wp_get_attachment_image_url( $media_id, 'medium' );

if ( is_string( $custom_url ) && $custom_url !== '' ) {
return $custom_url;
}
}

return $data->owner->avatar_url;
}

/**
* Alt text for the repository block avatar image.
*/
protected function get_repo_avatar_alt( object $data ): string {
$media_id = (int) ( $this->attributes['customAvatarMediaId'] ?? 0 );

if ( $media_id > 0 ) {
$attachment_alt = get_post_meta( $media_id, '_wp_attachment_image_alt', true );

if ( is_string( $attachment_alt ) && $attachment_alt !== '' ) {
return $attachment_alt;
}
}

$owner_type = isset( $data->owner->type ) && $data->owner->type === 'Organization'
? __( 'organization', 'blocks-for-github' )
: __( 'owner', 'blocks-for-github' );

return sprintf(
/* translators: 1: repository name, 2: owner or organization label, 3: GitHub login */
__( '%1$s %2$s avatar (%3$s)', 'blocks-for-github' ),
$data->name,
$owner_type,
$data->owner->login
);
}

protected function fetchData( string $url, string $keySuffix = '' ) {
$key = "blocks_for_github_$keySuffix";
$data = get_transient( $key );
Expand Down Expand Up @@ -117,12 +163,15 @@ protected function getOutputOrError( $data, $output ) {
* @throws Exception
*/
public function renderRepo( $data ) {
$avatar_url = $this->get_repo_avatar_url( $data );
$avatar_alt = $this->get_repo_avatar_alt( $data );

ob_start(); ?>
<div class="bfg-wrap bfg-repo" id="bfg-wrap-<?php esc_html_e( $data->id ); ?>">
<div class="bfg-repo-header bfg-grid-container">
<div class="bfg-repo-avatar-wrap">
<img class="bfg-avatar" src="<?php esc_html_e( $data->owner->avatar_url ); ?>"
alt="<?php esc_html_e( $data->name ); ?>" />
<img class="bfg-avatar" src="<?php echo esc_url( $avatar_url ); ?>"
alt="<?php echo esc_attr( $avatar_alt ); ?>" />
</div>
<div class="bfg-repo-content">
<div class="bfg-repo-name-wrap">
Expand Down
97 changes: 96 additions & 1 deletion src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default function Edit({attributes, setAttributes}) {
showTwitter,
mediaId,
mediaUrl,
customAvatarMediaId,
customAvatarMediaUrl,
preview,
} = attributes;

Expand Down Expand Up @@ -79,11 +81,32 @@ export default function Edit({attributes, setAttributes}) {
});
};

const removeCustomAvatar = () => {
setAttributes({
customAvatarMediaId: 0,
customAvatarMediaUrl: '',
});
};

const onSelectCustomAvatar = (media) => {
setAttributes({
customAvatarMediaId: media.id,
customAvatarMediaUrl: media.url,
});
};

const media = useSelect(
(select) => {
return select('core').getMedia(mediaId);
},
[onSelectMedia]
[mediaId]
);

const customAvatarMedia = useSelect(
(select) => {
return select('core').getMedia(customAvatarMediaId);
},
[customAvatarMediaId]
);

return (
Expand Down Expand Up @@ -134,6 +157,76 @@ export default function Edit({attributes, setAttributes}) {
}}
/>
</PanelRow>
<PanelRow>
<div className="bfg-media-uploader">
<p className={'bfg-label'}>
<label>{__('Custom avatar', 'stellarpay')}</label>
</p>
<MediaUploadCheck>
<MediaUpload
onSelect={onSelectCustomAvatar}
value={customAvatarMediaId}
allowedTypes={['image']}
render={({open}) => (
<Button
className={
customAvatarMediaId === 0
? 'editor-post-featured-image__toggle'
: 'editor-post-featured-image__preview'
}
onClick={open}
>
{customAvatarMediaId === 0 &&
__('Choose an image', 'stellarpay')}
{customAvatarMedia !== undefined && (
<ResponsiveWrapper
naturalWidth={customAvatarMedia.media_details.width}
naturalHeight={customAvatarMedia.media_details.height}
>
<img src={customAvatarMedia.source_url} alt="" />
</ResponsiveWrapper>
)}
</Button>
)}
/>
</MediaUploadCheck>
<div className="bfg-media-btns">
{customAvatarMediaId !== 0 && (
<MediaUploadCheck>
<MediaUpload
title={__('Replace image', 'stellarpay')}
value={customAvatarMediaId}
onSelect={onSelectCustomAvatar}
allowedTypes={['image']}
render={({open}) => (
<Button
onClick={open}
isSmall
variant="secondary"
className={'bfg-replace-image-btn'}
>
{__('Replace image', 'stellarpay')}
</Button>
)}
/>
</MediaUploadCheck>
)}
{customAvatarMediaId !== 0 && (
<MediaUploadCheck>
<Button onClick={removeCustomAvatar} isSmall variant="secondary">
{__('Remove image', 'stellarpay')}
</Button>
</MediaUploadCheck>
)}
</div>
<p className={'bfg-help-text'}>
{__(
'Optional image for this repo block. When empty, the GitHub owner or organization avatar is used.',
'stellarpay'
)}
</p>
</div>
</PanelRow>
<PanelRow>
<CheckboxControl
label={__('Show tags', 'stellarpay')}
Expand Down Expand Up @@ -340,6 +433,8 @@ export default function Edit({attributes, setAttributes}) {
repoUrl: attributes.repoUrl,
mediaId: attributes.mediaId,
mediaUrl: attributes.mediaUrl,
customAvatarMediaId: attributes.customAvatarMediaId,
customAvatarMediaUrl: attributes.customAvatarMediaUrl,
showTags: attributes.showTags,
showForks: attributes.showForks,
showSubscribers: attributes.showSubscribers,
Expand Down