Skip to content

Commit

Permalink
Subscription Site: Release the Subscriber Login navigation placement …
Browse files Browse the repository at this point in the history
…toggle (#36717)
  • Loading branch information
pkuliga authored Apr 5, 2024
1 parent 3e8e42c commit 241baf3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
currentThemeIsBlockTheme,
currentThemeStylesheet,
getSiteAdminUrl,
isSubscriptionSiteEnabled,
subscriptionSiteEditSupported,
} from 'state/initial-state';
import { getModule } from 'state/modules';
Expand Down Expand Up @@ -47,7 +46,6 @@ function SubscriptionsSettings( props ) {
isSmEnabled,
isSubscribePostEndEnabled,
isLoginNavigationEnabled,
isSubscriptionSiteFeatureEnabled,
isSubscriptionSiteEditSupported,
isSubscriptionsActive,
siteRawUrl,
Expand Down Expand Up @@ -78,8 +76,8 @@ function SubscriptionsSettings( props ) {

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

Expand Down Expand Up @@ -247,7 +245,7 @@ function SubscriptionsSettings( props ) {
'jetpack'
) }
/>
{ isSubscriptionSiteFeatureEnabled && (
{ isSubscriptionSiteEditSupported && (
<ToggleControl
checked={ isSubscriptionsActive && isLoginNavigationEnabled }
disabled={ isDisabled }
Expand All @@ -258,7 +256,7 @@ function SubscriptionsSettings( props ) {
label={
<>
{ __( 'Add the Subscriber Login Block to the navigation', 'jetpack' ) }
{ isBlockTheme && headerTemplateEditorUrl && (
{ headerTemplateEditorUrl && (
<>
{ '. ' }
<ExternalLink href={ headerTemplateEditorUrl }>
Expand Down Expand Up @@ -306,7 +304,6 @@ export default withModuleSettingsFormHelpers(
isLoginNavigationEnabled: ownProps.getOptionValue(
'jetpack_subscriptions_login_navigation_enabled'
),
isSubscriptionSiteFeatureEnabled: isSubscriptionSiteEnabled( state ),
isSubscriptionSiteEditSupported: subscriptionSiteEditSupported( state ),
isBlockTheme: currentThemeIsBlockTheme( state ),
siteAdminUrl: getSiteAdminUrl( state ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Subscription Site: Release the Subscriber Login navigation placement toggle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Automattic\Jetpack\Extensions\Subscriber_Login;

use WP_Block_Template;
use WP_Post;

/**
* Jetpack_Subscription_Site class.
*/
Expand Down Expand Up @@ -36,21 +39,26 @@ public static function init() {
* @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.
* Returns true if context is recognized as a header element.
*
* @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to.
*
* @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 );
protected function is_header_context( $context ) {
if ( $context instanceof WP_Post && $context->post_type === 'wp_navigation' ) {
return true;
}

if ( $context instanceof WP_Block_Template && $context->area === 'header' ) {
return true;
}

return false;
}

/**
Expand All @@ -59,26 +67,32 @@ protected function is_subscription_site_feature_enabled() {
* @return void
*/
protected function handle_subscriber_login_block_navigation_placement() {
global $wp_version;

$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.
if ( ! wp_is_block_theme() || version_compare( $wp_version, '6.5-beta2', '<' ) ) { // TODO Fallback for classic themes and wp core < 6.5-beta2.
return;
}

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

return $hooked_blocks;
},
10,
3
4
);
}
}

0 comments on commit 241baf3

Please sign in to comment.