|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Web Worker Offloading integration with Rank Math SEO. |
| 4 | + * |
| 5 | + * @since n.e.x.t |
| 6 | + * @package web-worker-offloading |
| 7 | + */ |
| 8 | + |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { |
| 10 | + exit; // Exit if accessed directly. |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Configures WWO for Rank Math SEO and Google Analytics. |
| 15 | + * |
| 16 | + * @since n.e.x.t |
| 17 | + * @access private |
| 18 | + * @link https://partytown.builder.io/google-tag-manager#forward-events |
| 19 | + * |
| 20 | + * @param array<string, mixed>|mixed $configuration Configuration. |
| 21 | + * @return array<string, mixed> Configuration. |
| 22 | + */ |
| 23 | +function plwwo_rank_math_configure( $configuration ): array { |
| 24 | + $configuration = (array) $configuration; |
| 25 | + |
| 26 | + $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. |
| 27 | + $configuration['forward'][] = 'dataLayer.push'; // Because the Partytown integration has this in its example config. |
| 28 | + return $configuration; |
| 29 | +} |
| 30 | +add_filter( 'plwwo_configuration', 'plwwo_rank_math_configure' ); |
| 31 | + |
| 32 | +/* |
| 33 | + * Note: The following integration is not targeting the \RankMath\Analytics\GTag::enqueue_gtag_js() code which is only |
| 34 | + * used for WP<5.7. In WP 5.7, the wp_script_attributes and wp_inline_script_attributes filters were introduced, and |
| 35 | + * Rank Math then deemed it preferable to use wp_print_script_tag() and wp_print_inline_script_tag() rather than |
| 36 | + * wp_enqueue_script() and wp_add_inline_script(), respectively. Since Web Worker Offloading requires WP 6.5+, there |
| 37 | + * is no point to integrate with the pre-5.7 code in Rank Math. |
| 38 | + */ |
| 39 | + |
| 40 | +/** |
| 41 | + * Filters script attributes to offload Rank Math's GTag script tag to Partytown. |
| 42 | + * |
| 43 | + * @since n.e.x.t |
| 44 | + * @access private |
| 45 | + * @link https://github.com/rankmath/seo-by-rank-math/blob/c78adba6f78079f27ff1430fabb75c6ac3916240/includes/modules/analytics/class-gtag.php#L161-L167 |
| 46 | + * |
| 47 | + * @param array|mixed $attributes Script attributes. |
| 48 | + * @return array|mixed Filtered script attributes. |
| 49 | + */ |
| 50 | +function plwwo_rank_math_filter_script_attributes( $attributes ) { |
| 51 | + if ( isset( $attributes['id'] ) && 'google_gtagjs' === $attributes['id'] ) { |
| 52 | + wp_enqueue_script( 'web-worker-offloading' ); |
| 53 | + $attributes['type'] = 'text/partytown'; |
| 54 | + } |
| 55 | + return $attributes; |
| 56 | +} |
| 57 | + |
| 58 | +add_filter( 'wp_script_attributes', 'plwwo_rank_math_filter_script_attributes' ); |
| 59 | + |
| 60 | +/** |
| 61 | + * Filters inline script attributes to offload Rank Math's GTag script tag to Partytown. |
| 62 | + * |
| 63 | + * @since n.e.x.t |
| 64 | + * @access private |
| 65 | + * @link https://github.com/rankmath/seo-by-rank-math/blob/c78adba6f78079f27ff1430fabb75c6ac3916240/includes/modules/analytics/class-gtag.php#L169-L174 |
| 66 | + * |
| 67 | + * @param array|mixed $attributes Script attributes. |
| 68 | + * @return array|mixed Filtered inline script attributes. |
| 69 | + */ |
| 70 | +function plwwo_rank_math_filter_inline_script_attributes( $attributes ) { |
| 71 | + if ( isset( $attributes['id'] ) && 'google_gtagjs-inline' === $attributes['id'] ) { |
| 72 | + wp_enqueue_script( 'web-worker-offloading' ); |
| 73 | + $attributes['type'] = 'text/partytown'; |
| 74 | + } |
| 75 | + return $attributes; |
| 76 | +} |
| 77 | + |
| 78 | +add_filter( 'wp_inline_script_attributes', 'plwwo_rank_math_filter_inline_script_attributes' ); |
0 commit comments