Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { useEffect, useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { Notice, SectionHeader, SelectControl } from '../../../../components/src';

export default function ActiveCampaign( { value, onChange } ) {
const [ inFlight, setInFlight ] = useState( false );
const [ lists, setLists ] = useState( [] );
const [ error, setError ] = useState( false );
const fetchLists = () => {
setError( false );
setInFlight( true );
apiFetch( {
path: '/newspack-newsletters/v1/lists',
} )
.then( setLists )
.catch( setError )
.finally( () => setInFlight( false ) );
};
useEffect( fetchLists, [] );
const handleChange = key => val => onChange && onChange( key, val );
return (
<>
{ error && (
<Notice
noticeText={ error?.message || __( 'Something went wrong.', 'newspack' ) }
isError
/>
) }
<SectionHeader
title={ __( 'ActiveCampaign', 'newspack' ) }
description={ __( 'Settings for the ActiveCampaign integration.', 'newspack' ) }
/>
<SelectControl
label={ __( 'Master List', 'newspack' ) }
help={ __( 'Choose a list to which all registered readers will be added.', 'newspack' ) }
disabled={ inFlight }
value={ value.masterList }
onChange={ handleChange( 'masterList' ) }
options={ [
{ value: '', label: __( 'None', 'newspack' ) },
...lists.map( list => ( { label: list.name, value: list.id } ) ),
] }
/>
</>
);
}
25 changes: 24 additions & 1 deletion assets/wizards/engagement/views/reader-activation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ import {
TextControl,
withWizardScreen,
} from '../../../../components/src';
import ActiveCampaign from './active-campaign';

export default withWizardScreen( () => {
const [ inFlight, setInFlight ] = useState( false );
const [ config, setConfig ] = useState( {} );
const [ error, setError ] = useState( null );
const [ error, setError ] = useState( false );
const [ isActiveCampaign, setIsActiveCampaign ] = useState( false );
const updateConfig = ( key, val ) => {
setConfig( { ...config, [ key ]: val } );
};
const fetchConfig = () => {
setError( false );
setInFlight( true );
apiFetch( {
path: '/newspack/v1/wizard/newspack-engagement-wizard/reader-activation',
Expand All @@ -36,6 +39,7 @@ export default withWizardScreen( () => {
.finally( () => setInFlight( false ) );
};
const saveConfig = () => {
setError( false );
setInFlight( true );
apiFetch( {
path: '/newspack/v1/wizard/newspack-engagement-wizard/reader-activation',
Expand All @@ -47,6 +51,15 @@ export default withWizardScreen( () => {
.finally( () => setInFlight( false ) );
};
useEffect( fetchConfig, [] );
useEffect( () => {
apiFetch( {
path: '/newspack/v1/wizard/newspack-engagement-wizard/newsletters',
} ).then( data => {
setIsActiveCampaign(
data?.settings?.newspack_newsletters_service_provider?.value === 'active_campaign'
);
} );
}, [] );
return (
<>
{ error && (
Expand Down Expand Up @@ -100,6 +113,16 @@ export default withWizardScreen( () => {
/>
</Grid>
</Card>
{ isActiveCampaign && (
<ActiveCampaign
value={ { masterList: config.active_campaign_master_list } }
onChange={ ( key, value ) => {
if ( key === 'masterList' ) {
updateConfig( 'active_campaign_master_list', value );
}
} }
/>
) }
<div className="newspack-buttons-card">
<Button isPrimary onClick={ saveConfig } disabled={ inFlight }>
{ __( 'Save Changes', 'newspack' ) }
Expand Down
1 change: 1 addition & 0 deletions includes/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private static function get_settings_config() {
'newsletters_label' => __( 'Subscribe to our newsletters:', 'newspack' ),
'terms_text' => __( 'By signing up, you agree to our Terms and Conditions.', 'newspack' ),
'terms_url' => '',
'active_campaign_master_list' => '',
];

/**
Expand Down
48 changes: 41 additions & 7 deletions includes/plugins/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class Newspack_Newsletters {
*/
public static function init() {
if ( Reader_Activation::is_enabled() ) {
\add_action( 'newspack_newsletters_update_contact_lists', [ __CLASS__, 'newspack_newsletters_update_contact_lists' ], 10, 5 );
\add_filter( 'newspack_newsletters_contact_data', [ __CLASS__, 'newspack_newsletters_contact_data' ], 10, 3 );
\add_action( 'newspack_newsletters_update_contact_lists', [ __CLASS__, 'update_contact_lists' ], 10, 5 );
\add_filter( 'newspack_newsletters_contact_data', [ __CLASS__, 'contact_data' ], 10, 3 );
\add_filter( 'newspack_newsletters_contact_lists', [ __CLASS__, 'add_activecampaign_master_list' ], 10, 3 );
}
}

Expand All @@ -32,7 +33,7 @@ public static function init() {
* @param string[] $lists_to_remove Array of list IDs to remove the contact from.
* @param bool|WP_Error $result True if the contact was updated or error if failed.
*/
public static function newspack_newsletters_update_contact_lists( $provider, $email, $lists_to_add, $lists_to_remove, $result ) {
public static function update_contact_lists( $provider, $email, $lists_to_add, $lists_to_remove, $result ) {
switch ( $provider ) {
case 'active_campaign':
if ( true === $result && method_exists( '\Newspack_Newsletters_Subscription', 'add_contact' ) && method_exists( '\Newspack_Newsletters_Subscription', 'get_contact_lists' ) ) {
Expand All @@ -47,17 +48,17 @@ public static function newspack_newsletters_update_contact_lists( $provider, $em
/**
* Modify metadata for newsletter contact creation.
*
* @param string $provider The provider name.
* @param array $contact {
* @param array $contact {
* Contact information.
*
* @type string $email Contact email address.
* @type string $name Contact name. Optional.
* @type string[] $metadata Contact additional metadata. Optional.
* }
* @param string[]|false $selected_list_ids Array of list IDs the contact will be subscribed to, or false.
* @param string[]|false $selected_list_ids Array of list IDs the contact will be subscribed to, or false.
* @param string $provider The provider name.
*/
public static function newspack_newsletters_contact_data( $provider, $contact, $selected_list_ids ) {
public static function contact_data( $contact, $selected_list_ids, $provider ) {
switch ( $provider ) {
case 'active_campaign':
$metadata = [];
Expand Down Expand Up @@ -158,5 +159,38 @@ function( $acc, $item ) {
return $contact;
}
}

/**
* Ensure the contact is always added to ActiveCampaign's selected master
* list.
*
* @param string[]|false $lists Array of list IDs the contact will be subscribed to, or false.
* @param array $contact {
* Contact information.
*
* @type string $email Contact email address.
* @type string $name Contact name. Optional.
* @type string[] $metadata Contact additional metadata. Optional.
* }
* @param string $provider The provider name.
*
* @return string[]|false
*/
public static function add_activecampaign_master_list( $lists, $contact, $provider ) {
if ( 'active_campaign' !== $provider ) {
return $lists;
}
$master_list_id = Reader_Activation::get_setting( 'active_campaign_master_list' );
if ( ! $master_list_id ) {
return $lists;
}
if ( empty( $lists ) ) {
return [ $master_list_id ];
}
if ( array_search( $master_list_id, $lists ) === false ) {
$lists[] = $master_list_id;
}
return $lists;
}
}
Newspack_Newsletters::init();