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_PostImages 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_PostImages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Jetpack_PostImages { |
||
| 14 | /** |
||
| 15 | * If a slideshow is embedded within a post, then parse out the images involved and return them |
||
| 16 | */ |
||
| 17 | static function from_slideshow( $post_id, $width = 200, $height = 200 ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * If a gallery is detected, then get all the images from it. |
||
| 92 | */ |
||
| 93 | static function from_gallery( $post_id ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get attachment images for a specified post and return them. Also make sure |
||
| 164 | * their dimensions are at or above a required minimum. |
||
| 165 | */ |
||
| 166 | static function from_attachment( $post_id, $width = 200, $height = 200 ) { |
||
| 167 | $images = array(); |
||
| 168 | |||
| 169 | $post = get_post( $post_id ); |
||
| 170 | |||
| 171 | if ( ! empty( $post->post_password ) ) { |
||
| 172 | return $images; |
||
| 173 | } |
||
| 174 | |||
| 175 | $post_images = get_posts( array( |
||
| 176 | 'post_parent' => $post_id, // Must be children of post |
||
| 177 | 'numberposts' => 5, // No more than 5 |
||
| 178 | 'post_type' => 'attachment', // Must be attachments |
||
| 179 | 'post_mime_type' => 'image', // Must be images |
||
| 180 | 'suppress_filters' => false, |
||
| 181 | ) ); |
||
| 182 | |||
| 183 | if ( ! $post_images ) { |
||
| 184 | return $images; |
||
| 185 | } |
||
| 186 | |||
| 187 | $permalink = get_permalink( $post_id ); |
||
| 188 | |||
| 189 | foreach ( $post_images as $post_image ) { |
||
| 190 | $current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height ); |
||
| 191 | if ( false !== $current_image ) { |
||
| 192 | $images[] = $current_image; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /* |
||
| 197 | * We only want to pass back attached images that were actually inserted. |
||
| 198 | * We can load up all the images found in the HTML source and then |
||
| 199 | * compare URLs to see if an image is attached AND inserted. |
||
| 200 | */ |
||
| 201 | $html_images = self::from_html( $post_id ); |
||
| 202 | $inserted_images = array(); |
||
| 203 | |||
| 204 | foreach( $html_images as $html_image ) { |
||
| 205 | $src = parse_url( $html_image['src'] ); |
||
| 206 | // strip off any query strings from src |
||
| 207 | if( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) { |
||
| 208 | $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path']; |
||
| 209 | } elseif( ! empty( $src['host'] ) ) { |
||
| 210 | $inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] ); |
||
| 211 | } else { |
||
| 212 | $inserted_images[] = site_url( '/' ) . $src['path']; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | foreach( $images as $i => $image ) { |
||
| 216 | if ( !in_array( $image['src'], $inserted_images ) ) |
||
| 217 | unset( $images[$i] ); |
||
| 218 | } |
||
| 219 | |||
| 220 | return $images; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Check if a Featured Image is set for this post, and return it in a similar |
||
| 225 | * format to the other images?_from_*() methods. |
||
| 226 | * @param int $post_id The post ID to check |
||
| 227 | * @return Array containing details of the Featured Image, or empty array if none. |
||
| 228 | */ |
||
| 229 | static function from_thumbnail( $post_id, $width = 200, $height = 200 ) { |
||
| 230 | $images = array(); |
||
| 231 | |||
| 232 | $post = get_post( $post_id ); |
||
| 233 | |||
| 234 | if ( ! empty( $post->post_password ) ) { |
||
| 235 | return $images; |
||
| 236 | } |
||
| 237 | |||
| 238 | if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) { |
||
| 239 | $thumb = $post_id; |
||
| 240 | } else { |
||
| 241 | $thumb = get_post_thumbnail_id( $post ); |
||
| 242 | } |
||
| 243 | |||
| 244 | if ( $thumb ) { |
||
| 245 | $meta = wp_get_attachment_metadata( $thumb ); |
||
| 246 | // Must be larger than requested minimums |
||
| 247 | if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
||
| 248 | return $images; |
||
| 249 | if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
||
| 250 | return $images; |
||
| 251 | |||
| 252 | $too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) ); |
||
| 253 | |||
| 254 | if ( |
||
| 255 | $too_big && |
||
| 256 | ( |
||
| 257 | ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) || |
||
| 258 | ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
||
| 259 | ) |
||
| 260 | ) { |
||
| 261 | $img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) ); |
||
| 262 | } else { |
||
| 263 | $img_src = wp_get_attachment_image_src( $thumb, 'full' ); |
||
| 264 | } |
||
| 265 | if ( ! is_array( $img_src ) ) { |
||
| 266 | // If wp_get_attachment_image_src returns false but we know that there should be an image that could be used. |
||
| 267 | // we try a bit harder and user the data that we have. |
||
| 268 | $thumb_post_data = get_post( $thumb ); |
||
| 269 | $img_src = array( $thumb_post_data->guid, $meta['width'], $meta['height'] ); |
||
| 270 | } |
||
| 271 | |||
| 272 | $url = $img_src[0]; |
||
| 273 | $images = array( array( // Other methods below all return an array of arrays |
||
| 274 | 'type' => 'image', |
||
| 275 | 'from' => 'thumbnail', |
||
| 276 | 'src' => $url, |
||
| 277 | 'src_width' => $img_src[1], |
||
| 278 | 'src_height' => $img_src[2], |
||
| 279 | 'href' => get_permalink( $thumb ), |
||
| 280 | 'alt_text' => self::get_alt_text( $thumb ), |
||
| 281 | ) ); |
||
| 282 | |||
| 283 | } |
||
| 284 | |||
| 285 | if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
||
| 286 | $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true ); |
||
| 287 | if ( ! empty( $meta_thumbnail ) ) { |
||
| 288 | if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) { |
||
| 289 | return $images; |
||
| 290 | } |
||
| 291 | |||
| 292 | if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) { |
||
| 293 | return $images; |
||
| 294 | } |
||
| 295 | |||
| 296 | $images = array( array( // Other methods below all return an array of arrays |
||
| 297 | 'type' => 'image', |
||
| 298 | 'from' => 'thumbnail', |
||
| 299 | 'src' => $meta_thumbnail['URL'], |
||
| 300 | 'src_width' => $meta_thumbnail['width'], |
||
| 301 | 'src_height' => $meta_thumbnail['height'], |
||
| 302 | 'href' => $meta_thumbnail['URL'], |
||
| 303 | 'alt_text' => self::get_alt_text( $thumb ), |
||
| 304 | ) ); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | return $images; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get images from Gutenberg Image blocks. |
||
| 313 | * |
||
| 314 | * @since 6.9.0 |
||
| 315 | * |
||
| 316 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 317 | * @param int $width Minimum Image width. |
||
| 318 | * @param int $height Minimum Image height. |
||
| 319 | */ |
||
| 320 | public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) { |
||
| 321 | $images = array(); |
||
| 322 | |||
| 323 | $html_info = self::get_post_html( $html_or_id ); |
||
| 324 | |||
| 325 | if ( empty( $html_info['html'] ) ) { |
||
| 326 | return $images; |
||
| 327 | } |
||
| 328 | |||
| 329 | // Look for block information in the HTML. |
||
| 330 | $blocks = parse_blocks( $html_info['html'] ); |
||
| 331 | if ( empty( $blocks ) ) { |
||
| 332 | return $images; |
||
| 333 | } |
||
| 334 | |||
| 335 | /* |
||
| 336 | * Let's loop through our blocks. |
||
| 337 | * Some blocks may include some other blocks. Let's go 2 levels deep to look for blocks |
||
| 338 | * that we support and that may include images (see get_images_from_block) |
||
| 339 | * |
||
| 340 | * @to-do: instead of looping manually (that's a lot of if and loops), search recursively instead. |
||
| 341 | */ |
||
| 342 | foreach ( $blocks as $block ) { |
||
| 343 | if ( ! self::is_nested_block( $block ) ) { |
||
| 344 | $images = self::get_images_from_block( $images, $block, $html_info, $width, $height ); |
||
| 345 | } else { |
||
| 346 | foreach ( $block['innerBlocks'] as $inner_block ) { |
||
| 347 | if ( ! self::is_nested_block( $inner_block ) ) { |
||
| 348 | $images = self::get_images_from_block( $images, $inner_block, $html_info, $width, $height ); |
||
| 349 | } else { |
||
| 350 | foreach ( $inner_block['innerBlocks'] as $inner_inner_block ) { |
||
| 351 | $images = self::get_images_from_block( $images, $inner_inner_block, $html_info, $width, $height ); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returning a filtered array because get_attachment_data returns false |
||
| 360 | * for unsuccessful attempts. |
||
| 361 | */ |
||
| 362 | return array_filter( $images ); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Very raw -- just parse the HTML and pull out any/all img tags and return their src |
||
| 367 | * |
||
| 368 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 369 | * @param int $width Minimum Image width. |
||
| 370 | * @param int $height Minimum Image height. |
||
| 371 | * |
||
| 372 | * @uses DOMDocument |
||
| 373 | * |
||
| 374 | * @return Array containing images |
||
| 375 | */ |
||
| 376 | static function from_html( $html_or_id, $width = 200, $height = 200 ) { |
||
| 377 | $images = array(); |
||
| 378 | |||
| 379 | $html_info = self::get_post_html( $html_or_id ); |
||
| 380 | |||
| 381 | if ( empty( $html_info['html'] ) ) { |
||
| 382 | return $images; |
||
| 383 | } |
||
| 384 | |||
| 385 | // Do not go any further if DOMDocument is disabled on the server. |
||
| 386 | if ( ! class_exists( 'DOMDocument' ) ) { |
||
| 387 | return $images; |
||
| 388 | } |
||
| 389 | |||
| 390 | // Let's grab all image tags from the HTML. |
||
| 391 | $dom_doc = new DOMDocument; |
||
| 392 | |||
| 393 | // The @ is not enough to suppress errors when dealing with libxml, |
||
| 394 | // we have to tell it directly how we want to handle errors. |
||
| 395 | libxml_use_internal_errors( true ); |
||
| 396 | @$dom_doc->loadHTML( $html_info['html'] ); |
||
| 397 | libxml_use_internal_errors( false ); |
||
| 398 | |||
| 399 | $image_tags = $dom_doc->getElementsByTagName( 'img' ); |
||
| 400 | |||
| 401 | // For each image Tag, make sure it can be added to the $images array, and add it. |
||
| 402 | foreach ( $image_tags as $image_tag ) { |
||
| 403 | $img_src = $image_tag->getAttribute( 'src' ); |
||
| 404 | |||
| 405 | if ( empty( $img_src ) ) { |
||
| 406 | continue; |
||
| 407 | } |
||
| 408 | |||
| 409 | // Do not grab smiley images that were automatically created by WP when entering text smilies. |
||
| 410 | if ( stripos( $img_src, '/smilies/' ) ) { |
||
| 411 | continue; |
||
| 412 | } |
||
| 413 | |||
| 414 | $meta = array( |
||
| 415 | 'width' => (int) $image_tag->getAttribute( 'width' ), |
||
| 416 | 'height' => (int) $image_tag->getAttribute( 'height' ), |
||
| 417 | 'alt_text' => $image_tag->getAttribute( 'alt' ), |
||
| 418 | ); |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Filters the switch to ignore minimum image size requirements. Can be used |
||
| 422 | * to add custom logic to image dimensions, like only enforcing one of the dimensions, |
||
| 423 | * or disabling it entirely. |
||
| 424 | * |
||
| 425 | * @since 6.4.0 |
||
| 426 | * |
||
| 427 | * @param bool $ignore Should the image dimensions be ignored? |
||
| 428 | * @param array $meta Array containing image dimensions parsed from the markup. |
||
| 429 | */ |
||
| 430 | $ignore_dimensions = apply_filters( 'jetpack_postimages_ignore_minimum_dimensions', false, $meta ); |
||
| 431 | |||
| 432 | // Must be larger than 200x200 (or user-specified). |
||
| 433 | if ( |
||
| 434 | ! $ignore_dimensions |
||
| 435 | && ( |
||
| 436 | empty( $meta['width'] ) |
||
| 437 | || empty( $meta['height'] ) |
||
| 438 | || $meta['width'] < $width |
||
| 439 | || $meta['height'] < $height |
||
| 440 | ) |
||
| 441 | ) { |
||
| 442 | continue; |
||
| 443 | } |
||
| 444 | |||
| 445 | $images[] = array( |
||
| 446 | 'type' => 'image', |
||
| 447 | 'from' => 'html', |
||
| 448 | 'src' => $img_src, |
||
| 449 | 'src_width' => $meta['width'], |
||
| 450 | 'src_height' => $meta['height'], |
||
| 451 | 'href' => $html_info['post_url'], |
||
| 452 | 'alt_text' => $meta['alt_text'], |
||
| 453 | ); |
||
| 454 | } |
||
| 455 | return $images; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param int $post_id The post ID to check |
||
| 460 | * @param int $size |
||
| 461 | * @return Array containing details of the image, or empty array if none. |
||
| 462 | */ |
||
| 463 | static function from_blavatar( $post_id, $size = 96 ) { |
||
| 464 | |||
| 465 | $permalink = get_permalink( $post_id ); |
||
| 466 | |||
| 467 | if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) { |
||
| 468 | $domain = blavatar_domain( $permalink ); |
||
| 469 | |||
| 470 | if ( ! blavatar_exists( $domain ) ) { |
||
| 471 | return array(); |
||
| 472 | } |
||
| 473 | |||
| 474 | $url = blavatar_url( $domain, 'img', $size ); |
||
| 475 | } else { |
||
| 476 | $url = get_site_icon_url( $size ); |
||
| 477 | if ( ! $url ) { |
||
| 478 | return array(); |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | return array( array( |
||
| 483 | 'type' => 'image', |
||
| 484 | 'from' => 'blavatar', |
||
| 485 | 'src' => $url, |
||
| 486 | 'src_width' => $size, |
||
| 487 | 'src_height' => $size, |
||
| 488 | 'href' => $permalink, |
||
| 489 | 'alt_text' => '', |
||
| 490 | ) ); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Gets a post image from the author avatar. |
||
| 495 | * |
||
| 496 | * @param int $post_id The post ID to check. |
||
| 497 | * @param int $size The size of the avatar to get. |
||
| 498 | * @param string $default The default image to use. |
||
| 499 | * @return Array containing details of the image, or empty array if none. |
||
| 500 | */ |
||
| 501 | static function from_gravatar( $post_id, $size = 96, $default = false ) { |
||
| 502 | $post = get_post( $post_id ); |
||
| 503 | $permalink = get_permalink( $post_id ); |
||
| 504 | |||
| 505 | if ( function_exists( 'wpcom_get_avatar_url' ) ) { |
||
| 506 | $url = wpcom_get_avatar_url( $post->post_author, $size, $default, true ); |
||
| 507 | if ( $url && is_array( $url ) ) { |
||
| 508 | $url = $url[0]; |
||
| 509 | } |
||
| 510 | } else { |
||
| 511 | $url = get_avatar_url( $post->post_author, array( |
||
| 512 | 'size' => $size, |
||
| 513 | 'default' => $default, |
||
| 514 | ) ); |
||
| 515 | } |
||
| 516 | |||
| 517 | return array( |
||
| 518 | array( |
||
| 519 | 'type' => 'image', |
||
| 520 | 'from' => 'gravatar', |
||
| 521 | 'src' => $url, |
||
| 522 | 'src_width' => $size, |
||
| 523 | 'src_height' => $size, |
||
| 524 | 'href' => $permalink, |
||
| 525 | 'alt_text' => '', |
||
| 526 | ), |
||
| 527 | ); |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Run through the different methods that we have available to try to find a single good |
||
| 532 | * display image for this post. |
||
| 533 | * @param int $post_id |
||
| 534 | * @param array $args Other arguments (currently width and height required for images where possible to determine) |
||
| 535 | * @return Array containing details of the best image to be used |
||
| 536 | */ |
||
| 537 | static function get_image( $post_id, $args = array() ) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Get an array containing a collection of possible images for this post, stopping once we hit a method |
||
| 574 | * that returns something useful. |
||
| 575 | * @param int $post_id |
||
| 576 | * @param array $args Optional args, see defaults list for details |
||
| 577 | * @return Array containing images that would be good for representing this post |
||
| 578 | */ |
||
| 579 | static function get_images( $post_id, $args = array() ) { |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Takes an image URL and pixel dimensions then returns a URL for the |
||
| 659 | * resized and cropped image. |
||
| 660 | * |
||
| 661 | * @param string $src |
||
| 662 | * @param int $dimension |
||
| 663 | * @return string Transformed image URL |
||
| 664 | */ |
||
| 665 | static function fit_image_url( $src, $width, $height ) { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Get HTML from given post content. |
||
| 704 | * |
||
| 705 | * @since 6.9.0 |
||
| 706 | * |
||
| 707 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 708 | * |
||
| 709 | * @return array $html_info { |
||
| 710 | * @type string $html Post content. |
||
| 711 | * @type string $post_url Post URL. |
||
| 712 | * } |
||
| 713 | */ |
||
| 714 | static function get_post_html( $html_or_id ) { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Get info about a WordPress attachment. |
||
| 737 | * |
||
| 738 | * @since 6.9.0 |
||
| 739 | * |
||
| 740 | * @param int $attachment_id Attachment ID. |
||
| 741 | * @param string $post_url URL of the post, if we have one. |
||
| 742 | * @param int $width Minimum Image width. |
||
| 743 | * @param int $height Minimum Image height. |
||
| 744 | * @return array|bool Image data or false if unavailable. |
||
| 745 | */ |
||
| 746 | public static function get_attachment_data( $attachment_id, $post_url = '', $width, $height ) { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Get the alt text for an image or other media from the Media Library. |
||
| 776 | * |
||
| 777 | * @since 7.1 |
||
| 778 | * |
||
| 779 | * @param int $attachment_id The Post ID of the media. |
||
| 780 | * @return string The alt text value or an emptry string. |
||
| 781 | */ |
||
| 782 | public static function get_alt_text( $attachment_id ) { |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Get an image from a block. |
||
| 788 | * |
||
| 789 | * @since 7.8.0 |
||
| 790 | * |
||
| 791 | * @param array $images Images found. |
||
| 792 | * @param array $block Block and its attributes. |
||
| 793 | * @param array $html_info Info about the post where the block is found. |
||
| 794 | * @param int $width Desired image width. |
||
| 795 | * @param int $height Desired image height. |
||
| 796 | */ |
||
| 797 | private static function get_images_from_block( $images, $block, $html_info, $width, $height ) { |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Check if a block has inner blocks. |
||
| 829 | * |
||
| 830 | * @since 7.8.0 |
||
| 831 | * |
||
| 832 | * @param array $block Block and its attributes. |
||
| 833 | * |
||
| 834 | * @return bool |
||
| 835 | */ |
||
| 836 | private static function is_nested_block( $block ) { |
||
| 843 | } |
||
| 844 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.