Completed
Push — try/allow-embeds-in-amp ( 8ebcd5 )
by
unknown
153:05 queued 145:20
created

Jetpack_AMP_Support::amp_content_sanitizers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
rs 9.9666
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
		add_action( 'wp_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_enqueue_sharing_css' ) );
35
36
		// Sharing for Reader mode.
37
		if ( function_exists( 'jetpack_social_menu_include_svg_icons' ) ) {
38
			add_action( 'amp_post_template_footer', 'jetpack_social_menu_include_svg_icons' );
39
		}
40
		add_action( 'amp_post_template_css', array( 'Jetpack_AMP_Support', 'amp_reader_sharing_css' ), 10, 0 );
41
42
		// enforce freedom mode for videopress.
43
		add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
44
45
		// include Jetpack og tags when rendering native AMP head.
46
		add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
47
48
		// Post rendering changes for legacy AMP.
49
		add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
50
51
		// Disable Comment Likes.
52
		add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) );
53
54
		// Transitional mode AMP should not have comment likes.
55
		add_filter( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) );
56
57
		// Remove the Likes button from the admin bar.
58
		add_filter( 'jetpack_admin_bar_likes_enabled', array( 'Jetpack_AMP_Support', 'disable_likes_admin_bar' ) );
59
60
		// Add post template metadata for legacy AMP.
61
		add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
62
63
		// Filter photon image args for AMP Stories.
64
		add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 );
65
66
		// Sync the amp-options.
67
		add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_safelist' ) );
68
69
		// Allow spec-violating AMP content.
70
		add_filter( 'amp_content_sanitizers', array( 'Jetpack_AMP_Support', 'amp_content_sanitizers' ) );
71
	}
72
73
	/**
74
	 * Selectively disable AMP validation errors for some Jetpack content
75
	 *
76
	 * @param array $sanitizers The array of sanitizers, 'MyClassName' => [] // array of constructor params for class.
77
	 */
78
	public static function amp_content_sanitizers( $sanitizers ) {
79
		if ( ! apply_filters( 'jetpack_allow_unsanitary_amp_content', true ) ) { // @todo - make false by default before merging
80
			return $sanitizers;
81
		}
82
83
		require_once JETPACK__PLUGIN_DIR . '/3rd-party/class-jetpack-amp-feature-assets-sanitizer.php';
84
		$sanitizers['Jetpack_AMP_Feature_Assets_Sanitizer'] = array();
85
		return $sanitizers;
86
	}
87
88
	/**
89
	 * Disable the Comment Likes feature on AMP views.
90
	 *
91
	 * @param bool $enabled Should comment likes be enabled.
92
	 */
93
	public static function comment_likes_enabled( $enabled ) {
94
		return $enabled && ! self::is_amp_request();
95
	}
96
97
	/**
98
	 * Apply custom AMP changes in wp-admin.
99
	 */
100
	public static function admin_init() {
101
		// disable Likes metabox for post editor if AMP canonical disabled.
102
		add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
103
	}
104
105
	/**
106
	 * Is the page in AMP 'canonical mode'.
107
	 * Used when themes register support for AMP with `add_theme_support( 'amp' )`.
108
	 *
109
	 * @return bool is_amp_canonical
110
	 */
111
	public static function is_amp_canonical() {
112
		return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
113
	}
114
115
	/**
116
	 * Does the page return AMP content.
117
	 *
118
	 * @return bool $is_amp_request Are we on am AMP view.
119
	 */
120
	public static function is_amp_request() {
121
		$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
122
123
		/**
124
		 * Returns true if the current request should return valid AMP content.
125
		 *
126
		 * @since 6.2.0
127
		 *
128
		 * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
129
		 */
130
		return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
131
	}
132
133
	/**
134
	 * Remove content filters added by Jetpack.
135
	 */
136
	public static function amp_disable_the_content_filters() {
137
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
138
			add_filter( 'videopress_show_2015_player', '__return_true' );
139
			add_filter( 'protected_embeds_use_form_post', '__return_false' );
140
			remove_filter( 'the_title', 'widont' );
141
		}
142
143
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
144
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
145
	}
146
147
	/**
148
	 * Do not add comment likes on AMP requests.
149
	 *
150
	 * @param string $content Post content.
151
	 */
152
	public static function disable_comment_likes_before_the_content( $content ) {
153
		if ( self::is_amp_request() ) {
154
			remove_filter( 'comment_text', 'comment_like_button', 12, 2 );
155
		}
156
		return $content;
157
	}
158
159
	/**
160
	 * Do not display the Likes' Admin bar on AMP requests.
161
	 *
162
	 * @param bool $is_admin_bar_button_visible Should the Like button be visible in the Admin bar. Default to true.
163
	 */
164
	public static function disable_likes_admin_bar( $is_admin_bar_button_visible ) {
165
		if ( self::is_amp_request() ) {
166
			return false;
167
		}
168
		return $is_admin_bar_button_visible;
169
	}
170
171
	/**
172
	 * Add Jetpack stats pixel.
173
	 *
174
	 * @since 6.2.1
175
	 */
176
	public static function add_stats_pixel() {
177
		if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
178
			return;
179
		}
180
		stats_render_amp_footer( stats_build_view_data() );
181
	}
182
183
	/**
184
	 * Add publisher and image metadata to legacy AMP post.
185
	 *
186
	 * @since 6.2.0
187
	 *
188
	 * @param array   $metadata Metadata array.
189
	 * @param WP_Post $post     Post.
190
	 * @return array Modified metadata array.
191
	 */
192
	public static function amp_post_template_metadata( $metadata, $post ) {
193
		if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
194
			$metadata = self::add_site_icon_to_metadata( $metadata );
195
		}
196
197
		if ( ! isset( $metadata['image'] ) ) {
198
			$metadata = self::add_image_to_metadata( $metadata, $post );
199
		}
200
201
		return $metadata;
202
	}
203
204
	/**
205
	 * Add blavatar to legacy AMP post metadata.
206
	 *
207
	 * @since 6.2.0
208
	 *
209
	 * @param array $metadata Metadata.
210
	 *
211
	 * @return array Metadata.
212
	 */
213
	private static function add_site_icon_to_metadata( $metadata ) {
214
		$size          = 60;
215
		$site_icon_url = class_exists( 'Automattic\\Jetpack\\Sync\\Functions' ) ? Functions::site_icon_url( $size ) : '';
216
217
		if ( function_exists( 'blavatar_domain' ) ) {
218
			$metadata['publisher']['logo'] = array(
219
				'@type'  => 'ImageObject',
220
				'url'    => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
221
				'width'  => $size,
222
				'height' => $size,
223
			);
224
		} elseif ( $site_icon_url ) {
225
			$metadata['publisher']['logo'] = array(
226
				'@type'  => 'ImageObject',
227
				'url'    => $site_icon_url,
228
				'width'  => $size,
229
				'height' => $size,
230
			);
231
		}
232
233
		return $metadata;
234
	}
235
236
	/**
237
	 * Add image to legacy AMP post metadata.
238
	 *
239
	 * @since 6.2.0
240
	 *
241
	 * @param array   $metadata Metadata.
242
	 * @param WP_Post $post     Post.
243
	 * @return array Metadata.
244
	 */
245
	private static function add_image_to_metadata( $metadata, $post ) {
246
		$image = Jetpack_PostImages::get_image(
247
			$post->ID,
248
			array(
249
				'fallback_to_avatars' => true,
250
				'avatar_size'         => 200,
251
				// AMP already attempts these.
252
				'from_thumbnail'      => false,
253
				'from_attachment'     => false,
254
			)
255
		);
256
257
		if ( empty( $image ) ) {
258
			return self::add_fallback_image_to_metadata( $metadata );
259
		}
260
261
		if ( ! isset( $image['src_width'] ) ) {
262
			$dimensions = self::extract_image_dimensions_from_getimagesize(
263
				array(
264
					$image['src'] => false,
265
				)
266
			);
267
268 View Code Duplication
			if ( false !== $dimensions[ $image['src'] ] ) {
269
				$image['src_width']  = $dimensions['width'];
270
				$image['src_height'] = $dimensions['height'];
271
			}
272
		}
273
274
		$metadata['image'] = array(
275
			'@type' => 'ImageObject',
276
			'url'   => $image['src'],
277
		);
278
		if ( isset( $image['src_width'] ) ) {
279
			$metadata['image']['width'] = $image['src_width'];
280
		}
281
		if ( isset( $image['src_width'] ) ) {
282
			$metadata['image']['height'] = $image['src_height'];
283
		}
284
285
		return $metadata;
286
	}
287
288
	/**
289
	 * Add fallback image to legacy AMP post metadata.
290
	 *
291
	 * @since 6.2.0
292
	 *
293
	 * @param array $metadata Metadata.
294
	 * @return array Metadata.
295
	 */
296
	private static function add_fallback_image_to_metadata( $metadata ) {
297
		/** This filter is documented in functions.opengraph.php */
298
		$default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
299
300
		$metadata['image'] = array(
301
			'@type'  => 'ImageObject',
302
			'url'    => self::staticize_subdomain( $default_image ),
303
			'width'  => 200,
304
			'height' => 200,
305
		);
306
307
		return $metadata;
308
	}
309
310
	/**
311
	 * Return static WordPress.com domain to use to load resources from WordPress.com.
312
	 *
313
	 * @param string $domain Asset URL.
314
	 */
315
	private static function staticize_subdomain( $domain ) {
316
		// deal with WPCOM vs Jetpack.
317
		if ( function_exists( 'staticize_subdomain' ) ) {
318
			return staticize_subdomain( $domain );
319
		} else {
320
			return Jetpack::staticize_subdomain( $domain );
321
		}
322
	}
323
324
	/**
325
	 * Extract image dimensions via wpcom/imagesize, only on WPCOM
326
	 *
327
	 * @since 6.2.0
328
	 *
329
	 * @param array $dimensions Dimensions.
330
	 * @return array Dimensions.
331
	 */
332
	private static function extract_image_dimensions_from_getimagesize( $dimensions ) {
333
		if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'jetpack_require_lib' ) ) ) {
334
			return $dimensions;
335
		}
336
		jetpack_require_lib( 'wpcom/imagesize' );
337
338
		foreach ( $dimensions as $url => $value ) {
339
			if ( is_array( $value ) ) {
340
				continue;
341
			}
342
			$result = wpcom_getimagesize( $url );
343
			if ( is_array( $result ) ) {
344
				$dimensions[ $url ] = array(
345
					'width'  => $result[0],
346
					'height' => $result[1],
347
				);
348
			}
349
		}
350
351
		return $dimensions;
352
	}
353
354
	/**
355
	 * Display Open Graph Meta tags in AMP views.
356
	 */
357
	public static function amp_post_jetpack_og_tags() {
358
		if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
359
			Jetpack::init()->check_open_graph();
360
		}
361
362
		if ( function_exists( 'jetpack_og_tags' ) ) {
363
			jetpack_og_tags();
364
		}
365
	}
366
367
	/**
368
	 * Force Freedom mode in VideoPress.
369
	 *
370
	 * @param array $options Array of VideoPress shortcode options.
371
	 */
372
	public static function videopress_enable_freedom_mode( $options ) {
373
		if ( self::is_amp_request() ) {
374
			$options['freedom'] = true;
375
		}
376
		return $options;
377
	}
378
379
	/**
380
	 * Display custom markup for the sharing buttons when in an AMP view.
381
	 *
382
	 * @param string $markup          Content markup of the Jetpack sharing links.
383
	 * @param array  $sharing_enabled Array of Sharing Services currently enabled.
384
	 */
385
	public static function render_sharing_html( $markup, $sharing_enabled ) {
386
		global $post;
387
388
		if ( empty( $post ) ) {
389
			return '';
390
		}
391
392
		if ( ! self::is_amp_request() ) {
393
			return $markup;
394
		}
395
396
		remove_action( 'wp_footer', 'sharing_add_footer' );
397
		if ( empty( $sharing_enabled ) ) {
398
			return $markup;
399
		}
400
401
		$sharing_links = array();
402
		foreach ( $sharing_enabled['visible'] as $id => $service ) {
403
			$sharing_link = $service->get_amp_display( $post );
404
			if ( ! empty( $sharing_link ) ) {
405
				$sharing_links[] = $sharing_link;
406
			}
407
		}
408
409
		// Replace the existing unordered list with AMP sharing buttons.
410
		$markup = preg_replace( '#<ul>(.+)</ul>#', implode( '', $sharing_links ), $markup );
411
412
		// Remove any lingering share-end list items.
413
		$markup = str_replace( '<li class="share-end"></li>', '', $markup );
414
415
		return $markup;
416
	}
417
418
	/**
419
	 * Tells Jetpack not to enqueue CSS for share buttons.
420
	 *
421
	 * @param  bool $enqueue Whether or not to enqueue.
422
	 * @return bool          Whether or not to enqueue.
423
	 */
424
	public static function amp_disable_sharedaddy_css( $enqueue ) {
425
		if ( self::is_amp_request() ) {
426
			$enqueue = false;
427
		}
428
429
		return $enqueue;
430
	}
431
432
	/**
433
	 * Enqueues the AMP specific sharing styles for the sharing icons.
434
	 */
435
	public static function amp_enqueue_sharing_css() {
436
		if ( self::is_amp_request() ) {
437
			wp_enqueue_style( 'sharedaddy-amp', plugin_dir_url( dirname( __FILE__ ) ) . 'modules/sharedaddy/amp-sharing.css', array( 'social-logos' ), JETPACK__VERSION );
438
		}
439
	}
440
441
	/**
442
	 * For the AMP Reader mode template, include styles that we need.
443
	 */
444
	public static function amp_reader_sharing_css() {
445
		// If sharing is not enabled, we should not proceed to render the CSS.
446
		if ( ! defined( 'JETPACK_SOCIAL_LOGOS_DIR' ) || ! defined( 'WP_SHARING_PLUGIN_DIR' ) ) {
447
			return;
448
		}
449
450
		/*
451
		 * We'll need to output the full contents of the 2 files
452
		 * in the head on AMP views. We can't rely on regular enqueues here.
453
		 *
454
		 * phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
455
		 * phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
456
		 */
457
		echo file_get_contents( JETPACK_SOCIAL_LOGOS_DIR . 'social-logos.css' );
458
		echo file_get_contents( WP_SHARING_PLUGIN_DIR . 'amp-sharing.css' );
459
460
		/*
461
		 * phpcs:enable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
462
		 * phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
463
		 */
464
	}
465
466
	/**
467
	 * Ensure proper Photon image dimensions for AMP Stories.
468
	 *
469
	 * @param array $args Array of Photon Arguments.
470
	 * @param array $details {
471
	 *     Array of image details.
472
	 *
473
	 *     @type string    $tag            Image tag (Image HTML output).
474
	 *     @type string    $src            Image URL.
475
	 *     @type string    $src_orig       Original Image URL.
476
	 *     @type int|false $width          Image width.
477
	 *     @type int|false $height         Image height.
478
	 *     @type int|false $width_orig     Original image width before constrained by content_width.
479
	 *     @type int|false $height_orig    Original Image height before constrained by content_width.
480
	 *     @type string    $transform_orig Original transform before constrained by content_width.
481
	 * }
482
	 * @return array Args.
483
	 */
484
	public static function filter_photon_post_image_args_for_stories( $args, $details ) {
485
		if ( ! is_singular( 'amp_story' ) ) {
486
			return $args;
487
		}
488
489
		// Percentage-based dimensions are not allowed in AMP, so this shouldn't happen, but short-circuit just in case.
490
		if ( false !== strpos( $details['width_orig'], '%' ) || false !== strpos( $details['height_orig'], '%' ) ) {
491
			return $args;
492
		}
493
494
		$max_height = 1280; // See image size with the slug \AMP_Story_Post_Type::MAX_IMAGE_SIZE_SLUG.
495
		$transform  = $details['transform_orig'];
496
		$width      = $details['width_orig'];
497
		$height     = $details['height_orig'];
498
499
		// If height is available, constrain to $max_height.
500
		if ( false !== $height ) {
501
			if ( $height > $max_height && false !== $height ) {
502
				$width  = ( $max_height * $width ) / $height;
503
				$height = $max_height;
504
			} elseif ( $height > $max_height ) {
505
				$height = $max_height;
506
			}
507
		}
508
509
		/*
510
		 * Set a height if none is found.
511
		 * If height is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing.
512
		 */
513
		if ( false === $height ) {
514
			$height = $max_height;
515
			if ( false !== $width ) {
516
				$transform = 'fit';
517
			}
518
		}
519
520
		// Build array of Photon args and expose to filter before passing to Photon URL function.
521
		$args = array();
522
523
		if ( false !== $width && false !== $height ) {
524
			$args[ $transform ] = $width . ',' . $height;
525
		} elseif ( false !== $width ) {
526
			$args['w'] = $width;
527
		} elseif ( false !== $height ) {
528
			$args['h'] = $height;
529
		}
530
531
		return $args;
532
	}
533
534
	/**
535
	 *  Adds amp-options to the list of options to sync, if AMP is available
536
	 *
537
	 * @param array $options_safelist Safelist of options to sync.
538
	 *
539
	 * @return array Updated options safelist
540
	 */
541
	public static function filter_jetpack_options_safelist( $options_safelist ) {
542
		if ( function_exists( 'is_amp_endpoint' ) ) {
543
			$options_safelist[] = 'amp-options';
544
		}
545
		return $options_safelist;
546
	}
547
}
548
549
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
550
551
add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 );
552