Skip to content

Instantly share code, notes, and snippets.

@ashleyfae
Created February 11, 2019 13:58
Show Gist options
  • Save ashleyfae/d1bc84a187cb7f7af75cadb3dbcc7c08 to your computer and use it in GitHub Desktop.
Save ashleyfae/d1bc84a187cb7f7af75cadb3dbcc7c08 to your computer and use it in GitHub Desktop.
RCP MailChimp Pro: Returns the "free" STATUS merge var value for unpaid memberships.
<?php
/**
* Plugin Name: Restrict Content Pro - MailChimp Pro "Free" Status
* Description: Returns the "free" STATUS merge var value for unpaid memberships.
* Version: 1.0
* Author: Restrict Content Pro Team
*/
/**
* Change `STATUS` merge var to `free` for unpaid users on initial registration.
*
* @param array $request_args
* @param RCP_Member $member
* @param string $list_id
*
* @return array
*/
function ag_rcp_mailchimp_pro_subscribe_args( $request_args, $member, $list_id ) {
// Bail if RCP is not on 3.0+
if ( ! defined( 'RCP_PLUGIN_VERSION' ) || version_compare( RCP_PLUGIN_VERSION, '3.0', '<' ) ) {
return $request_args;
}
// Bail if status isn't being set to `active`.
if ( 'active' != $request_args['merge_fields']['STATUS'] ) {
return $request_args;
}
$customer = rcp_get_customer_by_user_id( $member->ID );
// No customer - bail.
if ( empty( $customer ) ) {
return $request_args;
}
$membership = rcp_get_customer_single_membership( $customer->get_id() );
// No membership - bail.
if ( empty( $membership ) ) {
return $request_args;
}
// If the membership is not paid, change the `STATUS` value to `free`.
if ( ! $membership->is_paid() ) {
$request_args['merge_fields']['STATUS'] = 'free';
}
return $request_args;
}
add_filter( 'rcp_mailchimp_pro_subscribe_args', 'ag_rcp_mailchimp_pro_subscribe_args', 10, 3 );
/**
* When the membership status is updated, change the `STATUS` merge var to `free` if the membership isn't paid.
*
* @param array $request_args
* @param int $user_id
* @param string $list_id
*
* @return array
*/
function ag_rcp_mailchimp_pro_change_membership_status_args( $request_args, $user_id, $list_id ) {
// Bail if RCP is not on 3.0+
if ( ! defined( 'RCP_PLUGIN_VERSION' ) || version_compare( RCP_PLUGIN_VERSION, '3.0', '<' ) ) {
return $request_args;
}
// Bail if status isn't being set to `active`.
if ( 'active' != $request_args['merge_fields']['STATUS'] ) {
return $request_args;
}
$customer = rcp_get_customer_by_user_id( $user_id );
// No customer - bail.
if ( empty( $customer ) ) {
return $request_args;
}
$membership = rcp_get_customer_single_membership( $customer->get_id() );
// No membership - bail.
if ( empty( $membership ) ) {
return $request_args;
}
// If the membership is not paid, change the `STATUS` value to `free`.
if ( ! $membership->is_paid() ) {
$request_args['merge_fields']['STATUS'] = 'free';
}
return $request_args;
}
add_filter( 'rcp_mailchimp_pro_change_membership_status_args', 'ag_rcp_mailchimp_pro_change_membership_status_args', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment