Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/blocks/my-account-button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Provides a reader account/sign-in button for sites using Newspack Reader Activat
## Settings
- Signed in label (`signedInLabel`): Text shown when the reader is authenticated.
- Signed out label (`signedOutLabel`): Text shown when the reader is not authenticated.
- Display: ability to toggle on/off the display of the button's icon or text label. Note: you cannot toggle off both the icon and label at the same time.

## Editor behavior
- Use the toolbar toggle (Signed in / Signed out) to edit each label.
Expand Down
8 changes: 8 additions & 0 deletions src/blocks/my-account-button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
"signedOutLabel": {
"type": "string",
"default": "Sign in"
},
"showLabel": {
"type": "boolean",
"default": true
},
"showIcon": {
"type": "boolean",
"default": true
}
},
"supports": {
Expand Down
45 changes: 30 additions & 15 deletions src/blocks/my-account-button/class-my-account-button-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,19 @@ public static function render_block( $attrs ) {
];
$attrs = \wp_parse_args( $attrs, $default_attrs );

$is_signed_in = \is_user_logged_in();
$label = $is_signed_in ? $attrs['signedInLabel'] : $attrs['signedOutLabel'];
$is_signed_in = \is_user_logged_in();
$signed_in_label = '' === trim( (string) $attrs['signedInLabel'] ) ? $default_attrs['signedInLabel'] : $attrs['signedInLabel'];
$signed_out_label = '' === trim( (string) $attrs['signedOutLabel'] ) ? $default_attrs['signedOutLabel'] : $attrs['signedOutLabel'];
$label = $is_signed_in ? $signed_in_label : $signed_out_label;
$show_label = isset( $attrs['showLabel'] ) ? (bool) $attrs['showLabel'] : true;
$show_icon = isset( $attrs['showIcon'] ) ? (bool) $attrs['showIcon'] : true;

if ( ! $show_label && ! $show_icon ) {
$show_label = true;
}

if ( '' === trim( (string) $label ) ) {
return '';
$label = $is_signed_in ? $default_attrs['signedInLabel'] : $default_attrs['signedOutLabel'];
}

$account_url = self::get_account_url();
Expand All @@ -118,8 +127,8 @@ public static function render_block( $attrs ) {
}

$labels = [
'signedin' => $attrs['signedInLabel'],
'signedout' => $attrs['signedOutLabel'],
'signedin' => $signed_in_label,
'signedout' => $signed_out_label,
];

$extra_classes = [
Expand Down Expand Up @@ -153,20 +162,26 @@ public static function render_block( $attrs ) {
$wrapper_div_classes = \array_merge( $wrapper_div_classes, $custom_classes );
}

$wrapper_attributes = \get_block_wrapper_attributes(
[
'class' => implode( ' ', $extra_classes ),
'href' => \esc_url_raw( $href ),
]
);
$wrapper_attribute_args = [
'class' => implode( ' ', $extra_classes ),
'href' => \esc_url_raw( $href ),
'data-wp-logged-in' => $is_signed_in ? '1' : '0',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data-wp-* prefix is used by WordPress Interactivity API, and custom plugins should avoid it to prevent future collisions. Can we rename it to something different, like data-newspack-logged-in?

Also, this attribute explicitly states the user's authentication state in the DOM, though it's already inferable from the button's href (hashtag vs anything else). Maybe this is not really needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rbcorrales! I updated the attribute name in 81ee30d, but left it -- it's a little yucky, but the My Account Button block behaves a little different than the My Account link that's inserted into the theme (the latter isn't visible for logged in non-readers, and when it is in the Customizer, it says 'Sign In'). The attribute seemed like the nicest way to switch it in just the one spot.

];
$wrapper_attributes = \get_block_wrapper_attributes( $wrapper_attribute_args );

$link = '<div class="' . \esc_attr( implode( ' ', $wrapper_div_classes ) ) . '">';
$link .= '<div class="wp-block-button">';
$link .= '<a ' . $wrapper_attributes . ' data-labels="' . \esc_attr( \wp_json_encode( $labels ) ) . '" ' . $should_modal_trigger . '>';
$link .= '<span class="wp-block-newspack-my-account-button__icon" aria-hidden="true">';
$link .= Newspack_UI_Icons::get_svg( 'account' );
$link .= '</span>';
$link .= '<span class="newspack-reader__account-link__label">' . \esc_html( $label ) . '</span>';
if ( $show_icon ) {
$link .= '<span class="wp-block-newspack-my-account-button__icon" aria-hidden="true">';
$link .= Newspack_UI_Icons::get_svg( 'account' );
$link .= '</span>';
}
$label_classes = [ 'newspack-reader__account-link__label' ];
if ( ! $show_label ) {
$label_classes[] = 'screen-reader-text';
}
$link .= '<span class="' . \esc_attr( implode( ' ', $label_classes ) ) . '">' . \esc_html( $label ) . '</span>';
$link .= '</a>';
$link .= '</div>';
$link .= '</div>';
Expand Down
37 changes: 32 additions & 5 deletions src/blocks/my-account-button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { useState } from '@wordpress/element';
import {
BlockControls,
InspectorControls,
RichText,
useBlockProps,
/* eslint-disable @wordpress/no-unsafe-wp-apis */
Expand All @@ -22,16 +23,18 @@ import {
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
/* eslint-enable @wordpress/no-unsafe-wp-apis */
} from '@wordpress/block-editor';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { PanelBody, ToggleControl, ToolbarButton, ToolbarGroup } from '@wordpress/components';

/**
* Internal dependencies
*/
function MyAccountButtonEdit( { attributes, setAttributes } ) {
const { signedInLabel, signedOutLabel, style, className: customClassName } = attributes;
const { signedInLabel, signedOutLabel, showLabel, showIcon, style, className: customClassName } = attributes;
const borderProps = useBorderProps( attributes );
const colorProps = useColorProps( attributes );
const spacingProps = useSpacingProps( attributes );
const isLabelVisible = showLabel !== false;
const isIconVisible = showIcon !== false;
const blockProps = useBlockProps( {
className: classnames(
'wp-block-button__link',
Expand All @@ -57,6 +60,21 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
const isSignedOutPreview = previewState === 'signedout';
const activeLabel = isSignedOutPreview ? signedOutLabel : signedInLabel;
const placeholderText = isSignedOutPreview ? __( 'Sign in', 'newspack-plugin' ) : __( 'My Account', 'newspack-plugin' );
function setShowLabel( nextValue ) {
if ( ! nextValue && ! isIconVisible ) {
setAttributes( { showLabel: false, showIcon: true } );
return;
}
setAttributes( { showLabel: nextValue } );
}

function setShowIcon( nextValue ) {
if ( ! nextValue && ! isLabelVisible ) {
setAttributes( { showLabel: true, showIcon: false } );
return;
}
setAttributes( { showIcon: nextValue } );
}

function setButtonText( newText ) {
const cleaned = stripHTML( newText );
Expand All @@ -67,6 +85,12 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
<div { ...blockProps } style={ { ...blockProps.style, display: 'none' } } />
) : (
<>
<InspectorControls>
<PanelBody title={ __( 'Display', 'newspack-plugin' ) }>
<ToggleControl label={ __( 'Show label', 'newspack-plugin' ) } checked={ isLabelVisible } onChange={ setShowLabel } />
<ToggleControl label={ __( 'Show icon', 'newspack-plugin' ) } checked={ isIconVisible } onChange={ setShowIcon } />
</PanelBody>
</InspectorControls>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
Expand All @@ -88,11 +112,14 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
<div className={ classnames( 'wp-block-buttons', customClassName ) }>
<div className="wp-block-button">
<div { ...blockProps }>
<span className="wp-block-newspack-my-account-button__icon" aria-hidden="true">
{ icon }
</span>
{ isIconVisible && (
<span className="wp-block-newspack-my-account-button__icon" aria-hidden="true">
{ icon }
</span>
) }
<RichText
tagName="span"
className={ ! isLabelVisible ? 'screen-reader-text' : undefined }
aria-label={ __( 'Button text', 'newspack-plugin' ) }
placeholder={ placeholderText }
value={ activeLabel || '' }
Expand Down
5 changes: 5 additions & 0 deletions src/reader-activation-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ window.newspackRAS.push( readerActivation => {
const labels = JSON.parse( link.getAttribute( 'data-labels' ) );
const labelEl = link.querySelector( '.newspack-reader__account-link__label' );
if ( labelEl ) {
const isLoggedIn = link.getAttribute( 'data-wp-logged-in' ) === '1';
if ( isLoggedIn ) {
labelEl.textContent = labels.signedin;
return;
}
labelEl.textContent = reader?.authenticated ? labels.signedin : labels.signedout;

// Set my account link href if the reader is authenticated.
Expand Down
17 changes: 13 additions & 4 deletions tests/unit-tests/class-test-my-account-button-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,24 @@ public function test_render_block_signed_in() {
}

/**
* Test empty labels -- render nothing if a button's state has an empty label.
* Test empty signed-out label falls back to default.
*/
public function test_render_block_empty_label() {
public function test_render_block_empty_signed_out_label() {
self::$reader_activation_enabled = true;

$signed_out_output = do_blocks(
'<!-- wp:newspack/my-account-button {"signedInLabel":"My Account","signedOutLabel":""} /-->'
);

$this->assertSame( '', trim( $signed_out_output ) );
$this->assertNotEmpty( $signed_out_output );
$this->assertStringContainsString( '&quot;signedout&quot;:&quot;Sign in&quot;', $signed_out_output );
}

/**
* Test empty signed-in label falls back to default.
*/
public function test_render_block_empty_signed_in_label() {
self::$reader_activation_enabled = true;

$user_id = self::factory()->user->create(
[
Expand All @@ -118,7 +126,8 @@ public function test_render_block_empty_label() {
'<!-- wp:newspack/my-account-button {"signedInLabel":"","signedOutLabel":"Sign in"} /-->'
);

$this->assertSame( '', trim( $signed_in_output ) );
$this->assertNotEmpty( $signed_in_output );
$this->assertStringContainsString( '&quot;signedin&quot;:&quot;My Account&quot;', $signed_in_output );
}

/**
Expand Down
Loading