-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blocks: Add a new "Avatar" block to use in Query Loop (#695)
* Post Types: Add featured image support to organizers and speakers. * Blocks: Add "Avatar" block to output speaker & organizer gravatars * Add extra avatar size options * Enable duotone feature on avatars * Use the featured image for avatar if one exists * Update the logic to always return an image, falling back to the default gravatar * Display the featured image in place of the avatar * Hide featured images on speaker & organizer posts to avoid changing behavior * Use new avatar_or_image function in speaker & organizer blocks * Update escaping * Update hook comment to explain reasoning
- Loading branch information
Showing
12 changed files
with
374 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
public_html/wp-content/mu-plugins/blocks/source/blocks/avatar/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "wordcamp/avatar", | ||
"title": "Avatar", | ||
"category": "wordcamp", | ||
"description": "Display the current person's avatar.", | ||
"textdomain": "wordcamporg", | ||
"usesContext": [ "postId", "postType", "queryId" ], | ||
"attributes": { | ||
"isLink": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"size": { | ||
"type": "number" | ||
} | ||
}, | ||
"supports": { | ||
"align": [ "left", "right", "center" ], | ||
"color": { | ||
"__experimentalDuotone": "img", | ||
"text": false, | ||
"background": false | ||
}, | ||
"html": false | ||
}, | ||
"editor_script": "wordcamp-blocks", | ||
"editor_style": "wordcamp-blocks" | ||
} |
112 changes: 112 additions & 0 deletions
112
public_html/wp-content/mu-plugins/blocks/source/blocks/avatar/controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
namespace WordCamp\Blocks\Avatar; | ||
use function WordCamp\Post_Types\Utilities\get_avatar_or_image; | ||
|
||
defined( 'WPINC' ) || die(); | ||
|
||
/** | ||
* Register block types and enqueue scripts. | ||
* | ||
* @return void | ||
*/ | ||
function init() { | ||
register_block_type_from_metadata( | ||
__DIR__, | ||
array( | ||
'attributes' => get_attributes_schema(), | ||
'render_callback' => __NAMESPACE__ . '\render', | ||
) | ||
); | ||
} | ||
add_action( 'init', __NAMESPACE__ . '\init' ); | ||
|
||
|
||
/** | ||
* Renders the block on the server. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
* @return string Returns the avatar for the current post. | ||
*/ | ||
function render( $attributes, $content, $block ) { | ||
if ( ! isset( $block->context['postId'] ) ) { | ||
return ''; | ||
} | ||
$post_ID = $block->context['postId']; | ||
|
||
$defaults = wp_list_pluck( get_attributes_schema(), 'default' ); | ||
$attributes = wp_parse_args( $attributes, $defaults ); | ||
$email = get_post_meta( $post_ID, '_wcb_speaker_email', true ); | ||
$user_id = get_post_meta( $post_ID, '_wcpt_user_id', true ); | ||
|
||
$size = intval( $attributes['size'] ); | ||
$wrapper_attributes = get_block_wrapper_attributes( array( | ||
'style' => "width:{$size}px;height:{$size}px;", | ||
) ); | ||
|
||
$avatar = get_avatar_or_image( $post_ID, $size ); | ||
|
||
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { | ||
$avatar = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $avatar ); | ||
} | ||
|
||
return "<figure $wrapper_attributes>$avatar</figure>"; | ||
} | ||
|
||
/** | ||
* Add data to be used by the JS scripts in the block editor. | ||
* | ||
* @param array $data | ||
* | ||
* @return array | ||
*/ | ||
function add_script_data( array $data ) { | ||
$data['avatar'] = array( | ||
'schema' => get_attributes_schema(), | ||
'options' => get_options(), | ||
); | ||
|
||
return $data; | ||
} | ||
add_filter( 'wordcamp_blocks_script_data', __NAMESPACE__ . '\add_script_data' ); | ||
|
||
/** | ||
* Get the schema for the block's attributes. | ||
* | ||
* @return array | ||
*/ | ||
function get_attributes_schema() { | ||
$schema = array( | ||
'isLink' => array( | ||
'type' => 'boolean', | ||
'default' => false, | ||
), | ||
'size' => array( | ||
'type' => 'number', | ||
'enum' => get_options( 'size' ), | ||
'default' => 96, | ||
), | ||
); | ||
|
||
return $schema; | ||
} | ||
|
||
/** | ||
* Get the label/value pairs for all options or a specific type. | ||
* | ||
* @param string $type | ||
* | ||
* @return array | ||
*/ | ||
function get_options( $type = '' ) { | ||
$options = array( | ||
'size' => rest_get_avatar_sizes(), | ||
); | ||
|
||
if ( $type ) { | ||
return empty( $options[ $type ] ) ? array() : $options[ $type ]; | ||
} | ||
|
||
return $options; | ||
} |
77 changes: 77 additions & 0 deletions
77
public_html/wp-content/mu-plugins/blocks/source/blocks/avatar/edit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { store as coreStore, useEntityProp } from '@wordpress/core-data'; | ||
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; | ||
import { PanelBody, SelectControl, ToggleControl } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
const placeholderChip = ( | ||
<div className="post-featured-image_placeholder"> | ||
<p> { __( 'Avatar', 'wordcamporg' ) }</p> | ||
</div> | ||
); | ||
|
||
function getPostLabel( postType ) { | ||
switch ( postType ) { | ||
case 'wcb_speaker': | ||
return __( 'Speaker', 'wordcamporg' ); | ||
case 'wcb_organizer': | ||
return __( 'Organizer', 'wordcamporg' ); | ||
default: | ||
return __( 'post', 'wordcamporg' ); | ||
} | ||
} | ||
|
||
const blockData = window.WordCampBlocks.avatar || {}; | ||
|
||
export default function PostAvatarEdit( { attributes, setAttributes, context: { postId, postType } } ) { | ||
const { isLink, size = blockData.schema.size.default } = attributes; | ||
const [ urls ] = useEntityProp( 'postType', postType, 'avatar_urls', postId ); | ||
const blockProps = useBlockProps( { style: { width: size, height: size } } ); | ||
const [ featuredImage ] = useEntityProp( 'postType', postType, 'featured_media', postId ); | ||
const url = useSelect( | ||
( select ) => { | ||
if ( ! featuredImage ) { | ||
return urls ? urls[ size ] : ''; | ||
} | ||
const image = select( coreStore ).getMedia( featuredImage, { context: 'view' } ); | ||
return image?.source_url || ''; | ||
}, | ||
[ featuredImage ] | ||
); | ||
|
||
if ( ! url ) { | ||
return <div { ...blockProps }>{ placeholderChip }</div>; | ||
} | ||
|
||
const sizeOptions = blockData.options.size.map( ( value ) => ( { label: value + 'px', value: value } ) ); | ||
|
||
return ( | ||
<> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Settings', 'wordcamporg' ) }> | ||
<SelectControl | ||
label={ __( 'Avatar size', 'wordcamporg' ) } | ||
value={ size } | ||
options={ sizeOptions } | ||
onChange={ ( newSize ) => setAttributes( { size: Number( newSize ) } ) } | ||
/> | ||
<ToggleControl | ||
label={ sprintf( | ||
// translators: %s: Name of the post type e.g: "post". | ||
__( 'Link to %s', 'wordcamporg' ), | ||
getPostLabel( postType ) | ||
) } | ||
onChange={ () => setAttributes( { isLink: ! isLink } ) } | ||
checked={ isLink } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
<figure { ...blockProps }> | ||
<img src={ url } alt={ __( 'Avatar', 'wordcamporg' ) } /> | ||
</figure> | ||
</> | ||
); | ||
} |
18 changes: 18 additions & 0 deletions
18
public_html/wp-content/mu-plugins/blocks/source/blocks/avatar/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { people as icon } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import edit from './edit'; | ||
import metadata from './block.json'; | ||
|
||
export const NAME = metadata.name; | ||
|
||
export const SETTINGS = { | ||
...metadata, | ||
icon, | ||
edit, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.