images
7 years ago
activecampaign.php
5 months ago
hustle-activecampaign-api.php
5 months ago
hustle-activecampaign-form-hooks.php
5 months ago
hustle-activecampaign-form-settings.php
5 months ago
hustle-activecampaign.php
3 years ago
hustle-activecampaign.php
424 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Activecampaign class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'Hustle_Activecampaign' ) ) : |
| 9 | |
| 10 | include_once 'hustle-activecampaign-api.php'; |
| 11 | |
| 12 | /** |
| 13 | * Class Hustle_Activecampaign |
| 14 | */ |
| 15 | class Hustle_Activecampaign extends Hustle_Provider_Abstract { |
| 16 | |
| 17 | const SLUG = 'activecampaign'; |
| 18 | |
| 19 | /** |
| 20 | * Api |
| 21 | * |
| 22 | * @var Hustle_Activecampaign_Api |
| 23 | */ |
| 24 | protected static $api; |
| 25 | /** |
| 26 | * Errors |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | protected static $errors; |
| 31 | |
| 32 | /** |
| 33 | * Provider Instance |
| 34 | * |
| 35 | * @since 3.0.5 |
| 36 | * |
| 37 | * @var self|null |
| 38 | */ |
| 39 | protected static $instance = null; |
| 40 | |
| 41 | /** |
| 42 | * Slug |
| 43 | * |
| 44 | * @since 3.0.5 |
| 45 | * @var string |
| 46 | */ |
| 47 | protected $slug = 'activecampaign'; |
| 48 | |
| 49 | /** |
| 50 | * Version |
| 51 | * |
| 52 | * @since 3.0.5 |
| 53 | * @var string |
| 54 | */ |
| 55 | protected $version = '1.0'; |
| 56 | |
| 57 | /** |
| 58 | * Class |
| 59 | * |
| 60 | * @since 3.0.5 |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $class = __CLASS__; |
| 64 | |
| 65 | /** |
| 66 | * Title |
| 67 | * |
| 68 | * @since 3.0.5 |
| 69 | * @var string |
| 70 | */ |
| 71 | protected $title = 'ActiveCampaign'; |
| 72 | |
| 73 | /** |
| 74 | * Class name of form settings |
| 75 | * |
| 76 | * @var string |
| 77 | */ |
| 78 | protected $form_settings = 'Hustle_Activecampaign_Form_Settings'; |
| 79 | |
| 80 | /** |
| 81 | * Class name of form hooks |
| 82 | * |
| 83 | * @since 4.0 |
| 84 | * @var string |
| 85 | */ |
| 86 | protected $form_hooks = 'Hustle_Activecampaign_Form_Hooks'; |
| 87 | |
| 88 | /** |
| 89 | * Array of options which should exist for confirming that settings are completed |
| 90 | * |
| 91 | * @since 4.0 |
| 92 | * @var array |
| 93 | */ |
| 94 | protected $completion_options = array( 'api_key', 'api_url' ); |
| 95 | |
| 96 | /** |
| 97 | * Provider constructor. |
| 98 | */ |
| 99 | public function __construct() { |
| 100 | $this->icon_2x = plugin_dir_url( __FILE__ ) . 'images/icon.png'; |
| 101 | $this->logo_2x = plugin_dir_url( __FILE__ ) . 'images/logo.png'; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get Instance |
| 106 | * |
| 107 | * @return self|null |
| 108 | */ |
| 109 | public static function get_instance() { |
| 110 | if ( is_null( self::$instance ) ) { |
| 111 | self::$instance = new self(); |
| 112 | } |
| 113 | |
| 114 | return self::$instance; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get api. |
| 119 | * |
| 120 | * @param string $url URL. |
| 121 | * @param string $api_key Api key. |
| 122 | * @return Hustle_Activecampaign_Api |
| 123 | */ |
| 124 | public static function api( $url, $api_key ) { |
| 125 | |
| 126 | if ( empty( self::$api ) ) { |
| 127 | try { |
| 128 | self::$api = new Hustle_Activecampaign_Api( $url, $api_key ); |
| 129 | self::$errors = array(); |
| 130 | } catch ( Exception $e ) { |
| 131 | self::$errors = array( 'api_error' => $e ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return self::$api; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * Get the wizard callbacks for the global settings. |
| 141 | * |
| 142 | * @since 4.0 |
| 143 | * |
| 144 | * @return array |
| 145 | */ |
| 146 | public function settings_wizards() { |
| 147 | return array( |
| 148 | array( |
| 149 | 'callback' => array( $this, 'configure_api_key' ), |
| 150 | 'is_completed' => array( $this, 'is_connected' ), |
| 151 | ), |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /** |
| 157 | * Configure the API key settings. Global settings. |
| 158 | * |
| 159 | * @since 4.0 |
| 160 | * |
| 161 | * @param array $submitted_data Submitted data. |
| 162 | * @return array |
| 163 | */ |
| 164 | public function configure_api_key( $submitted_data ) { |
| 165 | $has_errors = false; |
| 166 | $default_data = array( |
| 167 | 'api_key' => '', |
| 168 | 'api_url' => '', |
| 169 | 'name' => '', |
| 170 | ); |
| 171 | $current_data = $this->get_current_data( $default_data, $submitted_data ); |
| 172 | $is_submit = isset( $submitted_data['api_url'] ) && isset( $submitted_data['api_key'] ); |
| 173 | $global_multi_id = $this->get_global_multi_id( $submitted_data ); |
| 174 | |
| 175 | $api_url_valid = true; |
| 176 | $api_key_valid = true; |
| 177 | |
| 178 | if ( $is_submit ) { |
| 179 | |
| 180 | $api_url_valid = $this->is_non_empty( $current_data['api_url'] ); |
| 181 | $api_key_valid = $this->is_non_empty( $current_data['api_key'] ); |
| 182 | $api_key_validated = $api_url_valid && $api_key_valid |
| 183 | && $this->validate_credentials( $submitted_data['api_url'], $submitted_data['api_key'] ); |
| 184 | |
| 185 | if ( ! $api_key_validated ) { |
| 186 | $error_message = $this->provider_connection_falied(); |
| 187 | $api_url_valid = false; |
| 188 | $api_key_valid = false; |
| 189 | $has_errors = true; |
| 190 | } |
| 191 | |
| 192 | if ( ! $has_errors ) { |
| 193 | $settings_to_save = array( |
| 194 | 'api_key' => $current_data['api_key'], |
| 195 | 'api_url' => $current_data['api_url'], |
| 196 | 'name' => $current_data['name'], |
| 197 | ); |
| 198 | // If not active, activate it. |
| 199 | // TODO: Wrap this in a friendlier method. |
| 200 | if ( Hustle_Provider_Utils::is_provider_active( $this->slug ) |
| 201 | || Hustle_Providers::get_instance()->activate_addon( $this->slug ) ) { |
| 202 | $this->save_multi_settings_values( $global_multi_id, $settings_to_save ); |
| 203 | } else { |
| 204 | $error_message = __( "Provider couldn't be activated.", 'hustle' ); |
| 205 | $has_errors = true; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if ( ! $has_errors ) { |
| 210 | |
| 211 | return array( |
| 212 | 'html' => Hustle_Provider_Utils::get_integration_modal_title_markup( __( 'Activecampaign Added', 'hustle' ), __( 'You can now go to your pop-ups, slide-ins and embeds and assign them to this integration', 'hustle' ) ), |
| 213 | 'buttons' => array( |
| 214 | 'close' => array( |
| 215 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( __( 'Close', 'hustle' ), 'sui-button-ghost', 'close' ), |
| 216 | ), |
| 217 | ), |
| 218 | 'redirect' => false, |
| 219 | 'has_errors' => false, |
| 220 | 'notification' => array( |
| 221 | 'type' => 'success', |
| 222 | 'text' => '<strong>' . $this->get_title() . '</strong> ' . esc_html__( 'Successfully connected', 'hustle' ), |
| 223 | ), |
| 224 | ); |
| 225 | |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | $options = array( |
| 230 | array( |
| 231 | 'type' => 'wrapper', |
| 232 | 'class' => $api_url_valid ? '' : 'sui-form-field-error', |
| 233 | 'elements' => array( |
| 234 | 'label_url' => array( |
| 235 | 'type' => 'label', |
| 236 | 'for' => 'api_url', |
| 237 | 'value' => __( 'API URL', 'hustle' ), |
| 238 | ), |
| 239 | 'api_url' => array( |
| 240 | 'type' => 'text', |
| 241 | 'name' => 'api_url', |
| 242 | 'value' => $current_data['api_url'], |
| 243 | 'placeholder' => __( 'Enter URL', 'hustle' ), |
| 244 | 'id' => 'api_url', |
| 245 | 'icon' => 'web-globe-world', |
| 246 | ), |
| 247 | 'error' => array( |
| 248 | 'type' => 'error', |
| 249 | 'class' => $api_url_valid ? 'sui-hidden' : '', |
| 250 | 'value' => __( 'Please enter a valid ActiveCampaign URL', 'hustle' ), |
| 251 | ), |
| 252 | ), |
| 253 | ), |
| 254 | array( |
| 255 | 'type' => 'wrapper', |
| 256 | 'class' => $api_key_valid ? '' : 'sui-form-field-error', |
| 257 | 'elements' => array( |
| 258 | 'label_url' => array( |
| 259 | 'type' => 'label', |
| 260 | 'for' => 'api_key', |
| 261 | 'value' => __( 'API Key', 'hustle' ), |
| 262 | ), |
| 263 | 'api_url' => array( |
| 264 | 'type' => 'text', |
| 265 | 'name' => 'api_key', |
| 266 | 'value' => $current_data['api_key'], |
| 267 | 'placeholder' => __( 'Enter Key', 'hustle' ), |
| 268 | 'id' => 'api_key', |
| 269 | 'icon' => 'key', |
| 270 | ), |
| 271 | 'error' => array( |
| 272 | 'type' => 'error', |
| 273 | 'class' => $api_key_valid ? 'sui-hidden' : '', |
| 274 | 'value' => __( 'Please enter a valid ActiveCampaign API key', 'hustle' ), |
| 275 | ), |
| 276 | ), |
| 277 | ), |
| 278 | array( |
| 279 | 'type' => 'wrapper', |
| 280 | 'style' => 'margin-bottom: 0;', |
| 281 | 'elements' => array( |
| 282 | 'label' => array( |
| 283 | 'type' => 'label', |
| 284 | 'for' => 'instance-name-input', |
| 285 | 'value' => __( 'Identifier', 'hustle' ), |
| 286 | ), |
| 287 | 'name' => array( |
| 288 | 'type' => 'text', |
| 289 | 'name' => 'name', |
| 290 | 'value' => $current_data['name'], |
| 291 | 'placeholder' => __( 'E.g. Business Account', 'hustle' ), |
| 292 | 'id' => 'instance-name-input', |
| 293 | ), |
| 294 | 'message' => array( |
| 295 | 'type' => 'description', |
| 296 | 'value' => __( 'Helps to distinguish your integrations if you have connected to the multiple accounts of this integration.', 'hustle' ), |
| 297 | ), |
| 298 | ), |
| 299 | ), |
| 300 | ); |
| 301 | |
| 302 | $step_html = Hustle_Provider_Utils::get_integration_modal_title_markup( |
| 303 | __( 'Configure ActiveCampaign', 'hustle' ), |
| 304 | sprintf( |
| 305 | /* translators: 1. opening 'a' tag to ActivaCampaign login, 2. closing 'a' tag */ |
| 306 | __( 'Log in to your %1$sActiveCampaign account%2$s to get your URL and API Key.', 'hustle' ), |
| 307 | '<a href="http://www.activecampaign.com/login/" target="_blank">', |
| 308 | '</a>' |
| 309 | ) |
| 310 | ); |
| 311 | |
| 312 | if ( $has_errors ) { |
| 313 | $error_notice = array( |
| 314 | 'type' => 'notice', |
| 315 | 'icon' => 'info', |
| 316 | 'class' => 'sui-notice-error', |
| 317 | 'value' => esc_html( $error_message ), |
| 318 | ); |
| 319 | array_unshift( $options, $error_notice ); |
| 320 | } |
| 321 | |
| 322 | $step_html .= Hustle_Provider_Utils::get_html_for_options( $options ); |
| 323 | |
| 324 | $is_edit = $this->settings_are_completed( $global_multi_id ); |
| 325 | if ( $is_edit ) { |
| 326 | $buttons = array( |
| 327 | 'disconnect' => array( |
| 328 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 329 | __( 'Disconnect', 'hustle' ), |
| 330 | 'sui-button-ghost', |
| 331 | 'disconnect', |
| 332 | true |
| 333 | ), |
| 334 | ), |
| 335 | 'save' => array( |
| 336 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 337 | __( 'Save', 'hustle' ), |
| 338 | '', |
| 339 | 'connect', |
| 340 | true |
| 341 | ), |
| 342 | ), |
| 343 | ); |
| 344 | } else { |
| 345 | $buttons = array( |
| 346 | 'connect' => array( |
| 347 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 348 | __( 'Connect', 'hustle' ), |
| 349 | 'sui-button-right', |
| 350 | 'connect', |
| 351 | true |
| 352 | ), |
| 353 | ), |
| 354 | ); |
| 355 | |
| 356 | } |
| 357 | |
| 358 | $response = array( |
| 359 | 'html' => $step_html, |
| 360 | 'buttons' => $buttons, |
| 361 | 'has_errors' => $has_errors, |
| 362 | ); |
| 363 | |
| 364 | return $response; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Validate the provided API key. |
| 369 | * |
| 370 | * @since 4.0 |
| 371 | * |
| 372 | * @param string $api_url Api URL. |
| 373 | * @param string $api_key Api key. |
| 374 | * @return bool |
| 375 | */ |
| 376 | private function validate_credentials( $api_url, $api_key ) { |
| 377 | if ( empty( $api_key ) || empty( $api_url ) ) { |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | try { |
| 382 | // Check if credentials are valid. |
| 383 | $api = self::api( $api_url, $api_key ); |
| 384 | if ( $api ) { |
| 385 | $account = $api->get_account(); |
| 386 | } |
| 387 | |
| 388 | if ( is_wp_error( $account ) || ! $account ) { |
| 389 | Hustle_Api_Utils::maybe_log( __METHOD__, __( 'Invalid Activecampaign API credentials.', 'hustle' ) ); |
| 390 | return false; |
| 391 | } |
| 392 | } catch ( Exception $e ) { |
| 393 | Hustle_Provider_Utils::maybe_log( __METHOD__, $e->getMessage() ); |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Is non empty |
| 402 | * |
| 403 | * @param string $value Value. |
| 404 | * @return bool |
| 405 | */ |
| 406 | private function is_non_empty( $value ) { |
| 407 | return ! empty( trim( $value ) ); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Get 3.0 provider mappings |
| 412 | * |
| 413 | * @return array |
| 414 | */ |
| 415 | protected function get_30_provider_mappings() { |
| 416 | return array( |
| 417 | 'api_key' => 'api_key', |
| 418 | 'url' => 'api_url', |
| 419 | ); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | endif; |
| 424 |