Skip to content

Commit

Permalink
Blocks: upgrade to API version 3 - Part 1 (#36701)
Browse files Browse the repository at this point in the history
* Bump Blog Stats block API version

* Bump Premium Content Button API version

* Bump Recurring Payments block API version

* Bump Send A Message block API version

* Bump Word Ads block API version

* changelog

* Fix tests

* Fix tests

* Update snapshot
  • Loading branch information
monsieur-z authored Apr 4, 2024
1 parent a7c6d7d commit 73f4817
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Update blocks to use API version 3
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 3,
"name": "jetpack/blog-stats",
"title": "Blog Stats",
"description": "Show a stats counter for your blog.",
Expand Down
7 changes: 4 additions & 3 deletions projects/plugins/jetpack/extensions/blocks/blog-stats/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { numberFormat } from '@automattic/jetpack-components';
import { useModuleStatus } from '@automattic/jetpack-shared-extension-utils';
import apiFetch from '@wordpress/api-fetch';
import { InspectorControls, RichText } from '@wordpress/block-editor';
import { InspectorControls, RichText, useBlockProps } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';
import { __, _n } from '@wordpress/i18n';
Expand All @@ -15,6 +15,7 @@ function BlogStatsEdit( { attributes, className, setAttributes } ) {
const [ blogViews, setBlogViews ] = useState( null );
const [ blogVisitors, setBlogVisitors ] = useState();
const [ postViews, setPostViews ] = useState();
const blockProps = useBlockProps();

const blogStats = statsData === 'views' ? blogViews : blogVisitors;
const stats = statsOption === 'post' ? postViews : blogStats;
Expand Down Expand Up @@ -64,7 +65,7 @@ function BlogStatsEdit( { attributes, className, setAttributes } ) {
_n( 'hit', 'hits', parseInt( stats ), 'jetpack', 0 );

return (
<>
<div { ...blockProps }>
<InspectorControls>
<BlogStatsInspectorControls attributes={ attributes } setAttributes={ setAttributes } />
</InspectorControls>
Expand All @@ -85,7 +86,7 @@ function BlogStatsEdit( { attributes, className, setAttributes } ) {
</p>
) }
</div>
</>
</div>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ useSelect.mockImplementation( () => {
};
} );

jest.mock( '@wordpress/block-editor', () => ( {
...jest.requireActual( '@wordpress/block-editor' ),
useBlockProps: jest.fn(),
} ) );

const defaultAttributes = {
label: '',
statsOption: 'site',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import save from './save';

const name = 'premium-content/buttons';
const settings = {
apiVersion: 2,
apiVersion: 3,
title: __( 'Premium Content buttons', 'jetpack' ),
description: __(
'Prompt Premium Content visitors to take action with a group of button-style links.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"apiVersion": 3,
"name": "jetpack/recurring-payments",
"title": "Payment Button",
"description": "Button allowing you to sell products and subscriptions.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 3,
"name": "jetpack/send-a-message",
"title": "Send A Message",
"description": "Let your visitors send you messages with the tap of a button.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { InnerBlocks } from '@wordpress/block-editor';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

export default function SendAMessageEdit( { className } ) {
export default function SendAMessageEdit() {
const blockProps = useBlockProps();
// Default template is single WhatsApp block until we offer
// more services
const DEFAULT_TEMPLATE = [ [ 'jetpack/whatsapp-button', {} ] ];
const ALLOWED_BLOCKS = [ 'jetpack/whatsapp-button' ];

return (
<div className={ className }>
<div { ...blockProps }>
<InnerBlocks template={ DEFAULT_TEMPLATE } allowedBlocks={ ALLOWED_BLOCKS } />
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { InnerBlocks } from '@wordpress/block-editor';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

export default () => {
const blockProps = useBlockProps.save();

export default props => {
return (
<div className={ props.className }>
<div { ...blockProps }>
<InnerBlocks.Content />
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"apiVersion": 3,
"name": "jetpack/wordads",
"title": "Ad",
"description": "Earn income by adding high quality ads to your post.",
Expand Down
54 changes: 30 additions & 24 deletions projects/plugins/jetpack/extensions/blocks/wordads/edit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ThemeProvider } from '@automattic/jetpack-components';
import { useModuleStatus } from '@automattic/jetpack-shared-extension-utils';
import { useBlockProps } from '@wordpress/block-editor';
import { WordAdsPlaceholder } from './components/jetpack-wordads-placeholder';
import { WordAdsSkeletonLoader } from './components/jetpack-wordads-skeleton-loader';
import { AD_FORMATS } from './constants';
Expand All @@ -12,6 +13,7 @@ import leaderboardExample from './example_728x90.png';
import './editor.scss';

const WordAdsEdit = ( { attributes, setAttributes } ) => {
const blockProps = useBlockProps();
const { format } = attributes;
const { isLoadingModules, isChangingStatus, isModuleActive, changeStatus } =
useModuleStatus( 'wordads' );
Expand All @@ -30,40 +32,44 @@ const WordAdsEdit = ( { attributes, setAttributes } ) => {
}
};

let content;

if ( ! isModuleActive ) {
if ( isLoadingModules ) {
return (
content = (
<ThemeProvider>
<WordAdsSkeletonLoader />
</ThemeProvider>
);
} else {
content = (
<WordAdsPlaceholder
changeStatus={ changeStatus }
isModuleActive={ isModuleActive }
isLoading={ isChangingStatus }
/>
);
}

return (
<WordAdsPlaceholder
changeStatus={ changeStatus }
isModuleActive={ isModuleActive }
isLoading={ isChangingStatus }
/>
} else {
content = (
<>
<AdControls { ...{ attributes, setAttributes } } />
<div className={ `wp-block-jetpack-wordads jetpack-wordads-${ format }` }>
<div
className="jetpack-wordads__ad"
style={ {
width: selectedFormatObject.width,
height: selectedFormatObject.height,
backgroundImage: `url( ${ getExampleAd( format ) } )`,
backgroundSize: 'cover',
} }
></div>
</div>
</>
);
}

return (
<>
<AdControls { ...{ attributes, setAttributes } } />
<div className={ `wp-block-jetpack-wordads jetpack-wordads-${ format }` }>
<div
className="jetpack-wordads__ad"
style={ {
width: selectedFormatObject.width,
height: selectedFormatObject.height,
backgroundImage: `url( ${ getExampleAd( format ) } )`,
backgroundSize: 'cover',
} }
></div>
</div>
</>
);
return <div { ...blockProps }>{ content }</div>;
};

export default WordAdsEdit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`WordAdsEdit matches snapshot: WordAdsEdit 1`] = `
<div>
<div
aria-label="Block: undefined"
class="block-editor-block-list__block wp-block"
id="block-undefined"
role="document"
tabindex="0"
>
<div
class="wp-block-jetpack-wordads jetpack-wordads-mrec"
>
<div
class="jetpack-wordads__ad"
style="width: 300px; height: 250px; background-size: cover;"
/>
</div>
</div>
</div>
`;
12 changes: 4 additions & 8 deletions projects/plugins/jetpack/extensions/blocks/wordads/test/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ describe( 'WordAdsEdit', () => {
const renderWordAdsEdit = props => {
const { container } = render( <WordAdsEdit { ...props } /> );

// eslint-disable-next-line testing-library/no-node-access
return container.firstChild.firstChild;
// eslint-disable-next-line testing-library/no-node-access,testing-library/no-container
return container.querySelector( '.jetpack-wordads__ad' );
};

// Renders the component, extracting the inner placeholder element and finding
Expand All @@ -47,21 +47,17 @@ describe( 'WordAdsEdit', () => {
return { placeholder, selectedFormat };
};

test( 'renders wrapper with correct css class', () => {
test( 'matches snapshot', () => {
const { container } = render( <WordAdsEdit { ...defaultProps } /> );

// eslint-disable-next-line testing-library/no-node-access
expect( container.firstChild ).toHaveClass( 'wp-block-jetpack-wordads' );
// eslint-disable-next-line testing-library/no-node-access
expect( container.firstChild ).toHaveClass( `jetpack-wordads-${ defaultAttributes.format }` );
expect( container ).toMatchSnapshot( 'WordAdsEdit' );
} );

test( 'renders ad placeholder with correct css class and styles', () => {
// eslint-disable-next-line testing-library/render-result-naming-convention -- False positive.
const placeholder = renderWordAdsEdit( defaultProps );
const selectedFormat = getFormat( defaultAttributes.format );

expect( placeholder ).toHaveClass( 'jetpack-wordads__ad' );
expect( placeholder ).toHaveStyle( `width: ${ selectedFormat.width }px` );
expect( placeholder ).toHaveStyle( `height: ${ selectedFormat.height }px` );
expect( placeholder ).toHaveStyle( `backgroundImage: url( ${ rectangleExample } )` );
Expand Down

0 comments on commit 73f4817

Please sign in to comment.