From 8312663ebdc20ab8832dc8c49eeda685db4791c8 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 18 Mar 2021 11:16:33 +0200 Subject: [PATCH] Add loginout block (#29766) * Add loginout block * Update packages/block-library/src/loginout/index.php Co-authored-by: Timothy Jacobs * 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 Co-authored-by: Nik Tsekouras --- lib/blocks.php | 1 + packages/block-library/src/index.js | 2 + .../block-library/src/loginout/block.json | 19 +++++++ packages/block-library/src/loginout/edit.js | 44 ++++++++++++++++ packages/block-library/src/loginout/index.js | 22 ++++++++ packages/block-library/src/loginout/index.php | 51 +++++++++++++++++++ .../fixtures/blocks/core__loginout.html | 1 + .../fixtures/blocks/core__loginout.json | 13 +++++ .../blocks/core__loginout.parsed.json | 18 +++++++ .../blocks/core__loginout.serialized.html | 1 + packages/icons/src/index.js | 1 + packages/icons/src/library/login.js | 12 +++++ 12 files changed, 185 insertions(+) create mode 100644 packages/block-library/src/loginout/block.json create mode 100644 packages/block-library/src/loginout/edit.js create mode 100644 packages/block-library/src/loginout/index.js create mode 100644 packages/block-library/src/loginout/index.php create mode 100644 packages/e2e-tests/fixtures/blocks/core__loginout.html create mode 100644 packages/e2e-tests/fixtures/blocks/core__loginout.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__loginout.parsed.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__loginout.serialized.html create mode 100644 packages/icons/src/library/login.js diff --git a/lib/blocks.php b/lib/blocks.php index 70f9ec44cb32e1..ac285855625a9c 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -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', 'navigation.php' => 'core/navigation', 'navigation-link.php' => 'core/navigation-link', 'rss.php' => 'core/rss', diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 8235b566311548..80bc845b9363e1 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -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'; import * as list from './list'; import * as missing from './missing'; import * as more from './more'; @@ -227,6 +228,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = queryPaginationNext, queryPaginationNumbers, queryPaginationPrevious, + loginOut, postTitle, postContent, postAuthor, diff --git a/packages/block-library/src/loginout/block.json b/packages/block-library/src/loginout/block.json new file mode 100644 index 00000000000000..5c8240c0d7ca8d --- /dev/null +++ b/packages/block-library/src/loginout/block.json @@ -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 + } +} diff --git a/packages/block-library/src/loginout/edit.js b/packages/block-library/src/loginout/edit.js new file mode 100644 index 00000000000000..70e90bf84e903a --- /dev/null +++ b/packages/block-library/src/loginout/edit.js @@ -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 ( + <> + + + + setAttributes( { + displayLoginAsForm: ! displayLoginAsForm, + } ) + } + /> + + setAttributes( { + redirectToCurrent: ! redirectToCurrent, + } ) + } + /> + + +
+ { __( 'Log out' ) } +
+ + ); +} diff --git a/packages/block-library/src/loginout/index.js b/packages/block-library/src/loginout/index.js new file mode 100644 index 00000000000000..3f5c6625e0dc76 --- /dev/null +++ b/packages/block-library/src/loginout/index.js @@ -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, +}; diff --git a/packages/block-library/src/loginout/index.php b/packages/block-library/src/loginout/index.php new file mode 100644 index 00000000000000..3f4131ac1e572b --- /dev/null +++ b/packages/block-library/src/loginout/index.php @@ -0,0 +1,51 @@ + false ) ); + } + + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); + + return '
' . $contents . '
'; +} + +/** + * 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' ); diff --git a/packages/e2e-tests/fixtures/blocks/core__loginout.html b/packages/e2e-tests/fixtures/blocks/core__loginout.html new file mode 100644 index 00000000000000..dab3964df07c55 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__loginout.html @@ -0,0 +1 @@ + diff --git a/packages/e2e-tests/fixtures/blocks/core__loginout.json b/packages/e2e-tests/fixtures/blocks/core__loginout.json new file mode 100644 index 00000000000000..67eaab753bff39 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__loginout.json @@ -0,0 +1,13 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/loginout", + "isValid": true, + "attributes": { + "displayLoginAsForm": false, + "redirectToCurrent": true + }, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__loginout.parsed.json b/packages/e2e-tests/fixtures/blocks/core__loginout.parsed.json new file mode 100644 index 00000000000000..96c310c7e14561 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__loginout.parsed.json @@ -0,0 +1,18 @@ +[ + { + "blockName": "core/loginout", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + }, + { + "blockName": null, + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n", + "innerContent": [ + "\n" + ] + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__loginout.serialized.html b/packages/e2e-tests/fixtures/blocks/core__loginout.serialized.html new file mode 100644 index 00000000000000..dab3964df07c55 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__loginout.serialized.html @@ -0,0 +1 @@ + diff --git a/packages/icons/src/index.js b/packages/icons/src/index.js index c90d743bb76d3b..0e927cad850288 100644 --- a/packages/icons/src/index.js +++ b/packages/icons/src/index.js @@ -108,6 +108,7 @@ export { default as linkOff } from './library/link-off'; export { default as list } from './library/list'; export { default as listView } from './library/list-view'; export { default as lock } from './library/lock'; +export { default as login } from './library/login'; export { default as loop } from './library/loop'; export { default as mapMarker } from './library/map-marker'; export { default as media } from './library/media'; diff --git a/packages/icons/src/library/login.js b/packages/icons/src/library/login.js new file mode 100644 index 00000000000000..9f529a0229fa4f --- /dev/null +++ b/packages/icons/src/library/login.js @@ -0,0 +1,12 @@ +/** + * WordPress dependencies + */ +import { SVG, Path } from '@wordpress/primitives'; + +const login = ( + + + +); + +export default login;