Changeset 5137
- Timestamp:
- 03/09/2017 09:18:35 PM (8 years ago)
- Location:
- sites/trunk/api.wordpress.org/public_html/events/1.0
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/api.wordpress.org/public_html/events/1.0/index.php
r5128 r5137 183 183 } 184 184 185 /** 186 * Determine a location for the given IPv4 address 187 * 188 * @todo - Add support for IPv6 addresses. Otherwise, this will quickly lose effectiveness. As of March 2017, IPv6 189 * adoption is at 16% globally and rising relatively fast. Some countries are as high as 30%. 190 * See https://www.google.com/intl/en/ipv6/statistics.html#tab=ipv6-adoption for current stats. 191 * 192 * @param string $dotted_ip 193 * 194 * @return null|object `null` on failure; an object on success 195 */ 185 196 function guess_location_from_ip( $dotted_ip ) { 186 197 global $wpdb; … … 203 214 // For a country request, no lat/long are returned. 204 215 if ( isset( $args['country'] ) ) { 205 returnarray(216 $location = array( 206 217 'country' => $args['country'], 207 218 ); … … 209 220 210 221 $country_code = null; 211 if ( isset( $args['locale'] ) && preg_match( '/^[a-z]+[-_]([a-z]+)$/i', $args['locale'], $match) ) {222 if ( ! $location && ( isset( $args['locale'] ) && preg_match( '/^[a-z]+[-_]([a-z]+)$/i', $args['locale'], $match ) ) ) { 212 223 $country_code = $match[1]; 213 224 } 214 225 215 226 // Location was provided by the user: 216 if ( isset( $args['location_name'] ) ) {227 if ( ! $location && isset( $args['location_name'] ) ) { 217 228 $guess = guess_location_from_city( $args['location_name'], $args['timezone'] ?? '', $country_code ); 218 229 219 230 if ( $guess ) { 220 returnarray(231 $location = array( 221 232 'description' => $guess->name, 222 233 'latitude' => $guess->latitude, … … 224 235 'country' => $guess->country, 225 236 ); 226 } 227 228 $guess = guess_location_from_country( $args['location_name'] ); 237 } else { 238 $guess = guess_location_from_country( $args['location_name'] ); 239 240 if ( ! $location && $guess ) { 241 $location = array( 242 'country' => $guess, 243 ); 244 } 245 } 246 } 247 248 // IP: 249 if ( ! $location && isset( $args['ip'] ) ) { 250 $guess = guess_location_from_ip( $args['ip'] ); 229 251 230 252 if ( $guess ) { 231 return array( 232 'country' => $guess, 233 ); 234 } 235 } 236 237 // IP: 238 if ( isset( $args['ip'] ) ) { 239 $guess = guess_location_from_ip( $args['ip'] ); 240 if ( $guess ) { 241 return array( 253 $location = array( 242 254 'description' => $guess->ip_city, 243 255 'latitude' => $guess->ip_latitude, … … 249 261 250 262 if ( 251 ! empty( $args['latitude'] ) && is_numeric( $args['latitude'] ) && 252 ! empty( $args['longitude'] ) && is_numeric( $args['longitude'] ) 263 ! $location && ( 264 ! empty( $args['latitude'] ) && is_numeric( $args['latitude'] ) && 265 ! empty( $args['longitude'] ) && is_numeric( $args['longitude'] ) 266 ) 253 267 ) { 254 268 $city = get_city_from_coordinates( $args['latitude'], $args['longitude'] ); 255 269 256 returnarray(270 $location = array( 257 271 'description' => $city ? $city : "{$args['latitude']}, {$args['longitude']}", 258 272 'latitude' => $args['latitude'], … … 261 275 } 262 276 263 // If any of these are specified, and no localitity was guessed based on the above checks, bail with no location. 264 if ( isset( $args['location_name'] ) || isset( $args['ip'] ) || ! empty( $args['latitude'] ) || ! empty( $args['longitude'] ) ) { 265 return false; 266 } 267 268 // No specific location details. 269 return array(); 277 if ( ! $location ) { 278 if ( isset( $args['location_name'] ) || isset( $args['ip'] ) || ! empty( $args['latitude'] ) || ! empty( $args['longitude'] ) ) { 279 // If any of these are specified, and no localitity was guessed based on the above checks, bail with no location. 280 $location = false; 281 } else { 282 // No specific location details. 283 $location = array(); 284 } 285 } 286 287 return $location; 270 288 } 271 289 -
sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php
r5128 r5137 156 156 ), 157 157 158 /* 159 * No input was provided 160 */ 161 'input-empty' => array( 162 'input' => array(), 163 'expected' => array(), 164 ), 165 158 166 159 167 /*
Note: See TracChangeset
for help on using the changeset viewer.