images
3 months ago
convertkit.php
3 months ago
hustle-convertkit-api-intefrace.php
3 months ago
hustle-convertkit-api-v2.php
3 months ago
hustle-convertkit-api.php
3 months ago
hustle-convertkit-form-hooks.php
3 months ago
hustle-convertkit-form-settings.php
5 months ago
hustle-convertkit-v2.php
3 months ago
hustle-convertkit.php
3 months ago
hustle-convertkit-api.php
336 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_ConvertKit_Api class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * ConvertKit API |
| 10 | * |
| 11 | * @class Hustle_ConvertKit_Api |
| 12 | **/ |
| 13 | class Hustle_ConvertKit_Api implements Hustle_ConvertKit_Api_Interface { |
| 14 | |
| 15 | /** |
| 16 | * Api key |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | private $api_key; |
| 21 | /** |
| 22 | * Api secret |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private $api_secret; |
| 27 | /** |
| 28 | * Endpoint |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | private $endpoint = 'https://api.convertkit.com/v3/'; |
| 33 | |
| 34 | /** |
| 35 | * Constructs class with required data |
| 36 | * |
| 37 | * Hustle_ConvertKit_Api constructor. |
| 38 | * |
| 39 | * @param string $api_key Api key. |
| 40 | * @param string $api_secret Api secret. |
| 41 | */ |
| 42 | public function __construct( $api_key, $api_secret = '' ) { |
| 43 | $this->api_key = $api_key; |
| 44 | $this->api_secret = $api_secret; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Sends request to the endpoint url with the provided $action |
| 49 | * |
| 50 | * @param string $action rest action. |
| 51 | * @param string $verb Verb. |
| 52 | * @param array $args Args. |
| 53 | * @return object|WP_Error |
| 54 | */ |
| 55 | private function request( $action, $verb = 'GET', $args = array() ) { |
| 56 | $url = trailingslashit( $this->endpoint ) . $action; |
| 57 | |
| 58 | $_args = array( |
| 59 | 'method' => $verb, |
| 60 | 'headers' => array( |
| 61 | 'X-Auth-Token' => 'api-key ' . $this->api_key, |
| 62 | 'Content-Type' => 'application/json;charset=utf-8', |
| 63 | ), |
| 64 | ); |
| 65 | |
| 66 | if ( 'GET' === $verb ) { |
| 67 | $url .= ( '?' . http_build_query( $args ) ); |
| 68 | } else { |
| 69 | $_args['body'] = wp_json_encode( $args['body'] ); |
| 70 | } |
| 71 | |
| 72 | $res = wp_remote_request( $url, $_args ); |
| 73 | |
| 74 | // logging data. |
| 75 | $utils = Hustle_Provider_Utils::get_instance(); |
| 76 | $utils->last_url_request = $url; |
| 77 | $utils->last_data_sent = $_args; |
| 78 | $utils->last_data_received = $res; |
| 79 | |
| 80 | if ( ! is_wp_error( $res ) && is_array( $res ) ) { |
| 81 | |
| 82 | if ( $res['response']['code'] <= 204 ) { |
| 83 | return json_decode( wp_remote_retrieve_body( $res ) ); |
| 84 | } |
| 85 | |
| 86 | $err = new WP_Error(); |
| 87 | $err->add( $res['response']['code'], $res['response']['message'] ); |
| 88 | return $err; |
| 89 | } |
| 90 | |
| 91 | return $res; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sends rest GET request |
| 96 | * |
| 97 | * @param string $action Action. |
| 98 | * @param array $args Args. |
| 99 | * @return array|mixed|object|WP_Error |
| 100 | */ |
| 101 | private function get( $action, $args = array() ) { |
| 102 | return $this->request( $action, 'GET', $args ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sends rest POST request |
| 107 | * |
| 108 | * @param string $action Action. |
| 109 | * @param array $args Args. |
| 110 | * @return array|mixed|object|WP_Error |
| 111 | */ |
| 112 | private function post( $action, $args = array() ) { |
| 113 | return $this->request( $action, 'POST', $args ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Sends rest PUT request |
| 118 | * |
| 119 | * @param string $action Action. |
| 120 | * @param array $args Args. |
| 121 | * @return array|mixed|object|WP_Error |
| 122 | */ |
| 123 | private function put( $action, $args = array() ) { |
| 124 | return $this->request( $action, 'PUT', $args ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Retrieves ConvertKit forms as array of objects |
| 129 | * |
| 130 | * @return array|WP_Error |
| 131 | */ |
| 132 | public function get_forms() { |
| 133 | $forms = $this->get( |
| 134 | 'forms', |
| 135 | array( |
| 136 | 'api_key' => $this->api_key, |
| 137 | ) |
| 138 | ); |
| 139 | if ( is_wp_error( $forms ) ) { |
| 140 | return $forms; |
| 141 | } |
| 142 | if ( ! isset( $forms->forms ) ) { |
| 143 | return new WP_Error( 'forms_not_found', __( 'Not found forms with this api key.', 'hustle' ) ); |
| 144 | } |
| 145 | return $forms->forms; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Retrieves ConvertKit subscribers as array of objects |
| 150 | * |
| 151 | * @return array|WP_Error |
| 152 | */ |
| 153 | public function get_subscribers() { |
| 154 | $subscribers = $this->get( |
| 155 | 'subscribers', |
| 156 | array( |
| 157 | 'api_secret' => $this->api_secret, |
| 158 | ) |
| 159 | ); |
| 160 | if ( is_wp_error( $subscribers ) ) { |
| 161 | return $subscribers; |
| 162 | } |
| 163 | return $subscribers->subscribers; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Retrieves ConvertKit form's custom fields as array of objects |
| 168 | * |
| 169 | * @return array|WP_Error |
| 170 | */ |
| 171 | public function get_form_custom_fields() { |
| 172 | return $this->get( |
| 173 | 'custom_fields', |
| 174 | array( |
| 175 | 'api_key' => $this->api_key, |
| 176 | ) |
| 177 | )->custom_fields; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Add new custom fields to subscription |
| 182 | * |
| 183 | * @param array $field_data Fields data. |
| 184 | * @return array|mixed|object|WP_Error |
| 185 | */ |
| 186 | public function create_custom_fields( $field_data ) { |
| 187 | $url = 'custom_fields'; |
| 188 | $args = array( |
| 189 | 'body' => $field_data, |
| 190 | ); |
| 191 | $res = $this->post( $url, $args ); |
| 192 | |
| 193 | return empty( $res ) ? __( 'Successfully added custom field', 'hustle' ) : $res; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Add new subscriber |
| 198 | * |
| 199 | * @param string $form_id Form ID. |
| 200 | * @param array $data Data. |
| 201 | * @return array|mixed|object|WP_Error |
| 202 | */ |
| 203 | public function subscribe( $form_id, $data ) { |
| 204 | $url = 'forms/' . $form_id . '/subscribe'; |
| 205 | $args = array( |
| 206 | 'body' => $data, |
| 207 | ); |
| 208 | $res = $this->post( $url, $args ); |
| 209 | |
| 210 | return empty( $res ) ? __( 'Successful subscription', 'hustle' ) : $res; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Update subscriber |
| 215 | * |
| 216 | * @since 4.0 |
| 217 | * |
| 218 | * @param string $id ID. |
| 219 | * @param array $data Data. |
| 220 | * @return array|mixed|object|WP_Error |
| 221 | */ |
| 222 | public function update_subscriber( $id, $data ) { |
| 223 | $url = 'subscribers/' . $id; |
| 224 | $data['api_secret'] = $this->api_secret; |
| 225 | $args = array( |
| 226 | 'body' => $data, |
| 227 | ); |
| 228 | $res = $this->put( $url, $args ); |
| 229 | |
| 230 | return empty( $res ) ? __( 'Successful subscription', 'hustle' ) : $res; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Delete subscriber from the list |
| 235 | * |
| 236 | * @param string $list_id List ID. |
| 237 | * @param string $email Email. |
| 238 | * |
| 239 | * @return bool |
| 240 | */ |
| 241 | public function delete_email( $list_id, $email ) { |
| 242 | $args = array( |
| 243 | 'body' => array( |
| 244 | 'email' => $email, |
| 245 | 'form' => $list_id, // Actually they don't support this argument and have ability only cancel subscriptions to all forms. |
| 246 | 'api_secret' => $this->api_secret, |
| 247 | ), |
| 248 | ); |
| 249 | $res = $this->put( 'unsubscribe', $args ); |
| 250 | |
| 251 | return ! is_wp_error( $res ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Verify if an email is already a subscriber. |
| 256 | * |
| 257 | * @param (string) $email Email. |
| 258 | * |
| 259 | * @return (object) Returns data of existing subscriber if exist otherwise false. |
| 260 | **/ |
| 261 | public function is_subscriber( $email ) { |
| 262 | $url = 'subscribers'; |
| 263 | $args = array( |
| 264 | 'api_key' => $this->api_key, |
| 265 | 'api_secret' => $this->api_secret, |
| 266 | 'email_address' => $email, |
| 267 | ); |
| 268 | |
| 269 | $res = $this->get( $url, $args ); |
| 270 | |
| 271 | return ! is_wp_error( $res ) && ! empty( $res->subscribers ) ? array_shift( $res->subscribers ) : false; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Verify if an email is already a subscriber in a form. |
| 276 | * |
| 277 | * @param string $email Email. |
| 278 | * @param integer $form_id Form ID. |
| 279 | * |
| 280 | * @return boolean True if the subscriber exists, otherwise false. |
| 281 | **/ |
| 282 | public function is_form_subscriber( $email, $form_id ) { |
| 283 | $url = 'forms/' . $form_id . '/subscriptions'; |
| 284 | $args = array( |
| 285 | 'api_secret' => $this->api_secret, |
| 286 | ); |
| 287 | $exist = false; |
| 288 | $res = $this->get( $url, $args ); |
| 289 | |
| 290 | $utils = Hustle_Provider_Utils::get_instance(); |
| 291 | $utils->last_data_received = $res; |
| 292 | $utils->last_url_request = trailingslashit( $this->endpoint ) . $url; |
| 293 | $utils->last_data_sent = $args; |
| 294 | |
| 295 | if ( is_wp_error( $res ) ) { |
| 296 | Hustle_Provider_Utils::maybe_log( 'There was an error retrieving the subscribers from Convertkit: ' . $res->get_error_message() ); |
| 297 | return false; |
| 298 | } elseif ( empty( $res->subscriptions ) ) { |
| 299 | return false; |
| 300 | } else { |
| 301 | $subscriptions = wp_list_pluck( $res->subscriptions, 'subscriber' ); |
| 302 | $subscribers = wp_list_pluck( $subscriptions, 'email_address' ); |
| 303 | $subscribers_id = wp_list_pluck( $subscriptions, 'id' ); |
| 304 | $exist = in_array( $email, $subscribers, true ) ? $subscribers_id[ array_search( $email, $subscribers, true ) ] : false; |
| 305 | if ( false === $exist && $res->total_pages > 1 ) { |
| 306 | for ( $i = 2; $i <= $res->total_pages; $i++ ) { |
| 307 | |
| 308 | $url = 'forms/' . $form_id . '/subscriptions'; |
| 309 | $args = array( |
| 310 | 'api_secret' => $this->api_secret, |
| 311 | 'page' => $i, |
| 312 | ); |
| 313 | $res = $this->get( $url, $args ); |
| 314 | $utils = Hustle_Provider_Utils::get_instance(); |
| 315 | $utils->last_data_received = $res; |
| 316 | $utils->last_url_request = trailingslashit( $this->endpoint ) . $url; |
| 317 | $utils->last_data_sent = $args; |
| 318 | |
| 319 | $subscriptions = wp_list_pluck( $res->subscriptions, 'subscriber' ); |
| 320 | $subscribers = wp_list_pluck( $subscriptions, 'email_address' ); |
| 321 | $subscribers_id = wp_list_pluck( $subscriptions, 'id' ); |
| 322 | |
| 323 | if ( in_array( $email, $subscribers, true ) ) { |
| 324 | $exist = $subscribers_id[ array_search( $email, $subscribers, true ) ]; |
| 325 | return $exist; |
| 326 | } |
| 327 | } |
| 328 | } else { |
| 329 | return $exist; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return $exist; |
| 334 | } |
| 335 | } |
| 336 |