forked from mustardBees/cmb_field_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmb-field-map.php
executable file
·204 lines (174 loc) · 6.78 KB
/
cmb-field-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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
Plugin Name: CMB2 Field Type: Google Maps
Plugin URI: https://github.com/spencerm/cmb_field_map
GitHub Plugin URI: https://github.com/spencerm/cmb_field_map
Description: Google Maps field type for CMB2.
Version: 2.3
Author: Phil Wylie & Gareth Gillman & Spencer McCormick
Author URI:
License: GPLv2+
*/
/**
* Class PW_CMB2_Field_Google_Maps
*/
class PW_CMB2_Field_Google_Maps {
/**
* Current version number
*/
const VERSION = '2.3.1';
/**
* Initialize the plugin by hooking into CMB2
*/
public function __construct() {
add_filter( 'cmb2_render_pw_map', array( $this, 'render_pw_map' ), 10, 5 );
add_filter( 'cmb2_sanitize_pw_map', array( $this, 'sanitize_pw_map' ), 10, 4 );
}
/**
* Render field
*/
public function render_pw_map( $field, $field_escaped_value, $field_object_id, $field_object_type, $field_type_object ) {
$this->setup_admin_scripts();
$api_key = get_option( 'cmb_field_map_settings_option_name' );
$api_key = $api_key['cmb_field_map_google_map_api'];
if(!empty($api_key)) {
$field_type_object->_desc( true, true );
echo $field_type_object->input( array(
'type' => 'text',
'name' => $field->args('_name') . '[address]',
'value' => isset( $field_escaped_value['address'] ) ? $field_escaped_value['address'] : '',
'class' => 'large-text pw-map-search',
'id' => $field->args( 'id' ),
'desc' => '',
) );
echo '<div class="pw-map"></div>';
$field_type_object->_desc( true, true );
echo $field_type_object->input(
array(
'type' => 'hidden',
'name' => $field->args('_name') . '[latitude]',
'value' => isset( $field_escaped_value['latitude'] ) ? $field_escaped_value['latitude'] : '',
'class' => 'pw-map-latitude',
'desc' => '',
)
);
echo $field_type_object->input(
array(
'type' => 'hidden',
'name' => $field->args('_name') . '[longitude]',
'value' => isset( $field_escaped_value['longitude'] ) ? $field_escaped_value['longitude'] : '',
'class' => 'pw-map-longitude',
'desc' => '',
)
);
} else {
echo '<div class="pw_map_notice">';
echo '<p>Please add your Google API Key <a href="'.get_admin_url().'options-general.php?page=cmb-field-map-settings">here</a></p>';
echo '</div>';
} // end API Key
}
/**
* Optionally save the latitude/longitude values into two custom fields
*/
public function sanitize_pw_map( $override_value, $value, $object_id, $field_args ) {
if ( isset( $field_args['split_values'] ) && $field_args['split_values'] ) {
if ( ! empty( $value['latitude'] ) ) {
update_post_meta( $object_id, $field_args['id'] . '_latitude', $value['latitude'] );
}
if ( ! empty( $value['longitude'] ) ) {
update_post_meta( $object_id, $field_args['id'] . '_longitude', $value['longitude'] );
}
}
if( ! empty( $value['address'] ) ) {
update_post_meta( $object_id, $field_args['id'] . '_address', $value['address'] );
}
return $value;
}
/**
* Enqueue scripts and styles
*/
public function setup_admin_scripts() {
$api_url = '//maps.googleapis.com/maps/api/js?libraries=places';
$api_key = get_option( 'cmb_field_map_settings_option_name' );
$api_key = $api_key['cmb_field_map_google_map_api'];
if ( ! empty( $api_key ) ) {
$api_url .= '&key=' . $api_key;
wp_register_script( 'pw-google-maps-api', $api_url, null, null );
}
wp_enqueue_script( 'pw-google-maps', plugins_url( 'js/script.js', __FILE__ ), array( 'pw-google-maps-api' ), self::VERSION );
wp_enqueue_style( 'pw-google-maps', plugins_url( 'css/style.css', __FILE__ ), array(), self::VERSION );
}
}
$pw_cmb2_field_google_maps = new PW_CMB2_Field_Google_Maps();
/**
* Generated by the WordPress Option Page generator
* at http://jeremyhixon.com/wp-tools/option-page/
*/
class CMBFieldMapSettings {
private $cmb_field_map_settings_options;
public function __construct() {
add_action( 'admin_menu', array( $this, 'cmb_field_map_settings_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'cmb_field_map_settings_page_init' ) );
}
public function cmb_field_map_settings_add_plugin_page() {
add_options_page(
'CMB Field Map Settings', // page_title
'CMB Field Map Settings', // menu_title
'manage_options', // capability
'cmb-field-map-settings', // menu_slug
array( $this, 'cmb_field_map_settings_create_admin_page' ) // function
);
}
public function cmb_field_map_settings_create_admin_page() {
$this->cmb_field_map_settings_options = get_option( 'cmb_field_map_settings_option_name' ); ?>
<div class="wrap">
<h2>CMB Field Map Settings</h2>
<p>Settings for the CMB Map Field Plugin</p>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'cmb_field_map_settings_option_group' );
do_settings_sections( 'cmb-field-map-settings-admin' );
submit_button();
?>
</form>
</div>
<?php }
public function cmb_field_map_settings_page_init() {
register_setting(
'cmb_field_map_settings_option_group', // option_group
'cmb_field_map_settings_option_name', // option_name
array( $this, 'cmb_field_map_settings_sanitize' ) // sanitize_callback
);
add_settings_section(
'cmb_field_map_settings_setting_section', // id
'Settings', // title
array( $this, 'cmb_field_map_settings_section_info' ), // callback
'cmb-field-map-settings-admin' // page
);
add_settings_field(
'cmb_field_map_google_map_api', // id
'Google Maps API Key', // title
array( $this, 'cmb_field_map_google_map_api_callback' ), // callback
'cmb-field-map-settings-admin', // page
'cmb_field_map_settings_setting_section' // section
);
}
public function cmb_field_map_settings_sanitize($input) {
$sanitary_values = array();
if ( isset( $input['cmb_field_map_google_map_api'] ) ) {
$sanitary_values['cmb_field_map_google_map_api'] = sanitize_text_field( $input['cmb_field_map_google_map_api'] );
}
return $sanitary_values;
}
public function cmb_field_map_settings_section_info() {
}
public function cmb_field_map_google_map_api_callback() {
printf(
'<input class="regular-text" type="text" name="cmb_field_map_settings_option_name[cmb_field_map_google_map_api]" id="cmb_field_map_google_map_api" value="%s">',
isset( $this->cmb_field_map_settings_options['cmb_field_map_google_map_api'] ) ? esc_attr( $this->cmb_field_map_settings_options['cmb_field_map_google_map_api']) : ''
);
}
}
if ( is_admin() )
$cmb_field_map_settings = new CMBFieldMapSettings();