Skip to content
Closed
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-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
64 changes: 18 additions & 46 deletions extensions/blocks/recurring-payments/edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class MembershipsButtonEdit extends Component {
connected: API_STATE_LOADING,
connectURL: null,
addingMembershipAmount: PRODUCT_NOT_ADDING,
shouldUpgrade: false,
upgradeURL: '',
products: [],
siteSlug: '',
Expand Down Expand Up @@ -84,14 +83,13 @@ class MembershipsButtonEdit extends Component {
const {
connect_url: connectURL,
products,
should_upgrade_to_access_memberships: shouldUpgrade,
upgrade_url: upgradeURL,
site_slug: siteSlug,
} = result;
const connected = result.connected_account_id
? API_STATE_CONNECTED
: API_STATE_NOTCONNECTED;
this.setState( { connected, connectURL, products, shouldUpgrade, upgradeURL, siteSlug } );
this.setState( { connected, connectURL, products, upgradeURL, siteSlug } );
},
result => {
const connectURL = null;
Expand Down Expand Up @@ -355,7 +353,14 @@ class MembershipsButtonEdit extends Component {
return (
<Fragment>
{ this.props.noticeUI }
{ this.state.shouldUpgrade && (
{ ( connected === API_STATE_LOADING ||
this.state.addingMembershipAmount === PRODUCT_FORM_SUBMITTED ) &&
! this.props.attributes.planId && (
<Placeholder icon={ <BlockIcon icon={ icon } /> } notices={ notices }>
<Spinner />
</Placeholder>
) }
{ ! this.props.attributes.planId && connected === API_STATE_NOTCONNECTED && (
<div className="wp-block-jetpack-recurring-payments">
<Placeholder
icon={ <BlockIcon icon={ icon } /> }
Expand All @@ -365,55 +370,23 @@ class MembershipsButtonEdit extends Component {
<div className="components-placeholder__instructions">
<p>
{ __(
"You'll need to upgrade your plan to use the Recurring Payments button.",
'In order to start selling Recurring Payments plans, you have to connect to Stripe:',
'jetpack'
) }
</p>
<Button isDefault isLarge href={ this.state.upgradeURL } target="_blank">
{ __( 'Upgrade Your Plan', 'jetpack' ) }
<Button isDefault isLarge href={ connectURL } target="_blank">
{ __( '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>
) }
{ ( connected === API_STATE_LOADING ||
this.state.addingMembershipAmount === PRODUCT_FORM_SUBMITTED ) &&
! this.props.attributes.planId && (
<Placeholder icon={ <BlockIcon icon={ icon } /> } notices={ notices }>
<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 href={ connectURL } target="_blank">
{ __( '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 &&
{ ! this.props.attributes.planId &&
connected === API_STATE_CONNECTED &&
products.length === 0 && (
<div className="wp-block-jetpack-recurring-payments">
Expand All @@ -430,8 +403,7 @@ class MembershipsButtonEdit extends Component {
</Placeholder>
</div>
) }
{ ! this.state.shouldUpgrade &&
! this.props.attributes.planId &&
{ ! this.props.attributes.planId &&
this.state.addingMembershipAmount !== PRODUCT_FORM_SUBMITTED &&
connected === API_STATE_CONNECTED &&
products.length > 0 && (
Expand Down
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();
}
50 changes: 50 additions & 0 deletions modules/memberships/class-jetpack-memberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class Jetpack_Memberships {
* @var array
*/
private static $tags_allowed_in_the_button = array( 'br' => array() );

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

/**
* Classic singleton pattern
*
Expand All @@ -63,6 +71,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 +101,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 +288,44 @@ 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( 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id );
}

// For all Jetpack sites.
return Jetpack::is_active() && Jetpack_Plan::supports( 'recurring-payments' );
}

/**
* Register the Recurring Payments Gutenberg block
*/
public function register_gutenberg_block() {

@chriskmnds chriskmnds Aug 15, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name (register_gutenberg_block) feels a little generic vs the enclosed logic that only deals with a particular block (recurring-payments). I presume this is likely going to evolve into a more general block registration handler (?).

That said, is the underlying logic here a little specific for a general memberships class (or what appears to be)? I prefer to decouple things like these early on if that's the case. Fear being, it can get cluttered with specific block type references sooner than later, and harder to refactor later (or nobody bothers to do so). I do see the class is only a few months old though, so it probably makes sense for v1. 🤔

Just curious on perspective - and the little I understand so far. :-)

p.s. I guess there is the option of moving "block registration" to a trait as an intermediate solution.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name (register_gutenberg_block) feels a little generic vs the enclosed logic that only deals with a particular block (recurring-payments).

Hmm, I was thinking of this method to mean "register this Jetpack module's Gutenberg block". I didn't really want to do a method named register_xyz_gutenberg_block inside of a class named XYZ, that seemed to redundant. (Okay, in this case, it would probably be Jetpack_Memberships::register_recurring_payments_gutenberg_block() rather than Jetpack_Memberships::register_memberships_gutenberg_block(), but I'd argue that memberships and recurring_payments are mostly used synonymously in this file (and that the fact that two names exist for the same thing is mostly circumstantial).

I presume this is likely going to evolve into a more general block registration handler (?).

I didn't intend to evolve it into somehting more general, TBH. The logic driving whether a block should be registered, or whether it should be set as unavailable, and for what reason, is something fairly specific to each individual block.

That said, is the underlying logic here a little specific for a general memberships class (or what appears to be)? I prefer to decouple things like these early on if that's the case. Fear being, it can get cluttered with specific block type references sooner than later, and harder to refactor later (or nobody bothers to do so). I do see the class is only a few months old though, so it probably makes sense for v1.

Just curious on perspective - and the little I understand so far. :-)

p.s. I guess there is the option of moving "block registration" to a trait as an intermediate solution.

I agree in principle; but as stated above, I'm not sure we can make things a lot more homogenous here. We have fairly indidivual Jetpack modules (a somewhat legacy concept) each of which does things quite differently, and we often have to find the best place to register the corresponding block (or set their (un)availability).

I've so far considered the jetpack_register_block() and Jetpack_Gutenberg::set_extension_unavailable our interface; modules can essentially use them at whatever point into their lifecycle (as long as it's soon enough for the consumer -- mostly the REST API -- to get accurate information). I guess for the purpose of the required plan check, we could require each module to provide information about the minimum plan it nees, and evaluate that information and register/set availability based on that.

But this is complicated by the heterogenity of modules themselves. Note that not all modules come with a block; and not all blocks are tied to a module. Furthermore, a lot of modules exist both on JP and WP.com, but are implemented differently in both places.

And finally, there are other criteria besides required plan that can determine whether or not to show a block as available.

(That turned out a bit verbose 😅 I hope I didn't squash that -- commendable! -- idea of better consistency. If I'm beside the point, or if you had any other idea on how to do this, please let me know!)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this was an excellent overview writeup of the problem space to explain the why's of the current setup. :-)

Would some of it deserve a place in the docs?

@chriskmnds chriskmnds Aug 16, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verbose is good. Thanks for the extra clarifications! :-)

I think the "modules" folder is a bit of a scary place for one to be (glad to see you mentioning "legacy" above)!

I can see this is following along how simple-payments is registering blocks. What I find missing I think is a contract binding these on common characteristics, or some abstraction of the things they do in common (which they apparently do - registering blocks for one). I believe you implied that in your answer above.

if you had any other idea on how to do this, please let me know!

I don't, concretely, but I can throw some likely obvious ideas.

Modules seems to be an all-encompassing folder here - so I guess I would start with that - add some structure to the things we know (in this case, "modules that register blocks"). I think I would approach this along these lines:

Centralise the common logic elsewhere - provide a "yet another module" to handle block registration. I think I'd be happy if a module that registers a block defines some options for that block and only registers an action that takes care of the rest e.g.

  1. define block options in the respective module (to be worked out in form, obviously):
class Simple_Payments {
    ...
    const OPTIONS = array( name => "simple-payments", ... );
  1. pass a callback to the action hooks:
class Simple_Payments {
    ...
    private function register_init_hooks() {
        add_action( 'jetpack_register_gutenberg_extension', Fancy_Module_Name::init( self::OPTIONS ) );
    }
  1. with Fancy_Module_Name handling block registration. Simple, and presumably of the form:
class Fancy_Module_Name {
    static function init( $options ) {
        return function() use ( $options ) {
            // do stuff to register block with passed options/config
        };
    }
}

Alternatively, I guess the more OO way - a parent class that these modules can extend from, contract block registration functions via an interface, a trait that these modules can mix in, etc. Just stating the obvious perhaps - but something along these lines! (assuming my naive understanding of any underlying complexities)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and my understanding is indeed fairly naive right now! I can hardly even tell how tightly-coupled the terms "membership" and "recurring-payments" are for one.

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,
)
);
}
}
}
Jetpack_Memberships::get_instance();