PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.3.75
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.3.75
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / public / admin / class-connection.php

class-connection.php in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.3.75, at public/admin/class-connection.php

169 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Returns true if the current request is for the plugin to connect to a portal
36 */
37 public static function is_connection_requested() {
38 $maybe_leadin_connect = QueryParameters::get_param( self::CONNECT_NONCE_ARG, 'hubspot-nonce', self::CONNECT_NONCE_ARG );
39 $maybe_refresh_token = QueryParameters::get_param( 'refresh_token', 'hubspot-nonce', self::CONNECT_NONCE_ARG );
40
41 return isset( $maybe_leadin_connect ) && isset( $maybe_refresh_token );
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_new_portal() {
48 $maybe_is_new_portal = QueryParameters::get_param( 'is_new_portal', 'hubspot-nonce', self::CONNECT_NONCE_ARG );
49
50 return isset( $maybe_is_new_portal );
51 }
52
53 /**
54 * Returns true if the current request is to disconnect the plugin from the portal
55 */
56 public static function is_disconnection_requested() {
57 $maybe_leadin_disconnect = QueryParameters::get_param( self::DISCONNECT_NONCE_ARG, 'hubspot-nonce', self::DISCONNECT_NONCE_ARG );
58 return isset( $maybe_leadin_disconnect );
59 }
60 /**
61 * Retrieves user ID and create new metadata
62 *
63 * @param Array $user_meta array of pairs metadata - value.
64 */
65 private static function add_metadata( $user_meta ) {
66 $wp_user = wp_get_current_user();
67 $wp_user_id = $wp_user->ID;
68 foreach ( $user_meta as $key => $value ) {
69 add_user_meta( $wp_user_id, $key, $value );
70 }
71 }
72
73 /**
74 * Retrieves user ID and deletes a piece of the users meta data.
75 *
76 * @param String $meta_key is the key of the data you want to delete.
77 */
78 private static function delete_metadata( $meta_key ) {
79 $wp_user = wp_get_current_user();
80 $wp_user_id = $wp_user->ID;
81 delete_user_meta( $wp_user_id, $meta_key );
82 }
83
84 /**
85 * Connect portal id, domain, name to WordPress options and HubSpot email to user meta data.
86 *
87 * @param Number $portal_id HubSpot account id.
88 * @param String $portal_name HubSpot account name.
89 * @param String $portal_domain HubSpot account domain.
90 * @param String $hs_user_email HubSpot user email.
91 * @param String $hublet HubSpot account's hublet.
92 */
93 public static function connect( $portal_id, $portal_name, $portal_domain, $hs_user_email, $hublet ) {
94 self::disconnect();
95 self::store_portal_info( $portal_id, $portal_name, $portal_domain, $hublet );
96 self::add_metadata( array( 'leadin_email' => $hs_user_email ) );
97 }
98
99 /**
100 * Connect the plugin with OAuthorization. Storing OAuth tokens and metadata for the connected portal.
101 */
102 public static function oauth_connect() {
103 $connect_params = QueryParameters::get_parameters( self::CONNECT_KEYS, 'hubspot-nonce', self::CONNECT_NONCE_ARG );
104
105 self::disconnect();
106 self::store_portal_info(
107 $connect_params['portal_id'],
108 $connect_params['portal_name'],
109 $connect_params['portal_domain'],
110 $connect_params['hublet']
111 );
112
113 OAuth::authorize( $connect_params['refresh_token'] );
114 }
115
116 /**
117 * Removes portal id and domain from the WordPress options.
118 */
119 public static function disconnect() {
120 Portal_Options::set_last_disconnect_time();
121
122 self::delete_portal_info();
123
124 $users = get_users(
125 array( 'role__in' => array( 'administrator', 'editor' ) ),
126 array( 'fields' => array( 'ID' ) )
127 );
128 foreach ( $users as $user ) {
129 delete_user_meta( $user->ID, 'leadin_email' );
130 delete_user_meta( $user->ID, 'leadin_skip_review' );
131 delete_user_meta( $user->ID, 'leadin_review_banner_last_call' );
132 delete_user_meta( $user->ID, 'leadin_has_min_contacts' );
133 delete_user_meta( $user->ID, 'leadin_track_consent' );
134 }
135
136 OAuth::deauthorize();
137 }
138
139 /**
140 * Store the portal metadata for connecting the plugin in the options table
141 *
142 * @param String $portal_id ID for connecting portal.
143 * @param String $portal_name Name of the connecting portal.
144 * @param String $portal_domain Domain for the connecting portal.
145 * @param String $hublet Hublet for the connecting portal.
146 */
147 public static function store_portal_info( $portal_id, $portal_name, $portal_domain, $hublet ) {
148 Portal_Options::set_portal_id( $portal_id );
149 Portal_Options::set_account_name( $portal_name );
150 Portal_Options::set_portal_domain( $portal_domain );
151 Portal_Options::set_hublet( $hublet );
152 Portal_Options::set_disable_internal_tracking();
153 Portal_Options::set_proxy_mappings_enabled(false);
154 }
155
156 /**
157 * Delete stored portal metadata for disconnecting the plugin from the options table
158 */
159 private static function delete_portal_info() {
160 Portal_Options::delete_portal_id();
161 Portal_Options::delete_account_name();
162 Portal_Options::delete_portal_domain();
163 Portal_Options::delete_hublet();
164 Portal_Options::delete_disable_internal_tracking();
165 Portal_Options::delete_business_unit_id();
166 Portal_Options::delete_proxy_mappings_enabled();
167 }
168 }
169