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 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 |
||
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 | |||
35 | // enforce freedom mode for videopress. |
||
36 | add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) ); |
||
37 | |||
38 | // include Jetpack og tags when rendering native AMP head. |
||
39 | add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) ); |
||
40 | |||
41 | // Post rendering changes for legacy AMP. |
||
42 | add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) ); |
||
43 | |||
44 | // Transitional mode AMP should not have comment likes. |
||
45 | add_action( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) ); |
||
46 | |||
47 | // Add post template metadata for legacy AMP. |
||
48 | add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 ); |
||
49 | |||
50 | // Filter photon image args for AMP Stories. |
||
51 | add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 ); |
||
52 | |||
53 | // Sync the amp-options. |
||
54 | add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_whitelist' ) ); |
||
55 | |||
56 | // Show admin bar. |
||
57 | add_filter( 'show_admin_bar', array( 'Jetpack_AMP_Support', 'show_admin_bar' ) ); |
||
58 | add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) ); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Disable the admin bar on AMP views. |
||
63 | * |
||
64 | * @param Whether bool $show the admin bar should be shown. Default false. |
||
65 | */ |
||
66 | public static function show_admin_bar( $show ) { |
||
67 | return $show && ! self::is_amp_request(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Disable the Comment Likes feature on AMP views. |
||
72 | * |
||
73 | * @param bool $enabled Should comment likes be enabled. |
||
74 | */ |
||
75 | public static function comment_likes_enabled( $enabled ) { |
||
76 | return $enabled && ! self::is_amp_request(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Apply custom AMP changes in wp-admin. |
||
81 | */ |
||
82 | public static function admin_init() { |
||
83 | // disable Likes metabox for post editor if AMP canonical disabled. |
||
84 | add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Is the page in AMP 'canonical mode'. |
||
89 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`. |
||
90 | * |
||
91 | * @return bool is_amp_canonical |
||
92 | */ |
||
93 | public static function is_amp_canonical() { |
||
94 | return function_exists( 'amp_is_canonical' ) && amp_is_canonical(); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Does the page return AMP content. |
||
99 | * |
||
100 | * @return bool $is_amp_request Are we on am AMP view. |
||
101 | */ |
||
102 | public static function is_amp_request() { |
||
103 | $is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ); |
||
104 | |||
105 | /** |
||
106 | * Returns true if the current request should return valid AMP content. |
||
107 | * |
||
108 | * @since 6.2.0 |
||
109 | * |
||
110 | * @param boolean $is_amp_request Is this request supposed to return valid AMP content? |
||
111 | */ |
||
112 | return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Remove content filters added by Jetpack. |
||
117 | */ |
||
118 | public static function amp_disable_the_content_filters() { |
||
128 | |||
129 | /** |
||
130 | * Do not add comment likes on AMP requests. |
||
131 | * |
||
132 | * @param string $content Post content. |
||
133 | */ |
||
134 | public static function disable_comment_likes_before_the_content( $content ) { |
||
140 | |||
141 | /** |
||
142 | * Add Jetpack stats pixel. |
||
143 | * |
||
144 | * @since 6.2.1 |
||
145 | */ |
||
146 | public static function add_stats_pixel() { |
||
152 | |||
153 | /** |
||
154 | * Add publisher and image metadata to legacy AMP post. |
||
155 | * |
||
156 | * @since 6.2.0 |
||
157 | * |
||
158 | * @param array $metadata Metadata array. |
||
159 | * @param WP_Post $post Post. |
||
160 | * @return array Modified metadata array. |
||
161 | */ |
||
162 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
173 | |||
174 | /** |
||
175 | * Add blavatar to legacy AMP post metadata. |
||
176 | * |
||
177 | * @since 6.2.0 |
||
178 | * |
||
179 | * @param array $metadata Metadata. |
||
180 | * |
||
181 | * @return array Metadata. |
||
182 | */ |
||
183 | private static function add_site_icon_to_metadata( $metadata ) { |
||
205 | |||
206 | /** |
||
207 | * Add image to legacy AMP post metadata. |
||
208 | * |
||
209 | * @since 6.2.0 |
||
210 | * |
||
211 | * @param array $metadata Metadata. |
||
212 | * @param WP_Post $post Post. |
||
213 | * @return array Metadata. |
||
214 | */ |
||
215 | private static function add_image_to_metadata( $metadata, $post ) { |
||
257 | |||
258 | /** |
||
259 | * Add fallback image to legacy AMP post metadata. |
||
260 | * |
||
261 | * @since 6.2.0 |
||
262 | * |
||
263 | * @param array $metadata Metadata. |
||
264 | * @return array Metadata. |
||
265 | */ |
||
266 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
279 | |||
280 | /** |
||
281 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
282 | * |
||
283 | * @param string $domain Asset URL. |
||
284 | */ |
||
285 | private static function staticize_subdomain( $domain ) { |
||
293 | |||
294 | /** |
||
295 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
296 | * |
||
297 | * @since 6.2.0 |
||
298 | * |
||
299 | * @param array $dimensions Dimensions. |
||
300 | * @return array Dimensions. |
||
301 | */ |
||
302 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
323 | |||
324 | /** |
||
325 | * Display Open Graph Meta tags in AMP views. |
||
326 | */ |
||
327 | public static function amp_post_jetpack_og_tags() { |
||
336 | |||
337 | /** |
||
338 | * Force Freedom mode in VideoPress. |
||
339 | * |
||
340 | * @param array $options Array of VideoPress shortcode options. |
||
341 | */ |
||
342 | public static function videopress_enable_freedom_mode( $options ) { |
||
348 | |||
349 | /** |
||
350 | * Display custom markup for the sharing buttons when in an AMP view. |
||
351 | * |
||
352 | * @param string $markup Content markup of the Jetpack sharing links. |
||
353 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
354 | */ |
||
355 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
403 | |||
404 | /** |
||
405 | * Tells Jetpack not to enqueue CSS for share buttons. |
||
406 | * |
||
407 | * @param bool $enqueue Whether or not to enqueue. |
||
408 | * @return bool Whether or not to enqueue. |
||
409 | */ |
||
410 | public static function amp_disable_sharedaddy_css( $enqueue ) { |
||
417 | |||
418 | /** |
||
419 | * Ensure proper Photon image dimensions for AMP Stories. |
||
420 | * |
||
421 | * @param array $args Array of Photon Arguments. |
||
422 | * @param array $details { |
||
423 | * Array of image details. |
||
424 | * |
||
425 | * @type string $tag Image tag (Image HTML output). |
||
426 | * @type string $src Image URL. |
||
427 | * @type string $src_orig Original Image URL. |
||
428 | * @type int|false $width Image width. |
||
429 | * @type int|false $height Image height. |
||
430 | * @type int|false $width_orig Original image width before constrained by content_width. |
||
431 | * @type int|false $height_orig Original Image height before constrained by content_width. |
||
432 | * @type string $transform_orig Original transform before constrained by content_width. |
||
433 | * } |
||
434 | * @return array Args. |
||
435 | */ |
||
436 | public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
||
485 | |||
486 | /** |
||
487 | * Adds amp-options to the list of options to sync, if AMP is available |
||
488 | * |
||
489 | * @param array $options_whitelist Whitelist of options to sync. |
||
490 | * @return array Updated options whitelist |
||
491 | */ |
||
492 | public static function filter_jetpack_options_whitelist( $options_whitelist ) { |
||
498 | } |
||
499 | |||
503 |