diff --git a/projects/plugins/jetpack/changelog/add-shield-memberships-blocks-for-unconnected-jetpack-sites b/projects/plugins/jetpack/changelog/add-shield-memberships-blocks-for-unconnected-jetpack-sites new file mode 100644 index 0000000000000..878ee8cec67b0 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-shield-memberships-blocks-for-unconnected-jetpack-sites @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +[In administration] Do not register Memberships blocks when the site is not connected to Jetpack diff --git a/projects/plugins/jetpack/extensions/blocks/donations/donations.php b/projects/plugins/jetpack/extensions/blocks/donations/donations.php index b59b63f760733..8b5966abe76bd 100644 --- a/projects/plugins/jetpack/extensions/blocks/donations/donations.php +++ b/projects/plugins/jetpack/extensions/blocks/donations/donations.php @@ -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' ); diff --git a/projects/plugins/jetpack/extensions/blocks/payment-buttons/payment-buttons.php b/projects/plugins/jetpack/extensions/blocks/payment-buttons/payment-buttons.php index 642575b7aec03..ce91746b8e13d 100644 --- a/projects/plugins/jetpack/extensions/blocks/payment-buttons/payment-buttons.php +++ b/projects/plugins/jetpack/extensions/blocks/payment-buttons/payment-buttons.php @@ -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( diff --git a/projects/plugins/jetpack/extensions/blocks/premium-content/premium-content.php b/projects/plugins/jetpack/extensions/blocks/premium-content/premium-content.php index 484bfa4898a61..d8449eb44d4b8 100644 --- a/projects/plugins/jetpack/extensions/blocks/premium-content/premium-content.php +++ b/projects/plugins/jetpack/extensions/blocks/premium-content/premium-content.php @@ -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', diff --git a/projects/plugins/jetpack/extensions/blocks/subscriptions/subscriptions.php b/projects/plugins/jetpack/extensions/blocks/subscriptions/subscriptions.php index 77c9664df72ea..c7728d1060ef5 100644 --- a/projects/plugins/jetpack/extensions/blocks/subscriptions/subscriptions.php +++ b/projects/plugins/jetpack/extensions/blocks/subscriptions/subscriptions.php @@ -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; @@ -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( diff --git a/projects/plugins/jetpack/modules/memberships/class-jetpack-memberships.php b/projects/plugins/jetpack/modules/memberships/class-jetpack-memberships.php index 70e1567f3c036..214ac57f15bc6 100644 --- a/projects/plugins/jetpack/modules/memberships/class-jetpack-memberships.php +++ b/projects/plugins/jetpack/modules/memberships/class-jetpack-memberships.php @@ -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; @@ -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; @@ -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() ) { + // 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. *