| Conditions | 22 |
| Paths | > 20000 |
| Total Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 120 | function render_static_map_image_block( $attr, $content, $access_token_key ) { |
||
| 121 | // Set default and passed in values to be used in generating the image url. |
||
| 122 | $width = 1000; |
||
| 123 | $height = isset( $attr['mapHeight'] ) && is_numeric( $attr['mapHeight'] ) ? $attr['mapHeight'] : 400; |
||
| 124 | $zoom = isset( $attr['zoom'] ) && is_numeric( $attr['zoom'] ) ? $attr['zoom'] : 13; |
||
| 125 | $bearing = 0; |
||
| 126 | $longitude = is_numeric( $attr['mapCenter']['lng'] ) ? $attr['mapCenter']['lng'] : -122.41941550000001; |
||
| 127 | $latitude = is_numeric( $attr['mapCenter']['lat'] ) ? $attr['mapCenter']['lat'] : 37.7749295; |
||
| 128 | $show_streets = isset( $attr['mapDetails'] ) && false === $attr['mapDetails'] ? false : true; |
||
| 129 | $marker_color = 'ff0000'; // Default to bright red. |
||
| 130 | |||
| 131 | // Use custom marker color if provided colour is a valid hex code. |
||
| 132 | if ( isset( $attr['markerColor'] ) ) { |
||
| 133 | $stripped_color = str_replace( '#', '', $attr['markerColor'] ); |
||
| 134 | if ( \ctype_xdigit( $stripped_color ) ) { |
||
| 135 | $marker_color = $stripped_color; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | // Generate slug for all markers on the map. |
||
| 140 | $markers_slug = ''; |
||
| 141 | if ( ! empty( $attr['points'] ) ) { |
||
| 142 | foreach ( $attr['points'] as $point ) { |
||
| 143 | $marker = empty( $markers_slug ) ? '' : ','; |
||
| 144 | $marker .= 'pin-s+' . $marker_color; |
||
| 145 | if ( |
||
| 146 | is_numeric( $point['coordinates']['longitude'] ) && |
||
| 147 | is_numeric( $point['coordinates']['latitude'] ) |
||
| 148 | ) { |
||
| 149 | $marker .= sprintf( |
||
| 150 | '(%s,%s)', |
||
| 151 | $point['coordinates']['longitude'], |
||
| 152 | $point['coordinates']['latitude'] |
||
| 153 | ); |
||
| 154 | $markers_slug .= $marker; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | if ( ! empty( $markers_slug ) ) { |
||
| 159 | $markers_slug .= '/'; |
||
| 160 | } |
||
| 161 | |||
| 162 | // Set the type of map or map style, known in the Static Image API as an overlay. |
||
| 163 | // Default to basic / street overlay. |
||
| 164 | $overlay = 'streets-v11'; |
||
| 165 | if ( isset( $attr['className'] ) ) { |
||
| 166 | if ( 'is-style-satellite' === $attr['className'] ) { |
||
| 167 | if ( $show_streets ) { |
||
| 168 | $overlay = 'satellite-streets-v11'; |
||
| 169 | } else { |
||
| 170 | $overlay = 'satellite-v9'; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | if ( 'is-style-black_and_white' === $attr['className'] ) { |
||
| 175 | $overlay = 'light-v10'; |
||
| 176 | } |
||
| 177 | |||
| 178 | if ( 'is-style-terrain' === $attr['className'] ) { |
||
| 179 | $overlay = 'outdoors-v11'; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | // Generate a Mapbox Image API URL in the appropriate format: |
||
| 184 | // https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/pin-s+555555(-77,52),pin-s+555555(-77.5,54)/-77.25,53.0116,6,0/1000x4002x?access_token=YOUR_MAPBOX_ACCESS_TOKEN. |
||
| 185 | |||
| 186 | $url_base = 'https://api.mapbox.com/styles/v1/mapbox'; |
||
| 187 | $url_with_paths = "{$url_base}/{$overlay}/static/{$markers_slug}{$longitude},{$latitude},{$zoom},{$bearing}/{$width}x{$height}@2x"; |
||
| 188 | |||
| 189 | $url = add_query_arg( |
||
| 190 | array( |
||
| 191 | 'access_token' => $access_token_key, |
||
| 192 | ), |
||
| 193 | $url_with_paths |
||
| 194 | ); |
||
| 195 | |||
| 196 | return '<div class="wp-block-jetpack-map--static_image"><img src="' . $url . '" /></div>'; |
||
| 197 | } |
||
| 198 | |||
| 261 |
If you suppress an error, we recommend checking for the error condition explicitly: