Exemplo n.º 1
0
/**
 * Is Valid Geo Coord
 *
 * @todo This function can be deprecated.
 *
 * @param   float  $lat   Latitude.
 * @param   float  $long  Longitude.
 * @return  bool
 */
function wpgeo_is_valid_geo_coord($lat, $lng)
{
    $coord = new WPGeo_Coord($lat, $lng);
    return $coord->is_valid_coord();
}
Exemplo n.º 2
0
 /**
  * WP Geo Location Save post data
  * When the post is saved, saves our custom data.
  *
  * @todo Use update_post_meta() where appropriate, rather than always adding/deleting.
  *
  * @param int $post_id Post ID.
  */
 function wpgeo_location_save_postdata($post_id)
 {
     global $wpgeo;
     if (!$wpgeo->checkGoogleAPIKey()) {
         return;
     }
     // Verify this came from the our screen and with proper authorization,
     // because save_post can be triggered at other times
     if (!isset($_POST['wpgeo_location_noncename']) || !wp_verify_nonce($_POST['wpgeo_location_noncename'], plugin_basename('wpgeo_edit_post'))) {
         return $post_id;
     }
     // Authenticate user
     if ('page' == $_POST['post_type']) {
         if (!current_user_can('edit_page', $post_id)) {
             return $post_id;
         }
     } elseif ('post' == $_POST['post_type']) {
         if (!current_user_can('edit_post', $post_id)) {
             return $post_id;
         }
     } elseif (function_exists('get_post_type_object')) {
         $post_type = get_post_type_object($_POST['post_type']);
         // @todo Should this be "edit_" . $post_type->capability_type
         if (!current_user_can($post_type->cap->edit_post, $post_id)) {
             return $post_id;
         }
     }
     $mydata = array();
     // Find and save the location data
     if (isset($_POST['wp_geo_latitude']) && isset($_POST['wp_geo_longitude'])) {
         // Only delete post meta if isset (to avoid deletion in bulk/quick edit mode)
         delete_post_meta($post_id, WPGEO_LATITUDE_META);
         delete_post_meta($post_id, WPGEO_LONGITUDE_META);
         $coord = new WPGeo_Coord($_POST['wp_geo_latitude'], $_POST['wp_geo_longitude']);
         if ($coord->is_valid_coord()) {
             add_post_meta($post_id, WPGEO_LATITUDE_META, $coord->latitude());
             add_post_meta($post_id, WPGEO_LONGITUDE_META, $coord->longitude());
             $mydata[WPGEO_LATITUDE_META] = $coord->latitude();
             $mydata[WPGEO_LONGITUDE_META] = $coord->longitude();
         }
     }
     // Find and save the title data
     if (isset($_POST['wp_geo_title'])) {
         delete_post_meta($post_id, WPGEO_TITLE_META);
         if (!empty($_POST['wp_geo_title'])) {
             add_post_meta($post_id, WPGEO_TITLE_META, $_POST['wp_geo_title']);
             $mydata[WPGEO_TITLE_META] = $_POST['wp_geo_title'];
         }
     }
     // Find and save the marker data
     if (isset($_POST['wp_geo_marker'])) {
         if (!empty($_POST['wp_geo_marker'])) {
             update_post_meta($post_id, WPGEO_MARKER_META, $_POST['wp_geo_marker']);
             $mydata[WPGEO_MARKER_META] = $_POST['wp_geo_marker'];
         } else {
             delete_post_meta($post_id, WPGEO_MARKER_META);
         }
     }
     // Find and save the settings data
     delete_post_meta($post_id, WPGEO_MAP_SETTINGS_META);
     $settings = array();
     if (isset($_POST['wpgeo_map_settings_zoom']) && !empty($_POST['wpgeo_map_settings_zoom'])) {
         $settings['zoom'] = $_POST['wpgeo_map_settings_zoom'];
     }
     if (isset($_POST['wpgeo_map_settings_type']) && !empty($_POST['wpgeo_map_settings_type'])) {
         $settings['type'] = $wpgeo->decode_api_string($_POST['wpgeo_map_settings_type'], 'maptype');
     }
     if (isset($_POST['wpgeo_map_settings_centre']) && !empty($_POST['wpgeo_map_settings_centre'])) {
         $settings['centre'] = $_POST['wpgeo_map_settings_centre'];
     }
     add_post_meta($post_id, WPGEO_MAP_SETTINGS_META, $settings);
     $mydata[WPGEO_MAP_SETTINGS_META] = $settings;
     return $mydata;
 }
Exemplo n.º 3
0
    /**
     * Map Scripts Init
     * Output Javascripts to display maps in admin.
     *
     * @param object $coord WPGeo_Coord.
     * @param int $zoom Zoom.
     * @param bool $panel_open Admin panel open?
     * @param bool $hide_marker Hide marker?
     * @return string HTML content.
     */
    function mapScriptsInit($coord, $zoom = 5, $panel_open = false, $hide_marker = false)
    {
        global $wpgeo, $post;
        $wp_geo_options = get_option('wp_geo_options');
        $maptype = empty($wp_geo_options['google_map_type']) ? 'G_NORMAL_MAP' : $wp_geo_options['google_map_type'];
        // Centre on London
        if (!$coord->is_valid_coord()) {
            $coord = new WPGeo_Coord($wp_geo_options['default_map_latitude'], $wp_geo_options['default_map_longitude']);
            $zoom = $wp_geo_options['default_map_zoom'];
            $panel_open = true;
            $hide_marker = true;
        }
        $map_center_coord = new WPGeo_Coord($coord->latitude(), $coord->longitude());
        if (isset($post) && is_numeric($post->ID) && $post->ID > 0) {
            $settings = WPGeo::get_post_map_settings($post->ID);
            if (isset($settings['zoom']) && is_numeric($settings['zoom'])) {
                $zoom = $settings['zoom'];
            }
            if (!empty($settings['type'])) {
                $maptype = $settings['type'];
            }
            if (!empty($settings['centre'])) {
                $map_center = explode(',', $settings['centre']);
                if (count($map_center) == 2) {
                    $new_mapcentre_coord = new WPGeo_Coord($map_center[0], $map_center[1]);
                    if ($new_mapcentre_coord->is_valid_coord()) {
                        $map_center_coord = $new_mapcentre_coord;
                    }
                }
            }
        }
        // Vars
        $panel_open = !$hide_marker || $panel_open ? '.removeClass("closed")' : '';
        $wpgeo_admin_vars = array('api' => $this->get_api_string(), 'map_dom_id' => $this->admin->map->get_dom_id(), 'map' => null, 'marker' => null, 'zoom' => intval($zoom), 'mapCentreX' => $map_center_coord->latitude(), 'mapCentreY' => $map_center_coord->longitude(), 'latitude' => $coord->latitude(), 'longitude' => $coord->longitude(), 'hideMarker' => absint($hide_marker));
        // Script
        // @todo Maps API needs changing
        return '
			<script type="text/javascript">
			var WPGeo_Admin = ' . json_encode($wpgeo_admin_vars) . ';
			WPGeo_Admin.mapType = ' . $this->api_string($maptype, 'maptype') . ';
			
			jQuery(document).ready(function($) {
				$("#wpgeo_location")' . $panel_open . '.bind("WPGeo_adminPostMapReady", function(e){
					' . apply_filters('wpgeo_map_js_preoverlays', '', 'WPGeo_Admin.map') . '
				});
			});
			</script>
			';
    }
Exemplo n.º 4
0
/**
 * Get WP Geo Post Static Map
 * Gets the HTML for a static post map.
 *
 * @param   int    $post_id  (optional) Post ID.
 * @param   array  $query    (optional) Parameters.
 * @return  string           HTML.
 */
function get_wpgeo_post_static_map($post_id = 0, $query = null)
{
    global $post, $wpgeo;
    $post_id = absint($post_id);
    $post_id = $post_id > 0 ? $post_id : $post->ID;
    // Show Map?
    if (!$post_id || is_feed() || !$wpgeo->show_maps() || !$wpgeo->checkGoogleAPIKey()) {
        return '';
    }
    $coord = get_wpgeo_post_coord($post_id);
    if (!$coord->is_valid_coord()) {
        return '';
    }
    // Fetch wp geo options & post settings
    $wp_geo_options = get_option('wp_geo_options');
    $settings = WPGeo::get_post_map_settings($post_id);
    // Options
    $options = wp_parse_args($query, array('width' => trim($wp_geo_options['default_map_width'], 'px'), 'height' => trim($wp_geo_options['default_map_height'], 'px'), 'maptype' => $wp_geo_options['google_map_type'], 'zoom' => $wp_geo_options['default_map_zoom']));
    // Can't do percentage sizes so abort
    if (strpos($options['width'], '%') !== false || strpos($options['height'], '%') !== false) {
        return '';
    }
    // Map Options
    $zoom = isset($settings['zoom']) && is_numeric($settings['zoom']) ? $settings['zoom'] : $options['zoom'];
    $map_type = !empty($settings['type']) ? $settings['type'] : $options['maptype'];
    $center_coord = new WPGeo_Coord($coord->latitude(), $coord->longitude());
    if (!empty($settings['centre'])) {
        $center = explode(',', $settings['centre']);
        $maybe_center_coord = new WPGeo_Coord($center[0], $center[1]);
        if ($maybe_center_coord->is_valid_coord()) {
            $center_coord = $maybe_center_coord;
        }
    }
    // Map
    $map = new WPGeo_Map();
    $map->set_map_centre($center_coord);
    $map->set_map_zoom($zoom);
    $map->set_map_type($map_type);
    $map->set_size($options['width'], $options['height']);
    $map->add_point($coord);
    $url = $wpgeo->api->static_map_url($map);
    return sprintf('<img id="wp_geo_static_map_%s" src="%s" class="wp_geo_static_map" />', $post_id, esc_url($url));
}