2097152 || ! self::html_response() || ! class_exists( '\WP_HTML_Tag_Processor' ) ) { return $html; } $p = new \WP_HTML_Tag_Processor( $html ); if ( ! method_exists( $p, 'set_attribute' ) || ! method_exists( $p, 'is_tag_closer' ) ) { return $html; } $threshold = max( 0, (int) wp_omit_loading_attr_threshold() ); $seen = 0; $changed = 0; $n = 0; $skip = array( 'HEADER' => 0, 'NOSCRIPT' => 0, 'TEMPLATE' => 0, ); while ( $n++ < 20000 && $p->next_tag( array( 'tag_closers' => 'visit' ) ) ) { $tag = $p->get_tag(); if ( isset( $skip[ $tag ] ) ) { $skip[ $tag ] = $p->is_tag_closer() ? max( 0, $skip[ $tag ] - 1 ) : $skip[ $tag ] + 1; continue; } if ( 'IMG' !== $tag || $p->is_tag_closer() || $skip['NOSCRIPT'] || $skip['TEMPLATE'] ) { continue; } $src = $p->get_attribute( 'src' ); if ( ! is_string( $src ) || '' === trim( $src ) || preg_match( '/^\s*(?:data|javascript|blob):/i', $src ) ) { continue; } // Every real picture counts toward the first ones on screen, in page order, whoever printed it. ++$seen; if ( $skip['HEADER'] || $seen <= $threshold || null !== $p->get_attribute( 'loading' ) || 'high' === strtolower( (string) $p->get_attribute( 'fetchpriority' ) ) ) { continue; } if ( isset( self::$core[ html_entity_decode( $src, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ] ) ) { continue; } $width = $p->get_attribute( 'width' ); $height = $p->get_attribute( 'height' ); if ( ! is_string( $width ) || ! is_string( $height ) || ! ctype_digit( trim( $width ) ) || ! ctype_digit( trim( $height ) ) ) { continue; } $scripted = false; foreach ( self::SCRIPTED as $attr ) { if ( null !== $p->get_attribute( $attr ) ) { $scripted = true; break; } } if ( $scripted || $p->has_class( 'skip-lazy' ) ) { continue; } $p->set_attribute( 'loading', 'lazy' ); ++$changed; } return $changed ? $p->get_updated_html() : $html; } }