Completed
Push — add/changelog-62 ( 9977bb...d371c2 )
by
unknown
10:21
created

Jetpack_AMP_Support::is_amp_request()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 5
nop 0
dl 0
loc 19
rs 9.2
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
		if ( ! self::is_amp_request() ) {
12
			return;
13
		}
14
15
		// carousel
16
		add_filter( 'jp_carousel_maybe_disable', '__return_true' );
17
18
		// sharing
19
		add_filter( 'sharing_enqueue_scripts', '__return_false' );
20
		add_filter( 'jetpack_sharing_counts', '__return_false' );
21
		add_filter( 'sharing_js', '__return_false' );
22
		add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
23
24
		// disable lazy images
25
		add_filter( 'lazyload_is_enabled', '__return_false' );
26
27
		// disable imploding CSS
28
		add_filter( 'jetpack_implode_frontend_css', '__return_false' );
29
30
		// enforce freedom mode for videopress
31
		add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
32
33
		// include Jetpack og tags when rendering native AMP head
34
		add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
35
36
		// Post rendering changes for legacy AMP
37
		add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
38
39
		// Add post template metadata for legacy AMP
40
		add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
41
	}
42
43
	static function admin_init() {
44
		// disable Likes metabox for post editor if AMP canonical disabled
45
		add_filter( 'post_flair_disable',  array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
46
	}
47
48
	static function init_filter_jetpack_widgets() {
49
		if ( ! self::is_amp_request() ) {
50
			return;
51
		}
52
53
		// widgets
54
		add_filter( 'jetpack_widgets_to_include', array( 'Jetpack_AMP_Support', 'filter_available_widgets' ) );
55
	}
56
57
	static function is_amp_canonical() {
58
		return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
59
	}
60
61
	static function is_amp_request() {
62
		// can't use is_amp_endpoint() since it's not ready early enough in init.
63
		// is_amp_endpoint() implementation calls is_feed, which bails with a notice if plugins_loaded isn't finished
64
		// "Conditional query tags do not work before the query is run"
65
		$is_amp_request = ! is_admin() // this is necessary so that modules can still be enabled/disabled/configured as per normal via Jetpack admin
66
			&&
67
				function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists
68
			&&
69
				( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) );
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
	static function filter_available_widgets( $widgets ) {
82
		if ( self::is_amp_request() ) {
83
			$widgets = array_filter( $widgets, array( 'Jetpack_AMP_Support', 'is_supported_widget' ) );
84
		}
85
86
		return $widgets;
87
	}
88
89
	static function is_supported_widget( $widget_path ) {
90
		return substr( $widget_path, -14 ) !== '/milestone.php';
91
	}
92
93
	static function amp_disable_the_content_filters() {
94
		if ( defined( 'WPCOM') && WPCOM ) {
95
			add_filter( 'videopress_show_2015_player', '__return_true' );
96
			add_filter( 'protected_embeds_use_form_post', '__return_false' );
97
			remove_filter( 'the_title', 'widont' );
98
		}
99
100
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
101
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
102
	}
103
104
	/**
105
	 * Add publisher and image metadata to legacy AMP post.
106
	 *
107
	 * @since 6.2.0
108
	 *
109
	 * @param array   $metadata Metadata array.
110
	 * @param WP_Post $post     Post.
111
	 * @return array Modified metadata array.
112
	 */
113
	static function amp_post_template_metadata( $metadata, $post ) {
114
		if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
115
			$metadata = self::add_site_icon_to_metadata( $metadata );
116
		}
117
118
		if ( ! isset( $metadata['image'] ) ) {
119
			$metadata = self::add_image_to_metadata( $metadata, $post );
120
		}
121
122
		return $metadata;
123
	}
124
125
	/**
126
	 * Add blavatar to legacy AMP post metadata.
127
	 *
128
	 * @since 6.2.0
129
	 *
130
	 * @param array $metadata Metadata.
131
	 * @return array Metadata.
132
	 */
133
	static function add_site_icon_to_metadata( $metadata ) {
134
		$size = 60;
135
136
		if ( function_exists( 'blavatar_domain' ) ) {
137
			$metadata['publisher']['logo'] = array(
138
				'@type'  => 'ImageObject',
139
				'url'    => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
140
				'width'  => $size,
141
				'height' => $size,
142
			);
143
		} else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) {
144
			$metadata['publisher']['logo'] = array(
145
				'@type'  => 'ImageObject',
146
				'url'    => $site_icon_url,
147
				'width'  => $size,
148
				'height' => $size,
149
			);
150
		}
151
152
		return $metadata;
153
	}
154
155
	/**
156
	 * Add image to legacy AMP post metadata.
157
	 *
158
	 * @since 6.2.0
159
	 *
160
	 * @param array   $metadata Metadata.
161
	 * @param WP_Post $post     Post.
162
	 * @return array Metadata.
163
	 */
164
	static function add_image_to_metadata( $metadata, $post ) {
165
		$image = Jetpack_PostImages::get_image( $post->ID, array(
166
			'fallback_to_avatars' => true,
167
			'avatar_size'         => 200,
168
			// AMP already attempts these.
169
			'from_thumbnail'      => false,
170
			'from_attachment'     => false,
171
		) );
172
173
		if ( empty( $image ) ) {
174
			return self::add_fallback_image_to_metadata( $metadata );
175
		}
176
177
		if ( ! isset( $image['src_width'] ) ) {
178
			$dimensions = self::extract_image_dimensions_from_getimagesize( array(
179
				$image['src'] => false,
180
			) );
181
182 View Code Duplication
			if ( false !== $dimensions[ $image['src'] ] ) {
183
				$image['src_width']  = $dimensions['width'];
184
				$image['src_height'] = $dimensions['height'];
185
			}
186
		}
187
188
		$metadata['image'] = array(
189
			'@type'  => 'ImageObject',
190
			'url'    => $image['src'],
191
			'width'  => $image['src_width'],
192
			'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
		$options['freedom'] = true;
268
		return $options;
269
	}
270
271
	static function render_sharing_html( $markup, $sharing_enabled ) {
272
		remove_action( 'wp_footer', 'sharing_add_footer' );
273
		if ( empty( $sharing_enabled ) ) {
274
			return $markup;
275
		}
276
		$supported_services = array(
277
			'facebook'      => array(
278
				/** This filter is documented in modules/sharedaddy/sharing-sources.php */
279
				'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
280
			),
281
			'twitter'       => array(),
282
			'pinterest'     => array(),
283
			'whatsapp'      => array(),
284
			'google-plus-1' => array(
285
				'type' => 'gplus',
286
			),
287
			'tumblr'        => array(),
288
			'linkedin'      => array(),
289
		);
290
		$sharing_links = array();
291
		foreach ( $sharing_enabled['visible'] as $id => $service ) {
292
			if ( ! isset( $supported_services[ $id ] ) ) {
293
				$sharing_links[] = "<!-- not supported: $id -->";
294
				continue;
295
			}
296
			$args = array_merge(
297
				array(
298
					'type' => $id,
299
				),
300
				$supported_services[ $id ]
301
			);
302
			$sharing_link = '<amp-social-share';
303
			foreach ( $args as $key => $value ) {
304
				$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
305
			}
306
			$sharing_link .= '></amp-social-share>';
307
			$sharing_links[] = $sharing_link;
308
		}
309
		return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup );
310
	}
311
}
312
313
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
314
315
add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 );
316
317
// this is necessary since for better or worse Jetpack modules and widget files are loaded during plugins_loaded, which means we must
318
// take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module,
319
// or preventing it from loading in the first place (better for performance)
320
add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_widgets' ), 1 );
321