| 1 |
<?php |
| 2 |
namespace Leadin\admin; |
| 3 |
|
| 4 |
use Leadin\data\Filters; |
| 5 |
use Leadin\admin\Links; |
| 6 |
use Leadin\admin\Routing; |
| 7 |
use Leadin\auth\OAuth; |
| 8 |
use Leadin\utils\Versions; |
| 9 |
use Leadin\data\User; |
| 10 |
use Leadin\data\Portal_Options; |
| 11 |
use Leadin\admin\Connection; |
| 12 |
use Leadin\admin\Impact; |
| 13 |
use Leadin\data\User_Metadata; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class containing all the constants used for admin script localization. |
| 17 |
*/ |
| 18 |
class AdminConstants { |
| 19 |
|
| 20 |
|
| 21 |
/** |
| 22 |
* Return utm_campaign to add to the signup link. |
| 23 |
*/ |
| 24 |
private static function get_utm_campaign() { |
| 25 |
$wpe_template = get_option( 'wpe_template' ); |
| 26 |
if ( 'hubspot' === $wpe_template ) { |
| 27 |
return 'wp-engine-site-template'; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Return an array with the utm parameters for signup |
| 33 |
*/ |
| 34 |
private static function get_utm_query_params_array() { |
| 35 |
$utm_params = array( |
| 36 |
'utm_source' => 'wordpress-plugin', |
| 37 |
'utm_medium' => 'marketplaces', |
| 38 |
); |
| 39 |
|
| 40 |
$utm_campaign = self::get_utm_campaign(); |
| 41 |
if ( ! empty( $utm_campaign ) ) { |
| 42 |
$utm_params['utm_campaign'] = $utm_campaign; |
| 43 |
} |
| 44 |
return $utm_params; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Return a nonce used on the connection class |
| 49 |
*/ |
| 50 |
private static function get_connection_nonce() { |
| 51 |
return wp_create_nonce( 'hubspot-nonce' ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Return an array with the user's pre-fill info for signup |
| 56 |
*/ |
| 57 |
private static function get_signup_prefill_params_array() { |
| 58 |
$wp_user = wp_get_current_user(); |
| 59 |
$user_info = array( |
| 60 |
'firstName' => $wp_user->user_firstname, |
| 61 |
'lastName' => $wp_user->user_lastname, |
| 62 |
'email' => $wp_user->user_email, |
| 63 |
'company' => get_bloginfo( 'name' ), |
| 64 |
); |
| 65 |
|
| 66 |
return $user_info; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Return an array of properties to be included in the signup search string |
| 71 |
*/ |
| 72 |
public static function get_signup_query_params_array() { |
| 73 |
$signup_params = array(); |
| 74 |
$signup_params['leadinPluginVersion'] = constant( 'LEADIN_PLUGIN_VERSION' ); |
| 75 |
$signup_params['trackConsent'] = User_Metadata::get_track_consent(); |
| 76 |
$user_prefill_params = self::get_signup_prefill_params_array(); |
| 77 |
$signup_params = array_merge( $signup_params, $user_prefill_params ); |
| 78 |
return $signup_params; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Return query params array for the iframe. |
| 83 |
*/ |
| 84 |
public static function get_hubspot_query_params_array() { |
| 85 |
$wp_user = wp_get_current_user(); |
| 86 |
$hubspot_config = array( |
| 87 |
'l' => get_locale(), |
| 88 |
'php' => Versions::get_php_version(), |
| 89 |
'v' => LEADIN_PLUGIN_VERSION, |
| 90 |
'wp' => Versions::get_wp_version(), |
| 91 |
'theme' => get_option( 'stylesheet' ), |
| 92 |
'adminUrl' => admin_url(), |
| 93 |
'websiteName' => get_bloginfo( 'name' ), |
| 94 |
'domain' => parse_url( get_site_url(), PHP_URL_HOST ), |
| 95 |
'wp_user' => $wp_user->first_name ? $wp_user->first_name : $wp_user->user_nicename, |
| 96 |
'nonce' => self::get_connection_nonce(), |
| 97 |
'accountName' => Portal_Options::get_account_name(), |
| 98 |
'hsdio' => Portal_Options::get_device_id(), |
| 99 |
'portalDomain' => Portal_Options::get_portal_domain(), |
| 100 |
); |
| 101 |
|
| 102 |
$utm_params = self::get_utm_query_params_array(); |
| 103 |
$hubspot_config = array_merge( $hubspot_config, $utm_params ); |
| 104 |
|
| 105 |
if ( User::is_admin() ) { |
| 106 |
$hubspot_config['admin'] = '1'; |
| 107 |
} |
| 108 |
|
| 109 |
if ( Routing::has_just_connected_with_oauth() ) { |
| 110 |
$hubspot_config['justConnected'] = true; |
| 111 |
} |
| 112 |
if ( Routing::is_new_portal_with_oauth() ) { |
| 113 |
$hubspot_config['isNewPortal'] = true; |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! Connection::is_connected() ) { |
| 117 |
$signup_params = self::get_signup_query_params_array(); |
| 118 |
$hubspot_config = array_merge( $hubspot_config, $signup_params, Impact::get_params() ); |
| 119 |
} |
| 120 |
|
| 121 |
return $hubspot_config; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns a minimal version of leadinConfig, containing the data needed by the background iframe. |
| 126 |
*/ |
| 127 |
public static function get_background_leadin_config() { |
| 128 |
$wp_user_id = get_current_user_id(); |
| 129 |
|
| 130 |
$background_config = array( |
| 131 |
'adminUrl' => admin_url(), |
| 132 |
'activationTime' => Portal_Options::get_activation_time(), |
| 133 |
'deviceId' => Portal_Options::get_device_id(), |
| 134 |
'formsScript' => Filters::apply_forms_script_url_filters(), |
| 135 |
'formsScriptPayload' => Filters::apply_forms_payload_filters(), |
| 136 |
'meetingsScript' => Filters::apply_meetings_script_url_filters(), |
| 137 |
'hublet' => Filters::apply_hublet_filters(), |
| 138 |
'hubspotBaseUrl' => Filters::apply_base_url_filters( Connection::is_connected() ), |
| 139 |
'leadinPluginVersion' => constant( 'LEADIN_PLUGIN_VERSION' ), |
| 140 |
'locale' => get_locale(), |
| 141 |
'restUrl' => get_rest_url(), |
| 142 |
'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 143 |
'redirectNonce' => wp_create_nonce( Routing::REDIRECT_NONCE ), |
| 144 |
'phpVersion' => Versions::get_php_version(), |
| 145 |
'pluginPath' => constant( 'LEADIN_PATH' ), |
| 146 |
'plugins' => get_plugins(), |
| 147 |
'portalId' => Portal_Options::get_portal_id(), |
| 148 |
'accountName' => Portal_Options::get_account_name(), |
| 149 |
'portalDomain' => Portal_Options::get_portal_domain(), |
| 150 |
'portalEmail' => get_user_meta( $wp_user_id, 'leadin_email', true ), |
| 151 |
'reviewSkippedDate' => User_Metadata::get_skip_review(), |
| 152 |
'theme' => get_option( 'stylesheet' ), |
| 153 |
'wpVersion' => Versions::get_wp_version(), |
| 154 |
'leadinQueryParams' => self::get_hubspot_query_params_array(), |
| 155 |
'connectionStatus' => Connection::is_connected() ? 'Connected' : 'NotConnected', |
| 156 |
); |
| 157 |
|
| 158 |
if ( Connection::is_connected() ) { |
| 159 |
$background_config['refreshToken'] = OAuth::get_refresh_token(); |
| 160 |
} |
| 161 |
|
| 162 |
return $background_config; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Returns leadinConfig, containing all the data needed by the leadin javascript. |
| 167 |
*/ |
| 168 |
public static function get_leadin_config() { |
| 169 |
$leadin_config = self::get_background_leadin_config(); |
| 170 |
|
| 171 |
if ( ! Connection::is_connected() ) { |
| 172 |
if ( ! Impact::has_params() ) { |
| 173 |
$impact_link = Impact::get_affiliate_link(); |
| 174 |
if ( ! empty( $impact_link ) ) { |
| 175 |
$leadin_config['impactLink'] = Impact::get_affiliate_link(); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return $leadin_config; |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|