diff --git a/src/blocks/my-account-button/README.md b/src/blocks/my-account-button/README.md index 82b1349410..df303d6b9d 100644 --- a/src/blocks/my-account-button/README.md +++ b/src/blocks/my-account-button/README.md @@ -6,6 +6,11 @@ Provides a reader account/sign-in button for sites using Newspack Reader Activat - Signed in label (`signedInLabel`): Text shown when the reader is authenticated. - Signed out label (`signedOutLabel`): Text shown when the reader is not authenticated. +### Display +The button also has the ability to toggle on/off the display of the icon or text label. You cannot toggle off both the icon and label at the same time. + +When only the icon is visible, screenreaders will still be able to read the label, even if it's not visible. If this hidden label needs to be edited, it will need to be toggled back on in the editor, changed, and then hidden again. + ## Editor behavior - Use the toolbar toggle (Signed in / Signed out) to edit each label. diff --git a/src/blocks/my-account-button/block.json b/src/blocks/my-account-button/block.json index e4d50aafa9..1a0ecac5ac 100644 --- a/src/blocks/my-account-button/block.json +++ b/src/blocks/my-account-button/block.json @@ -20,6 +20,10 @@ "signedOutLabel": { "type": "string", "default": "Sign in" + }, + "className": { + "type": "string", + "default": "" } }, "supports": { diff --git a/src/blocks/my-account-button/class-my-account-button-block.php b/src/blocks/my-account-button/class-my-account-button-block.php index a7a85a4cd7..766fd7a87e 100644 --- a/src/blocks/my-account-button/class-my-account-button-block.php +++ b/src/blocks/my-account-button/class-my-account-button-block.php @@ -93,18 +93,31 @@ public static function render_block( $attrs ) { $default_attrs = [ 'signedInLabel' => __( 'My Account', 'newspack-plugin' ), 'signedOutLabel' => __( 'Sign in', 'newspack-plugin' ), + 'className' => '', ]; $attrs = \wp_parse_args( $attrs, $default_attrs ); - $is_signed_in = \is_user_logged_in(); - $label = $is_signed_in ? $attrs['signedInLabel'] : $attrs['signedOutLabel']; - if ( '' === trim( (string) $label ) ) { - return ''; + $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; + + // Display mode from block style class in className (default = icon + text). + $classes = explode( ' ', (string) $attrs['className'] ); + if ( in_array( 'is-style-icon-only', $classes, true ) ) { + $show_label = false; + $show_icon = true; + } elseif ( in_array( 'is-style-text-only', $classes, true ) ) { + $show_label = true; + $show_icon = false; + } else { + $show_label = true; + $show_icon = true; } $account_url = self::get_account_url(); - /** Do not render link for authenticated readers if account page doesn't exist. */ + // Do not render link for authenticated readers if account page doesn't exist. if ( empty( $account_url ) && \is_user_logged_in() ) { return ''; } @@ -118,8 +131,8 @@ public static function render_block( $attrs ) { } $labels = [ - 'signedin' => $attrs['signedInLabel'], - 'signedout' => $attrs['signedOutLabel'], + 'signedin' => $signed_in_label, + 'signedout' => $signed_out_label, ]; $extra_classes = [ @@ -128,45 +141,50 @@ public static function render_block( $attrs ) { 'newspack-reader__account-link', ]; - /** Get default wrapper attributes to extract custom classes */ + // Get default wrapper attributes to extract custom classes. $default_wrapper_attributes = \get_block_wrapper_attributes(); - /** Extract custom classes (everything except the default block class) */ - $default_block_class = 'wp-block-newspack-my-account-button'; - $custom_classes = []; + // Extract custom classes (everything except the default block class). + $custom_classes = []; - /** Parse class attribute from default wrapper */ + // Parse class attribute from default wrapper. if ( \preg_match( '/class=["\']([^"\']+)["\']/', $default_wrapper_attributes, $matches ) ) { $all_classes = \explode( ' ', $matches[1] ); foreach ( $all_classes as $class ) { $class = \trim( $class ); - /** Only include classes that contain "-size" (e.g., has-small-size) */ + // Only include classes that contain "-size" (e.g., has-small-size). if ( ! empty( $class ) && \strpos( $class, '-size' ) !== false ) { $custom_classes[] = $class; } } } - /** Build wrapper div classes */ - $wrapper_div_classes = [ 'wp-block-buttons' ]; + // Build wrapper div classes. + $wrapper_div_classes = [ 'wp-block-buttons', 'is-layout-flex' ]; if ( ! empty( $custom_classes ) ) { $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-newspack-logged-in' => $is_signed_in ? '1' : '0', + ]; + $wrapper_attributes = \get_block_wrapper_attributes( $wrapper_attribute_args ); $link = '