Skip to main content

Send a pre-publish choice with the publish

Free Advanced

A section you add to Ready to Publish? has no episode id on a first publish, and a tier available at publish unlocks at the end of that same request. So hand your choice to the save: your fill receives set_publish_data( key, value ), and PHP reads the value back with benecaster_get_episode_publish_data().

The section, in your add-on’s admin JavaScript:

function MySection( { set_publish_data } ) {
    const { React } = window.BenecasterExtensions;
    const [ send, setSend ] = React.useState( true );

    // Set it on mount too: the value lasts for one opening of the dialog.
    React.useEffect( () => set_publish_data( 'my_addon_send', send ), [ send, set_publish_data ] );

    return React.createElement( 'label', null,
        React.createElement( 'input', { type: 'checkbox', checked: send, onChange: ( e ) => setSend( e.target.checked ) } ),
        ' Notify subscribers'
    );
}

wp.domReady( () => window.BenecasterExtensions.registerFill( 'BenecasterEpisodePrePublish', MySection ) );

The PHP below keeps the choice and acts on it.

Code

<?php
// Tiers that unlock in this request can read it directly. A tier scheduled for
// later unlocks from cron, where it is gone, so keep it now.
add_action( 'benecaster_episode_published', function ( int $episode_id ): void {
    $send = benecaster_get_episode_publish_data( 'my_addon_send' );
    if ( null !== $send ) {
        update_post_meta( $episode_id, '_my_addon_send', (bool) $send ? '1' : '0' );
    }
} );

add_action( 'benecaster_availability_unlocked', function ( int $episode_id, int $show_id, string $tier_slug ): void {
    if ( '0' === get_post_meta( $episode_id, '_my_addon_send', true ) ) {
        return;
    }
    my_addon_notify( $episode_id, $show_id, $tier_slug );
}, 10, 3 );

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →