Completed
Push — update/akismet-dashitem ( 001fc7...3a6c16 )
by
unknown
08:51 queued 01:45
created

Jetpack_AMP_Support::add_stats_pixel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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