settings = $settings; } /** * Process HTML buffer to optimize images. * * @param string $html Output HTML string. * @return string */ public function process_html( $html ) { if ( empty( $html ) || false === stripos( $html, 'image_counter = 0; $do_lazy = (bool) $this->settings->get( 'lazy_load_images', true ); $exclude_count = (int) $this->settings->get( 'lazy_load_exclude_count', 2 ); $do_dimensions = (bool) $this->settings->get( 'add_missing_dimensions', true ); if ( ! $do_lazy && ! $do_dimensions ) { return $html; } $pattern = '/]+)>/is'; return preg_replace_callback( $pattern, function( $matches ) use ( $do_lazy, $exclude_count, $do_dimensions ) { $this->image_counter++; $tag_attributes = $matches[1]; $skip_lazy = false; if ( stripos( $tag_attributes, 'data-no-lazy' ) !== false || stripos( $tag_attributes, 'skip-lazy' ) !== false || stripos( $tag_attributes, 'custom-logo' ) !== false || stripos( $tag_attributes, 'loading="eager"' ) !== false || stripos( $tag_attributes, "loading='eager'" ) !== false || $this->image_counter <= $exclude_count ) { $skip_lazy = true; } // 1. Add missing dimensions if enabled if ( $do_dimensions ) { $tag_attributes = $this->inject_missing_dimensions( $tag_attributes ); } // 2. Add lazy loading if enabled and not excluded if ( $do_lazy && ! $skip_lazy ) { if ( stripos( $tag_attributes, 'loading=' ) === false ) { $tag_attributes .= ' loading="lazy"'; } if ( stripos( $tag_attributes, 'decoding=' ) === false ) { $tag_attributes .= ' decoding="async"'; } } return ''; }, $html ); } /** * Inject width and height attributes into img tag if missing. * * @param string $attributes Attributes string of tag. * @return string */ private function inject_missing_dimensions( $attributes ) { $has_width = (bool) preg_match( '/\bwidth\s*=\s*["\']?\d+/i', $attributes ); $has_height = (bool) preg_match( '/\bheight\s*=\s*["\']?\d+/i', $attributes ); if ( $has_width && $has_height ) { return $attributes; } if ( ! preg_match( '/\bsrc\s*=\s*["\']([^"\']+)["\']/i', $attributes, $src_match ) ) { return $attributes; } $src = $src_match[1]; if ( empty( $src ) || false !== strpos( $src, 'data:image' ) ) { return $attributes; } $dimensions = $this->get_image_dimensions( $src ); if ( $dimensions && ! empty( $dimensions['width'] ) && ! empty( $dimensions['height'] ) ) { if ( ! $has_width ) { $attributes .= ' width="' . absint( $dimensions['width'] ) . '"'; } if ( ! $has_height ) { $attributes .= ' height="' . absint( $dimensions['height'] ) . '"'; } } return $attributes; } /** * Resolve image dimensions via WordPress attachment or file path. * * @param string $src Image URL. * @return array|null Array with width and height, or null. */ private function get_image_dimensions( $src ) { $attachment_id = attachment_url_to_postid( $src ); if ( $attachment_id > 0 ) { $meta = wp_get_attachment_metadata( $attachment_id ); if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { return array( 'width' => $meta['width'], 'height' => $meta['height'], ); } } $uploads = wp_get_upload_dir(); if ( 0 === strpos( $src, $uploads['baseurl'] ) ) { $local_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $src ); if ( file_exists( $local_path ) ) { $size = @getimagesize( $local_path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors if ( ! empty( $size[0] ) && ! empty( $size[1] ) ) { return array( 'width' => $size[0], 'height' => $size[1], ); } } } return null; } }