PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.8.14
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.8.14
7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / inc / providers / infusionsoft / hustle-infusion-soft.php
wordpress-popup / inc / providers / infusionsoft Last commit date
images 3 years ago model 5 months ago hustle-infusion-soft-form-hooks.php 5 months ago hustle-infusion-soft-form-settings.php 5 months ago hustle-infusion-soft-oauth-non-hub.php 5 months ago hustle-infusion-soft-oauth.php 5 months ago hustle-infusion-soft-request-method.php 5 months ago hustle-infusion-soft-rest-api.php 5 months ago hustle-infusion-soft.php 5 months ago infusionsoft.php 5 months ago interface-opt-in-infusionsoft-connection.php 5 months ago
hustle-infusion-soft.php
657 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Hustle_Infusion_Soft class
4 *
5 * @package Hustle
6 */
7
8 if ( ! class_exists( 'Hustle_Infusion_Soft' ) ) :
9
10 include_once 'hustle-infusion-soft-oauth.php';
11 include_once 'interface-opt-in-infusionsoft-connection.php';
12 include_once 'hustle-infusion-soft-request-method.php';
13 include_once 'hustle-infusion-soft-rest-api.php';
14 include_once __DIR__ . '/model/hustle-infusion-soft-contact.php';
15 include_once __DIR__ . '/model/hustle-infusion-soft-custom-field.php';
16
17
18 /**
19 * Class Hustle_Infusion_Soft
20 */
21 class Hustle_Infusion_Soft extends Hustle_Provider_Abstract {
22
23 const SLUG = 'infusionsoft';
24 const OAUTH_ACTION = 'infusionsoft_oauth';
25 const KEAP_APPS_URL = 'https://keys.developer.keap.com/my-apps';
26
27 const CLIENT_ID = 'inc_opt_infusionsoft_clientid';
28 const CLIENT_SECRET = 'inc_opt_infusionsoft_clientsecret';
29
30 /**
31 * Api
32 *
33 * @var Opt_In_Infusionsoft_Connection
34 */
35 protected static $api;
36 /**
37 * Errors
38 *
39 * @var array
40 */
41 protected static $errors;
42
43 /**
44 * Provider Instance
45 *
46 * @since 3.0.5
47 *
48 * @var self|null
49 */
50 protected static $instance = null;
51
52 /**
53 * OAuth
54 *
55 * @var Hustle_Infusion_Soft_OAuth
56 */
57 protected $auth;
58
59 /**
60 * Slug
61 *
62 * @since 3.0.5
63 * @var string
64 */
65 protected $slug = 'infusionsoft';
66
67 /**
68 * Is multi on global
69 *
70 * @var boolean
71 */
72 protected $is_multi_on_global = false;
73
74 /**
75 * Version
76 *
77 * @since 3.0.5
78 * @var string
79 */
80 protected $version = '2.0';
81
82 /**
83 * Class
84 *
85 * @since 3.0.5
86 * @var string
87 */
88 protected $class = __CLASS__;
89
90 /**
91 * Title
92 *
93 * @since 3.0.5
94 * @var string
95 */
96 protected $title = 'Keap';
97
98 /**
99 * Class name of form settings
100 *
101 * @var string
102 */
103 protected $form_settings = 'Hustle_Infusion_Soft_Form_Settings';
104
105 /**
106 * Class name of form hooks
107 *
108 * @since 4.0
109 * @var string
110 */
111 protected $form_hooks = 'Hustle_InfusionSoft_Form_Hooks';
112
113 /**
114 * Array of options which should exist for confirming that settings are completed
115 *
116 * @since 4.0
117 * @var array
118 */
119 protected $completion_options = array( 'account_name' );
120
121 /**
122 * Provider constructor.
123 */
124 public function __construct() {
125 $this->icon_2x = plugin_dir_url( __FILE__ ) . 'images/icon.png';
126 $this->logo_2x = plugin_dir_url( __FILE__ ) . 'images/logo.png';
127 $this->get_oauth();
128 }
129
130 /**
131 * Get OAuth instance
132 *
133 * @return Hustle_Infusion_Soft_OAuth
134 */
135 public function get_oauth() {
136 if ( ! $this->auth ) {
137 if ( Opt_In_Utils::get_hub_api_key() ) {
138 $this->auth = new Hustle_Infusion_Soft_OAuth();
139 } else {
140 $settings = $this->get_settings_values();
141 $this->auth = new Hustle_Infusion_Soft_OAuth_Non_Hub();
142 // Set the keys if they exist.
143 if ( ! is_array( $settings ) ) {
144 $settings = array();
145 }
146 if ( ! empty( $settings['api_key'] ) ) {
147 $this->auth->set_api_key( $settings['api_key'] );
148 }
149 if ( ! empty( $settings['private_key'] ) ) {
150 $this->auth->set_secret_key( $settings['private_key'] );
151 }
152 }
153 }
154
155 return $this->auth;
156 }
157
158 /**
159 * Get token
160 *
161 * @return string
162 */
163 public function get_access_token() {
164 $token = $this->get_oauth()->get_token();
165 if ( is_wp_error( $token ) || is_null( $token ) ) {
166 return '';
167 }
168
169 return $token->get_access_token();
170 }
171
172 /**
173 * Get Instance
174 *
175 * @return self|null
176 */
177 public static function get_instance() {
178 if ( is_null( self::$instance ) ) {
179 self::$instance = new self();
180 }
181
182 return self::$instance;
183 }
184
185 /**
186 * Returns a cached api
187 *
188 * @param string $api_key Api key.
189 * @return Opt_In_Infusionsoft_Api
190 */
191 public static function api( $api_key ) {
192
193 if ( empty( self::$api ) ) {
194 try {
195 self::$errors = array();
196 self::$api = new Opt_In_Infusionsoft_Rest_Api( $api_key );
197 } catch ( Exception $e ) {
198 self::$errors = array( 'api_error' => $e );
199 }
200 }
201
202 return self::$api;
203 }
204
205 /**
206 * Get the wizard callbacks for the global settings.
207 *
208 * @since 4.0
209 *
210 * @return array
211 */
212 public function settings_wizards() {
213 if ( Opt_In_Utils::get_hub_api_key() ) {
214 // Hub user integration.
215 return array(
216 array(
217 'callback' => array( $this, 'build_user_authorization_form' ),
218 'is_completed' => array( $this, 'is_connected' ),
219 ),
220 );
221 } else {
222 // Non hub user integration.
223 return array(
224 array(
225 'callback' => array( $this, 'build_non_hub_user_integration_settings' ),
226 'is_completed' => array( $this, 'is_connected' ),
227 ),
228 );
229 }
230 }
231
232 /**
233 * Build authorization form for integration.
234 *
235 * @param array $submitted_data Submitted data.
236 * @param bool $is_submit Is submit.
237 * @param int $module_id Module ID.
238 * @return array Response array.
239 */
240 public function build_user_authorization_form( $submitted_data, $is_submit, $module_id ) {
241 $oauth = $this->get_oauth();
242 $page = $oauth->get_authorization_uri( $module_id, true, Hustle_Data::INTEGRATIONS_PAGE );
243
244 $description = '';
245 $is_connected = $this->is_connected();
246
247 if ( $is_connected ) {
248 if ( $this->migration_required() ) {
249 /* translators: %s is a link to Keap developer keys page */
250 $description = sprintf( __( 'Re-authenticate your Hustle → Keap integration using OAuth2. Enter your Keap access tokens to update your integration. Get your API keys %s.', 'hustle' ), '<a href="' . esc_url( self::KEAP_APPS_URL ) . '" target="_blank">' . esc_html__( 'here', 'hustle' ) . '</a>' );
251 }
252 // If the user is already authorized, we can skip the connection step.
253 $buttons = array(
254 'disconnect' => array(
255 'markup' => Hustle_Provider_Utils::get_provider_button_markup(
256 __( 'Disconnect', 'hustle' ),
257 'sui-button-ghost sui-button-center',
258 'disconnect',
259 true
260 ),
261 ),
262 );
263 } else {
264 /* translators: Plugin name */
265 $description = sprintf( __( 'Connect the Keap integration by authenticating it using the button below. Note that you’ll be taken to the Keap website to grant access to %s and then redirected back.', 'hustle' ), Opt_In_Utils::get_plugin_name() );
266
267 $buttons = array(
268 'connect' => array(
269 'markup' => Hustle_Provider_Utils::get_provider_button_markup(
270 __( 'Connect', 'hustle' ),
271 'sui-button-blue sui-button-right',
272 'connect',
273 true,
274 false,
275 $page
276 ),
277 ),
278 );
279 }
280
281 if ( $this->migration_required() ) {
282 $title = __( 'Migrate Keap', 'hustle' );
283 } else {
284 $title = __( 'Connect Keap', 'hustle' );
285 }
286
287 $step_html = Hustle_Provider_Utils::get_integration_modal_title_markup(
288 $title,
289 $description
290 );
291
292 if ( $is_connected ) {
293 // Show success message if already connected.
294 $step_html .= Hustle_Provider_Utils::get_html_for_options(
295 array(
296 array(
297 'type' => 'notice',
298 'icon' => 'check-tick',
299 'value' => esc_html__( 'Hustle is successfully integrated with Keap. You can start sending data to this integration.', 'hustle' ),
300 'class' => 'sui-notice-success',
301 ),
302 )
303 );
304 }
305
306 return array(
307 'html' => $step_html,
308 'buttons' => $buttons,
309 'has_errors' => false,
310 );
311 }
312
313 /**
314 * Check if API key is configured for non-hub users
315 *
316 * @since 4.0
317 * @return bool
318 */
319 public function is_api_key_configured() {
320 $settings = $this->get_settings_values();
321
322 return ! empty( $settings['api_key'] ) && ! empty( $settings['private_key'] );
323 }
324
325 /**
326 * Build authorization form for non-hub user integration.
327 *
328 * @param array $submitted_data Submitted data.
329 * @param bool $is_submit Is submit.
330 * @param int $module_id Module ID.
331 * @return array Response array.
332 */
333 public function build_non_hub_user_integration_settings( $submitted_data, $is_submit, $module_id ) {
334
335 if ( $this->is_connected() ) {
336 return $this->user_account_info();
337 }
338
339 $has_errors = false;
340 $html = '';
341 $buttons = array();
342
343 if ( $is_submit ) {
344 $public_key = isset( $submitted_data['public_key'] ) ? sanitize_text_field( $submitted_data['public_key'] ) : '';
345 $secret_key = isset( $submitted_data['secret_key'] ) ? sanitize_text_field( $submitted_data['secret_key'] ) : '';
346
347 if ( empty( $public_key ) || empty( $secret_key ) ) {
348 $has_errors = true;
349 } else {
350 // Save the keys.
351 $settings = $this->get_settings_values();
352 if ( ! is_array( $settings ) ) {
353 $settings = array();
354 }
355 $settings['api_key'] = $public_key;
356 $settings['private_key'] = $secret_key;
357
358 $this->save_settings_values( $settings, $module_id );
359
360 $this->get_oauth()->set_api_key( $public_key );
361 $this->get_oauth()->set_secret_key( $secret_key );
362 }
363
364 $page = $this->
365 get_oauth()->
366 get_authorization_uri( $module_id, true, Hustle_Data::INTEGRATIONS_PAGE );
367
368 return array(
369 'redirect' => $page,
370 'is_close' => false,
371 );
372 } else {
373 $settings = $this->get_settings_values();
374 $public_key = isset( $settings['api_key'] ) ? $settings['api_key'] : '';
375 $secret_key = isset( $settings['private_key'] ) ? $settings['private_key'] : '';
376 }
377
378 if ( $this->migration_required() ) {
379 $this->get_oauth()->set_api_key( $public_key );
380 $this->get_oauth()->set_secret_key( $secret_key );
381
382 $page = $this->
383 get_oauth()->
384 get_authorization_uri( $module_id, true, Hustle_Data::INTEGRATIONS_PAGE );
385
386 return array(
387 'redirect' => $page,
388 'is_close' => false,
389 );
390 }
391
392 ob_start();
393 ?>
394 <div class="hustle-wizard-content">
395 <?php
396 $keap_link = 'https://keys.developer.keap.com/my-apps';
397 echo wp_kses_post(
398 Hustle_Provider_Utils::get_integration_modal_title_markup(
399 __( 'Keap integration', 'hustle' ),
400 /* translators: %s is a link to Keap developer keys page */
401 sprintf( __( 'Authenticate your Hustle → Keap integration using OAuth2. Enter your Keap access tokens to update your integration. Get your API keys %s.', 'hustle' ), '<a href="' . esc_url( $keap_link ) . '" target="_blank">' . esc_html__( 'here', 'hustle' ) . '</a>' )
402 )
403 );
404 ?>
405
406 <div class="sui-form-field">
407 <label class="sui-label"><?php esc_html_e( 'Client ID', 'hustle' ); ?></label>
408 <input
409 name="public_key"
410 placeholder="<?php esc_attr_e( 'Enter your client ID', 'hustle' ); ?>"
411 value="<?php echo esc_attr( $public_key ); ?>"
412 class="sui-form-control" />
413 </div>
414
415 <div class="sui-form-field">
416 <label class="sui-label"><?php esc_html_e( 'Secret key', 'hustle' ); ?></label>
417 <input
418 name="secret_key"
419 type="password"
420 placeholder="<?php esc_attr_e( 'Enter your secret key', 'hustle' ); ?>"
421 value="<?php echo esc_attr( $secret_key ); ?>"
422 class="sui-form-control" />
423 </div>
424 </div>
425 <?php
426 $html = ob_get_clean();
427
428 $buttons = array(
429 'authorize' => array(
430 'markup' => Hustle_Provider_Utils::get_provider_button_markup(
431 __( 'Authorize', 'hustle' ),
432 'sui-button-blue sui-button-center',
433 'next',
434 true,
435 false,
436 ''
437 ),
438 ),
439 );
440
441 return array(
442 'html' => $html,
443 'buttons' => $buttons,
444 'has_errors' => $has_errors,
445 );
446 }
447
448 /**
449 * User account info after connection.
450 *
451 * @return array Response array.
452 */
453 private function user_account_info() {
454 $buttons = array(
455 'disconnect' => array(
456 'markup' => Hustle_Provider_Utils::get_provider_button_markup(
457 __( 'Disconnect', 'hustle' ),
458 'sui-button-ghost sui-button-center',
459 'disconnect',
460 true
461 ),
462 ),
463 );
464
465 $step_html = Hustle_Provider_Utils::get_integration_modal_title_markup(
466 __( 'Connect Keap', 'hustle' ),
467 );
468
469 // Show success message if already connected.
470 $step_html .= Hustle_Provider_Utils::get_html_for_options(
471 array(
472 array(
473 'type' => 'notice',
474 'icon' => 'check-tick',
475 'value' => esc_html__( 'Hustle is successfully integrated with Keap. You can start sending data to this integration.', 'hustle' ),
476 'class' => 'sui-notice-success',
477 ),
478 )
479 );
480
481 return array(
482 'html' => $step_html,
483 'buttons' => $buttons,
484 'has_errors' => false,
485 );
486 }
487
488 /**
489 * Safe tokens after authorization complete.
490 */
491 public function process_external_redirect() {
492
493 $auth = $this->get_oauth();
494 $status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
495
496 if ( 'success' === $status && $auth->is_authorized() ) {
497 $providers = $this->get_providers();
498
499 if ( ! $providers->addon_is_active( $this->slug ) ) {
500 // Activate the provider if not active.
501 $activated = $providers->activate_addon( $this->slug );
502
503 if ( $activated ) {
504 // Success message for first time connection.
505 $response = Opt_In_Utils::build_notification(
506 'success',
507 /* translators: integration name */
508 sprintf( esc_html__( '%s successfully connected.', 'hustle' ), '<strong>' . esc_html( $this->title ) . '</strong>' )
509 );
510 } else {
511 $response = Opt_In_Utils::build_notification(
512 'error',
513 /* translators: integration name */
514 sprintf( esc_html__( 'Failed to activate %s. Please try again.', 'hustle' ), esc_html( $this->title ) )
515 );
516 }
517 } else {
518 $response = Opt_In_Utils::build_notification(
519 'success',
520 /* translators: integration name */
521 sprintf( esc_html__( '%s successfully reconnected.', 'hustle' ), '<strong>' . esc_html( $this->title ) . '</strong>' )
522 );
523 }
524
525 update_option( $this->get_version_options_name(), $this->version );
526 } else {
527 $response = Opt_In_Utils::build_notification(
528 'error',
529 /* translators: integration name */
530 sprintf( esc_html__( 'Authentication failed! Please check your %s credentials and try again.', 'hustle' ), esc_html( $this->title ) )
531 );
532 }
533
534 return $response;
535 }
536
537 /**
538 * Get providers
539 *
540 * @return Hustle_Providers
541 */
542 protected function get_providers() {
543 return Hustle_Providers::get_instance();
544 }
545
546 /**
547 * Check if settings are completed
548 *
549 * @param string $multi_id Multi ID.
550 * @return bool
551 */
552 protected function settings_are_completed( $multi_id = '' ) {
553 $token = $this->get_oauth()
554 ->get_token();
555
556 if ( is_wp_error( $token ) || ! $token instanceof Hustle_Auth_Token ) {
557 // Error retrieving token or invalid token.
558 return false;
559 }
560
561 // Check if token is valid.
562 return $token->get_access_token() &&
563 $token->get_refresh_token() &&
564 ( time() < $token->get_expiration_time() );
565 }
566
567 /**
568 * Check if migration is needed
569 *
570 * @return bool
571 */
572 private function migration_required() {
573 $api_version = $this->get_installed_version();
574 if ( empty( $api_version ) ) {
575 return false;
576 }
577
578 $settings = $this->get_settings_values();
579
580 return ! empty( $settings['api_key'] ) &&
581 ! empty( $settings['private_key'] ) &&
582 version_compare(
583 $api_version,
584 '2.0',
585 '<'
586 );
587 }
588
589 /**
590 * Get 3.0 provider mappings
591 *
592 * @return type
593 */
594 public function get_30_provider_mappings() {
595 return array(
596 'api_key' => 'api_key',
597 'account_name' => 'account_name',
598 );
599 }
600
601 /**
602 * Configure migrated api keys
603 *
604 * @param array $data Data.
605 * @return void
606 */
607 public function configure_migrated_api_keys( $data ) {
608 if ( ! is_array( $data ) ) {
609 return;
610 }
611
612 // Activate the provider if not active.
613 if (
614 $this->is_active() ||
615 Hustle_Providers::get_instance()->activate_addon( $this->slug )
616 ) {
617
618 $api_key = isset( $data['api_key'] ) ? sanitize_text_field( $data['api_key'] ) : '';
619 $secret_key = isset( $data['private_key'] ) ? sanitize_text_field( $data['private_key'] ) : '';
620 // Save the keys.
621 $this->update_api_keys( $api_key, $secret_key );
622 }
623 }
624
625 /**
626 * Update api key and secret key.
627 *
628 * @param string $api_key Api key.
629 * @param string $secret_key Secret key.
630 * @return void
631 */
632 public function update_api_keys( $api_key, $secret_key ) {
633 $settings = $this->get_settings_values();
634 if ( ! is_array( $settings ) ) {
635 $settings = array();
636 }
637
638 $settings['api_key'] = $api_key;
639 $settings['private_key'] = $secret_key;
640
641 $this->save_settings_values( $settings );
642 }
643
644 /**
645 * Clean up resources.
646 */
647 public function remove_wp_options() {
648 $auth = $this->get_oauth();
649
650 if ( $auth ) {
651 $auth->remove_wp_options();
652 }
653 }
654 }
655
656 endif;
657