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