Skip to content

Commit

Permalink
Combine script and style addition
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 30, 2024
1 parent d9b3b66 commit 2b75add
Showing 1 changed file with 4 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,12 @@ final class Image_Prioritizer_Background_Image_Styled_Tag_Visitor extends Image_
const LAZY_BG_IMAGE_CLASS_NAME = 'od-lazy-bg-image';

/**
* Whether the lazy-loading stylesheet has been added to the head.
* Whether the lazy-loading script and stylesheet have been added.
*
* @since n.e.x.t
* @var bool
*/
private $added_lazy_stylesheet = false;

/**
* Whether the lazy-loading script was added to the body.
*
* @since n.e.x.t
* @var bool
*/
private $added_lazy_script = false;
private $added_lazy_assets = false;

/**
* Visits a tag.
Expand Down Expand Up @@ -128,14 +120,10 @@ private function lazy_load_bg_images( OD_Tag_Visitor_Context $context ): void {

$processor->add_class( self::LAZY_BG_IMAGE_CLASS_NAME );

if ( ! $this->added_lazy_stylesheet ) {
if ( ! $this->added_lazy_assets ) {
$processor->append_head_html( sprintf( "<style>\n%s\n</style>\n", image_prioritizer_get_lazy_load_bg_image_stylesheet() ) );
$this->added_lazy_stylesheet = true;
}

if ( ! $this->added_lazy_script ) {
$processor->append_body_html( wp_get_inline_script_tag( image_prioritizer_get_lazy_load_bg_image_script(), array( 'type' => 'module' ) ) );
$this->added_lazy_script = true;
$this->added_lazy_assets = true;
}
}
}

0 comments on commit 2b75add

Please sign in to comment.