forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpcom-helper.php
156 lines (124 loc) · 4.31 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
<?php
// WPCOM-specific things
define( 'AMP_DEV_MODE', defined( 'WPCOM_SANDBOXED' ) && WPCOM_SANDBOXED );
// Add stats pixel
add_filter( 'amp_post_template_footer', 'jetpack_amp_add_stats_pixel' );
function jetpack_amp_add_stats_pixel( $amp_template ) {
?>
<amp-pixel src="<?php echo esc_url( wpcom_amp_get_pageview_url() ); ?>"></amp-pixel>
<amp-pixel src="<?php echo esc_url( wpcom_amp_get_mc_url() ); ?>"></amp-pixel>
<amp-pixel src="<?php echo esc_url( wpcom_amp_get_stats_extras_url() ); ?>"></amp-pixel>
<?php
}
function wpcom_amp_get_pageview_url() {
$stats_info = stats_collect_info();
$a = $stats_info['st_go_args'];
$url = add_query_arg( array(
'rand' => 'RANDOM', // AMP placeholder
'host' => rawurlencode( $_SERVER['HTTP_HOST'] ),
'ref' => 'DOCUMENT_REFERRER', // AMP placeholder
), 'https://pixel.wp.com/b.gif' );
$url .= '&' . stats_array_string( $a );
return $url;
}
function wpcom_amp_get_mc_url() {
return add_query_arg( array(
'rand' => 'RANDOM', // special amp placeholder
'v' => 'wpcom-no-pv',
'x_amp-views' => 'view',
), 'https://pixel.wp.com/b.gif' );
}
function wpcom_amp_get_stats_extras_url() {
$stats_extras = stats_extras();
if ( ! $stats_extras ) {
return false;
}
$url = add_query_arg( array(
'rand' => 'RANDOM', // special amp placeholder
'v' => 'wpcom-no-pv',
), 'https://pixel.wp.com/b.gif' );
$url .= '&' . stats_array_string( array(
'crypt' => base64_encode(
wp_encrypt_plus(
ltrim(
add_query_arg( $stats_extras, ''),
'?'),
8, 'url')
)
) );
return $url;
}
add_action( 'pre_amp_render_post', 'jetpack_amp_disable_the_content_filters' );
function jetpack_amp_disable_the_content_filters( $post_id ) {
add_filter( 'post_flair_disable', '__return_true', 99 );
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' );
function jetpack_amp_add_og_tags( $amp_template ) {
if ( function_exists( 'jetpack_og_tags' ) ) {
jetpack_og_tags();
}
}
add_filter( 'amp_post_template_metadata', 'jetpack_amp_post_template_metadata', 10, 2 );
function jetpack_amp_post_template_metadata( $metadata, $post ) {
$metadata = wpcom_amp_add_blavatar( $metadata, $post );
return $metadata;
}
function wpcom_amp_add_blavatar( $metadata, $post ) {
if ( ! function_exists( 'blavatar_domain' ) ) {
return $metadata;
}
if ( ! isset( $metadata['publisher'] ) ) {
return $metadata;
}
if ( isset( $metadata['publisher']['logo'] ) ) {
return $metadata;
}
$size = 60;
$blavatar_domain = blavatar_domain( site_url() );
if ( blavatar_exists( $blavatar_domain ) ) {
$metadata['publisher']['logo'] = array(
'@type' => 'ImageObject',
'url' => blavatar_url( $blavatar_domain, 'img', $size, false, true ),
'width' => $size,
'height' => $size,
);
}
return $metadata;
}
// If images are being served from Photon or WP.com files, try extracting the size using querystring.
add_action( 'amp_extract_image_dimensions', 'wpcom_amp_extract_image_dimensions_from_querystring', 9, 2 ); // Hook in before the default extractors
function wpcom_amp_extract_image_dimensions_from_querystring( $dimensions, $url ) {
if ( $dimensions ) {
return $dimensions;
}
$host = parse_url( $url, PHP_URL_HOST );
if ( ! wp_endswith( $host, '.wp.com' ) || ! wp_endswith( $host, '.files.wordpress.com' ) ) {
return false;
}
$query = parse_url( $url, PHP_URL_QUERY );
$w = isset( $query['w'] ) ? absint( $query['w'] ) : false;
$h = isset( $query['h'] ) ? absint( $query['h'] ) : false;
if ( false !== $w && false !== $h ) {
return array( $w, $h );
}
return false;
}
// 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', 'wpcom_amp_extract_image_dimensions_from_getimagesize', 100, 2 ); // Our last resort
function wpcom_amp_extract_image_dimensions_from_getimagesize( $dimensions, $url ) {
if ( $dimensions ) {
return $dimensions;
}
if ( ! function_exists( 'require_lib' ) ) {
return false;
}
require_lib( 'wpcom/imagesize' );
$size = wpcom_getimagesize( $url );
if ( $size ) {
return array( $size[0], $size[1] );
}
return false;
}