From 67e794ad109c81e610c792a27e4b9bc6c34f5851 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 3 Jun 2019 12:57:10 +0200 Subject: [PATCH 1/4] Print amp analytics in single story template --- includes/templates/single-amp_story.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/templates/single-amp_story.php b/includes/templates/single-amp_story.php index 8c508d51288..248bfff2c29 100644 --- a/includes/templates/single-amp_story.php +++ b/includes/templates/single-amp_story.php @@ -68,6 +68,8 @@ From 1aa924d35d0436763a4e41f4f8100470a3be3fc4 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 3 Jun 2019 13:10:57 +0200 Subject: [PATCH 2/4] Add new amp_print_story_auto_ads function --- includes/amp-helper-functions.php | 31 +++++++++++++++++++++ includes/templates/single-amp_story.php | 5 +++- tests/test-class-amp-story-post-type.php | 35 ++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 6cff8366e78..6c2b8e595d7 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_stories_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 248bfff2c29..c58cbdcbf2d 100644 --- a/includes/templates/single-amp_story.php +++ b/includes/templates/single-amp_story.php @@ -62,7 +62,10 @@ poster-landscape-src="" > - + 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_stories_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( '