Skip to content

Commit

Permalink
Subscription Site: Hook Subscriber Login block into the navigation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pkuliga authored Mar 28, 2024
1 parent 2ece631 commit 9d777e1
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function SubscriptionsSettings( props ) {
isStcEnabled,
isSmEnabled,
isSubscribePostEndEnabled,
isLoginNavigationEnabled,
isSubscriptionSiteFeatureEnabled,
isSubscriptionSiteEditSupported,
isSubscriptionsActive,
siteRawUrl,
Expand Down Expand Up @@ -74,6 +76,13 @@ function SubscriptionsSettings( props ) {
} )
: null;

const headerTemplateEditorUrl = siteAdminUrl
? addQueryArgs( `${ siteAdminUrl }site-editor.php`, {
postType: 'wp_template_part',
postId: `${ themeStylesheet }//header`,
} )
: null;

const handleSubscribeToBlogToggleChange = useCallback( () => {
updateFormStateModuleOption( SUBSCRIPTIONS_MODULE_NAME, 'stb_enabled' );
}, [ updateFormStateModuleOption ] );
Expand All @@ -93,6 +102,13 @@ function SubscriptionsSettings( props ) {
);
}, [ updateFormStateModuleOption ] );

const handleLoginNavigationToggleChange = useCallback( () => {
updateFormStateModuleOption(
SUBSCRIPTIONS_MODULE_NAME,
'jetpack_subscriptions_login_navigation_enabled'
);
}, [ updateFormStateModuleOption ] );

const getSubClickableCard = () => {
if ( unavailableInOfflineMode || ! isSubscriptionsActive || ! isLinked ) {
return '';
Expand Down Expand Up @@ -231,6 +247,29 @@ function SubscriptionsSettings( props ) {
'jetpack'
) }
/>
{ isSubscriptionSiteFeatureEnabled && (
<ToggleControl
checked={ isSubscriptionsActive && isLoginNavigationEnabled }
disabled={ isDisabled }
toggling={ isSavingAnyOption( [
'jetpack_subscriptions_login_navigation_enabled',
] ) }
onChange={ handleLoginNavigationToggleChange }
label={
<>
{ __( 'Add the Subscriber Login Block to the navigation', 'jetpack' ) }
{ isBlockTheme && headerTemplateEditorUrl && (
<>
{ '. ' }
<ExternalLink href={ headerTemplateEditorUrl }>
{ __( 'Preview and edit', 'jetpack' ) }
</ExternalLink>
</>
) }
</>
}
/>
) }
</FormFieldset>
}
</SettingsGroup>
Expand Down Expand Up @@ -264,6 +303,9 @@ export default withModuleSettingsFormHelpers(
isSubscribePostEndEnabled: ownProps.getOptionValue(
'jetpack_subscriptions_subscribe_post_end_enabled'
),
isLoginNavigationEnabled: ownProps.getOptionValue(
'jetpack_subscriptions_login_navigation_enabled'
),
isSubscriptionSiteFeatureEnabled: isSubscriptionSiteEnabled( state ),
isSubscriptionSiteEditSupported: subscriptionSiteEditSupported( state ),
isBlockTheme: currentThemeIsBlockTheme( state ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2659,6 +2659,13 @@ public static function get_updateable_data_list( $selector = '' ) {
'validate_callback' => __CLASS__ . '::validate_boolean',
'jp_group' => 'subscriptions',
),
'jetpack_subscriptions_login_navigation_enabled' => array(
'description' => esc_html__( 'Add Subscriber Login block to the navigation.', 'jetpack' ),
'type' => 'boolean',
'default' => 0,
'validate_callback' => __CLASS__ . '::validate_boolean',
'jp_group' => 'subscriptions',
),
'social_notifications_subscribe' => array(
'description' => esc_html__( 'Send email notification when someone subscribes to my blog', 'jetpack' ),
'type' => 'boolean',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ public function update_data( $request ) {
case 'wpcom_featured_image_in_email':
case 'wpcom_subscription_emails_use_excerpt':
case 'jetpack_subscriptions_subscribe_post_end_enabled':
case 'jetpack_subscriptions_login_navigation_enabled':
// Convert the false value to 0. This allows the option to be updated if it doesn't exist yet.
$sub_value = $value ? $value : 0;
$updated = (string) get_option( $option ) !== (string) $sub_value ? update_option( $option, $sub_value ) : true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Subscription Site: Hook Subscriber Login block into the navigation
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Adds support for Jetpack Subscription Site feature.
*
* @package automattic/jetpack
* @since $$next_version$$
*/

namespace Automattic\Jetpack\Extensions\Subscriber_Login;

/**
* Jetpack_Subscription_Site class.
*/
class Jetpack_Subscription_Site {
/**
* Jetpack_Subscription_Site singleton instance.
*
* @var Jetpack_Subscription_Site|null
*/
private static $instance;

/**
* Jetpack_Subscription_Site instance init.
*/
public static function init() {
if ( self::$instance === null ) {
self::$instance = new Jetpack_Subscription_Site();
}

return self::$instance;
}

/**
* Handles Subscriber Login block placements.
*
* @return void
*/
public function handle_subscriber_login_block_placements() {
if ( ! $this->is_subscription_site_feature_enabled() ) {
return;
}

$this->handle_subscriber_login_block_navigation_placement();
}

/**
* Returns true if Subscription Site feature is enabled.
*
* @return bool
*/
protected function is_subscription_site_feature_enabled() {
// It's temporary. Allows to enable the Subscription Site feature.
return (bool) apply_filters( 'jetpack_subscription_site_enabled', false );
}

/**
* Handles Subscriber Login block navigation placement.
*
* @return void
*/
protected function handle_subscriber_login_block_navigation_placement() {
$subscriber_login_navigation_enabled = get_option( 'jetpack_subscriptions_login_navigation_enabled', false );
if ( ! $subscriber_login_navigation_enabled ) {
return;
}

if ( ! wp_is_block_theme() ) { // TODO Fallback for classic themes.
return;
}

add_filter(
'hooked_block_types',
function ( $hooked_blocks, $relative_position, $anchor_block ) {
if ( $anchor_block === 'core/navigation' && $relative_position === 'last_child' ) {
$hooked_blocks[] = 'jetpack/subscriber-login';
}

return $hooked_blocks;
},
10,
3
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Jetpack_Memberships;
use Jetpack_Options;

require_once __DIR__ . '/class-jetpack-subscription-site.php';

/**
* Registers the block for use in Gutenberg
* This is done via an action so that we can disable
Expand All @@ -35,6 +37,8 @@ function register_block() {
__DIR__,
array( 'render_callback' => __NAMESPACE__ . '\render_block' )
);

Jetpack_Subscription_Site::init()->handle_subscriber_login_block_placements();
}
add_action( 'init', __NAMESPACE__ . '\register_block' );

Expand Down

0 comments on commit 9d777e1

Please sign in to comment.