Completed
Push — update/cli-debugger ( de978b )
by
unknown
10:49 queued 01:58
created

Jetpack_AMP_Support::has_amp_suffix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 4
rs 10
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 =
66
				defined( 'AMP__VERSION' )
67
			&&
68
				! is_admin() // this is necessary so that modules can still be enabled/disabled/configured as per normal via Jetpack admin
69
			&&
70
				function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists
71
			&&
72
				(
73
					amp_is_canonical()
74
				||
75
					isset( $_GET[ amp_get_slug() ] )
76
				||
77
					( version_compare( AMP__VERSION, '1.0', '<' ) && self::has_amp_suffix() ) // after AMP 1.0, the amp suffix will no longer be supported
78
				);
79
80
		/**
81
		 * Returns true if the current request should return valid AMP content.
82
		 *
83
		 * @since 6.2.0
84
		 *
85
		 * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
86
		 */
87
		return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
88
	}
89
90
	static function has_amp_suffix() {
91
		$request_path = wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
92
		return $request_path && preg_match( '#/amp/?$#i', $request_path );
93
	}
94
95
	static function filter_available_widgets( $widgets ) {
96
		if ( self::is_amp_request() ) {
97
			$widgets = array_filter( $widgets, array( 'Jetpack_AMP_Support', 'is_supported_widget' ) );
98
		}
99
100
		return $widgets;
101
	}
102
103
	static function is_supported_widget( $widget_path ) {
104
		return substr( $widget_path, -14 ) !== '/milestone.php';
105
	}
106
107
	static function amp_disable_the_content_filters() {
108
		if ( defined( 'WPCOM') && WPCOM ) {
109
			add_filter( 'videopress_show_2015_player', '__return_true' );
110
			add_filter( 'protected_embeds_use_form_post', '__return_false' );
111
			remove_filter( 'the_title', 'widont' );
112
		}
113
114
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
115
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
116
	}
117
118
	/**
119
	 * Add publisher and image metadata to legacy AMP post.
120
	 *
121
	 * @since 6.2.0
122
	 *
123
	 * @param array   $metadata Metadata array.
124
	 * @param WP_Post $post     Post.
125
	 * @return array Modified metadata array.
126
	 */
127
	static function amp_post_template_metadata( $metadata, $post ) {
128
		if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
129
			$metadata = self::add_site_icon_to_metadata( $metadata );
130
		}
131
132
		if ( ! isset( $metadata['image'] ) ) {
133
			$metadata = self::add_image_to_metadata( $metadata, $post );
134
		}
135
136
		return $metadata;
137
	}
138
139
	/**
140
	 * Add blavatar to legacy AMP post metadata.
141
	 *
142
	 * @since 6.2.0
143
	 *
144
	 * @param array $metadata Metadata.
145
	 * @return array Metadata.
146
	 */
147
	static function add_site_icon_to_metadata( $metadata ) {
148
		$size = 60;
149
150
		if ( function_exists( 'blavatar_domain' ) ) {
151
			$metadata['publisher']['logo'] = array(
152
				'@type'  => 'ImageObject',
153
				'url'    => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
154
				'width'  => $size,
155
				'height' => $size,
156
			);
157
		} else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) {
158
			$metadata['publisher']['logo'] = array(
159
				'@type'  => 'ImageObject',
160
				'url'    => $site_icon_url,
161
				'width'  => $size,
162
				'height' => $size,
163
			);
164
		}
165
166
		return $metadata;
167
	}
168
169
	/**
170
	 * Add image to legacy AMP post metadata.
171
	 *
172
	 * @since 6.2.0
173
	 *
174
	 * @param array   $metadata Metadata.
175
	 * @param WP_Post $post     Post.
176
	 * @return array Metadata.
177
	 */
178
	static function add_image_to_metadata( $metadata, $post ) {
179
		$image = Jetpack_PostImages::get_image( $post->ID, array(
180
			'fallback_to_avatars' => true,
181
			'avatar_size'         => 200,
182
			// AMP already attempts these.
183
			'from_thumbnail'      => false,
184
			'from_attachment'     => false,
185
		) );
186
187
		if ( empty( $image ) ) {
188
			return self::add_fallback_image_to_metadata( $metadata );
189
		}
190
191
		if ( ! isset( $image['src_width'] ) ) {
192
			$dimensions = self::extract_image_dimensions_from_getimagesize( array(
193
				$image['src'] => false,
194
			) );
195
196 View Code Duplication
			if ( false !== $dimensions[ $image['src'] ] ) {
197
				$image['src_width']  = $dimensions['width'];
198
				$image['src_height'] = $dimensions['height'];
199
			}
200
		}
201
202
		$metadata['image'] = array(
203
			'@type'  => 'ImageObject',
204
			'url'    => $image['src'],
205
			'width'  => $image['src_width'],
206
			'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
	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
	static function staticize_subdomain( $domain ) {
235
		// deal with WPCOM vs Jetpack
236
		if ( function_exists( 'staticize_subdomain' ) ) {
237
			return staticize_subdomain( $domain );
238
		} else {
239
			return Jetpack::staticize_subdomain( $domain );
240
		}
241
	}
242
243
	/**
244
	 * Extract image dimensions via wpcom/imagesize, only on WPCOM
245
	 *
246
	 * @since 6.2.0
247
	 *
248
	 * @param array $dimensions Dimensions.
249
	 * @return array Dimensions.
250
	 */
251
	static function extract_image_dimensions_from_getimagesize( $dimensions ) {
252
		if ( ! ( defined('WPCOM') && WPCOM && function_exists( 'require_lib' ) ) ) {
253
			return $dimensions;
254
		}
255
		require_lib( 'wpcom/imagesize' );
256
257
		foreach ( $dimensions as $url => $value ) {
258
			if ( is_array( $value ) ) {
259
				continue;
260
			}
261
			$result = wpcom_getimagesize( $url );
262
			if ( is_array( $result ) ) {
263
				$dimensions[ $url ] = array(
264
					'width'  => $result[0],
265
					'height' => $result[1],
266
				);
267
			}
268
		}
269
270
		return $dimensions;
271
	}
272
273
	static function amp_post_jetpack_og_tags() {
274
		Jetpack::init()->check_open_graph();
275
		if ( function_exists( 'jetpack_og_tags' ) ) {
276
			jetpack_og_tags();
277
		}
278
	}
279
280
	static function videopress_enable_freedom_mode( $options ) {
281
		$options['freedom'] = true;
282
		return $options;
283
	}
284
285
	static function render_sharing_html( $markup, $sharing_enabled ) {
286
		remove_action( 'wp_footer', 'sharing_add_footer' );
287
		if ( empty( $sharing_enabled ) ) {
288
			return $markup;
289
		}
290
		$supported_services = array(
291
			'facebook'      => array(
292
				/** This filter is documented in modules/sharedaddy/sharing-sources.php */
293
				'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
294
			),
295
			'twitter'       => array(),
296
			'pinterest'     => array(),
297
			'whatsapp'      => array(),
298
			'google-plus-1' => array(
299
				'type' => 'gplus',
300
			),
301
			'tumblr'        => array(),
302
			'linkedin'      => array(),
303
		);
304
		$sharing_links = array();
305
		foreach ( $sharing_enabled['visible'] as $id => $service ) {
306
			if ( ! isset( $supported_services[ $id ] ) ) {
307
				$sharing_links[] = "<!-- not supported: $id -->";
308
				continue;
309
			}
310
			$args = array_merge(
311
				array(
312
					'type' => $id,
313
				),
314
				$supported_services[ $id ]
315
			);
316
			$sharing_link = '<amp-social-share';
317
			foreach ( $args as $key => $value ) {
318
				$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
319
			}
320
			$sharing_link .= '></amp-social-share>';
321
			$sharing_links[] = $sharing_link;
322
		}
323
		return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup );
324
	}
325
}
326
327
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
328
329
add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 );
330
331
// this is necessary since for better or worse Jetpack modules and widget files are loaded during plugins_loaded, which means we must
332
// take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module,
333
// or preventing it from loading in the first place (better for performance)
334
add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_widgets' ), 1 );
335