Completed
Push — fix/extend-amp-sharing-icons ( b03195 )
by
unknown
28:14 queued 21:17
created

Jetpack_AMP_Support   F

Complexity

Total Complexity 87

Size/Duplication

Total Lines 558
Duplicated Lines 3.41 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 19
loc 558
rs 2
c 0
b 0
f 0
wmc 87
lcom 2
cbo 3

22 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 46 4
A show_admin_bar() 0 3 2
A comment_likes_enabled() 0 3 2
A admin_init() 0 4 1
A is_amp_canonical() 0 3 2
A is_amp_request() 0 12 2
A amp_disable_the_content_filters() 0 10 3
A disable_comment_likes_before_the_content() 0 6 2
A add_stats_pixel() 0 6 2
A amp_post_template_metadata() 0 11 4
A add_site_icon_to_metadata() 0 22 4
B add_image_to_metadata() 4 42 6
A add_fallback_image_to_metadata() 0 13 1
A staticize_subdomain() 0 8 2
B extract_image_dimensions_from_getimagesize() 0 21 7
A amp_post_jetpack_og_tags() 0 9 4
A videopress_enable_freedom_mode() 0 6 2
D render_sharing_html() 15 106 17
A amp_disable_sharedaddy_css() 0 7 2
A amp_enqueue_sharing_css() 0 6 2
C filter_photon_post_image_args_for_stories() 0 49 14
A filter_jetpack_options_whitelist() 0 6 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_AMP_Support often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_AMP_Support, and based on these observations, apply Extract Interface, too.

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