forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpcom-helper.php
237 lines (201 loc) · 6.12 KB
/
wpcom-helper.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* WPCOM-specific things.
*
* @todo Move this into Jetpack. See https://github.com/Automattic/amp-wp/issues/1021
* @package AMP
*/
add_action( 'pre_amp_render_post', 'jetpack_amp_disable_the_content_filters' );
// Disable admin menu.
add_filter( 'amp_options_menu_is_enabled', '__return_false', 9999 );
/**
* Disable the_content filters for Jetpack.
*
* @since 0.3
*/
function jetpack_amp_disable_the_content_filters() {
add_filter( 'post_flair_disable', '__return_true', 99 );
add_filter( 'videopress_show_2015_player', '__return_true' );
add_filter( 'protected_embeds_use_form_post', '__return_false' );
remove_filter( 'the_title', 'widont' );
remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
}
add_action( 'amp_post_template_head', 'jetpack_amp_add_og_tags' );
/**
* Add Open Graph tags.
*
* @since 0.3
*/
function jetpack_amp_add_og_tags() {
if ( function_exists( 'jetpack_og_tags' ) ) {
jetpack_og_tags();
}
}
add_filter( 'amp_post_template_metadata', 'jetpack_amp_post_template_metadata', 10, 2 );
/**
* Add publisher and image metadata.
*
* @since 0.3
*
* @param array $metadata Metadata array.
* @param WP_Post $post Post.
* @return array Modified metadata array.
*/
function jetpack_amp_post_template_metadata( $metadata, $post ) {
if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
$metadata = wpcom_amp_add_blavatar_to_metadata( $metadata );
}
if ( ! isset( $metadata['image'] ) ) {
$metadata = wpcom_amp_add_image_to_metadata( $metadata, $post );
}
return $metadata;
}
/**
* Add blavatar to metadata.
*
* @since 0.3
*
* @param array $metadata Metadata.
* @return array Metadata.
*/
function wpcom_amp_add_blavatar_to_metadata( $metadata ) {
if ( ! function_exists( 'blavatar_domain' ) ) {
return $metadata;
}
$size = 60;
$metadata['publisher']['logo'] = array(
'@type' => 'ImageObject',
'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
'width' => $size,
'height' => $size,
);
return $metadata;
}
/**
* Add image to metadata.
*
* @since 0.3.2
*
* @param array $metadata Metadata.
* @param WP_Post $post Post.
* @return array Metadata.
*/
function wpcom_amp_add_image_to_metadata( $metadata, $post ) {
if ( ! class_exists( 'Jetpack_PostImages' ) ) {
return wpcom_amp_add_fallback_image_to_metadata( $metadata );
}
$image = Jetpack_PostImages::get_image( $post->ID, array(
'fallback_to_avatars' => true,
'avatar_size' => 200,
// AMP already attempts these.
'from_thumbnail' => false,
'from_attachment' => false,
) );
if ( empty( $image ) ) {
return wpcom_amp_add_fallback_image_to_metadata( $metadata );
}
if ( ! isset( $image['src_width'] ) ) {
$dimensions = wpcom_amp_extract_image_dimensions_from_getimagesize( array(
$image['src'] => false,
) );
if ( false !== $dimensions[ $image['src'] ] ) {
$image['src_width'] = $dimensions['width'];
$image['src_height'] = $dimensions['height'];
}
}
$metadata['image'] = array(
'@type' => 'ImageObject',
'url' => $image['src'],
'width' => $image['src_width'],
'height' => $image['src_height'],
);
return $metadata;
}
/**
* Add fallback image to metadata.
*
* @since 0.3.2
*
* @param array $metadata Metadata.
* @return array Metadata.
*/
function wpcom_amp_add_fallback_image_to_metadata( $metadata ) {
$metadata['image'] = array(
'@type' => 'ImageObject',
'url' => staticize_subdomain( 'https://wordpress.com/i/blank.jpg' ),
'width' => 200,
'height' => 200,
);
return $metadata;
}
add_action( 'amp_extract_image_dimensions_batch_callbacks_registered', 'wpcom_amp_extract_image_dimensions_batch_add_custom_callbacks' );
/**
* Add hooks to extract image dimensions.
*
* @since 0.5
*/
function wpcom_amp_extract_image_dimensions_batch_add_custom_callbacks() {
// If images are being served from Photon or WP.com files, try extracting the size using querystring.
add_action( 'amp_extract_image_dimensions_batch', 'wpcom_amp_extract_image_dimensions_from_querystring', 9, 1 ); // Hook in before the default extractors.
// Uses a special wpcom lib (wpcom_getimagesize) to extract dimensions as a last resort if we weren't able to figure them out.
add_action( 'amp_extract_image_dimensions_batch', 'wpcom_amp_extract_image_dimensions_from_getimagesize', 99, 1 ); // Our last resort, so run late.
// The wpcom override obviates this one, so take it out.
remove_filter( 'amp_extract_image_dimensions_batch', array( 'AMP_Image_Dimension_Extractor', 'extract_by_downloading_images' ), 999 );
}
/**
* Extract image dimensions from query string.
*
* @since 0.5
*
* @param array $dimensions Dimensions.
* @return array Dimensions.
*/
function wpcom_amp_extract_image_dimensions_from_querystring( $dimensions ) {
foreach ( $dimensions as $url => $value ) {
if ( is_array( $value ) ) {
continue;
}
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ! wp_endswith( $host, '.wp.com' ) && ! wp_endswith( $host, '.files.wordpress.com' ) ) {
continue;
}
parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query );
$w = isset( $query['w'] ) ? absint( $query['w'] ) : false;
$h = isset( $query['h'] ) ? absint( $query['h'] ) : false;
if ( false !== $w && false !== $h ) {
$dimensions[ $url ] = array(
'width' => $w,
'height' => $h,
);
}
}
return $dimensions;
}
/**
* Extract image dimensions via wpcom/imagesize.
*
* @since 0.5
*
* @param array $dimensions Dimensions.
* @return array Dimensions.
*/
function wpcom_amp_extract_image_dimensions_from_getimagesize( $dimensions ) {
if ( ! function_exists( 'require_lib' ) ) {
return $dimensions;
}
require_lib( 'wpcom/imagesize' );
foreach ( $dimensions as $url => $value ) {
if ( is_array( $value ) ) {
continue;
}
$result = wpcom_getimagesize( $url );
if ( is_array( $result ) ) {
$dimensions[ $url ] = array(
'width' => $result[0],
'height' => $result[1],
);
}
}
return $dimensions;
}