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 | add_action( 'wp_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_enqueue_sharing_css' ) ); |
||
| 35 | |||
| 36 | // Sharing for Reader mode. |
||
| 37 | if ( function_exists( 'jetpack_social_menu_include_svg_icons' ) ) { |
||
| 38 | add_action( 'amp_post_template_footer', 'jetpack_social_menu_include_svg_icons' ); |
||
| 39 | } |
||
| 40 | add_action( 'amp_post_template_css', array( 'Jetpack_AMP_Support', 'amp_reader_sharing_css' ), 10, 0 ); |
||
| 41 | |||
| 42 | // enforce freedom mode for videopress. |
||
| 43 | add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) ); |
||
| 44 | |||
| 45 | // include Jetpack og tags when rendering native AMP head. |
||
| 46 | add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) ); |
||
| 47 | |||
| 48 | // Post rendering changes for legacy AMP. |
||
| 49 | add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) ); |
||
| 50 | |||
| 51 | // Disable Comment Likes. |
||
| 52 | add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) ); |
||
| 53 | |||
| 54 | // Transitional mode AMP should not have comment likes. |
||
| 55 | add_filter( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) ); |
||
| 56 | |||
| 57 | // Remove the Likes button from the admin bar. |
||
| 58 | add_filter( 'jetpack_admin_bar_likes_enabled', array( 'Jetpack_AMP_Support', 'disable_likes_admin_bar' ) ); |
||
| 59 | |||
| 60 | // Add post template metadata for legacy AMP. |
||
| 61 | add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 ); |
||
| 62 | |||
| 63 | // Filter photon image args for AMP Stories. |
||
| 64 | add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 ); |
||
| 65 | |||
| 66 | // Sync the amp-options. |
||
| 67 | add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_safelist' ) ); |
||
| 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 | * Do not display the Likes' Admin bar on AMP requests. |
||
| 143 | * |
||
| 144 | * @param bool $is_admin_bar_button_visible Should the Like button be visible in the Admin bar. Default to true. |
||
| 145 | */ |
||
| 146 | public static function disable_likes_admin_bar( $is_admin_bar_button_visible ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Add Jetpack stats pixel. |
||
| 155 | * |
||
| 156 | * @since 6.2.1 |
||
| 157 | */ |
||
| 158 | public static function add_stats_pixel() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add publisher and image metadata to legacy AMP post. |
||
| 167 | * |
||
| 168 | * @since 6.2.0 |
||
| 169 | * |
||
| 170 | * @param array $metadata Metadata array. |
||
| 171 | * @param WP_Post $post Post. |
||
| 172 | * @return array Modified metadata array. |
||
| 173 | */ |
||
| 174 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Add blavatar to legacy AMP post metadata. |
||
| 188 | * |
||
| 189 | * @since 6.2.0 |
||
| 190 | * |
||
| 191 | * @param array $metadata Metadata. |
||
| 192 | * |
||
| 193 | * @return array Metadata. |
||
| 194 | */ |
||
| 195 | private static function add_site_icon_to_metadata( $metadata ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Add image to legacy AMP post metadata. |
||
| 220 | * |
||
| 221 | * @since 6.2.0 |
||
| 222 | * |
||
| 223 | * @param array $metadata Metadata. |
||
| 224 | * @param WP_Post $post Post. |
||
| 225 | * @return array Metadata. |
||
| 226 | */ |
||
| 227 | private static function add_image_to_metadata( $metadata, $post ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Add fallback image to legacy AMP post metadata. |
||
| 272 | * |
||
| 273 | * @since 6.2.0 |
||
| 274 | * |
||
| 275 | * @param array $metadata Metadata. |
||
| 276 | * @return array Metadata. |
||
| 277 | */ |
||
| 278 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
| 294 | * |
||
| 295 | * @param string $domain Asset URL. |
||
| 296 | */ |
||
| 297 | private static function staticize_subdomain( $domain ) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
| 308 | * |
||
| 309 | * @since 6.2.0 |
||
| 310 | * |
||
| 311 | * @param array $dimensions Dimensions. |
||
| 312 | * @return array Dimensions. |
||
| 313 | */ |
||
| 314 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Display Open Graph Meta tags in AMP views. |
||
| 338 | */ |
||
| 339 | public static function amp_post_jetpack_og_tags() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Force Freedom mode in VideoPress. |
||
| 351 | * |
||
| 352 | * @param array $options Array of VideoPress shortcode options. |
||
| 353 | */ |
||
| 354 | public static function videopress_enable_freedom_mode( $options ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Display custom markup for the sharing buttons when in an AMP view. |
||
| 363 | * |
||
| 364 | * @param string $markup Content markup of the Jetpack sharing links. |
||
| 365 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
| 366 | */ |
||
| 367 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Tells Jetpack not to enqueue CSS for share buttons. |
||
| 402 | * |
||
| 403 | * @param bool $enqueue Whether or not to enqueue. |
||
| 404 | * @return bool Whether or not to enqueue. |
||
| 405 | */ |
||
| 406 | public static function amp_disable_sharedaddy_css( $enqueue ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Enqueues the AMP specific sharing styles for the sharing icons. |
||
| 416 | */ |
||
| 417 | public static function amp_enqueue_sharing_css() { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * For the AMP Reader mode template, include styles that we need. |
||
| 425 | */ |
||
| 426 | public static function amp_reader_sharing_css() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Ensure proper Photon image dimensions for AMP Stories. |
||
| 445 | * |
||
| 446 | * @param array $args Array of Photon Arguments. |
||
| 447 | * @param array $details { |
||
| 448 | * Array of image details. |
||
| 449 | * |
||
| 450 | * @type string $tag Image tag (Image HTML output). |
||
| 451 | * @type string $src Image URL. |
||
| 452 | * @type string $src_orig Original Image URL. |
||
| 453 | * @type int|false $width Image width. |
||
| 454 | * @type int|false $height Image height. |
||
| 455 | * @type int|false $width_orig Original image width before constrained by content_width. |
||
| 456 | * @type int|false $height_orig Original Image height before constrained by content_width. |
||
| 457 | * @type string $transform_orig Original transform before constrained by content_width. |
||
| 458 | * } |
||
| 459 | * @return array Args. |
||
| 460 | */ |
||
| 461 | public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Adds amp-options to the list of options to sync, if AMP is available |
||
| 513 | * |
||
| 514 | * @param array $options_safelist Safelist of options to sync. |
||
| 515 | * |
||
| 516 | * @return array Updated options safelist |
||
| 517 | */ |
||
| 518 | public static function filter_jetpack_options_safelist( $options_safelist ) { |
||
| 524 | } |
||
| 525 | |||
| 529 |