Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.76% covered (warning)
75.76%
25 / 33
50.00% covered (danger)
50.00%
3 / 6
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\get_iso_639_locale
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\determine_iso_639_locale
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_enqueue_tracking_scripts
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_record_tracks_event
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
7.46
Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_get_launch_site_url
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\Jetpack_Mu_Wpcom\Common\wpcom_get_launch_site_back_to
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * File for various functionality which needs to be added to Simple and Atomic
4 * sites.
5 *
6 * @package automattic/jetpack-mu-wpcom
7 */
8
9namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Common;
10
11use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
12use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13use Automattic\Jetpack\Status;
14use Automattic\Jetpack\Terms_Of_Service;
15use Automattic\Jetpack\Tracking;
16
17require_once __DIR__ . '/wpcom-enqueue-dynamic-script/class-wpcom-enqueue-dynamic-script.php';
18require_once __DIR__ . '/custom-domain.php';
19
20/**
21 * Returns ISO 639 conforming locale string.
22 *
23 * @param string $language a language tag to be converted e.g. "en_US".
24 * @return string ISO 639 locale string e.g. "en"
25 */
26function get_iso_639_locale( $language ) {
27    $language = strtolower( $language );
28
29    if ( in_array( $language, array( 'pt_br', 'pt-br', 'zh_tw', 'zh-tw', 'zh_cn', 'zh-cn' ), true ) ) {
30        $language = str_replace( '_', '-', $language );
31    } else {
32        $language = preg_replace( '/([-_].*)$/i', '', $language );
33    }
34
35    if ( empty( $language ) ) {
36        return 'en';
37    }
38
39    return $language;
40}
41
42/**
43 * Returns ISO 639 conforming locale string of the current user.
44 *
45 * @return string ISO 639 locale string e.g. "en"
46 */
47function determine_iso_639_locale() {
48    $locale = get_user_locale();
49    return get_iso_639_locale( $locale );
50}
51
52/**
53 * Enqueue the tracking scripts for the given script handle.
54 *
55 * @param string $handle A script handle.
56 */
57function wpcom_enqueue_tracking_scripts( string $handle ) {
58    Connection_Initial_State::render_script( $handle );
59
60    $status            = new Status();
61    $connection        = new Connection_Manager();
62    $tracking          = new Tracking( 'jetpack-mu-wpcom', $connection );
63    $can_use_analytics = $tracking->should_enable_tracking( new Terms_Of_Service(), $status );
64
65    if ( $can_use_analytics ) {
66        Tracking::register_tracks_functions_scripts( true );
67    }
68}
69
70/**
71 * Record tracks event.
72 *
73 * @param mixed $event_name The event.
74 * @param mixed $event_properties The event property.
75 *
76 * @return void
77 */
78function wpcom_record_tracks_event( $event_name, $event_properties ) {
79    if ( function_exists( 'wpcomsh_record_tracks_event' ) ) {
80        wpcomsh_record_tracks_event( $event_name, $event_properties );
81    } elseif ( function_exists( 'require_lib' ) && function_exists( 'tracks_record_event' ) ) {
82        require_lib( 'tracks/client' );
83        tracks_record_event( get_current_user_id(), $event_name, $event_properties );
84    }
85}
86
87/**
88 * The Calypso launch-flow URL for this site.
89 *
90 * @param array $args Query args to add, or to override `siteSlug` and `ref` with.
91 * @return string
92 */
93function wpcom_get_launch_site_url( array $args = array() ): string {
94    $args = array_merge(
95        array(
96            'siteSlug' => (string) wp_parse_url( home_url(), PHP_URL_HOST ),
97            'ref'      => 'wp-admin',
98        ),
99        $args
100    );
101
102    return add_query_arg( array_map( 'rawurlencode', $args ), 'https://wordpress.com/setup/launch-site' );
103}
104
105/**
106 * The current request's URL on the site's own host, for the launch flow's `back_to`.
107 *
108 * Calypso drops a `back_to` unless it is https on the launched site's host, so the request host is not used.
109 *
110 * @return string
111 */
112function wpcom_get_launch_site_back_to(): string {
113    $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/';
114
115    return 'https://' . wp_parse_url( home_url(), PHP_URL_HOST ) . $request_uri;
116}