diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 6cff8366e78..ef6909b64d6 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -1130,3 +1130,34 @@ function amp_add_admin_bar_view_link( $wp_admin_bar ) { $wp_admin_bar->add_menu( $parent ); } + +/** + * Prints AMP Stories auto ads. + * + * @since 1.2 + */ +function amp_print_story_auto_ads() { + /** + * Filters the configuration data for . + * + * This allows Dynamically inserting ads into a story. + * + * @param array $data Story ads configuration data. + * @param WP_Post $post The current story's post object. + */ + $data = apply_filters( 'amp_story_auto_ads_configuration', array(), get_post() ); + + if ( empty( $data ) ) { + return; + } + + $script_element = AMP_HTML_Utils::build_tag( + 'script', + array( + 'type' => 'application/json', + ), + wp_json_encode( $data ) + ); + + echo AMP_HTML_Utils::build_tag( 'amp-story-auto-ads', array(), $script_element ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped +} diff --git a/includes/templates/single-amp_story.php b/includes/templates/single-amp_story.php index 8c508d51288..c58cbdcbf2d 100644 --- a/includes/templates/single-amp_story.php +++ b/includes/templates/single-amp_story.php @@ -62,12 +62,17 @@ poster-landscape-src="" > - + diff --git a/tests/test-class-amp-story-post-type.php b/tests/test-class-amp-story-post-type.php index e759d98998c..27355c05e37 100644 --- a/tests/test-class-amp-story-post-type.php +++ b/tests/test-class-amp-story-post-type.php @@ -373,4 +373,39 @@ public function create_story_posts_with_featured_images( $featured_images ) { return $stories; } + + /** + * Test amp_print_story_auto_ads() + * + * @covers ::amp_print_story_auto_ads() + */ + public function test_amp_print_story_auto_ads_empty() { + $actual = get_echo( 'amp_print_story_auto_ads' ); + + $this->assertEmpty( $actual ); + } + + /** + * Test amp_print_story_auto_ads() + * + * @covers ::amp_print_story_auto_ads() + */ + public function test_amp_print_story_auto_ads() { + add_filter( + 'amp_story_auto_ads_configuration', + static function() { + return array( + 'ad-attributes' => array( + 'type' => 'doubleclick', + 'data-slot' => '/30497360/a4a/amp_story_dfp_example', + ), + ); + } + ); + + $actual = get_echo( 'amp_print_story_auto_ads' ); + + $this->assertStringStartsWith( 'assertContains( '