Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ public static function set_extension_unavailable( $slug, $reason, $details = arr
* Filter 'jetpack_block_editor_enable_upgrade_nudge' with `true` to enable or `false`
* to disable paid feature upgrade nudges in the block editor.
*
* When this is changed to default to `true`, you should also update `modules/memberships/class-jetpack-memberships.php`
* See https://github.com/Automattic/jetpack/pull/13394#pullrequestreview-293063378
*
* @since 7.7.0
*
* @param boolean
Expand Down
3 changes: 3 additions & 0 deletions class.jetpack-plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public static function get() {
if ( in_array( $plan['product_slug'], $personal_plans, true ) ) {
// special support value, not a module but a separate plugin.
$supports[] = 'akismet';
$supports[] = 'recurring-payments';
$plan['class'] = 'personal';
}

Expand All @@ -144,6 +145,7 @@ public static function get() {

if ( in_array( $plan['product_slug'], $premium_plans, true ) ) {
$supports[] = 'akismet';
$supports[] = 'recurring-payments';
$supports[] = 'simple-payments';
$supports[] = 'vaultpress';
$supports[] = 'videopress';
Expand All @@ -165,6 +167,7 @@ public static function get() {

if ( in_array( $plan['product_slug'], $business_plans, true ) ) {
$supports[] = 'akismet';
$supports[] = 'recurring-payments';
$supports[] = 'simple-payments';
$supports[] = 'vaultpress';
$supports[] = 'videopress';
Expand Down
82 changes: 24 additions & 58 deletions extensions/blocks/recurring-payments/edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { trimEnd } from 'lodash';
import formatCurrency, { getCurrencyDefaults } from '@automattic/format-currency';
import { addQueryArgs, getQueryArg, isURL } from '@wordpress/url';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';

import { withSelect } from '@wordpress/data';
import {
Button,
ExternalLink,
Expand All @@ -27,6 +26,8 @@ import { Fragment, Component } from '@wordpress/element';
/**
* Internal dependencies
*/
import getJetpackExtensionAvailability from '../../shared/get-jetpack-extension-availability';
import StripeNudge from '../../shared/components/stripe-nudge';
import { icon, SUPPORTED_CURRENCY_LIST } from '.';

const API_STATE_LOADING = 0;
Expand Down Expand Up @@ -56,6 +57,11 @@ class MembershipsButtonEdit extends Component {
editedProductRenewInterval: '1 month',
};
this.timeout = null;

const recurringPaymentsAvailability = getJetpackExtensionAvailability( 'recurring-payments' );
this.hasUpgradeNudge =
! recurringPaymentsAvailability.available &&
recurringPaymentsAvailability.unavailableReason === 'missing_plan';
Comment thread
sirreal marked this conversation as resolved.
Resolved
}

componentDidMount = () => {
Expand Down Expand Up @@ -344,16 +350,12 @@ class MembershipsButtonEdit extends Component {
}

render = () => {
const { attributes, className, notices, postId } = this.props;
const { attributes, className, notices } = this.props;
const { connected, products } = this.state;
const { align } = attributes;

const stripeConnectUrl = this.getConnectUrl();

// If we know the postId, assume we'll return to the editor. Navigate the top window.
// Otherwise, open a new window.
const stripeConnectTarget = postId ? '_top' : '_blank';

const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Product', 'jetpack' ) }>
Expand Down Expand Up @@ -393,7 +395,12 @@ class MembershipsButtonEdit extends Component {
return (
<Fragment>
{ this.props.noticeUI }
{ this.state.shouldUpgrade && (
{ ! this.hasUpgradeNudge &&
! this.state.shouldUpgrade &&
connected === API_STATE_NOTCONNECTED && (
<StripeNudge blockName="recurring-payments" stripeConnectUrl={ stripeConnectUrl } />
) }
{ ! this.hasUpgradeNudge && this.state.shouldUpgrade && (
<div className="wp-block-jetpack-recurring-payments">
<Placeholder
icon={ <BlockIcon icon={ icon } /> }
Expand Down Expand Up @@ -422,42 +429,6 @@ class MembershipsButtonEdit extends Component {
<Spinner />
</Placeholder>
) }
{ ! this.state.shouldUpgrade &&
! this.props.attributes.planId &&
connected === API_STATE_NOTCONNECTED && (
<div className="wp-block-jetpack-recurring-payments">
<Placeholder
icon={ <BlockIcon icon={ icon } /> }
label={ __( 'Recurring Payments', 'jetpack' ) }
notices={ notices }
>
<div className="components-placeholder__instructions">
<p>
{ __(
'In order to start selling Recurring Payments plans, you have to connect to Stripe:',
'jetpack'
) }
</p>
<Button
isDefault
isLarge
disabled={ ! stripeConnectUrl }
href={ stripeConnectUrl }
target={ stripeConnectTarget }
rel={ stripeConnectTarget === '_blank' ? 'noopener noreferrer' : undefined }
onClick={ this.props.autosaveAndNavigateToConnection }
>
{ __( 'Connect to Stripe or set up an account', 'jetpack' ) }
</Button>
<br />
<Button isLink onClick={ this.apiCall }>
{ __( 'Re-check Connection', 'jetpack' ) }
</Button>
{ this.renderDisclaimer() }
</div>
</Placeholder>
</div>
) }
{ ! this.state.shouldUpgrade &&
! this.props.attributes.planId &&
connected === API_STATE_CONNECTED &&
Expand Down Expand Up @@ -498,26 +469,21 @@ class MembershipsButtonEdit extends Component {
</div>
) }
{ this.state.products && inspectorControls }
{ this.props.attributes.planId && blockContent }
{ ( ( ( this.hasUpgradeNudge || ! this.state.shouldUpgrade ) &&
connected !== API_STATE_LOADING ) ||
this.props.attributes.planId ) &&
blockContent }
{ this.hasUpgradeNudge && connected === API_STATE_NOTCONNECTED && (
<div className="wp-block-jetpack-recurring-payments disclaimer-only">
{ this.renderDisclaimer() }
</div>
) }
</Fragment>
);
};
}

export default compose( [
withSelect( select => ( { postId: select( 'core/editor' ).getCurrentPostId() } ) ),
withDispatch( dispatch => ( {
autosaveAndNavigateToConnection: async event => {
const { href, target } = event.target;

// Special handling if we're opening in _this_ window. Otherwise, just let the navigation happen.
if ( href && target !== '_blank' ) {
event.preventDefault();
await dispatch( 'core/editor' ).autosave();
// Using window.top to escape from the editor iframe on WordPress.com
window.top.location.href = href;
}
},
} ) ),
withNotices,
] )( MembershipsButtonEdit );
23 changes: 19 additions & 4 deletions extensions/blocks/recurring-payments/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import './view.scss';
@import '../../shared/styles/gutenberg-colors.scss';
@import '../../shared/styles/gutenberg-variables.scss';

.wp-block-jetpack-recurring-payments {
Expand Down Expand Up @@ -39,7 +40,6 @@
}

.membership-button {

&__add-amount {
margin-right: 4px;
}
Expand All @@ -56,7 +56,7 @@
&__field-button {
margin-right: 4px;
}

&__field-currency {
width: 30%;
}
Expand All @@ -77,8 +77,23 @@
}
}

&.disclaimer-only {
box-sizing: content-box;
font-size: 13px;
margin: 0 -14px;
padding: 14px;
text-align: center;
transform: translateY( 14px );

// Use opacity to work in various editor styles.
background: $dark-opacity-light-200;

.is-dark-theme & {
background: $light-opacity-light-200;
}
}

.wp-block-jetpack-membership-button_notification {
display: block;
}

}
}
8 changes: 1 addition & 7 deletions extensions/blocks/recurring-payments/recurring-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,5 @@

if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) {
require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';

jetpack_register_block(
'jetpack/recurring-payments',
array(
'render_callback' => array( Jetpack_Memberships::get_instance(), 'render_button' ),
)
);
Jetpack_Memberships::get_instance()->register_gutenberg_block();
}
39 changes: 39 additions & 0 deletions extensions/shared/components/stripe-nudge/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* External dependencies
*/
import GridiconStar from 'gridicons/dist/star';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import analytics from '../../../../_inc/client/lib/analytics';
import BlockNudge from '../block-nudge';

import './style.scss';

export default ( { stripeConnectUrl } ) => (
<BlockNudge
buttonLabel={ __( 'Connect', 'jetpack' ) }
icon={
<GridiconStar
className="jetpack-stripe-nudge__icon"
size={ 18 }
aria-hidden="true"
role="img"
focusable="false"
/>
}
href={ stripeConnectUrl }
onClick={ blockName =>
void analytics.tracks.recordEvent( 'jetpack_editor_block_stripe_connect_click', {
block: blockName,
} )
}
title={ __( 'Connect to Stripe to use this block on your site', 'jetpack' ) }
subtitle={ __(
'This block will be hidden from your visitors until you connect to Stripe.',
'jetpack'
) }
/>
);
13 changes: 13 additions & 0 deletions extensions/shared/components/stripe-nudge/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@import '../../styles/gutenberg-colors.scss';

.jetpack-stripe-nudge__icon {
align-self: center;
background: var( --color-primary );
border-radius: 50%;
box-sizing: content-box;
color: $white;
fill: $white;
flex-shrink: 0;
margin-right: 16px;
padding: 6px;
}
2 changes: 1 addition & 1 deletion extensions/shared/components/upgrade-nudge/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const UpgradeNudge = ( { planName, trackEvent, upgradeUrl } ) => (
: __( 'Upgrade to a paid plan to use this block on your site.', 'jetpack' )
}
subtitle={ __(
'You can try it out before upgrading, but only you will see it. It will be hidden from visitors until you upgrade.',
'You can try it out before upgrading, but only you will see it. It will be hidden from your visitors until you upgrade.',
'jetpack'
) }
/>
Expand Down
70 changes: 70 additions & 0 deletions modules/memberships/class-jetpack-memberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ class Jetpack_Memberships {
* @var array
*/
private static $tags_allowed_in_the_button = array( 'br' => array() );

/**
* The minimum required plan for this Gutenberg block.
*
* @var string Plan slug
*/
private static $required_plan;

/**
* Track recurring payments block registration.
*
* @var boolean True if block registration has been executed.
*/
private static $has_registered_block = false;

/**
* Classic singleton pattern
*
Expand All @@ -63,6 +78,8 @@ public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
self::$instance->register_init_hook();
// Yes, `personal-bundle` with a dash, `jetpack_personal` with an underscore. Check the v1.5 endpoint to verify.
self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'personal-bundle' : 'jetpack_personal';
}

return self::$instance;
Expand Down Expand Up @@ -91,6 +108,7 @@ private static function get_plan_property_mapping() {
*/
private function register_init_hook() {
add_action( 'init', array( $this, 'init_hook_action' ) );
add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'register_gutenberg_block' ) );
}

/**
Expand Down Expand Up @@ -277,5 +295,57 @@ public static function get_blog_id() {
public static function get_connected_account_id() {
return get_option( self::$connected_account_id_option_name );
}

/**
* Whether Recurring Payments are enabled.
*
* @return bool
*/
public static function is_enabled_jetpack_recurring_payments() {
// For WPCOM sites.
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_any_blog_stickers' ) ) {
$site_id = get_current_blog_id();
return has_any_blog_stickers( array( 'personal-plan', 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id );
}

// For Jetpack sites.
return Jetpack::is_active() && (
/** This filter is documented in class.jetpack-gutenberg.php */
! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ) || // Remove when the default becomes `true`.
Jetpack_Plan::supports( 'recurring-payments' )
);
}

/**
* Register the Recurring Payments Gutenberg block
*/
public function register_gutenberg_block() {
// This gate was introduced to prevent duplicate registration. A race condition exists where
// the registration that happens via extensions/blocks/recurring-payments/recurring-payments.php
// was adding the registration action after the action had been run in some contexts.
if ( self::$has_registered_block ) {
return;
}

if ( self::is_enabled_jetpack_recurring_payments() ) {
jetpack_register_block(
'jetpack/recurring-payments',
array(
'render_callback' => array( $this, 'render_button' ),
)
);
} else {
Jetpack_Gutenberg::set_extension_unavailable(
'jetpack/recurring-payments',
'missing_plan',
array(
'required_feature' => 'memberships',
'required_plan' => self::$required_plan,
)
);
}

self::$has_registered_block = true;
}
}
Jetpack_Memberships::get_instance();