Skip to content

Commit

Permalink
Use queried object in amp_render() and allow passing post object to A…
Browse files Browse the repository at this point in the history
…MP_Post_Template for sake of preview
  • Loading branch information
westonruter committed Dec 7, 2017
1 parent 4f58949 commit 18ab955
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 58 deletions.
30 changes: 23 additions & 7 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,40 @@ function amp_prepare_render() {
add_action( 'template_redirect', 'amp_render' );
}

/**
* Render AMP for queried post.
*
* @since 0.1
*/
function amp_render() {
$post_id = get_queried_object_id();
amp_render_post( $post_id );

// Note that queried object is used instead of the ID so that the_preview for the queried post can apply.
amp_render_post( get_queried_object() );
exit;
}

function amp_render_post( $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
return;
/**
* Render AMP post template.
*
* @since 0.5
* @param WP_Post|int $post Post.
*/
function amp_render_post( $post ) {

if ( ! ( $post instanceof WP_Post ) ) {
$post = get_post( $post );
if ( ! $post ) {
return;
}
}
$post_id = $post->ID;

amp_load_classes();

do_action( 'pre_amp_render_post', $post_id );

amp_add_post_template_actions();
$template = new AMP_Post_Template( $post_id );
$template = new AMP_Post_Template( $post );
$template->load();
}

Expand Down
191 changes: 140 additions & 51 deletions includes/class-amp-post-template.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,129 @@
<?php

require_once( AMP__DIR__ . '/includes/utils/class-amp-dom-utils.php' );
require_once( AMP__DIR__ . '/includes/utils/class-amp-html-utils.php' );
require_once( AMP__DIR__ . '/includes/utils/class-amp-string-utils.php' );
require_once( AMP__DIR__ . '/includes/utils/class-amp-wp-utils.php' );

require_once( AMP__DIR__ . '/includes/class-amp-content.php' );

require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-style-sanitizer.php' );
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-blacklist-sanitizer.php' );
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php' );
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-img-sanitizer.php' );
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-video-sanitizer.php' );
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-iframe-sanitizer.php' );
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-audio-sanitizer.php' );
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-playbuzz-sanitizer.php' );

require_once( AMP__DIR__ . '/includes/embeds/class-amp-twitter-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-youtube-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-dailymotion-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-vimeo-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-soundcloud-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-gallery-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-instagram-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-vine-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-facebook-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-pinterest-embed.php' );

/**
* AMP_Post_Template class.
*
* @package AMP
*/

require_once AMP__DIR__ . '/includes/utils/class-amp-dom-utils.php';
require_once AMP__DIR__ . '/includes/utils/class-amp-html-utils.php';
require_once AMP__DIR__ . '/includes/utils/class-amp-string-utils.php';
require_once AMP__DIR__ . '/includes/utils/class-amp-wp-utils.php';

require_once AMP__DIR__ . '/includes/class-amp-content.php';

require_once AMP__DIR__ . '/includes/sanitizers/class-amp-style-sanitizer.php';
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-blacklist-sanitizer.php';
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php';
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-img-sanitizer.php';
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-video-sanitizer.php';
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-iframe-sanitizer.php';
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-audio-sanitizer.php';
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-playbuzz-sanitizer.php';

require_once AMP__DIR__ . '/includes/embeds/class-amp-twitter-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-youtube-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-dailymotion-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-vimeo-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-soundcloud-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-gallery-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-instagram-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-vine-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-facebook-embed.php';
require_once AMP__DIR__ . '/includes/embeds/class-amp-pinterest-embed.php';

/**
* Class AMP_Post_Template
*
* @since 0.2
*/
class AMP_Post_Template {

/**
* Site icon size.
*
* @since 0.2
* @var int
*/
const SITE_ICON_SIZE = 32;

/**
* Content max width.
*
* @since 0.4
* @var int
*/
const CONTENT_MAX_WIDTH = 600;

// Needed for 0.3 back-compat
/**
* Default navbar background.
*
* Needed for 0.3 back-compat
*
* @since 0.4
* @var string
*/
const DEFAULT_NAVBAR_BACKGROUND = '#0a89c0';

/**
* Default navbar color.
*
* Needed for 0.3 back-compat
*
* @since 0.4
* @var string
*/
const DEFAULT_NAVBAR_COLOR = '#fff';

/**
* Template directory.
*
* @since 0.2
* @var string
*/
private $template_dir;

/**
* Post template data.
*
* @since 0.2
* @var array
*/
private $data;

public function __construct( $post_id ) {
/**
* Post ID.
*
* @since 0.2
* @var int
*/
public $ID;

/**
* Post.
*
* @since 0.2
* @var WP_Post
*/
public $post;

/**
* AMP_Post_Template constructor.
*
* @param WP_Post|int $post Post.
*/
public function __construct( $post ) {

$this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' );

$this->ID = $post_id;
$this->post = get_post( $post_id );
if ( $post instanceof WP_Post ) {
$this->ID = $post->ID;
$this->post = $post;
} else {
$this->post = get_post( $post );
$this->ID = $this->post->ID;
}
$post_id = $this->ID;

$content_max_width = self::CONTENT_MAX_WIDTH;
if ( isset( $GLOBALS['content_width'] ) && $GLOBALS['content_width'] > 0 ) {
Expand All @@ -51,53 +132,61 @@ public function __construct( $post_id ) {
$content_max_width = apply_filters( 'amp_content_max_width', $content_max_width );

$this->data = array(
'content_max_width' => $content_max_width,
'content_max_width' => $content_max_width,

'document_title' => function_exists( 'wp_get_document_title' ) ? wp_get_document_title() : wp_title( '', false ), // back-compat with 4.3
'canonical_url' => get_permalink( $post_id ),
'home_url' => home_url(),
'blog_name' => get_bloginfo( 'name' ),
'document_title' => function_exists( 'wp_get_document_title' ) ? wp_get_document_title() : wp_title( '', false ), // Back-compat with 4.3.
'canonical_url' => get_permalink( $post_id ),
'home_url' => home_url(),
'blog_name' => get_bloginfo( 'name' ),
'generator_metadata' => 'AMP Plugin v' . AMP__VERSION,

'html_tag_attributes' => array(),
'body_class' => '',
'html_tag_attributes' => array(),
'body_class' => '',

'site_icon_url' => apply_filters( 'amp_site_icon_url', function_exists( 'get_site_icon_url' ) ? get_site_icon_url( self::SITE_ICON_SIZE ) : '' ),
'site_icon_url' => apply_filters( 'amp_site_icon_url', function_exists( 'get_site_icon_url' ) ? get_site_icon_url( self::SITE_ICON_SIZE ) : '' ),
'placeholder_image_url' => amp_get_asset_url( 'images/placeholder-icon.png' ),

'featured_image' => false,
'comments_link_url' => false,
'comments_link_text' => false,
'featured_image' => false,
'comments_link_url' => false,
'comments_link_text' => false,

'amp_runtime_script' => 'https://cdn.ampproject.org/v0.js',
'amp_runtime_script' => 'https://cdn.ampproject.org/v0.js',
'amp_component_scripts' => array(),

'customizer_settings' => array(),
'customizer_settings' => array(),

'font_urls' => array(
'font_urls' => array(
'merriweather' => 'https://fonts.googleapis.com/css?family=Merriweather:400,400italic,700,700italic',
),

'post_amp_styles' => array(),
'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 object $post The current post.
*
* @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 ),
);
);

$this->build_post_content();
$this->build_post_data();
$this->build_customizer_settings();
$this->build_html_tag_attributes();

/**
* Filters AMP template data.
*
* @since 0.2
*
* @param array $data Template data.
* @param WP_Post $post Post.
*/
$this->data = apply_filters( 'amp_post_template_data', $this->data, $this->post );
}

Expand Down

0 comments on commit 18ab955

Please sign in to comment.