Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
34.92% covered (danger)
34.92%
22 / 63
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Odyssey_Assets
34.92% covered (danger)
34.92%
22 / 63
33.33% covered (danger)
33.33%
1 / 3
51.69
0.00% covered (danger)
0.00%
0 / 1
 load_admin_scripts
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
20
 get_cdn_asset_cache_buster
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
7.01
 get_cache_buster_option_value
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Stats Assets
4 *
5 * @package automattic/jetpack-stats-admin
6 */
7
8namespace Automattic\Jetpack\Stats_Admin;
9
10use Automattic\Jetpack\Assets;
11
12/**
13 * Class Odyssey_Config_Data
14 *
15 * @package automattic/jetpack-stats-admin
16 */
17class Odyssey_Assets {
18    // This is a fixed list @see https://github.com/Automattic/wp-calypso/pull/71442/
19    const JS_DEPENDENCIES = array( 'lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-element', 'wp-html-entities', 'wp-i18n', 'wp-is-shallow-equal', 'wp-polyfill', 'wp-primitives', 'wp-url', 'wp-warning', 'moment' );
20    // Sometimes custom scripts would strip the `ver` query params, so we need to make sure it doesn't by adding a custom version param `osv` here.
21    const ODYSSEY_CDN_URL = 'https://widgets.wp.com/odyssey-stats/%s/%s?minify=false&osv=%s';
22
23    /**
24     * We bump the asset version when the Jetpack back end is not compatible anymore.
25     */
26    const ODYSSEY_STATS_VERSION                = 'v1';
27    const ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY = 'odyssey_stats_admin_asset_cache_buster';
28
29    /**
30     * Load the admin scripts.
31     *
32     * @param string $asset_handle The handle of the asset.
33     * @param string $asset_name The name of the asset.
34     * @param array  $options The options.
35     */
36    public function load_admin_scripts( $asset_handle, $asset_name, $options = array() ) {
37        $default_options = array(
38            'config_data'          => ( new Odyssey_Config_Data() )->get_data(),
39            'config_variable_name' => 'configData',
40            'enqueue_css'          => true,
41        );
42        $options         = wp_parse_args( $options, $default_options );
43        if ( file_exists( __DIR__ . "/../dist/{$asset_name}.js" ) ) {
44            // Load local assets for the convinience of development.
45            Assets::register_script(
46                $asset_handle,
47                "../dist/{$asset_name}.js",
48                __FILE__,
49                array(
50                    'in_footer'  => true,
51                    'textdomain' => 'jetpack-stats-admin',
52                )
53            );
54            Assets::enqueue_script( $asset_handle );
55        } else {
56            // In production, we load the assets from our CDN.
57            wp_register_script(
58                $asset_handle,
59                sprintf( self::ODYSSEY_CDN_URL, self::ODYSSEY_STATS_VERSION, "{$asset_name}.js", $this->get_cdn_asset_cache_buster() ),
60                self::JS_DEPENDENCIES,
61                $this->get_cdn_asset_cache_buster(),
62                true
63            );
64            wp_enqueue_script( $asset_handle );
65
66            // Enqueue CSS if needed.
67            if ( $options['enqueue_css'] ) {
68                $css_url    = $asset_name . ( is_rtl() ? '.rtl' : '' ) . '.css';
69                $css_handle = $asset_handle . '-style';
70                wp_register_style(
71                    $css_handle,
72                    sprintf( self::ODYSSEY_CDN_URL, self::ODYSSEY_STATS_VERSION, $css_url, $this->get_cdn_asset_cache_buster() ),
73                    array(),
74                    $this->get_cdn_asset_cache_buster()
75                );
76                wp_enqueue_style( $css_handle );
77            }
78        }
79
80        wp_add_inline_script(
81            $asset_handle,
82            ( new Odyssey_Config_Data() )->get_js_config_data( $options['config_variable_name'], $options['config_data'] ),
83            'before'
84        );
85    }
86
87    /**
88     * Returns cache buster string for assets.
89     * Development mode doesn't need this, as it's handled by `Assets` class.
90     *
91     * @return string
92     */
93    protected function get_cdn_asset_cache_buster() {
94        $now_in_ms = floor( microtime( true ) * 1000 );
95        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
96        if ( isset( $_GET['force_refresh'] ) ) {
97            update_option( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY, $this->get_cache_buster_option_value( $now_in_ms ), false );
98        }
99
100        // Use cached cache buster in production.
101        $remote_asset_version = get_option( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY );
102
103        if ( ! empty( $remote_asset_version ) ) {
104            $remote_asset_version = json_decode( $remote_asset_version, true );
105            // If cache buster is cached and not expired (valid in 15 min), return it.
106            if ( ! empty( $remote_asset_version['cache_buster'] ) && $remote_asset_version['cached_at'] > $now_in_ms - MINUTE_IN_SECONDS * 1000 * 15 ) {
107                return $remote_asset_version['cache_buster'];
108            }
109        }
110
111        // If no cached cache buster, we fetch it from CDN and set to transient.
112        $response = wp_remote_get( sprintf( self::ODYSSEY_CDN_URL, self::ODYSSEY_STATS_VERSION, 'build_meta.json', $now_in_ms ), array( 'timeout' => 5 ) );
113
114        if ( is_wp_error( $response ) ) {
115            // fallback to current timestamp.
116            return (string) $now_in_ms;
117        }
118
119        $build_meta = json_decode( wp_remote_retrieve_body( $response ), true );
120        if ( ! empty( $build_meta['cache_buster'] ) ) {
121            // Cache the cache buster for 15 mins.
122            update_option( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY, $this->get_cache_buster_option_value( $build_meta['cache_buster'] ), false );
123            return $build_meta['cache_buster'];
124        }
125
126        // fallback to current timestamp.
127        return (string) $now_in_ms;
128    }
129
130    /**
131     * Get the cache buster option value.
132     *
133     * @param string|int|float $cache_buster The cache buster.
134     * @return string|false
135     */
136    protected function get_cache_buster_option_value( $cache_buster ) {
137        return wp_json_encode(
138            array(
139                'cache_buster' => (string) $cache_buster,
140                'cached_at'    => floor( microtime( true ) * 1000 ), // milliseconds.
141            ),
142            JSON_UNESCAPED_SLASHES
143        );
144    }
145}