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 onthe frontend.  | 
            ||
| 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 | // Sharing.  | 
            ||
| 26 | add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );  | 
            ||
| 27 | add_filter( 'sharing_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_disable_sharedaddy_css' ) );  | 
            ||
| 28 | |||
| 29 | // enforce freedom mode for videopress.  | 
            ||
| 30 | add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );  | 
            ||
| 31 | |||
| 32 | // include Jetpack og tags when rendering native AMP head.  | 
            ||
| 33 | add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );  | 
            ||
| 34 | |||
| 35 | // Post rendering changes for legacy AMP.  | 
            ||
| 36 | add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );  | 
            ||
| 37 | |||
| 38 | // Add post template metadata for legacy AMP.  | 
            ||
| 39 | add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );  | 
            ||
| 40 | |||
| 41 | // Filter photon image args for AMP Stories.  | 
            ||
| 42 | add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 );  | 
            ||
| 43 | }  | 
            ||
| 44 | |||
| 45 | /**  | 
            ||
| 46 | * Apply custom AMP changes in wp-admin.  | 
            ||
| 47 | */  | 
            ||
| 48 | 	public static function admin_init() { | 
            ||
| 52 | |||
| 53 | /**  | 
            ||
| 54 | * Is the page in AMP 'canonical mode'.  | 
            ||
| 55 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`.  | 
            ||
| 56 | *  | 
            ||
| 57 | * @return bool is_amp_canonical  | 
            ||
| 58 | */  | 
            ||
| 59 | 	public static function is_amp_canonical() { | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * Does the page return AMP content.  | 
            ||
| 65 | *  | 
            ||
| 66 | * @return bool $is_amp_request Are we on am AMP view.  | 
            ||
| 67 | */  | 
            ||
| 68 | 	public static function is_amp_request() { | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * Remove content filters added by Jetpack.  | 
            ||
| 83 | */  | 
            ||
| 84 | 	public static function amp_disable_the_content_filters() { | 
            ||
| 94 | |||
| 95 | /**  | 
            ||
| 96 | * Add Jetpack stats pixel.  | 
            ||
| 97 | *  | 
            ||
| 98 | * @since 6.2.1  | 
            ||
| 99 | */  | 
            ||
| 100 | 	public static function add_stats_pixel() { | 
            ||
| 106 | |||
| 107 | /**  | 
            ||
| 108 | * Add publisher and image metadata to legacy AMP post.  | 
            ||
| 109 | *  | 
            ||
| 110 | * @since 6.2.0  | 
            ||
| 111 | *  | 
            ||
| 112 | * @param array $metadata Metadata array.  | 
            ||
| 113 | * @param WP_Post $post Post.  | 
            ||
| 114 | * @return array Modified metadata array.  | 
            ||
| 115 | */  | 
            ||
| 116 | 	public static function amp_post_template_metadata( $metadata, $post ) { | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * Add blavatar to legacy AMP post metadata.  | 
            ||
| 130 | *  | 
            ||
| 131 | * @since 6.2.0  | 
            ||
| 132 | *  | 
            ||
| 133 | * @param array $metadata Metadata.  | 
            ||
| 134 | *  | 
            ||
| 135 | * @return array Metadata.  | 
            ||
| 136 | */  | 
            ||
| 137 | 	private static function add_site_icon_to_metadata( $metadata ) { | 
            ||
| 159 | |||
| 160 | /**  | 
            ||
| 161 | * Add image to legacy AMP post metadata.  | 
            ||
| 162 | *  | 
            ||
| 163 | * @since 6.2.0  | 
            ||
| 164 | *  | 
            ||
| 165 | * @param array $metadata Metadata.  | 
            ||
| 166 | * @param WP_Post $post Post.  | 
            ||
| 167 | * @return array Metadata.  | 
            ||
| 168 | */  | 
            ||
| 169 | 	private static function add_image_to_metadata( $metadata, $post ) { | 
            ||
| 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 | 	private static function add_fallback_image_to_metadata( $metadata ) { | 
            ||
| 233 | |||
| 234 | /**  | 
            ||
| 235 | * Return static WordPress.com domain to use to load resources from WordPress.com.  | 
            ||
| 236 | *  | 
            ||
| 237 | * @param string $domain Asset URL.  | 
            ||
| 238 | */  | 
            ||
| 239 | 	private static function staticize_subdomain( $domain ) { | 
            ||
| 247 | |||
| 248 | /**  | 
            ||
| 249 | * Extract image dimensions via wpcom/imagesize, only on WPCOM  | 
            ||
| 250 | *  | 
            ||
| 251 | * @since 6.2.0  | 
            ||
| 252 | *  | 
            ||
| 253 | * @param array $dimensions Dimensions.  | 
            ||
| 254 | * @return array Dimensions.  | 
            ||
| 255 | */  | 
            ||
| 256 | 	private static function extract_image_dimensions_from_getimagesize( $dimensions ) { | 
            ||
| 277 | |||
| 278 | /**  | 
            ||
| 279 | * Display Open Graph Meta tags in AMP views.  | 
            ||
| 280 | */  | 
            ||
| 281 | 	public static function amp_post_jetpack_og_tags() { | 
            ||
| 290 | |||
| 291 | /**  | 
            ||
| 292 | * Force Freedom mode in VideoPress.  | 
            ||
| 293 | *  | 
            ||
| 294 | * @param array $options Array of VideoPress shortcode options.  | 
            ||
| 295 | */  | 
            ||
| 296 | 	public static function videopress_enable_freedom_mode( $options ) { | 
            ||
| 302 | |||
| 303 | /**  | 
            ||
| 304 | * Display custom markup for the sharing buttons when in an AMP view.  | 
            ||
| 305 | *  | 
            ||
| 306 | * @param string $markup Content markup of the Jetpack sharing links.  | 
            ||
| 307 | * @param array $sharing_enabled Array of Sharing Services currently enabled.  | 
            ||
| 308 | */  | 
            ||
| 309 | 	public static function render_sharing_html( $markup, $sharing_enabled ) { | 
            ||
| 357 | |||
| 358 | /**  | 
            ||
| 359 | * Tells Jetpack not to enqueue CSS for share buttons.  | 
            ||
| 360 | *  | 
            ||
| 361 | * @param bool $enqueue Whether or not to enqueue.  | 
            ||
| 362 | * @return bool Whether or not to enqueue.  | 
            ||
| 363 | */  | 
            ||
| 364 | 	public static function amp_disable_sharedaddy_css( $enqueue ) { | 
            ||
| 371 | |||
| 372 | /**  | 
            ||
| 373 | * Ensure proper Photon image dimensions for AMP Stories.  | 
            ||
| 374 | *  | 
            ||
| 375 | * @param array $args Array of Photon Arguments.  | 
            ||
| 376 | 	 * @param array $details { | 
            ||
| 377 | * Array of image details.  | 
            ||
| 378 | *  | 
            ||
| 379 | * @type string $tag Image tag (Image HTML output).  | 
            ||
| 380 | * @type string $src Image URL.  | 
            ||
| 381 | * @type string $src_orig Original Image URL.  | 
            ||
| 382 | * @type int|false $width Image width.  | 
            ||
| 383 | * @type int|false $height Image height.  | 
            ||
| 384 | * @type int|false $width_orig Original image width before constrained by content_width.  | 
            ||
| 385 | * @type int|false $height_orig Original Image height before constrained by content_width.  | 
            ||
| 386 | * @type string $transform_orig Original transform before constrained by content_width.  | 
            ||
| 387 | * }  | 
            ||
| 388 | * @return array Args.  | 
            ||
| 389 | */  | 
            ||
| 390 | 	public static function filter_photon_post_image_args_for_stories( $args, $details ) { | 
            ||
| 439 | }  | 
            ||
| 440 | |||
| 444 |