| 1 |
<?php |
| 2 |
|
| 3 |
require_once plugin_dir_path( __FILE__ ) . "../../lib/convertkit-api.php"; |
| 4 |
|
| 5 |
if(!class_exists('ConvertKitWishlistIntegration')) { |
| 6 |
class ConvertKitWishlistIntegration { |
| 7 |
protected $api; |
| 8 |
protected $options; |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
$general_options = get_option('_wp_convertkit_settings'); |
| 12 |
$this->options = get_option('_wp_convertkit_integration_wishlistmember_settings'); |
| 13 |
$api_key = $general_options && array_key_exists("api_key", $general_options) ? $general_options['api_key'] : null; |
| 14 |
$this->api = new ConvertKitAPI($api_key); |
| 15 |
|
| 16 |
add_action( |
| 17 |
'wishlistmember_add_user_levels', // hook |
| 18 |
array($this, 'add_user_levels'), // function to call |
| 19 |
null, // priority (default is fine) |
| 20 |
2 // number of arguments passed |
| 21 |
); |
| 22 |
|
| 23 |
add_action( |
| 24 |
'wishlistmember_remove_user_levels', // hook |
| 25 |
array($this, 'remove_user_levels'), // function to call |
| 26 |
null, // priority |
| 27 |
2 // number of arguments passed |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Callback function for wishlistmember_add_user_levels action |
| 33 |
* |
| 34 |
* @param string $member_id ID for member that has just had levels added |
| 35 |
* @param array $levels Levels to which member was added |
| 36 |
*/ |
| 37 |
public function add_user_levels($member_id, $levels) { |
| 38 |
$member = $this->get_member($member_id); |
| 39 |
|
| 40 |
foreach ($levels as $wlm_level_id) { |
| 41 |
if (!isset($this->options[$wlm_level_id . '_form'])) continue; |
| 42 |
|
| 43 |
$this->member_resource_subscribe( |
| 44 |
$member, |
| 45 |
$this->options[$wlm_level_id . '_form'] |
| 46 |
); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Callback function for wishlistmember_remove_user_levels action |
| 52 |
* |
| 53 |
* @param string $member_id ID for member that has just had levels removed |
| 54 |
* @param array $levels Levels from which member was removed |
| 55 |
*/ |
| 56 |
public function remove_user_levels($member_id, $levels) { |
| 57 |
$member = $this->get_member($member_id); |
| 58 |
|
| 59 |
foreach ($levels as $wlm_level_id) { |
| 60 |
if ( |
| 61 |
isset($this->options[$wlm_level_id . '_form']) |
| 62 |
&& isset($this->options[$wlm_level_id . '_unsubscribe']) |
| 63 |
&& $this->options[$wlm_level_id . '_unsubscribe'] == '1' |
| 64 |
) { |
| 65 |
$this->member_resource_unsubscribe( |
| 66 |
$member, |
| 67 |
$this->options[$wlm_level_id . '_form'] |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Subscribes a member to a ConvertKit resource |
| 75 |
* |
| 76 |
* @param array $member UserInfo from WishList Member |
| 77 |
* @param string $form_id ConvertKit form id |
| 78 |
* @return object Response object from API |
| 79 |
*/ |
| 80 |
public function member_resource_subscribe($member, $form_id) { |
| 81 |
return $this->api->form_subscribe( |
| 82 |
$form_id, |
| 83 |
array( |
| 84 |
'email' => $member['user_email'], |
| 85 |
'fname' => $member['display_name'] |
| 86 |
) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Unsubscribes a member from a ConvertKit resource |
| 92 |
* |
| 93 |
* @param array $member UserInfo from WishList Member |
| 94 |
* @param string $form_id ConvertKit form id |
| 95 |
* @return object Response object from API |
| 96 |
*/ |
| 97 |
public function member_resource_unsubscribe($member, $form_id) { |
| 98 |
return $this->api->form_unsubscribe( |
| 99 |
$form_id, |
| 100 |
array( |
| 101 |
'email' => $member['user_email'] |
| 102 |
) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Gets a WLM member using the wlmapi functions |
| 108 |
* |
| 109 |
* @param string $id The member id |
| 110 |
* @return array The member fields from the API request |
| 111 |
*/ |
| 112 |
public function get_member($id) { |
| 113 |
if (!function_exists('wlmapi_get_member')) return false; |
| 114 |
|
| 115 |
$wlm_get_member = wlmapi_get_member($id); |
| 116 |
|
| 117 |
if ($wlm_get_member['success'] == 0) return false; |
| 118 |
|
| 119 |
return $wlm_get_member['member'][0]['UserInfo']; |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
$convertkit_wishlist_integration = new ConvertKitWishlistIntegration; |
| 125 |
} |
| 126 |
|