Completed
Push — renovate/babel-plugin-add-modu... ( 6e5406...4d1bee )
by
unknown
06:36
created

Jetpack_AMP_Support::is_amp_request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Manages compatibility with the amp-wp plugin
5
 *
6
 * @see https://github.com/Automattic/amp-wp
7
 */
8
class Jetpack_AMP_Support {
9
10
	static function init() {
11
12
		// enable stats
13
		if ( Jetpack::is_module_active( 'stats' ) ) {
14
			add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
15
		}
16
17
		// carousel
18
		add_filter( 'jp_carousel_maybe_disable', array( __CLASS__, 'is_amp_request' ) );
19
20
		// sharing
21
		add_filter( 'sharing_enqueue_scripts', array( __CLASS__, 'is_not_amp_request' ) );
22
		add_filter( 'jetpack_sharing_counts', array( __CLASS__, 'is_not_amp_request' ) );
23
		add_filter( 'sharing_js', array( __CLASS__, 'is_not_amp_request' ) );
24
		add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
25
26
		// disable lazy images
27
		add_filter( 'lazyload_is_enabled', array( __CLASS__, 'is_not_amp_request' ) );
28
29
		// disable imploding CSS
30
		add_filter( 'jetpack_implode_frontend_css', array( __CLASS__, 'is_not_amp_request' ) );
31
32
		// enforce freedom mode for videopress
33
		add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
34
35
		// include Jetpack og tags when rendering native AMP head
36
		add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
37
38
		// Post rendering changes for legacy AMP
39
		add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
40
41
		// Add post template metadata for legacy AMP
42
		add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
43
	}
44
45
	static function admin_init() {
46
		// disable Likes metabox for post editor if AMP canonical disabled
47
		add_filter( 'post_flair_disable',  array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
48
	}
49
50
	static function is_amp_canonical() {
51
		return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
52
	}
53
54
	static function is_amp_request() {
55
		$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
56
57
		/**
58
		 * Returns true if the current request should return valid AMP content.
59
		 *
60
		 * @since 6.2.0
61
		 *
62
		 * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
63
		 */
64
		return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
65
	}
66
67
	/**
68
	 * Returns whether the request is not AMP.
69
	 *
70
	 * @see Jetpack_AMP_Support::is_amp_request()
71
	 * @return bool Whether not AMP.
72
	 */
73
	static function is_not_amp_request() {
74
		return ! self::is_amp_request();
75
	}
76
77
	static function amp_disable_the_content_filters() {
78
		if ( defined( 'WPCOM') && WPCOM ) {
79
			add_filter( 'videopress_show_2015_player', '__return_true' );
80
			add_filter( 'protected_embeds_use_form_post', '__return_false' );
81
			remove_filter( 'the_title', 'widont' );
82
		}
83
84
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
85
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
86
	}
87
88
	/**
89
	 * Add Jetpack stats pixel.
90
	 *
91
	 * @since 6.2.1
92
	 */
93
	static function add_stats_pixel() {
94
		if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
95
			return;
96
		}
97
		stats_render_amp_footer( stats_build_view_data() );
98
	}
99
100
	/**
101
	 * Add publisher and image metadata to legacy AMP post.
102
	 *
103
	 * @since 6.2.0
104
	 *
105
	 * @param array   $metadata Metadata array.
106
	 * @param WP_Post $post     Post.
107
	 * @return array Modified metadata array.
108
	 */
109
	static function amp_post_template_metadata( $metadata, $post ) {
110
		if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
111
			$metadata = self::add_site_icon_to_metadata( $metadata );
112
		}
113
114
		if ( ! isset( $metadata['image'] ) ) {
115
			$metadata = self::add_image_to_metadata( $metadata, $post );
116
		}
117
118
		return $metadata;
119
	}
120
121
	/**
122
	 * Add blavatar to legacy AMP post metadata.
123
	 *
124
	 * @since 6.2.0
125
	 *
126
	 * @param array $metadata Metadata.
127
	 * @return array Metadata.
128
	 */
129
	static function add_site_icon_to_metadata( $metadata ) {
130
		$size = 60;
131
132
		if ( function_exists( 'blavatar_domain' ) ) {
133
			$metadata['publisher']['logo'] = array(
134
				'@type'  => 'ImageObject',
135
				'url'    => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
136
				'width'  => $size,
137
				'height' => $size,
138
			);
139
		} else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) {
140
			$metadata['publisher']['logo'] = array(
141
				'@type'  => 'ImageObject',
142
				'url'    => $site_icon_url,
143
				'width'  => $size,
144
				'height' => $size,
145
			);
146
		}
147
148
		return $metadata;
149
	}
150
151
	/**
152
	 * Add image to legacy AMP post metadata.
153
	 *
154
	 * @since 6.2.0
155
	 *
156
	 * @param array   $metadata Metadata.
157
	 * @param WP_Post $post     Post.
158
	 * @return array Metadata.
159
	 */
160
	static function add_image_to_metadata( $metadata, $post ) {
161
		$image = Jetpack_PostImages::get_image( $post->ID, array(
162
			'fallback_to_avatars' => true,
163
			'avatar_size'         => 200,
164
			// AMP already attempts these.
165
			'from_thumbnail'      => false,
166
			'from_attachment'     => false,
167
		) );
168
169
		if ( empty( $image ) ) {
170
			return self::add_fallback_image_to_metadata( $metadata );
171
		}
172
173
		if ( ! isset( $image['src_width'] ) ) {
174
			$dimensions = self::extract_image_dimensions_from_getimagesize( array(
175
				$image['src'] => false,
176
			) );
177
178 View Code Duplication
			if ( false !== $dimensions[ $image['src'] ] ) {
179
				$image['src_width']  = $dimensions['width'];
180
				$image['src_height'] = $dimensions['height'];
181
			}
182
		}
183
184
		$metadata['image'] = array(
185
			'@type' => 'ImageObject',
186
			'url'   => $image['src'],
187
		);
188
		if ( isset( $image['src_width'] ) ) {
189
			$metadata['image']['width'] = $image['src_width'];
190
		}
191
		if ( isset( $image['src_width'] ) ) {
192
			$metadata['image']['height'] = $image['src_height'];
193
		}
194
195
		return $metadata;
196
	}
197
198
	/**
199
	 * Add fallback image to legacy AMP post metadata.
200
	 *
201
	 * @since 6.2.0
202
	 *
203
	 * @param array $metadata Metadata.
204
	 * @return array Metadata.
205
	 */
206
	static function add_fallback_image_to_metadata( $metadata ) {
207
		/** This filter is documented in functions.opengraph.php */
208
		$default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
209
210
		$metadata['image'] = array(
211
			'@type'  => 'ImageObject',
212
			'url'    => self::staticize_subdomain( $default_image ),
213
			'width'  => 200,
214
			'height' => 200,
215
		);
216
217
		return $metadata;
218
	}
219
220
	static function staticize_subdomain( $domain ) {
221
		// deal with WPCOM vs Jetpack
222
		if ( function_exists( 'staticize_subdomain' ) ) {
223
			return staticize_subdomain( $domain );
224
		} else {
225
			return Jetpack::staticize_subdomain( $domain );
226
		}
227
	}
228
229
	/**
230
	 * Extract image dimensions via wpcom/imagesize, only on WPCOM
231
	 *
232
	 * @since 6.2.0
233
	 *
234
	 * @param array $dimensions Dimensions.
235
	 * @return array Dimensions.
236
	 */
237
	static function extract_image_dimensions_from_getimagesize( $dimensions ) {
238
		if ( ! ( defined('WPCOM') && WPCOM && function_exists( 'require_lib' ) ) ) {
239
			return $dimensions;
240
		}
241
		require_lib( 'wpcom/imagesize' );
242
243
		foreach ( $dimensions as $url => $value ) {
244
			if ( is_array( $value ) ) {
245
				continue;
246
			}
247
			$result = wpcom_getimagesize( $url );
248
			if ( is_array( $result ) ) {
249
				$dimensions[ $url ] = array(
250
					'width'  => $result[0],
251
					'height' => $result[1],
252
				);
253
			}
254
		}
255
256
		return $dimensions;
257
	}
258
259
	static function amp_post_jetpack_og_tags() {
260
		Jetpack::init()->check_open_graph();
261
		if ( function_exists( 'jetpack_og_tags' ) ) {
262
			jetpack_og_tags();
263
		}
264
	}
265
266
	static function videopress_enable_freedom_mode( $options ) {
267
		if ( self::is_amp_request() ) {
268
			$options['freedom'] = true;
269
		}
270
		return $options;
271
	}
272
273
	static function render_sharing_html( $markup, $sharing_enabled ) {
274
		if ( ! self::is_amp_request() ) {
275
			return $markup;
276
		}
277
278
		remove_action( 'wp_footer', 'sharing_add_footer' );
279
		if ( empty( $sharing_enabled ) ) {
280
			return $markup;
281
		}
282
		$supported_services = array(
283
			'facebook'      => array(
284
				/** This filter is documented in modules/sharedaddy/sharing-sources.php */
285
				'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
286
			),
287
			'twitter'       => array(),
288
			'pinterest'     => array(),
289
			'whatsapp'      => array(),
290
			'google-plus-1' => array(
291
				'type' => 'gplus',
292
			),
293
			'tumblr'        => array(),
294
			'linkedin'      => array(),
295
		);
296
		$sharing_links = array();
297
		foreach ( $sharing_enabled['visible'] as $id => $service ) {
298
			if ( ! isset( $supported_services[ $id ] ) ) {
299
				$sharing_links[] = "<!-- not supported: $id -->";
300
				continue;
301
			}
302
			$args = array_merge(
303
				array(
304
					'type' => $id,
305
				),
306
				$supported_services[ $id ]
307
			);
308
			$sharing_link = '<amp-social-share';
309
			foreach ( $args as $key => $value ) {
310
				$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
311
			}
312
			$sharing_link .= '></amp-social-share>';
313
			$sharing_links[] = $sharing_link;
314
		}
315
		return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup );
316
	}
317
}
318
319
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
320
321
add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 );
322
323