holder_atts . ' data-lazy-src="' . $image['src'] . '"', $image_lazyload );
if ( preg_match( $native_pattern, $image_lazyload ) ) {
$image_lazyload = preg_replace( $native_pattern, '', $image_lazyload );
}
}
/**
* Filter the LazyLoad HTML output
*
* @since 1.0
*
* @param string $html Output that will be printed
*/
$image_lazyload = apply_filters( 'rocket_lazyload_html', $image_lazyload );
return $image_lazyload;
}
/**
* Returns the HTML tag wrapped inside noscript tags
*
* @param string $element Element to wrap.
* @return string
*/
private function noscript( $element ) {
return '';
}
/**
* Applies lazyload on srcset and sizes attributes
*
* @param string $html HTML image tag.
* @return string
*/
public function lazyloadResponsiveAttributes( $html ) {
$html = preg_replace( '/[\s|"|\'](srcset)\s*=\s*("|\')([^"|\']+)\2/i', ' data-lazy-$1=$2$3$2', $html );
$html = preg_replace( '/[\s|"|\'](sizes)\s*=\s*("|\')([^"|\']+)\2/i', ' data-lazy-$1=$2$3$2', $html );
return $html;
}
/**
* Finds patterns matching smiley and call the callback method to replace them with the image
*
* @param string $text Content to search in.
* @return string
*/
public function convertSmilies( $text ) {
global $wp_smiliessearch;
if ( empty( $text ) || ! is_string( $text ) ) {
return $text;
}
if ( ! get_option( 'use_smilies' ) || empty( $wp_smiliessearch ) ) {
return $text;
}
$output = '';
// HTML loop taken from texturize function, could possible be consolidated.
$textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between.
$stop = count( $textarr );// loop stuff.
// Ignore proessing of specific tags.
$tags_to_ignore = 'code|pre|style|script|textarea';
$ignore_block_element = '';
for ( $i = 0; $i < $stop; $i++ ) {
$content = $textarr[ $i ];
// If we're in an ignore block, wait until we find its closing tag.
if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) {
$ignore_block_element = $matches[1];
}
// If it's not a tag and not in ignore block.
if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] ) {
$content = preg_replace_callback( $wp_smiliessearch, [ $this, 'translateSmiley' ], $content );
}
// did we exit ignore block.
if ( '' !== $ignore_block_element && '' . $ignore_block_element . '>' === $content ) {
$ignore_block_element = '';
}
$output .= $content;
}
return $output;
}
/**
* Replace matches by smiley image, lazyloaded
*
* @param array $matches Array of matches.
* @return string
*/
private function translateSmiley( $matches ) {
global $wpsmiliestrans;
if ( count( $matches ) === 0 ) {
return '';
}
$smiley = trim( reset( $matches ) );
$img = $wpsmiliestrans[ $smiley ];
$matches = [];
$ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
$image_exts = [ 'jpg', 'jpeg', 'jpe', 'gif', 'png' ];
// Don't convert smilies that aren't images - they're probably emoji.
if ( ! in_array( $ext, $image_exts, true ) ) {
return $img;
}
/**
* Filter the Smiley image URL before it's used in the image element.
*
* @since 2.9.0
*
* @param string $smiley_url URL for the smiley image.
* @param string $img Filename for the smiley image.
* @param string $site_url Site URL, as returned by site_url().
*/
$src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
// Don't LazyLoad if process is stopped for these reasons.
if ( is_feed() || is_preview() ) {
return sprintf( ' ', esc_url( $src_url ), esc_attr( $smiley ) );
}
return sprintf( ' ', $this->getPlaceholder(), esc_url( $src_url ), esc_attr( $smiley ) );
}
/**
* Returns the placeholder for the src attribute
*
* @since 1.2
*
* @param int $width Width of the placeholder image. Default 0.
* @param int $height Height of the placeholder image. Default 0.
* @return string
*/
public function getPlaceholder( $width = 0, $height = 0 ) {
$width = 0 === $width ? 0 : absint( $width );
$height = 0 === $height ? 0 : absint( $height );
$placeholder = str_replace( ' ', '%20', "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 $width $height'%3E%3C/svg%3E" );
/**
* Filter the image lazyLoad placeholder on src attribute
*
* @since 1.1
*
* @param string $placeholder Placeholder that will be printed.
* @param int $width Placeholder width.
* @param int $height Placeholder height.
*/
return apply_filters( 'rocket_lazyload_placeholder', $placeholder, $width, $height );
}
}