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