Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Woo Analytics: use core WP function to enqueue script #13173

Merged
merged 2 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ class Jetpack {
'jetpack-widget-social-icons-styles',
);

/**
* The handles of scripts that can be loaded asynchronously.
*
* @var array
*/
public $async_script_handles = array(
'woocommerce-analytics',
);

/**
* Contains all assets that have had their URL rewritten to minified versions.
*
Expand Down Expand Up @@ -719,6 +728,11 @@ private function __construct() {
if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) {
add_action( 'shutdown', array( $this, 'push_stats' ) );
}

/*
* Load some scripts asynchronously.
*/
add_action( 'script_loader_tag', array( $this, 'script_add_async' ), 10, 3 );
}

function setup_xmlrpc_handlers( $request_params, $is_active, $is_signed, Jetpack_XMLRPC_Server $xmlrpc_server = null ) {
Expand Down Expand Up @@ -6695,6 +6709,24 @@ function concat_remove_style_loader_tag( $tag, $handle ) {
return $tag;
}

/**
* Add an async attribute to scripts that can be loaded asynchronously.
* https://www.w3schools.com/tags/att_script_async.asp
*
* @since 7.7.0
*
* @param string $tag The <script> tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
*/
public function script_add_async( $tag, $handle, $src ) {
if ( in_array( $handle, $this->async_script_handles, true ) ) {
return preg_replace( '/^<script /i', '<script async ', $tag );
}

return $tag;
}

/*
* Check the heartbeat data
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function __construct() {
// add to carts from non-product pages or lists (search, store etc.)
add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 );

// loading s.js
add_action( 'wp_head', array( $this, 'wp_head_bottom' ), 999999 );
// loading s.js.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) );

// Capture cart events
add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 );
Expand Down Expand Up @@ -64,12 +64,16 @@ public function wp_head_top() {


/**
* Place script to call s.js, Store Analytics
* Place script to call s.js, Store Analytics.
*/
public function wp_head_bottom() {
$filename = 's-' . gmdate( 'YW' ) . '.js';
$async_code = "<script async src='https://stats.wp.com/" . $filename . "'></script>";
echo "$async_code\r\n";
public function enqueue_tracking_script() {
$filename = sprintf(
'https://stats.wp.com/s-%d.js',
gmdate( 'YW' )
);

// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_enqueue_script( 'woocommerce-analytics', esc_url( $filename ), array(), null, false );
}

/**
Expand Down