Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.92% covered (success)
97.92%
47 / 48
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Survicate
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
6 / 6
20
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 should_load
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 get_editor_context
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
 get_visitor_traits
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 enqueue_scripts
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Survicate survey integration
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace A8C\FSE;
9
10/**
11 * Class Survicate
12 */
13class Survicate {
14    /**
15     * Survicate workspace key.
16     */
17    const WORKSPACE_KEY = 'e4794374cce15378101b63de24117572';
18
19    /**
20     * Class instance.
21     *
22     * @var Survicate
23     */
24    private static $instance = null;
25
26    /**
27     * Survicate constructor.
28     */
29    public function __construct() {
30        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 100 );
31    }
32
33    /**
34     * Creates instance.
35     *
36     * @return void
37     */
38    public static function init() {
39        if ( self::$instance === null ) {
40            self::$instance = new self();
41        }
42    }
43
44    /**
45     * Check whether Survicate should load on the current page.
46     *
47     * @return bool
48     */
49    private function should_load() {
50        if ( ! is_user_logged_in() ) {
51            return false;
52        }
53
54        if ( ! is_admin() ) {
55            return false;
56        }
57
58        // Only load for English locale users.
59        if ( strpos( strtolower( get_user_locale() ), 'en' ) !== 0 ) {
60            return false;
61        }
62
63        return true;
64    }
65
66    /**
67     * Detect the current editor context.
68     *
69     * @return string One of 'site-editor', 'block-editor', or 'wp-admin'.
70     */
71    private function get_editor_context() {
72        global $pagenow;
73
74        if ( $pagenow === 'site-editor.php' ) {
75            return 'site-editor';
76        }
77
78        if ( function_exists( 'get_current_screen' ) ) {
79            $current_screen = get_current_screen();
80            if ( $current_screen && $current_screen->is_block_editor() && $current_screen->id !== 'widgets' ) {
81                return 'block-editor';
82            }
83        }
84
85        return 'wp-admin';
86    }
87
88    /**
89     * Get visitor traits for Survicate.
90     *
91     * @return array
92     */
93    private function get_visitor_traits() {
94        $user_data = get_userdata( get_current_user_id() );
95        $email     = $user_data ? $user_data->user_email : '';
96        $site_id   = get_wpcom_blog_id();
97        $site_type = ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) ? 'atomic' : 'simple';
98
99        return array(
100            'email'          => $email,
101            'site_id'        => $site_id ? (string) $site_id : '',
102            'site_type'      => $site_type,
103            'editor_context' => $this->get_editor_context(),
104        );
105    }
106
107    /**
108     * Enqueue Survicate scripts.
109     */
110    public function enqueue_scripts() {
111        if ( ! $this->should_load() ) {
112            return;
113        }
114
115        $traits_json   = wp_json_encode( $this->get_visitor_traits(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
116        $workspace_key = self::WORKSPACE_KEY;
117
118        // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
119        wp_register_script(
120            'wpcom-survicate',
121            false,
122            array(),
123            '1.0',
124            false
125        );
126
127        wp_add_inline_script(
128            'wpcom-survicate',
129            <<<JS
130( function () {
131    if ( window.innerWidth < 480 ) {
132        return;
133    }
134    var script = document.createElement( 'script' );
135    script.src = 'https://survey.survicate.com/workspaces/{$workspace_key}/web_surveys.js';
136    script.async = true;
137    document.head.appendChild( script );
138    var traits = {$traits_json};
139    window.addEventListener( 'SurvicateReady', function () {
140        window._sva.setVisitorTraits( traits );
141    } );
142} )();
143JS
144        );
145
146        wp_enqueue_script( 'wpcom-survicate' );
147    }
148}
149
150add_action( 'init', array( __NAMESPACE__ . '\Survicate', 'init' ) );