~s', 'self::noscripts_remove', $content );
preg_match_all( '/]*>/', $content, $matches );
foreach ( $matches[0] as $img_html ) {
if ( ! ( preg_match( $skip_images_regex, $img_html ) ) ) {
$add_class = false;
if ( ! preg_match( '/src=[\'"]([^\'"]+)[\'"]/', $img_html ) && preg_match( '/srcset=[\'"]([^\'"]+)[\'"]/', $img_html ) ) {
$replace_html = preg_replace( '/';
array_push( $search, $img_html );
array_push( $replace, $replace_html );
}
}
}
$content = str_replace( $search, $replace, $content );
$content = preg_replace_callback( '~' . chr(20) . '([0-9]+)' . chr(20) . '~', 'self::noscripts_restore', $content );
return $content;
}
static function noscripts_remove( $match ) {
self::$noscript_id++;
self::$noscripts[self::$noscript_id] = $match[0];
return chr(20) . self::$noscript_id . chr(20);
}
static function noscripts_restore( $match ) {
return self::$noscripts[(int) $match[1]];
}
static function add_class( $html_string = '', $new_class ) {
$pattern = '/class=[\'"]([^\'"]*)[\'"]/';
if ( preg_match( $pattern, $html_string, $matches ) ) {
$defined_classes = explode( ' ', $matches[1] );
if ( ! in_array( $new_class, $defined_classes ) ) {
$defined_classes[] = $new_class;
$html_string = str_replace(
$matches[0],
sprintf( 'class="%s"', implode( ' ', $defined_classes ) ),
$html_string
);
}
} else {
$html_string = preg_replace( '/(\<.+\s)/', sprintf( '$1class="%s" ', $new_class ), $html_string );
}
return $html_string;
}
static function is_enabled() {
return self::$enabled;
}
static function kses_allowed_html( $allowedtags, $context ) {
$allowedtags['noscript'] = array();
$allowedtags['img']['data-src'] = true;
$allowedtags['img']['data-srcset'] = true;
$allowedtags['img']['data-sizes'] = true;
return $allowedtags;
}
static function kses_allowed_protocols( $allowedprotocols ) {
$allowedprotocols[] = 'data';
return $allowedprotocols;
}
}
add_action( 'init', array( 'LSX_LazyLoadImages', 'init' ) );