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

Add loginout block #29766

Merged
merged 15 commits into from
Mar 18, 2021
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function gutenberg_reregister_core_block_types() {
'cover.php' => 'core/cover',
'latest-comments.php' => 'core/latest-comments',
'latest-posts.php' => 'core/latest-posts',
'loginout.php' => 'core/loginout',
aristath marked this conversation as resolved.
Show resolved Hide resolved
'navigation.php' => 'core/navigation',
'navigation-link.php' => 'core/navigation-link',
'rss.php' => 'core/rss',
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as navigation from './navigation';
import * as navigationLink from './navigation-link';
import * as latestComments from './latest-comments';
import * as latestPosts from './latest-posts';
import * as loginOut from './loginout';
aristath marked this conversation as resolved.
Show resolved Hide resolved
import * as list from './list';
import * as missing from './missing';
import * as more from './more';
Expand Down Expand Up @@ -227,6 +228,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
queryPaginationNext,
queryPaginationNumbers,
queryPaginationPrevious,
loginOut,
postTitle,
postContent,
postAuthor,
Expand Down
19 changes: 19 additions & 0 deletions packages/block-library/src/loginout/block.json
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
}
}
46 changes: 46 additions & 0 deletions packages/block-library/src/loginout/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import { PanelBody, ToggleControl, Disabled } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import ServerSideRender from '@wordpress/server-side-render';

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() }>
<Disabled>
<ServerSideRender
aristath marked this conversation as resolved.
Show resolved Hide resolved
block="core/loginout"
attributes={ attributes }
/>
</Disabled>
</div>
</>
);
}
23 changes: 23 additions & 0 deletions packages/block-library/src/loginout/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { postList as icon } from '@wordpress/icons';
aristath marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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.' ),
aristath marked this conversation as resolved.
Show resolved Hide resolved
icon,
keywords: [ __( 'login logout form' ) ],
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
example: {},
aristath marked this conversation as resolved.
Show resolved Hide resolved
edit,
};
55 changes: 55 additions & 0 deletions packages/block-library/src/loginout/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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.
$redirect_url = isset( $attributes['redirectToCurrent'] ) && $attributes['redirectToCurrent']
? get_permalink( get_the_ID() )
: '';

$classes = is_user_logged_in() ? 'logged-in' : 'logged-out';
$contents = wp_loginout( $redirect_url, false );

// If logged-out and displayLoginAsForm is true, show the login form.
if ( ! is_user_logged_in() && ! empty( $attributes['displayLoginAsForm'] ) ) {
aristath marked this conversation as resolved.
Show resolved Hide resolved
// Add a class.
$classes .= ' has-login-form';

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

$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' );
1 change: 1 addition & 0 deletions packages/e2e-tests/fixtures/blocks/core__loginout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:loginout /-->
13 changes: 13 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__loginout.json
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 packages/e2e-tests/fixtures/blocks/core__loginout.parsed.json
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"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:loginout /-->