diff --git a/amp.php b/amp.php index b837d2a6efa..1b63a1e7310 100644 --- a/amp.php +++ b/amp.php @@ -91,7 +91,6 @@ function amp_after_setup_theme() { add_action( 'widgets_init', 'AMP_Theme_Support::register_widgets' ); // @todo Let this be called by AMP_Theme_Support::init(). add_action( 'init', 'AMP_Theme_Support::setup_commenting' ); // @todo Let this be called by AMP_Theme_Support::init(). add_action( 'admin_init', 'AMP_Options_Manager::register_settings' ); - add_filter( 'amp_post_template_analytics', 'amp_add_custom_analytics' ); add_action( 'wp_loaded', 'amp_post_meta_box' ); add_action( 'wp_loaded', 'amp_add_options_menu' ); add_action( 'parse_query', 'amp_correct_query_when_is_front_page' ); diff --git a/includes/admin/functions.php b/includes/admin/functions.php index 6a1d4536e76..f5539a05a04 100644 --- a/includes/admin/functions.php +++ b/includes/admin/functions.php @@ -115,23 +115,29 @@ function amp_add_options_menu() { /** * Add custom analytics. * + * This is currently only used for legacy AMP post templates. + * + * @since 0.5 + * @see amp_get_analytics() + * * @param array $analytics Analytics. * @return array Analytics. */ -function amp_add_custom_analytics( $analytics ) { - $analytics_entries = AMP_Options_Manager::get_option( 'analytics', array() ); +function amp_add_custom_analytics( $analytics = array() ) { + $analytics = amp_get_analytics( $analytics ); - if ( ! $analytics_entries ) { - return $analytics; - } - - foreach ( $analytics_entries as $entry_id => $entry ) { - $analytics[ $entry_id ] = array( - 'type' => $entry['type'], - 'attributes' => array(), - 'config_data' => json_decode( $entry['config'] ), - ); - } + /** + * Add amp-analytics tags. + * + * This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting. + * This filter should be used to alter entries for legacy AMP templates. + * + * @since 0.4 + * + * @param array $analytics An associative array of the analytics entries we want to output. Each array entry must have a unique key, and the value should be an array with the following keys: `type`, `attributes`, `script_data`. See readme for more details. + * @param WP_Post $post The current post. + */ + $analytics = apply_filters( 'amp_post_template_analytics', $analytics, get_queried_object() ); return $analytics; } diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 930f8133647..f3a4d9896a9 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -149,6 +149,78 @@ function amp_print_boilerplate_code() { echo ''; } +/** + * Retrieve analytics data added in backend. + * + * @since 0.7 + * + * @param array $analytics Analytics entries. + * @return array Analytics. + */ +function amp_get_analytics( $analytics = array() ) { + $analytics_entries = AMP_Options_Manager::get_option( 'analytics', array() ); + + /** + * Add amp-analytics tags. + * + * This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting. + * This filter should be used to alter entries for paired mode. + * + * @since 0.7 + * + * @param array $analytics_entries An associative array of the analytics entries we want to output. Each array entry must have a unique key, and the value should be an array with the following keys: `type`, `attributes`, `script_data`. See readme for more details. + */ + $analytics_entries = apply_filters( 'amp_analytics_entries', $analytics_entries ); + + if ( ! $analytics_entries ) { + return $analytics; + } + + foreach ( $analytics_entries as $entry_id => $entry ) { + $analytics[ $entry_id ] = array( + 'type' => $entry['type'], + 'attributes' => array(), + 'config_data' => json_decode( $entry['config'] ), + ); + } + + return $analytics; +} + +/** + * Print analytics data. + * + * @since 0.7 + * + * @param array $analytics Analytics entries. + */ +function amp_print_analytics( $analytics ) { + $analytics_entries = amp_get_analytics( $analytics ); + + if ( empty( $analytics_entries ) ) { + return; + } + + // Can enter multiple configs within backend. + foreach ( $analytics_entries as $id => $analytics_entry ) { + if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) { + /* translators: %1$s is analytics entry ID, %2$s is actual entry keys. */ + _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %1$s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %2$s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' ); + continue; + } + $script_element = AMP_HTML_Utils::build_tag( 'script', array( + 'type' => 'application/json', + ), wp_json_encode( $analytics_entry['config_data'] ) ); + + $amp_analytics_attr = array_merge( array( + 'id' => $id, + 'type' => $analytics_entry['type'], + ), $analytics_entry['attributes'] ); + + echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); // WPCS: XSS OK. + } +} + /** * Get content embed handlers. * diff --git a/includes/amp-post-template-actions.php b/includes/amp-post-template-actions.php index 7cd1ec2949c..9206b30f00b 100644 --- a/includes/amp-post-template-actions.php +++ b/includes/amp-post-template-actions.php @@ -128,31 +128,11 @@ function amp_post_template_add_analytics_script( $data ) { /** * Print analytics data. * - * @param AMP_Post_Template $amp_template Template. + * @since 0.3.2 */ -function amp_post_template_add_analytics_data( $amp_template ) { - $analytics_entries = $amp_template->get( 'amp_analytics' ); - if ( empty( $analytics_entries ) ) { - return; - } - - foreach ( $analytics_entries as $id => $analytics_entry ) { - if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) { - /* translators: %1$s is analytics entry ID, %2$s is actual entry keys. */ - _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %1$s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %2$s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' ); - continue; - } - $script_element = AMP_HTML_Utils::build_tag( 'script', array( - 'type' => 'application/json', - ), wp_json_encode( $analytics_entry['config_data'] ) ); - - $amp_analytics_attr = array_merge( array( - 'id' => $id, - 'type' => $analytics_entry['type'], - ), $analytics_entry['attributes'] ); - - echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); // WPCS: XSS OK. - } +function amp_post_template_add_analytics_data() { + $analytics = amp_add_custom_analytics(); + amp_print_analytics( $analytics ); } /** diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 92c3646f3d7..ea4374de0fd 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -178,6 +178,8 @@ public static function register_hooks() { add_action( 'wp_head', 'amp_add_generator_metadata', 20 ); add_action( 'wp_head', 'amp_print_schemaorg_metadata' ); + add_action( 'wp_footer', 'amp_print_analytics' ); + /* * Disable admin bar because admin-bar.css (28K) and Dashicons (48K) alone * combine to surpass the 50K limit imposed for the amp-custom style. diff --git a/includes/templates/class-amp-post-template.php b/includes/templates/class-amp-post-template.php index c8060e29e2d..a917e7aaf10 100644 --- a/includes/templates/class-amp-post-template.php +++ b/includes/templates/class-amp-post-template.php @@ -131,17 +131,7 @@ public function __construct( $post ) { 'post_amp_styles' => array(), - /** - * Add amp-analytics tags. - * - * This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting. - * - * @since 0.4 - * - * @param array $analytics An associative array of the analytics entries we want to output. Each array entry must have a unique key, and the value should be an array with the following keys: `type`, `attributes`, `script_data`. See readme for more details. - * @param WP_Post $post The current post. - */ - 'amp_analytics' => apply_filters( 'amp_post_template_analytics', array(), $this->post ), + 'amp_analytics' => amp_add_custom_analytics(), ); $this->build_post_content(); diff --git a/tests/test-amp-analytics-options.php b/tests/test-amp-analytics-options.php index 38493086b3b..274934ffc45 100644 --- a/tests/test-amp-analytics-options.php +++ b/tests/test-amp-analytics-options.php @@ -212,4 +212,52 @@ function test_two_analytics_components_added() { libxml_use_internal_errors( $libxml_previous_state ); } + + /** + * Test amp_get_analytics() + * + * @covers amp_get_analytics() + */ + public function test_amp_get_analytics() { + $this->insert_one_option( + $this->vendor, + $this->config_one + ); + + $analytics = amp_get_analytics(); + $this->assertEquals( 1, count( $analytics ) ); + + $key = key( $analytics ); + $this->assertArrayHasKey( 'type', $analytics[ $key ] ); + $this->assertEquals( 'googleanalytics', $analytics[ $key ]['type'] ); + + add_theme_support( 'amp' ); + add_filter( 'amp_analytics_entries', function( $analytics ) use ( $key ) { + $analytics[ $key ]['type'] = 'test'; + return $analytics; + } ); + $analytics = amp_get_analytics(); + $this->assertEquals( 'test', $analytics[ $key ]['type'] ); + } + + /** + * Test amp_print_analytics() + * + * @covers amp_print_analytics() + */ + public function test_amp_print_analytics() { + $this->insert_one_option( + $this->vendor, + $this->config_one + ); + + $analytics = amp_get_analytics(); + + ob_start(); + amp_print_analytics( $analytics ); + $output = ob_get_clean(); + + $this->assertEquals( 0, strpos( $output, '