| Conditions | 19 |
| Paths | 3 |
| Total Lines | 105 |
| Lines | 32 |
| Ratio | 30.48 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 42 | public static function render( $attr, $content ) { |
||
| 43 | Jetpack_Gutenberg::load_assets_as_required( 'tiled-gallery' ); |
||
| 44 | |||
| 45 | $is_squareish_layout = self::is_squareish_layout( $attr ); |
||
| 46 | |||
| 47 | if ( preg_match_all( '/<img [^>]+>/', $content, $images ) ) { |
||
| 48 | /** |
||
| 49 | * This block processes all of the images that are found and builds $find and $replace. |
||
| 50 | * |
||
| 51 | * The original img is added to the $find array and the replacement is made and added |
||
| 52 | * to the $replace array. This is so that the same find and replace operations can be |
||
| 53 | * made on the entire $content. |
||
| 54 | */ |
||
| 55 | $find = array(); |
||
| 56 | $replace = array(); |
||
| 57 | |||
| 58 | foreach ( $images[0] as $image_html ) { |
||
| 59 | if ( |
||
| 60 | preg_match( '/data-width="([0-9]+)"/', $image_html, $img_height ) |
||
| 61 | && preg_match( '/data-height="([0-9]+)"/', $image_html, $img_width ) |
||
| 62 | && preg_match( '/src="([^"]+)"/', $image_html, $img_src ) |
||
| 63 | ) { |
||
| 64 | // Drop img src query string so it can be used as a base to add photon params |
||
| 65 | // for the srcset. |
||
| 66 | $src_parts = explode( '?', $img_src[1], 2 ); |
||
| 67 | $orig_src = $src_parts[0]; |
||
| 68 | $orig_height = absint( $img_height[1] ); |
||
| 69 | $orig_width = absint( $img_width[1] ); |
||
| 70 | |||
| 71 | // Because URLs are already "photon", the photon function used short-circuits |
||
| 72 | // before ssl is added. Detect ssl and add is if necessary. |
||
| 73 | $is_ssl = ! empty( $src_parts[1] ) && false !== strpos( $src_parts[1], 'ssl=1' ); |
||
| 74 | |||
| 75 | if ( ! $orig_width || ! $orig_height || ! $orig_src ) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | $srcset_parts = array(); |
||
| 80 | if ( $is_squareish_layout ) { |
||
| 81 | $min_width = min( self::IMG_SRCSET_WIDTH_MIN, $orig_width, $orig_height ); |
||
| 82 | $max_width = min( self::IMG_SRCSET_WIDTH_MAX, $orig_width, $orig_height ); |
||
| 83 | |||
| 84 | View Code Duplication | for ( $w = $min_width; $w <= $max_width; $w = min( $max_width, $w + self::IMG_SRCSET_WIDTH_STEP ) ) { |
|
| 85 | $srcset_src = add_query_arg( |
||
| 86 | array( |
||
| 87 | 'resize' => $w . ',' . $w, |
||
| 88 | 'strip' => 'all', |
||
| 89 | ), |
||
| 90 | $orig_src |
||
| 91 | ); |
||
| 92 | if ( $is_ssl ) { |
||
| 93 | $srcset_src = add_query_arg( 'ssl', '1', $srcset_src ); |
||
| 94 | } |
||
| 95 | $srcset_parts[] = esc_url( $srcset_src ) . ' ' . $w . 'w'; |
||
| 96 | if ( $w >= $max_width ) { |
||
| 97 | break; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | $min_width = min( self::IMG_SRCSET_WIDTH_MIN, $orig_width ); |
||
| 102 | $max_width = min( self::IMG_SRCSET_WIDTH_MAX, $orig_width ); |
||
| 103 | |||
| 104 | View Code Duplication | for ( $w = $min_width; $w <= $max_width; $w = min( $max_width, $w + self::IMG_SRCSET_WIDTH_STEP ) ) { |
|
| 105 | $srcset_src = add_query_arg( |
||
| 106 | array( |
||
| 107 | 'strip' => 'all', |
||
| 108 | 'w' => $w, |
||
| 109 | ), |
||
| 110 | $orig_src |
||
| 111 | ); |
||
| 112 | if ( $is_ssl ) { |
||
| 113 | $srcset_src = add_query_arg( 'ssl', '1', $srcset_src ); |
||
| 114 | } |
||
| 115 | $srcset_parts[] = esc_url( $srcset_src ) . ' ' . $w . 'w'; |
||
| 116 | if ( $w >= $max_width ) { |
||
| 117 | break; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | if ( ! empty( $srcset_parts ) ) { |
||
| 123 | $srcset = 'srcset="' . esc_attr( implode( ',', $srcset_parts ) ) . '"'; |
||
| 124 | |||
| 125 | $find[] = $image_html; |
||
| 126 | $replace[] = str_replace( '<img', '<img ' . $srcset, $image_html ); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ( ! empty( $find ) ) { |
||
| 132 | $content = str_replace( $find, $replace, $content ); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Filter the output of the Tiled Galleries content. |
||
| 138 | * |
||
| 139 | * @module tiled-gallery |
||
| 140 | * |
||
| 141 | * @since 6.9.0 |
||
| 142 | * |
||
| 143 | * @param string $content Tiled Gallery block content. |
||
| 144 | */ |
||
| 145 | return apply_filters( 'jetpack_tiled_galleries_block_content', $content ); |
||
| 146 | } |
||
| 147 | |||
| 172 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.