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-v2.php
331 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_ConvertKit_V2 class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'Hustle_ConvertKit_V2' ) ) : |
| 9 | |
| 10 | include_once 'hustle-convertkit-api-v2.php'; |
| 11 | |
| 12 | /** |
| 13 | * ConvertKit V2 Email Integration (API Key only) |
| 14 | * |
| 15 | * @class Hustle_ConvertKit_V2 |
| 16 | * @version 7.8.11 |
| 17 | **/ |
| 18 | class Hustle_ConvertKit_V2 extends Hustle_ConvertKit { |
| 19 | |
| 20 | /** |
| 21 | * Api |
| 22 | * |
| 23 | * @var Hustle_ConvertKit_API_V2 |
| 24 | */ |
| 25 | protected static $api; |
| 26 | |
| 27 | /** |
| 28 | * Version |
| 29 | * |
| 30 | * @since 7.8.11 |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $version = '2.0'; |
| 34 | |
| 35 | /** |
| 36 | * Array of options which should exist for confirming that settings are completed |
| 37 | * |
| 38 | * @since 7.8.11 |
| 39 | * @var array |
| 40 | */ |
| 41 | protected $completion_options = array( 'api_key' ); |
| 42 | |
| 43 | /** |
| 44 | * Provider constructor. |
| 45 | */ |
| 46 | public function __construct() { |
| 47 | $this->icon_2x = plugin_dir_url( __FILE__ ) . 'images/icon.png'; |
| 48 | $this->logo_2x = plugin_dir_url( __FILE__ ) . 'images/logo.jpg'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Configure the API key settings. Global settings. |
| 53 | * |
| 54 | * @since 7.8.11 |
| 55 | * |
| 56 | * @param array $submitted_data Submitted data. |
| 57 | * @return array |
| 58 | */ |
| 59 | public function configure_api_key( $submitted_data ) { |
| 60 | $has_errors = false; |
| 61 | $default_data = array( |
| 62 | 'api_key' => '', |
| 63 | 'name' => '', |
| 64 | ); |
| 65 | $current_data = $this->get_current_data( $default_data, $submitted_data ); |
| 66 | $is_submit = isset( $submitted_data['api_key'] ); |
| 67 | $global_multi_id = $this->get_global_multi_id( $submitted_data ); |
| 68 | |
| 69 | $api_key_valid = true; |
| 70 | |
| 71 | if ( $is_submit ) { |
| 72 | |
| 73 | $api_key_valid = ! empty( $current_data['api_key'] ); |
| 74 | $api_key_validated = $api_key_valid && $this->validate_credentials( $submitted_data['api_key'] ); |
| 75 | |
| 76 | if ( ! $api_key_validated ) { |
| 77 | $error_message = $this->provider_connection_falied(); |
| 78 | $api_key_valid = false; |
| 79 | $has_errors = true; |
| 80 | } |
| 81 | |
| 82 | if ( ! $has_errors ) { |
| 83 | $settings_to_save = array( |
| 84 | 'api_key' => $current_data['api_key'], |
| 85 | 'name' => $current_data['name'], |
| 86 | 'version' => $this->version, |
| 87 | ); |
| 88 | // If not active, activate it. |
| 89 | // TODO: Wrap this in a friendlier method. |
| 90 | if ( Hustle_Provider_Utils::is_provider_active( $this->slug ) |
| 91 | || Hustle_Providers::get_instance()->activate_addon( $this->slug ) ) { |
| 92 | $this->save_multi_settings_values( $global_multi_id, $settings_to_save ); |
| 93 | } else { |
| 94 | $error_message = __( "Provider couldn't be activated.", 'hustle' ); |
| 95 | $has_errors = true; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if ( ! $has_errors ) { |
| 100 | |
| 101 | return array( |
| 102 | 'html' => Hustle_Provider_Utils::get_integration_modal_title_markup( __( 'Kit Added', 'hustle' ), __( 'You can now go to your pop-ups, slide-ins and embeds and assign them to this integration', 'hustle' ) ), |
| 103 | 'buttons' => array( |
| 104 | 'close' => array( |
| 105 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( __( 'Close', 'hustle' ), 'sui-button-ghost', 'close' ), |
| 106 | ), |
| 107 | ), |
| 108 | 'redirect' => false, |
| 109 | 'has_errors' => false, |
| 110 | 'notification' => array( |
| 111 | 'type' => 'success', |
| 112 | 'text' => '<strong>' . $this->get_title() . '</strong> ' . esc_html__( 'Successfully connected', 'hustle' ), |
| 113 | ), |
| 114 | ); |
| 115 | |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | $options = array( |
| 120 | array( |
| 121 | 'type' => 'wrapper', |
| 122 | 'class' => $api_key_valid ? '' : 'sui-form-field-error', |
| 123 | 'elements' => array( |
| 124 | 'label' => array( |
| 125 | 'type' => 'label', |
| 126 | 'for' => 'api_key', |
| 127 | 'value' => __( 'API Key', 'hustle' ), |
| 128 | ), |
| 129 | 'api_key' => array( |
| 130 | 'type' => 'text', |
| 131 | 'name' => 'api_key', |
| 132 | 'value' => $current_data['api_key'], |
| 133 | 'placeholder' => __( 'Enter API Key', 'hustle' ), |
| 134 | 'id' => 'api_key', |
| 135 | 'icon' => 'key', |
| 136 | ), |
| 137 | 'error' => array( |
| 138 | 'type' => 'error', |
| 139 | 'class' => $api_key_valid ? 'sui-hidden' : '', |
| 140 | 'value' => __( 'Please enter a valid ConvertKit API key', 'hustle' ), |
| 141 | ), |
| 142 | ), |
| 143 | ), |
| 144 | array( |
| 145 | 'type' => 'wrapper', |
| 146 | 'style' => 'margin-bottom: 0;', |
| 147 | 'elements' => array( |
| 148 | 'label' => array( |
| 149 | 'type' => 'label', |
| 150 | 'for' => 'instance-name-input', |
| 151 | 'value' => __( 'Identifier', 'hustle' ), |
| 152 | ), |
| 153 | 'name' => array( |
| 154 | 'type' => 'text', |
| 155 | 'name' => 'name', |
| 156 | 'value' => $current_data['name'], |
| 157 | 'placeholder' => __( 'E.g. Business Account', 'hustle' ), |
| 158 | 'id' => 'instance-name-input', |
| 159 | ), |
| 160 | 'message' => array( |
| 161 | 'type' => 'description', |
| 162 | 'value' => __( 'Helps distinguish your integrations if you have connected multiple accounts for this integration.', 'hustle' ), |
| 163 | ), |
| 164 | ), |
| 165 | ), |
| 166 | ); |
| 167 | |
| 168 | if ( $has_errors ) { |
| 169 | $error_notice = array( |
| 170 | 'type' => 'notice', |
| 171 | 'icon' => 'info', |
| 172 | 'class' => 'sui-notice-error', |
| 173 | 'value' => esc_html( $error_message ), |
| 174 | ); |
| 175 | array_unshift( $options, $error_notice ); |
| 176 | } |
| 177 | |
| 178 | $step_html = Hustle_Provider_Utils::get_integration_modal_title_markup( |
| 179 | __( 'Configure Kit', 'hustle' ), |
| 180 | sprintf( |
| 181 | /* translators: 1. opening 'a' tag to Kit account, 2. closing 'a' tag */ |
| 182 | __( 'Log in to your %1$sKit%2$s account to get your API Key.', 'hustle' ), |
| 183 | '<a href="https://app.kit.com/account_settings/developer_settings" target="_blank">', |
| 184 | '</a>' |
| 185 | ) |
| 186 | ); |
| 187 | $step_html .= Hustle_Provider_Utils::get_html_for_options( $options ); |
| 188 | |
| 189 | $is_edit = $this->settings_are_completed( $global_multi_id ); |
| 190 | if ( $is_edit ) { |
| 191 | $buttons = array( |
| 192 | 'disconnect' => array( |
| 193 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 194 | __( 'Disconnect', 'hustle' ), |
| 195 | 'sui-button-ghost', |
| 196 | 'disconnect', |
| 197 | true |
| 198 | ), |
| 199 | ), |
| 200 | 'save' => array( |
| 201 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 202 | __( 'Save', 'hustle' ), |
| 203 | '', |
| 204 | 'connect', |
| 205 | true |
| 206 | ), |
| 207 | ), |
| 208 | ); |
| 209 | } else { |
| 210 | $buttons = array( |
| 211 | 'connect' => array( |
| 212 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 213 | __( 'Connect', 'hustle' ), |
| 214 | 'sui-button-right', |
| 215 | 'connect', |
| 216 | true |
| 217 | ), |
| 218 | ), |
| 219 | ); |
| 220 | |
| 221 | } |
| 222 | |
| 223 | $response = array( |
| 224 | 'html' => $step_html, |
| 225 | 'buttons' => $buttons, |
| 226 | 'has_errors' => $has_errors, |
| 227 | ); |
| 228 | |
| 229 | return $response; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Validate the provided API key. |
| 234 | * |
| 235 | * @since 7.8.11 |
| 236 | * |
| 237 | * @param string $api_key Api key. |
| 238 | * @return bool |
| 239 | */ |
| 240 | private function validate_credentials( $api_key ) { |
| 241 | if ( empty( $api_key ) ) { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | try { |
| 246 | // Check if API key is valid. |
| 247 | $api = self::api( $api_key ); |
| 248 | $forms = $api->get_forms(); // check API key. |
| 249 | |
| 250 | if ( is_wp_error( $forms ) ) { |
| 251 | Hustle_Provider_Utils::maybe_log( __METHOD__, __( 'Invalid Kit API key.', 'hustle' ) ); |
| 252 | return false; |
| 253 | } |
| 254 | } catch ( Exception $e ) { |
| 255 | Hustle_Provider_Utils::maybe_log( __METHOD__, $e->getMessage() ); |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get api |
| 264 | * |
| 265 | * @param string $api_key Api key. |
| 266 | * @param string $api_secret Api secret. |
| 267 | * @return Hustle_ConvertKit_API_Interface |
| 268 | */ |
| 269 | public static function api( $api_key, $api_secret = '' ) { |
| 270 | |
| 271 | if ( empty( self::$api ) ) { |
| 272 | try { |
| 273 | self::$api = new Hustle_ConvertKit_API_V2( $api_key ); |
| 274 | self::$errors = array(); |
| 275 | } catch ( Exception $e ) { |
| 276 | self::$errors = array( 'api_error' => $e ); |
| 277 | } |
| 278 | } |
| 279 | return self::$api; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Creates necessary custom fields for the form |
| 284 | * |
| 285 | * @param string $global_multi_id Global multi ID. |
| 286 | * @param array $fields Fields. |
| 287 | * @return array|mixed|object|WP_Error |
| 288 | */ |
| 289 | public function maybe_create_custom_fields( $global_multi_id, array $fields ) { |
| 290 | $api_key = $this->get_setting( 'api_key', '', $global_multi_id ); |
| 291 | |
| 292 | // check if already existing. |
| 293 | $custom_fields = self::api( $api_key )->get_form_custom_fields(); |
| 294 | $proceed = true; |
| 295 | foreach ( $custom_fields as $custom_field ) { |
| 296 | if ( isset( $fields[ $custom_field->key ] ) ) { |
| 297 | unset( $fields[ $custom_field->key ] ); |
| 298 | } |
| 299 | } |
| 300 | // create necessary fields |
| 301 | // Note: we don't delete fields here, let the user do it on ConvertKit app.convertkit.com . |
| 302 | $api = self::api( $api_key ); |
| 303 | foreach ( $fields as $key => $field ) { |
| 304 | $add_custom_field = $api->create_custom_fields( |
| 305 | array( |
| 306 | 'label' => $field['label'], |
| 307 | ) |
| 308 | ); |
| 309 | if ( is_wp_error( $add_custom_field ) ) { |
| 310 | $proceed = false; |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return $proceed; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Get 3.0 provider mappings |
| 320 | * |
| 321 | * @return array |
| 322 | */ |
| 323 | public function get_30_provider_mappings() { |
| 324 | return array( |
| 325 | 'api_key' => 'api_key', |
| 326 | ); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | endif; |
| 331 |