*/ private array $inside_picture = array(); /** * Marks an img this plugin has wrapped, so a second pass over the same HTML leaves it alone. * Some builders, Divi among them, run WordPress's image filter more than once. */ public const MARKER = 'data-pixelkeep-picture'; /** * Builds the rewriter. * * @param Settings $settings Plugin settings. * @param Derivatives $derivatives Derivative records. */ public function __construct( private readonly Settings $settings, private readonly Derivatives $derivatives ) {} /** * Adds the hooks. * * @return void */ public function register(): void { add_filter( 'the_content', array( $this, 'note_existing_pictures' ), 11 ); add_filter( 'widget_block_content', array( $this, 'note_existing_pictures' ), 11 ); add_filter( 'the_content', array( $this, 'forget_existing_pictures' ), 13 ); add_filter( 'widget_block_content', array( $this, 'forget_existing_pictures' ), 13 ); add_filter( 'wp_content_img_tag', array( $this, 'filter_content_image' ), 20, 3 ); add_filter( 'post_thumbnail_html', array( $this, 'filter_thumbnail' ), 20, 3 ); } /** * Remembers images a theme or editor already placed in a picture, which must not be nested. * * @param mixed $content Content, which other filters may have changed to anything. * @return mixed */ public function note_existing_pictures( $content ) { if ( is_string( $content ) && str_contains( $content, '#is', $content, $pictures ) ) { foreach ( $pictures[0] as $picture ) { $tags = new WP_HTML_Tag_Processor( $picture ); while ( $tags->next_tag( array( 'tag_name' => 'img' ) ) ) { $this->inside_picture[ (string) $tags->get_attribute( 'src' ) ] = true; } } } return $content; } /** * Clears the list, so the same image elsewhere on the page is still handled. * * @param mixed $content Content. * @return mixed */ public function forget_existing_pictures( $content ) { $this->inside_picture = array(); return $content; } /** * Filters an image in post content. * * @param string $image Image tag. * @param string $context Context. * @param int $attachment_id Attachment ID, or 0. * @return string */ public function filter_content_image( $image, $context, $attachment_id ) { return $this->rewrite( (string) $image, (int) $attachment_id ); } /** * Filters a featured image. * * @param string $html Image tag. * @param int $post_id Post ID. * @param int $thumbnail_id Attachment ID. * @return string */ public function filter_thumbnail( $html, $post_id, $thumbnail_id ) { return $this->rewrite( (string) $html, (int) $thumbnail_id ); } /** * Why delivery is standing down, if it is. * * @return string `performance_lab`, or empty. */ public function conflict(): string { // Performance Lab's Modern Image Formats also rewrites images, and doing both would double-wrap them. return defined( 'WEBP_UPLOADS_VERSION' ) ? 'performance_lab' : ''; } /** * Builds the picture markup. * * @param string $image Image tag. * @param int $attachment_id Attachment ID. * @return string */ public function rewrite( string $image, int $attachment_id ): string { if ( $attachment_id <= 0 || '' === $image || ! $this->is_allowed_here() ) { return $image; } $tag = new WP_HTML_Tag_Processor( $image ); if ( ! $tag->next_tag( array( 'tag_name' => 'img' ) ) ) { return $image; } $src = (string) $tag->get_attribute( 'src' ); if ( '' === $src || isset( $this->inside_picture[ $src ] ) || null !== $tag->get_attribute( self::MARKER ) ) { return $image; } $map = $this->derivatives->file_map( $attachment_id ); if ( array() === $map ) { return $image; } // A blank srcset, such as the placeholder Divi's gallery prints, means there is none. $srcset = trim( (string) $tag->get_attribute( 'srcset' ) ); $sizes = $tag->get_attribute( 'sizes' ); $sources = ''; foreach ( Derivatives::FORMATS as $format ) { $candidates = $this->candidates_for( $format, $srcset, $src, $map ); if ( '' === $candidates ) { continue; } $sources .= sprintf( '', esc_attr( $format ), esc_attr( $candidates ), is_string( $sizes ) && '' !== $sizes && '' !== $srcset ? ' sizes="' . esc_attr( $sizes ) . '"' : '' ); } if ( '' === $sources ) { return $image; } $tag->set_attribute( self::MARKER, true ); $picture = '' . $sources . $tag->get_updated_html() . ''; /** * Filters the picture markup. * * @param string $picture Picture element. * @param string $image Original image tag. * @param int $attachment_id Attachment ID. */ return (string) apply_filters( 'pixelkeep_picture_markup', $picture, $image, $attachment_id ); } /** * Builds a source's srcset, holding only sizes that have this derivative. * * @param string $format `webp` or `avif`. * @param string $srcset Image srcset. * @param string $src Image src. * @param array> $map File name to formats. * @return string */ private function candidates_for( string $format, string $srcset, string $src, array $map ): string { if ( '' === $srcset ) { return $this->derivative_url( $src, $format, $map ) ?? ''; } $candidates = array(); foreach ( explode( ',', $srcset ) as $candidate ) { $parts = preg_split( '/\s+/', trim( $candidate ), 2 ); if ( false === $parts || '' === $parts[0] ) { continue; } $url = $this->derivative_url( $parts[0], $format, $map ); if ( null !== $url ) { $candidates[] = trim( $url . ' ' . ( $parts[1] ?? '' ) ); } } return implode( ', ', $candidates ); } /** * Returns a file's derivative URL, keeping the host a CDN plugin may have rewritten. * * @param string $url File URL. * @param string $format `webp` or `avif`. * @param array> $map File name to formats. * @return string|null */ private function derivative_url( string $url, string $format, array $map ): ?string { $parts = explode( '?', $url, 2 ); $name = rawurldecode( wp_basename( $parts[0] ) ); if ( ! in_array( $format, $map[ $name ] ?? array(), true ) ) { return null; } return $parts[0] . '.' . $format . ( isset( $parts[1] ) ? '?' . $parts[1] : '' ); } /** * Whether a page builder's editor or the Customizer is showing this page. * * Editors run on the front end, and changed image markup can get in the way of editing. * * @return bool */ private function in_builder_editor(): bool { $params = array( 'et_fb', 'elementor-preview', 'fl_builder', 'bricks', 'ct_builder', 'oxygen', 'breakdance', 'breakdance_iframe', 'brizy-edit', 'brizy-edit-iframe', 'vcv-editable', 'vcv-action', 'tve', 'tcbf' ); foreach ( $params as $param ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only detects an editor view; nothing is changed. if ( isset( $_GET[ $param ] ) ) { return true; } } return is_customize_preview() || ( function_exists( 'et_core_is_fb_enabled' ) && et_core_is_fb_enabled() ); } /** * Whether this request may receive picture markup. * * @return bool */ public function is_allowed_here(): bool { $allowed = 'picture' === $this->settings->get( 'delivery' ) && '' === $this->conflict() && ! $this->in_builder_editor() && ! is_admin() && ! is_feed() && ! wp_doing_ajax() && ! wp_doing_cron() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) && ! ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) && ! ( defined( 'WP_CLI' ) && WP_CLI ) && ! ( function_exists( 'amp_is_request' ) && amp_is_request() ) && ! doing_filter( 'wp_mail' ) && ! doing_action( 'woocommerce_email_header' ) && ! doing_action( 'woocommerce_email_order_details' ); /** * Filters whether picture delivery runs on this request. * * @param bool $allowed Whether it runs. */ return (bool) apply_filters( 'pixelkeep_delivery_enabled', $allowed ); } }