Skip to content

Commit

Permalink
feat: select mailchimp interests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonrabb committed Apr 16, 2020
1 parent 48d1366 commit 59f0643
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 20 deletions.
14 changes: 0 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@ public static function rest_api_init() {
],
]
);
\register_rest_route(
'newspack-newsletters/v1/',
'mailchimp/(?P<id>[\a-z]+)/interest/(?P<interest_id>[\a-z]+)',
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ __CLASS__, 'api_set_mailchimp_interest' ],
'permission_callback' => [ __CLASS__, 'api_permissions_check' ],
'args' => [
'id' => [
'sanitize_callback' => 'absint',
],
'interest_id' => [
'sanitize_callback' => 'esc_attr',
],
],
]
);
\register_rest_route(
'newspack-newsletters/v1/',
'mailchimp/(?P<id>[\a-z]+)/settings',
Expand Down Expand Up @@ -306,6 +323,72 @@ public static function api_set_mailchimp_list( $request ) {
return \rest_ensure_response( $data );
}

/**
* Set Mailchimp Interest.
*
* @param WP_REST_Request $request API request object.
*/
public static function api_set_mailchimp_interest( $request ) {
$id = $request['id'];
$exploded = explode( ':', $request['interest_id'] );
$field = $exploded[0];
$interest_id = $exploded[1];

if ( self::NEWSPACK_NEWSLETTERS_CPT !== get_post_type( $id ) ) {
return new WP_Error(
'newspack_newsletters_incorrect_post_type',
__( 'Post is not a Newsletter.', 'newspack-newsletters' )
);
}

$mc_campaign_id = get_post_meta( $id, 'mc_campaign_id', true );
if ( ! $mc_campaign_id ) {
return new WP_Error(
'newspack_newsletters_no_campaign_id',
__( 'Mailchimp campaign ID not found.', 'newspack-newsletters' )
);
}

$mc = new Mailchimp( self::mailchimp_api_key() );
$campaign = $mc->get( "campaigns/$mc_campaign_id" );
$list_id = isset( $campaign, $campaign['recipients'], $campaign['recipients']['list_id'] ) ? $campaign['recipients']['list_id'] : null;

if ( ! $list_id ) {
return new WP_Error(
'newspack_newsletters_no_campaign_id',
__( 'Mailchimp list ID not found.', 'newspack-newsletters' )
);
}

$segment_opts = [
'match' => 'any',
'conditions' => [
[
'condition_type' => 'Interests',
'field' => $field,
'op' => 'interestcontains',
'value' => [
$interest_id,
],
],
],
];

$payload = [
'recipients' => [
'list_id' => $list_id,
'segment_opts' => $segment_opts,
],
];

$result = $mc->patch( "campaigns/$mc_campaign_id", $payload );

$data = self::retrieve_data( $id );
$data['result'] = $result;

return \rest_ensure_response( $data );
}

/**
* Register custom fields.
*/
Expand Down
29 changes: 23 additions & 6 deletions src/editor/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import {
TextControl,
} from '@wordpress/components';

/**
* External dependencies
*/
import { get } from 'lodash';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -78,6 +83,15 @@ class Sidebar extends Component {
};
apiFetch( params ).then( result => this.setStateFromAPIResponse( result ) );
};
setInterest = interestId => {
this.setState( { inFlight: true } );
const { postId } = this.props;
const params = {
path: `/newspack-newsletters/v1/mailchimp/${ postId }/interest/${ interestId }`,
method: 'POST',
};
apiFetch( params ).then( result => this.setStateFromAPIResponse( result ) );
};
updateSender = ( senderName, senderEmail ) => {
this.setState( { inFlight: true } );
const { postId } = this.props;
Expand Down Expand Up @@ -106,7 +120,7 @@ class Sidebar extends Component {
};

interestCategories = () => {
const { inFlight, interestCategories } = this.state;
const { campaign, inFlight, interestCategories } = this.state;
if (
! interestCategories ||
! interestCategories.categories ||
Expand All @@ -115,27 +129,30 @@ class Sidebar extends Component {
return;
}
const options = interestCategories.categories.reduce( ( accumulator, item ) => {
const { title, interests } = item;
const { title, interests, id } = item;
accumulator.push( {
label: title,
value: null,
value: interest_id,
} );
if ( interests && interests.interests && interests.interests.length ) {
interests.interests.forEach( interest => {
accumulator.push( {
label: '- ' + interest.name,
value: interest.id,
value: 'interests-' + id + ':' + interest.id,
} );
} );
}
return accumulator;
}, [] );
const field = get( campaign, 'recipients.segment_opts.conditions.[0].field' );
const interest_id = get( campaign, 'recipients.segment_opts.conditions.[0].value.[0]' );
const interestValue = field && interest_id ? field + ':' + interest_id : 0;
return (
<SelectControl
label={ __( 'Interests', 'newspack-newsletters' ) }
value={ '' }
value={ interestValue }
options={ options }
onChange={ () => null }
onChange={ value => this.setInterest( value ) }
disabled={ inFlight }
/>
);
Expand Down

0 comments on commit 59f0643

Please sign in to comment.