Skip to content

Commit 833ba2b

Browse files
authored
Merge pull request #6233 from ampproject/fix/schemaorg-metadata-filtering-during-ob-callback
Call `amp_get_schemaorg_metadata()` before output buffer processing
2 parents 1c7728c + 15310db commit 833ba2b

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

includes/class-amp-theme-support.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use AmpProject\Optimizer;
2020
use AmpProject\RequestDestination;
2121
use AmpProject\Tag;
22+
use AmpProject\AmpWP\Optimizer\Transformer\AmpSchemaOrgMetadata;
23+
use AmpProject\AmpWP\Optimizer\Transformer\AmpSchemaOrgMetadataConfiguration;
2224

2325
/**
2426
* Class AMP_Theme_Support
@@ -124,6 +126,13 @@ class AMP_Theme_Support {
124126
*/
125127
protected static $is_output_buffering = false;
126128

129+
/**
130+
* Schema.org metadata
131+
*
132+
* @var array
133+
*/
134+
protected static $metadata;
135+
127136
/**
128137
* Initialize.
129138
*
@@ -1622,6 +1631,9 @@ public static function start_output_buffering() {
16221631
newrelic_disable_autorum();
16231632
}
16241633

1634+
// Obtain Schema.org metadata so that it will be available.
1635+
self::$metadata = (array) amp_get_schemaorg_metadata();
1636+
16251637
ob_start( [ __CLASS__, 'finish_output_buffering' ] );
16261638
self::$is_output_buffering = true;
16271639
}
@@ -2090,6 +2102,18 @@ static function () use ( $args ) {
20902102
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
20912103
);
20922104

2105+
// Supply the Schema.org metadata, previously obtained just before output buffering began, to the AmpSchemaOrgMetadataConfiguration.
2106+
add_filter(
2107+
'amp_optimizer_config',
2108+
function ( $config ) {
2109+
if ( is_array( self::$metadata ) ) {
2110+
$config[ AmpSchemaOrgMetadata::class ][ AmpSchemaOrgMetadataConfiguration::METADATA ] = self::$metadata;
2111+
}
2112+
return $config;
2113+
},
2114+
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
2115+
);
2116+
20932117
return Services::get( 'injector' )->make( OptimizerService::class );
20942118
}
20952119

src/Optimizer/Transformer/AmpSchemaOrgMetadataConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function getAllowedKeys() {
4646
}
4747

4848
return [
49-
self::METADATA => (array) amp_get_schemaorg_metadata(),
49+
self::METADATA => [],
5050
];
5151
}
5252

tests/php/test-class-amp-schema-org-metadata.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,21 @@ public function get_schema_script_data() {
5555
* @param int $expected Expected count of valid JSON+LD schema.
5656
*/
5757
public function test_transform( $json, $expected ) {
58-
$html = '<html><head><script type="application/ld+json">%s</script></head><body>Test</body></html>';
59-
$dom = Document::fromHtml( sprintf( $html, $json ), Options::DEFAULTS );
60-
$transformer = new AmpSchemaOrgMetadata( new AmpSchemaOrgMetadataConfiguration() );
61-
$errors = new ErrorCollection();
58+
$html = '<html><head><script type="application/ld+json">%s</script></head><body>Test</body></html>';
59+
$dom = Document::fromHtml( sprintf( $html, $json ), Options::DEFAULTS );
60+
$configuration = new AmpSchemaOrgMetadataConfiguration(
61+
[
62+
AmpSchemaOrgMetadataConfiguration::METADATA => [
63+
'@context' => 'http://schema.org',
64+
'publisher' => [
65+
'@type' => 'Organization',
66+
'name' => 'Acme',
67+
],
68+
],
69+
]
70+
);
71+
$transformer = new AmpSchemaOrgMetadata( $configuration );
72+
$errors = new ErrorCollection();
6273
$transformer->transform( $dom, $errors );
6374
$this->assertEquals( $expected, substr_count( $dom->saveHTML(), 'schema.org' ) );
6475
}
@@ -69,15 +80,11 @@ public function test_transform( $json, $expected ) {
6980
* @covers ::transform
7081
*/
7182
public function test_empty_metadata_configuration() {
72-
add_filter( 'amp_schemaorg_metadata', '__return_empty_array' );
73-
7483
$dom = new Document();
7584
$transformer = new AmpSchemaOrgMetadata( new AmpSchemaOrgMetadataConfiguration() );
7685
$transformer->transform( $dom, new ErrorCollection() );
7786

7887
$xpath_query = '//script[ @type = "application/ld+json" ]';
7988
$this->assertEquals( 0, $dom->xpath->query( $xpath_query )->length );
80-
81-
remove_filter( 'amp_schemaorg_metadata', '__return_empty_array' );
8289
}
8390
}

tests/php/test-class-amp-theme-support.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ public function test_prepare_response_throwing_exception() {
19821982
);
19831983

19841984
add_filter(
1985-
'amp_schemaorg_metadata',
1985+
'amp_enable_optimizer',
19861986
static function () {
19871987
throw new RuntimeException( 'FAILURE', 42 );
19881988
}

0 commit comments

Comments
 (0)