Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shield memberships blocks for unconnected jetpack sites #39390

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

[In administration] Do not register Memberships blocks when the site is not connected to Jetpack
16 changes: 10 additions & 6 deletions projects/plugins/jetpack/extensions/blocks/donations/donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
* registration if we need to.
*/
function register_block() {
Blocks::jetpack_register_block(
__DIR__,
array(
'render_callback' => __NAMESPACE__ . '\render_block',
)
);

require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
if ( \Jetpack_Memberships::should_enable_monetize_blocks_in_editor() ) {
Blocks::jetpack_register_block(
__DIR__,
array(
'render_callback' => __NAMESPACE__ . '\render_block',
)
);
}
}
add_action( 'init', __NAMESPACE__ . '\register_block' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function register_block() {
}

require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
if ( \Jetpack_Memberships::is_enabled_jetpack_recurring_payments() ) {
if ( \Jetpack_Memberships::is_enabled_jetpack_recurring_payments() &&
\Jetpack_Memberships::should_enable_monetize_blocks_in_editor() ) {
Blocks::jetpack_register_block(
__DIR__,
array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@
* registration if we need to.
*/
function register_block() {
Blocks::jetpack_register_block(
__DIR__,
array(
'render_callback' => __NAMESPACE__ . '\render_block',
'provides_context' => array(
'premium-content/planId' => 'selectedPlanId', // Deprecated.
'premium-content/planIds' => 'selectedPlanIds',
'isPremiumContentChild' => 'isPremiumContentChild',
),
)
);

require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
if ( \Jetpack_Memberships::should_enable_monetize_blocks_in_editor() ) {
Blocks::jetpack_register_block(
__DIR__,
array(
'render_callback' => __NAMESPACE__ . '\render_block',
'provides_context' => array(
'premium-content/planId' => 'selectedPlanId', // Deprecated.
'premium-content/planIds' => 'selectedPlanIds',
'isPremiumContentChild' => 'isPremiumContentChild',
),
)
);
}

register_post_meta(
'post',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
namespace Automattic\Jetpack\Extensions\Subscriptions;

use Automattic\Jetpack\Blocks;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Status\Host;
use Jetpack;
use Jetpack_Gutenberg;
Expand Down Expand Up @@ -45,10 +43,9 @@ function register_block() {
return;
}

if (
( defined( 'IS_WPCOM' ) && IS_WPCOM )
|| ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() )
) {
require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
if ( \Jetpack_Memberships::should_enable_monetize_blocks_in_editor() ) {

Blocks::jetpack_register_block(
__DIR__,
array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

use Automattic\Jetpack\Blocks;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Status\Host;
use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS;
use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_FOR_POST_TIER_ID_SETTINGS;
Expand Down Expand Up @@ -174,7 +176,7 @@ public static function get_instance() {
self::$instance->register_init_hook();
// Yes, `pro-plan` with a dash, `jetpack_personal` with an underscore. Check the v1.5 endpoint to verify.
$wpcom_plan_slug = defined( 'ENABLE_PRO_PLAN' ) ? 'pro-plan' : 'personal-bundle';
self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? $wpcom_plan_slug : 'jetpack_personal';
self::$required_plan = ( new Host() )->is_wpcom_simple() ? $wpcom_plan_slug : 'jetpack_personal';
}

return self::$instance;
Expand Down Expand Up @@ -743,10 +745,30 @@ public static function user_can_view_post( $post_id = null ) {
* @return bool
*/
public static function is_enabled_jetpack_recurring_payments() {
$api_available = ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_connection_ready() );
$api_available = ( new Host() )->is_wpcom_simple() || Jetpack::is_connection_ready();
return $api_available;
}

/**
* Whether to enable the blocks in the editor.
* All Monetize blocks (except Simple Payments) need an active connecting and a user with at least `edit_posts` capability
*
* @return bool
*/
public static function should_enable_monetize_blocks_in_editor() {
if ( ! is_admin() ) {
millerf marked this conversation as resolved.
Show resolved Hide resolved
// We enable the block for the front-end in all cases
return true;

}

$manager = new Connection_Manager( 'jetpack' );
$jetpack_ready_and_connected = $manager->is_connected() && $manager->has_connected_owner();
$is_offline_mode = ( new Status() )->is_offline_mode();
$enable_monetize_blocks_in_editor = ( new Host() )->is_wpcom_simple() || ( $jetpack_ready_and_connected && ! $is_offline_mode );
return $enable_monetize_blocks_in_editor;
}

/**
* Whether site has any paid plan.
*
Expand Down
Loading