From 02ace15e42a78eec1a256cd65a2765a4c0c72bbe Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 13 May 2021 23:15:05 -0700 Subject: [PATCH 1/3] Call amp_get_schemaorg_metadata() before output buffer processing --- includes/class-amp-theme-support.php | 24 +++++++++++++++++++ .../AmpSchemaOrgMetadataConfiguration.php | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 069511c8074..7f7c279ce30 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -19,6 +19,8 @@ use AmpProject\Optimizer; use AmpProject\RequestDestination; use AmpProject\Tag; +use AmpProject\AmpWP\Optimizer\Transformer\AmpSchemaOrgMetadata; +use AmpProject\AmpWP\Optimizer\Transformer\AmpSchemaOrgMetadataConfiguration; /** * Class AMP_Theme_Support @@ -124,6 +126,13 @@ class AMP_Theme_Support { */ protected static $is_output_buffering = false; + /** + * Schema.org metadata + * + * @var array + */ + protected static $metadata; + /** * Initialize. * @@ -1622,6 +1631,9 @@ public static function start_output_buffering() { newrelic_disable_autorum(); } + // Obtain Schema.org metadata so that it will be available. + self::$metadata = (array) amp_get_schemaorg_metadata(); + ob_start( [ __CLASS__, 'finish_output_buffering' ] ); self::$is_output_buffering = true; } @@ -2090,6 +2102,18 @@ static function () use ( $args ) { defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound ); + // Supply the Schema.org metadata, previously obtained just before output buffering began, to the AmpSchemaOrgMetadataConfiguration. + add_filter( + 'amp_optimizer_config', + function ( $config ) { + if ( is_array( self::$metadata ) ) { + $config[ AmpSchemaOrgMetadata::class ][ AmpSchemaOrgMetadataConfiguration::METADATA ] = self::$metadata; + } + return $config; + }, + defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound + ); + return Services::get( 'injector' )->make( OptimizerService::class ); } diff --git a/src/Optimizer/Transformer/AmpSchemaOrgMetadataConfiguration.php b/src/Optimizer/Transformer/AmpSchemaOrgMetadataConfiguration.php index 385a2b0eaad..1b14c33ba51 100644 --- a/src/Optimizer/Transformer/AmpSchemaOrgMetadataConfiguration.php +++ b/src/Optimizer/Transformer/AmpSchemaOrgMetadataConfiguration.php @@ -46,7 +46,7 @@ protected function getAllowedKeys() { } return [ - self::METADATA => (array) amp_get_schemaorg_metadata(), + self::METADATA => [], ]; } From ccb5edaa6b27e124657a31df12dbcc8dc04c4b2d Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 13 May 2021 23:46:35 -0700 Subject: [PATCH 2/3] Fix tests after on changes to Schema.org metadata inclusion --- .../test-class-amp-schema-org-metadata.php | 21 +++++++++++++------ tests/php/test-class-amp-theme-support.php | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/php/test-class-amp-schema-org-metadata.php b/tests/php/test-class-amp-schema-org-metadata.php index 1fed5a56208..0b739373ad1 100644 --- a/tests/php/test-class-amp-schema-org-metadata.php +++ b/tests/php/test-class-amp-schema-org-metadata.php @@ -55,10 +55,21 @@ public function get_schema_script_data() { * @param int $expected Expected count of valid JSON+LD schema. */ public function test_transform( $json, $expected ) { - $html = 'Test'; - $dom = Document::fromHtml( sprintf( $html, $json ), Options::DEFAULTS ); - $transformer = new AmpSchemaOrgMetadata( new AmpSchemaOrgMetadataConfiguration() ); - $errors = new ErrorCollection(); + $html = 'Test'; + $dom = Document::fromHtml( sprintf( $html, $json ), Options::DEFAULTS ); + $configuration = new AmpSchemaOrgMetadataConfiguration( + [ + AmpSchemaOrgMetadataConfiguration::METADATA => [ + '@context' => 'http://schema.org', + 'publisher' => [ + '@type' => 'Organization', + 'name' => 'Acme', + ], + ], + ] + ); + $transformer = new AmpSchemaOrgMetadata( $configuration ); + $errors = new ErrorCollection(); $transformer->transform( $dom, $errors ); $this->assertEquals( $expected, substr_count( $dom->saveHTML(), 'schema.org' ) ); } @@ -69,8 +80,6 @@ public function test_transform( $json, $expected ) { * @covers ::transform */ public function test_empty_metadata_configuration() { - add_filter( 'amp_schemaorg_metadata', '__return_empty_array' ); - $dom = new Document(); $transformer = new AmpSchemaOrgMetadata( new AmpSchemaOrgMetadataConfiguration() ); $transformer->transform( $dom, new ErrorCollection() ); diff --git a/tests/php/test-class-amp-theme-support.php b/tests/php/test-class-amp-theme-support.php index 8bc88d5b336..29eb499b9fb 100644 --- a/tests/php/test-class-amp-theme-support.php +++ b/tests/php/test-class-amp-theme-support.php @@ -1982,7 +1982,7 @@ public function test_prepare_response_throwing_exception() { ); add_filter( - 'amp_schemaorg_metadata', + 'amp_enable_optimizer', static function () { throw new RuntimeException( 'FAILURE', 42 ); } From 15310db2a0fea460209c505e085338c4ab818d71 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 14 May 2021 00:47:20 -0700 Subject: [PATCH 3/3] Remove extraneous remove_filter() call --- tests/php/test-class-amp-schema-org-metadata.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/php/test-class-amp-schema-org-metadata.php b/tests/php/test-class-amp-schema-org-metadata.php index 0b739373ad1..12f3310d728 100644 --- a/tests/php/test-class-amp-schema-org-metadata.php +++ b/tests/php/test-class-amp-schema-org-metadata.php @@ -86,7 +86,5 @@ public function test_empty_metadata_configuration() { $xpath_query = '//script[ @type = "application/ld+json" ]'; $this->assertEquals( 0, $dom->xpath->query( $xpath_query )->length ); - - remove_filter( 'amp_schemaorg_metadata', '__return_empty_array' ); } }