images
7 years ago
campaignmonitor.php
3 years ago
hustle-campaignmonitor-api.php
3 years ago
hustle-campaignmonitor-form-hooks.php
3 years ago
hustle-campaignmonitor-form-settings.php
3 years ago
hustle-campaignmonitor.php
3 years ago
hustle-campaignmonitor.php
484 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Campaignmonitor class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'Hustle_Campaignmonitor' ) ) : |
| 9 | |
| 10 | /** |
| 11 | * Class Hustle_Campaignmonitor |
| 12 | */ |
| 13 | class Hustle_Campaignmonitor extends Hustle_Provider_Abstract { |
| 14 | |
| 15 | const SLUG = 'campaignmonitor'; |
| 16 | |
| 17 | /** |
| 18 | * Api |
| 19 | * |
| 20 | * @var AWeberAPI |
| 21 | */ |
| 22 | protected static $api; |
| 23 | /** |
| 24 | * Errors |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected static $errors; |
| 29 | |
| 30 | /** |
| 31 | * Activecampaign Provider Instance |
| 32 | * |
| 33 | * @since 3.0.5 |
| 34 | * |
| 35 | * @var self|null |
| 36 | */ |
| 37 | protected static $instance = null; |
| 38 | |
| 39 | /** |
| 40 | * Slug |
| 41 | * |
| 42 | * @since 3.0.5 |
| 43 | * @var string |
| 44 | */ |
| 45 | protected $slug = 'campaignmonitor'; |
| 46 | |
| 47 | /** |
| 48 | * Version |
| 49 | * |
| 50 | * @since 3.0.5 |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $version = '1.0'; |
| 54 | |
| 55 | /** |
| 56 | * Class |
| 57 | * |
| 58 | * @since 3.0.5 |
| 59 | * @var string |
| 60 | */ |
| 61 | protected $class = __CLASS__; |
| 62 | |
| 63 | /** |
| 64 | * Title |
| 65 | * |
| 66 | * @since 3.0.5 |
| 67 | * @var string |
| 68 | */ |
| 69 | protected $title = 'Campaign Monitor'; |
| 70 | |
| 71 | /** |
| 72 | * Class name of form settings |
| 73 | * |
| 74 | * @var string |
| 75 | */ |
| 76 | protected $form_settings = 'Hustle_Campaignmonitor_Form_Settings'; |
| 77 | |
| 78 | /** |
| 79 | * Class name of form hooks |
| 80 | * |
| 81 | * @since 4.0 |
| 82 | * @var string |
| 83 | */ |
| 84 | protected $form_hooks = 'Hustle_Campaignmonitor_Form_Hooks'; |
| 85 | |
| 86 | /** |
| 87 | * Provider constructor. |
| 88 | */ |
| 89 | public function __construct() { |
| 90 | $this->icon_2x = plugin_dir_url( __FILE__ ) . 'images/icon.png'; |
| 91 | $this->logo_2x = plugin_dir_url( __FILE__ ) . 'images/logo.png'; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get Instance |
| 96 | * |
| 97 | * @return self|null |
| 98 | */ |
| 99 | public static function get_instance() { |
| 100 | if ( is_null( self::$instance ) ) { |
| 101 | self::$instance = new self(); |
| 102 | } |
| 103 | |
| 104 | return self::$instance; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get api |
| 109 | * |
| 110 | * @param string $api_key Api key. |
| 111 | * @return CS_REST_General |
| 112 | */ |
| 113 | public static function api( $api_key ) { |
| 114 | if ( empty( self::$api ) ) { |
| 115 | try { |
| 116 | self::$api = Hustle_Campaignmonitor_API::boot( $api_key ); |
| 117 | self::$errors = array(); |
| 118 | } catch ( Exception $e ) { |
| 119 | self::$errors = array( 'api_error' => $e ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return self::$api; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Get the wizard callbacks for the global settings. |
| 129 | * |
| 130 | * @since 4.0 |
| 131 | * |
| 132 | * @return array |
| 133 | */ |
| 134 | public function settings_wizards() { |
| 135 | return array( |
| 136 | array( |
| 137 | 'callback' => array( $this, 'configure_api_key' ), |
| 138 | 'is_completed' => array( $this, 'is_connected' ), |
| 139 | ), |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Configure the API key settings. Global settings. |
| 146 | * |
| 147 | * @since 4.0 |
| 148 | * |
| 149 | * @param array $submitted_data Submitted data. |
| 150 | * @return array |
| 151 | */ |
| 152 | public function configure_api_key( $submitted_data ) { |
| 153 | $has_errors = false; |
| 154 | $default_data = array( |
| 155 | 'api_key' => '', |
| 156 | 'name' => '', |
| 157 | 'client_id' => '', |
| 158 | ); |
| 159 | $current_data = $this->get_current_data( $default_data, $submitted_data ); |
| 160 | $is_submit = isset( $submitted_data['api_key'] ); |
| 161 | $global_multi_id = $this->get_global_multi_id( $submitted_data ); |
| 162 | |
| 163 | $api_key_validated = true; |
| 164 | $api_client_validated = true; |
| 165 | if ( $is_submit ) { |
| 166 | |
| 167 | $api_key_validated = $this->validate_api_key( $submitted_data['api_key'] ); |
| 168 | $client_id = isset( $current_data['client_id'] ) ? $current_data['client_id'] : ''; |
| 169 | if ( ! $api_key_validated ) { |
| 170 | $error_message = $this->provider_connection_falied(); |
| 171 | $has_errors = true; |
| 172 | } |
| 173 | |
| 174 | if ( ! empty( $client_id ) ) { |
| 175 | $api_client_validated = $this->validate_client( $submitted_data['api_key'], $client_id ); |
| 176 | } |
| 177 | |
| 178 | if ( ! $has_errors ) { |
| 179 | $settings_to_save = array( |
| 180 | 'api_key' => $current_data['api_key'], |
| 181 | 'name' => $current_data['name'], |
| 182 | ); |
| 183 | $api_key = $submitted_data['api_key']; |
| 184 | |
| 185 | $client_details = null; |
| 186 | if ( ! empty( $client_id ) && $api_client_validated ) { |
| 187 | $client_details = $this->get_client( $api_key, $client_id ); |
| 188 | } else { |
| 189 | // find first client. |
| 190 | $clients = self::api( $api_key )->get_clients(); |
| 191 | if ( is_array( $clients ) ) { |
| 192 | if ( isset( $clients[0] ) ) { |
| 193 | $client = $clients[0]; |
| 194 | if ( isset( $client->ClientID ) ) {// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 195 | $client_id = $client->ClientID;// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 196 | $client_details = $this->get_client( $api_key, $client_id ); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if ( ! isset( $client_details->BasicDetails ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 203 | || ! isset( $client_details->BasicDetails->ClientID ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 204 | || ! isset( $client_details->BasicDetails->CompanyName ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 205 | $error_message = __( 'Could not find client details, please try again.', 'hustle' ); |
| 206 | $has_errors = true; |
| 207 | } |
| 208 | |
| 209 | if ( ! $has_errors ) { |
| 210 | $settings_to_save['client_id'] = $client_id; |
| 211 | $settings_to_save['client_name'] = $client_details->BasicDetails->CompanyName;// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 212 | $api_client_validated = true; |
| 213 | } |
| 214 | // If not active, activate it. |
| 215 | // TODO: Wrap this in a friendlier method. |
| 216 | if ( Hustle_Provider_Utils::is_provider_active( $this->slug ) |
| 217 | || Hustle_Providers::get_instance()->activate_addon( $this->slug ) ) { |
| 218 | $this->save_multi_settings_values( $global_multi_id, $settings_to_save ); |
| 219 | } else { |
| 220 | $error_message = __( "Provider couldn't be activated.", 'hustle' ); |
| 221 | $has_errors = true; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if ( ! $has_errors ) { |
| 226 | |
| 227 | return array( |
| 228 | 'html' => Hustle_Provider_Utils::get_integration_modal_title_markup( __( 'Campaign Monitor Added', 'hustle' ), __( 'You can now go to your pop-ups, slide-ins and embeds and assign them to this integration', 'hustle' ) ), |
| 229 | 'buttons' => array( |
| 230 | 'close' => array( |
| 231 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( __( 'Close', 'hustle' ), 'sui-button-ghost', 'close' ), |
| 232 | ), |
| 233 | ), |
| 234 | 'redirect' => false, |
| 235 | 'has_errors' => false, |
| 236 | 'notification' => array( |
| 237 | 'type' => 'success', |
| 238 | 'text' => '<strong>' . $this->get_title() . '</strong> ' . esc_html__( 'Successfully connected', 'hustle' ), |
| 239 | ), |
| 240 | ); |
| 241 | |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | $options = array( |
| 246 | array( |
| 247 | 'type' => 'wrapper', |
| 248 | 'class' => $api_key_validated ? '' : 'sui-form-field-error', |
| 249 | 'elements' => array( |
| 250 | 'label' => array( |
| 251 | 'type' => 'label', |
| 252 | 'for' => 'api_key', |
| 253 | 'value' => __( 'API Key', 'hustle' ), |
| 254 | ), |
| 255 | 'api_key' => array( |
| 256 | 'type' => 'text', |
| 257 | 'id' => 'api_key', |
| 258 | 'name' => 'api_key', |
| 259 | 'value' => $current_data['api_key'], |
| 260 | 'placeholder' => __( 'Enter Key', 'hustle' ), |
| 261 | 'icon' => 'key', |
| 262 | ), |
| 263 | 'error' => array( |
| 264 | 'type' => 'error', |
| 265 | 'class' => $api_key_validated ? 'sui-hidden' : '', |
| 266 | 'value' => __( 'Please enter a valid Campaign Monitor API key', 'hustle' ), |
| 267 | ), |
| 268 | ), |
| 269 | ), |
| 270 | array( |
| 271 | 'type' => 'wrapper', |
| 272 | 'class' => $api_client_validated ? '' : 'sui-form-field-error', |
| 273 | 'elements' => array( |
| 274 | 'api_key_label' => array( |
| 275 | 'type' => 'label', |
| 276 | 'for' => 'client_id', |
| 277 | 'value' => __( 'Client ID', 'hustle' ), |
| 278 | 'note' => __( 'Required for Agency accounts only', 'hustle' ), |
| 279 | ), |
| 280 | 'client_id' => array( |
| 281 | 'type' => 'text', |
| 282 | 'id' => 'client_id', |
| 283 | 'name' => 'client_id', |
| 284 | 'value' => $current_data['client_id'], |
| 285 | 'placeholder' => __( 'Enter Key', 'hustle' ), |
| 286 | 'icon' => 'key', |
| 287 | ), |
| 288 | 'client_message' => array( |
| 289 | 'type' => 'description', |
| 290 | 'value' => /* translators: 1. Open 'strong' tag. 2. Close 'strong' tag. */ sprintf( esc_html__( "If you have an agency account, enter the client id to connect to one of your clients' account. You can find client id on the %1\$sAccount Settings > API Keys%2\$s page.", 'hustle' ), '<strong>', '</strong>' ), |
| 291 | ), |
| 292 | 'client_error' => array( |
| 293 | 'type' => 'error', |
| 294 | 'class' => $api_client_validated ? 'sui-hidden' : '', |
| 295 | 'value' => __( 'Please, enter a valid Campaign Monitor client id.', 'hustle' ), |
| 296 | ), |
| 297 | ), |
| 298 | ), |
| 299 | array( |
| 300 | 'type' => 'wrapper', |
| 301 | 'style' => 'margin-bottom: 0;', |
| 302 | 'elements' => array( |
| 303 | 'label' => array( |
| 304 | 'type' => 'label', |
| 305 | 'for' => 'instance-name-input', |
| 306 | 'value' => __( 'Identifier', 'hustle' ), |
| 307 | ), |
| 308 | 'name' => array( |
| 309 | 'type' => 'text', |
| 310 | 'name' => 'name', |
| 311 | 'value' => $current_data['name'], |
| 312 | 'placeholder' => __( 'E.g. Business Account', 'hustle' ), |
| 313 | 'id' => 'instance-name-input', |
| 314 | ), |
| 315 | 'message' => array( |
| 316 | 'type' => 'description', |
| 317 | 'value' => __( 'Helps to distinguish your integrations if you have connected to the multiple accounts of this integration.', 'hustle' ), |
| 318 | ), |
| 319 | ), |
| 320 | ), |
| 321 | ); |
| 322 | |
| 323 | $step_html = Hustle_Provider_Utils::get_integration_modal_title_markup( |
| 324 | __( 'Configure Campaign Monitor', 'hustle' ), |
| 325 | sprintf( |
| 326 | /* translators: 1. link to Campaign Monitor account 2. 'Account Settings' text 3. 'API keys' text */ |
| 327 | esc_html__( 'To get your API key, log in to your %1$s, then click on your profile picture at the top-right corner to open a menu, then select %2$s and finally click on %3$s.', 'hustle' ), |
| 328 | sprintf( |
| 329 | '<a href="%1$s" target="_blank">%2$s</a>', |
| 330 | 'https://login.createsend.com/l/?ReturnUrl=%2Faccount%2Fapikeys', |
| 331 | esc_html__( 'Campaign Monitor account' ) |
| 332 | ), |
| 333 | sprintf( '<strong>%s</strong>', esc_html__( 'Account Settings' ) ), |
| 334 | sprintf( '<strong>%s</strong>', esc_html__( 'API keys' ) ) |
| 335 | ) |
| 336 | ); |
| 337 | if ( $has_errors ) { |
| 338 | $error_notice = array( |
| 339 | 'type' => 'notice', |
| 340 | 'icon' => 'info', |
| 341 | 'class' => 'sui-notice-error', |
| 342 | 'value' => esc_html( $error_message ), |
| 343 | ); |
| 344 | array_unshift( $options, $error_notice ); |
| 345 | } |
| 346 | $step_html .= Hustle_Provider_Utils::get_html_for_options( $options ); |
| 347 | |
| 348 | $is_edit = $this->settings_are_completed( $global_multi_id ); |
| 349 | if ( $is_edit ) { |
| 350 | $buttons = array( |
| 351 | 'disconnect' => array( |
| 352 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 353 | __( 'Disconnect', 'hustle' ), |
| 354 | 'sui-button-ghost', |
| 355 | 'disconnect', |
| 356 | true |
| 357 | ), |
| 358 | ), |
| 359 | 'save' => array( |
| 360 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 361 | __( 'Save', 'hustle' ), |
| 362 | '', |
| 363 | 'connect', |
| 364 | true |
| 365 | ), |
| 366 | ), |
| 367 | ); |
| 368 | } else { |
| 369 | $buttons = array( |
| 370 | 'connect' => array( |
| 371 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 372 | __( 'Connect', 'hustle' ), |
| 373 | 'sui-button-right', |
| 374 | 'connect', |
| 375 | true |
| 376 | ), |
| 377 | ), |
| 378 | ); |
| 379 | |
| 380 | } |
| 381 | |
| 382 | $response = array( |
| 383 | 'html' => $step_html, |
| 384 | 'buttons' => $buttons, |
| 385 | 'has_errors' => $has_errors, |
| 386 | ); |
| 387 | |
| 388 | return $response; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Validate the provided API key. |
| 393 | * |
| 394 | * @since 4.0 |
| 395 | * |
| 396 | * @param string $api_key Api key. |
| 397 | * @return bool |
| 398 | */ |
| 399 | private function validate_api_key( $api_key ) { |
| 400 | if ( empty( trim( $api_key ) ) ) { |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | // Check API Key by validating it on get_info request. |
| 405 | try { |
| 406 | // Check if API key is valid. |
| 407 | $clients = self::api( $api_key )->get_system_date(); |
| 408 | |
| 409 | if ( is_wp_error( $clients ) ) { |
| 410 | Hustle_Provider_Utils::maybe_log( __METHOD__, __( 'Invalid Campaignmonitor API key.', 'hustle' ) ); |
| 411 | return false; |
| 412 | } |
| 413 | } catch ( Exception $e ) { |
| 414 | Hustle_Provider_Utils::maybe_log( __METHOD__, $e->getMessage() ); |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Get client |
| 423 | * |
| 424 | * @since 1.0 Campaign Monitor Addon |
| 425 | * |
| 426 | * @param string $api_key Api key. |
| 427 | * @param string $client_id Client ID. |
| 428 | * |
| 429 | * @return array|mixed|object |
| 430 | */ |
| 431 | public function get_client( $api_key, $client_id ) { |
| 432 | self::$api = null; |
| 433 | $api = self::api( $api_key ); |
| 434 | |
| 435 | $client_details = $api->get_client( $client_id ); |
| 436 | |
| 437 | return $client_details; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Validate Client |
| 442 | * |
| 443 | * @since 1.0 Campaign Monitor Addon |
| 444 | * |
| 445 | * @param string $api_key Api key. |
| 446 | * @param string $client_id Client ID. |
| 447 | * |
| 448 | * @return array|mixed|object |
| 449 | */ |
| 450 | public function validate_client( $api_key, $client_id ) { |
| 451 | |
| 452 | // Check API Key by validating it on get_info request. |
| 453 | try { |
| 454 | // Check if API key is valid. |
| 455 | self::$api = null; |
| 456 | $api = self::api( $api_key ); |
| 457 | $client_details = $api->get_client( $client_id ); |
| 458 | |
| 459 | if ( is_wp_error( $client_details ) ) { |
| 460 | Hustle_Provider_Utils::maybe_log( __METHOD__, __( 'Invalid Campaignmonitor Client ID key.', 'hustle' ) ); |
| 461 | return false; |
| 462 | } |
| 463 | } catch ( Exception $e ) { |
| 464 | Hustle_Provider_Utils::maybe_log( __METHOD__, $e->getMessage() ); |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Get 3.0 provider mappings |
| 473 | * |
| 474 | * @return type |
| 475 | */ |
| 476 | public function get_30_provider_mappings() { |
| 477 | return array( |
| 478 | 'api_key' => 'api_key', |
| 479 | ); |
| 480 | } |
| 481 | |
| 482 | } |
| 483 | endif; |
| 484 |