| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\admin; |
| 4 |
|
| 5 |
use Leadin\data\Filters; |
| 6 |
use Leadin\data\Portal_Options; |
| 7 |
use Leadin\admin\MenuConstants; |
| 8 |
use Leadin\admin\utils\Background; |
| 9 |
use Leadin\data\User; |
| 10 |
use Leadin\utils\QueryParameters; |
| 11 |
use Leadin\utils\Versions; |
| 12 |
use Leadin\auth\OAuth; |
| 13 |
use Leadin\admin\Connection; |
| 14 |
use Leadin\admin\Impact; |
| 15 |
use Leadin\admin\AdminConstants; |
| 16 |
use Leadin\includes\utils as utils; |
| 17 |
use Leadin\auth\OAuthCrypto; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class containing all the functions to generate links to HubSpot. |
| 21 |
*/ |
| 22 |
class Links { |
| 23 |
/** |
| 24 |
* Deprecated for OAuth2 routes |
| 25 |
* |
| 26 |
* Get a map of <admin_page, url> |
| 27 |
* Where |
| 28 |
* - admin_page is a string |
| 29 |
* - url is either a string or another map <route, string_url>, both strings |
| 30 |
*/ |
| 31 |
public static function get_routes_mapping() { |
| 32 |
$portal_id = Portal_Options::get_portal_id(); |
| 33 |
$reporting_page = "/reports-dashboard/$portal_id"; |
| 34 |
$user_guide = "/wordpress-plugin-ui/$portal_id/onboarding/start"; |
| 35 |
|
| 36 |
return array( |
| 37 |
MenuConstants::ROOT => $user_guide, |
| 38 |
MenuConstants::USER_GUIDE => $user_guide, |
| 39 |
MenuConstants::REPORTING => $reporting_page, |
| 40 |
MenuConstants::CONTACTS => "/contacts/$portal_id", |
| 41 |
MenuConstants::LISTS => array( |
| 42 |
'' => "/contacts/$portal_id/objectLists/all", |
| 43 |
'default' => "/contacts/$portal_id/objectLists", |
| 44 |
'lists' => "/contacts/$portal_id/lists", |
| 45 |
), |
| 46 |
MenuConstants::EMAIL => array( |
| 47 |
'' => "/email/$portal_id", |
| 48 |
'cms' => "/content/$portal_id/create/email", |
| 49 |
), |
| 50 |
MenuConstants::SETTINGS => array( |
| 51 |
'' => "/wordpress-plugin-ui/$portal_id/settings", |
| 52 |
'forms' => "/settings/$portal_id/marketing/form", |
| 53 |
), |
| 54 |
MenuConstants::PRICING => "/pricing/$portal_id/marketing", |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get page name from the current page id. |
| 60 |
* E.g. "hubspot_page_leadin_forms" => "forms" |
| 61 |
*/ |
| 62 |
private static function get_page_id() { |
| 63 |
$screen_id = get_current_screen()->id; |
| 64 |
return preg_replace( '/^(hubspot_page_|toplevel_page_)/', '', $screen_id ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get the parsed `leadin_route` from the query string. |
| 69 |
*/ |
| 70 |
private static function get_iframe_route() { |
| 71 |
$iframe_route = QueryParameters::get_param_array( 'leadin_route', 'hubspot-route' ); |
| 72 |
return is_array( $iframe_route ) ? $iframe_route : array(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get the parsed `leadin_search` from the query string. |
| 77 |
*/ |
| 78 |
private static function get_iframe_search_string() { |
| 79 |
return QueryParameters::get_param( 'leadin_search', 'hubspot-route' ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Return query string from object |
| 84 |
* |
| 85 |
* @param array $arr query parameters to stringify. |
| 86 |
*/ |
| 87 |
private static function http_build_query( $arr ) { |
| 88 |
if ( ! is_array( $arr ) ) { |
| 89 |
return ''; |
| 90 |
} |
| 91 |
return http_build_query( $arr, '', ini_get( 'arg_separator.output' ), PHP_QUERY_RFC3986 ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Validate static version. |
| 96 |
* |
| 97 |
* @param String $version version of the static bundle. |
| 98 |
*/ |
| 99 |
private static function is_static_version_valid( $version ) { |
| 100 |
preg_match( '/static-\d+\.\d+/', $version, $match ); |
| 101 |
return ! empty( $match ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Return a string query parameters to add to the iframe src. |
| 106 |
*/ |
| 107 |
public static function get_query_params() { |
| 108 |
$config_array = AdminConstants::get_hubspot_query_params_array(); |
| 109 |
\array_merge( $config_array, Impact::get_params() ); |
| 110 |
|
| 111 |
return self::http_build_query( $config_array ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* This function computes the right iframe src based on query params and current page. |
| 116 |
* |
| 117 |
* The `page` query param is used as a key to get the url from the get_routes_mapping |
| 118 |
* This query param will get ignored if $page_id function param is present |
| 119 |
* The `leadin_route[]` query params are added to the url |
| 120 |
* |
| 121 |
* e.g.: |
| 122 |
* ?page=leadin_forms&leadin_route[]=foo&leadin_route[]=bar will redirect to /forms/$portal_id/foo/bar |
| 123 |
* |
| 124 |
* If the value of get_routes_mapping is an array, the first value of `leadin_route` will be used as key. |
| 125 |
* If the key isn't found, it will fall back to '' |
| 126 |
* |
| 127 |
* e.g.: |
| 128 |
* ?page=leadin_settings&leadin=route[]=forms&leadin_route[]=bar will redirect to /settings/$portal_id/forms/bar |
| 129 |
* ?page=leadin_settings&leadin=route[]=foo&leadin_route[]=bar will redirect to /wordpress_plugin_ui/$portal_id/settings/foo/bar |
| 130 |
* |
| 131 |
* @param String $page_id is used as a key to get the url from the get_routes_mapping by overriding the query param. |
| 132 |
* |
| 133 |
* Returns the right iframe src. |
| 134 |
*/ |
| 135 |
public static function get_iframe_src( $page_id = '' ) { |
| 136 |
$leadin_onboarding = 'leadin_onboarding'; |
| 137 |
$leadin_new_portal = 'leadin_new_portal'; |
| 138 |
$browser_search_string = ''; |
| 139 |
|
| 140 |
if ( get_transient( $leadin_onboarding ) ) { |
| 141 |
delete_transient( $leadin_onboarding ); |
| 142 |
$browser_search_string = '&justConnected=true'; |
| 143 |
if ( get_transient( $leadin_new_portal ) ) { |
| 144 |
delete_transient( $leadin_new_portal ); |
| 145 |
$browser_search_string = $browser_search_string . '&isNewPortal=true'; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$sub_routes_array = self::get_iframe_route(); |
| 150 |
$inframe_search_string = self::get_iframe_search_string(); |
| 151 |
$browser_search_string = $browser_search_string . $inframe_search_string; |
| 152 |
|
| 153 |
if ( empty( Portal_Options::get_portal_id() ) ) { |
| 154 |
$wp_user = wp_get_current_user(); |
| 155 |
$wp_user_id = $wp_user->ID; |
| 156 |
set_transient( $leadin_onboarding, 'true' ); |
| 157 |
} else { |
| 158 |
if ( '' === $page_id ) { |
| 159 |
$page_id = self::get_page_id(); |
| 160 |
} |
| 161 |
|
| 162 |
$routes = self::get_routes_mapping(); |
| 163 |
|
| 164 |
$is_external = MenuConstants::PRICING === $page_id || MenuConstants::REPORTING === $page_id || MenuConstants::LISTS === $page_id || MenuConstants::CONTACTS === $page_id || MenuConstants::EMAIL === $page_id; |
| 165 |
|
| 166 |
if ( empty( $route ) && isset( $routes[ $page_id ] ) ) { |
| 167 |
$route = $routes[ $page_id ]; |
| 168 |
|
| 169 |
if ( \is_array( $route ) && isset( $sub_routes_array[0] ) ) { |
| 170 |
$first_sub_route = $sub_routes_array[0]; |
| 171 |
|
| 172 |
if ( isset( $route[ $first_sub_route ] ) ) { |
| 173 |
$route = $route[ $first_sub_route ]; |
| 174 |
array_shift( $sub_routes_array ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if ( \is_array( $route ) ) { |
| 179 |
$route = $route['']; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$sub_routes = join( '/', $sub_routes_array ); |
| 185 |
$sub_routes = empty( $sub_routes ) ? $sub_routes : "/$sub_routes"; |
| 186 |
// Query string separator "?" may have been added to the URL already. |
| 187 |
$add_separator = strpos( $sub_routes, '?' ) ? '&' : '?'; |
| 188 |
$additional_query_params = Filters::apply_query_params_filters(); |
| 189 |
|
| 190 |
return Filters::apply_base_url_filters() . "$route$sub_routes" . $add_separator . self::get_query_params() . $browser_search_string . $additional_query_params; |
| 191 |
} |
| 192 |
} |
| 193 |
|