-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyGMaps.php
133 lines (102 loc) · 4.01 KB
/
EasyGMaps.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
Plugin Name: Easy GMaps
Plugin URI:
Description: Easily add an Google Map Embed to your Post.
Author: Emerson Carvalho < emersoncarvalho.com >
Version: 1.0
*/
//Admin Menu Page
//add_action('admin_menu', array('EasyGMaps', 'addMenuPage'));
//Register Metabox
add_action('add_meta_boxes', array('EasyGMaps', 'registerMetaBox') );
//Add javascript
add_action('wp_enqueue_scripts', array('EasyGMaps', 'loadScripts'));
add_action('admin_enqueue_scripts', array('EasyGMaps', 'loadScripts'));
//Register Shortcode
add_shortcode( 'easygmaps', array('EasyGMaps', 'renderShortCode') );
//Ajax for Save Api Key
add_action('wp_ajax_saveEasyGMapsApiKey', array('EasyGMaps', 'saveEasyGMapsApiKey'));
add_action('wp_ajax_nopriv_saveEasyGMapsApiKey', array('EasyGMaps', 'saveEasyGMapsApiKey'));
Class EasyGMaps
{
const name = 'Easy GMaps';
const slug = 'easy-g-maps';
protected $apiKey;
protected $path;
protected $url;
public function __construct()
{
$this->path = plugin_dir_path(__FILE__);
$this->url = plugins_url('', __FILE__ ).'/';
$this->apiKey = get_option(static::slug.'apikey');
}
public function controller()
{
echo $this->path . '<br/>';
echo $this->url . '<br/>';
// echo plugins_url( static::slug . '-icon.png' , __FILE__ );
}
public function registerMetaBox()
{
$functionCall = array(new EasyGMaps() , 'addEasyGMapsMetabox');
$functionSave = array(new EasyGMaps() , 'saveEasyGMapsMetabox');
$metaboxId = static::slug.'div';
$custom_post_types = get_post_types();
foreach ($custom_post_types as $type) {
add_meta_box($metaboxId, static::name , $functionCall, $type, 'normal');
}
add_meta_box($metaboxId, static::name , $functionCall, 'page', 'normal');
add_action('save_post', $functionSave);
}
public function addEasyGMapsMetabox()
{
require_once $this->path . 'form.php';
}
public function saveEasyGMapsApiKey()
{
$api_key = mysql_real_escape_string($_REQUEST[static::slug.'apikey']);
update_option(static::slug.'apikey', $api_key);
die();
}
public function loadScripts()
{
$egm = new EasyGMaps();
if( empty($egm->apiKey) || is_null($egm->apiKey) ){
$googleMapsJs = 'https://maps.google.com/maps/api/js?sensor=true';
}else{
$googleMapsJs = 'https://maps.google.com/maps/api/js?key='.$egm->apiKey.'&sensor=true';
}
wp_enqueue_script(
'gmaps',
$googleMapsJs
);
wp_enqueue_script(
static::slug,
$egm->url . static::slug . '.js',
array('jquery')
);
}
public function renderShortCode( $attr )
{
extract( shortcode_atts( array(
'width' => null,
'height'=> null,
'lat' => null,
'lng' => null,
'type' => null,
'zoom' => null,
'id' => null
), $attr ) );
if($type == '1'){
$html = '<style type="text/css">#'.$id.' img { max-width: none; }</style><!-- Some hack for Wordpress Twenty eleven, twelve theme -->';
$html.= '<div style="width: '.$width.'; height:'.$width.';">';
$html.= '<div class="'.static::slug.'embed" style="width: '.$width.'; height:100%;" id="'.$id.'" data-id="'.$id.'" data-lat="'.$lat.'" data-lng="'.$lng.'" data-zoom="'.$zoom.'" >Loagind Easy G Maps</div>';
$html.= '</div>';
return $html;
}else if($type == '2'){
$staticMapUrl = 'http://maps.googleapis.com/maps/api/staticmap?center='.$lat.','.$lng.'&zoom='.$zoom.'&size='.$width.'x'.$height.'&maptype=roadmap&markers=color:red%7C'.$lat.','.$lng.'&sensor=false';
return '<img src="'.$staticMapUrl.'" width="'.$width.'" height="'.$height.'" alt="Generated by EasyGMaps Plugin" id="'.$id.'" />';
}
}
}