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