Completed
Push — branch-8.4 ( 2b405d...95d4b8 )
by Jeremy
13:25 queued 06:42
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 //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
use Automattic\Jetpack\Sync\Functions;
4
5
/**
6
 * Manages compatibility with the amp-wp plugin
7
 *
8
 * @see https://github.com/Automattic/amp-wp
9
 */
10
class Jetpack_AMP_Support {
11
12
	/**
13
	 * Apply custom AMP changes on the front-end.
14
	 */
15
	public static function init() {
16
17
		// Add Stats tracking pixel on Jetpack sites when the Stats module is active.
18
		if (
19
			Jetpack::is_module_active( 'stats' )
20
			&& ! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
21
		) {
22
			add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
23
		}
24
25
		/**
26
		 * Remove this during the init hook in case users have enabled it during
27
		 * the after_setup_theme hook, which triggers before init.
28
		 */
29
		remove_theme_support( 'jetpack-devicepx' );
30
31
		// Sharing.
32
		add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
33
		add_filter( 'sharing_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_disable_sharedaddy_css' ) );
34
35
		// enforce freedom mode for videopress.
36
		add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
37
38
		// include Jetpack og tags when rendering native AMP head.
39
		add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
40
41
		// Post rendering changes for legacy AMP.
42
		add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
43
44
		// Disable Comment Likes.
45
		add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) );
46
47
		// Transitional mode AMP should not have comment likes.
48
		add_filter( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) );
49
50
		// Remove the Likes button from the admin bar.
51
		add_filter( 'jetpack_admin_bar_likes_enabled', array( 'Jetpack_AMP_Support', 'disable_likes_admin_bar' ) );
52
53
		// Add post template metadata for legacy AMP.
54
		add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
55
56
		// Filter photon image args for AMP Stories.
57
		add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 );
58
59
		// Sync the amp-options.
60
		add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_whitelist' ) );
61
	}
62
63
	/**
64
	 * Disable the Comment Likes feature on AMP views.
65
	 *
66
	 * @param bool $enabled Should comment likes be enabled.
67
	 */
68
	public static function comment_likes_enabled( $enabled ) {
69
		return $enabled && ! self::is_amp_request();
70
	}
71
72
	/**
73
	 * Apply custom AMP changes in wp-admin.
74
	 */
75
	public static function admin_init() {
76
		// disable Likes metabox for post editor if AMP canonical disabled.
77
		add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
78
	}
79
80
	/**
81
	 * Is the page in AMP 'canonical mode'.
82
	 * Used when themes register support for AMP with `add_theme_support( 'amp' )`.
83
	 *
84
	 * @return bool is_amp_canonical
85
	 */
86
	public static function is_amp_canonical() {
87
		return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
88
	}
89
90
	/**
91
	 * Does the page return AMP content.
92
	 *
93
	 * @return bool $is_amp_request Are we on am AMP view.
94
	 */
95
	public static function is_amp_request() {
96
		$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
97
98
		/**
99
		 * Returns true if the current request should return valid AMP content.
100
		 *
101
		 * @since 6.2.0
102
		 *
103
		 * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
104
		 */
105
		return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
106
	}
107
108
	/**
109
	 * Remove content filters added by Jetpack.
110
	 */
111
	public static function amp_disable_the_content_filters() {
112
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
113
			add_filter( 'videopress_show_2015_player', '__return_true' );
114
			add_filter( 'protected_embeds_use_form_post', '__return_false' );
115
			remove_filter( 'the_title', 'widont' );
116
		}
117
118
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
119
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
120
	}
121
122
	/**
123
	 * Do not add comment likes on AMP requests.
124
	 *
125
	 * @param string $content Post content.
126
	 */
127
	public static function disable_comment_likes_before_the_content( $content ) {
128
		if ( self::is_amp_request() ) {
129
			remove_filter( 'comment_text', 'comment_like_button', 12, 2 );
130
		}
131
		return $content;
132
	}
133
134
	/**
135
	 * Do not display the Likes' Admin bar on AMP requests.
136
	 *
137
	 * @param bool $is_admin_bar_button_visible Should the Like button be visible in the Admin bar. Default to true.
138
	 */
139
	public static function disable_likes_admin_bar( $is_admin_bar_button_visible ) {
140
		if ( self::is_amp_request() ) {
141
			return false;
142
		}
143
		return $is_admin_bar_button_visible;
144
	}
145
146
	/**
147
	 * Add Jetpack stats pixel.
148
	 *
149
	 * @since 6.2.1
150
	 */
151
	public static function add_stats_pixel() {
152
		if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
153
			return;
154
		}
155
		stats_render_amp_footer( stats_build_view_data() );
156
	}
157
158
	/**
159
	 * Add publisher and image metadata to legacy AMP post.
160
	 *
161
	 * @since 6.2.0
162
	 *
163
	 * @param array   $metadata Metadata array.
164
	 * @param WP_Post $post     Post.
165
	 * @return array Modified metadata array.
166
	 */
167
	public static function amp_post_template_metadata( $metadata, $post ) {
168
		if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
169
			$metadata = self::add_site_icon_to_metadata( $metadata );
170
		}
171
172
		if ( ! isset( $metadata['image'] ) ) {
173
			$metadata = self::add_image_to_metadata( $metadata, $post );
174
		}
175
176
		return $metadata;
177
	}
178
179
	/**
180
	 * Add blavatar to legacy AMP post metadata.
181
	 *
182
	 * @since 6.2.0
183
	 *
184
	 * @param array $metadata Metadata.
185
	 *
186
	 * @return array Metadata.
187
	 */
188
	private static function add_site_icon_to_metadata( $metadata ) {
189
		$size          = 60;
190
		$site_icon_url = class_exists( 'Automattic\\Jetpack\\Sync\\Functions' ) ? Functions::site_icon_url( $size ) : '';
191
192
		if ( function_exists( 'blavatar_domain' ) ) {
193
			$metadata['publisher']['logo'] = array(
194
				'@type'  => 'ImageObject',
195
				'url'    => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
196
				'width'  => $size,
197
				'height' => $size,
198
			);
199
		} elseif ( $site_icon_url ) {
200
			$metadata['publisher']['logo'] = array(
201
				'@type'  => 'ImageObject',
202
				'url'    => $site_icon_url,
203
				'width'  => $size,
204
				'height' => $size,
205
			);
206
		}
207
208
		return $metadata;
209
	}
210
211
	/**
212
	 * Add image to legacy AMP post metadata.
213
	 *
214
	 * @since 6.2.0
215
	 *
216
	 * @param array   $metadata Metadata.
217
	 * @param WP_Post $post     Post.
218
	 * @return array Metadata.
219
	 */
220
	private static function add_image_to_metadata( $metadata, $post ) {
221
		$image = Jetpack_PostImages::get_image(
222
			$post->ID,
223
			array(
224
				'fallback_to_avatars' => true,
225
				'avatar_size'         => 200,
226
				// AMP already attempts these.
227
				'from_thumbnail'      => false,
228
				'from_attachment'     => false,
229
			)
230
		);
231
232
		if ( empty( $image ) ) {
233
			return self::add_fallback_image_to_metadata( $metadata );
234
		}
235
236
		if ( ! isset( $image['src_width'] ) ) {
237
			$dimensions = self::extract_image_dimensions_from_getimagesize(
238
				array(
239
					$image['src'] => false,
240
				)
241
			);
242
243 View Code Duplication
			if ( false !== $dimensions[ $image['src'] ] ) {
244
				$image['src_width']  = $dimensions['width'];
245
				$image['src_height'] = $dimensions['height'];
246
			}
247
		}
248
249
		$metadata['image'] = array(
250
			'@type' => 'ImageObject',
251
			'url'   => $image['src'],
252
		);
253
		if ( isset( $image['src_width'] ) ) {
254
			$metadata['image']['width'] = $image['src_width'];
255
		}
256
		if ( isset( $image['src_width'] ) ) {
257
			$metadata['image']['height'] = $image['src_height'];
258
		}
259
260
		return $metadata;
261
	}
262
263
	/**
264
	 * Add fallback image to legacy AMP post metadata.
265
	 *
266
	 * @since 6.2.0
267
	 *
268
	 * @param array $metadata Metadata.
269
	 * @return array Metadata.
270
	 */
271
	private static function add_fallback_image_to_metadata( $metadata ) {
272
		/** This filter is documented in functions.opengraph.php */
273
		$default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
274
275
		$metadata['image'] = array(
276
			'@type'  => 'ImageObject',
277
			'url'    => self::staticize_subdomain( $default_image ),
278
			'width'  => 200,
279
			'height' => 200,
280
		);
281
282
		return $metadata;
283
	}
284
285
	/**
286
	 * Return static WordPress.com domain to use to load resources from WordPress.com.
287
	 *
288
	 * @param string $domain Asset URL.
289
	 */
290
	private static function staticize_subdomain( $domain ) {
291
		// deal with WPCOM vs Jetpack.
292
		if ( function_exists( 'staticize_subdomain' ) ) {
293
			return staticize_subdomain( $domain );
294
		} else {
295
			return Jetpack::staticize_subdomain( $domain );
296
		}
297
	}
298
299
	/**
300
	 * Extract image dimensions via wpcom/imagesize, only on WPCOM
301
	 *
302
	 * @since 6.2.0
303
	 *
304
	 * @param array $dimensions Dimensions.
305
	 * @return array Dimensions.
306
	 */
307
	private static function extract_image_dimensions_from_getimagesize( $dimensions ) {
308
		if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'require_lib' ) ) ) {
309
			return $dimensions;
310
		}
311
		require_lib( 'wpcom/imagesize' );
312
313
		foreach ( $dimensions as $url => $value ) {
314
			if ( is_array( $value ) ) {
315
				continue;
316
			}
317
			$result = wpcom_getimagesize( $url );
318
			if ( is_array( $result ) ) {
319
				$dimensions[ $url ] = array(
320
					'width'  => $result[0],
321
					'height' => $result[1],
322
				);
323
			}
324
		}
325
326
		return $dimensions;
327
	}
328
329
	/**
330
	 * Display Open Graph Meta tags in AMP views.
331
	 */
332
	public static function amp_post_jetpack_og_tags() {
333
		if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
334
			Jetpack::init()->check_open_graph();
335
		}
336
337
		if ( function_exists( 'jetpack_og_tags' ) ) {
338
			jetpack_og_tags();
339
		}
340
	}
341
342
	/**
343
	 * Force Freedom mode in VideoPress.
344
	 *
345
	 * @param array $options Array of VideoPress shortcode options.
346
	 */
347
	public static function videopress_enable_freedom_mode( $options ) {
348
		if ( self::is_amp_request() ) {
349
			$options['freedom'] = true;
350
		}
351
		return $options;
352
	}
353
354
	/**
355
	 * Display custom markup for the sharing buttons when in an AMP view.
356
	 *
357
	 * @param string $markup          Content markup of the Jetpack sharing links.
358
	 * @param array  $sharing_enabled Array of Sharing Services currently enabled.
359
	 */
360
	public static function render_sharing_html( $markup, $sharing_enabled ) {
361
		if ( ! self::is_amp_request() ) {
362
			return $markup;
363
		}
364
365
		remove_action( 'wp_footer', 'sharing_add_footer' );
366
		if ( empty( $sharing_enabled ) ) {
367
			return $markup;
368
		}
369
		$supported_services = array(
370
			'facebook'  => array(
371
				/** This filter is documented in modules/sharedaddy/sharing-sources.php */
372
				'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
373
			),
374
			'twitter'   => array(),
375
			'pinterest' => array(),
376
			'whatsapp'  => array(),
377
			'tumblr'    => array(),
378
			'linkedin'  => array(),
379
		);
380
		$sharing_links      = array();
381
		foreach ( $sharing_enabled['visible'] as $id => $service ) {
382
			if ( ! isset( $supported_services[ $id ] ) ) {
383
				$sharing_links[] = "<!-- not supported: $id -->";
384
				continue;
385
			}
386
			$args         = array_merge(
387
				array(
388
					'type' => $id,
389
				),
390
				$supported_services[ $id ]
391
			);
392
			$sharing_link = '<amp-social-share';
393
			foreach ( $args as $key => $value ) {
394
				$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
395
			}
396
			$sharing_link   .= '></amp-social-share>';
397
			$sharing_links[] = $sharing_link;
398
		}
399
400
		// Wrap AMP sharing buttons in container.
401
		$markup = preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup );
402
403
		// Remove any lingering share-end list items.
404
		$markup = str_replace( '<li class="share-end"></li>', '', $markup );
405
406
		return $markup;
407
	}
408
409
	/**
410
	 * Tells Jetpack not to enqueue CSS for share buttons.
411
	 *
412
	 * @param  bool $enqueue Whether or not to enqueue.
413
	 * @return bool          Whether or not to enqueue.
414
	 */
415
	public static function amp_disable_sharedaddy_css( $enqueue ) {
416
		if ( self::is_amp_request() ) {
417
			$enqueue = false;
418
		}
419
420
		return $enqueue;
421
	}
422
423
	/**
424
	 * Ensure proper Photon image dimensions for AMP Stories.
425
	 *
426
	 * @param array $args Array of Photon Arguments.
427
	 * @param array $details {
428
	 *     Array of image details.
429
	 *
430
	 *     @type string    $tag            Image tag (Image HTML output).
431
	 *     @type string    $src            Image URL.
432
	 *     @type string    $src_orig       Original Image URL.
433
	 *     @type int|false $width          Image width.
434
	 *     @type int|false $height         Image height.
435
	 *     @type int|false $width_orig     Original image width before constrained by content_width.
436
	 *     @type int|false $height_orig    Original Image height before constrained by content_width.
437
	 *     @type string    $transform_orig Original transform before constrained by content_width.
438
	 * }
439
	 * @return array Args.
440
	 */
441
	public static function filter_photon_post_image_args_for_stories( $args, $details ) {
442
		if ( ! is_singular( 'amp_story' ) ) {
443
			return $args;
444
		}
445
446
		// Percentage-based dimensions are not allowed in AMP, so this shouldn't happen, but short-circuit just in case.
447
		if ( false !== strpos( $details['width_orig'], '%' ) || false !== strpos( $details['height_orig'], '%' ) ) {
448
			return $args;
449
		}
450
451
		$max_height = 1280; // See image size with the slug \AMP_Story_Post_Type::MAX_IMAGE_SIZE_SLUG.
452
		$transform  = $details['transform_orig'];
453
		$width      = $details['width_orig'];
454
		$height     = $details['height_orig'];
455
456
		// If height is available, constrain to $max_height.
457
		if ( false !== $height ) {
458
			if ( $height > $max_height && false !== $height ) {
459
				$width  = ( $max_height * $width ) / $height;
460
				$height = $max_height;
461
			} elseif ( $height > $max_height ) {
462
				$height = $max_height;
463
			}
464
		}
465
466
		/*
467
		 * Set a height if none is found.
468
		 * If height is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing.
469
		 */
470
		if ( false === $height ) {
471
			$height = $max_height;
472
			if ( false !== $width ) {
473
				$transform = 'fit';
474
			}
475
		}
476
477
		// Build array of Photon args and expose to filter before passing to Photon URL function.
478
		$args = array();
479
480
		if ( false !== $width && false !== $height ) {
481
			$args[ $transform ] = $width . ',' . $height;
482
		} elseif ( false !== $width ) {
483
			$args['w'] = $width;
484
		} elseif ( false !== $height ) {
485
			$args['h'] = $height;
486
		}
487
488
		return $args;
489
	}
490
491
	/**
492
	 *  Adds amp-options to the list of options to sync, if AMP is available
493
	 *
494
	 * @param array $options_whitelist Whitelist of options to sync.
495
	 * @return array Updated options whitelist
496
	 */
497
	public static function filter_jetpack_options_whitelist( $options_whitelist ) {
498
		if ( function_exists( 'is_amp_endpoint' ) ) {
499
			$options_whitelist[] = 'amp-options';
500
		}
501
		return $options_whitelist;
502
	}
503
}
504
505
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
506
507
add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 );
508