Skip to content
7 changes: 7 additions & 0 deletions extensions/blocks/subscriptions-toggle/editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Internal dependencies
*/
import { name, settings } from '.';
import registerJetpackPlugin from '../../shared/register-jetpack-plugin';

registerJetpackPlugin( name, settings );
8 changes: 8 additions & 0 deletions extensions/blocks/subscriptions-toggle/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Internal dependencies
*/
import SubscriptionsCheckbox from './subscriptions-checkbox';

export const name = 'subscriptions-toggle';

export const settings = { render: SubscriptionsCheckbox };
45 changes: 45 additions & 0 deletions extensions/blocks/subscriptions-toggle/subscriptions-checkbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { CheckboxControl } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { PostTypeSupportCheck } from '@wordpress/editor';
import { withDispatch, withSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import JetpackSubscriptionsPanel from '../../shared/jetpack-subscriptions-panel';

const SubscriptionsCheckbox = ( { isPostExcludedFromSubs, editPost } ) => (
<PostTypeSupportCheck supportKeys="jetpack-post-subscriptions">
<JetpackSubscriptionsPanel>
<CheckboxControl
label={ __( 'Don’t send this post to subscribers.', 'jetpack' ) }
checked={ isPostExcludedFromSubs }
onChange={ value => {
editPost( { jetpack_dont_email_post_to_subs: value } );
} }
/>
</JetpackSubscriptionsPanel>
</PostTypeSupportCheck>
);

// Fetch the post meta.
const applyWithSelect = withSelect( select => {
const { getEditedPostAttribute } = select( 'core/editor' );
const isPostExcludedFromSubs = getEditedPostAttribute( 'jetpack_dont_email_post_to_subs' );

return { isPostExcludedFromSubs };
} );

// Provide method to update post meta.
const applyWithDispatch = withDispatch( dispatch => {
const { editPost } = dispatch( 'core/editor' );

return { editPost };
} );

// Combine the higher-order components.
export default compose( [ applyWithSelect, applyWithDispatch ] )( SubscriptionsCheckbox );
1 change: 1 addition & 0 deletions extensions/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"simple-payments",
"slideshow",
"subscriptions",
"subscriptions-toggle",
"tiled-gallery",
"videopress",
"wordads"
Expand Down
35 changes: 35 additions & 0 deletions extensions/shared/jetpack-subscriptions-panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { createSlotFill, PanelBody } from '@wordpress/components';
import { registerPlugin } from '@wordpress/plugins';

/**
* Internal dependencies
*/
import JetpackPluginSidebar from './jetpack-plugin-sidebar';

const { Fill, Slot } = createSlotFill( 'JetpackSubscriptionsPanel' );

export { Fill as default };

registerPlugin( 'jetpack-subscriptions-panel', {
render() {
return (
<Slot>
{ fills => {
if ( ! fills.length ) {
return null;
}

return (
<JetpackPluginSidebar>
<PanelBody title={ __( 'Subscriptions', 'jetpack' ) }>{ fills }</PanelBody>
</JetpackPluginSidebar>
);
} }
</Slot>
);
},
} );
105 changes: 97 additions & 8 deletions modules/subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ function __construct() {
add_filter( 'jetpack_published_post_flags', array( $this, 'set_post_flags' ), 10, 2 );

add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 18, 1 );

add_action( 'rest_api_init', array( $this, 'register_sub_check_rest_field' ) );
add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'register_gutenberg_extension' ) );
}

/**
Expand All @@ -109,11 +112,14 @@ function xmlrpc_methods( $methods ) {
);
}

/*
* Disable Subscribe on Single Post
* Register post meta
/**
* Check whether a subscription toggle should be displayed in the editor.
*
* @since 8.0.0
*
* @return bool
*/
function subscription_post_page_metabox() {
private function should_show_subscription_toggle() {
if (
/**
* Filter whether or not to show the per-post subscription option.
Expand All @@ -124,12 +130,22 @@ function subscription_post_page_metabox() {
*
* @param bool true = show checkbox option on all new posts | false = hide the option.
*/
! apply_filters( 'jetpack_allow_per_post_subscriptions', false ) )
{
return;
! apply_filters( 'jetpack_allow_per_post_subscriptions', false )
|| has_filter( 'jetpack_subscriptions_exclude_these_categories' )
|| has_filter( 'jetpack_subscriptions_include_only_these_categories' )
) {
return false;
}

if ( has_filter( 'jetpack_subscriptions_exclude_these_categories' ) || has_filter( 'jetpack_subscriptions_include_only_these_categories' ) ) {
return true;
}

/**
* Disable Subscribe on Single Post
* Register post meta
*/
public function subscription_post_page_metabox() {
if ( ! $this->should_show_subscription_toggle() ) {
return;
}

Expand All @@ -148,6 +164,79 @@ function subscription_post_page_metabox() {
<?php endif;
}

/**
* Add subscription post_meta to the REST API Post response.
* Only when the jetpack_allow_per_post_subscriptions filter is in use.
* This controls the checkbox displayed in the Jetpack sidebar, in the block editor.
*
* @since 8.0.0
*
* @uses register_rest_field
*/
public function register_sub_check_rest_field() {
if ( ! $this->should_show_subscription_toggle() ) {
return;
}

// Subscriptions only support posts for now.
$post_type = 'post';

register_rest_field(
$post_type,
'jetpack_dont_email_post_to_subs',
array(
'get_callback' => function( $post ) {
$disable_subscribe_value = get_post_meta(
$post['id'],
'_jetpack_dont_email_post_to_subs',
true
);

return $disable_subscribe_value;
},
'update_callback' => function( $disable_subscribe_value, $post ) {
$updated = update_post_meta(
$post->ID,
'_jetpack_dont_email_post_to_subs',
$disable_subscribe_value
);

if ( false === $updated ) {
return new WP_Error(
'jetpack_subscription_disable_failed',
__( 'Failed to change the Subscription status of this post.', 'jetpack' ),
array( 'status' => 500 )
);
}
return true;
},
'schema' => array(
'description' => __( 'Don’t send this to subscribers', 'jetpack' ),
'type' => 'boolean',
),
)
);

/**
* Ensures all public internal post-types support `subscriptions`
* This feature support flag is used by the REST API and Gutenberg.
*/
add_post_type_support( $post_type, 'jetpack-post-subscriptions' );
}

/**
* Register the block extension so it can be displayed in the editor.
*
* @since 8.0.0
*/
public function register_gutenberg_extension() {
if ( ! $this->should_show_subscription_toggle() ) {
return;
}

Jetpack_Gutenberg::set_extension_available( 'jetpack/subscriptions-toggle' );
}

/**
* Checks whether or not the post should be emailed to subscribers
*
Expand Down