Skip to content

Commit 18ab955

Browse files
committed
Use queried object in amp_render() and allow passing post object to AMP_Post_Template for sake of preview
1 parent 4f58949 commit 18ab955

2 files changed

Lines changed: 163 additions & 58 deletions

File tree

amp.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,40 @@ function amp_prepare_render() {
148148
add_action( 'template_redirect', 'amp_render' );
149149
}
150150

151+
/**
152+
* Render AMP for queried post.
153+
*
154+
* @since 0.1
155+
*/
151156
function amp_render() {
152-
$post_id = get_queried_object_id();
153-
amp_render_post( $post_id );
157+
158+
// Note that queried object is used instead of the ID so that the_preview for the queried post can apply.
159+
amp_render_post( get_queried_object() );
154160
exit;
155161
}
156162

157-
function amp_render_post( $post_id ) {
158-
$post = get_post( $post_id );
159-
if ( ! $post ) {
160-
return;
163+
/**
164+
* Render AMP post template.
165+
*
166+
* @since 0.5
167+
* @param WP_Post|int $post Post.
168+
*/
169+
function amp_render_post( $post ) {
170+
171+
if ( ! ( $post instanceof WP_Post ) ) {
172+
$post = get_post( $post );
173+
if ( ! $post ) {
174+
return;
175+
}
161176
}
177+
$post_id = $post->ID;
162178

163179
amp_load_classes();
164180

165181
do_action( 'pre_amp_render_post', $post_id );
166182

167183
amp_add_post_template_actions();
168-
$template = new AMP_Post_Template( $post_id );
184+
$template = new AMP_Post_Template( $post );
169185
$template->load();
170186
}
171187

includes/class-amp-post-template.php

Lines changed: 140 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,129 @@
11
<?php
2-
3-
require_once( AMP__DIR__ . '/includes/utils/class-amp-dom-utils.php' );
4-
require_once( AMP__DIR__ . '/includes/utils/class-amp-html-utils.php' );
5-
require_once( AMP__DIR__ . '/includes/utils/class-amp-string-utils.php' );
6-
require_once( AMP__DIR__ . '/includes/utils/class-amp-wp-utils.php' );
7-
8-
require_once( AMP__DIR__ . '/includes/class-amp-content.php' );
9-
10-
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-style-sanitizer.php' );
11-
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-blacklist-sanitizer.php' );
12-
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php' );
13-
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-img-sanitizer.php' );
14-
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-video-sanitizer.php' );
15-
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-iframe-sanitizer.php' );
16-
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-audio-sanitizer.php' );
17-
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-playbuzz-sanitizer.php' );
18-
19-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-twitter-embed.php' );
20-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-youtube-embed.php' );
21-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-dailymotion-embed.php' );
22-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-vimeo-embed.php' );
23-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-soundcloud-embed.php' );
24-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-gallery-embed.php' );
25-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-instagram-embed.php' );
26-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-vine-embed.php' );
27-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-facebook-embed.php' );
28-
require_once( AMP__DIR__ . '/includes/embeds/class-amp-pinterest-embed.php' );
29-
2+
/**
3+
* AMP_Post_Template class.
4+
*
5+
* @package AMP
6+
*/
7+
8+
require_once AMP__DIR__ . '/includes/utils/class-amp-dom-utils.php';
9+
require_once AMP__DIR__ . '/includes/utils/class-amp-html-utils.php';
10+
require_once AMP__DIR__ . '/includes/utils/class-amp-string-utils.php';
11+
require_once AMP__DIR__ . '/includes/utils/class-amp-wp-utils.php';
12+
13+
require_once AMP__DIR__ . '/includes/class-amp-content.php';
14+
15+
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-style-sanitizer.php';
16+
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-blacklist-sanitizer.php';
17+
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php';
18+
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-img-sanitizer.php';
19+
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-video-sanitizer.php';
20+
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-iframe-sanitizer.php';
21+
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-audio-sanitizer.php';
22+
require_once AMP__DIR__ . '/includes/sanitizers/class-amp-playbuzz-sanitizer.php';
23+
24+
require_once AMP__DIR__ . '/includes/embeds/class-amp-twitter-embed.php';
25+
require_once AMP__DIR__ . '/includes/embeds/class-amp-youtube-embed.php';
26+
require_once AMP__DIR__ . '/includes/embeds/class-amp-dailymotion-embed.php';
27+
require_once AMP__DIR__ . '/includes/embeds/class-amp-vimeo-embed.php';
28+
require_once AMP__DIR__ . '/includes/embeds/class-amp-soundcloud-embed.php';
29+
require_once AMP__DIR__ . '/includes/embeds/class-amp-gallery-embed.php';
30+
require_once AMP__DIR__ . '/includes/embeds/class-amp-instagram-embed.php';
31+
require_once AMP__DIR__ . '/includes/embeds/class-amp-vine-embed.php';
32+
require_once AMP__DIR__ . '/includes/embeds/class-amp-facebook-embed.php';
33+
require_once AMP__DIR__ . '/includes/embeds/class-amp-pinterest-embed.php';
34+
35+
/**
36+
* Class AMP_Post_Template
37+
*
38+
* @since 0.2
39+
*/
3040
class AMP_Post_Template {
41+
42+
/**
43+
* Site icon size.
44+
*
45+
* @since 0.2
46+
* @var int
47+
*/
3148
const SITE_ICON_SIZE = 32;
49+
50+
/**
51+
* Content max width.
52+
*
53+
* @since 0.4
54+
* @var int
55+
*/
3256
const CONTENT_MAX_WIDTH = 600;
3357

34-
// Needed for 0.3 back-compat
58+
/**
59+
* Default navbar background.
60+
*
61+
* Needed for 0.3 back-compat
62+
*
63+
* @since 0.4
64+
* @var string
65+
*/
3566
const DEFAULT_NAVBAR_BACKGROUND = '#0a89c0';
67+
68+
/**
69+
* Default navbar color.
70+
*
71+
* Needed for 0.3 back-compat
72+
*
73+
* @since 0.4
74+
* @var string
75+
*/
3676
const DEFAULT_NAVBAR_COLOR = '#fff';
3777

78+
/**
79+
* Template directory.
80+
*
81+
* @since 0.2
82+
* @var string
83+
*/
3884
private $template_dir;
85+
86+
/**
87+
* Post template data.
88+
*
89+
* @since 0.2
90+
* @var array
91+
*/
3992
private $data;
4093

41-
public function __construct( $post_id ) {
94+
/**
95+
* Post ID.
96+
*
97+
* @since 0.2
98+
* @var int
99+
*/
100+
public $ID;
101+
102+
/**
103+
* Post.
104+
*
105+
* @since 0.2
106+
* @var WP_Post
107+
*/
108+
public $post;
109+
110+
/**
111+
* AMP_Post_Template constructor.
112+
*
113+
* @param WP_Post|int $post Post.
114+
*/
115+
public function __construct( $post ) {
116+
42117
$this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' );
43118

44-
$this->ID = $post_id;
45-
$this->post = get_post( $post_id );
119+
if ( $post instanceof WP_Post ) {
120+
$this->ID = $post->ID;
121+
$this->post = $post;
122+
} else {
123+
$this->post = get_post( $post );
124+
$this->ID = $this->post->ID;
125+
}
126+
$post_id = $this->ID;
46127

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

53134
$this->data = array(
54-
'content_max_width' => $content_max_width,
135+
'content_max_width' => $content_max_width,
55136

56-
'document_title' => function_exists( 'wp_get_document_title' ) ? wp_get_document_title() : wp_title( '', false ), // back-compat with 4.3
57-
'canonical_url' => get_permalink( $post_id ),
58-
'home_url' => home_url(),
59-
'blog_name' => get_bloginfo( 'name' ),
137+
'document_title' => function_exists( 'wp_get_document_title' ) ? wp_get_document_title() : wp_title( '', false ), // Back-compat with 4.3.
138+
'canonical_url' => get_permalink( $post_id ),
139+
'home_url' => home_url(),
140+
'blog_name' => get_bloginfo( 'name' ),
60141
'generator_metadata' => 'AMP Plugin v' . AMP__VERSION,
61142

62-
'html_tag_attributes' => array(),
63-
'body_class' => '',
143+
'html_tag_attributes' => array(),
144+
'body_class' => '',
64145

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

68-
'featured_image' => false,
69-
'comments_link_url' => false,
70-
'comments_link_text' => false,
149+
'featured_image' => false,
150+
'comments_link_url' => false,
151+
'comments_link_text' => false,
71152

72-
'amp_runtime_script' => 'https://cdn.ampproject.org/v0.js',
153+
'amp_runtime_script' => 'https://cdn.ampproject.org/v0.js',
73154
'amp_component_scripts' => array(),
74155

75-
'customizer_settings' => array(),
156+
'customizer_settings' => array(),
76157

77-
'font_urls' => array(
158+
'font_urls' => array(
78159
'merriweather' => 'https://fonts.googleapis.com/css?family=Merriweather:400,400italic,700,700italic',
79160
),
80161

81-
'post_amp_styles' => array(),
162+
'post_amp_styles' => array(),
82163

83164
/**
84165
* Add amp-analytics tags.
85166
*
86167
* This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting.
87168
*
88169
* @since 0.4
89-
*.
90-
* @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.
91-
* @param object $post The current post.
170+
*
171+
* @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.
172+
* @param WP_Post $post The current post.
92173
*/
93174
'amp_analytics' => apply_filters( 'amp_post_template_analytics', array(), $this->post ),
94-
);
175+
);
95176

96177
$this->build_post_content();
97178
$this->build_post_data();
98179
$this->build_customizer_settings();
99180
$this->build_html_tag_attributes();
100181

182+
/**
183+
* Filters AMP template data.
184+
*
185+
* @since 0.2
186+
*
187+
* @param array $data Template data.
188+
* @param WP_Post $post Post.
189+
*/
101190
$this->data = apply_filters( 'amp_post_template_data', $this->data, $this->post );
102191
}
103192

0 commit comments

Comments
 (0)