コード例 #1
0
 /**
  * Address validation function
  *
  * Since 1.37.2 extracted from save_property and revalidate_all_addresses to make same functionality
  *
  * @global array  $wp_properties
  *
  * @param integer $post_id
  * @param array   $args
  *
  * @return array
  * @since 1.37.2
  * @author odokienko@UD
  */
 public static function revalidate_address($post_id, $args = array())
 {
     global $wp_properties;
     $args = wp_parse_args($args, array('skip_existing' => 'false', 'return_geo_data' => false));
     extract($args, EXTR_SKIP);
     $skip_existing = isset($skip_existing) ? $skip_existing : 'false';
     $return_geo_data = isset($return_geo_data) ? $return_geo_data : false;
     $return = array();
     $geo_data = false;
     $geo_data_coordinates = false;
     $latitude = get_post_meta($post_id, 'latitude', true);
     $longitude = get_post_meta($post_id, 'longitude', true);
     $current_coordinates = $latitude . $longitude;
     $address_is_formatted = get_post_meta($post_id, 'address_is_formatted', true);
     $address = get_post_meta($post_id, $wp_properties['configuration']['address_attribute'], true);
     $coordinates = empty($latitude) || empty($longitude) ? false : array('lat' => get_post_meta($post_id, 'latitude', true), 'lng' => get_post_meta($post_id, 'longitude', true));
     if ($skip_existing == 'true' && !empty($current_coordinates) && in_array($address_is_formatted, array('1', 'true'))) {
         $return['status'] = 'skipped';
         return $return;
     }
     if (!(empty($coordinates) && empty($address))) {
         /* will be true if address is empty and used manual_coordinates and coordinates is not empty */
         $manual_coordinates = get_post_meta($post_id, 'manual_coordinates', true);
         $manual_coordinates = $manual_coordinates != 'true' && $manual_coordinates != '1' ? false : true;
         $address_by_coordinates = !empty($coordinates) && $manual_coordinates && empty($address);
         if (!empty($coordinates) && $manual_coordinates) {
             $geo_data_coordinates = WPP_F::geo_locate_address($address, $wp_properties['configuration']['google_maps_localization'], true, $coordinates);
         } elseif (!empty($address)) {
             $geo_data = WPP_F::geo_locate_address($address, $wp_properties['configuration']['google_maps_localization'], true);
         }
         /** if Address was invalid or empty but we have valid $coordinates we use them */
         if (!empty($geo_data_coordinates->formatted_address) && ($address_by_coordinates || empty($geo_data->formatted_address))) {
             $geo_data = $geo_data_coordinates;
             /** clean up $address to remember that addres was empty or invalid*/
             $address = '';
         }
         if (empty($geo_data)) {
             $return['status'] = 'empty_address';
         }
     }
     if (!empty($geo_data->formatted_address)) {
         foreach ((array) $wp_properties['geo_type_attributes'] + array('display_address') as $meta_key) {
             delete_post_meta($post_id, $meta_key);
         }
         update_post_meta($post_id, 'address_is_formatted', true);
         if (!empty($wp_properties['configuration']['address_attribute']) && (!$manual_coordinates || $address_by_coordinates)) {
             update_post_meta($post_id, $wp_properties['configuration']['address_attribute'], $geo_data->formatted_address);
         }
         foreach ($geo_data as $geo_type => $this_data) {
             if (in_array($geo_type, (array) $wp_properties['geo_type_attributes']) && !in_array($geo_type, array('latitude', 'longitude'))) {
                 update_post_meta($post_id, $geo_type, $this_data);
             }
         }
         update_post_meta($post_id, 'wpp::last_address_validation', time());
         if ($manual_coordinates) {
             $lat = !empty($coordinates['lat']) ? $coordinates['lat'] : 0;
             $lng = !empty($coordinates['lng']) ? $coordinates['lng'] : 0;
         } else {
             $lat = $geo_data->latitude;
             $lng = $geo_data->longitude;
         }
         update_post_meta($post_id, 'latitude', $lat);
         update_post_meta($post_id, 'longitude', $lng);
         if ($return_geo_data) {
             $return['geo_data'] = $geo_data;
         }
         $return['status'] = 'updated';
     }
     //** Logs the last validation status for better troubleshooting */
     update_post_meta($post_id, 'wpp::google_validation_status', isset($geo_data->status) ? $geo_data->status : 'success');
     // Try to figure out what went wrong
     if (!empty($geo_data->status) && ($geo_data->status == 'OVER_QUERY_LIMIT' || $geo_data->status == 'REQUEST_DENIED')) {
         $return['status'] = 'over_query_limit';
     } elseif (empty($address) && empty($geo_data)) {
         foreach ((array) $wp_properties['geo_type_attributes'] + array('display_address') as $meta_key) {
             delete_post_meta($post_id, $meta_key);
         }
         $return['status'] = 'empty_address';
         update_post_meta($post_id, 'address_is_formatted', false);
     } elseif (empty($return['status'])) {
         $return['status'] = 'failed';
         update_post_meta($post_id, 'address_is_formatted', false);
     }
     //** Neccessary meta data which is required by Supermap Premium Feature. Should be always set even the Supermap disabled. peshkov@UD */
     if (!metadata_exists('post', $post_id, 'exclude_from_supermap')) {
         add_post_meta($post_id, 'exclude_from_supermap', 'false');
     }
     return $return;
 }