diff --git a/amp.php b/amp.php index 3b35a572800..1986bac1435 100644 --- a/amp.php +++ b/amp.php @@ -21,6 +21,9 @@ require_once( AMP__DIR__ . '/includes/settings/class-amp-customizer-settings.php' ); require_once( AMP__DIR__ . '/includes/settings/class-amp-customizer-design-settings.php' ); +require_once( AMP__DIR__ . '/includes/actions/class-amp-frontend-actions.php' ); +require_once( AMP__DIR__ . '/includes/actions/class-amp-paired-post-actions.php' ); + register_activation_hook( __FILE__, 'amp_activate' ); function amp_activate() { if ( ! did_action( 'amp_init' ) ) { @@ -111,13 +114,12 @@ function amp_load_classes() { } function amp_add_frontend_actions() { - require_once( AMP__DIR__ . '/includes/amp-frontend-actions.php' ); + AMP_Frontend_Actions::register_hooks(); } function amp_add_post_template_actions() { - require_once( AMP__DIR__ . '/includes/amp-post-template-actions.php' ); + AMP_Paired_Post_Actions::register_hooks(); require_once( AMP__DIR__ . '/includes/amp-post-template-functions.php' ); - amp_post_template_init_hooks(); } function amp_prepare_render() { diff --git a/includes/actions/class-amp-actions.php b/includes/actions/class-amp-actions.php new file mode 100644 index 00000000000..4294334c422 --- /dev/null +++ b/includes/actions/class-amp-actions.php @@ -0,0 +1,12 @@ +', esc_url( $amp_url ) ); + } +} diff --git a/includes/actions/class-amp-paired-post-actions.php b/includes/actions/class-amp-paired-post-actions.php new file mode 100644 index 00000000000..5611c09f3ae --- /dev/null +++ b/includes/actions/class-amp-paired-post-actions.php @@ -0,0 +1,106 @@ + + <?php echo esc_html( $amp_template->get( 'document_title' ) ); ?> + + + get( 'amp_component_scripts', array() ); + foreach ( $scripts as $element => $script ) : + $custom_type = ($element == 'amp-mustache') ? 'template' : 'element'; ?> + + + + get( 'font_urls', array() ); + foreach ( $font_urls as $slug => $url ) : ?> + + + + get( 'metadata' ); + if ( empty( $metadata ) ) { + return; + } + ?> + + get( 'post_amp_styles' ); + if ( ! empty( $styles ) ) { + echo '/* Inline styles */' . PHP_EOL; + foreach ( $styles as $selector => $declarations ) { + $declarations = implode( ';', $declarations ) . ';'; + printf( '%1$s{%2$s}', $selector, $declarations ); + } + } + } + + public static function add_analytics_scripts( $data ) { + if ( ! empty( $data['amp_analytics'] ) ) { + $data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js'; + } + return $data; + } + + public static function 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'] ) ) { + _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %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 ); + } + } +} diff --git a/includes/amp-frontend-actions.php b/includes/amp-frontend-actions.php deleted file mode 100644 index e0fcdb7c93e..00000000000 --- a/includes/amp-frontend-actions.php +++ /dev/null @@ -1,13 +0,0 @@ -', esc_url( $amp_url ) ); -} diff --git a/includes/amp-post-template-actions.php b/includes/amp-post-template-actions.php deleted file mode 100644 index 5d6ea98540a..00000000000 --- a/includes/amp-post-template-actions.php +++ /dev/null @@ -1,101 +0,0 @@ - - <?php echo esc_html( $amp_template->get( 'document_title' ) ); ?> - - - get( 'amp_component_scripts', array() ); - foreach ( $scripts as $element => $script ) : - $custom_type = ($element == 'amp-mustache') ? 'template' : 'element'; ?> - - - - get( 'font_urls', array() ); - foreach ( $font_urls as $slug => $url ) : ?> - - - - get( 'metadata' ); - if ( empty( $metadata ) ) { - return; - } - ?> - - get( 'post_amp_styles' ); - if ( ! empty( $styles ) ) { - echo '/* Inline styles */' . PHP_EOL; - foreach ( $styles as $selector => $declarations ) { - $declarations = implode( ';', $declarations ) . ';'; - printf( '%1$s{%2$s}', $selector, $declarations ); - } - } -} - -function amp_post_template_add_analytics_script( $data ) { - if ( ! empty( $data['amp_analytics'] ) ) { - $data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js'; - } - return $data; -} - -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'] ) ) { - _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %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 ); - } -}