forked from Automattic/fb-instant-articles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-instant-articles-amp-markup.php
180 lines (145 loc) · 4.45 KB
/
class-instant-articles-amp-markup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Facebook Instant Articles for WP.
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @package default
* @since 4.0
*/
use Facebook\InstantArticles\AMP\AMPArticle;
use Facebook\InstantArticles\Validators\Type;
class Instant_Articles_AMP_Markup {
const SETTING_AMP_MARKUP = 'amp_markup'; // Setting to check if AMP Markup generation is enabled
const SETTING_STYLE = 'amp_stylesheet'; // Setting that stores the JSON stylesheet
const SETTING_DL_MEDIA = 'amp_download_media'; // Enable or disable media download option
const QUERY_ARG = 'amp_markup'; // Query argument that will trigger the AMP markup generation
// To memoize the settings
static $settings = null;
/**
* Gets the settings
*
* @return array The settings, check Instant_Articles_Option::get_option_decoded()
* @since 4.0
*/
static function get_settings() {
if ( self::$settings === null ) {
self::$settings = Instant_Articles_Option_AMP::get_option_decoded();
}
return self::$settings;
}
/**
* Checks if the AMP markup is enabled
*
* @return bool true if markup is enabled
* @since 4.0
*/
static function is_markup_enabled() {
$settings = self::get_settings();
return
isset( $settings[ self::SETTING_AMP_MARKUP ] )
? (bool) $settings[ self::SETTING_AMP_MARKUP ]
: false;
}
/**
* Adds the meta tag for AMP Markup if enabled
*
* @since 4.0
*/
static function inject_link_rel() {
if ( ! self::is_markup_enabled() ) {
return;
}
// Transform the post to an Instant Article.
$adapter = new Instant_Articles_Post( get_post() );
if ( ! $adapter->should_submit_post() ) {
return;
}
$url = $adapter->get_canonical_url();
$url = add_query_arg( self::QUERY_ARG, '1', $url );
?>
<link rel="amphtml" href="<?php echo esc_url($url); ?>">
<?php
}
/**
* Generates the AMP markup if post has amp_markup
*
* NOTE: side-effect: function calls die() in the end.
*
* @since 4.0
*/
static function markup_version() {
if ( ! (isset( $_GET[ self::QUERY_ARG ] ) && $_GET[ self::QUERY_ARG ]) ) {
return;
}
$settings = self::get_settings();
if ( ! self::is_markup_enabled() ) {
return;
}
$has_stylesheet =
isset( $settings[ self::SETTING_STYLE ] )
? self::validate_json( $settings[ self::SETTING_STYLE ] )
: false;
$properties = array();
$download_media =
isset( $settings[ self::SETTING_DL_MEDIA ] )
? (bool) $settings[ self::SETTING_DL_MEDIA ]
: false;
$properties[ AMPArticle::ENABLE_DOWNLOAD_FOR_MEDIA_SIZING_KEY ] = $download_media;
if ( $has_stylesheet ) {
$properties[ AMPArticle::OVERRIDE_STYLES_KEY ] =
json_decode( $settings[ self::SETTING_STYLE ], true );
}
$post = get_post();
// This array will hold the image sizes
$media_sizes = array();
// Get all children with mime type image that are attached to the posts
// NOTE: this will not get images that are not hosted in the WP
$query_args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => 100,
'post_mime_type' => 'image'
);
$image_children = get_children( $query_args );
foreach ( $image_children as $img_id => $img ) {
$meta = wp_get_attachment_metadata( $img_id );
// Removes the file name from the URL
$url_chunks = explode( '/', $img->guid );
array_pop( $url_chunks );
$base_image_url = implode( '/', $url_chunks ) . '/';
// This is the uploaded original file
$media_sizes[ $img->guid ] = array( $meta['width'], $meta['height'] );
// These are the possible redimensions
foreach ( $meta['sizes'] as $size ) {
$size_url = $base_image_url . $size['file'];
$media_sizes[ $size_url ] = array( $size['width'], $size['height'] );
}
}
$properties[ AMPArticle::MEDIA_SIZES_KEY ] = $media_sizes;
// Transform the post to an Instant Article.
$adapter = new Instant_Articles_Post( $post );
$article = $adapter->to_instant_article();
$article_html = $article->render();
$amp = AMPArticle::create( $article_html, $properties );
echo $amp->render();
die();
}
/**
* Helper function to validate the json string
*
* @param $json_str string JSON string
* @return bool true for valid JSON
* @since 4.0
*/
static function validate_json( $json_str ) {
if ( Type::isTextEmpty( $json_str ) ) {
return false;
}
json_decode( $json_str );
if ( json_last_error() == JSON_ERROR_NONE ) {
return true;
}
return false;
}
}