Completed
Push — add/user-authentication ( 651ac4...684086 )
by
unknown
26:10 queued 18:04
created

Jetpack_AMP_Support::staticize_subdomain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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