Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Optimize Embeds #979

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions module-i18n.php
Expand Up @@ -9,8 +9,8 @@
_x( 'Creates WebP versions for new JPEG image uploads if supported by the server.', 'module description', 'performance-lab' ),
_x( 'Enqueued Assets Health Check', 'module name', 'performance-lab' ),
_x( 'Adds a CSS and JS resource check in Site Health status.', 'module description', 'performance-lab' ),
_x( 'oEmbed Optimizer', 'module name', 'performance-lab' ),
_x( 'Optimize oEmbed loading with improved lazy loading.', 'module description', 'performance-lab' ),
_x( 'Embed Optimizer', 'module name', 'performance-lab' ),
_x( 'Optimize the performance of embeds.', 'module description', 'performance-lab' ),
_x( 'Autoloaded Options Health Check', 'module name', 'performance-lab' ),
_x( 'Adds a check for autoloaded options in Site Health status.', 'module description', 'performance-lab' ),
);
Expand Down
@@ -1,6 +1,6 @@
<?php
/**
* Hook callbacks used for oEmbed Optimizer.
* Hook callbacks used for Optimize Embeds.
*
* @since n.e.x.t
* @package performance-lab
Expand All @@ -21,7 +21,7 @@
* @param string $html The oEmbed HTML.
* @return string
*/
function perflab_optimize_oembed_html( string $html ): string {
function embed_optimizer_filter_oembed_html( string $html ): string {
$p = new WP_HTML_Tag_Processor( $html );

/**
Expand Down Expand Up @@ -55,7 +55,7 @@ function perflab_optimize_oembed_html( string $html ): string {
}
// If there was only one non-inline script, make it lazy.
if ( 1 === $script_count && ! $has_inline_script ) {
add_action( 'wp_footer', 'perflab_optimize_oembed_lazy_load_scripts' );
add_action( 'wp_footer', 'embed_optimizer_lazy_load_scripts' );
$p->seek( 'script' );
$p->set_attribute( 'data-lazy-embed-src', $p->get_attribute( 'src' ) );
$p->remove_attribute( 'src' );
Expand All @@ -67,15 +67,15 @@ function perflab_optimize_oembed_html( string $html ): string {
}
return $p->get_updated_html();
}
add_filter( 'embed_oembed_html', 'perflab_optimize_oembed_html' );
add_filter( 'embed_oembed_html', 'embed_optimizer_filter_oembed_html' );

/**
* Add a script to the footer if there are lazy loaded embeds.
* Load the embed's scripts when they approach the viewport using an IntersectionObserver.
*
* @since n.e.x.t
*/
function perflab_optimize_oembed_lazy_load_scripts() {
function embed_optimizer_lazy_load_scripts() {
?>
<script type="module">
const lazyEmbedsScripts = document.querySelectorAll( 'script[data-lazy-embed-src]' );
Expand Down
12 changes: 12 additions & 0 deletions modules/js-and-css/embed-optimizer/load.php
@@ -0,0 +1,12 @@
<?php
/**
* Module Name: Embed Optimizer
* Description: Optimize the performance of embeds.
* Experimental: Yes
*
* @since n.e.x.t
* @package performance-lab
*/

// Load in the Embed Optimizer module hooks.
require_once __DIR__ . '/hooks.php';
12 changes: 0 additions & 12 deletions modules/js-and-css/optimize-oembeds/load.php

This file was deleted.

5 changes: 5 additions & 0 deletions phpcs.xml.dist
Expand Up @@ -54,6 +54,11 @@
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<!-- Do not apply script rules for unit tests -->
<rule ref="WordPress.WP.EnqueuedResources.NonEnqueuedScript">
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<!-- Do not apply filename rules for unit tests and object cache -->
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
<exclude-pattern>server-timing/object-cache.copy.php</exclude-pattern>
Expand Down