Skip to content

Commit

Permalink
Merge pull request #714 from Automattic/amedina/amp-analytics-customizer
Browse files Browse the repository at this point in the history
Enable adding AMP Analytics components using the Options API
  • Loading branch information
amedina authored Jul 20, 2017
2 parents afd52cb + e7ba261 commit c0bdac8
Show file tree
Hide file tree
Showing 16 changed files with 641 additions and 37 deletions.
1 change: 1 addition & 0 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function amp_add_frontend_actions() {
function amp_add_post_template_actions() {
require_once( AMP__DIR__ . '/includes/amp-post-template-actions.php' );
require_once( AMP__DIR__ . '/includes/amp-post-template-functions.php' );
amp_post_template_init_hooks();
}

function amp_prepare_render() {
Expand Down
78 changes: 59 additions & 19 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
// Callbacks for adding AMP-related things to the admin.

require_once( AMP__DIR__ . '/includes/options/class-amp-options-menu.php' );
require_once( AMP__DIR__ . '/includes/options/views/class-amp-options-manager.php' );

define( 'AMP_CUSTOMIZER_QUERY_VAR', 'customize_amp' );

/**
Expand All @@ -22,6 +25,34 @@ function amp_init_customizer() {
add_action( 'admin_menu', 'amp_add_customizer_link' );
}

function amp_admin_get_preview_permalink() {
/**
* Filter the post type to retrieve the latest for use in the AMP template customizer.
*
* @param string $post_type Post type slug. Default 'post'.
*/
$post_type = (string) apply_filters( 'amp_customizer_post_type', 'post' );

if ( ! post_type_supports( $post_type, 'amp' ) ) {
return;
}

$post_ids = get_posts( array(
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => 1,
'fields' => 'ids',
) );

if ( empty( $post_ids ) ) {
return false;
}

$post_id = $post_ids[0];

return amp_get_permalink( $post_id );
}

/**
* Registers a submenu page to access the AMP template editor panel in the Customizer.
*/
Expand All @@ -34,38 +65,47 @@ function amp_add_customizer_link() {
), 'customize.php' );

// Add the theme page.
$page = add_theme_page(
add_theme_page(
__( 'AMP', 'amp' ),
__( 'AMP', 'amp' ),
'edit_theme_options',
$menu_slug
);
}

function amp_admin_get_preview_permalink() {
/**
* Filter the post type to retrieve the latest of for use in the AMP template customizer.
*
* @param string $post_type Post type slug. Default 'post'.
*/
$post_type = (string) apply_filters( 'amp_customizer_post_type', 'post' );
/**
* Registers a top-level menu for AMP configuration options
*/
function amp_add_options_menu() {
if ( ! is_admin() ) {
return;
}

if ( ! post_type_supports( $post_type, 'amp' ) ) {
$show_options_menu = apply_filters( 'amp_options_menu_is_enabled', true );
if ( true !== $show_options_menu ) {
return;
}

$post_ids = get_posts( array(
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => 1,
'fields' => 'ids',
) );
$amp_options = new AMP_Options_Menu();
$amp_options->init();
}
add_action( 'wp_loaded', 'amp_add_options_menu' );

if ( empty( $post_ids ) ) {
return false;
function amp_add_custom_analytics( $analytics ) {
$analytics_entries = AMP_Options_Manager::get_option( 'analytics', array() );

if ( ! $analytics_entries ) {
return $analytics;
}

$post_id = $post_ids[0];
foreach ( $analytics_entries as $entry_id => $entry ) {
$analytics[ $entry_id ] = array(
'type' => $entry['type'],
'attributes' => array(),
'config_data' => json_decode( $entry['config'] ),
);
}

return amp_get_permalink( $post_id );
return $analytics;
}
add_filter( 'amp_post_template_analytics', 'amp_add_custom_analytics' );
24 changes: 13 additions & 11 deletions includes/amp-post-template-actions.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
<?php
// Callbacks for adding content to an AMP template

add_action( 'amp_post_template_head', 'amp_post_template_add_title' );
function amp_post_template_init_hooks() {
add_action( 'amp_post_template_head', 'amp_post_template_add_title' );
add_action( 'amp_post_template_head', 'amp_post_template_add_canonical' );
add_action( 'amp_post_template_head', 'amp_post_template_add_scripts' );
add_action( 'amp_post_template_head', 'amp_post_template_add_fonts' );
add_action( 'amp_post_template_head', 'amp_post_template_add_boilerplate_css' );
add_action( 'amp_post_template_head', 'amp_post_template_add_schemaorg_metadata' );
add_action( 'amp_post_template_css', 'amp_post_template_add_styles', 99 );
add_action( 'amp_post_template_data', 'amp_post_template_add_analytics_script' );
add_action( 'amp_post_template_footer', 'amp_post_template_add_analytics_data' );
}

function amp_post_template_add_title( $amp_template ) {
?>
<title><?php echo esc_html( $amp_template->get( 'document_title' ) ); ?></title>
<?php
}

add_action( 'amp_post_template_head', 'amp_post_template_add_canonical' );
function amp_post_template_add_canonical( $amp_template ) {
?>
<link rel="canonical" href="<?php echo esc_url( $amp_template->get( 'canonical_url' ) ); ?>" />
<?php
}

add_action( 'amp_post_template_head', 'amp_post_template_add_scripts' );
function amp_post_template_add_scripts( $amp_template ) {
$scripts = $amp_template->get( 'amp_component_scripts', array() );
foreach ( $scripts as $element => $script ) :
foreach ( $scripts as $element => $script ) :
$custom_type = ($element == 'amp-mustache') ? 'template' : 'element'; ?>
<script custom-<?php echo esc_attr( $custom_type ); ?>="<?php echo esc_attr( $element ); ?>" src="<?php echo esc_url( $script ); ?>" async></script>
<?php endforeach; ?>
<script src="<?php echo esc_url( $amp_template->get( 'amp_runtime_script' ) ); ?>" async></script>
<?php
}

add_action( 'amp_post_template_head', 'amp_post_template_add_fonts' );
function amp_post_template_add_fonts( $amp_template ) {
$font_urls = $amp_template->get( 'font_urls', array() );
foreach ( $font_urls as $slug => $url ) : ?>
<link rel="stylesheet" href="<?php echo esc_url( $url ); ?>">
<?php endforeach;
}

add_action( 'amp_post_template_head', 'amp_post_template_add_boilerplate_css' );
function amp_post_template_add_boilerplate_css( $amp_template ) {
?>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<?php
}

add_action( 'amp_post_template_head', 'amp_post_template_add_schemaorg_metadata' );
function amp_post_template_add_schemaorg_metadata( $amp_template ) {
$metadata = $amp_template->get( 'metadata' );
if ( empty( $metadata ) ) {
Expand All @@ -52,7 +58,6 @@ function amp_post_template_add_schemaorg_metadata( $amp_template ) {
<?php
}

add_action( 'amp_post_template_css', 'amp_post_template_add_styles', 99 );
function amp_post_template_add_styles( $amp_template ) {
$styles = $amp_template->get( 'post_amp_styles' );
if ( ! empty( $styles ) ) {
Expand All @@ -64,15 +69,13 @@ function amp_post_template_add_styles( $amp_template ) {
}
}

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 ) ) {
Expand All @@ -84,7 +87,6 @@ function amp_post_template_add_analytics_data( $amp_template ) {
_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'] ) );
Expand Down
53 changes: 53 additions & 0 deletions includes/options/class-amp-analytics-options-submenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

require_once( AMP__DIR__ . '/includes/options/views/class-amp-analytics-options-submenu-page.php' );
require_once( AMP__DIR__ . '/includes/utils/class-amp-html-utils.php' );

class AMP_Analytics_Options_Submenu {

private $parent_menu_slug;
private $menu_slug;
private $menu_page;

public function __construct( $parent_menu_slug ) {
$this->parent_menu_slug = $parent_menu_slug;
$this->menu_slug = 'amp-analytics-options';
$this->menu_page = new AMP_Analytics_Options_Submenu_Page();
}

public function init() {
$this->add_submenu();
add_action(
'admin_print_styles-amp_page_' . $this->menu_slug,
array( $this, 'amp_options_styles' )
);
}

private function add_submenu() {
add_submenu_page(
$this->parent_menu_slug,
__( 'AMP Analytics Options', 'amp' ),
__( 'Analytics', 'amp' ),
'manage_options',
$this->menu_slug,
array( $this->menu_page, 'render' )
);
}

public function amp_options_styles() {
?>
<style>
.analytics-data-container #delete {
background: red;
border-color: red;
text-shadow: 0 0 0;
margin: 0 5px;
}
.amp-analytics-options.notice {
width: 300px;
}
</style>;

<?php
}
}
54 changes: 54 additions & 0 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

require_once( AMP__DIR__ . '/includes/options/class-amp-analytics-options-submenu.php' );
require_once( AMP__DIR__ . '/includes/options/views/class-amp-options-menu-page.php' );
require_once( AMP__DIR__ . '/includes/options/views/class-amp-options-manager.php' );

class AMP_Options_Menu {
const ICON_BASE64_SVG = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyB3aWR0aD0iNjJweCIgaGVpZ2h0PSI2MnB4IiB2aWV3Qm94PSIwIDAgNjIgNjIiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+ICAgICAgICA8dGl0bGU+QU1QLUJyYW5kLUJsYWNrLUljb248L3RpdGxlPiAgICA8ZGVzYz5DcmVhdGVkIHdpdGggU2tldGNoLj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EzLjg3OTczMzMgMCwzMSBDMCw0OC4xMjEzIDEzLjg3ODcsNjIgMzEsNjIgQzQ4LjEyMDI2NjcsNjIgNjIsNDguMTIxMyA2MiwzMSBDNjIsMTMuODc5NzMzMyA0OC4xMjAyNjY3LDAgMzEsMCBMMzEsMCBaIiBpZD0iRmlsbC0xIj48L3BhdGg+ICAgICAgICA8L2c+ICAgIDwvZz48L3N2Zz4=';

private $menu_page;
private $menu_slug;

public function __construct() {
$this->menu_page = new AMP_Options_Menu_Page();
$this->menu_slug = 'amp-plugin-options';
}

public function init() {
add_action( 'admin_post_amp_analytics_options', 'AMP_Options_Manager::handle_analytics_submit' );

add_action( 'admin_menu', array( $this, 'add_menu_items' ) );
}

public function add_menu_items() {
add_menu_page(
__( 'AMP Options', 'amp' ),
__( 'AMP', 'amp' ),
'manage_options',
$this->menu_slug,
array( $this->menu_page, 'render' ),
self::ICON_BASE64_SVG
);

$submenus = array(
new AMP_Analytics_Options_Submenu( $this->menu_slug ),
);

// Create submenu items and calls on the Submenu Page object to render the actual contents of the page.
foreach ( $submenus as $submenu ) {
$submenu->init( $this->menu_slug );
}

$this->remove_toplevel_menu_item();
}

// Helper function to avoid having the top-level menu as
// the first menu item
function remove_toplevel_menu_item() {
global $submenu;
if ( isset( $submenu['amp-plugin-options'][0] ) ) {
unset( $submenu['amp-plugin-options'][0] );
}
}
}
Loading

0 comments on commit c0bdac8

Please sign in to comment.