Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login/out Block: Enhance Customization Options for "Display as Form" #68919

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ Show login & logout links. ([Source](https://github.com/WordPress/gutenberg/tree
- **Name:** core/loginout
- **Category:** theme
- **Supports:** className, color (background, gradients, link, ~~text~~), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight)
- **Attributes:** displayLoginAsForm, redirectToCurrent
- **Attributes:** displayLoginAsForm, formData, redirectToCurrent, showLostLink, showRememberMe

## Media & Text

Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/loginout/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
"redirectToCurrent": {
"type": "boolean",
"default": true
},
"formData": {
"type": "object",
"default": {}
},
"showRememberMe": {
"type": "boolean",
"default": true
},
"showLostLink": {
"type": "boolean",
"default": true
}
},
"example": {
Expand Down
130 changes: 127 additions & 3 deletions packages/block-library/src/loginout/edit.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
/**
* WordPress dependencies
*/
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
InspectorControls,
RichText,
useBlockProps,
} from '@wordpress/block-editor';
import {
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';

export default function LoginOutEdit( { attributes, setAttributes } ) {
const { displayLoginAsForm, redirectToCurrent } = attributes;
const { displayLoginAsForm, redirectToCurrent, formData, showRememberMe } =
attributes;
const [ isEditingForm, setIsEditingForm ] = useState( displayLoginAsForm );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

return (
Expand Down Expand Up @@ -49,6 +56,46 @@ export default function LoginOutEdit( { attributes, setAttributes } ) {
}
/>
</ToolsPanelItem>
{ displayLoginAsForm && (
<>
<ToolsPanelItem
label={ __( 'View Login Form' ) }
isShownByDefault
hasValue={ () => ! isEditingForm }
onDeselect={ () => setIsEditingForm( true ) }
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'View Login Form' ) }
checked={ isEditingForm }
onChange={ () =>
setIsEditingForm( ! isEditingForm )
}
/>
</ToolsPanelItem>
<ToolsPanelItem
label={ __( 'Show Remember Field' ) }
isShownByDefault
hasValue={ () => ! showRememberMe }
onDeselect={ () => {
setAttributes( {
showRememberMe: true,
} );
} }
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show Remember Form' ) }
checked={ showRememberMe }
onChange={ () =>
setAttributes( {
showRememberMe: ! showRememberMe,
} )
}
/>
</ToolsPanelItem>
</>
) }
<ToolsPanelItem
label={ __( 'Redirect to current URL' ) }
isShownByDefault
Expand All @@ -75,7 +122,84 @@ export default function LoginOutEdit( { attributes, setAttributes } ) {
className: 'logged-in',
} ) }
>
<a href="#login-pseudo-link">{ __( 'Log out' ) }</a>
{ isEditingForm ? (
<div className="wp-block-loginout-form">
<div className="wp-block-loginout-form__fields">
<RichText
tagName="label"
value={ formData.usernameLabel ?? 'Username' }
onChange={ ( usernameLabel ) =>
setAttributes( {
formData: {
...formData,
usernameLabel,
},
} )
}
/>

<input
type="text"
placeholder="Username"
disabled
/>
</div>
<div className="wp-block-loginout-form__fields">
<RichText
tagName="label"
value={ formData.passwordLabel ?? 'Password' }
onChange={ ( passwordLabel ) =>
setAttributes( {
formData: {
...formData,
passwordLabel,
},
} )
}
/>
<input
type="password"
placeholder="Password"
disabled
/>
</div>
{ /* Remember me checkbox */ }
{ !! showRememberMe && (
<div className="wp-block-loginout-form__fields">
<input type="checkbox" disabled />

<RichText
tagName="label"
value={
formData.rememberLabel ?? 'Remember me'
}
onChange={ ( rememberLabel ) =>
setAttributes( {
formData: {
...formData,
rememberLabel,
},
} )
}
/>
</div>
) }

<div className="wp-block-loginout-form__fields">
<RichText
tagName="button"
value={ formData.submitLabel ?? 'Log in' }
onChange={ ( submitLabel ) =>
setAttributes( {
formData: { ...formData, submitLabel },
} )
}
/>
</div>
</div>
) : (
<a href="#login-pseudo-link">{ __( 'Log out' ) }</a>
) }
</div>
</>
);
Expand Down
22 changes: 21 additions & 1 deletion packages/block-library/src/loginout/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,28 @@ function render_block_core_loginout( $attributes ) {
// Add a class.
$classes .= ' has-login-form';

$args = array(
'echo' => false,
'remember' => $attributes['showRememberMe'],
);

if ( ! empty( $attributes['formData'] ) ) {
if ( ! empty( $attributes['formData']['rememberLabel'] ) ) {
$args['label_remember'] = $attributes['formData']['rememberLabel'];
}
if ( ! empty( $attributes['formData']['passwordLabel'] ) ) {
$args['label_password'] = $attributes['formData']['passwordLabel'];
}
if ( ! empty( $attributes['formData']['usernameLabel'] ) ) {
$args['label_username'] = $attributes['formData']['usernameLabel'];
}
if ( ! empty( $attributes['formData']['submitLabel'] ) ) {
$args['label_log_in'] = $attributes['formData']['submitLabel'];
}
}

// Get the form.
$contents = wp_login_form( array( 'echo' => false ) );
$contents = wp_login_form( $args );
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/loginout/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,18 @@
// This block has customizable padding, border-box makes that more predictable.
box-sizing: border-box;
}

.wp-block-loginout .wp-block-loginout-form {
// This block has customizable padding, border-box makes that more predictable.
display: flex;
flex-direction: column;
justify-content: center;
gap: 20px;
}

.wp-block-loginout-form__fields {
// This block has customizable padding, border-box makes that more predictable.
display: flex;
flex-direction: row;
gap: 5px;
}
Loading