| Conditions | 11 |
| Paths | 6 |
| Total Lines | 88 |
| 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 gmb_v2_upgrades() { |
||
| 121 | |||
| 122 | //Set key variables |
||
| 123 | $google_api_key = gmb_get_option( 'gmb_api_key' ); |
||
| 124 | |||
| 125 | //Loop through maps |
||
| 126 | $args = array( |
||
| 127 | 'post_type' => 'google_maps', |
||
| 128 | 'posts_per_page' => - 1 |
||
| 129 | ); |
||
| 130 | |||
| 131 | // The Query |
||
| 132 | $the_query = new WP_Query( $args ); |
||
| 133 | |||
| 134 | // The CPT Loop |
||
| 135 | if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); |
||
| 136 | |||
| 137 | //Repeater markers data |
||
| 138 | $markers = get_post_meta( get_the_ID(), 'gmb_markers_group', true ); |
||
| 139 | |||
| 140 | //If no markers skip |
||
| 141 | if ( ! empty( $markers ) ) { |
||
| 142 | |||
| 143 | //Markers loop |
||
| 144 | foreach ( $markers as $key => $marker ) { |
||
| 145 | |||
| 146 | $ref_id = isset( $marker['reference'] ) ? $marker['reference'] : ''; |
||
| 147 | $place_id = isset( $marker['place_id'] ) ? $marker['place_id'] : ''; |
||
| 148 | |||
| 149 | //No ref ID -> skip; If place_id already there skip |
||
| 150 | if ( empty( $ref_id ) ) { |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | if ( ! empty( $place_id ) ) { |
||
| 154 | continue; |
||
| 155 | } |
||
| 156 | //cURL the Google API for the Google Place ID |
||
| 157 | $google_places_url = add_query_arg( |
||
| 158 | array( |
||
| 159 | 'reference' => $ref_id, |
||
| 160 | 'key' => $google_api_key |
||
| 161 | ), |
||
| 162 | 'https://maps.googleapis.com/maps/api/place/details/json' |
||
| 163 | ); |
||
| 164 | |||
| 165 | $response = wp_remote_get( $google_places_url, |
||
| 166 | array( |
||
| 167 | 'timeout' => 15, |
||
| 168 | 'sslverify' => false |
||
| 169 | ) |
||
| 170 | ); |
||
| 171 | |||
| 172 | // make sure the response came back okay |
||
| 173 | if ( is_wp_error( $response ) ) { |
||
| 174 | return; |
||
| 175 | } |
||
| 176 | |||
| 177 | // decode the license data |
||
| 178 | $response = json_decode( $response['body'], true ); |
||
| 179 | |||
| 180 | //Place ID is there, now let's update the widget data |
||
| 181 | if ( isset( $response['result']['place_id'] ) ) { |
||
| 182 | |||
| 183 | //Add Place ID to markers array |
||
| 184 | $markers[ $key ]['place_id'] = $response['result']['place_id']; |
||
| 185 | |||
| 186 | } |
||
| 187 | |||
| 188 | //Pause for 2 seconds so we don't overwhelm the Google API with requests |
||
| 189 | sleep( 2 ); |
||
| 190 | |||
| 191 | |||
| 192 | } //end foreach |
||
| 193 | |||
| 194 | //Update repeater data with new data |
||
| 195 | update_post_meta( get_the_ID(), 'gmb_markers_group', $markers ); |
||
| 196 | |||
| 197 | } //endif |
||
| 198 | |||
| 199 | endwhile; endif; |
||
| 200 | |||
| 201 | // Reset Post Data |
||
| 202 | wp_reset_postdata(); |
||
| 203 | |||
| 204 | //Update our options and GTF out |
||
| 205 | update_option( 'gmb_refid_upgraded', 'upgraded' ); |
||
| 206 | |||
| 207 | } |
||
| 208 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.