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 | public static function show_admin_bar( $show ) { |
||
| 62 | return $show && ! self::is_amp_request(); |
||
| 63 | } |
||
| 64 | |||
| 65 | public static function comment_likes_enabled( $enabled ) { |
||
| 66 | return $enabled && ! self::is_amp_request(); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Apply custom AMP changes in wp-admin. |
||
| 71 | */ |
||
| 72 | public static function admin_init() { |
||
| 73 | // disable Likes metabox for post editor if AMP canonical disabled. |
||
| 74 | add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 ); |
||
| 75 | } |
||
| 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() { |
||
| 84 | return function_exists( 'amp_is_canonical' ) && amp_is_canonical(); |
||
| 85 | } |
||
| 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() { |
||
| 93 | $is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns true if the current request should return valid AMP content. |
||
| 97 | * |
||
| 98 | * @since 6.2.0 |
||
| 99 | * |
||
| 100 | * @param boolean $is_amp_request Is this request supposed to return valid AMP content? |
||
| 101 | */ |
||
| 102 | return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Remove content filters added by Jetpack. |
||
| 107 | */ |
||
| 108 | public static function amp_disable_the_content_filters() { |
||
| 109 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 110 | add_filter( 'videopress_show_2015_player', '__return_true' ); |
||
| 111 | add_filter( 'protected_embeds_use_form_post', '__return_false' ); |
||
| 112 | remove_filter( 'the_title', 'widont' ); |
||
| 113 | } |
||
| 114 | |||
| 115 | remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 ); |
||
| 116 | remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 ); |
||
| 117 | } |
||
| 118 | |||
| 119 | public static function disable_comment_likes_before_the_content( $content ) { |
||
| 120 | if ( self::is_amp_request() ) { |
||
| 121 | remove_filter( 'comment_text', 'comment_like_button', 12, 2 ); |
||
| 122 | } |
||
| 123 | return $content; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Add Jetpack stats pixel. |
||
| 128 | * |
||
| 129 | * @since 6.2.1 |
||
| 130 | */ |
||
| 131 | public static function add_stats_pixel() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Add publisher and image metadata to legacy AMP post. |
||
| 140 | * |
||
| 141 | * @since 6.2.0 |
||
| 142 | * |
||
| 143 | * @param array $metadata Metadata array. |
||
| 144 | * @param WP_Post $post Post. |
||
| 145 | * @return array Modified metadata array. |
||
| 146 | */ |
||
| 147 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Add blavatar to legacy AMP post metadata. |
||
| 161 | * |
||
| 162 | * @since 6.2.0 |
||
| 163 | * |
||
| 164 | * @param array $metadata Metadata. |
||
| 165 | * |
||
| 166 | * @return array Metadata. |
||
| 167 | */ |
||
| 168 | private static function add_site_icon_to_metadata( $metadata ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Add image to legacy AMP post metadata. |
||
| 193 | * |
||
| 194 | * @since 6.2.0 |
||
| 195 | * |
||
| 196 | * @param array $metadata Metadata. |
||
| 197 | * @param WP_Post $post Post. |
||
| 198 | * @return array Metadata. |
||
| 199 | */ |
||
| 200 | private static function add_image_to_metadata( $metadata, $post ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Add fallback image to legacy AMP post metadata. |
||
| 245 | * |
||
| 246 | * @since 6.2.0 |
||
| 247 | * |
||
| 248 | * @param array $metadata Metadata. |
||
| 249 | * @return array Metadata. |
||
| 250 | */ |
||
| 251 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
| 267 | * |
||
| 268 | * @param string $domain Asset URL. |
||
| 269 | */ |
||
| 270 | private static function staticize_subdomain( $domain ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
| 281 | * |
||
| 282 | * @since 6.2.0 |
||
| 283 | * |
||
| 284 | * @param array $dimensions Dimensions. |
||
| 285 | * @return array Dimensions. |
||
| 286 | */ |
||
| 287 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Display Open Graph Meta tags in AMP views. |
||
| 311 | */ |
||
| 312 | public static function amp_post_jetpack_og_tags() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Force Freedom mode in VideoPress. |
||
| 324 | * |
||
| 325 | * @param array $options Array of VideoPress shortcode options. |
||
| 326 | */ |
||
| 327 | public static function videopress_enable_freedom_mode( $options ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Display custom markup for the sharing buttons when in an AMP view. |
||
| 336 | * |
||
| 337 | * @param string $markup Content markup of the Jetpack sharing links. |
||
| 338 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
| 339 | */ |
||
| 340 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Tells Jetpack not to enqueue CSS for share buttons. |
||
| 391 | * |
||
| 392 | * @param bool $enqueue Whether or not to enqueue. |
||
| 393 | * @return bool Whether or not to enqueue. |
||
| 394 | */ |
||
| 395 | public static function amp_disable_sharedaddy_css( $enqueue ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Ensure proper Photon image dimensions for AMP Stories. |
||
| 405 | * |
||
| 406 | * @param array $args Array of Photon Arguments. |
||
| 407 | * @param array $details { |
||
| 408 | * Array of image details. |
||
| 409 | * |
||
| 410 | * @type string $tag Image tag (Image HTML output). |
||
| 411 | * @type string $src Image URL. |
||
| 412 | * @type string $src_orig Original Image URL. |
||
| 413 | * @type int|false $width Image width. |
||
| 414 | * @type int|false $height Image height. |
||
| 415 | * @type int|false $width_orig Original image width before constrained by content_width. |
||
| 416 | * @type int|false $height_orig Original Image height before constrained by content_width. |
||
| 417 | * @type string $transform_orig Original transform before constrained by content_width. |
||
| 418 | * } |
||
| 419 | * @return array Args. |
||
| 420 | */ |
||
| 421 | public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Adds amp-options to the list of options to sync, if AMP is available |
||
| 473 | * |
||
| 474 | * @param array $options_whitelist Whitelist of options to sync. |
||
| 475 | * @return array Updated options whitelist |
||
| 476 | */ |
||
| 477 | public static function filter_jetpack_options_whitelist( $options_whitelist ) { |
||
| 483 | } |
||
| 484 | |||
| 488 |