1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* WP Google Maps API |
4
|
|
|
* |
5
|
|
|
* @package WP-Google-Maps-API |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/* |
9
|
|
|
* Plugin Name: WP Google Maps API |
10
|
|
|
* Plugin URI: https://github.com/wp-api-libraries/wp-google-maps-api |
11
|
|
|
* Description: Perform API requests to Google Maps API. |
12
|
|
|
* Author: WP API Libraries, sgarza |
13
|
|
|
* Version: 1.0.0 |
14
|
|
|
* Author URI: https://wp-api-libraries.com |
15
|
|
|
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-google-maps-api |
16
|
|
|
* GitHub Branch: master |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
// Exit if accessed directly. |
20
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; } |
21
|
|
|
if ( ! class_exists( 'WPAPI_GOOGLE_MAPS' ) ) { |
22
|
|
|
require_once( 'maps-widget.php' ); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Google Maps Class. |
26
|
|
|
*/ |
27
|
|
|
class WPAPI_GOOGLE_MAPS { |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* API Key. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
static private $api_key; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Output Type. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
static private $output; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Map data to be sent to JS. |
45
|
|
|
* |
46
|
|
|
* @var [Array] |
47
|
|
|
*/ |
48
|
|
|
static private $map_data; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Default map options. |
52
|
|
|
* |
53
|
|
|
* @var [Array] |
54
|
|
|
*/ |
55
|
|
|
static public $defaults = array( |
56
|
|
|
'width' => '300px', |
57
|
|
|
'height' => '300px', |
58
|
|
|
'lat' => '-17.7134', |
59
|
|
|
'lng' => '178.0650', |
60
|
|
|
'info' => '', |
61
|
|
|
'style' => '[]', |
62
|
|
|
'zoom' => 14, |
63
|
|
|
'scrollwheel' => 0, |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* __construct function. |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* @param String $api_key : API Key. |
71
|
|
|
*/ |
72
|
|
|
public function __construct( $api_key ) { |
73
|
|
|
static::$api_key = $api_key; |
|
|
|
|
74
|
|
|
|
75
|
|
|
add_action( 'wp_footer', array( $this, 'footer' ), 11 ); |
76
|
|
|
add_action( 'widgets_init', array( $this, 'register_widgets' ) ); |
77
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); |
78
|
|
|
add_shortcode( 'wp_google_maps', array( $this, 'shortcode' ) ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Enqueue JS. |
83
|
|
|
*/ |
84
|
|
|
public function enqueue() { |
85
|
|
|
wp_enqueue_script( 'wpapi-google-maps', plugins_url( 'assets/js/google-maps.min.js', REPRO_PLUGIN_FILE ), array( 'jquery' ), null, true ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Handle multiple google maps js api enqueues on the footer. |
90
|
|
|
*/ |
91
|
|
|
public function footer() { |
92
|
|
|
wp_localize_script( 'wpapi-google-maps', 'wpapi_gmaps', static::$map_data ); |
|
|
|
|
93
|
|
|
|
94
|
|
|
// Only enqueue google maps API if yoast hasnt enqueued it already. |
95
|
|
|
if ( ! wp_script_is( 'maps-geocoder' ) ) { |
96
|
|
|
wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . static::$api_key, array(), null ); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Print dat map. |
102
|
|
|
* |
103
|
|
|
* @param [Mixed] $map_data : Array of map data to send to js. |
|
|
|
|
104
|
|
|
* @param [Bool] $echo : If html should be returned or echoed, defaults to true. |
|
|
|
|
105
|
|
|
*/ |
106
|
|
|
public static function print_map( $map_data, $echo = true ) { |
107
|
|
|
|
108
|
|
|
$map_data = apply_filters( 'wpapi_google_map_data', wp_parse_args( $map_data, static::$defaults ) ); |
109
|
|
|
static::$map_data[] = $map_data; |
|
|
|
|
110
|
|
|
|
111
|
|
|
$index = count( static::$map_data ) - 1; |
|
|
|
|
112
|
|
|
|
113
|
|
|
$html = '<div id="wpapi-gmap-' . $index . '" style="width:' . esc_attr( $map_data['width'] ) . ';height:' . esc_attr( $map_data['height'] ) . '"></div>'; |
114
|
|
|
|
115
|
|
|
if ( $echo ) { |
116
|
|
|
echo $html; |
117
|
|
|
} else { |
118
|
|
|
return $html; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Shortcode for printing a single map. |
124
|
|
|
* |
125
|
|
|
* @param [type] $atts : shortcode attributes. |
|
|
|
|
126
|
|
|
*/ |
127
|
|
|
public function shortcode( $atts ) { |
128
|
|
|
// Set widget info. |
129
|
|
|
$atts = shortcode_atts( static::$defaults, $atts, 'wp_google_maps' ); |
130
|
|
|
|
131
|
|
|
return static::print_map( $atts, false ); |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Register Google maps Widgets. |
137
|
|
|
* |
138
|
|
|
* @access public |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function register_widgets() { |
142
|
|
|
register_widget( 'WP_API_MAPS_WIDGET' ); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.