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