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 | // enforce freedom mode for videopress. |
||
| 37 | add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) ); |
||
| 38 | |||
| 39 | // include Jetpack og tags when rendering native AMP head. |
||
| 40 | add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) ); |
||
| 41 | |||
| 42 | // Post rendering changes for legacy AMP. |
||
| 43 | add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) ); |
||
| 44 | |||
| 45 | // Transitional mode AMP should not have comment likes. |
||
| 46 | add_action( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) ); |
||
| 47 | |||
| 48 | // Add post template metadata for legacy AMP. |
||
| 49 | add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 ); |
||
| 50 | |||
| 51 | // Filter photon image args for AMP Stories. |
||
| 52 | add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 ); |
||
| 53 | |||
| 54 | // Sync the amp-options. |
||
| 55 | add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_whitelist' ) ); |
||
| 56 | |||
| 57 | // Show admin bar. |
||
| 58 | add_filter( 'show_admin_bar', array( 'Jetpack_AMP_Support', 'show_admin_bar' ) ); |
||
| 59 | add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) ); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Disable the admin bar on AMP views. |
||
| 64 | * |
||
| 65 | * @param Whether bool $show the admin bar should be shown. Default false. |
||
| 66 | */ |
||
| 67 | public static function show_admin_bar( $show ) { |
||
| 68 | return $show && ! self::is_amp_request(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Disable the Comment Likes feature on AMP views. |
||
| 73 | * |
||
| 74 | * @param bool $enabled Should comment likes be enabled. |
||
| 75 | */ |
||
| 76 | public static function comment_likes_enabled( $enabled ) { |
||
| 77 | return $enabled && ! self::is_amp_request(); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Apply custom AMP changes in wp-admin. |
||
| 82 | */ |
||
| 83 | public static function admin_init() { |
||
| 84 | // disable Likes metabox for post editor if AMP canonical disabled. |
||
| 85 | add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 ); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Is the page in AMP 'canonical mode'. |
||
| 90 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`. |
||
| 91 | * |
||
| 92 | * @return bool is_amp_canonical |
||
| 93 | */ |
||
| 94 | public static function is_amp_canonical() { |
||
| 95 | return function_exists( 'amp_is_canonical' ) && amp_is_canonical(); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Does the page return AMP content. |
||
| 100 | * |
||
| 101 | * @return bool $is_amp_request Are we on am AMP view. |
||
| 102 | */ |
||
| 103 | public static function is_amp_request() { |
||
| 104 | $is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns true if the current request should return valid AMP content. |
||
| 108 | * |
||
| 109 | * @since 6.2.0 |
||
| 110 | * |
||
| 111 | * @param boolean $is_amp_request Is this request supposed to return valid AMP content? |
||
| 112 | */ |
||
| 113 | return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Remove content filters added by Jetpack. |
||
| 118 | */ |
||
| 119 | public static function amp_disable_the_content_filters() { |
||
| 120 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 121 | add_filter( 'videopress_show_2015_player', '__return_true' ); |
||
| 122 | add_filter( 'protected_embeds_use_form_post', '__return_false' ); |
||
| 123 | remove_filter( 'the_title', 'widont' ); |
||
| 124 | } |
||
| 125 | |||
| 126 | remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 ); |
||
| 127 | remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 ); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Do not add comment likes on AMP requests. |
||
| 132 | * |
||
| 133 | * @param string $content Post content. |
||
| 134 | */ |
||
| 135 | public static function disable_comment_likes_before_the_content( $content ) { |
||
| 136 | if ( self::is_amp_request() ) { |
||
| 137 | remove_filter( 'comment_text', 'comment_like_button', 12, 2 ); |
||
| 138 | } |
||
| 139 | return $content; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Add Jetpack stats pixel. |
||
| 144 | * |
||
| 145 | * @since 6.2.1 |
||
| 146 | */ |
||
| 147 | public static function add_stats_pixel() { |
||
| 148 | if ( ! has_action( 'wp_footer', 'stats_footer' ) ) { |
||
| 149 | return; |
||
| 150 | } |
||
| 151 | stats_render_amp_footer( stats_build_view_data() ); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Add publisher and image metadata to legacy AMP post. |
||
| 156 | * |
||
| 157 | * @since 6.2.0 |
||
| 158 | * |
||
| 159 | * @param array $metadata Metadata array. |
||
| 160 | * @param WP_Post $post Post. |
||
| 161 | * @return array Modified metadata array. |
||
| 162 | */ |
||
| 163 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
| 164 | if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) { |
||
| 165 | $metadata = self::add_site_icon_to_metadata( $metadata ); |
||
| 166 | } |
||
| 167 | |||
| 168 | if ( ! isset( $metadata['image'] ) ) { |
||
| 169 | $metadata = self::add_image_to_metadata( $metadata, $post ); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $metadata; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Add blavatar to legacy AMP post metadata. |
||
| 177 | * |
||
| 178 | * @since 6.2.0 |
||
| 179 | * |
||
| 180 | * @param array $metadata Metadata. |
||
| 181 | * |
||
| 182 | * @return array Metadata. |
||
| 183 | */ |
||
| 184 | private static function add_site_icon_to_metadata( $metadata ) { |
||
| 185 | $size = 60; |
||
| 186 | $site_icon_url = class_exists( 'Automattic\\Jetpack\\Sync\\Functions' ) ? Functions::site_icon_url( $size ) : ''; |
||
| 187 | |||
| 188 | if ( function_exists( 'blavatar_domain' ) ) { |
||
| 189 | $metadata['publisher']['logo'] = array( |
||
| 190 | '@type' => 'ImageObject', |
||
| 191 | 'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ), |
||
| 192 | 'width' => $size, |
||
| 193 | 'height' => $size, |
||
| 194 | ); |
||
| 195 | } elseif ( $site_icon_url ) { |
||
| 196 | $metadata['publisher']['logo'] = array( |
||
| 197 | '@type' => 'ImageObject', |
||
| 198 | 'url' => $site_icon_url, |
||
| 199 | 'width' => $size, |
||
| 200 | 'height' => $size, |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $metadata; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Add image to legacy AMP post metadata. |
||
| 209 | * |
||
| 210 | * @since 6.2.0 |
||
| 211 | * |
||
| 212 | * @param array $metadata Metadata. |
||
| 213 | * @param WP_Post $post Post. |
||
| 214 | * @return array Metadata. |
||
| 215 | */ |
||
| 216 | private static function add_image_to_metadata( $metadata, $post ) { |
||
| 217 | $image = Jetpack_PostImages::get_image( |
||
| 218 | $post->ID, |
||
| 219 | array( |
||
| 220 | 'fallback_to_avatars' => true, |
||
| 221 | 'avatar_size' => 200, |
||
| 222 | // AMP already attempts these. |
||
| 223 | 'from_thumbnail' => false, |
||
| 224 | 'from_attachment' => false, |
||
| 225 | ) |
||
| 226 | ); |
||
| 227 | |||
| 228 | if ( empty( $image ) ) { |
||
| 229 | return self::add_fallback_image_to_metadata( $metadata ); |
||
| 230 | } |
||
| 231 | |||
| 232 | if ( ! isset( $image['src_width'] ) ) { |
||
| 233 | $dimensions = self::extract_image_dimensions_from_getimagesize( |
||
| 234 | array( |
||
| 235 | $image['src'] => false, |
||
| 236 | ) |
||
| 237 | ); |
||
| 238 | |||
| 239 | View Code Duplication | if ( false !== $dimensions[ $image['src'] ] ) { |
|
| 240 | $image['src_width'] = $dimensions['width']; |
||
| 241 | $image['src_height'] = $dimensions['height']; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | $metadata['image'] = array( |
||
| 246 | '@type' => 'ImageObject', |
||
| 247 | 'url' => $image['src'], |
||
| 248 | ); |
||
| 249 | if ( isset( $image['src_width'] ) ) { |
||
| 250 | $metadata['image']['width'] = $image['src_width']; |
||
| 251 | } |
||
| 252 | if ( isset( $image['src_width'] ) ) { |
||
| 253 | $metadata['image']['height'] = $image['src_height']; |
||
| 254 | } |
||
| 255 | |||
| 256 | return $metadata; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Add fallback image to legacy AMP post metadata. |
||
| 261 | * |
||
| 262 | * @since 6.2.0 |
||
| 263 | * |
||
| 264 | * @param array $metadata Metadata. |
||
| 265 | * @return array Metadata. |
||
| 266 | */ |
||
| 267 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
| 268 | /** This filter is documented in functions.opengraph.php */ |
||
| 269 | $default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' ); |
||
| 270 | |||
| 271 | $metadata['image'] = array( |
||
| 272 | '@type' => 'ImageObject', |
||
| 273 | 'url' => self::staticize_subdomain( $default_image ), |
||
| 274 | 'width' => 200, |
||
| 275 | 'height' => 200, |
||
| 276 | ); |
||
| 277 | |||
| 278 | return $metadata; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
| 283 | * |
||
| 284 | * @param string $domain Asset URL. |
||
| 285 | */ |
||
| 286 | private static function staticize_subdomain( $domain ) { |
||
| 287 | // deal with WPCOM vs Jetpack. |
||
| 288 | if ( function_exists( 'staticize_subdomain' ) ) { |
||
| 289 | return staticize_subdomain( $domain ); |
||
| 290 | } else { |
||
| 291 | return Jetpack::staticize_subdomain( $domain ); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
| 297 | * |
||
| 298 | * @since 6.2.0 |
||
| 299 | * |
||
| 300 | * @param array $dimensions Dimensions. |
||
| 301 | * @return array Dimensions. |
||
| 302 | */ |
||
| 303 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
| 304 | if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'require_lib' ) ) ) { |
||
| 305 | return $dimensions; |
||
| 306 | } |
||
| 307 | require_lib( 'wpcom/imagesize' ); |
||
| 308 | |||
| 309 | foreach ( $dimensions as $url => $value ) { |
||
| 310 | if ( is_array( $value ) ) { |
||
| 311 | continue; |
||
| 312 | } |
||
| 313 | $result = wpcom_getimagesize( $url ); |
||
| 314 | if ( is_array( $result ) ) { |
||
| 315 | $dimensions[ $url ] = array( |
||
| 316 | 'width' => $result[0], |
||
| 317 | 'height' => $result[1], |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | return $dimensions; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Display Open Graph Meta tags in AMP views. |
||
| 327 | */ |
||
| 328 | public static function amp_post_jetpack_og_tags() { |
||
| 329 | if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
||
| 330 | Jetpack::init()->check_open_graph(); |
||
| 331 | } |
||
| 332 | |||
| 333 | if ( function_exists( 'jetpack_og_tags' ) ) { |
||
| 334 | jetpack_og_tags(); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Force Freedom mode in VideoPress. |
||
| 340 | * |
||
| 341 | * @param array $options Array of VideoPress shortcode options. |
||
| 342 | */ |
||
| 343 | public static function videopress_enable_freedom_mode( $options ) { |
||
| 344 | if ( self::is_amp_request() ) { |
||
| 345 | $options['freedom'] = true; |
||
| 346 | } |
||
| 347 | return $options; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Display custom markup for the sharing buttons when in an AMP view. |
||
| 352 | * |
||
| 353 | * @param string $markup Content markup of the Jetpack sharing links. |
||
| 354 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
| 355 | */ |
||
| 356 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
| 357 | global $post; |
||
| 358 | |||
| 359 | if ( empty( $post ) ) { |
||
| 360 | return ''; |
||
| 361 | } |
||
| 362 | |||
| 363 | if ( ! self::is_amp_request() ) { |
||
| 364 | return $markup; |
||
| 365 | } |
||
| 366 | |||
| 367 | remove_action( 'wp_footer', 'sharing_add_footer' ); |
||
| 368 | if ( empty( $sharing_enabled ) ) { |
||
| 369 | return $markup; |
||
| 370 | } |
||
| 371 | |||
| 372 | $sharing_links = array(); |
||
| 373 | foreach ( $sharing_enabled['visible'] as $id => $service ) { |
||
| 374 | $sharing_link = $service->get_amp_display( $post ); |
||
| 375 | if ( ! empty( $sharing_link ) ) { |
||
| 376 | $sharing_links[] = $sharing_link; |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | // Replace the existing unordered list with AMP sharing buttons. |
||
| 381 | $markup = preg_replace( '#<ul>(.+)</ul>#', implode( '', $sharing_links ), $markup ); |
||
| 382 | |||
| 383 | // Remove any lingering share-end list items. |
||
| 384 | $markup = str_replace( '<li class="share-end"></li>', '', $markup ); |
||
| 385 | |||
| 386 | return $markup; |
||
| 387 | } |
||
| 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 | * Enqueues the AMP specific sharing styles for the sharing icons. |
||
| 405 | */ |
||
| 406 | public static function amp_enqueue_sharing_css() { |
||
| 407 | if ( self::is_amp_request() ) { |
||
| 408 | wp_enqueue_style( 'sharedaddy-amp', plugin_dir_url( dirname( __FILE__ ) ) . 'modules/sharedaddy/amp-sharing.css', array( 'social-logos' ), JETPACK__VERSION ); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Ensure proper Photon image dimensions for AMP Stories. |
||
| 414 | * |
||
| 415 | * @param array $args Array of Photon Arguments. |
||
| 416 | * @param array $details { |
||
| 417 | * Array of image details. |
||
| 418 | * |
||
| 419 | * @type string $tag Image tag (Image HTML output). |
||
| 420 | * @type string $src Image URL. |
||
| 421 | * @type string $src_orig Original Image URL. |
||
| 422 | * @type int|false $width Image width. |
||
| 423 | * @type int|false $height Image height. |
||
| 424 | * @type int|false $width_orig Original image width before constrained by content_width. |
||
| 425 | * @type int|false $height_orig Original Image height before constrained by content_width. |
||
| 426 | * @type string $transform_orig Original transform before constrained by content_width. |
||
| 427 | * } |
||
| 428 | * @return array Args. |
||
| 429 | */ |
||
| 430 | public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Adds amp-options to the list of options to sync, if AMP is available |
||
| 482 | * |
||
| 483 | * @param array $options_whitelist Whitelist of options to sync. |
||
| 484 | * @return array Updated options whitelist |
||
| 485 | */ |
||
| 486 | public static function filter_jetpack_options_whitelist( $options_whitelist ) { |
||
| 492 | } |
||
| 493 | |||
| 497 |