primaryImage(); if ( $primaryUrl === null || ! self::isHttpUrl( $primaryUrl ) ) { return new ImageSet( null, array() ); } $loading = $this->resolveLoading( $context ); $title = $snapshot->title(); $primary = new ImageView( $primaryUrl, $title, null, null, $loading ); $gallery = array(); $seen = array( $primaryUrl => true ); foreach ( $snapshot->galleryImages() as $url ) { if ( ! self::isHttpUrl( $url ) ) { continue; } if ( isset( $seen[ $url ] ) ) { continue; } $seen[ $url ] = true; $gallery[] = new ImageView( $url, $title, null, null, $loading ); } return new ImageSet( $primary, $gallery ); } /** * Resolve the §12.2 `loading` attribute per the surface-driven * selection documented at the file level. * * @param RenderContext $context Active render context. */ private function resolveLoading( RenderContext $context ): string { if ( $context->isAmp() ) { return ImageView::LOADING_EAGER; } if ( $context->isEditor() ) { return ImageView::LOADING_EAGER; } if ( $context->lazyLoadEnabled() ) { return ImageView::LOADING_LAZY; } return ImageView::LOADING_EAGER; } /** * Test whether a URL string starts with `http://` or * `https://`. * * @param string $url Raw URL string. */ private static function isHttpUrl( string $url ): bool { $trimmed = trim( $url ); if ( $trimmed === '' ) { return false; } return strpos( $trimmed, 'http://' ) === 0 || strpos( $trimmed, 'https://' ) === 0; } }