1<?php
2
3namespace WPaaS;
4
5if ( ! defined( 'ABSPATH' ) ) {
6
7 exit;
8
9}
10
11final class RUM {
12 use Helpers;
13 /**
14 * Class constructor.
15 */
16 public function __construct() {
17
18 if ( ! self::is_enabled() ) {
19
20 return;
21
22 }
23
24 add_action( 'wp_footer', [ $this, 'print_inline_script' ], PHP_INT_MAX );
25 add_action( 'admin_footer', [ $this, 'print_inline_script' ], PHP_INT_MAX );
26 add_action( 'admin_enqueue_scripts' , [ $this, 'print_console_logging_cript' ], PHP_INT_MAX );
27
28 }
29
30 public function get_assets($path) {
31 $fallback_assets = [
32 'dependencies' => [],
33 'version' => Plugin::version(),
34 ];
35
36 $asset_path = Plugin::assets_dir( $path . '.min.asset.php' );
37
38 // Check asset path existence.
39 if ( ! file_exists( $asset_path ) ) {
40 return $fallback_assets;
41 }
42
43 $asset_file = require $asset_path;
44 if ( ! is_array( $asset_file ) ) {
45 return $fallback_assets;
46 }
47 return $asset_file;
48 }
49
50 /**
51 * Add the console logging script to the header of all admin pages.
52 *
53 * @action admin_header
54 */
55 public function print_console_logging_cript() {
56 // Bail if the script is missing.
57 if ( ! file_exists( Plugin::assets_dir('/js/block-validation-tracker.min.js') ) ) {
58 return;
59 }
60
61 $asset_file = $this->get_assets('/js/block-validation-tracker');
62
63 wp_enqueue_script(
64 'logging_script',
65 Plugin::assets_url('/js/block-validation-tracker.min.js'),
66 // Use the generated assets file for deps and version.
67 $asset_file['dependencies'],
68 $asset_file['version'],
69 array(
70 'strategy' => 'defer',
71 'in_footer' => true,
72 )
73 );
74 }
75
76 /**
77 * Add the RUM code to the footer of all pages.
78 *
79 * @action wp_footer
80 * @action admin_footer
81 */
82 public function print_inline_script() {
83
84 global $wp_version, $post;
85
86 $env = Plugin::get_env();
87 $host = in_array( $env, [ 'dev', 'test' ], true ) ? "{$env}-secureserver.net" : 'secureserver.net';
88 $coming_soon_page_status = (int) apply_filters( 'gdl_coming_soon_page', false ) ? 1 : 0;
89
90 $scc_asset = in_array( $env, [ 'prod', 'test' ], true ) ? 'scc-c2.min.js' : 'scc-c2.js';
91 $scc_host = $env === 'prod' ? 'img1.wsimg.com' : ($env === 'test' ? 'img1.test-wsimg.com' : 'img1.dev-wsimg.com');
92 $scc_url = "https://$scc_host/signals/js/clients/scc-c2/$scc_asset";
93 $ap = self::is_wpaas_v2() ? 'wpaas_v2' : 'wpaas';
94 $v2_app_id = self::get_v2_app_id();
95 ?>
96 <script>'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),
97 _trfd.push({'tccl.baseHost':'<?php echo esc_js( $host ); ?>'}),
98 _trfd.push({'ap':'<?php echo $ap; ?>'},
99 {'server':'<?php echo esc_js( gethostname() ); ?>'},
100 {'pod':'<?php echo esc_js( getenv('WPAAS_POD') ?: 'null' ); ?>'},
101 <?php if ( ! self::is_wpaas_v2()) : ?>{'storage':'<?php echo esc_js( getenv('WPAAS_STORAGE') ?: 'null' ); ?>'}, <?php endif; ?>
102 {'xid':'<?php echo absint( Plugin::xid() ); ?>'},
103 {'wp':'<?php echo esc_js( $wp_version ); ?>'},
104 {'php':'<?php echo esc_js( PHP_VERSION ); ?>'},
105 {'loggedin':'<?php echo is_user_logged_in() ? 1 : 0; ?>'},
106 {'cdn':'<?php echo CDN::is_enabled() ? 1 : 0; ?>'},
107 {'builder':'<?php echo esc_js( Plugin::get_page_builder( $post ) ); ?>'},
108 {'theme':'<?php echo esc_js( sanitize_title( get_template() ) ); ?>'},
109 {'wds':'<?php echo defined( 'GD_cORe_VERSION' ) ? 1 : 0; ?>'},
110 {'wp_alloptions_count':'<?php echo count( wp_load_alloptions() ); ?>'},
111 {'wp_alloptions_bytes':'<?php echo strlen( serialize( wp_load_alloptions() ) ); ?>'},
112 {'gdl_coming_soon_page':'<?php echo esc_js( $coming_soon_page_status ); ?>'}
113 <?php if ( self::is_wpaas_v2() ) : ?>, {'appid':'<?php echo esc_js( $v2_app_id ); ?>'} <?php endif; ?>
114 );
115 var trafficScript = document.createElement('script'); trafficScript.src = '<?php echo esc_js( $scc_url ); ?>'; window.document.head.appendChild(trafficScript);</script>
116 <script>window.addEventListener('click', function (elem) { var _elem$target, _elem$target$dataset, _window, _window$_trfq; return (elem === null || elem === void 0 ? void 0 : (_elem$target = elem.target) === null || _elem$target === void 0 ? void 0 : (_elem$target$dataset = _elem$target.dataset) === null || _elem$target$dataset === void 0 ? void 0 : _elem$target$dataset.eid) && ((_window = window) === null || _window === void 0 ? void 0 : (_window$_trfq = _window._trfq) === null || _window$_trfq === void 0 ? void 0 : _window$_trfq.push(["cmdLogEvent", "click", elem.target.dataset.eid]));});</script>
117 <script src='https://img1.wsimg.com/traffic-assets/js/tccl-tti.min.js' onload="window.tti.calculateTTI()"></script>
118 <?php
119
120 if ( ! self::should_load_full_story_sessions() ) {
121
122 return;
123
124 }
125
126 ?>
127 <script>
128 window['_fs_host'] = 'fullstory.com';
129 window['_fs_script'] = 'edge.fullstory.com/s/fs.js';
130 window['_fs_org'] = 'YKBRC';
131 window['_fs_namespace'] = 'FS';
132 (function(m,n,e,t,l,o,g,y){
133 if (e in m) {if(m.console && m.console.log) { m.console.log('FullStory namespace conflict. Please set window["_fs_namespace"].');} return;}
134 g=m[e]=function(a,b,s){g.q?g.q.push([a,b,s]):g._api(a,b,s);};g.q=[];
135 o=n.createElement(t);o.async=1;o.crossOrigin='anonymous';o.src='https://'+_fs_script;
136 y=n.getElementsByTagName(t)[0];y.parentNode.insertBefore(o,y);
137 g.identify=function(i,v,s){g(l,{uid:i},s);if(v)g(l,v,s)};g.setUserVars=function(v,s){g(l,v,s)};g.event=function(i,v,s){g('event',{n:i,p:v},s)};
138 g.anonymize=function(){g.identify(!!0)};
139 g.shutdown=function(){g("rec",!1)};g.restart=function(){g("rec",!0)};
140 g.log = function(a,b){g("log",[a,b])};
141 g.consent=function(a){g("consent",!arguments.length||a)};
142 g.identifyAccount=function(i,v){o='account';v=v||{};v.acctId=i;g(o,v)};
143 g.clearUserCookie=function(){};
144 g.setVars=function(n, p){g('setVars',[n,p]);};
145 g._w={};y='XMLHttpRequest';g._w[y]=m[y];y='fetch';g._w[y]=m[y];
146 if(m[y])m[y]=function(){return g._w[y].apply(this,arguments)};
147 g._v="1.3.0";
148 })(window,document,window['_fs_namespace'],'script','user');
149 FS.identify('<?php echo GD_CUSTOMER_ID; ?>');
150 </script>
151 <?php
152
153 }
154
155 /**
156 * Determine if the fullstory.com script should load.
157 *
158 * Conditions: Is an admin page, created less than 60 days ago, has a valid custom ID,
159 * still using the temporary domain as the primary domain, and the fullstory_wpadmin feature flag is true.
160 *
161 * @return bool True when the conditions are met, otherwise false.
162 */
163 private static function should_load_full_story_sessions() {
164
165 $is_admin = is_admin();
166 $is_less_than_sixty_days_old = ( defined( 'GD_SITE_CREATED' ) && GD_SITE_CREATED >= strtotime( '-60 days' ) );
167 $is_valid_customer_id = ( defined( 'GD_CUSTOMER_ID' ) && wp_is_uuid( GD_CUSTOMER_ID ) );
168 $is_using_temp_domain = ( defined( 'GD_TEMP_DOMAIN' ) && GD_TEMP_DOMAIN === parse_url( home_url(), PHP_URL_HOST ) );
169 $is_enabled = $GLOBALS['wpaas_feature_flag']->get_feature_flag_value( 'fullstory_wpadmin', false );
170
171 return ( $is_admin && $is_less_than_sixty_days_old && $is_valid_customer_id && $is_using_temp_domain && $is_enabled );
172
173 }
174
175 /**
176 * Return whether RUM should be enabled on the current page load.
177 *
178 * @return bool
179 */
180 public static function is_enabled() {
181
182 $rum_enabled = Plugin::is_rum_enabled();
183 $temp_domain = defined( 'GD_TEMP_DOMAIN' ) ? GD_TEMP_DOMAIN : null;
184 $is_nocache = (bool) filter_input( INPUT_GET, 'nocache' );
185 $is_gddebug = (bool) filter_input( INPUT_GET, 'gddebug' );
186 $is_amp = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
187
188 return ( $rum_enabled && $temp_domain && ! $is_nocache && ! $is_gddebug && ! $is_amp && ! WP_DEBUG );
189
190 }
191
192}
193