Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call amp_get_schemaorg_metadata() before output buffer processing #6233

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 15 additions & 6 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,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' );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corresponding remove_filter() needs to be removed as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 15310db. It wasn't needed in the first place since filters get reset when tests are torn down.


$dom = new Document();
$transformer = new AmpSchemaOrgMetadata( new AmpSchemaOrgMetadataConfiguration() );
$transformer->transform( $dom, new ErrorCollection() );
Expand Down
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