Skip to content

Commit

Permalink
Add premium content button block for login and subscribe buttons (#42715
Browse files Browse the repository at this point in the history
)
  • Loading branch information
eoigal authored Jun 12, 2020
1 parent f057011 commit 0797cbc
Show file tree
Hide file tree
Showing 20 changed files with 943 additions and 520 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* WordPress dependencies
*/
// eslint-disable-next-line wpcalypso/import-docblock
import {
__experimentalAlignmentHookSettingsProvider as AlignmentHookSettingsProvider,
InnerBlocks,
__experimentalBlock as Block,
} from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

const ALLOWED_BLOCKS = [
'core/button',
'jetpack/recurring-payments',
'premium-content/login-button',
];

// Inside buttons block alignment options are not supported.
const alignmentHooksSetting = {
isEmbedButton: true,
};

function ButtonsEdit( { context, innerBlocks, setRecurringPaymentsPlan } ) {
const planId = context[ 'premium-content/planId' ];

const template = [
[
'jetpack/recurring-payments',
{
planId,
submitButtonText: __( 'Subscribe', 'full-site-editing' ),
},
],
[ 'premium-content/login-button' ],
];

useEffect( () => {
if ( planId ) {
// Updates the plan on any Recurring Payment inner block.
setRecurringPaymentsPlan( planId );
}
}, [ planId, innerBlocks ] );

return (
// eslint-disable-next-line wpcalypso/jsx-classname-namespace
<Block.div className="wp-block-buttons">
<AlignmentHookSettingsProvider value={ alignmentHooksSetting }>
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
template={ template }
__experimentalMoverDirection="horizontal"
/>
</AlignmentHookSettingsProvider>
</Block.div>
);
}

export default compose( [
withSelect( ( select, props ) => ( {
innerBlocks: select( 'core/block-editor' ).getBlock( props.clientId ).innerBlocks,
} ) ),
withDispatch( ( dispatch, props, registry ) => ( {
/**
* Updates the selected plan on the Recurring Payments inner block.
*
* @param planId {int} Recurring Payments plan.
*/
setRecurringPaymentsPlan( planId ) {
const { updateBlockAttributes } = dispatch( 'core/block-editor' );
const { getBlock } = registry.select( 'core/block-editor' );

const updatePlanAttribute = ( block ) => {
if ( block.name === 'jetpack/recurring-payments' ) {
updateBlockAttributes( block.clientId, { planId } );
}

block.innerBlocks.forEach( updatePlanAttribute );
};

const block = getBlock( props.clientId );
updatePlanAttribute( block );
},
} ) ),
] )( ButtonsEdit );
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { button as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import edit from './edit';
import save from './save';

const name = 'premium-content/buttons';
const category = 'design';

const settings = {
name,
category,
title: __( 'Premium Content buttons', 'full-site-editing' ),
description: __(
'Prompt Premium Content visitors to take action with a group of button-style links.',
'full-site-editing'
),
icon,
supports: {
align: true,
alignWide: false,
lightBlockWrapper: true,
},
keywords: [ __( 'link', 'full-site-editing' ) ],
edit,
save,
context: [ 'premium-content/planId' ],
};

export { name, category, settings };
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* WordPress dependencies
*/
// eslint-disable-next-line wpcalypso/import-docblock
import { InnerBlocks } from '@wordpress/block-editor';

export default function save() {
return (
// eslint-disable-next-line wpcalypso/jsx-classname-namespace
<div className="wp-block-buttons">
<InnerBlocks.Content />
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ const settings = {
],
edit,
save,
providesContext: {
'premium-content/planId': 'selectedPlanId',
},
};

export { name, category, settings };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* WordPress dependencies
*/
// eslint-disable-next-line wpcalypso/import-docblock
import { createBlock } from '@wordpress/blocks';
import { InnerBlocks } from '@wordpress/block-editor';

const deprecated = [
{
attributes: {
subscribeButtonText: {
type: 'string',
default: 'Subscribe',
},
loginButtonText: {
type: 'string',
default: 'Log In',
},
buttonClasses: {
type: 'string',
default: '',
},
backgroundButtonColor: {
type: 'string',
default: '',
},
textButtonColor: {
type: 'string',
default: '',
},
customBackgroundButtonColor: {
type: 'string',
default: '',
},
customTextButtonColor: {
type: 'string',
default: '',
},
},

isEligible: ( { buttonClasses } ) => !! buttonClasses,

migrate: ( attributes, innerBlocks ) => {
const buttons = createBlock( 'premium-content/buttons', {}, [
createBlock( 'jetpack/recurring-payments', {
submitButtonText: attributes.subscribeButtonText,
backgroundButtonColor: attributes.backgroundButtonColor,
textButtonColor: attributes.textButtonColor,
customBackgroundButtonColor: attributes.customBackgroundButtonColor,
customTextButtonColor: attributes.customTextButtonColor,
} ),
createBlock( 'premium-content/login-button', {
text: attributes.loginButtonText,
backgroundColor: attributes.backgroundButtonColor,
textColor: attributes.textButtonColor,
style: {
color: {
background: attributes.customBackgroundButtonColor,
text: attributes.customTextButtonColor,
},
},
} ),
] );
return [ attributes, [ ...innerBlocks, buttons ] ];
},

save: () => (
// eslint-disable-next-line wpcalypso/jsx-classname-namespace
<div className="wp-block-premium-content-logged-out-view">
<InnerBlocks.Content />
</div>
),
},
];

export default deprecated;
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,32 @@ import { __ } from '@wordpress/i18n';
import { useEffect } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
import { pick } from 'lodash';

/**
* Internal dependencies
*/
import Context from '../container/context';
import SubmitButtons from './submit-buttons';

/**
* Block edit function
*
* @typedef { import('./').Attributes } Attributes
* @typedef {object} Props
* @typedef { object } Props
* @property { boolean } isSelected
* @property { string } className
* @property { string } clientId
* @property { string } containerClientId
* @property { Attributes } attributes
* @property { (attributes: Partial<Attributes>) => void } setAttributes
* @property { () => void } selectBlock
* @property { Function } setAttributes
* @property { Function } selectBlock
*
* @param { Props } props
* @param { Props } props Properties
*/
function Edit( props ) {
function Edit( { selectBlock } ) {
useEffect( () => {
props.selectBlock();
selectBlock();
}, [] );

const buttons = (
<SubmitButtons
{ ...{
attributes: pick( props.attributes, [
'subscribeButtonText',
'loginButtonText',
'backgroundButtonColor',
'textButtonColor',
'customBackgroundButtonColor',
'customTextButtonColor',
] ),
setAttributes: props.setAttributes,
} }
/>
);

return (
<Context.Consumer>
{ ( { selectedTab, stripeNudge } ) => (
Expand All @@ -73,9 +55,9 @@ function Edit( props ) {
),
},
],
[ 'premium-content/buttons' ],
] }
/>
{ buttons }
</div>
) }
</Context.Consumer>
Expand All @@ -84,19 +66,18 @@ function Edit( props ) {

export default compose( [
withSelect( ( select, props ) => {
const { getBlockHierarchyRootClientId } = select( 'core/block-editor' );
return {
// @ts-ignore difficult to type with JSDoc
containerClientId: select( 'core/block-editor' ).getBlockHierarchyRootClientId(
props.clientId
),
containerClientId: getBlockHierarchyRootClientId( props.clientId ),
};
} ),
withDispatch( ( dispatch, props ) => {
const blockEditor = dispatch( 'core/block-editor' );
const { selectBlock } = dispatch( 'core/block-editor' );
return {
selectBlock() {
// @ts-ignore difficult to type with JSDoc
blockEditor.selectBlock( props.containerClientId );
selectBlock( props.containerClientId );
},
};
} ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import edit from './edit';
import save from './save';
import deprecated from './deprecated';

/**
* WordPress dependencies
Expand All @@ -16,51 +17,14 @@ const name = 'premium-content/logged-out-view';
const category = 'common';
/**
* @typedef {object} Attributes
* @property {string} subscribeButtonText
* @property {string} loginButtonText
* @property {string} buttonClasses
* @property {string} backgroundButtonColor
* @property {string} textButtonColor
* @property {string} customBackgroundButtonColor
* @property {string} customTextButtonColor
*
* @typedef {import('@wordpress/blocks').BlockConfiguration<Attributes>} BlockConfiguration
* @type {BlockConfiguration}
*/
const settings = {
name,
category,
attributes: {
subscribeButtonText: {
type: 'string',
default: 'Subscribe',
},
loginButtonText: {
type: 'string',
default: 'Log In',
},
buttonClasses: {
type: 'string',
default: '',
},
backgroundButtonColor: {
type: 'string',
default: '',
},
textButtonColor: {
type: 'string',
default: '',
},
customBackgroundButtonColor: {
type: 'string',
default: '',
},
customTextButtonColor: {
type: 'string',
default: '',
},
},

attributes: {},
/* translators: block name */
title: __( 'Logged Out View', 'full-site-editing' ),
/* translators: block description */
Expand All @@ -73,6 +37,7 @@ const settings = {
},
edit,
save,
deprecated,
};

/**
Expand Down
Loading

0 comments on commit 0797cbc

Please sign in to comment.