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