get_optimizer()->optimizeDom( $document, $errors ); if ( \count( $errors ) > 0 ) { /** * Error list. * * @var Error[] $errors_array Error list. */ $errors_array = iterator_to_array( $errors ); $error_messages = array_filter( array_map( static function ( Error $error ) { // Hidden because amp-story is a render-delaying extension. if ( 'CannotRemoveBoilerplate' === $error->getCode() ) { return ''; } return ' - ' . $error->getCode() . ': ' . $error->getMessage(); }, $errors_array ) ); if ( ! empty( $error_messages ) ) { $document->head->appendChild( $document->createComment( "\n" . __( 'AMP optimization could not be completed due to the following:', 'web-stories' ) . "\n" . implode( "\n", $error_messages ) . "\n" ) ); } } } /** * Optimizer instance to use. * * @since 1.1.0 * * @link https://github.com/ampproject/amp-wp/blob/8856284d90fc8558c30acc029becd352ae26e4e1/includes/class-amp-theme-support.php#L2235-L2255 * @see AMP_Theme_Support::get_optimizer * * @return TransformationEngine Optimizer transformation engine to use. */ private function get_optimizer(): TransformationEngine { $configuration = self::get_optimizer_configuration(); $fallback_remote_request_pipeline = new FallbackRemoteGetRequest( new WpHttpRemoteGetRequest(), new FilesystemRemoteGetRequest( LocalFallback::getMappings() ) ); $cached_remote_request = new CachedRemoteGetRequest( $fallback_remote_request_pipeline, WEEK_IN_SECONDS ); return new TransformationEngine( $configuration, $cached_remote_request ); } /** * Get the AmpProject\Optimizer configuration object to use. * * @since 1.1.0 * * @link https://github.com/ampproject/amp-wp/blob/5405daa38e65f0ec16ffc920014d0110b03ee773/src/Optimizer/AmpWPConfiguration.php#L43-L78 * @see AmpWPConfiguration::apply_filters() * * @return Configuration Optimizer configuration to use. */ private static function get_optimizer_configuration(): Configuration { $transformers = Configuration::DEFAULT_TRANSFORMERS; $transformers[] = AmpStoryCssOptimizer::class; /** * Filter whether the AMP Optimizer should use server-side rendering or not. * * @since 1.1.0 * * @param bool $enable_ssr Whether the AMP Optimizer should use server-side rendering or not. */ $enable_ssr = apply_filters( 'web_stories_enable_ssr', true ); // In debugging mode, we don't use server-side rendering, as it further obfuscates the HTML markup. if ( ! $enable_ssr ) { $transformers = array_diff( $transformers, [ AmpRuntimeCss::class, OptimizeHeroImages::class, OptimizeAmpBind::class, RewriteAmpUrls::class, ServerSideRendering::class, TransformedIdentifier::class, AmpStoryCssOptimizer::class, ] ); } $configuration = [ Configuration::KEY_TRANSFORMERS => $transformers, AmpStoryCssOptimizer::class => [ AmpStoryCssOptimizerConfiguration::OPTIMIZE_AMP_STORY => true, ], MinifyHtml::class => [ // Prevents issues with rounding floats, relevant for things like shopping (product prices). Configuration\MinifyHtmlConfiguration::MINIFY_JSON => false, ], ]; /** * Filter the configuration to be used for the AMP Optimizer. * * @since 1.1.0 * * @param array $configuration Associative array of configuration data. */ $configuration = apply_filters( 'web_stories_amp_optimizer_config', $configuration ); return new DefaultConfiguration( $configuration ); } }