| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Wishlist class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Wishlist Integration |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Wishlist { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor. Registers required hooks with WishList Member. |
| 19 |
* |
| 20 |
* @since 1.9.6 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
// When a user has levels added or registers, perform actions on ConvertKit. |
| 25 |
add_action( 'wishlistmember_add_user_levels', array( $this, 'add_user_levels' ), 10, 2 ); |
| 26 |
|
| 27 |
// When a user has levels removed check, perform actions on ConvertKit. |
| 28 |
add_action( 'wishlistmember_remove_user_levels', array( $this, 'remove_user_levels' ), 10, 2 ); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* When a user has levels added or registers, subscribe them to ConvertKit |
| 34 |
* and optionally assign a Form, Tag or Sequence, based on the WishList Member Level |
| 35 |
* setting. |
| 36 |
* |
| 37 |
* @since 1.9.6 |
| 38 |
* |
| 39 |
* @param string $member_id ID for member that has just had levels added. |
| 40 |
* @param array $levels Levels to which member was added. |
| 41 |
*/ |
| 42 |
public function add_user_levels( $member_id, $levels ) { |
| 43 |
|
| 44 |
$this->manage_member( $member_id, $levels, 'add' ); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* When a user has levels removed, unsubscribe or subscribe them to ConvertKit |
| 50 |
* and optionally assign a Form, Tag or Sequence, based on the WishList Member Level |
| 51 |
* setting. |
| 52 |
* |
| 53 |
* @since 1.9.6 |
| 54 |
* |
| 55 |
* @param string $member_id ID for member that has just had levels added. |
| 56 |
* @param array $levels Levels to which member was added. |
| 57 |
*/ |
| 58 |
public function remove_user_levels( $member_id, $levels ) { |
| 59 |
|
| 60 |
$this->manage_member( $member_id, $levels, 'remove' ); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* When a user has levels added or registers, subscribe them to a ConvertKit Form |
| 66 |
* if the given WishList Member Level is mapped to a ConvertKit Form. |
| 67 |
* |
| 68 |
* @since 1.9.6 |
| 69 |
* |
| 70 |
* @param string $member_id ID for member that has just had levels added. |
| 71 |
* @param array $levels Levels to which member was added. |
| 72 |
* @param string $wlm_action WishList Member action (add,remove). |
| 73 |
*/ |
| 74 |
private function manage_member( $member_id, $levels, $wlm_action = 'add' ) { |
| 75 |
|
| 76 |
// Get WishList Member. |
| 77 |
$member = $this->get_member( $member_id ); |
| 78 |
|
| 79 |
// Bail if no Member was returned. |
| 80 |
if ( ! $member ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
// Bail if the API hasn't been configured. |
| 85 |
$settings = new ConvertKit_Settings(); |
| 86 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Initialize the API. |
| 91 |
$api = new ConvertKit_API_V4( |
| 92 |
CONVERTKIT_OAUTH_CLIENT_ID, |
| 93 |
CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, |
| 94 |
$settings->get_access_token(), |
| 95 |
$settings->get_refresh_token(), |
| 96 |
$settings->debug_enabled(), |
| 97 |
'wishlist_member' |
| 98 |
); |
| 99 |
|
| 100 |
// Check for temp email. |
| 101 |
if ( preg_match( '/temp_[a-f0-9]{32}/', $member['user_email'] ) ) { |
| 102 |
$email = $member['wlm_origemail']; |
| 103 |
} else { |
| 104 |
$email = $member['user_email']; |
| 105 |
} |
| 106 |
|
| 107 |
// Extract the first name. |
| 108 |
$first_name = ''; |
| 109 |
if ( isset( $member['display_name'] ) && ! empty( $member['display_name'] ) ) { |
| 110 |
$name = explode( ' ', $member['display_name'] ); |
| 111 |
$first_name = $name[0]; |
| 112 |
} |
| 113 |
|
| 114 |
// Initialize Wishlist Settings class. |
| 115 |
$wlm_settings = new ConvertKit_Wishlist_Settings(); |
| 116 |
|
| 117 |
// Iterate through the member's levels. |
| 118 |
foreach ( $levels as $wlm_level_id ) { |
| 119 |
// Fetch action setting. |
| 120 |
switch ( $wlm_action ) { |
| 121 |
case 'add': |
| 122 |
$setting = $wlm_settings->get_convertkit_add_setting_by_wishlist_member_level_id( $wlm_level_id ); |
| 123 |
break; |
| 124 |
|
| 125 |
case 'remove': |
| 126 |
$setting = $wlm_settings->get_convertkit_remove_setting_by_wishlist_member_level_id( $wlm_level_id ); |
| 127 |
break; |
| 128 |
|
| 129 |
default: |
| 130 |
$setting = false; |
| 131 |
break; |
| 132 |
} |
| 133 |
|
| 134 |
// If no setting / action exists, skip this level. |
| 135 |
if ( ! $setting ) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
// If the resource setting is 'unsubscribe', just unsubscribe the member. |
| 140 |
if ( $setting === 'unsubscribe' ) { |
| 141 |
// Get subscriber ID. |
| 142 |
$subscriber_id = $api->get_subscriber_id( $email ); |
| 143 |
|
| 144 |
// Bail if an error occurred e.g. no subscriber exists. |
| 145 |
if ( is_wp_error( $subscriber_id ) ) { |
| 146 |
return $subscriber_id; |
| 147 |
} |
| 148 |
|
| 149 |
// Unsubscribe. |
| 150 |
$api->unsubscribe( $subscriber_id ); |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
// If the resource setting is 'subscribe', create the subscriber in an active state and don't assign to a resource. |
| 155 |
if ( $setting === 'subscribe' ) { |
| 156 |
$api->create_subscriber( $email, $first_name ); |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
// Extract resource type and ID from the setting. |
| 161 |
list( $resource_type, $resource_id ) = explode( ':', $setting ); |
| 162 |
|
| 163 |
// Cast ID. |
| 164 |
$resource_id = absint( $resource_id ); |
| 165 |
|
| 166 |
// Add the subscriber to the resource type (form, tag etc). |
| 167 |
switch ( $resource_type ) { |
| 168 |
|
| 169 |
/** |
| 170 |
* Form |
| 171 |
*/ |
| 172 |
case 'form': |
| 173 |
// Subscribe with inactive state. |
| 174 |
$subscriber = $api->create_subscriber( $email, $first_name, 'inactive' ); |
| 175 |
|
| 176 |
// If an error occurred, don't attempt to add the subscriber to the Form, as it won't work. |
| 177 |
if ( is_wp_error( $subscriber ) ) { |
| 178 |
break; |
| 179 |
} |
| 180 |
|
| 181 |
// For Legacy Forms, a different endpoint is used. |
| 182 |
$forms = new ConvertKit_Resource_Forms(); |
| 183 |
if ( $forms->is_legacy( $resource_id ) ) { |
| 184 |
$api->add_subscriber_to_legacy_form( $resource_id, $subscriber['subscriber']['id'] ); |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
// Add subscriber to form. |
| 189 |
$api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'] ); |
| 190 |
break; |
| 191 |
|
| 192 |
/** |
| 193 |
* Sequence |
| 194 |
*/ |
| 195 |
case 'sequence': |
| 196 |
// Subscribe. |
| 197 |
$subscriber = $api->create_subscriber( $email, $first_name ); |
| 198 |
|
| 199 |
// If an error occurred, don't attempt to add the subscriber to the Form, as it won't work. |
| 200 |
if ( is_wp_error( $subscriber ) ) { |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
// Add subscriber to sequence. |
| 205 |
$api->add_subscriber_to_sequence( $resource_id, $subscriber['subscriber']['id'] ); |
| 206 |
break; |
| 207 |
|
| 208 |
/** |
| 209 |
* Tag |
| 210 |
*/ |
| 211 |
case 'tag': |
| 212 |
// Subscribe with inactive state. |
| 213 |
$subscriber = $api->create_subscriber( $email, $first_name ); |
| 214 |
|
| 215 |
// If an error occurred, don't attempt to add the subscriber to the Form, as it won't work. |
| 216 |
if ( is_wp_error( $subscriber ) ) { |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
// Add subscriber to tag. |
| 221 |
$api->tag_subscriber( $resource_id, $subscriber['subscriber']['id'] ); |
| 222 |
break; |
| 223 |
|
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Gets a WLM member using the wlmapi functions |
| 231 |
* |
| 232 |
* @param string $id The member id. |
| 233 |
* @return bool|array |
| 234 |
*/ |
| 235 |
public function get_member( $id ) { |
| 236 |
|
| 237 |
// Get WishList Member. |
| 238 |
$wlm_get_member = wlmapi_get_member( $id ); |
| 239 |
|
| 240 |
// Bail if an error occurred. |
| 241 |
if ( 0 === $wlm_get_member['success'] ) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
// Return user's information. |
| 246 |
return $wlm_get_member['member'][0]['UserInfo']; |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
// Bootstrap. |
| 253 |
add_action( |
| 254 |
'convertkit_initialize_global', |
| 255 |
function () { |
| 256 |
|
| 257 |
new ConvertKit_Wishlist(); |
| 258 |
|
| 259 |
} |
| 260 |
); |
| 261 |
|