/**
* Builds a URL to the Jetpack connection auth page
*
* @since 3.9.5
*
* @param bool $raw If true, URL will not be escaped.
* @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection.
* If string, will be a custom redirect.
* @param bool|string $from If not false, adds 'from=$from' param to the connect URL.
*
* @return string Connect URL
*/
function build_connect_url($raw = false, $redirect = false, $from = false)
{
if (!Jetpack_Options::get_option('blog_token') || !Jetpack_Options::get_option('id')) {
$url = Jetpack::nonce_url_no_esc(Jetpack::admin_url('action=register'), 'jetpack-register');
if (is_network_admin()) {
$url = add_query_arg('is_multisite', network_admin_url('admin.php?page=jetpack-settings'), $url);
}
} else {
require_once JETPACK__GLOTPRESS_LOCALES_PATH;
$role = $this->translate_current_user_to_role();
$signed_role = $this->sign_role($role);
$user = wp_get_current_user();
$redirect = $redirect ? esc_url_raw($redirect) : esc_url_raw(menu_page_url('jetpack', false));
$gp_locale = GP_Locales::by_field('wp_locale', get_locale());
if (isset($_REQUEST['is_multisite'])) {
$redirect = Jetpack_Network::init()->get_url('network_admin_page');
}
$secrets = Jetpack::init()->generate_secrets('authorize');
@(list($secret) = explode(':', $secrets));
$site_icon = function_exists('has_site_icon') && has_site_icon() ? get_site_icon_url() : false;
$args = urlencode_deep(array('response_type' => 'code', 'client_id' => Jetpack_Options::get_option('id'), 'redirect_uri' => add_query_arg(array('action' => 'authorize', '_wpnonce' => wp_create_nonce("jetpack-authorize_{$role}_{$redirect}"), 'redirect' => urlencode($redirect)), menu_page_url('jetpack', false)), 'state' => $user->ID, 'scope' => $signed_role, 'user_email' => $user->user_email, 'user_login' => $user->user_login, 'is_active' => Jetpack::is_active(), 'jp_version' => JETPACK__VERSION, 'auth_type' => 'calypso', 'secret' => $secret, 'locale' => isset($gp_locale->slug) ? $gp_locale->slug : '', 'blogname' => get_option('blogname'), 'site_url' => site_url(), 'home_url' => home_url(), 'site_icon' => $site_icon));
$url = add_query_arg($args, Jetpack::api_url('authorize'));
}
if ($from) {
$url = add_query_arg('from', $from, $url);
}
if (isset($_GET['calypso_env'])) {
$url = add_query_arg('calypso_env', $_GET['calypso_env'], $url);
}
return $raw ? $url : esc_url($url);
}