From 95feeb2148758bab2937b63239b2e47d2f9b8c10 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 20 Nov 2024 16:41:22 -0800 Subject: [PATCH] Integrate Web Worker Offloading with Rank Math SEO --- plugins/web-worker-offloading/third-party.php | 6 +- .../third-party/seo-by-rank-math.php | 76 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 plugins/web-worker-offloading/third-party/seo-by-rank-math.php diff --git a/plugins/web-worker-offloading/third-party.php b/plugins/web-worker-offloading/third-party.php index d6916a3fbb..d1830d81cf 100644 --- a/plugins/web-worker-offloading/third-party.php +++ b/plugins/web-worker-offloading/third-party.php @@ -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 . return class_exists( 'WooCommerce' ); }, + 'seo-by-rank-math' => static function (): bool { + return class_exists( 'RankMath' ); + }, ); foreach ( $plugins_with_integrations as $plugin_slug => $active_callback ) { diff --git a/plugins/web-worker-offloading/third-party/seo-by-rank-math.php b/plugins/web-worker-offloading/third-party/seo-by-rank-math.php new file mode 100644 index 0000000000..0852d1526e --- /dev/null +++ b/plugins/web-worker-offloading/third-party/seo-by-rank-math.php @@ -0,0 +1,76 @@ +|mixed $configuration Configuration. + * @return array 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' );