Skip to content

Commit

Permalink
Use plwwo instead of wwo as plugin prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Sep 30, 2024
1 parent 282a068 commit 0f37778
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 53 deletions.
6 changes: 3 additions & 3 deletions plugins/web-worker-offloading/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @return array<string, mixed> Configuration for Partytown.
*/
function wwo_get_configuration(): array {
function plwwo_get_configuration(): array {
$config = array(
'lib' => wp_parse_url( plugin_dir_url( __FILE__ ), PHP_URL_PATH ) . 'build/',
);
Expand All @@ -33,7 +33,7 @@ function wwo_get_configuration(): array {
*
* Many of the configuration options are not documented publicly, so refer to the TypeScript definitions.
* Additionally, not all of the configuration options (e.g. functions) can be serialized as JSON and must instead be
* defined in JavaScript instead. To do so, use the following PHP code instead of filtering `wwo_configuration`:
* defined in JavaScript instead. To do so, use the following PHP code instead of filtering `plwwo_configuration`:
*
* add_action(
* 'wp_enqueue_scripts',
Expand Down Expand Up @@ -66,5 +66,5 @@ function wwo_get_configuration(): array {
*
* @param array<string, mixed> $config Configuration for Partytown.
*/
return (array) apply_filters( 'wwo_configuration', $config );
return (array) apply_filters( 'plwwo_configuration', $config );
}
18 changes: 9 additions & 9 deletions plugins/web-worker-offloading/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @param WP_Scripts $scripts WP_Scripts instance.
*/
function wwo_register_default_scripts( WP_Scripts $scripts ): void {
function plwwo_register_default_scripts( WP_Scripts $scripts ): void {
$partytown_js = file_get_contents( __DIR__ . '/build/partytown.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
if ( false === $partytown_js ) {
return;
Expand All @@ -36,14 +36,14 @@ function wwo_register_default_scripts( WP_Scripts $scripts ): void {
'web-worker-offloading',
sprintf(
'window.partytown = {...(window.partytown || {}), ...%s};',
wp_json_encode( wwo_get_configuration() )
wp_json_encode( plwwo_get_configuration() )
),
'before'
);

$scripts->add_inline_script( 'web-worker-offloading', $partytown_js );
}
add_action( 'wp_default_scripts', 'wwo_register_default_scripts' );
add_action( 'wp_default_scripts', 'plwwo_register_default_scripts' );

/**
* Prepends web-worker-offloading to the list of scripts to print if one of the queued scripts is offloaded to a worker.
Expand All @@ -54,7 +54,7 @@ function wwo_register_default_scripts( WP_Scripts $scripts ): void {
* @param string[]|mixed $script_handles An array of enqueued script dependency handles.
* @return string[] Script handles.
*/
function wwo_filter_print_scripts_array( $script_handles ): array {
function plwwo_filter_print_scripts_array( $script_handles ): array {
$scripts = wp_scripts();
foreach ( (array) $script_handles as $handle ) {
if ( true === (bool) $scripts->get_data( $handle, 'worker' ) ) {
Expand All @@ -65,7 +65,7 @@ function wwo_filter_print_scripts_array( $script_handles ): array {
}
return $script_handles;
}
add_filter( 'print_scripts_array', 'wwo_filter_print_scripts_array', PHP_INT_MAX );
add_filter( 'print_scripts_array', 'plwwo_filter_print_scripts_array', PHP_INT_MAX );

/**
* Updates script type for handles having `web-worker-offloading` as dependency.
Expand All @@ -77,7 +77,7 @@ function wwo_filter_print_scripts_array( $script_handles ): array {
* @param string $handle Script handle.
* @return string|mixed Script tag with type="text/partytown" for eligible scripts.
*/
function wwo_update_script_type( $tag, string $handle ) {
function plwwo_update_script_type( $tag, string $handle ) {
if (
is_string( $tag )
&&
Expand All @@ -94,7 +94,7 @@ function wwo_update_script_type( $tag, string $handle ) {
}
return $tag;
}
add_filter( 'script_loader_tag', 'wwo_update_script_type', 10, 2 );
add_filter( 'script_loader_tag', 'plwwo_update_script_type', 10, 2 );

/**
* Filters inline script attributes to offload to a worker if the script has been opted-in.
Expand All @@ -105,7 +105,7 @@ function wwo_update_script_type( $tag, string $handle ) {
* @param array<string, mixed>|mixed $attributes Attributes.
* @return array<string, mixed> Attributes.
*/
function wwo_filter_inline_script_attributes( $attributes ): array {
function plwwo_filter_inline_script_attributes( $attributes ): array {
$attributes = (array) $attributes;
if (
isset( $attributes['id'] )
Expand All @@ -118,4 +118,4 @@ function wwo_filter_inline_script_attributes( $attributes ): array {
}
return $attributes;
}
add_filter( 'wp_inline_script_attributes', 'wwo_filter_inline_script_attributes' );
add_filter( 'wp_inline_script_attributes', 'plwwo_filter_inline_script_attributes' );
4 changes: 2 additions & 2 deletions plugins/web-worker-offloading/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Please monitor your analytics once activating to ensure all the expected events

This plugin relies on the [Partytown 🎉](https://partytown.builder.io/) library by Builder.io, released under the MIT license. This library is in beta and there are quite a few [open bugs](https://github.com/BuilderIO/partytown/issues?q=is%3Aopen+is%3Aissue+label%3Abug).

The [Partytown configuration](https://partytown.builder.io/configuration) can be modified via the `wwo_configuration` filter. For example:
The [Partytown configuration](https://partytown.builder.io/configuration) can be modified via the `plwwo_configuration` filter. For example:

`
<?php
add_filter( 'wwo_configuration', function ( $config ) {
add_filter( 'plwwo_configuration', function ( $config ) {
$config['mainWindowAccessors'][] = 'wp'; // Make the wp global available in the worker (e.g. wp.i18n and wp.hooks).
return $config;
} );
Expand Down
66 changes: 33 additions & 33 deletions plugins/web-worker-offloading/tests/test-web-worker-offloading.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ public function tear_down(): void {
}

/**
* @covers ::wwo_get_configuration
* @covers ::plwwo_get_configuration
*/
public function test_wwo_get_configuration(): void {
public function test_plwwo_get_configuration(): void {
$wp_content_dir = WP_CONTENT_DIR;
$partytown_assets_path = 'web-worker-offloading/build/';
$config = wwo_get_configuration();
$config = plwwo_get_configuration();

$this->assertArrayHasKey( 'lib', $config );
$this->assertStringStartsWith( '/' . basename( $wp_content_dir ), $config['lib'] );
$this->assertStringEndsWith( $partytown_assets_path, $config['lib'] );

// Test `wwo_configuration` filter.
// Test `plwwo_configuration` filter.
add_filter(
'wwo_configuration',
'plwwo_configuration',
static function ( $config ) {
$config['forward'] = array( 'datalayer.push' );
$config['debug'] = true;
return $config;
}
);

$config = wwo_get_configuration();
$config = plwwo_get_configuration();

$this->assertArrayHasKey( 'forward', $config );
$this->assertArrayHasKey( 'debug', $config );
Expand All @@ -57,16 +57,16 @@ static function ( $config ) {
}

/**
* @covers ::wwo_register_default_scripts
* @covers ::plwwo_register_default_scripts
*/
public function test_wwo_register_default_scripts(): void {
$this->assertEquals( 10, has_action( 'wp_default_scripts', 'wwo_register_default_scripts' ) );
public function test_plwwo_register_default_scripts(): void {
$this->assertEquals( 10, has_action( 'wp_default_scripts', 'plwwo_register_default_scripts' ) );

// Register scripts.
wp_scripts();

$wp_content_dir = WP_CONTENT_DIR;
$partytown_config = wwo_get_configuration();
$partytown_config = plwwo_get_configuration();
$partytown_lib = dirname( $wp_content_dir ) . $partytown_config['lib'];
$before_data = wp_scripts()->get_inline_script_data( 'web-worker-offloading', 'before' );
$after_data = wp_scripts()->get_inline_script_data( 'web-worker-offloading', 'after' );
Expand Down Expand Up @@ -113,37 +113,37 @@ public static function data_update_script_types(): array {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', true );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script>',
),
'add-defer-script-for-web-worker-offloading' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', array( 'strategy' => 'defer' ) );
wp_script_add_data( 'foo', 'worker', 1 );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" defer data-wp-strategy="defer"></script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" defer data-wp-strategy="defer"></script>',
),
'add-async-script-for-web-worker-offloading' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', array( 'strategy' => 'async' ) );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" async data-wp-strategy="async"></script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" async data-wp-strategy="async"></script>',
),
'add-script-for-web-worker-offloading-with-before-data' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', true );
wp_add_inline_script( 'foo', 'console.log("Hello, Before World!");', 'before' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script>',
),
'add-script-for-web-worker-offloading-with-after-data' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', true );
wp_add_inline_script( 'foo', 'console.log("Hello, After World!");', 'after' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
),
'add-script-for-web-worker-offloading-with-before-and-after-data' => array(
'set_up' => static function (): void {
Expand All @@ -152,7 +152,7 @@ public static function data_update_script_types(): array {
wp_add_inline_script( 'foo', 'console.log("Hello, After World!");', 'after' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
),
'add-async-script-for-web-worker-offloading-with-before-and-after-data' => array(
'set_up' => static function (): void {
Expand All @@ -161,7 +161,7 @@ public static function data_update_script_types(): array {
wp_add_inline_script( 'foo', 'console.log("Hello, After World!");', 'after' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" data-wp-strategy="async"></script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" data-wp-strategy="async"></script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
),
'add-defer-script-for-web-worker-offloading-with-before-and-after-data' => array(
'set_up' => static function (): void {
Expand All @@ -170,7 +170,7 @@ public static function data_update_script_types(): array {
wp_add_inline_script( 'foo', 'console.log("Hello, After World!");', 'after' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" data-wp-strategy="defer"></script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" data-wp-strategy="defer"></script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
),
'add-inline-script-offloaded-to-web-worker' => array(
'set_up' => static function (): void {
Expand All @@ -180,17 +180,17 @@ public static function data_update_script_types(): array {
wp_script_add_data( 'foo', 'worker', true );
wp_enqueue_script( 'foo' );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
'expected' => '{{ plwwo_config }}{{ plwwo_inline_script }}<script id="foo-js-before" type="text/partytown">console.log("Hello, Before World!");</script><script id="foo-js-after" type="text/partytown">console.log("Hello, After World!");</script>',
),
);
}

/**
* Test `wwo_update_script_type`.
*
* @covers ::wwo_update_script_type
* @covers ::wwo_filter_print_scripts_array
* @cogers ::wwo_filter_inline_script_attributes
* @covers ::plwwo_update_script_type
* @covers ::plwwo_filter_print_scripts_array
* @cogers ::plwwo_filter_inline_script_attributes
*
* @dataProvider data_update_script_types
*
Expand All @@ -217,15 +217,15 @@ public function test_update_script_types( Closure $set_up, string $expected ): v
/**
* Test head and footer scripts.
*
* @covers ::wwo_update_script_type
* @covers ::wwo_filter_print_scripts_array
* @covers ::plwwo_update_script_type
* @covers ::plwwo_filter_print_scripts_array
*/
public function test_head_and_footer_scripts(): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', false );
wp_script_add_data( 'foo', 'worker', true );

$this->assertEquals(
$this->replace_placeholders( '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script>' ),
$this->replace_placeholders( '{{ plwwo_config }}{{ plwwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script>' ),
trim( get_echo( 'wp_print_head_scripts' ) )
);

Expand All @@ -241,15 +241,15 @@ public function test_head_and_footer_scripts(): void {
/**
* Test only head script.
*
* @covers ::wwo_update_script_type
* @covers ::wwo_filter_print_scripts_array
* @covers ::plwwo_update_script_type
* @covers ::plwwo_filter_print_scripts_array
*/
public function test_only_head_script(): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', false );
wp_script_add_data( 'foo', 'worker', true );

$this->assertEquals(
$this->replace_placeholders( '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script>' ),
$this->replace_placeholders( '{{ plwwo_config }}{{ plwwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js"></script>' ),
trim( get_echo( 'wp_print_head_scripts' ) )
);

Expand All @@ -264,8 +264,8 @@ public function test_only_head_script(): void {
/**
* Test only footer script.
*
* @covers ::wwo_update_script_type
* @covers ::wwo_filter_print_scripts_array
* @covers ::plwwo_update_script_type
* @covers ::plwwo_filter_print_scripts_array
*/
public function test_only_footer_script(): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', false );
Expand All @@ -279,7 +279,7 @@ public function test_only_footer_script(): void {
wp_script_add_data( 'bar', 'worker', true );

$this->assertEquals(
$this->replace_placeholders( '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/bar.js?ver=1.0.0" id="bar-js"></script>' ),
$this->replace_placeholders( '{{ plwwo_config }}{{ plwwo_inline_script }}<script type="text/partytown" src="https://example.com/bar.js?ver=1.0.0" id="bar-js"></script>' ),
trim( get_echo( 'wp_print_footer_scripts' ) )
);
}
Expand All @@ -295,12 +295,12 @@ private function replace_placeholders( string $template ): string {
$wwo_inline_script_data = wp_scripts()->get_inline_script_data( 'web-worker-offloading', 'after' );

$template = str_replace(
'{{ wwo_config }}',
'{{ plwwo_config }}',
wp_get_inline_script_tag( $wwo_config_data, array( 'id' => 'web-worker-offloading-js-before' ) ),
$template
);
return str_replace(
'{{ wwo_inline_script }}',
'{{ plwwo_inline_script }}',
wp_get_inline_script_tag( $wwo_inline_script_data, array( 'id' => 'web-worker-offloading-js-after' ) ),
$template
);
Expand Down
6 changes: 3 additions & 3 deletions plugins/web-worker-offloading/third-party.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @param non-empty-string[] $script_handles Script handles.
*/
function wwo_mark_scripts_for_offloading( array $script_handles ): void {
function plwwo_mark_scripts_for_offloading( array $script_handles ): void {
add_filter(
'print_scripts_array',
static function ( $to_do ) use ( $script_handles ) {
Expand All @@ -37,7 +37,7 @@ static function ( $to_do ) use ( $script_handles ) {
* @since 0.1.0
* @access private
*/
function wwo_load_third_party_integrations(): void {
function plwwo_load_third_party_integrations(): void {
$plugins_with_integrations = array(
// TODO: google-site-kit.
// TODO: seo-by-rank-math.
Expand All @@ -53,4 +53,4 @@ function wwo_load_third_party_integrations(): void {
}
}
}
add_action( 'plugins_loaded', 'wwo_load_third_party_integrations' );
add_action( 'plugins_loaded', 'plwwo_load_third_party_integrations' );
Loading

0 comments on commit 0f37778

Please sign in to comment.