Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Add Store Notices block #8157

Merged
merged 34 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6c79dad
Block scaffolding
albarin Jan 10, 2023
fe9bb1b
Rename to store notices
albarin Jan 11, 2023
f8b4ad8
Register only in the site editor
albarin Jan 11, 2023
3ae12ad
Update description
albarin Jan 12, 2023
d122afa
Add woocommerce class
albarin Jan 12, 2023
afa7d79
Add tests
albarin Jan 12, 2023
4593bd8
Merge branch 'trunk' into add/8062-notices-block
albarin Jan 12, 2023
a8c2d63
Delete old test file
albarin Jan 13, 2023
cd2a2dc
Update notices message in the editor
albarin Jan 13, 2023
21d1991
Remove customization
albarin Jan 13, 2023
abd1c07
Disable the block server side
albarin Jan 13, 2023
91361e3
Fix TS error
albarin Jan 13, 2023
139c7be
Merge branch 'trunk' into add/8062-notices-block
albarin Jan 16, 2023
4d96178
Add custom classes
albarin Jan 16, 2023
895f90e
Reprashed test description
albarin Jan 16, 2023
ffbd13d
Escape notices
albarin Jan 16, 2023
e2fb244
Remove unnecessary disabled component
albarin Jan 16, 2023
4e58255
Only allow to insert the block once
albarin Jan 16, 2023
842b06c
Improve class name
albarin Jan 16, 2023
5eee3a7
Merge edit and block files
albarin Jan 16, 2023
ee47a82
Fix copy
albarin Jan 16, 2023
3676311
Simplify test case
albarin Jan 16, 2023
107ddc5
Remove align support
albarin Jan 16, 2023
589308a
Remove attributes
albarin Jan 16, 2023
0f1536e
Change editor markup
albarin Jan 16, 2023
3dcecc0
Use the Notice component to display the notice placeholder on the editor
albarin Jan 17, 2023
f6fabc8
Use sprintf to improve readability
albarin Jan 17, 2023
c82c336
Inline component
albarin Jan 17, 2023
dbfd45d
Remove styles and unregister scripts
albarin Jan 17, 2023
da30f61
Merge branch 'trunk' into add/8062-notices-block
albarin Jan 17, 2023
4ee65dd
Use ordered placeholders
albarin Jan 17, 2023
8d581a1
Use info as the notices status
albarin Jan 17, 2023
728f38f
Merge branch 'trunk' into add/8062-notices-block
albarin Jan 17, 2023
9bf720e
Fix php lint error
albarin Jan 18, 2023
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
14 changes: 14 additions & 0 deletions assets/js/blocks/store-notices/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "woocommerce/store-notices",
"version": "1.0.0",
"title": "Store Notices",
"description": "Display shopper-facing notifications generated by WooCommerce or extensions.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"supports": {
"multiple": false
},
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
}
25 changes: 25 additions & 0 deletions assets/js/blocks/store-notices/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { Notice } from '@wordpress/components';

const Edit = (): JSX.Element => {
const blockProps = useBlockProps( {
className: 'wc-block-store-notices',
} );

return (
<div { ...blockProps }>
<Notice status="warning" isDismissible={ false }>
albarin marked this conversation as resolved.
Show resolved Hide resolved
{ __(
'Notices added by WooCommerce or extensions will show up here.',
'woo-gutenberg-products-block'
) }
</Notice>
</div>
);
};

export default Edit;
30 changes: 30 additions & 0 deletions assets/js/blocks/store-notices/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* External dependencies
*/
import { registerBlockType } from '@wordpress/blocks';
import { Icon } from '@wordpress/icons';
import { totals } from '@woocommerce/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';

registerBlockType( metadata, {
icon: {
src: (
<Icon
icon={ totals }
className="wc-block-editor-components-block-icon"
/>
),
},
attributes: {
...metadata.attributes,
},
edit,
save() {
return null;
},
} );
1 change: 1 addition & 0 deletions bin/webpack-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const blocks = {
'mini-cart-contents': {
customDir: 'mini-cart/mini-cart-contents',
},
'store-notices': {},
'price-filter': {},
'product-best-sellers': {},
'product-category': {},
Expand Down
54 changes: 54 additions & 0 deletions src/BlockTypes/StoreNotices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Automattic\WooCommerce\Blocks\BlockTypes;

use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;

/**
* StoreNotices class.
*/
class StoreNotices extends AbstractBlock {

/**
* Block name.
*
* @var string
*/
protected $block_name = 'store-notices';

/**
* Render the block.
*
* @param array $attributes Block attributes.
* @param string $content Block content.
* @param WP_Block $block Block instance.
*
* @return string | void Rendered block output.
*/
protected function render( $attributes, $content, $block ) {
ob_start();
woocommerce_output_all_notices();
$notices = ob_get_clean();

if ( ! $notices ) {
return;
}

$classname = isset( $attributes['className'] ) ? $attributes['className'] : '';

return sprintf(
'<div class="woocommerce wc-block-store-notices %s">%s</div>',
albarin marked this conversation as resolved.
Show resolved Hide resolved
esc_attr( $classname ),
wc_kses_notice( $notices )
);
}

/**
* Get the frontend script handle for this block type.
*
* @param string $key Data to get, or default to everything.
*/
protected function get_block_type_script( $key = null ) {
return null;
}
}
albarin marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions src/BlockTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ protected function get_block_types() {
'HandpickedProducts',
'MiniCart',
'MiniCartContents',
'StoreNotices',
'PriceFilter',
'ProductAddToCart',
'ProductBestSellers',
Expand Down Expand Up @@ -247,6 +248,7 @@ protected function get_block_types() {
$block_types,
[
'ClassicTemplate',
'StoreNotices',
]
);
}
Expand Down
62 changes: 62 additions & 0 deletions tests/e2e/specs/backend/store-notices.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* External dependencies
*/
import {
canvas,
createNewPost,
insertBlock,
switchUserToAdmin,
} from '@wordpress/e2e-test-utils';
import { searchForBlock } from '@wordpress/e2e-test-utils/build/inserter';

/**
* Internal dependencies
*/
import { goToSiteEditor, useTheme, waitForCanvas } from '../../utils.js';

const block = {
name: 'Store Notices',
slug: 'woocommerce/store-notices',
class: '.wc-block-store-notices',
selectors: {
insertButton: "//button//span[text()='Store Notices']",
insertButtonDisabled:
"//button[@aria-disabled]//span[text()='Store Notices']",
},
};

describe( `${ block.name } Block`, () => {
it( 'can not be inserted in the Post Editor', async () => {
await switchUserToAdmin();

await createNewPost( {
postType: 'post',
title: block.name,
} );
await searchForBlock( block.name );
expect( page ).toMatch( 'No results found.' );
} );

describe( 'in FSE editor', () => {
useTheme( 'emptytheme' );

beforeEach( async () => {
await goToSiteEditor();
await waitForCanvas();
} );

it( 'can be inserted in FSE area', async () => {
await insertBlock( block.name );
await expect( canvas() ).toMatchElement( block.class );
} );

it( 'can only be inserted once', async () => {
await insertBlock( block.name );
await searchForBlock( block.name );
const storeNoticesButton = await page.$x(
block.selectors.insertButtonDisabled
);
expect( storeNoticesButton ).toHaveLength( 1 );
} );
} );
} );