Skip to content

Commit

Permalink
Integrate Web Worker Offloading with Rank Math SEO
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 21, 2024
1 parent e82a04b commit 95feeb2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
6 changes: 4 additions & 2 deletions plugins/web-worker-offloading/third-party.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ static function ( $to_do ) use ( $script_handles ) {
function plwwo_load_third_party_integrations(): void {
$plugins_with_integrations = array(
// TODO: google-site-kit.
// TODO: seo-by-rank-math.
'woocommerce' => static function (): bool {
'woocommerce' => static function (): bool {
// See <https://woocommerce.com/document/query-whether-woocommerce-is-activated/>.
return class_exists( 'WooCommerce' );
},
'seo-by-rank-math' => static function (): bool {
return class_exists( 'RankMath' );
},
);

foreach ( $plugins_with_integrations as $plugin_slug => $active_callback ) {
Expand Down
76 changes: 76 additions & 0 deletions plugins/web-worker-offloading/third-party/seo-by-rank-math.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Web Worker Offloading integration with Rank Math SEO.
*
* @since n.e.x.t
* @package web-worker-offloading
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Configures WWO for Rank Math SEO and Google Analytics.
*
* @since 0.1.0
* @access private
* @link https://partytown.builder.io/google-tag-manager#forward-events
*
* @param array<string, mixed>|mixed $configuration Configuration.
* @return array<string, mixed> Configuration.
*/
function plwwo_rank_math_configure( $configuration ): array {
$configuration = (array) $configuration;

$configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another.
$configuration['forward'][] = 'dataLayer.push'; // Because the Partytown integration has this in its example config.
return $configuration;
}
add_filter( 'plwwo_configuration', 'plwwo_rank_math_configure' );

/*
* Note: The following integration is not targeting the \RankMath\Analytics\GTag::enqueue_gtag_js() code which is only
* used for WP<5.7. In WP 5.7, the wp_script_attributes and wp_inline_script_attributes filters were introduced, and
* Rank Math then deemed it preferable to use wp_print_script_tag() and wp_print_inline_script_tag() rather than
* wp_enqueue_script() and wp_add_inline_script(), respectively. Since Web Worker Offloading requires WP 6.5+, there
* is no point to integrate with the pre-5.7 code in Rank Math.
*/

/**
* Filters script attributes to offload Rank Math's GTag script tag to Partytown.
*
* @since n.e.x.t
* @link https://github.com/rankmath/seo-by-rank-math/blob/c78adba6f78079f27ff1430fabb75c6ac3916240/includes/modules/analytics/class-gtag.php#L161-L167
*
* @param array|mixed $attributes Script attributes.
* @return array|mixed Filtered script attributes.
*/
function plwwo_rank_math_filter_script_attributes( $attributes ) {
if ( isset( $attributes['id'] ) && 'google_gtagjs' === $attributes['id'] ) {
wp_enqueue_script( 'web-worker-offloading' );
$attributes['type'] = 'text/partytown';
}
return $attributes;
}

add_filter( 'wp_script_attributes', 'plwwo_rank_math_filter_script_attributes' );

/**
* Filters inline script attributes to offload Rank Math's GTag script tag to Partytown.
*
* @since n.e.x.t
* @link https://github.com/rankmath/seo-by-rank-math/blob/c78adba6f78079f27ff1430fabb75c6ac3916240/includes/modules/analytics/class-gtag.php#L169-L174
*
* @param array|mixed $attributes Script attributes.
* @return array|mixed Filtered inline script attributes.
*/
function plwwo_rank_math_filter_inline_script_attributes( $attributes ) {
if ( isset( $attributes['id'] ) && 'google_gtagjs-inline' === $attributes['id'] ) {
wp_enqueue_script( 'web-worker-offloading' );
$attributes['type'] = 'text/partytown';
}
return $attributes;
}

add_filter( 'wp_inline_script_attributes', 'plwwo_rank_math_filter_inline_script_attributes' );

0 comments on commit 95feeb2

Please sign in to comment.