← All changes
|
includes/integrations/wishlist/class-convertkit-wishlist.php
+140
-112
2.2.2
→
3.4.4
View file →
| @@ -20,20 +20,20 @@ | ||
| 20 | 20 | * @since 1.9.6 |
| 21 | 21 | */ |
| 22 | 22 | public function __construct() { |
| 23 | 23 | |
| 24 | - // When a user has levels added or registers check for a mapping to a ConvertKit form. | |
| 24 | + // When a user has levels added or registers, perform actions on ConvertKit. | |
| 25 | 25 | add_action( 'wishlistmember_add_user_levels', array( $this, 'add_user_levels' ), 10, 2 ); |
| 26 | 26 | |
| 27 | - // When a user has levels removed check for a mapping to a ConvertKit tag, or if the subscriber | |
| 28 | - // should be removed from ConvertKit. | |
| 27 | + // When a user has levels removed check, perform actions on ConvertKit. | |
| 29 | 28 | add_action( 'wishlistmember_remove_user_levels', array( $this, 'remove_user_levels' ), 10, 2 ); |
| 30 | 29 | |
| 31 | 30 | } |
| 32 | 31 | |
| 33 | 32 | /** |
| 34 | - * When a user has levels added or registers, subscribe them to a ConvertKit Form | |
| 35 | - * if the given WishList Member Level is mapped to a ConvertKit Form. | |
| 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 | 36 | * |
| 37 | 37 | * @since 1.9.6 |
| 38 | 38 | * |
| 39 | 39 | * @param string $member_id ID for member that has just had levels added. |
| @@ -40,42 +40,39 @@ | ||
| 40 | 40 | * @param array $levels Levels to which member was added. |
| 41 | 41 | */ |
| 42 | 42 | public function add_user_levels( $member_id, $levels ) { |
| 43 | 43 | |
| 44 | - // Get WishList Member. | |
| 45 | - $member = $this->get_member( $member_id ); | |
| 44 | + $this->manage_member( $member_id, $levels, 'add' ); | |
| 46 | 45 | |
| 47 | - // Bail if no Member was returned. | |
| 48 | - if ( ! $member ) { | |
| 49 | - return; | |
| 50 | - } | |
| 46 | + } | |
| 51 | 47 | |
| 52 | - // Initialize Wishlist Settings class. | |
| 53 | - $wlm_settings = new ConvertKit_Wishlist_Settings(); | |
| 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 ) { | |
| 54 | 59 | |
| 55 | - // Iterate through the member's levels. | |
| 56 | - foreach ( $levels as $wlm_level_id ) { | |
| 57 | - // If no ConvertKit Form is mapped to this level, skip it. | |
| 58 | - $convertkit_form_id = $wlm_settings->get_convertkit_form_id_by_wishlist_member_level_id( $wlm_level_id ); | |
| 59 | - if ( ! $convertkit_form_id ) { | |
| 60 | - continue; | |
| 61 | - } | |
| 60 | + $this->manage_member( $member_id, $levels, 'remove' ); | |
| 62 | 61 | |
| 63 | - // Subscribe the user to the ConvertKit Form for this level. | |
| 64 | - $this->member_resource_subscribe( $member, $convertkit_form_id ); | |
| 65 | - } | |
| 66 | - | |
| 67 | 62 | } |
| 68 | 63 | |
| 69 | 64 | /** |
| 70 | - * Note: Form level unsubscribe is not available in v3 of the API. | |
| 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. | |
| 71 | 67 | * |
| 72 | - * Callback function for wishlistmember_remove_user_levels action | |
| 68 | + * @since 1.9.6 | |
| 73 | 69 | * |
| 74 | - * @param string $member_id ID for member that has just had levels removed. | |
| 75 | - * @param array $levels Levels from which member was removed. | |
| 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). | |
| 76 | 73 | */ |
| 77 | - public function remove_user_levels( $member_id, $levels ) { | |
| 74 | + private function manage_member( $member_id, $levels, $wlm_action = 'add' ) { | |
| 78 | 75 | |
| 79 | 76 | // Get WishList Member. |
| 80 | 77 | $member = $this->get_member( $member_id ); |
| 81 | 78 | |
| @@ -83,48 +80,23 @@ | ||
| 83 | 80 | if ( ! $member ) { |
| 84 | 81 | return; |
| 85 | 82 | } |
| 86 | 83 | |
| 87 | - // Initialize Wishlist Settings class. | |
| 88 | - $wlm_settings = new ConvertKit_Wishlist_Settings(); | |
| 89 | - | |
| 90 | - // Iterate through the member's levels. | |
| 91 | - foreach ( $levels as $wlm_level_id ) { | |
| 92 | - // If no ConvertKit Tag is mapped to this level, skip it. | |
| 93 | - $convertkit_tag_id = $wlm_settings->get_convertkit_tag_id_by_wishlist_member_level_id( $wlm_level_id ); | |
| 94 | - if ( ! $convertkit_tag_id ) { | |
| 95 | - continue; | |
| 96 | - } | |
| 97 | - | |
| 98 | - // If the Tag ID is 'unsubscribe', unsubscribe the member from tags. | |
| 99 | - if ( $convertkit_tag_id === 'unsubscribe' ) { | |
| 100 | - $this->member_resource_unsubscribe( $member ); | |
| 101 | - continue; | |
| 102 | - } | |
| 103 | - | |
| 104 | - // Tag the member. | |
| 105 | - $this->member_tag( $member, $convertkit_tag_id ); | |
| 106 | - } | |
| 107 | - | |
| 108 | - } | |
| 109 | - | |
| 110 | - /** | |
| 111 | - * Subscribes a member to a ConvertKit Form. | |
| 112 | - * | |
| 113 | - * @param array $member UserInfo from WishList Member. | |
| 114 | - * @param int $form_id ConvertKit Form ID. | |
| 115 | - * @return bool|WP_Error|array | |
| 116 | - */ | |
| 117 | - public function member_resource_subscribe( $member, $form_id ) { | |
| 118 | - | |
| 119 | 84 | // Bail if the API hasn't been configured. |
| 120 | 85 | $settings = new ConvertKit_Settings(); |
| 121 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 122 | - return false; | |
| 86 | + if ( ! $settings->has_access_and_refresh_token() ) { | |
| 87 | + return; | |
| 123 | 88 | } |
| 124 | 89 | |
| 125 | 90 | // Initialize the API. |
| 126 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'wishlist_member' ); | |
| 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 | + ); | |
| 127 | 99 | |
| 128 | 100 | // Check for temp email. |
| 129 | 101 | if ( preg_match( '/temp_[a-f0-9]{32}/', $member['user_email'] ) ) { |
| 130 | 102 | $email = $member['wlm_origemail']; |
| @@ -138,65 +110,121 @@ | ||
| 138 | 110 | $name = explode( ' ', $member['display_name'] ); |
| 139 | 111 | $first_name = $name[0]; |
| 140 | 112 | } |
| 141 | 113 | |
| 142 | - // Note Wishlist Member combines first and last name into 'display_name'. | |
| 143 | - return $api->form_subscribe( | |
| 144 | - $form_id, | |
| 145 | - $email, | |
| 146 | - $first_name | |
| 147 | - ); | |
| 148 | - } | |
| 114 | + // Initialize Wishlist Settings class. | |
| 115 | + $wlm_settings = new ConvertKit_Wishlist_Settings(); | |
| 149 | 116 | |
| 150 | - /** | |
| 151 | - * Unsubscribes a member from ConvertKit. | |
| 152 | - * | |
| 153 | - * @param array $member UserInfo from WishList Member. | |
| 154 | - * @return bool|WP_Error|array | |
| 155 | - */ | |
| 156 | - public function member_resource_unsubscribe( $member ) { | |
| 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; | |
| 157 | 124 | |
| 158 | - // Bail if the API hasn't been configured. | |
| 159 | - $settings = new ConvertKit_Settings(); | |
| 160 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 161 | - return false; | |
| 162 | - } | |
| 125 | + case 'remove': | |
| 126 | + $setting = $wlm_settings->get_convertkit_remove_setting_by_wishlist_member_level_id( $wlm_level_id ); | |
| 127 | + break; | |
| 163 | 128 | |
| 164 | - // Initialize the API. | |
| 165 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'wishlist_member' ); | |
| 129 | + default: | |
| 130 | + $setting = false; | |
| 131 | + break; | |
| 132 | + } | |
| 166 | 133 | |
| 167 | - // Check for temp email. | |
| 168 | - if ( preg_match( '/temp_[a-f0-9]{32}/', $member['user_email'] ) ) { | |
| 169 | - $email = $member['wlm_origemail']; | |
| 170 | - } else { | |
| 171 | - $email = $member['user_email']; | |
| 172 | - } | |
| 134 | + // If no setting / action exists, skip this level. | |
| 135 | + if ( ! $setting ) { | |
| 136 | + continue; | |
| 137 | + } | |
| 173 | 138 | |
| 174 | - // Unsubscribe the email. | |
| 175 | - return $api->unsubscribe( $email ); | |
| 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 ); | |
| 176 | 143 | |
| 177 | - } | |
| 144 | + // Bail if an error occurred e.g. no subscriber exists. | |
| 145 | + if ( is_wp_error( $subscriber_id ) ) { | |
| 146 | + return $subscriber_id; | |
| 147 | + } | |
| 178 | 148 | |
| 179 | - /** | |
| 180 | - * Tag a ConvertKit User with the given Tag ID. | |
| 181 | - * | |
| 182 | - * @param array $member UserInfo from WishList Member. | |
| 183 | - * @param int $tag_id ConvertKit Tag ID. | |
| 184 | - * @return bool|WP_Error|array | |
| 185 | - */ | |
| 186 | - public function member_tag( $member, $tag_id ) { | |
| 149 | + // Unsubscribe. | |
| 150 | + $api->unsubscribe( $subscriber_id ); | |
| 151 | + continue; | |
| 152 | + } | |
| 187 | 153 | |
| 188 | - // Bail if the API hasn't been configured. | |
| 189 | - $settings = new ConvertKit_Settings(); | |
| 190 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 191 | - return false; | |
| 192 | - } | |
| 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 | + } | |
| 193 | 159 | |
| 194 | - // Initialize the API. | |
| 195 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'wishlist_member' ); | |
| 160 | + // Extract resource type and ID from the setting. | |
| 161 | + list( $resource_type, $resource_id ) = explode( ':', $setting ); | |
| 196 | 162 | |
| 197 | - return $api->tag_subscribe( $tag_id, $member['user_email'] ); | |
| 163 | + // Cast ID. | |
| 164 | + $resource_id = absint( $resource_id ); | |
| 198 | 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 | + | |
| 199 | 227 | } |
| 200 | 228 | |
| 201 | 229 | /** |
| 202 | 230 | * Gets a WLM member using the wlmapi functions |
| @@ -208,9 +236,9 @@ | ||
| 208 | 236 | |
| 209 | 237 | // Get WishList Member. |
| 210 | 238 | $wlm_get_member = wlmapi_get_member( $id ); |
| 211 | 239 | |
| 212 | - // Bail if an error occured. | |
| 240 | + // Bail if an error occurred. | |
| 213 | 241 | if ( 0 === $wlm_get_member['success'] ) { |
| 214 | 242 | return false; |
| 215 | 243 | } |
| 216 | 244 | |
| @@ -223,9 +251,9 @@ | ||
| 223 | 251 | |
| 224 | 252 | // Bootstrap. |
| 225 | 253 | add_action( |
| 226 | 254 | 'convertkit_initialize_global', |
| 227 | - function() { | |
| 255 | + function () { | |
| 228 | 256 | |
| 229 | 257 | new ConvertKit_Wishlist(); |
| 230 | 258 | |
| 231 | 259 | } |