From 5932d558534ab9791eef96c1b785ae1856f43021 Mon Sep 17 00:00:00 2001 From: Alberto Medina Date: Wed, 5 Jul 2017 13:58:12 -0700 Subject: [PATCH 1/5] Abstract notion of Actions --- amp.php | 7 +- includes/actions/class-amp-actions.php | 12 ++ .../class-amp-canonical-mode-actions.php | 5 + .../class-amp-canonical-post-actions.php | 5 + .../actions/class-amp-frontend-actions.php | 19 ++++ .../actions/class-amp-paired-mode-actions.php | 5 + .../actions/class-amp-paired-post-actions.php | 106 ++++++++++++++++++ includes/amp-frontend-actions.php | 13 --- includes/amp-post-template-actions.php | 99 ---------------- 9 files changed, 157 insertions(+), 114 deletions(-) create mode 100644 includes/actions/class-amp-actions.php create mode 100644 includes/actions/class-amp-canonical-mode-actions.php create mode 100644 includes/actions/class-amp-canonical-post-actions.php create mode 100644 includes/actions/class-amp-frontend-actions.php create mode 100644 includes/actions/class-amp-paired-mode-actions.php create mode 100644 includes/actions/class-amp-paired-post-actions.php delete mode 100644 includes/amp-frontend-actions.php delete mode 100644 includes/amp-post-template-actions.php diff --git a/amp.php b/amp.php index 2a912cd3e67..b07ab0513de 100755 --- 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,11 +114,11 @@ function amp_load_classes() { } function amp_add_frontend_actions() { - require_once( AMP__DIR__ . '/includes/amp-frontend-actions.php' ); + AMP_Frontend_Actions::register_frontend_actions_filter(); } function amp_add_post_template_actions() { - require_once( AMP__DIR__ . '/includes/amp-post-template-actions.php' ); + AMP_Paired_Post_Actions::register(); require_once( AMP__DIR__ . '/includes/amp-post-template-functions.php' ); } diff --git a/includes/actions/class-amp-actions.php b/includes/actions/class-amp-actions.php new file mode 100644 index 00000000000..b7529078442 --- /dev/null +++ b/includes/actions/class-amp-actions.php @@ -0,0 +1,12 @@ +', esc_url( $amp_url ) ); + } +} \ No newline at end of file diff --git a/includes/actions/class-amp-paired-mode-actions.php b/includes/actions/class-amp-paired-mode-actions.php new file mode 100644 index 00000000000..68f2d047ff3 --- /dev/null +++ b/includes/actions/class-amp-paired-mode-actions.php @@ -0,0 +1,5 @@ + + <?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 ); + } + } +} \ No newline at end of file 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 15bac9fbe52..00000000000 --- a/includes/amp-post-template-actions.php +++ /dev/null @@ -1,99 +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 ); - } - } -} - -add_action( 'amp_post_template_data', 'amp_post_template_add_analytics_script' ); -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; -} - -add_action( 'amp_post_template_footer', 'amp_post_template_add_analytics_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 ); - } -} From 2a0bacb5fc45b3be4f18608d201d642ac1fc81ab Mon Sep 17 00:00:00 2001 From: Alberto Medina Date: Mon, 17 Jul 2017 11:38:57 -0700 Subject: [PATCH 2/5] Make AMP_Frontend_Actions class standalone --- amp.php | 2 +- includes/actions/class-amp-frontend-actions.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/amp.php b/amp.php index b07ab0513de..40393fda47c 100755 --- a/amp.php +++ b/amp.php @@ -114,7 +114,7 @@ function amp_load_classes() { } function amp_add_frontend_actions() { - AMP_Frontend_Actions::register_frontend_actions_filter(); + AMP_Frontend_Actions::register_hooks(); } function amp_add_post_template_actions() { diff --git a/includes/actions/class-amp-frontend-actions.php b/includes/actions/class-amp-frontend-actions.php index 30f780daee7..f8772b13481 100644 --- a/includes/actions/class-amp-frontend-actions.php +++ b/includes/actions/class-amp-frontend-actions.php @@ -3,14 +3,14 @@ require_once ( AMP__DIR__ . '/includes/actions/class-amp-actions.php' ); -class AMP_Frontend_Actions extends AMP_Actions { +class AMP_Frontend_Actions { - public static function register_frontend_actions_filter() { - add_action( 'wp_head', 'AMP_Frontend_Actions::amp_frontend_add_canonical' ); + public static function register_hooks() { + add_action( 'wp_head', 'AMP_Frontend_Actions::add_canonical' ); } - public static function amp_frontend_add_canonical() { - if ( false === apply_filters( 'amp_frontend_show_canonical', true ) ) { + public static function add_canonical() { + if ( false === apply_filters( 'add_canonical_link', true ) ) { return; } $amp_url = amp_get_permalink( get_queried_object_id() ); From 70373a566ead1c5cca03612164359fcc148f5bba Mon Sep 17 00:00:00 2001 From: Alberto Medina Date: Thu, 10 Aug 2017 16:17:22 -0700 Subject: [PATCH 3/5] Add EOF newlines; rename register_hooks function --- amp.php | 2 +- includes/actions/class-amp-actions.php | 2 +- .../class-amp-canonical-mode-actions.php | 2 +- .../class-amp-canonical-post-actions.php | 2 +- .../actions/class-amp-frontend-actions.php | 2 +- .../actions/class-amp-paired-mode-actions.php | 2 +- .../actions/class-amp-paired-post-actions.php | 18 +++++++++--------- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/amp.php b/amp.php index 7cdb168264a..4218c68dac2 100755 --- a/amp.php +++ b/amp.php @@ -119,7 +119,7 @@ function amp_add_frontend_actions() { } function amp_add_post_template_actions() { - AMP_Paired_Post_Actions::register(); + AMP_Paired_Post_Actions::register_hooks(); require_once( AMP__DIR__ . '/includes/amp-post-template-functions.php' ); amp_post_template_init_hooks(); } diff --git a/includes/actions/class-amp-actions.php b/includes/actions/class-amp-actions.php index b7529078442..4294334c422 100644 --- a/includes/actions/class-amp-actions.php +++ b/includes/actions/class-amp-actions.php @@ -9,4 +9,4 @@ public static function add_schemaorg_metadata( $template ) {} public static function add_analytics_scripts( $template ) {} public static function add_analytics_data( $template ) {} public static function add_canonical_link( $template ) {} -} \ No newline at end of file +} diff --git a/includes/actions/class-amp-canonical-mode-actions.php b/includes/actions/class-amp-canonical-mode-actions.php index b56ddae31f1..c93ff2df296 100644 --- a/includes/actions/class-amp-canonical-mode-actions.php +++ b/includes/actions/class-amp-canonical-mode-actions.php @@ -2,4 +2,4 @@ class AMP_Canonical_Mode_Actions extends AMP_Actions { -} \ No newline at end of file +} diff --git a/includes/actions/class-amp-canonical-post-actions.php b/includes/actions/class-amp-canonical-post-actions.php index ebb0f02ea6d..261b52c7e37 100644 --- a/includes/actions/class-amp-canonical-post-actions.php +++ b/includes/actions/class-amp-canonical-post-actions.php @@ -2,4 +2,4 @@ class AMP_Canonical_Post_Actions extends AMP_Canonical_Mode_Actions { -} \ No newline at end of file +} diff --git a/includes/actions/class-amp-frontend-actions.php b/includes/actions/class-amp-frontend-actions.php index f8772b13481..9fb2690210c 100644 --- a/includes/actions/class-amp-frontend-actions.php +++ b/includes/actions/class-amp-frontend-actions.php @@ -16,4 +16,4 @@ public static function add_canonical() { $amp_url = amp_get_permalink( get_queried_object_id() ); printf( '', esc_url( $amp_url ) ); } -} \ No newline at end of file +} diff --git a/includes/actions/class-amp-paired-mode-actions.php b/includes/actions/class-amp-paired-mode-actions.php index 68f2d047ff3..ac071f8cd99 100644 --- a/includes/actions/class-amp-paired-mode-actions.php +++ b/includes/actions/class-amp-paired-mode-actions.php @@ -2,4 +2,4 @@ class AMP_Paired_Mode_Actions extends AMP_Actions { -} \ No newline at end of file +} diff --git a/includes/actions/class-amp-paired-post-actions.php b/includes/actions/class-amp-paired-post-actions.php index a08d1dba4d3..26247c76a93 100644 --- a/includes/actions/class-amp-paired-post-actions.php +++ b/includes/actions/class-amp-paired-post-actions.php @@ -4,7 +4,7 @@ class AMP_Paired_Post_Actions extends AMP_Paired_Mode_Actions { - public static function register() { + public static function register_hooks() { add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_title' ); add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_canonical_link' ); add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_scripts' ); @@ -15,19 +15,19 @@ public static function register() { add_action( 'amp_post_template_data', 'AMP_Paired_Post_Actions::add_analytics_scripts' ); add_action( 'amp_post_template_footer', 'AMP_Paired_Post_Actions::add_analytics_data' ); } - + public static function add_title( $amp_template ) { ?> <?php echo esc_html( $amp_template->get( 'document_title' ) ); ?> get( 'amp_component_scripts', array() ); foreach ( $scripts as $element => $script ) : @@ -37,20 +37,20 @@ public static function add_scripts( $amp_template ) { get( 'font_urls', array() ); foreach ( $font_urls as $slug => $url ) : ?> get( 'metadata' ); if ( empty( $metadata ) ) { @@ -60,7 +60,7 @@ public static function add_schemaorg_metadata( $amp_template ) { get( 'post_amp_styles' ); if ( ! empty( $styles ) ) { @@ -103,4 +103,4 @@ public static function add_analytics_data( $amp_template ) { echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); } } -} \ No newline at end of file +} From f383a0fbe5074f735ac7c3df9085ee4c03649d64 Mon Sep 17 00:00:00 2001 From: Alberto Medina Date: Fri, 11 Aug 2017 13:06:45 -0700 Subject: [PATCH 4/5] Remove redundant file and corresponding file include and function call and --- amp.php | 2 - includes/amp-post-template-actions.php | 101 ------------------------- 2 files changed, 103 deletions(-) delete mode 100644 includes/amp-post-template-actions.php diff --git a/amp.php b/amp.php index 4218c68dac2..6d034fc1bcc 100755 --- a/amp.php +++ b/amp.php @@ -21,7 +21,6 @@ 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/amp-post-template-actions.php' ); require_once ( AMP__DIR__ . '/includes/actions/class-amp-frontend-actions.php' ); require_once( AMP__DIR__ . '/includes/actions/class-amp-paired-post-actions.php' ); @@ -121,7 +120,6 @@ function amp_add_frontend_actions() { function amp_add_post_template_actions() { 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/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 ); - } -} From dde7cf5836930c3c64298ad50467806dbcb06766 Mon Sep 17 00:00:00 2001 From: Alberto Medina Date: Thu, 17 Aug 2017 09:20:36 -0700 Subject: [PATCH 5/5] Fix minor formatting issues; remove classes in Actions hierarchy not used currently and will be added in subsequent PR --- amp.php | 2 +- includes/actions/class-amp-canonical-mode-actions.php | 5 ----- includes/actions/class-amp-canonical-post-actions.php | 5 ----- includes/actions/class-amp-frontend-actions.php | 2 +- includes/actions/class-amp-paired-mode-actions.php | 5 ----- includes/actions/class-amp-paired-post-actions.php | 4 ++-- 6 files changed, 4 insertions(+), 19 deletions(-) delete mode 100644 includes/actions/class-amp-canonical-mode-actions.php delete mode 100644 includes/actions/class-amp-canonical-post-actions.php delete mode 100644 includes/actions/class-amp-paired-mode-actions.php diff --git a/amp.php b/amp.php index 6d034fc1bcc..c55599bf49c 100755 --- a/amp.php +++ b/amp.php @@ -21,7 +21,7 @@ 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-frontend-actions.php' ); require_once( AMP__DIR__ . '/includes/actions/class-amp-paired-post-actions.php' ); register_activation_hook( __FILE__, 'amp_activate' ); diff --git a/includes/actions/class-amp-canonical-mode-actions.php b/includes/actions/class-amp-canonical-mode-actions.php deleted file mode 100644 index c93ff2df296..00000000000 --- a/includes/actions/class-amp-canonical-mode-actions.php +++ /dev/null @@ -1,5 +0,0 @@ -