| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\admin; |
| 4 |
|
| 5 |
use Leadin\data\Portal_Options; |
| 6 |
use Leadin\data\User; |
| 7 |
use Leadin\utils\QueryParameters; |
| 8 |
use Leadin\auth\OAuth; |
| 9 |
|
| 10 |
/** |
| 11 |
* Handles portal connection to the plugin. |
| 12 |
*/ |
| 13 |
class Connection { |
| 14 |
|
| 15 |
const CONNECT_KEYS = array( |
| 16 |
'refresh_token', |
| 17 |
'expires_in', |
| 18 |
'portal_id', |
| 19 |
'portal_domain', |
| 20 |
'portal_name', |
| 21 |
'hublet', |
| 22 |
); |
| 23 |
|
| 24 |
const CONNECT_NONCE_ARG = 'leadin_connect'; |
| 25 |
const DISCONNECT_NONCE_ARG = 'leadin_disconnect'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns true if a portal has been connected to the plugin |
| 29 |
*/ |
| 30 |
public static function is_connected() { |
| 31 |
return ! empty( Portal_Options::get_portal_id() ) && ! empty( OAuth::get_refresh_token() ); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Returns true if the current request is for the plugin to connect to a portal |
| 38 |
*/ |
| 39 |
public static function is_connection_requested() { |
| 40 |
$maybe_leadin_connect = QueryParameters::get_param( self::CONNECT_NONCE_ARG, 'hubspot-nonce', self::CONNECT_NONCE_ARG ); |
| 41 |
$maybe_refresh_token = QueryParameters::get_param( 'refresh_token', 'hubspot-nonce', self::CONNECT_NONCE_ARG ); |
| 42 |
|
| 43 |
return isset( $maybe_leadin_connect ) && isset( $maybe_refresh_token ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns true if the current request is for the plugin to connect to a portal |
| 48 |
*/ |
| 49 |
public static function is_new_portal() { |
| 50 |
$maybe_is_new_portal = QueryParameters::get_param( 'is_new_portal', 'hubspot-nonce', self::CONNECT_NONCE_ARG ); |
| 51 |
|
| 52 |
return isset( $maybe_is_new_portal ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns true if the current request is to disconnect the plugin from the portal |
| 57 |
*/ |
| 58 |
public static function is_disconnection_requested() { |
| 59 |
$maybe_leadin_disconnect = QueryParameters::get_param( self::DISCONNECT_NONCE_ARG, 'hubspot-nonce', self::DISCONNECT_NONCE_ARG ); |
| 60 |
return isset( $maybe_leadin_disconnect ); |
| 61 |
} |
| 62 |
/** |
| 63 |
* Retrieves user ID and create new metadata |
| 64 |
* |
| 65 |
* @param Array $user_meta array of pairs metadata - value. |
| 66 |
*/ |
| 67 |
private static function add_metadata( $user_meta ) { |
| 68 |
$wp_user = wp_get_current_user(); |
| 69 |
$wp_user_id = $wp_user->ID; |
| 70 |
foreach ( $user_meta as $key => $value ) { |
| 71 |
add_user_meta( $wp_user_id, $key, $value ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Retrieves user ID and deletes a piece of the users meta data. |
| 77 |
* |
| 78 |
* @param String $meta_key is the key of the data you want to delete. |
| 79 |
*/ |
| 80 |
private static function delete_metadata( $meta_key ) { |
| 81 |
$wp_user = wp_get_current_user(); |
| 82 |
$wp_user_id = $wp_user->ID; |
| 83 |
delete_user_meta( $wp_user_id, $meta_key ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Connect portal id, domain, name to WordPress options and HubSpot email to user meta data. |
| 88 |
* |
| 89 |
* @param Number $portal_id HubSpot account id. |
| 90 |
* @param String $portal_name HubSpot account name. |
| 91 |
* @param String $portal_domain HubSpot account domain. |
| 92 |
* @param String $hs_user_email HubSpot user email. |
| 93 |
* @param String $hublet HubSpot account's hublet. |
| 94 |
*/ |
| 95 |
public static function connect( $portal_id, $portal_name, $portal_domain, $hs_user_email, $hublet ) { |
| 96 |
self::disconnect(); |
| 97 |
self::store_portal_info( $portal_id, $portal_name, $portal_domain, $hublet ); |
| 98 |
self::add_metadata( array( 'leadin_email' => $hs_user_email ) ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Connect the plugin with OAuthorization. Storing OAuth tokens and metadata for the connected portal. |
| 103 |
*/ |
| 104 |
public static function oauth_connect() { |
| 105 |
$connect_params = QueryParameters::get_parameters( self::CONNECT_KEYS, 'hubspot-nonce', self::CONNECT_NONCE_ARG ); |
| 106 |
|
| 107 |
self::disconnect(); |
| 108 |
self::store_portal_info( |
| 109 |
$connect_params['portal_id'], |
| 110 |
$connect_params['portal_name'], |
| 111 |
$connect_params['portal_domain'], |
| 112 |
$connect_params['hublet'] |
| 113 |
); |
| 114 |
|
| 115 |
OAuth::authorize( $connect_params['refresh_token'] ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Removes portal id and domain from the WordPress options. |
| 120 |
*/ |
| 121 |
public static function disconnect() { |
| 122 |
Portal_Options::set_last_disconnect_time(); |
| 123 |
|
| 124 |
self::delete_portal_info(); |
| 125 |
|
| 126 |
$users = get_users( array( 'fields' => array( 'ID' ) ) ); |
| 127 |
foreach ( $users as $user ) { |
| 128 |
delete_user_meta( $user->ID, 'leadin_email' ); |
| 129 |
delete_user_meta( $user->ID, 'leadin_skip_review' ); |
| 130 |
delete_user_meta( $user->ID, 'leadin_review_banner_last_call' ); |
| 131 |
delete_user_meta( $user->ID, 'leadin_has_min_contacts' ); |
| 132 |
delete_user_meta( $user->ID, 'leadin_track_consent' ); |
| 133 |
} |
| 134 |
|
| 135 |
OAuth::deauthorize(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Store the portal metadata for connecting the plugin in the options table |
| 140 |
* |
| 141 |
* @param String $portal_id ID for connecting portal. |
| 142 |
* @param String $portal_name Name of the connecting portal. |
| 143 |
* @param String $portal_domain Domain for the connecting portal. |
| 144 |
* @param String $hublet Hublet for the connecting portal. |
| 145 |
*/ |
| 146 |
public static function store_portal_info( $portal_id, $portal_name, $portal_domain, $hublet ) { |
| 147 |
Portal_Options::set_portal_id( $portal_id ); |
| 148 |
Portal_Options::set_account_name( $portal_name ); |
| 149 |
Portal_Options::set_portal_domain( $portal_domain ); |
| 150 |
Portal_Options::set_hublet( $hublet ); |
| 151 |
Portal_Options::set_disable_internal_tracking(); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Delete stored portal metadata for disconnecting the plugin from the options table |
| 156 |
*/ |
| 157 |
private static function delete_portal_info() { |
| 158 |
Portal_Options::delete_portal_id(); |
| 159 |
Portal_Options::delete_account_name(); |
| 160 |
Portal_Options::delete_portal_domain(); |
| 161 |
Portal_Options::delete_hublet(); |
| 162 |
Portal_Options::delete_disable_internal_tracking(); |
| 163 |
Portal_Options::delete_business_unit_id(); |
| 164 |
} |
| 165 |
} |
| 166 |
|