Skip to content

Commit

Permalink
Call amp_get_schemaorg_metadata() before output buffer processing (#6233
Browse files Browse the repository at this point in the history
)
  • Loading branch information
westonruter committed May 14, 2021
1 parent 50221e7 commit 828fefb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
24 changes: 24 additions & 0 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,6 +126,13 @@ class AMP_Theme_Support {
*/
protected static $is_output_buffering = false;

/**
* Schema.org metadata
*
* @var array
*/
protected static $metadata;

/**
* Initialize.
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function getAllowedKeys() {
}

return [
self::METADATA => (array) amp_get_schemaorg_metadata(),
self::METADATA => [],
];
}

Expand Down
23 changes: 15 additions & 8 deletions tests/php/test-class-amp-schema-org-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<html><head><script type="application/ld+json">%s</script></head><body>Test</body></html>';
$dom = Document::fromHtml( sprintf( $html, $json ), Options::DEFAULTS );
$transformer = new AmpSchemaOrgMetadata( new AmpSchemaOrgMetadataConfiguration() );
$errors = new ErrorCollection();
$html = '<html><head><script type="application/ld+json">%s</script></head><body>Test</body></html>';
$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' ) );
}
Expand All @@ -69,15 +80,11 @@ 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() );

$xpath_query = '//script[ @type = "application/ld+json" ]';
$this->assertEquals( 0, $dom->xpath->query( $xpath_query )->length );

remove_filter( 'amp_schemaorg_metadata', '__return_empty_array' );
}
}
2 changes: 1 addition & 1 deletion tests/php/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down

0 comments on commit 828fefb

Please sign in to comment.