Skip to content

Commit

Permalink
feat: allow unsetting interest groups
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonrabb committed Apr 16, 2020
1 parent 59f0643 commit 5498f66
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
35 changes: 22 additions & 13 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ public static function api_set_mailchimp_list( $request ) {
public static function api_set_mailchimp_interest( $request ) {
$id = $request['id'];
$exploded = explode( ':', $request['interest_id'] );
$field = $exploded[0];
$interest_id = $exploded[1];
$field = count( $exploded ) ? $exploded[0] : null;
$interest_id = count( $exploded ) > 1 ? $exploded[1] : null;

if ( self::NEWSPACK_NEWSLETTERS_CPT !== get_post_type( $id ) ) {
return new WP_Error(
Expand All @@ -341,6 +341,13 @@ public static function api_set_mailchimp_interest( $request ) {
);
}

if ( 'no_interests' !== $request['interest_id'] && ( ! $field || ! $interest_id ) ) {
return new WP_Error(
'newspack_newsletters_incorrect_post_type',
__( 'Invalid Mailchimp Interest .', 'newspack-newsletters' )
);
}

$mc_campaign_id = get_post_meta( $id, 'mc_campaign_id', true );
if ( ! $mc_campaign_id ) {
return new WP_Error(
Expand All @@ -360,19 +367,21 @@ public static function api_set_mailchimp_interest( $request ) {
);
}

$segment_opts = [
'match' => 'any',
'conditions' => [
[
'condition_type' => 'Interests',
'field' => $field,
'op' => 'interestcontains',
'value' => [
$interest_id,
$segment_opts = ( 'no_interests' === $request['interest_id'] ) ?
(object) [] :
[
'match' => 'any',
'conditions' => [
[
'condition_type' => 'Interests',
'field' => $field,
'op' => 'interestcontains',
'value' => [
$interest_id,
],
],
],
],
];
];

$payload = [
'recipients' => [
Expand Down
13 changes: 11 additions & 2 deletions src/editor/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class Sidebar extends Component {
apiFetch( params ).then( result => this.setStateFromAPIResponse( result ) );
};
setInterest = interestId => {
if ( ! interestId || 'interest_group' === interestId ) {
return;
}
this.setState( { inFlight: true } );
const { postId } = this.props;
const params = {
Expand Down Expand Up @@ -132,7 +135,7 @@ class Sidebar extends Component {
const { title, interests, id } = item;
accumulator.push( {
label: title,
value: interest_id,
value: 'interest_group',
} );
if ( interests && interests.interests && interests.interests.length ) {
interests.interests.forEach( interest => {
Expand All @@ -151,7 +154,13 @@ class Sidebar extends Component {
<SelectControl
label={ __( 'Interests', 'newspack-newsletters' ) }
value={ interestValue }
options={ options }
options={ [
{
label: __( '-- No Interest Selected --', 'newspack-newsletters' ),
value: 'no_interests',
},
...options,
] }
onChange={ value => this.setInterest( value ) }
disabled={ inFlight }
/>
Expand Down

0 comments on commit 5498f66

Please sign in to comment.