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