-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add loginout block * Update packages/block-library/src/loginout/index.php Co-authored-by: Timothy Jacobs <[email protected]> * Add wrapper with the right classes * Add extra class if login form * rename edit function * fix redirects * Remove empty example * no need for the block to be dynamic * change icon * camelCase SVG props * remove unused props * Update packages/block-library/src/loginout/index.js Co-authored-by: Timothy Jacobs <[email protected]> Co-authored-by: Nik Tsekouras <[email protected]>
- Loading branch information
1 parent
50cdfbe
commit 8312663
Showing
12 changed files
with
185 additions
and
0 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
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,19 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "core/loginout", | ||
"category": "design", | ||
"attributes": { | ||
"displayLoginAsForm": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"redirectToCurrent": { | ||
"type": "boolean", | ||
"default": true | ||
} | ||
}, | ||
"supports": { | ||
"className": true, | ||
"fontSize": false | ||
} | ||
} |
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,44 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { PanelBody, ToggleControl } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
export default function LoginOutEdit( { attributes, setAttributes } ) { | ||
const { displayLoginAsForm, redirectToCurrent } = attributes; | ||
|
||
return ( | ||
<> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Login/out settings' ) }> | ||
<ToggleControl | ||
label={ __( 'Display login as form' ) } | ||
checked={ displayLoginAsForm } | ||
onChange={ () => | ||
setAttributes( { | ||
displayLoginAsForm: ! displayLoginAsForm, | ||
} ) | ||
} | ||
/> | ||
<ToggleControl | ||
label={ __( 'Redirect to current URL' ) } | ||
checked={ redirectToCurrent } | ||
onChange={ () => | ||
setAttributes( { | ||
redirectToCurrent: ! redirectToCurrent, | ||
} ) | ||
} | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
<div | ||
{ ...useBlockProps( { | ||
className: 'logged-in', | ||
} ) } | ||
> | ||
<a href="#login-pseudo-link">{ __( 'Log out' ) }</a> | ||
</div> | ||
</> | ||
); | ||
} |
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,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, _x } from '@wordpress/i18n'; | ||
import { login as icon } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import edit from './edit'; | ||
import metadata from './block.json'; | ||
|
||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: _x( 'Login/out', 'block title' ), | ||
description: __( 'Show login & logout links.' ), | ||
icon, | ||
keywords: [ __( 'login' ), __( 'logout' ), __( 'form' ) ], | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/loginout` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/loginout` block on server. | ||
* | ||
* @param array $attributes The block attributes. | ||
* | ||
* @return string Returns the login-out link or form. | ||
*/ | ||
function render_block_core_loginout( $attributes ) { | ||
|
||
// Build the redirect URL. | ||
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | ||
|
||
$classes = is_user_logged_in() ? 'logged-in' : 'logged-out'; | ||
$contents = wp_loginout( | ||
isset( $attributes['redirectToCurrent'] ) && $attributes['redirectToCurrent'] ? $current_url : '', | ||
false | ||
); | ||
|
||
// If logged-out and displayLoginAsForm is true, show the login form. | ||
if ( ! is_user_logged_in() && ! empty( $attributes['displayLoginAsForm'] ) ) { | ||
// Add a class. | ||
$classes .= ' has-login-form'; | ||
|
||
// Get the form. | ||
$contents = wp_login_form( array( 'echo' => false ) ); | ||
} | ||
|
||
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); | ||
|
||
return '<div ' . $wrapper_attributes . '>' . $contents . '</div>'; | ||
} | ||
|
||
/** | ||
* Registers the `core/latest-posts` block on server. | ||
*/ | ||
function register_block_core_loginout() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/loginout', | ||
array( | ||
'render_callback' => 'render_block_core_loginout', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_loginout' ); |
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 @@ | ||
<!-- wp:loginout /--> |
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,13 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/loginout", | ||
"isValid": true, | ||
"attributes": { | ||
"displayLoginAsForm": false, | ||
"redirectToCurrent": true | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
18 changes: 18 additions & 0 deletions
18
packages/e2e-tests/fixtures/blocks/core__loginout.parsed.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,18 @@ | ||
[ | ||
{ | ||
"blockName": "core/loginout", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/fixtures/blocks/core__loginout.serialized.html
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 @@ | ||
<!-- wp:loginout /--> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { SVG, Path } from '@wordpress/primitives'; | ||
|
||
const login = ( | ||
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
<Path d="M11 14.5l1.1 1.1 3-3 .5-.5-.6-.6-3-3-1 1 1.7 1.7H5v1.5h7.7L11 14.5zM16.8 5h-7c-1.1 0-2 .9-2 2v1.5h1.5V7c0-.3.2-.5.5-.5h7c.3 0 .5.2.5.5v10c0 .3-.2.5-.5.5h-7c-.3 0-.5-.2-.5-.5v-1.5H7.8V17c0 1.1.9 2 2 2h7c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2z" /> | ||
</SVG> | ||
); | ||
|
||
export default login; |