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 ) { |
||
| 18 | $images = array(); |
||
| 19 | |||
| 20 | $post = get_post( $post_id ); |
||
| 21 | |||
| 22 | if ( ! $post ) { |
||
| 23 | return $images; |
||
| 24 | } |
||
| 25 | |||
| 26 | if ( ! empty( $post->post_password ) ) { |
||
| 27 | return $images; |
||
| 28 | } |
||
| 29 | |||
| 30 | if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) { |
||
| 31 | return $images; // no slideshow - bail |
||
| 32 | } |
||
| 33 | |||
| 34 | $permalink = get_permalink( $post->ID ); |
||
| 35 | |||
| 36 | // Mechanic: Somebody set us up the bomb |
||
| 37 | $old_post = $GLOBALS['post']; |
||
| 38 | $GLOBALS['post'] = $post; |
||
| 39 | $old_shortcodes = $GLOBALS['shortcode_tags']; |
||
| 40 | $GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] ); |
||
| 41 | |||
| 42 | // Find all the slideshows |
||
| 43 | preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER ); |
||
| 44 | |||
| 45 | ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that |
||
| 46 | |||
| 47 | foreach ( $slideshow_matches as $slideshow_match ) { |
||
|
|
|||
| 48 | $slideshow = do_shortcode_tag( $slideshow_match ); |
||
| 49 | if ( false === $pos = stripos( $slideshow, 'jetpack-slideshow' ) ) { // must be something wrong - or we changed the output format in which case none of the following will work |
||
| 50 | continue; |
||
| 51 | } |
||
| 52 | $start = strpos( $slideshow, '[', $pos ); |
||
| 53 | $end = strpos( $slideshow, ']', $start ); |
||
| 54 | $post_images = json_decode( wp_specialchars_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ), ENT_QUOTES ) ); // parse via JSON |
||
| 55 | // If the JSON didn't decode don't try and act on it. |
||
| 56 | if ( is_array( $post_images ) ) { |
||
| 57 | foreach ( $post_images as $post_image ) { |
||
| 58 | if ( ! $post_image_id = absint( $post_image->id ) ) { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | |||
| 62 | $meta = wp_get_attachment_metadata( $post_image_id ); |
||
| 63 | |||
| 64 | // Must be larger than 200x200 (or user-specified) |
||
| 65 | if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) { |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | $url = wp_get_attachment_url( $post_image_id ); |
||
| 73 | |||
| 74 | $images[] = array( |
||
| 75 | 'type' => 'image', |
||
| 76 | 'from' => 'slideshow', |
||
| 77 | 'src' => $url, |
||
| 78 | 'src_width' => $meta['width'], |
||
| 79 | 'src_height' => $meta['height'], |
||
| 80 | 'href' => $permalink, |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | ob_end_clean(); |
||
| 86 | |||
| 87 | // Operator: Main screen turn on |
||
| 88 | $GLOBALS['shortcode_tags'] = $old_shortcodes; |
||
| 89 | $GLOBALS['post'] = $old_post; |
||
| 90 | |||
| 91 | return $images; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * If a gallery is detected, then get all the images from it. |
||
| 96 | */ |
||
| 97 | static function from_gallery( $post_id ) { |
||
| 98 | $images = array(); |
||
| 99 | |||
| 100 | $post = get_post( $post_id ); |
||
| 101 | |||
| 102 | if ( ! $post ) { |
||
| 103 | return $images; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ( ! empty( $post->post_password ) ) { |
||
| 107 | return $images; |
||
| 108 | } |
||
| 109 | |||
| 110 | $permalink = get_permalink( $post->ID ); |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Juggle global post object because the gallery shortcode uses the |
||
| 114 | * global object. |
||
| 115 | * |
||
| 116 | * See core ticket: |
||
| 117 | * https://core.trac.wordpress.org/ticket/39304 |
||
| 118 | */ |
||
| 119 | if ( isset( $GLOBALS['post'] ) ) { |
||
| 120 | $juggle_post = $GLOBALS['post']; |
||
| 121 | $GLOBALS['post'] = $post; |
||
| 122 | $galleries = get_post_galleries( $post->ID, false ); |
||
| 123 | $GLOBALS['post'] = $juggle_post; |
||
| 124 | } else { |
||
| 125 | $GLOBALS['post'] = $post; |
||
| 126 | $galleries = get_post_galleries( $post->ID, false ); |
||
| 127 | unset( $GLOBALS['post'] ); |
||
| 128 | } |
||
| 129 | |||
| 130 | foreach ( $galleries as $gallery ) { |
||
| 131 | if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] && ! empty( $gallery['ids'] ) ) { |
||
| 132 | $image_ids = explode( ',', $gallery['ids'] ); |
||
| 133 | $image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail'; |
||
| 134 | foreach ( $image_ids as $image_id ) { |
||
| 135 | $image = wp_get_attachment_image_src( $image_id, $image_size ); |
||
| 136 | View Code Duplication | if ( ! empty( $image[0] ) ) { |
|
| 137 | list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250) |
||
| 138 | $raw_src = wp_specialchars_decode( $raw_src ); // rawify it |
||
| 139 | $raw_src = esc_url_raw( $raw_src ); // clean it |
||
| 140 | $images[] = array( |
||
| 141 | 'type' => 'image', |
||
| 142 | 'from' => 'gallery', |
||
| 143 | 'src' => $raw_src, |
||
| 144 | 'href' => $permalink, |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | View Code Duplication | } elseif ( ! empty( $gallery['src'] ) ) { |
|
| 149 | foreach ( $gallery['src'] as $src ) { |
||
| 150 | list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250) |
||
| 151 | $raw_src = wp_specialchars_decode( $raw_src ); // rawify it |
||
| 152 | $raw_src = esc_url_raw( $raw_src ); // clean it |
||
| 153 | $images[] = array( |
||
| 154 | 'type' => 'image', |
||
| 155 | 'from' => 'gallery', |
||
| 156 | 'src' => $raw_src, |
||
| 157 | 'href' => $permalink, |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | return $images; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get attachment images for a specified post and return them. Also make sure |
||
| 168 | * their dimensions are at or above a required minimum. |
||
| 169 | */ |
||
| 170 | static function from_attachment( $post_id, $width = 200, $height = 200 ) { |
||
| 171 | $images = array(); |
||
| 172 | |||
| 173 | $post = get_post( $post_id ); |
||
| 174 | |||
| 175 | if ( ! empty( $post->post_password ) ) { |
||
| 176 | return $images; |
||
| 177 | } |
||
| 178 | |||
| 179 | $post_images = get_posts( |
||
| 180 | array( |
||
| 181 | 'post_parent' => $post_id, // Must be children of post |
||
| 182 | 'numberposts' => 5, // No more than 5 |
||
| 183 | 'post_type' => 'attachment', // Must be attachments |
||
| 184 | 'post_mime_type' => 'image', // Must be images |
||
| 185 | 'suppress_filters' => false, |
||
| 186 | ) |
||
| 187 | ); |
||
| 188 | |||
| 189 | if ( ! $post_images ) { |
||
| 190 | return $images; |
||
| 191 | } |
||
| 192 | |||
| 193 | $permalink = get_permalink( $post_id ); |
||
| 194 | |||
| 195 | foreach ( $post_images as $post_image ) { |
||
| 196 | $current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height ); |
||
| 197 | if ( false !== $current_image ) { |
||
| 198 | $images[] = $current_image; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /* |
||
| 203 | * We only want to pass back attached images that were actually inserted. |
||
| 204 | * We can load up all the images found in the HTML source and then |
||
| 205 | * compare URLs to see if an image is attached AND inserted. |
||
| 206 | */ |
||
| 207 | $html_images = self::from_html( $post_id ); |
||
| 208 | $inserted_images = array(); |
||
| 209 | |||
| 210 | foreach ( $html_images as $html_image ) { |
||
| 211 | $src = wp_parse_url( $html_image['src'] ); |
||
| 212 | // strip off any query strings from src |
||
| 213 | if ( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) { |
||
| 214 | $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path']; |
||
| 215 | } elseif ( ! empty( $src['host'] ) ) { |
||
| 216 | $inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] ); |
||
| 217 | } else { |
||
| 218 | $inserted_images[] = site_url( '/' ) . $src['path']; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | foreach ( $images as $i => $image ) { |
||
| 222 | if ( ! in_array( $image['src'], $inserted_images ) ) { |
||
| 223 | unset( $images[ $i ] ); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | return $images; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Check if a Featured Image is set for this post, and return it in a similar |
||
| 232 | * format to the other images?_from_*() methods. |
||
| 233 | * |
||
| 234 | * @param int $post_id The post ID to check |
||
| 235 | * @return array containing details of the Featured Image, or empty array if none. |
||
| 236 | */ |
||
| 237 | static function from_thumbnail( $post_id, $width = 200, $height = 200 ) { |
||
| 238 | $images = array(); |
||
| 239 | |||
| 240 | $post = get_post( $post_id ); |
||
| 241 | |||
| 242 | if ( ! empty( $post->post_password ) ) { |
||
| 243 | return $images; |
||
| 244 | } |
||
| 245 | |||
| 246 | if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) { |
||
| 247 | $thumb = $post_id; |
||
| 248 | } else { |
||
| 249 | $thumb = get_post_thumbnail_id( $post ); |
||
| 250 | } |
||
| 251 | |||
| 252 | if ( $thumb ) { |
||
| 253 | $meta = wp_get_attachment_metadata( $thumb ); |
||
| 254 | // Must be larger than requested minimums |
||
| 255 | if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) { |
||
| 256 | return $images; |
||
| 257 | } |
||
| 258 | if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) { |
||
| 259 | return $images; |
||
| 260 | } |
||
| 261 | |||
| 262 | $too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) ); |
||
| 263 | |||
| 264 | if ( |
||
| 265 | $too_big && |
||
| 266 | ( |
||
| 267 | ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) || |
||
| 268 | ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
||
| 269 | ) |
||
| 270 | ) { |
||
| 271 | $img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) ); |
||
| 272 | } else { |
||
| 273 | $img_src = wp_get_attachment_image_src( $thumb, 'full' ); |
||
| 274 | } |
||
| 275 | if ( ! is_array( $img_src ) ) { |
||
| 276 | // If wp_get_attachment_image_src returns false but we know that there should be an image that could be used. |
||
| 277 | // we try a bit harder and user the data that we have. |
||
| 278 | $thumb_post_data = get_post( $thumb ); |
||
| 279 | $img_src = array( $thumb_post_data->guid, $meta['width'], $meta['height'] ); |
||
| 280 | } |
||
| 281 | |||
| 282 | $url = $img_src[0]; |
||
| 283 | $images = array( |
||
| 284 | array( // Other methods below all return an array of arrays |
||
| 285 | 'type' => 'image', |
||
| 286 | 'from' => 'thumbnail', |
||
| 287 | 'src' => $url, |
||
| 288 | 'src_width' => $img_src[1], |
||
| 289 | 'src_height' => $img_src[2], |
||
| 290 | 'href' => get_permalink( $thumb ), |
||
| 291 | 'alt_text' => self::get_alt_text( $thumb ), |
||
| 292 | ), |
||
| 293 | ); |
||
| 294 | |||
| 295 | } |
||
| 296 | |||
| 297 | if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
||
| 298 | $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true ); |
||
| 299 | if ( ! empty( $meta_thumbnail ) ) { |
||
| 300 | if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) { |
||
| 301 | return $images; |
||
| 302 | } |
||
| 303 | |||
| 304 | if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) { |
||
| 305 | return $images; |
||
| 306 | } |
||
| 307 | |||
| 308 | $images = array( |
||
| 309 | array( // Other methods below all return an array of arrays |
||
| 310 | 'type' => 'image', |
||
| 311 | 'from' => 'thumbnail', |
||
| 312 | 'src' => $meta_thumbnail['URL'], |
||
| 313 | 'src_width' => $meta_thumbnail['width'], |
||
| 314 | 'src_height' => $meta_thumbnail['height'], |
||
| 315 | 'href' => $meta_thumbnail['URL'], |
||
| 316 | 'alt_text' => self::get_alt_text( $thumb ), |
||
| 317 | ), |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | return $images; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get images from Gutenberg Image blocks. |
||
| 327 | * |
||
| 328 | * @since 6.9.0 |
||
| 329 | * |
||
| 330 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 331 | * @param int $width Minimum Image width. |
||
| 332 | * @param int $height Minimum Image height. |
||
| 333 | */ |
||
| 334 | public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) { |
||
| 335 | $images = array(); |
||
| 336 | |||
| 337 | $html_info = self::get_post_html( $html_or_id ); |
||
| 338 | |||
| 339 | if ( empty( $html_info['html'] ) ) { |
||
| 340 | return $images; |
||
| 341 | } |
||
| 342 | |||
| 343 | // Look for block information in the HTML. |
||
| 344 | $blocks = parse_blocks( $html_info['html'] ); |
||
| 345 | if ( empty( $blocks ) ) { |
||
| 346 | return $images; |
||
| 347 | } |
||
| 348 | |||
| 349 | /* |
||
| 350 | * Let's loop through our blocks. |
||
| 351 | * Some blocks may include some other blocks. Let's go 2 levels deep to look for blocks |
||
| 352 | * that we support and that may include images (see get_images_from_block) |
||
| 353 | * |
||
| 354 | * @to-do: instead of looping manually (that's a lot of if and loops), search recursively instead. |
||
| 355 | */ |
||
| 356 | foreach ( $blocks as $block ) { |
||
| 357 | if ( ! self::is_nested_block( $block ) || 'core/media-text' === $block['blockName'] ) { |
||
| 358 | $images = self::get_images_from_block( $images, $block, $html_info, $width, $height ); |
||
| 359 | } else { |
||
| 360 | foreach ( $block['innerBlocks'] as $inner_block ) { |
||
| 361 | if ( ! self::is_nested_block( $inner_block ) ) { |
||
| 362 | $images = self::get_images_from_block( $images, $inner_block, $html_info, $width, $height ); |
||
| 363 | } else { |
||
| 364 | foreach ( $inner_block['innerBlocks'] as $inner_inner_block ) { |
||
| 365 | $images = self::get_images_from_block( $images, $inner_inner_block, $html_info, $width, $height ); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Returning a filtered array because get_attachment_data returns false |
||
| 374 | * for unsuccessful attempts. |
||
| 375 | */ |
||
| 376 | return array_filter( $images ); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Very raw -- just parse the HTML and pull out any/all img tags and return their src |
||
| 381 | * |
||
| 382 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 383 | * @param int $width Minimum Image width. |
||
| 384 | * @param int $height Minimum Image height. |
||
| 385 | * |
||
| 386 | * @uses DOMDocument |
||
| 387 | * |
||
| 388 | * @return array containing images |
||
| 389 | */ |
||
| 390 | static function from_html( $html_or_id, $width = 200, $height = 200 ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param int $post_id The post ID to check |
||
| 474 | * @param int $size |
||
| 475 | * @return array containing details of the image, or empty array if none. |
||
| 476 | */ |
||
| 477 | static function from_blavatar( $post_id, $size = 96 ) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Gets a post image from the author avatar. |
||
| 511 | * |
||
| 512 | * @param int $post_id The post ID to check. |
||
| 513 | * @param int $size The size of the avatar to get. |
||
| 514 | * @param string $default The default image to use. |
||
| 515 | * @return array containing details of the image, or empty array if none. |
||
| 516 | */ |
||
| 517 | static function from_gravatar( $post_id, $size = 96, $default = false ) { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Run through the different methods that we have available to try to find a single good |
||
| 551 | * display image for this post. |
||
| 552 | * |
||
| 553 | * @param int $post_id |
||
| 554 | * @param array $args Other arguments (currently width and height required for images where possible to determine) |
||
| 555 | * @return array containing details of the best image to be used |
||
| 556 | */ |
||
| 557 | static function get_image( $post_id, $args = array() ) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Get an array containing a collection of possible images for this post, stopping once we hit a method |
||
| 593 | * that returns something useful. |
||
| 594 | * |
||
| 595 | * @param int $post_id |
||
| 596 | * @param array $args Optional args, see defaults list for details |
||
| 597 | * @return array containing images that would be good for representing this post |
||
| 598 | */ |
||
| 599 | static function get_images( $post_id, $args = array() ) { |
||
| 600 | // Figure out which image to attach to this post. |
||
| 601 | $media = false; |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Filters the array of images that would be good for a specific post. |
||
| 605 | * This filter is applied before options ($args) filter the original array. |
||
| 606 | * |
||
| 607 | * @since 2.0.0 |
||
| 608 | * |
||
| 609 | * @param array $media Array of images that would be good for a specific post. |
||
| 610 | * @param int $post_id Post ID. |
||
| 611 | * @param array $args Array of options to get images. |
||
| 612 | */ |
||
| 613 | $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args ); |
||
| 614 | if ( $media ) { |
||
| 615 | return $media; |
||
| 616 | } |
||
| 617 | |||
| 618 | $defaults = array( |
||
| 619 | 'width' => 200, // Required minimum width (if possible to determine) |
||
| 620 | 'height' => 200, // Required minimum height (if possible to determine) |
||
| 621 | |||
| 622 | 'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack |
||
| 623 | 'avatar_size' => 96, // Used for both Grav and Blav |
||
| 624 | 'gravatar_default' => false, // Default image to use if we end up with no Gravatar |
||
| 625 | |||
| 626 | 'from_thumbnail' => true, // Use these flags to specify which methods to use to find an image |
||
| 627 | 'from_slideshow' => true, |
||
| 628 | 'from_gallery' => true, |
||
| 629 | 'from_attachment' => true, |
||
| 630 | 'from_blocks' => true, |
||
| 631 | 'from_html' => true, |
||
| 632 | |||
| 633 | 'html_content' => '', // HTML string to pass to from_html() |
||
| 634 | ); |
||
| 635 | $args = wp_parse_args( $args, $defaults ); |
||
| 636 | |||
| 637 | $media = false; |
||
| 638 | if ( $args['from_thumbnail'] ) { |
||
| 639 | $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] ); |
||
| 640 | } |
||
| 641 | View Code Duplication | if ( ! $media && $args['from_slideshow'] ) { |
|
| 642 | $media = self::from_slideshow( $post_id, $args['width'], $args['height'] ); |
||
| 643 | } |
||
| 644 | if ( ! $media && $args['from_gallery'] ) { |
||
| 645 | $media = self::from_gallery( $post_id ); |
||
| 646 | } |
||
| 647 | View Code Duplication | if ( ! $media && $args['from_attachment'] ) { |
|
| 648 | $media = self::from_attachment( $post_id, $args['width'], $args['height'] ); |
||
| 649 | } |
||
| 650 | View Code Duplication | if ( ! $media && $args['from_blocks'] ) { |
|
| 651 | if ( empty( $args['html_content'] ) ) { |
||
| 652 | $media = self::from_blocks( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content |
||
| 653 | } else { |
||
| 654 | $media = self::from_blocks( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that |
||
| 655 | } |
||
| 656 | } |
||
| 657 | View Code Duplication | if ( ! $media && $args['from_html'] ) { |
|
| 658 | if ( empty( $args['html_content'] ) ) { |
||
| 659 | $media = self::from_html( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content |
||
| 660 | } else { |
||
| 661 | $media = self::from_html( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that |
||
| 662 | } |
||
| 663 | } |
||
| 664 | |||
| 665 | if ( ! $media && $args['fallback_to_avatars'] ) { |
||
| 666 | $media = self::from_blavatar( $post_id, $args['avatar_size'] ); |
||
| 667 | if ( ! $media ) { |
||
| 668 | $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] ); |
||
| 669 | } |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Filters the array of images that would be good for a specific post. |
||
| 674 | * This filter is applied after options ($args) filter the original array. |
||
| 675 | * |
||
| 676 | * @since 2.0.0 |
||
| 677 | * |
||
| 678 | * @param array $media Array of images that would be good for a specific post. |
||
| 679 | * @param int $post_id Post ID. |
||
| 680 | * @param array $args Array of options to get images. |
||
| 681 | */ |
||
| 682 | return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args ); |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Takes an image URL and pixel dimensions then returns a URL for the |
||
| 687 | * resized and cropped image. |
||
| 688 | * |
||
| 689 | * @param string $src |
||
| 690 | * @param int $dimension |
||
| 691 | * @return string Transformed image URL |
||
| 692 | */ |
||
| 693 | static function fit_image_url( $src, $width, $height ) { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Get HTML from given post content. |
||
| 739 | * |
||
| 740 | * @since 6.9.0 |
||
| 741 | * |
||
| 742 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 743 | * |
||
| 744 | * @return array $html_info { |
||
| 745 | * @type string $html Post content. |
||
| 746 | * @type string $post_url Post URL. |
||
| 747 | * } |
||
| 748 | */ |
||
| 749 | static function get_post_html( $html_or_id ) { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Get info about a WordPress attachment. |
||
| 772 | * |
||
| 773 | * @since 6.9.0 |
||
| 774 | * |
||
| 775 | * @param int $attachment_id Attachment ID. |
||
| 776 | * @param string $post_url URL of the post, if we have one. |
||
| 777 | * @param int $width Minimum Image width. |
||
| 778 | * @param int $height Minimum Image height. |
||
| 779 | * @return array|bool Image data or false if unavailable. |
||
| 780 | */ |
||
| 781 | public static function get_attachment_data( $attachment_id, $post_url = '', $width, $height ) { |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Get the alt text for an image or other media from the Media Library. |
||
| 833 | * |
||
| 834 | * @since 7.1 |
||
| 835 | * |
||
| 836 | * @param int $attachment_id The Post ID of the media. |
||
| 837 | * @return string The alt text value or an emptry string. |
||
| 838 | */ |
||
| 839 | public static function get_alt_text( $attachment_id ) { |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Get an image from a block. |
||
| 845 | * |
||
| 846 | * @since 7.8.0 |
||
| 847 | * |
||
| 848 | * @param array $images Images found. |
||
| 849 | * @param array $block Block and its attributes. |
||
| 850 | * @param array $html_info Info about the post where the block is found. |
||
| 851 | * @param int $width Desired image width. |
||
| 852 | * @param int $height Desired image height. |
||
| 853 | * |
||
| 854 | * @return array Array of images found. |
||
| 855 | */ |
||
| 856 | private static function get_images_from_block( $images, $block, $html_info, $width, $height ) { |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Check if a block has inner blocks. |
||
| 903 | * |
||
| 904 | * @since 7.8.0 |
||
| 905 | * |
||
| 906 | * @param array $block Block and its attributes. |
||
| 907 | * |
||
| 908 | * @return bool |
||
| 909 | */ |
||
| 910 | private static function is_nested_block( $block ) { |
||
| 917 | } |
||
| 918 |
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.