Skip to content

Commit

Permalink
Add/shield memberships blocks for unconnected jetpack sites (#39390)
Browse files Browse the repository at this point in the history
* Do not register memberships buttons when site is not available

Use of Jetpack_Memberships::is_enabled_jetpack_recurring_payments()

* Create add-shield-memberships-blocks-for-unconnected-jetpack-sites

* Introduce should_enable_monetize_blocks_in_editor

* Use manager

* Replace  ( defined( 'IS_WPCOM' ) && IS_WPCOM ) with ( new Host() )->is_wpcom_simple()

* Use $is_offline_mode

* return is_enabled_jetpack_recurring_payments() in front-end

* Update add-shield-memberships-blocks-for-unconnected-jetpack-sites

* Prevent paid-content to be displayed if the site is not connected
  • Loading branch information
millerf authored Sep 23, 2024
1 parent e173ec2 commit 8caefec
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 26 deletions.
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() ) {
// 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

0 comments on commit 8caefec

Please sign in to comment.