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 | // Disable Comment Likes. |
||
| 57 | add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Disable the Comment Likes feature on AMP views. |
||
| 62 | * |
||
| 63 | * @param bool $enabled Should comment likes be enabled. |
||
| 64 | */ |
||
| 65 | public static function comment_likes_enabled( $enabled ) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Apply custom AMP changes in wp-admin. |
||
| 71 | */ |
||
| 72 | public static function admin_init() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Is the page in AMP 'canonical mode'. |
||
| 79 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`. |
||
| 80 | * |
||
| 81 | * @return bool is_amp_canonical |
||
| 82 | */ |
||
| 83 | public static function is_amp_canonical() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Does the page return AMP content. |
||
| 89 | * |
||
| 90 | * @return bool $is_amp_request Are we on am AMP view. |
||
| 91 | */ |
||
| 92 | public static function is_amp_request() { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Remove content filters added by Jetpack. |
||
| 107 | */ |
||
| 108 | public static function amp_disable_the_content_filters() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Do not add comment likes on AMP requests. |
||
| 121 | * |
||
| 122 | * @param string $content Post content. |
||
| 123 | */ |
||
| 124 | public static function disable_comment_likes_before_the_content( $content ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Add Jetpack stats pixel. |
||
| 133 | * |
||
| 134 | * @since 6.2.1 |
||
| 135 | */ |
||
| 136 | public static function add_stats_pixel() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Add publisher and image metadata to legacy AMP post. |
||
| 145 | * |
||
| 146 | * @since 6.2.0 |
||
| 147 | * |
||
| 148 | * @param array $metadata Metadata array. |
||
| 149 | * @param WP_Post $post Post. |
||
| 150 | * @return array Modified metadata array. |
||
| 151 | */ |
||
| 152 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Add blavatar to legacy AMP post metadata. |
||
| 166 | * |
||
| 167 | * @since 6.2.0 |
||
| 168 | * |
||
| 169 | * @param array $metadata Metadata. |
||
| 170 | * |
||
| 171 | * @return array Metadata. |
||
| 172 | */ |
||
| 173 | private static function add_site_icon_to_metadata( $metadata ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Add image to legacy AMP post metadata. |
||
| 198 | * |
||
| 199 | * @since 6.2.0 |
||
| 200 | * |
||
| 201 | * @param array $metadata Metadata. |
||
| 202 | * @param WP_Post $post Post. |
||
| 203 | * @return array Metadata. |
||
| 204 | */ |
||
| 205 | private static function add_image_to_metadata( $metadata, $post ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Add fallback image to legacy AMP post metadata. |
||
| 250 | * |
||
| 251 | * @since 6.2.0 |
||
| 252 | * |
||
| 253 | * @param array $metadata Metadata. |
||
| 254 | * @return array Metadata. |
||
| 255 | */ |
||
| 256 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
| 272 | * |
||
| 273 | * @param string $domain Asset URL. |
||
| 274 | */ |
||
| 275 | private static function staticize_subdomain( $domain ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
| 286 | * |
||
| 287 | * @since 6.2.0 |
||
| 288 | * |
||
| 289 | * @param array $dimensions Dimensions. |
||
| 290 | * @return array Dimensions. |
||
| 291 | */ |
||
| 292 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Display Open Graph Meta tags in AMP views. |
||
| 316 | */ |
||
| 317 | public static function amp_post_jetpack_og_tags() { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Force Freedom mode in VideoPress. |
||
| 329 | * |
||
| 330 | * @param array $options Array of VideoPress shortcode options. |
||
| 331 | */ |
||
| 332 | public static function videopress_enable_freedom_mode( $options ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Display custom markup for the sharing buttons when in an AMP view. |
||
| 341 | * |
||
| 342 | * @param string $markup Content markup of the Jetpack sharing links. |
||
| 343 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
| 344 | */ |
||
| 345 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Tells Jetpack not to enqueue CSS for share buttons. |
||
| 396 | * |
||
| 397 | * @param bool $enqueue Whether or not to enqueue. |
||
| 398 | * @return bool Whether or not to enqueue. |
||
| 399 | */ |
||
| 400 | public static function amp_disable_sharedaddy_css( $enqueue ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Ensure proper Photon image dimensions for AMP Stories. |
||
| 410 | * |
||
| 411 | * @param array $args Array of Photon Arguments. |
||
| 412 | * @param array $details { |
||
| 413 | * Array of image details. |
||
| 414 | * |
||
| 415 | * @type string $tag Image tag (Image HTML output). |
||
| 416 | * @type string $src Image URL. |
||
| 417 | * @type string $src_orig Original Image URL. |
||
| 418 | * @type int|false $width Image width. |
||
| 419 | * @type int|false $height Image height. |
||
| 420 | * @type int|false $width_orig Original image width before constrained by content_width. |
||
| 421 | * @type int|false $height_orig Original Image height before constrained by content_width. |
||
| 422 | * @type string $transform_orig Original transform before constrained by content_width. |
||
| 423 | * } |
||
| 424 | * @return array Args. |
||
| 425 | */ |
||
| 426 | public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Adds amp-options to the list of options to sync, if AMP is available |
||
| 478 | * |
||
| 479 | * @param array $options_whitelist Whitelist of options to sync. |
||
| 480 | * @return array Updated options whitelist |
||
| 481 | */ |
||
| 482 | public static function filter_jetpack_options_whitelist( $options_whitelist ) { |
||
| 488 | } |
||
| 489 | |||
| 493 |