forked from miya0001/simple-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-map.php
168 lines (149 loc) · 4.24 KB
/
simple-map.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
Plugin Name: Simple Map
Author: Takayuki Miyauchi
Plugin URI: https://github.com/miya0001/simple-map
Description: Insert google map convert from address.
Version: 2.3.0
Author URI: http://wpist.me/
Domain Path: /languages
Text Domain: simplemap
*/
$simplemap = new Simple_Map();
class Simple_Map {
private $shortcode_tag = 'map';
private $class_name = 'simplemap';
private $width = '100%';
private $height = '200px';
private $zoom = 16;
private $breakpoint = 480;
private $max_breakpoint = 640;
function __construct()
{
add_action( 'init', array( $this, 'init' ) );
}
public function init()
{
add_action( 'wp_head', array( $this, 'wp_head' ) );
add_shortcode( $this->get_shortcode_tag(), array( $this, 'shortcode' ) );
wp_embed_register_handler(
'google-map',
'#( https://( www|maps ).google.[a-z]{2,3}\.?[a-z]{0,3}/maps( /ms )?\?.+ )#i',
array( &$this, 'oembed_handler' )
);
}
public function oembed_handler( $match )
{
return sprintf(
'[%s url="%s"]',
$this->get_shortcode_tag(),
esc_url( $match[0] )
);
}
public function wp_head()
{
echo "<style>.simplemap img{max-width:none !important;padding:0 !important;margin:0 !important;}.staticmap,.staticmap img{max-width:100% !important;height:auto !important;}.simplemap .simplemap-content{display:none;}</style>\n";
}
public function wp_enqueue_scripts()
{
wp_register_script(
'google-maps-api',
'//maps.google.com/maps/api/js?sensor=false',
false,
null,
true
);
wp_register_script(
'simplemap',
apply_filters(
'simplemap_script',
plugins_url( 'js/simple-map.min.js' , __FILE__ )
),
array( 'jquery', 'google-maps-api' ),
filemtime( dirname( __FILE__ ).'/js/simple-map.min.js' ),
true
);
wp_enqueue_script( 'simplemap' );
}
public function shortcode( $p, $content = null )
{
add_action( 'wp_footer', array( &$this, 'wp_enqueue_scripts' ) );
if ( isset( $p['width'] ) && preg_match( '/^[0-9]+(%|px)$/', $p['width'] ) ) {
$w = $p['width'];
} else {
$w = apply_filters( 'simplemap_default_width', $this->width );
}
if ( isset( $p['height'] ) && preg_match( '/^[0-9]+(%|px)$/', $p['height'] ) ) {
$h = $p['height'];
} else {
$h = apply_filters( 'simplemap_default_height', $this->height );
}
if ( isset( $p['zoom'] ) && intval( $p['zoom'] ) ) {
$zoom = $p['zoom'];
} else {
$zoom = apply_filters( 'simplemap_default_zoom', $this->zoom );
}
if ( isset( $p['breakpoint'] ) && intval( $p['breakpoint'] ) ) {
if ( intval( $p['breakpoint'] ) > $this->max_breakpoint ) {
$breakpoint = $this->max_breakpoint;
} else {
$breakpoint = intval( $p['breakpoint'] );
}
} else {
$breakpoint = apply_filters(
'simplemap_default_breakpoint',
$this->breakpoint
);
}
if ( $content ) {
$content = do_shortcode( $content );
}
if ( isset( $p['infowindow'] ) && $p['infowindow'] ) {
$infowindow = $p['infowindow'];
} else {
$infowindow = apply_filters( 'simplemap_default_infowindow', 'close' );
}
$addr = '';
$lat = '';
$lng = '';
if ( isset( $p['url'] ) && $p['url'] ) {
$iframe = '<iframe width="%s" height="%s" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="%s"></iframe>';
return sprintf(
$iframe,
$w,
$h,
esc_url( $p['url'].'&output=embed' )
);
} elseif ( isset( $p['lat'] ) && preg_match( '/^\-?[0-9\.]+$/', $p['lat'] )
&& isset( $p['lng'] ) && preg_match( '/^\-?[0-9\.]+$/', $p['lng'] ) ){
$lat = $p['lat'];
$lng = $p['lng'];
} elseif ( isset( $p['addr'] ) && $p['addr'] ) {
if ( $content ) {
$addr = esc_html( $p['addr'] );
} else {
$content = esc_html( $p['addr'] );
}
} elseif ( ! $content ) {
return;
}
return sprintf(
'<div class="%1$s"><div class="%1$s-content" data-breakpoint="%2$s" data-lat="%3$s" data-lng="%4$s" data-zoom="%5$s" data-addr="%6$s" data-infowindow="%7$s" style="width:%8$s;height:%9$s;">%10$s</div></div>',
apply_filters( 'simplemap_class_name', $this->class_name ),
$breakpoint,
$lat,
$lng,
$zoom,
$addr,
$infowindow,
$w,
$h,
trim( $content )
);
}
private function get_shortcode_tag()
{
return apply_filters( 'simplemap_shortcode_tag', $this->shortcode_tag );
}
} // end class
// EOF