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