Skip to content

Commit 8c43763

Browse files
authored
Merge branch 'trunk' into 354-media-model
2 parents 3f96e3f + df23645 commit 8c43763

File tree

7 files changed

+129
-17
lines changed

7 files changed

+129
-17
lines changed

composer.lock

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ parameters:
1010
- plugins/performance-lab/load.php
1111
bootstrapFiles:
1212
- tools/phpstan/constants.php
13-
- plugins/performance-lab/load.php
1413
- plugins/webp-uploads/load.php
1514
scanDirectories:
1615
- vendor/wp-phpunit/wp-phpunit/

plugins/web-worker-offloading/third-party.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ static function ( $to_do ) use ( $script_handles ) {
4040
function plwwo_load_third_party_integrations(): void {
4141
$plugins_with_integrations = array(
4242
// TODO: google-site-kit.
43-
// TODO: seo-by-rank-math.
44-
'woocommerce' => static function (): bool {
43+
'woocommerce' => static function (): bool {
4544
// See <https://woocommerce.com/document/query-whether-woocommerce-is-activated/>.
4645
return class_exists( 'WooCommerce' );
4746
},
47+
'seo-by-rank-math' => static function (): bool {
48+
return class_exists( 'RankMath' );
49+
},
4850
);
4951

5052
foreach ( $plugins_with_integrations as $plugin_slug => $active_callback ) {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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'; // See <https://partytown.builder.io/forwarding-event>.
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' );

plugins/webp-uploads/helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function webp_uploads_get_upload_image_mime_transforms(): array {
2828

2929
$default_transforms = array(
3030
'image/jpeg' => array( 'image/' . $output_format ),
31-
'image/webp' => array( 'image/webp' ),
31+
'image/webp' => array( 'image/' . $output_format ),
3232
'image/avif' => array( 'image/avif' ),
3333
'image/png' => array( 'image/' . $output_format ),
3434
);

plugins/webp-uploads/tests/test-helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public function test_it_should_return_default_transforms_when_filter_returns_non
365365
$this->set_image_output_type( 'avif' );
366366
$default_transforms = array(
367367
'image/jpeg' => array( 'image/avif' ),
368-
'image/webp' => array( 'image/webp' ),
368+
'image/webp' => array( 'image/avif' ),
369369
'image/avif' => array( 'image/avif' ),
370370
'image/png' => array( 'image/avif' ),
371371
);

plugins/webp-uploads/tests/test-load.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,4 +1087,36 @@ public function test_it_should_generate_fallback_images_for_all_sizes_when_gener
10871087

10881088
wp_delete_attachment( $attachment_id );
10891089
}
1090+
1091+
/**
1092+
* Convert WebP to AVIF on uploads.
1093+
*/
1094+
public function test_that_it_should_convert_webp_to_avif_on_upload(): void {
1095+
// Ensure the AVIF MIME type is supported; skip the test if not.
1096+
if ( ! webp_uploads_mime_type_supported( 'image/avif' ) ) {
1097+
$this->markTestSkipped( 'Mime type image/avif is not supported.' );
1098+
}
1099+
1100+
$this->set_image_output_type( 'avif' );
1101+
1102+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/balloons.webp' );
1103+
1104+
// There should be a AVIF source, but no WebP source for the full image.
1105+
$this->assertImageNotHasSource( $attachment_id, 'image/webp' );
1106+
$this->assertImageHasSource( $attachment_id, 'image/avif' );
1107+
1108+
$metadata = wp_get_attachment_metadata( $attachment_id );
1109+
1110+
// The full image should be a AVIF.
1111+
$this->assertArrayHasKey( 'file', $metadata );
1112+
$this->assertStringEndsWith( $metadata['sources']['image/avif']['file'], $metadata['file'] );
1113+
$this->assertStringEndsWith( $metadata['sources']['image/avif']['file'], get_attached_file( $attachment_id ) );
1114+
1115+
// There should be a AVIF source, but no WebP source for all sizes.
1116+
foreach ( array_keys( $metadata['sizes'] ) as $size_name ) {
1117+
$this->assertImageNotHasSizeSource( $attachment_id, $size_name, 'image/webp' );
1118+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/avif' );
1119+
}
1120+
wp_delete_attachment( $attachment_id );
1121+
}
10901122
}

0 commit comments

Comments
 (0)