Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pages #188

Closed
wants to merge 10 commits into from
5 changes: 3 additions & 2 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ function amp_init() {

load_plugin_textdomain( 'amp', false, plugin_basename( AMP__DIR__ ) . '/languages' );

add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK );
add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK | EP_PAGES );
add_post_type_support( 'post', AMP_QUERY_VAR );
add_post_type_support( 'page', AMP_QUERY_VAR );

add_action( 'wp', 'amp_maybe_add_actions' );

Expand Down Expand Up @@ -103,4 +104,4 @@ function amp_render() {
$template = new AMP_Post_Template( $post_id );
$template->load();
exit;
}
}
37 changes: 29 additions & 8 deletions includes/class-amp-post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ public function get( $property, $default = null ) {
}

public function load() {
$this->load_parts( array( 'single' ) );

if( true === is_post_type_hierarchical( get_post_type( $this->ID ) ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably move this check to a helper function since it's repeated.

Alternate thoughts: I wonder if we should just be more explicit with both this and the metadata below. For the template we could introduce a hierarchy via a new function like load_first_available_part( array( $this->post->post_type, 'single' ) ) where it finds and displays only the first available template from the array.

Similarly, for metadata, we default post => BlogPosting and page => WebPage and default everything else to a higher level schema object like CreativeWork or even Thing.

Let me know what you think.

$this->load_parts( array( 'page' ) );
} else {
$this->load_parts( array( 'single' ) );
}
}

public function load_parts( $templates ) {
Expand Down Expand Up @@ -109,21 +114,37 @@ private function build_post_data() {

$metadata = array(
'@context' => 'http://schema.org',
'@type' => 'BlogPosting',
'mainEntityOfPage' => $this->get( 'canonical_url' ),
'publisher' => array(
'@type' => 'Organization',
'name' => $this->get( 'blog_name' ),
),
'headline' => $post_title,
'datePublished' => date( 'c', $post_publish_timestamp ),
'dateModified' => date( 'c', $post_modified_timestamp ),
'author' => array(
'@type' => 'Person',
'name' => $post_author->display_name,
),
);

if( true === is_post_type_hierarchical( get_post_type( $this->ID ) ) ) {
$article_meta = array(
'@type' => 'WebPage'
);
} else {
$article_meta = array(
'@type' => 'BlogPosting',
'datePublished' => date( 'c', $post_publish_timestamp ),
'dateModified' => date( 'c', $post_modified_timestamp ),
'author' => array(
'@type' => 'Person',
'name' => $post_author->display_name,
),
);
}

/**
* Allows plugins to add or modify schema.org metadata
*
* @since 0.2
*/
$metadata = apply_filters( 'amp_metadata', array_merge( $metadata, $article_meta ), $this );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a filter further down, so this is unnecessary.


$site_icon_url = $this->get( 'site_icon_url' );
if ( $site_icon_url ) {
$metadata['publisher']['logo'] = array(
Expand Down
37 changes: 37 additions & 0 deletions templates/page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui">
<link href="https://fonts.googleapis.com/css?family=Merriweather:400,400italic,700,700italic|Open+Sans:400,700,400italic,700italic" rel="stylesheet" type="text/css">
<?php do_action( 'amp_post_template_head', $this ); ?>
<style>body {opacity: 0}</style><noscript><style>body {opacity: 1}</style></noscript>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This boilerplate should be removed.


<style amp-custom>
<?php $this->load_parts( array( 'style' ) ); ?>
<?php do_action( 'amp_post_template_css', $this ); ?>
</style>
</head>
<body>
<nav class="title-bar">
<div>
<a href="<?php echo esc_url( $this->get( 'home_url' ) ); ?>">
<?php $site_icon_url = $this->get( 'site_icon_url' ); ?>
<?php if ( $site_icon_url ) : ?>
<amp-img src="<?php echo esc_url( $site_icon_url ); ?>" width="32" height="32" class="site-icon"></amp-img>
<?php else : ?>
<svg x="0px" y="0px" width="24" height="24" viewBox="0 0 24 24">
<path class="st0" d="M12,0C5.4,0,0,5.4,0,12c0,6.6,5.4,12,12,12c6.6,0,12-5.4,12-12C24,5.4,18.6,0,12,0z M1.2,12c0-1.6,0.3-3,0.9-4.4l5.1,14.1C3.7,20,1.2,16.3,1.2,12z M12,22.8c-1.1,0-2.1-0.2-3-0.4l3.2-9.4l3.3,9.1c0,0.1,0,0.1,0.1,0.1C14.5,22.6,13.3,22.8,12,22.8z M13.5,6.9c0.6,0,1.2-0.1,1.2-0.1c0.6-0.1,0.5-0.9-0.1-0.9c0,0-1.7,0.1-2.9,0.1c-1.1,0-2.8-0.1-2.8-0.1c-0.6,0-0.7,0.9-0.1,0.9c0,0,0.6,0.1,1.1,0.1l1.7,4.6l-2.4,7.1L5.4,6.9c0.7,0,1.2-0.1,1.2-0.1c0.6-0.1,0.5-0.9-0.1-0.9c0,0-1.7,0.1-2.9,0.1c-0.2,0-0.4,0-0.7,0c1.9-2.9,5.2-4.9,9-4.9c2.8,0,5.4,1.1,7.3,2.8c0,0-0.1,0-0.1,0c-1.1,0-1.8,0.9-1.8,1.9c0,0.9,0.5,1.6,1.1,2.5c0.4,0.7,0.9,1.6,0.9,3c0,0.9-0.3,2.1-0.8,3.5l-1.1,3.6L13.5,6.9z M17.4,21.3l3.3-9.5c0.6-1.5,0.8-2.8,0.8-3.9c0-0.4,0-0.8-0.1-1.1c0.8,1.5,1.3,3.3,1.3,5.2C22.8,16,20.6,19.5,17.4,21.3z"/>
</svg>
<?php endif; ?>
<?php echo esc_html( $this->get( 'blog_name' ) ); ?>
</a>
</div>
</nav>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to introduce a new part for title bar so we can reuse it across both templates.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes with my prior suggestion this morning about classing and subclassing

<div class="content">
<h1 class="title"><?php echo esc_html( $this->get( 'post_title' ) ); ?></h1>
<?php echo $this->get( 'post_amp_content' ); // amphtml content; no kses ?>
</div>
<?php do_action( 'amp_post_template_footer', $this ); ?>
</body>
</html>