Completed
Push — update/remove-manage-module ( 76347c...953431 )
by
unknown
247:47 queued 240:03
created

Jetpack_AMP_Support   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 288
Duplicated Lines 1.39 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 4
loc 288
rs 8.8
c 0
b 0
f 0
wmc 45
lcom 1
cbo 3

15 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 22 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 add_stats_pixel() 0 6 2
A amp_post_template_metadata() 0 11 4
A add_site_icon_to_metadata() 0 21 3
B add_image_to_metadata() 4 37 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 6 2
A videopress_enable_freedom_mode() 0 6 2
B render_sharing_html() 0 44 6

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
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
12
		// enable stats
13
		if ( Jetpack::is_module_active( 'stats' ) ) {
14
			add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
15
		}
16
17
		// Sharing.
18
		add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
19
20
		// enforce freedom mode for videopress
21
		add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
22
23
		// include Jetpack og tags when rendering native AMP head
24
		add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
25
26
		// Post rendering changes for legacy AMP
27
		add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
28
29
		// Add post template metadata for legacy AMP
30
		add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
31
	}
32
33
	static function admin_init() {
34
		// disable Likes metabox for post editor if AMP canonical disabled
35
		add_filter( 'post_flair_disable',  array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
36
	}
37
38
	static function is_amp_canonical() {
39
		return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
40
	}
41
42
	static function is_amp_request() {
43
		$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
44
45
		/**
46
		 * Returns true if the current request should return valid AMP content.
47
		 *
48
		 * @since 6.2.0
49
		 *
50
		 * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
51
		 */
52
		return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
53
	}
54
55
	static function amp_disable_the_content_filters() {
56
		if ( defined( 'WPCOM') && WPCOM ) {
57
			add_filter( 'videopress_show_2015_player', '__return_true' );
58
			add_filter( 'protected_embeds_use_form_post', '__return_false' );
59
			remove_filter( 'the_title', 'widont' );
60
		}
61
62
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
63
		remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
64
	}
65
66
	/**
67
	 * Add Jetpack stats pixel.
68
	 *
69
	 * @since 6.2.1
70
	 */
71
	static function add_stats_pixel() {
72
		if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
73
			return;
74
		}
75
		stats_render_amp_footer( stats_build_view_data() );
76
	}
77
78
	/**
79
	 * Add publisher and image metadata to legacy AMP post.
80
	 *
81
	 * @since 6.2.0
82
	 *
83
	 * @param array   $metadata Metadata array.
84
	 * @param WP_Post $post     Post.
85
	 * @return array Modified metadata array.
86
	 */
87
	static function amp_post_template_metadata( $metadata, $post ) {
88
		if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
89
			$metadata = self::add_site_icon_to_metadata( $metadata );
90
		}
91
92
		if ( ! isset( $metadata['image'] ) ) {
93
			$metadata = self::add_image_to_metadata( $metadata, $post );
94
		}
95
96
		return $metadata;
97
	}
98
99
	/**
100
	 * Add blavatar to legacy AMP post metadata.
101
	 *
102
	 * @since 6.2.0
103
	 *
104
	 * @param array $metadata Metadata.
105
	 * @return array Metadata.
106
	 */
107
	static function add_site_icon_to_metadata( $metadata ) {
108
		$size = 60;
109
110
		if ( function_exists( 'blavatar_domain' ) ) {
111
			$metadata['publisher']['logo'] = array(
112
				'@type'  => 'ImageObject',
113
				'url'    => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
114
				'width'  => $size,
115
				'height' => $size,
116
			);
117
		} else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) {
118
			$metadata['publisher']['logo'] = array(
119
				'@type'  => 'ImageObject',
120
				'url'    => $site_icon_url,
121
				'width'  => $size,
122
				'height' => $size,
123
			);
124
		}
125
126
		return $metadata;
127
	}
128
129
	/**
130
	 * Add image to legacy AMP post metadata.
131
	 *
132
	 * @since 6.2.0
133
	 *
134
	 * @param array   $metadata Metadata.
135
	 * @param WP_Post $post     Post.
136
	 * @return array Metadata.
137
	 */
138
	static function add_image_to_metadata( $metadata, $post ) {
139
		$image = Jetpack_PostImages::get_image( $post->ID, array(
140
			'fallback_to_avatars' => true,
141
			'avatar_size'         => 200,
142
			// AMP already attempts these.
143
			'from_thumbnail'      => false,
144
			'from_attachment'     => false,
145
		) );
146
147
		if ( empty( $image ) ) {
148
			return self::add_fallback_image_to_metadata( $metadata );
149
		}
150
151
		if ( ! isset( $image['src_width'] ) ) {
152
			$dimensions = self::extract_image_dimensions_from_getimagesize( array(
153
				$image['src'] => false,
154
			) );
155
156 View Code Duplication
			if ( false !== $dimensions[ $image['src'] ] ) {
157
				$image['src_width']  = $dimensions['width'];
158
				$image['src_height'] = $dimensions['height'];
159
			}
160
		}
161
162
		$metadata['image'] = array(
163
			'@type' => 'ImageObject',
164
			'url'   => $image['src'],
165
		);
166
		if ( isset( $image['src_width'] ) ) {
167
			$metadata['image']['width'] = $image['src_width'];
168
		}
169
		if ( isset( $image['src_width'] ) ) {
170
			$metadata['image']['height'] = $image['src_height'];
171
		}
172
173
		return $metadata;
174
	}
175
176
	/**
177
	 * Add fallback image to legacy AMP post metadata.
178
	 *
179
	 * @since 6.2.0
180
	 *
181
	 * @param array $metadata Metadata.
182
	 * @return array Metadata.
183
	 */
184
	static function add_fallback_image_to_metadata( $metadata ) {
185
		/** This filter is documented in functions.opengraph.php */
186
		$default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
187
188
		$metadata['image'] = array(
189
			'@type'  => 'ImageObject',
190
			'url'    => self::staticize_subdomain( $default_image ),
191
			'width'  => 200,
192
			'height' => 200,
193
		);
194
195
		return $metadata;
196
	}
197
198
	static function staticize_subdomain( $domain ) {
199
		// deal with WPCOM vs Jetpack
200
		if ( function_exists( 'staticize_subdomain' ) ) {
201
			return staticize_subdomain( $domain );
202
		} else {
203
			return Jetpack::staticize_subdomain( $domain );
204
		}
205
	}
206
207
	/**
208
	 * Extract image dimensions via wpcom/imagesize, only on WPCOM
209
	 *
210
	 * @since 6.2.0
211
	 *
212
	 * @param array $dimensions Dimensions.
213
	 * @return array Dimensions.
214
	 */
215
	static function extract_image_dimensions_from_getimagesize( $dimensions ) {
216
		if ( ! ( defined('WPCOM') && WPCOM && function_exists( 'require_lib' ) ) ) {
217
			return $dimensions;
218
		}
219
		require_lib( 'wpcom/imagesize' );
220
221
		foreach ( $dimensions as $url => $value ) {
222
			if ( is_array( $value ) ) {
223
				continue;
224
			}
225
			$result = wpcom_getimagesize( $url );
226
			if ( is_array( $result ) ) {
227
				$dimensions[ $url ] = array(
228
					'width'  => $result[0],
229
					'height' => $result[1],
230
				);
231
			}
232
		}
233
234
		return $dimensions;
235
	}
236
237
	static function amp_post_jetpack_og_tags() {
238
		Jetpack::init()->check_open_graph();
239
		if ( function_exists( 'jetpack_og_tags' ) ) {
240
			jetpack_og_tags();
241
		}
242
	}
243
244
	static function videopress_enable_freedom_mode( $options ) {
245
		if ( self::is_amp_request() ) {
246
			$options['freedom'] = true;
247
		}
248
		return $options;
249
	}
250
251
	static function render_sharing_html( $markup, $sharing_enabled ) {
252
		if ( ! self::is_amp_request() ) {
253
			return $markup;
254
		}
255
256
		remove_action( 'wp_footer', 'sharing_add_footer' );
257
		if ( empty( $sharing_enabled ) ) {
258
			return $markup;
259
		}
260
		$supported_services = array(
261
			'facebook'      => array(
262
				/** This filter is documented in modules/sharedaddy/sharing-sources.php */
263
				'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
264
			),
265
			'twitter'       => array(),
266
			'pinterest'     => array(),
267
			'whatsapp'      => array(),
268
			'google-plus-1' => array(
269
				'type' => 'gplus',
270
			),
271
			'tumblr'        => array(),
272
			'linkedin'      => array(),
273
		);
274
		$sharing_links = array();
275
		foreach ( $sharing_enabled['visible'] as $id => $service ) {
276
			if ( ! isset( $supported_services[ $id ] ) ) {
277
				$sharing_links[] = "<!-- not supported: $id -->";
278
				continue;
279
			}
280
			$args = array_merge(
281
				array(
282
					'type' => $id,
283
				),
284
				$supported_services[ $id ]
285
			);
286
			$sharing_link = '<amp-social-share';
287
			foreach ( $args as $key => $value ) {
288
				$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
289
			}
290
			$sharing_link .= '></amp-social-share>';
291
			$sharing_links[] = $sharing_link;
292
		}
293
		return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup );
294
	}
295
}
296
297
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
298
299
add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 );
300
301